bfd/
[binutils.git] / bfd / elfxx-mips.c
blob00f33bd64ec6bce3ba5b464d02adde81184e8ea8
1 /* MIPS-specific support for ELF
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
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>
12 This file is part of BFD, the Binary File Descriptor library.
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 3 of the License, or
17 (at your option) any later version.
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.
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,
27 MA 02110-1301, USA. */
30 /* This file handles functionality common to the different MIPS ABI's. */
32 #include "sysdep.h"
33 #include "bfd.h"
34 #include "libbfd.h"
35 #include "libiberty.h"
36 #include "elf-bfd.h"
37 #include "elfxx-mips.h"
38 #include "elf/mips.h"
39 #include "elf-vxworks.h"
41 /* Get the ECOFF swapping routines. */
42 #include "coff/sym.h"
43 #include "coff/symconst.h"
44 #include "coff/ecoff.h"
45 #include "coff/mips.h"
47 #include "hashtab.h"
49 /* This structure is used to hold information about one GOT entry.
50 There are three types of entry:
52 (1) absolute addresses
53 (abfd == NULL)
54 (2) SYMBOL + OFFSET addresses, where SYMBOL is local to an input bfd
55 (abfd != NULL, symndx >= 0)
56 (3) global and forced-local symbols
57 (abfd != NULL, symndx == -1)
59 Type (3) entries are treated differently for different types of GOT.
60 In the "master" GOT -- i.e. the one that describes every GOT
61 reference needed in the link -- the mips_got_entry is keyed on both
62 the symbol and the input bfd that references it. If it turns out
63 that we need multiple GOTs, we can then use this information to
64 create separate GOTs for each input bfd.
66 However, we want each of these separate GOTs to have at most one
67 entry for a given symbol, so their type (3) entries are keyed only
68 on the symbol. The input bfd given by the "abfd" field is somewhat
69 arbitrary in this case.
71 This means that when there are multiple GOTs, each GOT has a unique
72 mips_got_entry for every symbol within it. We can therefore use the
73 mips_got_entry fields (tls_type and gotidx) to track the symbol's
74 GOT index.
76 However, if it turns out that we need only a single GOT, we continue
77 to use the master GOT to describe it. There may therefore be several
78 mips_got_entries for the same symbol, each with a different input bfd.
79 We want to make sure that each symbol gets a unique GOT entry, so when
80 there's a single GOT, we use the symbol's hash entry, not the
81 mips_got_entry fields, to track a symbol's GOT index. */
82 struct mips_got_entry
84 /* The input bfd in which the symbol is defined. */
85 bfd *abfd;
86 /* The index of the symbol, as stored in the relocation r_info, if
87 we have a local symbol; -1 otherwise. */
88 long symndx;
89 union
91 /* If abfd == NULL, an address that must be stored in the got. */
92 bfd_vma address;
93 /* If abfd != NULL && symndx != -1, the addend of the relocation
94 that should be added to the symbol value. */
95 bfd_vma addend;
96 /* If abfd != NULL && symndx == -1, the hash table entry
97 corresponding to a global symbol in the got (or, local, if
98 h->forced_local). */
99 struct mips_elf_link_hash_entry *h;
100 } d;
102 /* The TLS types included in this GOT entry (specifically, GD and
103 IE). The GD and IE flags can be added as we encounter new
104 relocations. LDM can also be set; it will always be alone, not
105 combined with any GD or IE flags. An LDM GOT entry will be
106 a local symbol entry with r_symndx == 0. */
107 unsigned char tls_type;
109 /* The offset from the beginning of the .got section to the entry
110 corresponding to this symbol+addend. If it's a global symbol
111 whose offset is yet to be decided, it's going to be -1. */
112 long gotidx;
115 /* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
116 The structures form a non-overlapping list that is sorted by increasing
117 MIN_ADDEND. */
118 struct mips_got_page_range
120 struct mips_got_page_range *next;
121 bfd_signed_vma min_addend;
122 bfd_signed_vma max_addend;
125 /* This structure describes the range of addends that are applied to page
126 relocations against a given symbol. */
127 struct mips_got_page_entry
129 /* The input bfd in which the symbol is defined. */
130 bfd *abfd;
131 /* The index of the symbol, as stored in the relocation r_info. */
132 long symndx;
133 /* The ranges for this page entry. */
134 struct mips_got_page_range *ranges;
135 /* The maximum number of page entries needed for RANGES. */
136 bfd_vma num_pages;
139 /* This structure is used to hold .got information when linking. */
141 struct mips_got_info
143 /* The global symbol in the GOT with the lowest index in the dynamic
144 symbol table. */
145 struct elf_link_hash_entry *global_gotsym;
146 /* The number of global .got entries. */
147 unsigned int global_gotno;
148 /* The number of .got slots used for TLS. */
149 unsigned int tls_gotno;
150 /* The first unused TLS .got entry. Used only during
151 mips_elf_initialize_tls_index. */
152 unsigned int tls_assigned_gotno;
153 /* The number of local .got entries, eventually including page entries. */
154 unsigned int local_gotno;
155 /* The maximum number of page entries needed. */
156 unsigned int page_gotno;
157 /* The number of local .got entries we have used. */
158 unsigned int assigned_gotno;
159 /* A hash table holding members of the got. */
160 struct htab *got_entries;
161 /* A hash table of mips_got_page_entry structures. */
162 struct htab *got_page_entries;
163 /* A hash table mapping input bfds to other mips_got_info. NULL
164 unless multi-got was necessary. */
165 struct htab *bfd2got;
166 /* In multi-got links, a pointer to the next got (err, rather, most
167 of the time, it points to the previous got). */
168 struct mips_got_info *next;
169 /* This is the GOT index of the TLS LDM entry for the GOT, MINUS_ONE
170 for none, or MINUS_TWO for not yet assigned. This is needed
171 because a single-GOT link may have multiple hash table entries
172 for the LDM. It does not get initialized in multi-GOT mode. */
173 bfd_vma tls_ldm_offset;
176 /* Map an input bfd to a got in a multi-got link. */
178 struct mips_elf_bfd2got_hash {
179 bfd *bfd;
180 struct mips_got_info *g;
183 /* Structure passed when traversing the bfd2got hash table, used to
184 create and merge bfd's gots. */
186 struct mips_elf_got_per_bfd_arg
188 /* A hashtable that maps bfds to gots. */
189 htab_t bfd2got;
190 /* The output bfd. */
191 bfd *obfd;
192 /* The link information. */
193 struct bfd_link_info *info;
194 /* A pointer to the primary got, i.e., the one that's going to get
195 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
196 DT_MIPS_GOTSYM. */
197 struct mips_got_info *primary;
198 /* A non-primary got we're trying to merge with other input bfd's
199 gots. */
200 struct mips_got_info *current;
201 /* The maximum number of got entries that can be addressed with a
202 16-bit offset. */
203 unsigned int max_count;
204 /* The maximum number of page entries needed by each got. */
205 unsigned int max_pages;
206 /* The total number of global entries which will live in the
207 primary got and be automatically relocated. This includes
208 those not referenced by the primary GOT but included in
209 the "master" GOT. */
210 unsigned int global_count;
213 /* Another structure used to pass arguments for got entries traversal. */
215 struct mips_elf_set_global_got_offset_arg
217 struct mips_got_info *g;
218 int value;
219 unsigned int needed_relocs;
220 struct bfd_link_info *info;
223 /* A structure used to count TLS relocations or GOT entries, for GOT
224 entry or ELF symbol table traversal. */
226 struct mips_elf_count_tls_arg
228 struct bfd_link_info *info;
229 unsigned int needed;
232 struct _mips_elf_section_data
234 struct bfd_elf_section_data elf;
235 union
237 struct mips_got_info *got_info;
238 bfd_byte *tdata;
239 } u;
242 #define mips_elf_section_data(sec) \
243 ((struct _mips_elf_section_data *) elf_section_data (sec))
245 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
246 the dynamic symbols. */
248 struct mips_elf_hash_sort_data
250 /* The symbol in the global GOT with the lowest dynamic symbol table
251 index. */
252 struct elf_link_hash_entry *low;
253 /* The least dynamic symbol table index corresponding to a non-TLS
254 symbol with a GOT entry. */
255 long min_got_dynindx;
256 /* The greatest dynamic symbol table index corresponding to a symbol
257 with a GOT entry that is not referenced (e.g., a dynamic symbol
258 with dynamic relocations pointing to it from non-primary GOTs). */
259 long max_unref_got_dynindx;
260 /* The greatest dynamic symbol table index not corresponding to a
261 symbol without a GOT entry. */
262 long max_non_got_dynindx;
265 /* The MIPS ELF linker needs additional information for each symbol in
266 the global hash table. */
268 struct mips_elf_link_hash_entry
270 struct elf_link_hash_entry root;
272 /* External symbol information. */
273 EXTR esym;
275 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
276 this symbol. */
277 unsigned int possibly_dynamic_relocs;
279 /* If the R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 reloc is against
280 a readonly section. */
281 bfd_boolean readonly_reloc;
283 /* We must not create a stub for a symbol that has relocations
284 related to taking the function's address, i.e. any but
285 R_MIPS_CALL*16 ones -- see "MIPS ABI Supplement, 3rd Edition",
286 p. 4-20. */
287 bfd_boolean no_fn_stub;
289 /* If there is a stub that 32 bit functions should use to call this
290 16 bit function, this points to the section containing the stub. */
291 asection *fn_stub;
293 /* Whether we need the fn_stub; this is set if this symbol appears
294 in any relocs other than a 16 bit call. */
295 bfd_boolean need_fn_stub;
297 /* If there is a stub that 16 bit functions should use to call this
298 32 bit function, this points to the section containing the stub. */
299 asection *call_stub;
301 /* This is like the call_stub field, but it is used if the function
302 being called returns a floating point value. */
303 asection *call_fp_stub;
305 /* Are we forced local? This will only be set if we have converted
306 the initial global GOT entry to a local GOT entry. */
307 bfd_boolean forced_local;
309 /* Are we referenced by some kind of relocation? */
310 bfd_boolean is_relocation_target;
312 /* Are we referenced by branch relocations? */
313 bfd_boolean is_branch_target;
315 #define GOT_NORMAL 0
316 #define GOT_TLS_GD 1
317 #define GOT_TLS_LDM 2
318 #define GOT_TLS_IE 4
319 #define GOT_TLS_OFFSET_DONE 0x40
320 #define GOT_TLS_DONE 0x80
321 unsigned char tls_type;
322 /* This is only used in single-GOT mode; in multi-GOT mode there
323 is one mips_got_entry per GOT entry, so the offset is stored
324 there. In single-GOT mode there may be many mips_got_entry
325 structures all referring to the same GOT slot. It might be
326 possible to use root.got.offset instead, but that field is
327 overloaded already. */
328 bfd_vma tls_got_offset;
331 /* MIPS ELF linker hash table. */
333 struct mips_elf_link_hash_table
335 struct elf_link_hash_table root;
336 #if 0
337 /* We no longer use this. */
338 /* String section indices for the dynamic section symbols. */
339 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
340 #endif
341 /* The number of .rtproc entries. */
342 bfd_size_type procedure_count;
343 /* The size of the .compact_rel section (if SGI_COMPAT). */
344 bfd_size_type compact_rel_size;
345 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
346 entry is set to the address of __rld_obj_head as in IRIX5. */
347 bfd_boolean use_rld_obj_head;
348 /* This is the value of the __rld_map or __rld_obj_head symbol. */
349 bfd_vma rld_value;
350 /* This is set if we see any mips16 stub sections. */
351 bfd_boolean mips16_stubs_seen;
352 /* True if we've computed the size of the GOT. */
353 bfd_boolean computed_got_sizes;
354 /* True if we're generating code for VxWorks. */
355 bfd_boolean is_vxworks;
356 /* True if we already reported the small-data section overflow. */
357 bfd_boolean small_data_overflow_reported;
358 /* Shortcuts to some dynamic sections, or NULL if they are not
359 being used. */
360 asection *srelbss;
361 asection *sdynbss;
362 asection *srelplt;
363 asection *srelplt2;
364 asection *sgotplt;
365 asection *splt;
366 /* The size of the PLT header in bytes (VxWorks only). */
367 bfd_vma plt_header_size;
368 /* The size of a PLT entry in bytes (VxWorks only). */
369 bfd_vma plt_entry_size;
370 /* The size of a function stub entry in bytes. */
371 bfd_vma function_stub_size;
374 #define TLS_RELOC_P(r_type) \
375 (r_type == R_MIPS_TLS_DTPMOD32 \
376 || r_type == R_MIPS_TLS_DTPMOD64 \
377 || r_type == R_MIPS_TLS_DTPREL32 \
378 || r_type == R_MIPS_TLS_DTPREL64 \
379 || r_type == R_MIPS_TLS_GD \
380 || r_type == R_MIPS_TLS_LDM \
381 || r_type == R_MIPS_TLS_DTPREL_HI16 \
382 || r_type == R_MIPS_TLS_DTPREL_LO16 \
383 || r_type == R_MIPS_TLS_GOTTPREL \
384 || r_type == R_MIPS_TLS_TPREL32 \
385 || r_type == R_MIPS_TLS_TPREL64 \
386 || r_type == R_MIPS_TLS_TPREL_HI16 \
387 || r_type == R_MIPS_TLS_TPREL_LO16)
389 /* Structure used to pass information to mips_elf_output_extsym. */
391 struct extsym_info
393 bfd *abfd;
394 struct bfd_link_info *info;
395 struct ecoff_debug_info *debug;
396 const struct ecoff_debug_swap *swap;
397 bfd_boolean failed;
400 /* The names of the runtime procedure table symbols used on IRIX5. */
402 static const char * const mips_elf_dynsym_rtproc_names[] =
404 "_procedure_table",
405 "_procedure_string_table",
406 "_procedure_table_size",
407 NULL
410 /* These structures are used to generate the .compact_rel section on
411 IRIX5. */
413 typedef struct
415 unsigned long id1; /* Always one? */
416 unsigned long num; /* Number of compact relocation entries. */
417 unsigned long id2; /* Always two? */
418 unsigned long offset; /* The file offset of the first relocation. */
419 unsigned long reserved0; /* Zero? */
420 unsigned long reserved1; /* Zero? */
421 } Elf32_compact_rel;
423 typedef struct
425 bfd_byte id1[4];
426 bfd_byte num[4];
427 bfd_byte id2[4];
428 bfd_byte offset[4];
429 bfd_byte reserved0[4];
430 bfd_byte reserved1[4];
431 } Elf32_External_compact_rel;
433 typedef struct
435 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
436 unsigned int rtype : 4; /* Relocation types. See below. */
437 unsigned int dist2to : 8;
438 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
439 unsigned long konst; /* KONST field. See below. */
440 unsigned long vaddr; /* VADDR to be relocated. */
441 } Elf32_crinfo;
443 typedef struct
445 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
446 unsigned int rtype : 4; /* Relocation types. See below. */
447 unsigned int dist2to : 8;
448 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
449 unsigned long konst; /* KONST field. See below. */
450 } Elf32_crinfo2;
452 typedef struct
454 bfd_byte info[4];
455 bfd_byte konst[4];
456 bfd_byte vaddr[4];
457 } Elf32_External_crinfo;
459 typedef struct
461 bfd_byte info[4];
462 bfd_byte konst[4];
463 } Elf32_External_crinfo2;
465 /* These are the constants used to swap the bitfields in a crinfo. */
467 #define CRINFO_CTYPE (0x1)
468 #define CRINFO_CTYPE_SH (31)
469 #define CRINFO_RTYPE (0xf)
470 #define CRINFO_RTYPE_SH (27)
471 #define CRINFO_DIST2TO (0xff)
472 #define CRINFO_DIST2TO_SH (19)
473 #define CRINFO_RELVADDR (0x7ffff)
474 #define CRINFO_RELVADDR_SH (0)
476 /* A compact relocation info has long (3 words) or short (2 words)
477 formats. A short format doesn't have VADDR field and relvaddr
478 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
479 #define CRF_MIPS_LONG 1
480 #define CRF_MIPS_SHORT 0
482 /* There are 4 types of compact relocation at least. The value KONST
483 has different meaning for each type:
485 (type) (konst)
486 CT_MIPS_REL32 Address in data
487 CT_MIPS_WORD Address in word (XXX)
488 CT_MIPS_GPHI_LO GP - vaddr
489 CT_MIPS_JMPAD Address to jump
492 #define CRT_MIPS_REL32 0xa
493 #define CRT_MIPS_WORD 0xb
494 #define CRT_MIPS_GPHI_LO 0xc
495 #define CRT_MIPS_JMPAD 0xd
497 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
498 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
499 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
500 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
502 /* The structure of the runtime procedure descriptor created by the
503 loader for use by the static exception system. */
505 typedef struct runtime_pdr {
506 bfd_vma adr; /* Memory address of start of procedure. */
507 long regmask; /* Save register mask. */
508 long regoffset; /* Save register offset. */
509 long fregmask; /* Save floating point register mask. */
510 long fregoffset; /* Save floating point register offset. */
511 long frameoffset; /* Frame size. */
512 short framereg; /* Frame pointer register. */
513 short pcreg; /* Offset or reg of return pc. */
514 long irpss; /* Index into the runtime string table. */
515 long reserved;
516 struct exception_info *exception_info;/* Pointer to exception array. */
517 } RPDR, *pRPDR;
518 #define cbRPDR sizeof (RPDR)
519 #define rpdNil ((pRPDR) 0)
521 static struct mips_got_entry *mips_elf_create_local_got_entry
522 (bfd *, struct bfd_link_info *, bfd *, struct mips_got_info *, asection *,
523 bfd_vma, unsigned long, struct mips_elf_link_hash_entry *, int);
524 static bfd_boolean mips_elf_sort_hash_table_f
525 (struct mips_elf_link_hash_entry *, void *);
526 static bfd_vma mips_elf_high
527 (bfd_vma);
528 static bfd_boolean mips_elf_create_dynamic_relocation
529 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
530 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
531 bfd_vma *, asection *);
532 static hashval_t mips_elf_got_entry_hash
533 (const void *);
534 static bfd_vma mips_elf_adjust_gp
535 (bfd *, struct mips_got_info *, bfd *);
536 static struct mips_got_info *mips_elf_got_for_ibfd
537 (struct mips_got_info *, bfd *);
539 /* This will be used when we sort the dynamic relocation records. */
540 static bfd *reldyn_sorting_bfd;
542 /* Nonzero if ABFD is using the N32 ABI. */
543 #define ABI_N32_P(abfd) \
544 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
546 /* Nonzero if ABFD is using the N64 ABI. */
547 #define ABI_64_P(abfd) \
548 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
550 /* Nonzero if ABFD is using NewABI conventions. */
551 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
553 /* The IRIX compatibility level we are striving for. */
554 #define IRIX_COMPAT(abfd) \
555 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
557 /* Whether we are trying to be compatible with IRIX at all. */
558 #define SGI_COMPAT(abfd) \
559 (IRIX_COMPAT (abfd) != ict_none)
561 /* The name of the options section. */
562 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
563 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
565 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
566 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
567 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
568 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
570 /* Whether the section is readonly. */
571 #define MIPS_ELF_READONLY_SECTION(sec) \
572 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
573 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
575 /* The name of the stub section. */
576 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
578 /* The size of an external REL relocation. */
579 #define MIPS_ELF_REL_SIZE(abfd) \
580 (get_elf_backend_data (abfd)->s->sizeof_rel)
582 /* The size of an external RELA relocation. */
583 #define MIPS_ELF_RELA_SIZE(abfd) \
584 (get_elf_backend_data (abfd)->s->sizeof_rela)
586 /* The size of an external dynamic table entry. */
587 #define MIPS_ELF_DYN_SIZE(abfd) \
588 (get_elf_backend_data (abfd)->s->sizeof_dyn)
590 /* The size of a GOT entry. */
591 #define MIPS_ELF_GOT_SIZE(abfd) \
592 (get_elf_backend_data (abfd)->s->arch_size / 8)
594 /* The size of a symbol-table entry. */
595 #define MIPS_ELF_SYM_SIZE(abfd) \
596 (get_elf_backend_data (abfd)->s->sizeof_sym)
598 /* The default alignment for sections, as a power of two. */
599 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
600 (get_elf_backend_data (abfd)->s->log_file_align)
602 /* Get word-sized data. */
603 #define MIPS_ELF_GET_WORD(abfd, ptr) \
604 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
606 /* Put out word-sized data. */
607 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
608 (ABI_64_P (abfd) \
609 ? bfd_put_64 (abfd, val, ptr) \
610 : bfd_put_32 (abfd, val, ptr))
612 /* Add a dynamic symbol table-entry. */
613 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
614 _bfd_elf_add_dynamic_entry (info, tag, val)
616 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
617 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
619 /* Determine whether the internal relocation of index REL_IDX is REL
620 (zero) or RELA (non-zero). The assumption is that, if there are
621 two relocation sections for this section, one of them is REL and
622 the other is RELA. If the index of the relocation we're testing is
623 in range for the first relocation section, check that the external
624 relocation size is that for RELA. It is also assumed that, if
625 rel_idx is not in range for the first section, and this first
626 section contains REL relocs, then the relocation is in the second
627 section, that is RELA. */
628 #define MIPS_RELOC_RELA_P(abfd, sec, rel_idx) \
629 ((NUM_SHDR_ENTRIES (&elf_section_data (sec)->rel_hdr) \
630 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel \
631 > (bfd_vma)(rel_idx)) \
632 == (elf_section_data (sec)->rel_hdr.sh_entsize \
633 == (ABI_64_P (abfd) ? sizeof (Elf64_External_Rela) \
634 : sizeof (Elf32_External_Rela))))
636 /* The name of the dynamic relocation section. */
637 #define MIPS_ELF_REL_DYN_NAME(INFO) \
638 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
640 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
641 from smaller values. Start with zero, widen, *then* decrement. */
642 #define MINUS_ONE (((bfd_vma)0) - 1)
643 #define MINUS_TWO (((bfd_vma)0) - 2)
645 /* The number of local .got entries we reserve. */
646 #define MIPS_RESERVED_GOTNO(INFO) \
647 (mips_elf_hash_table (INFO)->is_vxworks ? 3 : 2)
649 /* The value to write into got[1] for SVR4 targets, to identify it is
650 a GNU object. The dynamic linker can then use got[1] to store the
651 module pointer. */
652 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
653 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
655 /* The offset of $gp from the beginning of the .got section. */
656 #define ELF_MIPS_GP_OFFSET(INFO) \
657 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
659 /* The maximum size of the GOT for it to be addressable using 16-bit
660 offsets from $gp. */
661 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
663 /* Instructions which appear in a stub. */
664 #define STUB_LW(abfd) \
665 ((ABI_64_P (abfd) \
666 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
667 : 0x8f998010)) /* lw t9,0x8010(gp) */
668 #define STUB_MOVE(abfd) \
669 ((ABI_64_P (abfd) \
670 ? 0x03e0782d /* daddu t7,ra */ \
671 : 0x03e07821)) /* addu t7,ra */
672 #define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
673 #define STUB_JALR 0x0320f809 /* jalr t9,ra */
674 #define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
675 #define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
676 #define STUB_LI16S(abfd, VAL) \
677 ((ABI_64_P (abfd) \
678 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
679 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
681 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
682 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
684 /* The name of the dynamic interpreter. This is put in the .interp
685 section. */
687 #define ELF_DYNAMIC_INTERPRETER(abfd) \
688 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
689 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
690 : "/usr/lib/libc.so.1")
692 #ifdef BFD64
693 #define MNAME(bfd,pre,pos) \
694 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
695 #define ELF_R_SYM(bfd, i) \
696 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
697 #define ELF_R_TYPE(bfd, i) \
698 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
699 #define ELF_R_INFO(bfd, s, t) \
700 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
701 #else
702 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
703 #define ELF_R_SYM(bfd, i) \
704 (ELF32_R_SYM (i))
705 #define ELF_R_TYPE(bfd, i) \
706 (ELF32_R_TYPE (i))
707 #define ELF_R_INFO(bfd, s, t) \
708 (ELF32_R_INFO (s, t))
709 #endif
711 /* The mips16 compiler uses a couple of special sections to handle
712 floating point arguments.
714 Section names that look like .mips16.fn.FNNAME contain stubs that
715 copy floating point arguments from the fp regs to the gp regs and
716 then jump to FNNAME. If any 32 bit function calls FNNAME, the
717 call should be redirected to the stub instead. If no 32 bit
718 function calls FNNAME, the stub should be discarded. We need to
719 consider any reference to the function, not just a call, because
720 if the address of the function is taken we will need the stub,
721 since the address might be passed to a 32 bit function.
723 Section names that look like .mips16.call.FNNAME contain stubs
724 that copy floating point arguments from the gp regs to the fp
725 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
726 then any 16 bit function that calls FNNAME should be redirected
727 to the stub instead. If FNNAME is not a 32 bit function, the
728 stub should be discarded.
730 .mips16.call.fp.FNNAME sections are similar, but contain stubs
731 which call FNNAME and then copy the return value from the fp regs
732 to the gp regs. These stubs store the return value in $18 while
733 calling FNNAME; any function which might call one of these stubs
734 must arrange to save $18 around the call. (This case is not
735 needed for 32 bit functions that call 16 bit functions, because
736 16 bit functions always return floating point values in both
737 $f0/$f1 and $2/$3.)
739 Note that in all cases FNNAME might be defined statically.
740 Therefore, FNNAME is not used literally. Instead, the relocation
741 information will indicate which symbol the section is for.
743 We record any stubs that we find in the symbol table. */
745 #define FN_STUB ".mips16.fn."
746 #define CALL_STUB ".mips16.call."
747 #define CALL_FP_STUB ".mips16.call.fp."
749 #define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
750 #define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
751 #define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
753 /* The format of the first PLT entry in a VxWorks executable. */
754 static const bfd_vma mips_vxworks_exec_plt0_entry[] = {
755 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
756 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
757 0x8f390008, /* lw t9, 8(t9) */
758 0x00000000, /* nop */
759 0x03200008, /* jr t9 */
760 0x00000000 /* nop */
763 /* The format of subsequent PLT entries. */
764 static const bfd_vma mips_vxworks_exec_plt_entry[] = {
765 0x10000000, /* b .PLT_resolver */
766 0x24180000, /* li t8, <pltindex> */
767 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
768 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
769 0x8f390000, /* lw t9, 0(t9) */
770 0x00000000, /* nop */
771 0x03200008, /* jr t9 */
772 0x00000000 /* nop */
775 /* The format of the first PLT entry in a VxWorks shared object. */
776 static const bfd_vma mips_vxworks_shared_plt0_entry[] = {
777 0x8f990008, /* lw t9, 8(gp) */
778 0x00000000, /* nop */
779 0x03200008, /* jr t9 */
780 0x00000000, /* nop */
781 0x00000000, /* nop */
782 0x00000000 /* nop */
785 /* The format of subsequent PLT entries. */
786 static const bfd_vma mips_vxworks_shared_plt_entry[] = {
787 0x10000000, /* b .PLT_resolver */
788 0x24180000 /* li t8, <pltindex> */
791 /* Look up an entry in a MIPS ELF linker hash table. */
793 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
794 ((struct mips_elf_link_hash_entry *) \
795 elf_link_hash_lookup (&(table)->root, (string), (create), \
796 (copy), (follow)))
798 /* Traverse a MIPS ELF linker hash table. */
800 #define mips_elf_link_hash_traverse(table, func, info) \
801 (elf_link_hash_traverse \
802 (&(table)->root, \
803 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
804 (info)))
806 /* Get the MIPS ELF linker hash table from a link_info structure. */
808 #define mips_elf_hash_table(p) \
809 ((struct mips_elf_link_hash_table *) ((p)->hash))
811 /* Find the base offsets for thread-local storage in this object,
812 for GD/LD and IE/LE respectively. */
814 #define TP_OFFSET 0x7000
815 #define DTP_OFFSET 0x8000
817 static bfd_vma
818 dtprel_base (struct bfd_link_info *info)
820 /* If tls_sec is NULL, we should have signalled an error already. */
821 if (elf_hash_table (info)->tls_sec == NULL)
822 return 0;
823 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
826 static bfd_vma
827 tprel_base (struct bfd_link_info *info)
829 /* If tls_sec is NULL, we should have signalled an error already. */
830 if (elf_hash_table (info)->tls_sec == NULL)
831 return 0;
832 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
835 /* Create an entry in a MIPS ELF linker hash table. */
837 static struct bfd_hash_entry *
838 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
839 struct bfd_hash_table *table, const char *string)
841 struct mips_elf_link_hash_entry *ret =
842 (struct mips_elf_link_hash_entry *) entry;
844 /* Allocate the structure if it has not already been allocated by a
845 subclass. */
846 if (ret == NULL)
847 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
848 if (ret == NULL)
849 return (struct bfd_hash_entry *) ret;
851 /* Call the allocation method of the superclass. */
852 ret = ((struct mips_elf_link_hash_entry *)
853 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
854 table, string));
855 if (ret != NULL)
857 /* Set local fields. */
858 memset (&ret->esym, 0, sizeof (EXTR));
859 /* We use -2 as a marker to indicate that the information has
860 not been set. -1 means there is no associated ifd. */
861 ret->esym.ifd = -2;
862 ret->possibly_dynamic_relocs = 0;
863 ret->readonly_reloc = FALSE;
864 ret->no_fn_stub = FALSE;
865 ret->fn_stub = NULL;
866 ret->need_fn_stub = FALSE;
867 ret->call_stub = NULL;
868 ret->call_fp_stub = NULL;
869 ret->forced_local = FALSE;
870 ret->is_branch_target = FALSE;
871 ret->is_relocation_target = FALSE;
872 ret->tls_type = GOT_NORMAL;
875 return (struct bfd_hash_entry *) ret;
878 bfd_boolean
879 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
881 if (!sec->used_by_bfd)
883 struct _mips_elf_section_data *sdata;
884 bfd_size_type amt = sizeof (*sdata);
886 sdata = bfd_zalloc (abfd, amt);
887 if (sdata == NULL)
888 return FALSE;
889 sec->used_by_bfd = sdata;
892 return _bfd_elf_new_section_hook (abfd, sec);
895 /* Read ECOFF debugging information from a .mdebug section into a
896 ecoff_debug_info structure. */
898 bfd_boolean
899 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
900 struct ecoff_debug_info *debug)
902 HDRR *symhdr;
903 const struct ecoff_debug_swap *swap;
904 char *ext_hdr;
906 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
907 memset (debug, 0, sizeof (*debug));
909 ext_hdr = bfd_malloc (swap->external_hdr_size);
910 if (ext_hdr == NULL && swap->external_hdr_size != 0)
911 goto error_return;
913 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
914 swap->external_hdr_size))
915 goto error_return;
917 symhdr = &debug->symbolic_header;
918 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
920 /* The symbolic header contains absolute file offsets and sizes to
921 read. */
922 #define READ(ptr, offset, count, size, type) \
923 if (symhdr->count == 0) \
924 debug->ptr = NULL; \
925 else \
927 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
928 debug->ptr = bfd_malloc (amt); \
929 if (debug->ptr == NULL) \
930 goto error_return; \
931 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
932 || bfd_bread (debug->ptr, amt, abfd) != amt) \
933 goto error_return; \
936 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
937 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
938 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
939 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
940 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
941 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
942 union aux_ext *);
943 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
944 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
945 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
946 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
947 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
948 #undef READ
950 debug->fdr = NULL;
952 return TRUE;
954 error_return:
955 if (ext_hdr != NULL)
956 free (ext_hdr);
957 if (debug->line != NULL)
958 free (debug->line);
959 if (debug->external_dnr != NULL)
960 free (debug->external_dnr);
961 if (debug->external_pdr != NULL)
962 free (debug->external_pdr);
963 if (debug->external_sym != NULL)
964 free (debug->external_sym);
965 if (debug->external_opt != NULL)
966 free (debug->external_opt);
967 if (debug->external_aux != NULL)
968 free (debug->external_aux);
969 if (debug->ss != NULL)
970 free (debug->ss);
971 if (debug->ssext != NULL)
972 free (debug->ssext);
973 if (debug->external_fdr != NULL)
974 free (debug->external_fdr);
975 if (debug->external_rfd != NULL)
976 free (debug->external_rfd);
977 if (debug->external_ext != NULL)
978 free (debug->external_ext);
979 return FALSE;
982 /* Swap RPDR (runtime procedure table entry) for output. */
984 static void
985 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
987 H_PUT_S32 (abfd, in->adr, ex->p_adr);
988 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
989 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
990 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
991 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
992 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
994 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
995 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
997 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1000 /* Create a runtime procedure table from the .mdebug section. */
1002 static bfd_boolean
1003 mips_elf_create_procedure_table (void *handle, bfd *abfd,
1004 struct bfd_link_info *info, asection *s,
1005 struct ecoff_debug_info *debug)
1007 const struct ecoff_debug_swap *swap;
1008 HDRR *hdr = &debug->symbolic_header;
1009 RPDR *rpdr, *rp;
1010 struct rpdr_ext *erp;
1011 void *rtproc;
1012 struct pdr_ext *epdr;
1013 struct sym_ext *esym;
1014 char *ss, **sv;
1015 char *str;
1016 bfd_size_type size;
1017 bfd_size_type count;
1018 unsigned long sindex;
1019 unsigned long i;
1020 PDR pdr;
1021 SYMR sym;
1022 const char *no_name_func = _("static procedure (no name)");
1024 epdr = NULL;
1025 rpdr = NULL;
1026 esym = NULL;
1027 ss = NULL;
1028 sv = NULL;
1030 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1032 sindex = strlen (no_name_func) + 1;
1033 count = hdr->ipdMax;
1034 if (count > 0)
1036 size = swap->external_pdr_size;
1038 epdr = bfd_malloc (size * count);
1039 if (epdr == NULL)
1040 goto error_return;
1042 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1043 goto error_return;
1045 size = sizeof (RPDR);
1046 rp = rpdr = bfd_malloc (size * count);
1047 if (rpdr == NULL)
1048 goto error_return;
1050 size = sizeof (char *);
1051 sv = bfd_malloc (size * count);
1052 if (sv == NULL)
1053 goto error_return;
1055 count = hdr->isymMax;
1056 size = swap->external_sym_size;
1057 esym = bfd_malloc (size * count);
1058 if (esym == NULL)
1059 goto error_return;
1061 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1062 goto error_return;
1064 count = hdr->issMax;
1065 ss = bfd_malloc (count);
1066 if (ss == NULL)
1067 goto error_return;
1068 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1069 goto error_return;
1071 count = hdr->ipdMax;
1072 for (i = 0; i < (unsigned long) count; i++, rp++)
1074 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1075 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1076 rp->adr = sym.value;
1077 rp->regmask = pdr.regmask;
1078 rp->regoffset = pdr.regoffset;
1079 rp->fregmask = pdr.fregmask;
1080 rp->fregoffset = pdr.fregoffset;
1081 rp->frameoffset = pdr.frameoffset;
1082 rp->framereg = pdr.framereg;
1083 rp->pcreg = pdr.pcreg;
1084 rp->irpss = sindex;
1085 sv[i] = ss + sym.iss;
1086 sindex += strlen (sv[i]) + 1;
1090 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1091 size = BFD_ALIGN (size, 16);
1092 rtproc = bfd_alloc (abfd, size);
1093 if (rtproc == NULL)
1095 mips_elf_hash_table (info)->procedure_count = 0;
1096 goto error_return;
1099 mips_elf_hash_table (info)->procedure_count = count + 2;
1101 erp = rtproc;
1102 memset (erp, 0, sizeof (struct rpdr_ext));
1103 erp++;
1104 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1105 strcpy (str, no_name_func);
1106 str += strlen (no_name_func) + 1;
1107 for (i = 0; i < count; i++)
1109 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1110 strcpy (str, sv[i]);
1111 str += strlen (sv[i]) + 1;
1113 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1115 /* Set the size and contents of .rtproc section. */
1116 s->size = size;
1117 s->contents = rtproc;
1119 /* Skip this section later on (I don't think this currently
1120 matters, but someday it might). */
1121 s->map_head.link_order = NULL;
1123 if (epdr != NULL)
1124 free (epdr);
1125 if (rpdr != NULL)
1126 free (rpdr);
1127 if (esym != NULL)
1128 free (esym);
1129 if (ss != NULL)
1130 free (ss);
1131 if (sv != NULL)
1132 free (sv);
1134 return TRUE;
1136 error_return:
1137 if (epdr != NULL)
1138 free (epdr);
1139 if (rpdr != NULL)
1140 free (rpdr);
1141 if (esym != NULL)
1142 free (esym);
1143 if (ss != NULL)
1144 free (ss);
1145 if (sv != NULL)
1146 free (sv);
1147 return FALSE;
1150 /* We're about to redefine H. Create a symbol to represent H's
1151 current value and size, to help make the disassembly easier
1152 to read. */
1154 static bfd_boolean
1155 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1156 struct mips_elf_link_hash_entry *h,
1157 const char *prefix)
1159 struct bfd_link_hash_entry *bh;
1160 struct elf_link_hash_entry *elfh;
1161 const char *name;
1162 asection *s;
1163 bfd_vma value;
1165 /* Read the symbol's value. */
1166 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1167 || h->root.root.type == bfd_link_hash_defweak);
1168 s = h->root.root.u.def.section;
1169 value = h->root.root.u.def.value;
1171 /* Create a new symbol. */
1172 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1173 bh = NULL;
1174 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1175 BSF_LOCAL, s, value, NULL,
1176 TRUE, FALSE, &bh))
1177 return FALSE;
1179 /* Make it local and copy the other attributes from H. */
1180 elfh = (struct elf_link_hash_entry *) bh;
1181 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1182 elfh->other = h->root.other;
1183 elfh->size = h->root.size;
1184 elfh->forced_local = 1;
1185 return TRUE;
1188 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1189 function rather than to a hard-float stub. */
1191 static bfd_boolean
1192 section_allows_mips16_refs_p (asection *section)
1194 const char *name;
1196 name = bfd_get_section_name (section->owner, section);
1197 return (FN_STUB_P (name)
1198 || CALL_STUB_P (name)
1199 || CALL_FP_STUB_P (name)
1200 || strcmp (name, ".pdr") == 0);
1203 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1204 stub section of some kind. Return the R_SYMNDX of the target
1205 function, or 0 if we can't decide which function that is. */
1207 static unsigned long
1208 mips16_stub_symndx (asection *sec, const Elf_Internal_Rela *relocs,
1209 const Elf_Internal_Rela *relend)
1211 const Elf_Internal_Rela *rel;
1213 /* Trust the first R_MIPS_NONE relocation, if any. */
1214 for (rel = relocs; rel < relend; rel++)
1215 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1216 return ELF_R_SYM (sec->owner, rel->r_info);
1218 /* Otherwise trust the first relocation, whatever its kind. This is
1219 the traditional behavior. */
1220 if (relocs < relend)
1221 return ELF_R_SYM (sec->owner, relocs->r_info);
1223 return 0;
1226 /* Check the mips16 stubs for a particular symbol, and see if we can
1227 discard them. */
1229 static bfd_boolean
1230 mips_elf_check_mips16_stubs (struct mips_elf_link_hash_entry *h, void *data)
1232 struct bfd_link_info *info;
1234 info = (struct bfd_link_info *) data;
1235 if (h->root.root.type == bfd_link_hash_warning)
1236 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1238 /* Dynamic symbols must use the standard call interface, in case other
1239 objects try to call them. */
1240 if (h->fn_stub != NULL
1241 && h->root.dynindx != -1)
1243 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1244 h->need_fn_stub = TRUE;
1247 if (h->fn_stub != NULL
1248 && ! h->need_fn_stub)
1250 /* We don't need the fn_stub; the only references to this symbol
1251 are 16 bit calls. Clobber the size to 0 to prevent it from
1252 being included in the link. */
1253 h->fn_stub->size = 0;
1254 h->fn_stub->flags &= ~SEC_RELOC;
1255 h->fn_stub->reloc_count = 0;
1256 h->fn_stub->flags |= SEC_EXCLUDE;
1259 if (h->call_stub != NULL
1260 && ELF_ST_IS_MIPS16 (h->root.other))
1262 /* We don't need the call_stub; this is a 16 bit function, so
1263 calls from other 16 bit functions are OK. Clobber the size
1264 to 0 to prevent it from being included in the link. */
1265 h->call_stub->size = 0;
1266 h->call_stub->flags &= ~SEC_RELOC;
1267 h->call_stub->reloc_count = 0;
1268 h->call_stub->flags |= SEC_EXCLUDE;
1271 if (h->call_fp_stub != NULL
1272 && ELF_ST_IS_MIPS16 (h->root.other))
1274 /* We don't need the call_stub; this is a 16 bit function, so
1275 calls from other 16 bit functions are OK. Clobber the size
1276 to 0 to prevent it from being included in the link. */
1277 h->call_fp_stub->size = 0;
1278 h->call_fp_stub->flags &= ~SEC_RELOC;
1279 h->call_fp_stub->reloc_count = 0;
1280 h->call_fp_stub->flags |= SEC_EXCLUDE;
1283 return TRUE;
1286 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1287 Most mips16 instructions are 16 bits, but these instructions
1288 are 32 bits.
1290 The format of these instructions is:
1292 +--------------+--------------------------------+
1293 | JALX | X| Imm 20:16 | Imm 25:21 |
1294 +--------------+--------------------------------+
1295 | Immediate 15:0 |
1296 +-----------------------------------------------+
1298 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1299 Note that the immediate value in the first word is swapped.
1301 When producing a relocatable object file, R_MIPS16_26 is
1302 handled mostly like R_MIPS_26. In particular, the addend is
1303 stored as a straight 26-bit value in a 32-bit instruction.
1304 (gas makes life simpler for itself by never adjusting a
1305 R_MIPS16_26 reloc to be against a section, so the addend is
1306 always zero). However, the 32 bit instruction is stored as 2
1307 16-bit values, rather than a single 32-bit value. In a
1308 big-endian file, the result is the same; in a little-endian
1309 file, the two 16-bit halves of the 32 bit value are swapped.
1310 This is so that a disassembler can recognize the jal
1311 instruction.
1313 When doing a final link, R_MIPS16_26 is treated as a 32 bit
1314 instruction stored as two 16-bit values. The addend A is the
1315 contents of the targ26 field. The calculation is the same as
1316 R_MIPS_26. When storing the calculated value, reorder the
1317 immediate value as shown above, and don't forget to store the
1318 value as two 16-bit values.
1320 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
1321 defined as
1323 big-endian:
1324 +--------+----------------------+
1325 | | |
1326 | | targ26-16 |
1327 |31 26|25 0|
1328 +--------+----------------------+
1330 little-endian:
1331 +----------+------+-------------+
1332 | | | |
1333 | sub1 | | sub2 |
1334 |0 9|10 15|16 31|
1335 +----------+--------------------+
1336 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
1337 ((sub1 << 16) | sub2)).
1339 When producing a relocatable object file, the calculation is
1340 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1341 When producing a fully linked file, the calculation is
1342 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1343 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
1345 The table below lists the other MIPS16 instruction relocations.
1346 Each one is calculated in the same way as the non-MIPS16 relocation
1347 given on the right, but using the extended MIPS16 layout of 16-bit
1348 immediate fields:
1350 R_MIPS16_GPREL R_MIPS_GPREL16
1351 R_MIPS16_GOT16 R_MIPS_GOT16
1352 R_MIPS16_CALL16 R_MIPS_CALL16
1353 R_MIPS16_HI16 R_MIPS_HI16
1354 R_MIPS16_LO16 R_MIPS_LO16
1356 A typical instruction will have a format like this:
1358 +--------------+--------------------------------+
1359 | EXTEND | Imm 10:5 | Imm 15:11 |
1360 +--------------+--------------------------------+
1361 | Major | rx | ry | Imm 4:0 |
1362 +--------------+--------------------------------+
1364 EXTEND is the five bit value 11110. Major is the instruction
1365 opcode.
1367 All we need to do here is shuffle the bits appropriately.
1368 As above, the two 16-bit halves must be swapped on a
1369 little-endian system. */
1371 static inline bfd_boolean
1372 mips16_reloc_p (int r_type)
1374 switch (r_type)
1376 case R_MIPS16_26:
1377 case R_MIPS16_GPREL:
1378 case R_MIPS16_GOT16:
1379 case R_MIPS16_CALL16:
1380 case R_MIPS16_HI16:
1381 case R_MIPS16_LO16:
1382 return TRUE;
1384 default:
1385 return FALSE;
1389 static inline bfd_boolean
1390 got16_reloc_p (int r_type)
1392 return r_type == R_MIPS_GOT16 || r_type == R_MIPS16_GOT16;
1395 static inline bfd_boolean
1396 call16_reloc_p (int r_type)
1398 return r_type == R_MIPS_CALL16 || r_type == R_MIPS16_CALL16;
1401 static inline bfd_boolean
1402 hi16_reloc_p (int r_type)
1404 return r_type == R_MIPS_HI16 || r_type == R_MIPS16_HI16;
1407 static inline bfd_boolean
1408 lo16_reloc_p (int r_type)
1410 return r_type == R_MIPS_LO16 || r_type == R_MIPS16_LO16;
1413 static inline bfd_boolean
1414 mips16_call_reloc_p (int r_type)
1416 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
1419 void
1420 _bfd_mips16_elf_reloc_unshuffle (bfd *abfd, int r_type,
1421 bfd_boolean jal_shuffle, bfd_byte *data)
1423 bfd_vma extend, insn, val;
1425 if (!mips16_reloc_p (r_type))
1426 return;
1428 /* Pick up the mips16 extend instruction and the real instruction. */
1429 extend = bfd_get_16 (abfd, data);
1430 insn = bfd_get_16 (abfd, data + 2);
1431 if (r_type == R_MIPS16_26)
1433 if (jal_shuffle)
1434 val = ((extend & 0xfc00) << 16) | ((extend & 0x3e0) << 11)
1435 | ((extend & 0x1f) << 21) | insn;
1436 else
1437 val = extend << 16 | insn;
1439 else
1440 val = ((extend & 0xf800) << 16) | ((insn & 0xffe0) << 11)
1441 | ((extend & 0x1f) << 11) | (extend & 0x7e0) | (insn & 0x1f);
1442 bfd_put_32 (abfd, val, data);
1445 void
1446 _bfd_mips16_elf_reloc_shuffle (bfd *abfd, int r_type,
1447 bfd_boolean jal_shuffle, bfd_byte *data)
1449 bfd_vma extend, insn, val;
1451 if (!mips16_reloc_p (r_type))
1452 return;
1454 val = bfd_get_32 (abfd, data);
1455 if (r_type == R_MIPS16_26)
1457 if (jal_shuffle)
1459 insn = val & 0xffff;
1460 extend = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
1461 | ((val >> 21) & 0x1f);
1463 else
1465 insn = val & 0xffff;
1466 extend = val >> 16;
1469 else
1471 insn = ((val >> 11) & 0xffe0) | (val & 0x1f);
1472 extend = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
1474 bfd_put_16 (abfd, insn, data + 2);
1475 bfd_put_16 (abfd, extend, data);
1478 bfd_reloc_status_type
1479 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
1480 arelent *reloc_entry, asection *input_section,
1481 bfd_boolean relocatable, void *data, bfd_vma gp)
1483 bfd_vma relocation;
1484 bfd_signed_vma val;
1485 bfd_reloc_status_type status;
1487 if (bfd_is_com_section (symbol->section))
1488 relocation = 0;
1489 else
1490 relocation = symbol->value;
1492 relocation += symbol->section->output_section->vma;
1493 relocation += symbol->section->output_offset;
1495 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1496 return bfd_reloc_outofrange;
1498 /* Set val to the offset into the section or symbol. */
1499 val = reloc_entry->addend;
1501 _bfd_mips_elf_sign_extend (val, 16);
1503 /* Adjust val for the final section location and GP value. If we
1504 are producing relocatable output, we don't want to do this for
1505 an external symbol. */
1506 if (! relocatable
1507 || (symbol->flags & BSF_SECTION_SYM) != 0)
1508 val += relocation - gp;
1510 if (reloc_entry->howto->partial_inplace)
1512 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1513 (bfd_byte *) data
1514 + reloc_entry->address);
1515 if (status != bfd_reloc_ok)
1516 return status;
1518 else
1519 reloc_entry->addend = val;
1521 if (relocatable)
1522 reloc_entry->address += input_section->output_offset;
1524 return bfd_reloc_ok;
1527 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
1528 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
1529 that contains the relocation field and DATA points to the start of
1530 INPUT_SECTION. */
1532 struct mips_hi16
1534 struct mips_hi16 *next;
1535 bfd_byte *data;
1536 asection *input_section;
1537 arelent rel;
1540 /* FIXME: This should not be a static variable. */
1542 static struct mips_hi16 *mips_hi16_list;
1544 /* A howto special_function for REL *HI16 relocations. We can only
1545 calculate the correct value once we've seen the partnering
1546 *LO16 relocation, so just save the information for later.
1548 The ABI requires that the *LO16 immediately follow the *HI16.
1549 However, as a GNU extension, we permit an arbitrary number of
1550 *HI16s to be associated with a single *LO16. This significantly
1551 simplies the relocation handling in gcc. */
1553 bfd_reloc_status_type
1554 _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1555 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
1556 asection *input_section, bfd *output_bfd,
1557 char **error_message ATTRIBUTE_UNUSED)
1559 struct mips_hi16 *n;
1561 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1562 return bfd_reloc_outofrange;
1564 n = bfd_malloc (sizeof *n);
1565 if (n == NULL)
1566 return bfd_reloc_outofrange;
1568 n->next = mips_hi16_list;
1569 n->data = data;
1570 n->input_section = input_section;
1571 n->rel = *reloc_entry;
1572 mips_hi16_list = n;
1574 if (output_bfd != NULL)
1575 reloc_entry->address += input_section->output_offset;
1577 return bfd_reloc_ok;
1580 /* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
1581 like any other 16-bit relocation when applied to global symbols, but is
1582 treated in the same as R_MIPS_HI16 when applied to local symbols. */
1584 bfd_reloc_status_type
1585 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1586 void *data, asection *input_section,
1587 bfd *output_bfd, char **error_message)
1589 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1590 || bfd_is_und_section (bfd_get_section (symbol))
1591 || bfd_is_com_section (bfd_get_section (symbol)))
1592 /* The relocation is against a global symbol. */
1593 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1594 input_section, output_bfd,
1595 error_message);
1597 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1598 input_section, output_bfd, error_message);
1601 /* A howto special_function for REL *LO16 relocations. The *LO16 itself
1602 is a straightforward 16 bit inplace relocation, but we must deal with
1603 any partnering high-part relocations as well. */
1605 bfd_reloc_status_type
1606 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1607 void *data, asection *input_section,
1608 bfd *output_bfd, char **error_message)
1610 bfd_vma vallo;
1611 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
1613 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1614 return bfd_reloc_outofrange;
1616 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
1617 location);
1618 vallo = bfd_get_32 (abfd, location);
1619 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
1620 location);
1622 while (mips_hi16_list != NULL)
1624 bfd_reloc_status_type ret;
1625 struct mips_hi16 *hi;
1627 hi = mips_hi16_list;
1629 /* R_MIPS*_GOT16 relocations are something of a special case. We
1630 want to install the addend in the same way as for a R_MIPS*_HI16
1631 relocation (with a rightshift of 16). However, since GOT16
1632 relocations can also be used with global symbols, their howto
1633 has a rightshift of 0. */
1634 if (hi->rel.howto->type == R_MIPS_GOT16)
1635 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
1636 else if (hi->rel.howto->type == R_MIPS16_GOT16)
1637 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
1639 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
1640 carry or borrow will induce a change of +1 or -1 in the high part. */
1641 hi->rel.addend += (vallo + 0x8000) & 0xffff;
1643 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
1644 hi->input_section, output_bfd,
1645 error_message);
1646 if (ret != bfd_reloc_ok)
1647 return ret;
1649 mips_hi16_list = hi->next;
1650 free (hi);
1653 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1654 input_section, output_bfd,
1655 error_message);
1658 /* A generic howto special_function. This calculates and installs the
1659 relocation itself, thus avoiding the oft-discussed problems in
1660 bfd_perform_relocation and bfd_install_relocation. */
1662 bfd_reloc_status_type
1663 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1664 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
1665 asection *input_section, bfd *output_bfd,
1666 char **error_message ATTRIBUTE_UNUSED)
1668 bfd_signed_vma val;
1669 bfd_reloc_status_type status;
1670 bfd_boolean relocatable;
1672 relocatable = (output_bfd != NULL);
1674 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1675 return bfd_reloc_outofrange;
1677 /* Build up the field adjustment in VAL. */
1678 val = 0;
1679 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
1681 /* Either we're calculating the final field value or we have a
1682 relocation against a section symbol. Add in the section's
1683 offset or address. */
1684 val += symbol->section->output_section->vma;
1685 val += symbol->section->output_offset;
1688 if (!relocatable)
1690 /* We're calculating the final field value. Add in the symbol's value
1691 and, if pc-relative, subtract the address of the field itself. */
1692 val += symbol->value;
1693 if (reloc_entry->howto->pc_relative)
1695 val -= input_section->output_section->vma;
1696 val -= input_section->output_offset;
1697 val -= reloc_entry->address;
1701 /* VAL is now the final adjustment. If we're keeping this relocation
1702 in the output file, and if the relocation uses a separate addend,
1703 we just need to add VAL to that addend. Otherwise we need to add
1704 VAL to the relocation field itself. */
1705 if (relocatable && !reloc_entry->howto->partial_inplace)
1706 reloc_entry->addend += val;
1707 else
1709 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
1711 /* Add in the separate addend, if any. */
1712 val += reloc_entry->addend;
1714 /* Add VAL to the relocation field. */
1715 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
1716 location);
1717 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1718 location);
1719 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
1720 location);
1722 if (status != bfd_reloc_ok)
1723 return status;
1726 if (relocatable)
1727 reloc_entry->address += input_section->output_offset;
1729 return bfd_reloc_ok;
1732 /* Swap an entry in a .gptab section. Note that these routines rely
1733 on the equivalence of the two elements of the union. */
1735 static void
1736 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
1737 Elf32_gptab *in)
1739 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
1740 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
1743 static void
1744 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
1745 Elf32_External_gptab *ex)
1747 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
1748 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
1751 static void
1752 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
1753 Elf32_External_compact_rel *ex)
1755 H_PUT_32 (abfd, in->id1, ex->id1);
1756 H_PUT_32 (abfd, in->num, ex->num);
1757 H_PUT_32 (abfd, in->id2, ex->id2);
1758 H_PUT_32 (abfd, in->offset, ex->offset);
1759 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
1760 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
1763 static void
1764 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
1765 Elf32_External_crinfo *ex)
1767 unsigned long l;
1769 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
1770 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
1771 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
1772 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
1773 H_PUT_32 (abfd, l, ex->info);
1774 H_PUT_32 (abfd, in->konst, ex->konst);
1775 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
1778 /* A .reginfo section holds a single Elf32_RegInfo structure. These
1779 routines swap this structure in and out. They are used outside of
1780 BFD, so they are globally visible. */
1782 void
1783 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
1784 Elf32_RegInfo *in)
1786 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
1787 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
1788 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
1789 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
1790 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
1791 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
1794 void
1795 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
1796 Elf32_External_RegInfo *ex)
1798 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
1799 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
1800 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
1801 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
1802 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
1803 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
1806 /* In the 64 bit ABI, the .MIPS.options section holds register
1807 information in an Elf64_Reginfo structure. These routines swap
1808 them in and out. They are globally visible because they are used
1809 outside of BFD. These routines are here so that gas can call them
1810 without worrying about whether the 64 bit ABI has been included. */
1812 void
1813 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
1814 Elf64_Internal_RegInfo *in)
1816 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
1817 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
1818 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
1819 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
1820 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
1821 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
1822 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
1825 void
1826 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
1827 Elf64_External_RegInfo *ex)
1829 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
1830 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
1831 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
1832 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
1833 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
1834 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
1835 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
1838 /* Swap in an options header. */
1840 void
1841 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
1842 Elf_Internal_Options *in)
1844 in->kind = H_GET_8 (abfd, ex->kind);
1845 in->size = H_GET_8 (abfd, ex->size);
1846 in->section = H_GET_16 (abfd, ex->section);
1847 in->info = H_GET_32 (abfd, ex->info);
1850 /* Swap out an options header. */
1852 void
1853 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
1854 Elf_External_Options *ex)
1856 H_PUT_8 (abfd, in->kind, ex->kind);
1857 H_PUT_8 (abfd, in->size, ex->size);
1858 H_PUT_16 (abfd, in->section, ex->section);
1859 H_PUT_32 (abfd, in->info, ex->info);
1862 /* This function is called via qsort() to sort the dynamic relocation
1863 entries by increasing r_symndx value. */
1865 static int
1866 sort_dynamic_relocs (const void *arg1, const void *arg2)
1868 Elf_Internal_Rela int_reloc1;
1869 Elf_Internal_Rela int_reloc2;
1870 int diff;
1872 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
1873 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
1875 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
1876 if (diff != 0)
1877 return diff;
1879 if (int_reloc1.r_offset < int_reloc2.r_offset)
1880 return -1;
1881 if (int_reloc1.r_offset > int_reloc2.r_offset)
1882 return 1;
1883 return 0;
1886 /* Like sort_dynamic_relocs, but used for elf64 relocations. */
1888 static int
1889 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
1890 const void *arg2 ATTRIBUTE_UNUSED)
1892 #ifdef BFD64
1893 Elf_Internal_Rela int_reloc1[3];
1894 Elf_Internal_Rela int_reloc2[3];
1896 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
1897 (reldyn_sorting_bfd, arg1, int_reloc1);
1898 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
1899 (reldyn_sorting_bfd, arg2, int_reloc2);
1901 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
1902 return -1;
1903 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
1904 return 1;
1906 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
1907 return -1;
1908 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
1909 return 1;
1910 return 0;
1911 #else
1912 abort ();
1913 #endif
1917 /* This routine is used to write out ECOFF debugging external symbol
1918 information. It is called via mips_elf_link_hash_traverse. The
1919 ECOFF external symbol information must match the ELF external
1920 symbol information. Unfortunately, at this point we don't know
1921 whether a symbol is required by reloc information, so the two
1922 tables may wind up being different. We must sort out the external
1923 symbol information before we can set the final size of the .mdebug
1924 section, and we must set the size of the .mdebug section before we
1925 can relocate any sections, and we can't know which symbols are
1926 required by relocation until we relocate the sections.
1927 Fortunately, it is relatively unlikely that any symbol will be
1928 stripped but required by a reloc. In particular, it can not happen
1929 when generating a final executable. */
1931 static bfd_boolean
1932 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
1934 struct extsym_info *einfo = data;
1935 bfd_boolean strip;
1936 asection *sec, *output_section;
1938 if (h->root.root.type == bfd_link_hash_warning)
1939 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1941 if (h->root.indx == -2)
1942 strip = FALSE;
1943 else if ((h->root.def_dynamic
1944 || h->root.ref_dynamic
1945 || h->root.type == bfd_link_hash_new)
1946 && !h->root.def_regular
1947 && !h->root.ref_regular)
1948 strip = TRUE;
1949 else if (einfo->info->strip == strip_all
1950 || (einfo->info->strip == strip_some
1951 && bfd_hash_lookup (einfo->info->keep_hash,
1952 h->root.root.root.string,
1953 FALSE, FALSE) == NULL))
1954 strip = TRUE;
1955 else
1956 strip = FALSE;
1958 if (strip)
1959 return TRUE;
1961 if (h->esym.ifd == -2)
1963 h->esym.jmptbl = 0;
1964 h->esym.cobol_main = 0;
1965 h->esym.weakext = 0;
1966 h->esym.reserved = 0;
1967 h->esym.ifd = ifdNil;
1968 h->esym.asym.value = 0;
1969 h->esym.asym.st = stGlobal;
1971 if (h->root.root.type == bfd_link_hash_undefined
1972 || h->root.root.type == bfd_link_hash_undefweak)
1974 const char *name;
1976 /* Use undefined class. Also, set class and type for some
1977 special symbols. */
1978 name = h->root.root.root.string;
1979 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
1980 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
1982 h->esym.asym.sc = scData;
1983 h->esym.asym.st = stLabel;
1984 h->esym.asym.value = 0;
1986 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
1988 h->esym.asym.sc = scAbs;
1989 h->esym.asym.st = stLabel;
1990 h->esym.asym.value =
1991 mips_elf_hash_table (einfo->info)->procedure_count;
1993 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
1995 h->esym.asym.sc = scAbs;
1996 h->esym.asym.st = stLabel;
1997 h->esym.asym.value = elf_gp (einfo->abfd);
1999 else
2000 h->esym.asym.sc = scUndefined;
2002 else if (h->root.root.type != bfd_link_hash_defined
2003 && h->root.root.type != bfd_link_hash_defweak)
2004 h->esym.asym.sc = scAbs;
2005 else
2007 const char *name;
2009 sec = h->root.root.u.def.section;
2010 output_section = sec->output_section;
2012 /* When making a shared library and symbol h is the one from
2013 the another shared library, OUTPUT_SECTION may be null. */
2014 if (output_section == NULL)
2015 h->esym.asym.sc = scUndefined;
2016 else
2018 name = bfd_section_name (output_section->owner, output_section);
2020 if (strcmp (name, ".text") == 0)
2021 h->esym.asym.sc = scText;
2022 else if (strcmp (name, ".data") == 0)
2023 h->esym.asym.sc = scData;
2024 else if (strcmp (name, ".sdata") == 0)
2025 h->esym.asym.sc = scSData;
2026 else if (strcmp (name, ".rodata") == 0
2027 || strcmp (name, ".rdata") == 0)
2028 h->esym.asym.sc = scRData;
2029 else if (strcmp (name, ".bss") == 0)
2030 h->esym.asym.sc = scBss;
2031 else if (strcmp (name, ".sbss") == 0)
2032 h->esym.asym.sc = scSBss;
2033 else if (strcmp (name, ".init") == 0)
2034 h->esym.asym.sc = scInit;
2035 else if (strcmp (name, ".fini") == 0)
2036 h->esym.asym.sc = scFini;
2037 else
2038 h->esym.asym.sc = scAbs;
2042 h->esym.asym.reserved = 0;
2043 h->esym.asym.index = indexNil;
2046 if (h->root.root.type == bfd_link_hash_common)
2047 h->esym.asym.value = h->root.root.u.c.size;
2048 else if (h->root.root.type == bfd_link_hash_defined
2049 || h->root.root.type == bfd_link_hash_defweak)
2051 if (h->esym.asym.sc == scCommon)
2052 h->esym.asym.sc = scBss;
2053 else if (h->esym.asym.sc == scSCommon)
2054 h->esym.asym.sc = scSBss;
2056 sec = h->root.root.u.def.section;
2057 output_section = sec->output_section;
2058 if (output_section != NULL)
2059 h->esym.asym.value = (h->root.root.u.def.value
2060 + sec->output_offset
2061 + output_section->vma);
2062 else
2063 h->esym.asym.value = 0;
2065 else if (h->root.needs_plt)
2067 struct mips_elf_link_hash_entry *hd = h;
2068 bfd_boolean no_fn_stub = h->no_fn_stub;
2070 while (hd->root.root.type == bfd_link_hash_indirect)
2072 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
2073 no_fn_stub = no_fn_stub || hd->no_fn_stub;
2076 if (!no_fn_stub)
2078 /* Set type and value for a symbol with a function stub. */
2079 h->esym.asym.st = stProc;
2080 sec = hd->root.root.u.def.section;
2081 if (sec == NULL)
2082 h->esym.asym.value = 0;
2083 else
2085 output_section = sec->output_section;
2086 if (output_section != NULL)
2087 h->esym.asym.value = (hd->root.plt.offset
2088 + sec->output_offset
2089 + output_section->vma);
2090 else
2091 h->esym.asym.value = 0;
2096 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2097 h->root.root.root.string,
2098 &h->esym))
2100 einfo->failed = TRUE;
2101 return FALSE;
2104 return TRUE;
2107 /* A comparison routine used to sort .gptab entries. */
2109 static int
2110 gptab_compare (const void *p1, const void *p2)
2112 const Elf32_gptab *a1 = p1;
2113 const Elf32_gptab *a2 = p2;
2115 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2118 /* Functions to manage the got entry hash table. */
2120 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2121 hash number. */
2123 static INLINE hashval_t
2124 mips_elf_hash_bfd_vma (bfd_vma addr)
2126 #ifdef BFD64
2127 return addr + (addr >> 32);
2128 #else
2129 return addr;
2130 #endif
2133 /* got_entries only match if they're identical, except for gotidx, so
2134 use all fields to compute the hash, and compare the appropriate
2135 union members. */
2137 static hashval_t
2138 mips_elf_got_entry_hash (const void *entry_)
2140 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2142 return entry->symndx
2143 + ((entry->tls_type & GOT_TLS_LDM) << 17)
2144 + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
2145 : entry->abfd->id
2146 + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
2147 : entry->d.h->root.root.root.hash));
2150 static int
2151 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
2153 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2154 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2156 /* An LDM entry can only match another LDM entry. */
2157 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2158 return 0;
2160 return e1->abfd == e2->abfd && e1->symndx == e2->symndx
2161 && (! e1->abfd ? e1->d.address == e2->d.address
2162 : e1->symndx >= 0 ? e1->d.addend == e2->d.addend
2163 : e1->d.h == e2->d.h);
2166 /* multi_got_entries are still a match in the case of global objects,
2167 even if the input bfd in which they're referenced differs, so the
2168 hash computation and compare functions are adjusted
2169 accordingly. */
2171 static hashval_t
2172 mips_elf_multi_got_entry_hash (const void *entry_)
2174 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2176 return entry->symndx
2177 + (! entry->abfd
2178 ? mips_elf_hash_bfd_vma (entry->d.address)
2179 : entry->symndx >= 0
2180 ? ((entry->tls_type & GOT_TLS_LDM)
2181 ? (GOT_TLS_LDM << 17)
2182 : (entry->abfd->id
2183 + mips_elf_hash_bfd_vma (entry->d.addend)))
2184 : entry->d.h->root.root.root.hash);
2187 static int
2188 mips_elf_multi_got_entry_eq (const void *entry1, const void *entry2)
2190 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2191 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2193 /* Any two LDM entries match. */
2194 if (e1->tls_type & e2->tls_type & GOT_TLS_LDM)
2195 return 1;
2197 /* Nothing else matches an LDM entry. */
2198 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2199 return 0;
2201 return e1->symndx == e2->symndx
2202 && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
2203 : e1->abfd == NULL || e2->abfd == NULL
2204 ? e1->abfd == e2->abfd && e1->d.address == e2->d.address
2205 : e1->d.h == e2->d.h);
2208 static hashval_t
2209 mips_got_page_entry_hash (const void *entry_)
2211 const struct mips_got_page_entry *entry;
2213 entry = (const struct mips_got_page_entry *) entry_;
2214 return entry->abfd->id + entry->symndx;
2217 static int
2218 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
2220 const struct mips_got_page_entry *entry1, *entry2;
2222 entry1 = (const struct mips_got_page_entry *) entry1_;
2223 entry2 = (const struct mips_got_page_entry *) entry2_;
2224 return entry1->abfd == entry2->abfd && entry1->symndx == entry2->symndx;
2227 /* Return the dynamic relocation section. If it doesn't exist, try to
2228 create a new it if CREATE_P, otherwise return NULL. Also return NULL
2229 if creation fails. */
2231 static asection *
2232 mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
2234 const char *dname;
2235 asection *sreloc;
2236 bfd *dynobj;
2238 dname = MIPS_ELF_REL_DYN_NAME (info);
2239 dynobj = elf_hash_table (info)->dynobj;
2240 sreloc = bfd_get_section_by_name (dynobj, dname);
2241 if (sreloc == NULL && create_p)
2243 sreloc = bfd_make_section_with_flags (dynobj, dname,
2244 (SEC_ALLOC
2245 | SEC_LOAD
2246 | SEC_HAS_CONTENTS
2247 | SEC_IN_MEMORY
2248 | SEC_LINKER_CREATED
2249 | SEC_READONLY));
2250 if (sreloc == NULL
2251 || ! bfd_set_section_alignment (dynobj, sreloc,
2252 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
2253 return NULL;
2255 return sreloc;
2258 /* Returns the GOT section for ABFD. */
2260 static asection *
2261 mips_elf_got_section (bfd *abfd, bfd_boolean maybe_excluded)
2263 asection *sgot = bfd_get_section_by_name (abfd, ".got");
2264 if (sgot == NULL
2265 || (! maybe_excluded && (sgot->flags & SEC_EXCLUDE) != 0))
2266 return NULL;
2267 return sgot;
2270 /* Returns the GOT information associated with the link indicated by
2271 INFO. If SGOTP is non-NULL, it is filled in with the GOT
2272 section. */
2274 static struct mips_got_info *
2275 mips_elf_got_info (bfd *abfd, asection **sgotp)
2277 asection *sgot;
2278 struct mips_got_info *g;
2280 sgot = mips_elf_got_section (abfd, TRUE);
2281 BFD_ASSERT (sgot != NULL);
2282 BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
2283 g = mips_elf_section_data (sgot)->u.got_info;
2284 BFD_ASSERT (g != NULL);
2286 if (sgotp)
2287 *sgotp = (sgot->flags & SEC_EXCLUDE) == 0 ? sgot : NULL;
2289 return g;
2292 /* Count the number of relocations needed for a TLS GOT entry, with
2293 access types from TLS_TYPE, and symbol H (or a local symbol if H
2294 is NULL). */
2296 static int
2297 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
2298 struct elf_link_hash_entry *h)
2300 int indx = 0;
2301 int ret = 0;
2302 bfd_boolean need_relocs = FALSE;
2303 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2305 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2306 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
2307 indx = h->dynindx;
2309 if ((info->shared || indx != 0)
2310 && (h == NULL
2311 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2312 || h->root.type != bfd_link_hash_undefweak))
2313 need_relocs = TRUE;
2315 if (!need_relocs)
2316 return FALSE;
2318 if (tls_type & GOT_TLS_GD)
2320 ret++;
2321 if (indx != 0)
2322 ret++;
2325 if (tls_type & GOT_TLS_IE)
2326 ret++;
2328 if ((tls_type & GOT_TLS_LDM) && info->shared)
2329 ret++;
2331 return ret;
2334 /* Count the number of TLS relocations required for the GOT entry in
2335 ARG1, if it describes a local symbol. */
2337 static int
2338 mips_elf_count_local_tls_relocs (void **arg1, void *arg2)
2340 struct mips_got_entry *entry = * (struct mips_got_entry **) arg1;
2341 struct mips_elf_count_tls_arg *arg = arg2;
2343 if (entry->abfd != NULL && entry->symndx != -1)
2344 arg->needed += mips_tls_got_relocs (arg->info, entry->tls_type, NULL);
2346 return 1;
2349 /* Count the number of TLS GOT entries required for the global (or
2350 forced-local) symbol in ARG1. */
2352 static int
2353 mips_elf_count_global_tls_entries (void *arg1, void *arg2)
2355 struct mips_elf_link_hash_entry *hm
2356 = (struct mips_elf_link_hash_entry *) arg1;
2357 struct mips_elf_count_tls_arg *arg = arg2;
2359 if (hm->tls_type & GOT_TLS_GD)
2360 arg->needed += 2;
2361 if (hm->tls_type & GOT_TLS_IE)
2362 arg->needed += 1;
2364 return 1;
2367 /* Count the number of TLS relocations required for the global (or
2368 forced-local) symbol in ARG1. */
2370 static int
2371 mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
2373 struct mips_elf_link_hash_entry *hm
2374 = (struct mips_elf_link_hash_entry *) arg1;
2375 struct mips_elf_count_tls_arg *arg = arg2;
2377 arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
2379 return 1;
2382 /* Output a simple dynamic relocation into SRELOC. */
2384 static void
2385 mips_elf_output_dynamic_relocation (bfd *output_bfd,
2386 asection *sreloc,
2387 unsigned long indx,
2388 int r_type,
2389 bfd_vma offset)
2391 Elf_Internal_Rela rel[3];
2393 memset (rel, 0, sizeof (rel));
2395 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
2396 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
2398 if (ABI_64_P (output_bfd))
2400 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2401 (output_bfd, &rel[0],
2402 (sreloc->contents
2403 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
2405 else
2406 bfd_elf32_swap_reloc_out
2407 (output_bfd, &rel[0],
2408 (sreloc->contents
2409 + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
2410 ++sreloc->reloc_count;
2413 /* Initialize a set of TLS GOT entries for one symbol. */
2415 static void
2416 mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
2417 unsigned char *tls_type_p,
2418 struct bfd_link_info *info,
2419 struct mips_elf_link_hash_entry *h,
2420 bfd_vma value)
2422 int indx;
2423 asection *sreloc, *sgot;
2424 bfd_vma offset, offset2;
2425 bfd *dynobj;
2426 bfd_boolean need_relocs = FALSE;
2428 dynobj = elf_hash_table (info)->dynobj;
2429 sgot = mips_elf_got_section (dynobj, FALSE);
2431 indx = 0;
2432 if (h != NULL)
2434 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2436 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
2437 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
2438 indx = h->root.dynindx;
2441 if (*tls_type_p & GOT_TLS_DONE)
2442 return;
2444 if ((info->shared || indx != 0)
2445 && (h == NULL
2446 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
2447 || h->root.type != bfd_link_hash_undefweak))
2448 need_relocs = TRUE;
2450 /* MINUS_ONE means the symbol is not defined in this object. It may not
2451 be defined at all; assume that the value doesn't matter in that
2452 case. Otherwise complain if we would use the value. */
2453 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
2454 || h->root.root.type == bfd_link_hash_undefweak);
2456 /* Emit necessary relocations. */
2457 sreloc = mips_elf_rel_dyn_section (info, FALSE);
2459 /* General Dynamic. */
2460 if (*tls_type_p & GOT_TLS_GD)
2462 offset = got_offset;
2463 offset2 = offset + MIPS_ELF_GOT_SIZE (abfd);
2465 if (need_relocs)
2467 mips_elf_output_dynamic_relocation
2468 (abfd, sreloc, indx,
2469 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2470 sgot->output_offset + sgot->output_section->vma + offset);
2472 if (indx)
2473 mips_elf_output_dynamic_relocation
2474 (abfd, sreloc, indx,
2475 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
2476 sgot->output_offset + sgot->output_section->vma + offset2);
2477 else
2478 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2479 sgot->contents + offset2);
2481 else
2483 MIPS_ELF_PUT_WORD (abfd, 1,
2484 sgot->contents + offset);
2485 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2486 sgot->contents + offset2);
2489 got_offset += 2 * MIPS_ELF_GOT_SIZE (abfd);
2492 /* Initial Exec model. */
2493 if (*tls_type_p & GOT_TLS_IE)
2495 offset = got_offset;
2497 if (need_relocs)
2499 if (indx == 0)
2500 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
2501 sgot->contents + offset);
2502 else
2503 MIPS_ELF_PUT_WORD (abfd, 0,
2504 sgot->contents + offset);
2506 mips_elf_output_dynamic_relocation
2507 (abfd, sreloc, indx,
2508 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
2509 sgot->output_offset + sgot->output_section->vma + offset);
2511 else
2512 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
2513 sgot->contents + offset);
2516 if (*tls_type_p & GOT_TLS_LDM)
2518 /* The initial offset is zero, and the LD offsets will include the
2519 bias by DTP_OFFSET. */
2520 MIPS_ELF_PUT_WORD (abfd, 0,
2521 sgot->contents + got_offset
2522 + MIPS_ELF_GOT_SIZE (abfd));
2524 if (!info->shared)
2525 MIPS_ELF_PUT_WORD (abfd, 1,
2526 sgot->contents + got_offset);
2527 else
2528 mips_elf_output_dynamic_relocation
2529 (abfd, sreloc, indx,
2530 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2531 sgot->output_offset + sgot->output_section->vma + got_offset);
2534 *tls_type_p |= GOT_TLS_DONE;
2537 /* Return the GOT index to use for a relocation of type R_TYPE against
2538 a symbol accessed using TLS_TYPE models. The GOT entries for this
2539 symbol in this GOT start at GOT_INDEX. This function initializes the
2540 GOT entries and corresponding relocations. */
2542 static bfd_vma
2543 mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
2544 int r_type, struct bfd_link_info *info,
2545 struct mips_elf_link_hash_entry *h, bfd_vma symbol)
2547 BFD_ASSERT (r_type == R_MIPS_TLS_GOTTPREL || r_type == R_MIPS_TLS_GD
2548 || r_type == R_MIPS_TLS_LDM);
2550 mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
2552 if (r_type == R_MIPS_TLS_GOTTPREL)
2554 BFD_ASSERT (*tls_type & GOT_TLS_IE);
2555 if (*tls_type & GOT_TLS_GD)
2556 return got_index + 2 * MIPS_ELF_GOT_SIZE (abfd);
2557 else
2558 return got_index;
2561 if (r_type == R_MIPS_TLS_GD)
2563 BFD_ASSERT (*tls_type & GOT_TLS_GD);
2564 return got_index;
2567 if (r_type == R_MIPS_TLS_LDM)
2569 BFD_ASSERT (*tls_type & GOT_TLS_LDM);
2570 return got_index;
2573 return got_index;
2576 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
2577 for global symbol H. .got.plt comes before the GOT, so the offset
2578 will be negative. */
2580 static bfd_vma
2581 mips_elf_gotplt_index (struct bfd_link_info *info,
2582 struct elf_link_hash_entry *h)
2584 bfd_vma plt_index, got_address, got_value;
2585 struct mips_elf_link_hash_table *htab;
2587 htab = mips_elf_hash_table (info);
2588 BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
2590 /* Calculate the index of the symbol's PLT entry. */
2591 plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
2593 /* Calculate the address of the associated .got.plt entry. */
2594 got_address = (htab->sgotplt->output_section->vma
2595 + htab->sgotplt->output_offset
2596 + plt_index * 4);
2598 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
2599 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
2600 + htab->root.hgot->root.u.def.section->output_offset
2601 + htab->root.hgot->root.u.def.value);
2603 return got_address - got_value;
2606 /* Return the GOT offset for address VALUE. If there is not yet a GOT
2607 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
2608 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
2609 offset can be found. */
2611 static bfd_vma
2612 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
2613 bfd_vma value, unsigned long r_symndx,
2614 struct mips_elf_link_hash_entry *h, int r_type)
2616 asection *sgot;
2617 struct mips_got_info *g;
2618 struct mips_got_entry *entry;
2620 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
2622 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, g, sgot,
2623 value, r_symndx, h, r_type);
2624 if (!entry)
2625 return MINUS_ONE;
2627 if (TLS_RELOC_P (r_type))
2629 if (entry->symndx == -1 && g->next == NULL)
2630 /* A type (3) entry in the single-GOT case. We use the symbol's
2631 hash table entry to track the index. */
2632 return mips_tls_got_index (abfd, h->tls_got_offset, &h->tls_type,
2633 r_type, info, h, value);
2634 else
2635 return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type,
2636 r_type, info, h, value);
2638 else
2639 return entry->gotidx;
2642 /* Returns the GOT index for the global symbol indicated by H. */
2644 static bfd_vma
2645 mips_elf_global_got_index (bfd *abfd, bfd *ibfd, struct elf_link_hash_entry *h,
2646 int r_type, struct bfd_link_info *info)
2648 bfd_vma index;
2649 asection *sgot;
2650 struct mips_got_info *g, *gg;
2651 long global_got_dynindx = 0;
2653 gg = g = mips_elf_got_info (abfd, &sgot);
2654 if (g->bfd2got && ibfd)
2656 struct mips_got_entry e, *p;
2658 BFD_ASSERT (h->dynindx >= 0);
2660 g = mips_elf_got_for_ibfd (g, ibfd);
2661 if (g->next != gg || TLS_RELOC_P (r_type))
2663 e.abfd = ibfd;
2664 e.symndx = -1;
2665 e.d.h = (struct mips_elf_link_hash_entry *)h;
2666 e.tls_type = 0;
2668 p = htab_find (g->got_entries, &e);
2670 BFD_ASSERT (p->gotidx > 0);
2672 if (TLS_RELOC_P (r_type))
2674 bfd_vma value = MINUS_ONE;
2675 if ((h->root.type == bfd_link_hash_defined
2676 || h->root.type == bfd_link_hash_defweak)
2677 && h->root.u.def.section->output_section)
2678 value = (h->root.u.def.value
2679 + h->root.u.def.section->output_offset
2680 + h->root.u.def.section->output_section->vma);
2682 return mips_tls_got_index (abfd, p->gotidx, &p->tls_type, r_type,
2683 info, e.d.h, value);
2685 else
2686 return p->gotidx;
2690 if (gg->global_gotsym != NULL)
2691 global_got_dynindx = gg->global_gotsym->dynindx;
2693 if (TLS_RELOC_P (r_type))
2695 struct mips_elf_link_hash_entry *hm
2696 = (struct mips_elf_link_hash_entry *) h;
2697 bfd_vma value = MINUS_ONE;
2699 if ((h->root.type == bfd_link_hash_defined
2700 || h->root.type == bfd_link_hash_defweak)
2701 && h->root.u.def.section->output_section)
2702 value = (h->root.u.def.value
2703 + h->root.u.def.section->output_offset
2704 + h->root.u.def.section->output_section->vma);
2706 index = mips_tls_got_index (abfd, hm->tls_got_offset, &hm->tls_type,
2707 r_type, info, hm, value);
2709 else
2711 /* Once we determine the global GOT entry with the lowest dynamic
2712 symbol table index, we must put all dynamic symbols with greater
2713 indices into the GOT. That makes it easy to calculate the GOT
2714 offset. */
2715 BFD_ASSERT (h->dynindx >= global_got_dynindx);
2716 index = ((h->dynindx - global_got_dynindx + g->local_gotno)
2717 * MIPS_ELF_GOT_SIZE (abfd));
2719 BFD_ASSERT (index < sgot->size);
2721 return index;
2724 /* Find a GOT page entry that points to within 32KB of VALUE. These
2725 entries are supposed to be placed at small offsets in the GOT, i.e.,
2726 within 32KB of GP. Return the index of the GOT entry, or -1 if no
2727 entry could be created. If OFFSETP is nonnull, use it to return the
2728 offset of the GOT entry from VALUE. */
2730 static bfd_vma
2731 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
2732 bfd_vma value, bfd_vma *offsetp)
2734 asection *sgot;
2735 struct mips_got_info *g;
2736 bfd_vma page, index;
2737 struct mips_got_entry *entry;
2739 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
2741 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
2742 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, g, sgot,
2743 page, 0, NULL, R_MIPS_GOT_PAGE);
2745 if (!entry)
2746 return MINUS_ONE;
2748 index = entry->gotidx;
2750 if (offsetp)
2751 *offsetp = value - entry->d.address;
2753 return index;
2756 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
2757 EXTERNAL is true if the relocation was against a global symbol
2758 that has been forced local. */
2760 static bfd_vma
2761 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
2762 bfd_vma value, bfd_boolean external)
2764 asection *sgot;
2765 struct mips_got_info *g;
2766 struct mips_got_entry *entry;
2768 /* GOT16 relocations against local symbols are followed by a LO16
2769 relocation; those against global symbols are not. Thus if the
2770 symbol was originally local, the GOT16 relocation should load the
2771 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
2772 if (! external)
2773 value = mips_elf_high (value) << 16;
2775 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
2777 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
2778 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
2779 same in all cases. */
2780 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, g, sgot,
2781 value, 0, NULL, R_MIPS_GOT16);
2782 if (entry)
2783 return entry->gotidx;
2784 else
2785 return MINUS_ONE;
2788 /* Returns the offset for the entry at the INDEXth position
2789 in the GOT. */
2791 static bfd_vma
2792 mips_elf_got_offset_from_index (bfd *dynobj, bfd *output_bfd,
2793 bfd *input_bfd, bfd_vma index)
2795 asection *sgot;
2796 bfd_vma gp;
2797 struct mips_got_info *g;
2799 g = mips_elf_got_info (dynobj, &sgot);
2800 gp = _bfd_get_gp_value (output_bfd)
2801 + mips_elf_adjust_gp (output_bfd, g, input_bfd);
2803 return sgot->output_section->vma + sgot->output_offset + index - gp;
2806 /* Create and return a local GOT entry for VALUE, which was calculated
2807 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
2808 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
2809 instead. */
2811 static struct mips_got_entry *
2812 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
2813 bfd *ibfd, struct mips_got_info *gg,
2814 asection *sgot, bfd_vma value,
2815 unsigned long r_symndx,
2816 struct mips_elf_link_hash_entry *h,
2817 int r_type)
2819 struct mips_got_entry entry, **loc;
2820 struct mips_got_info *g;
2821 struct mips_elf_link_hash_table *htab;
2823 htab = mips_elf_hash_table (info);
2825 entry.abfd = NULL;
2826 entry.symndx = -1;
2827 entry.d.address = value;
2828 entry.tls_type = 0;
2830 g = mips_elf_got_for_ibfd (gg, ibfd);
2831 if (g == NULL)
2833 g = mips_elf_got_for_ibfd (gg, abfd);
2834 BFD_ASSERT (g != NULL);
2837 /* We might have a symbol, H, if it has been forced local. Use the
2838 global entry then. It doesn't matter whether an entry is local
2839 or global for TLS, since the dynamic linker does not
2840 automatically relocate TLS GOT entries. */
2841 BFD_ASSERT (h == NULL || h->root.forced_local);
2842 if (TLS_RELOC_P (r_type))
2844 struct mips_got_entry *p;
2846 entry.abfd = ibfd;
2847 if (r_type == R_MIPS_TLS_LDM)
2849 entry.tls_type = GOT_TLS_LDM;
2850 entry.symndx = 0;
2851 entry.d.addend = 0;
2853 else if (h == NULL)
2855 entry.symndx = r_symndx;
2856 entry.d.addend = 0;
2858 else
2859 entry.d.h = h;
2861 p = (struct mips_got_entry *)
2862 htab_find (g->got_entries, &entry);
2864 BFD_ASSERT (p);
2865 return p;
2868 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
2869 INSERT);
2870 if (*loc)
2871 return *loc;
2873 entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
2874 entry.tls_type = 0;
2876 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
2878 if (! *loc)
2879 return NULL;
2881 memcpy (*loc, &entry, sizeof entry);
2883 if (g->assigned_gotno > g->local_gotno)
2885 (*loc)->gotidx = -1;
2886 /* We didn't allocate enough space in the GOT. */
2887 (*_bfd_error_handler)
2888 (_("not enough GOT space for local GOT entries"));
2889 bfd_set_error (bfd_error_bad_value);
2890 return NULL;
2893 MIPS_ELF_PUT_WORD (abfd, value,
2894 (sgot->contents + entry.gotidx));
2896 /* These GOT entries need a dynamic relocation on VxWorks. */
2897 if (htab->is_vxworks)
2899 Elf_Internal_Rela outrel;
2900 asection *s;
2901 bfd_byte *loc;
2902 bfd_vma got_address;
2904 s = mips_elf_rel_dyn_section (info, FALSE);
2905 got_address = (sgot->output_section->vma
2906 + sgot->output_offset
2907 + entry.gotidx);
2909 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
2910 outrel.r_offset = got_address;
2911 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
2912 outrel.r_addend = value;
2913 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
2916 return *loc;
2919 /* Sort the dynamic symbol table so that symbols that need GOT entries
2920 appear towards the end. This reduces the amount of GOT space
2921 required. MAX_LOCAL is used to set the number of local symbols
2922 known to be in the dynamic symbol table. During
2923 _bfd_mips_elf_size_dynamic_sections, this value is 1. Afterward, the
2924 section symbols are added and the count is higher. */
2926 static bfd_boolean
2927 mips_elf_sort_hash_table (struct bfd_link_info *info, unsigned long max_local)
2929 struct mips_elf_hash_sort_data hsd;
2930 struct mips_got_info *g;
2931 bfd *dynobj;
2933 dynobj = elf_hash_table (info)->dynobj;
2935 g = mips_elf_got_info (dynobj, NULL);
2937 hsd.low = NULL;
2938 hsd.max_unref_got_dynindx =
2939 hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount
2940 /* In the multi-got case, assigned_gotno of the master got_info
2941 indicate the number of entries that aren't referenced in the
2942 primary GOT, but that must have entries because there are
2943 dynamic relocations that reference it. Since they aren't
2944 referenced, we move them to the end of the GOT, so that they
2945 don't prevent other entries that are referenced from getting
2946 too large offsets. */
2947 - (g->next ? g->assigned_gotno : 0);
2948 hsd.max_non_got_dynindx = max_local;
2949 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
2950 elf_hash_table (info)),
2951 mips_elf_sort_hash_table_f,
2952 &hsd);
2954 /* There should have been enough room in the symbol table to
2955 accommodate both the GOT and non-GOT symbols. */
2956 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
2957 BFD_ASSERT ((unsigned long)hsd.max_unref_got_dynindx
2958 <= elf_hash_table (info)->dynsymcount);
2960 /* Now we know which dynamic symbol has the lowest dynamic symbol
2961 table index in the GOT. */
2962 g->global_gotsym = hsd.low;
2964 return TRUE;
2967 /* If H needs a GOT entry, assign it the highest available dynamic
2968 index. Otherwise, assign it the lowest available dynamic
2969 index. */
2971 static bfd_boolean
2972 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
2974 struct mips_elf_hash_sort_data *hsd = data;
2976 if (h->root.root.type == bfd_link_hash_warning)
2977 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
2979 /* Symbols without dynamic symbol table entries aren't interesting
2980 at all. */
2981 if (h->root.dynindx == -1)
2982 return TRUE;
2984 /* Global symbols that need GOT entries that are not explicitly
2985 referenced are marked with got offset 2. Those that are
2986 referenced get a 1, and those that don't need GOT entries get
2987 -1. Forced local symbols may also be marked with got offset 1,
2988 but are never given global GOT entries. */
2989 if (h->root.got.offset == 2)
2991 BFD_ASSERT (h->tls_type == GOT_NORMAL);
2993 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
2994 hsd->low = (struct elf_link_hash_entry *) h;
2995 h->root.dynindx = hsd->max_unref_got_dynindx++;
2997 else if (h->root.got.offset != 1 || h->forced_local)
2998 h->root.dynindx = hsd->max_non_got_dynindx++;
2999 else
3001 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3003 h->root.dynindx = --hsd->min_got_dynindx;
3004 hsd->low = (struct elf_link_hash_entry *) h;
3007 return TRUE;
3010 /* If H is a symbol that needs a global GOT entry, but has a dynamic
3011 symbol table index lower than any we've seen to date, record it for
3012 posterity. */
3014 static bfd_boolean
3015 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3016 bfd *abfd, struct bfd_link_info *info,
3017 struct mips_got_info *g,
3018 unsigned char tls_flag)
3020 struct mips_got_entry entry, **loc;
3022 /* A global symbol in the GOT must also be in the dynamic symbol
3023 table. */
3024 if (h->dynindx == -1)
3026 switch (ELF_ST_VISIBILITY (h->other))
3028 case STV_INTERNAL:
3029 case STV_HIDDEN:
3030 _bfd_mips_elf_hide_symbol (info, h, TRUE);
3031 break;
3033 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3034 return FALSE;
3037 /* Make sure we have a GOT to put this entry into. */
3038 BFD_ASSERT (g != NULL);
3040 entry.abfd = abfd;
3041 entry.symndx = -1;
3042 entry.d.h = (struct mips_elf_link_hash_entry *) h;
3043 entry.tls_type = 0;
3045 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3046 INSERT);
3048 /* If we've already marked this entry as needing GOT space, we don't
3049 need to do it again. */
3050 if (*loc)
3052 (*loc)->tls_type |= tls_flag;
3053 return TRUE;
3056 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3058 if (! *loc)
3059 return FALSE;
3061 entry.gotidx = -1;
3062 entry.tls_type = tls_flag;
3064 memcpy (*loc, &entry, sizeof entry);
3066 if (h->got.offset != MINUS_ONE)
3067 return TRUE;
3069 if (tls_flag == 0)
3071 /* By setting this to a value other than -1, we are indicating that
3072 there needs to be a GOT entry for H. Avoid using zero, as the
3073 generic ELF copy_indirect_symbol tests for <= 0. */
3074 h->got.offset = 1;
3075 if (h->forced_local)
3076 g->local_gotno++;
3079 return TRUE;
3082 /* Reserve space in G for a GOT entry containing the value of symbol
3083 SYMNDX in input bfd ABDF, plus ADDEND. */
3085 static bfd_boolean
3086 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
3087 struct mips_got_info *g,
3088 unsigned char tls_flag)
3090 struct mips_got_entry entry, **loc;
3092 entry.abfd = abfd;
3093 entry.symndx = symndx;
3094 entry.d.addend = addend;
3095 entry.tls_type = tls_flag;
3096 loc = (struct mips_got_entry **)
3097 htab_find_slot (g->got_entries, &entry, INSERT);
3099 if (*loc)
3101 if (tls_flag == GOT_TLS_GD && !((*loc)->tls_type & GOT_TLS_GD))
3103 g->tls_gotno += 2;
3104 (*loc)->tls_type |= tls_flag;
3106 else if (tls_flag == GOT_TLS_IE && !((*loc)->tls_type & GOT_TLS_IE))
3108 g->tls_gotno += 1;
3109 (*loc)->tls_type |= tls_flag;
3111 return TRUE;
3114 if (tls_flag != 0)
3116 entry.gotidx = -1;
3117 entry.tls_type = tls_flag;
3118 if (tls_flag == GOT_TLS_IE)
3119 g->tls_gotno += 1;
3120 else if (tls_flag == GOT_TLS_GD)
3121 g->tls_gotno += 2;
3122 else if (g->tls_ldm_offset == MINUS_ONE)
3124 g->tls_ldm_offset = MINUS_TWO;
3125 g->tls_gotno += 2;
3128 else
3130 entry.gotidx = g->local_gotno++;
3131 entry.tls_type = 0;
3134 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3136 if (! *loc)
3137 return FALSE;
3139 memcpy (*loc, &entry, sizeof entry);
3141 return TRUE;
3144 /* Return the maximum number of GOT page entries required for RANGE. */
3146 static bfd_vma
3147 mips_elf_pages_for_range (const struct mips_got_page_range *range)
3149 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
3152 /* Record that ABFD has a page relocation against symbol SYMNDX and
3153 that ADDEND is the addend for that relocation. G is the GOT
3154 information. This function creates an upper bound on the number of
3155 GOT slots required; no attempt is made to combine references to
3156 non-overridable global symbols across multiple input files. */
3158 static bfd_boolean
3159 mips_elf_record_got_page_entry (bfd *abfd, long symndx, bfd_signed_vma addend,
3160 struct mips_got_info *g)
3162 struct mips_got_page_entry lookup, *entry;
3163 struct mips_got_page_range **range_ptr, *range;
3164 bfd_vma old_pages, new_pages;
3165 void **loc;
3167 /* Find the mips_got_page_entry hash table entry for this symbol. */
3168 lookup.abfd = abfd;
3169 lookup.symndx = symndx;
3170 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
3171 if (loc == NULL)
3172 return FALSE;
3174 /* Create a mips_got_page_entry if this is the first time we've
3175 seen the symbol. */
3176 entry = (struct mips_got_page_entry *) *loc;
3177 if (!entry)
3179 entry = bfd_alloc (abfd, sizeof (*entry));
3180 if (!entry)
3181 return FALSE;
3183 entry->abfd = abfd;
3184 entry->symndx = symndx;
3185 entry->ranges = NULL;
3186 entry->num_pages = 0;
3187 *loc = entry;
3190 /* Skip over ranges whose maximum extent cannot share a page entry
3191 with ADDEND. */
3192 range_ptr = &entry->ranges;
3193 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
3194 range_ptr = &(*range_ptr)->next;
3196 /* If we scanned to the end of the list, or found a range whose
3197 minimum extent cannot share a page entry with ADDEND, create
3198 a new singleton range. */
3199 range = *range_ptr;
3200 if (!range || addend < range->min_addend - 0xffff)
3202 range = bfd_alloc (abfd, sizeof (*range));
3203 if (!range)
3204 return FALSE;
3206 range->next = *range_ptr;
3207 range->min_addend = addend;
3208 range->max_addend = addend;
3210 *range_ptr = range;
3211 entry->num_pages++;
3212 g->page_gotno++;
3213 return TRUE;
3216 /* Remember how many pages the old range contributed. */
3217 old_pages = mips_elf_pages_for_range (range);
3219 /* Update the ranges. */
3220 if (addend < range->min_addend)
3221 range->min_addend = addend;
3222 else if (addend > range->max_addend)
3224 if (range->next && addend >= range->next->min_addend - 0xffff)
3226 old_pages += mips_elf_pages_for_range (range->next);
3227 range->max_addend = range->next->max_addend;
3228 range->next = range->next->next;
3230 else
3231 range->max_addend = addend;
3234 /* Record any change in the total estimate. */
3235 new_pages = mips_elf_pages_for_range (range);
3236 if (old_pages != new_pages)
3238 entry->num_pages += new_pages - old_pages;
3239 g->page_gotno += new_pages - old_pages;
3242 return TRUE;
3245 /* Compute the hash value of the bfd in a bfd2got hash entry. */
3247 static hashval_t
3248 mips_elf_bfd2got_entry_hash (const void *entry_)
3250 const struct mips_elf_bfd2got_hash *entry
3251 = (struct mips_elf_bfd2got_hash *)entry_;
3253 return entry->bfd->id;
3256 /* Check whether two hash entries have the same bfd. */
3258 static int
3259 mips_elf_bfd2got_entry_eq (const void *entry1, const void *entry2)
3261 const struct mips_elf_bfd2got_hash *e1
3262 = (const struct mips_elf_bfd2got_hash *)entry1;
3263 const struct mips_elf_bfd2got_hash *e2
3264 = (const struct mips_elf_bfd2got_hash *)entry2;
3266 return e1->bfd == e2->bfd;
3269 /* In a multi-got link, determine the GOT to be used for IBFD. G must
3270 be the master GOT data. */
3272 static struct mips_got_info *
3273 mips_elf_got_for_ibfd (struct mips_got_info *g, bfd *ibfd)
3275 struct mips_elf_bfd2got_hash e, *p;
3277 if (! g->bfd2got)
3278 return g;
3280 e.bfd = ibfd;
3281 p = htab_find (g->bfd2got, &e);
3282 return p ? p->g : NULL;
3285 /* Use BFD2GOT to find ABFD's got entry, creating one if none exists.
3286 Return NULL if an error occured. */
3288 static struct mips_got_info *
3289 mips_elf_get_got_for_bfd (struct htab *bfd2got, bfd *output_bfd,
3290 bfd *input_bfd)
3292 struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
3293 struct mips_got_info *g;
3294 void **bfdgotp;
3296 bfdgot_entry.bfd = input_bfd;
3297 bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
3298 bfdgot = (struct mips_elf_bfd2got_hash *) *bfdgotp;
3300 if (bfdgot == NULL)
3302 bfdgot = ((struct mips_elf_bfd2got_hash *)
3303 bfd_alloc (output_bfd, sizeof (struct mips_elf_bfd2got_hash)));
3304 if (bfdgot == NULL)
3305 return NULL;
3307 *bfdgotp = bfdgot;
3309 g = ((struct mips_got_info *)
3310 bfd_alloc (output_bfd, sizeof (struct mips_got_info)));
3311 if (g == NULL)
3312 return NULL;
3314 bfdgot->bfd = input_bfd;
3315 bfdgot->g = g;
3317 g->global_gotsym = NULL;
3318 g->global_gotno = 0;
3319 g->local_gotno = 0;
3320 g->page_gotno = 0;
3321 g->assigned_gotno = -1;
3322 g->tls_gotno = 0;
3323 g->tls_assigned_gotno = 0;
3324 g->tls_ldm_offset = MINUS_ONE;
3325 g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
3326 mips_elf_multi_got_entry_eq, NULL);
3327 if (g->got_entries == NULL)
3328 return NULL;
3330 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
3331 mips_got_page_entry_eq, NULL);
3332 if (g->got_page_entries == NULL)
3333 return NULL;
3335 g->bfd2got = NULL;
3336 g->next = NULL;
3339 return bfdgot->g;
3342 /* A htab_traverse callback for the entries in the master got.
3343 Create one separate got for each bfd that has entries in the global
3344 got, such that we can tell how many local and global entries each
3345 bfd requires. */
3347 static int
3348 mips_elf_make_got_per_bfd (void **entryp, void *p)
3350 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3351 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
3352 struct mips_got_info *g;
3354 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
3355 if (g == NULL)
3357 arg->obfd = NULL;
3358 return 0;
3361 /* Insert the GOT entry in the bfd's got entry hash table. */
3362 entryp = htab_find_slot (g->got_entries, entry, INSERT);
3363 if (*entryp != NULL)
3364 return 1;
3366 *entryp = entry;
3368 if (entry->tls_type)
3370 if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
3371 g->tls_gotno += 2;
3372 if (entry->tls_type & GOT_TLS_IE)
3373 g->tls_gotno += 1;
3375 else if (entry->symndx >= 0 || entry->d.h->forced_local)
3376 ++g->local_gotno;
3377 else
3378 ++g->global_gotno;
3380 return 1;
3383 /* A htab_traverse callback for the page entries in the master got.
3384 Associate each page entry with the bfd's got. */
3386 static int
3387 mips_elf_make_got_pages_per_bfd (void **entryp, void *p)
3389 struct mips_got_page_entry *entry = (struct mips_got_page_entry *) *entryp;
3390 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *) p;
3391 struct mips_got_info *g;
3393 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
3394 if (g == NULL)
3396 arg->obfd = NULL;
3397 return 0;
3400 /* Insert the GOT entry in the bfd's got entry hash table. */
3401 entryp = htab_find_slot (g->got_page_entries, entry, INSERT);
3402 if (*entryp != NULL)
3403 return 1;
3405 *entryp = entry;
3406 g->page_gotno += entry->num_pages;
3407 return 1;
3410 /* Consider merging the got described by BFD2GOT with TO, using the
3411 information given by ARG. Return -1 if this would lead to overflow,
3412 1 if they were merged successfully, and 0 if a merge failed due to
3413 lack of memory. (These values are chosen so that nonnegative return
3414 values can be returned by a htab_traverse callback.) */
3416 static int
3417 mips_elf_merge_got_with (struct mips_elf_bfd2got_hash *bfd2got,
3418 struct mips_got_info *to,
3419 struct mips_elf_got_per_bfd_arg *arg)
3421 struct mips_got_info *from = bfd2got->g;
3422 unsigned int estimate;
3424 /* Work out how many page entries we would need for the combined GOT. */
3425 estimate = arg->max_pages;
3426 if (estimate >= from->page_gotno + to->page_gotno)
3427 estimate = from->page_gotno + to->page_gotno;
3429 /* And conservatively estimate how many local, global and TLS entries
3430 would be needed. */
3431 estimate += (from->local_gotno
3432 + from->global_gotno
3433 + from->tls_gotno
3434 + to->local_gotno
3435 + to->global_gotno
3436 + to->tls_gotno);
3438 /* Bail out if the combined GOT might be too big. */
3439 if (estimate > arg->max_count)
3440 return -1;
3442 /* Commit to the merge. Record that TO is now the bfd for this got. */
3443 bfd2got->g = to;
3445 /* Transfer the bfd's got information from FROM to TO. */
3446 htab_traverse (from->got_entries, mips_elf_make_got_per_bfd, arg);
3447 if (arg->obfd == NULL)
3448 return 0;
3450 htab_traverse (from->got_page_entries, mips_elf_make_got_pages_per_bfd, arg);
3451 if (arg->obfd == NULL)
3452 return 0;
3454 /* We don't have to worry about releasing memory of the actual
3455 got entries, since they're all in the master got_entries hash
3456 table anyway. */
3457 htab_delete (from->got_entries);
3458 htab_delete (from->got_page_entries);
3459 return 1;
3462 /* Attempt to merge gots of different input bfds. Try to use as much
3463 as possible of the primary got, since it doesn't require explicit
3464 dynamic relocations, but don't use bfds that would reference global
3465 symbols out of the addressable range. Failing the primary got,
3466 attempt to merge with the current got, or finish the current got
3467 and then make make the new got current. */
3469 static int
3470 mips_elf_merge_gots (void **bfd2got_, void *p)
3472 struct mips_elf_bfd2got_hash *bfd2got
3473 = (struct mips_elf_bfd2got_hash *)*bfd2got_;
3474 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
3475 struct mips_got_info *g;
3476 unsigned int estimate;
3477 int result;
3479 g = bfd2got->g;
3481 /* Work out the number of page, local and TLS entries. */
3482 estimate = arg->max_pages;
3483 if (estimate > g->page_gotno)
3484 estimate = g->page_gotno;
3485 estimate += g->local_gotno + g->tls_gotno;
3487 /* We place TLS GOT entries after both locals and globals. The globals
3488 for the primary GOT may overflow the normal GOT size limit, so be
3489 sure not to merge a GOT which requires TLS with the primary GOT in that
3490 case. This doesn't affect non-primary GOTs. */
3491 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
3493 if (estimate <= arg->max_count)
3495 /* If we don't have a primary GOT, use it as
3496 a starting point for the primary GOT. */
3497 if (!arg->primary)
3499 arg->primary = bfd2got->g;
3500 return 1;
3503 /* Try merging with the primary GOT. */
3504 result = mips_elf_merge_got_with (bfd2got, arg->primary, arg);
3505 if (result >= 0)
3506 return result;
3509 /* If we can merge with the last-created got, do it. */
3510 if (arg->current)
3512 result = mips_elf_merge_got_with (bfd2got, arg->current, arg);
3513 if (result >= 0)
3514 return result;
3517 /* Well, we couldn't merge, so create a new GOT. Don't check if it
3518 fits; if it turns out that it doesn't, we'll get relocation
3519 overflows anyway. */
3520 g->next = arg->current;
3521 arg->current = g;
3523 return 1;
3526 /* Set the TLS GOT index for the GOT entry in ENTRYP. ENTRYP's NEXT field
3527 is null iff there is just a single GOT. */
3529 static int
3530 mips_elf_initialize_tls_index (void **entryp, void *p)
3532 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3533 struct mips_got_info *g = p;
3534 bfd_vma next_index;
3535 unsigned char tls_type;
3537 /* We're only interested in TLS symbols. */
3538 if (entry->tls_type == 0)
3539 return 1;
3541 next_index = MIPS_ELF_GOT_SIZE (entry->abfd) * (long) g->tls_assigned_gotno;
3543 if (entry->symndx == -1 && g->next == NULL)
3545 /* A type (3) got entry in the single-GOT case. We use the symbol's
3546 hash table entry to track its index. */
3547 if (entry->d.h->tls_type & GOT_TLS_OFFSET_DONE)
3548 return 1;
3549 entry->d.h->tls_type |= GOT_TLS_OFFSET_DONE;
3550 entry->d.h->tls_got_offset = next_index;
3551 tls_type = entry->d.h->tls_type;
3553 else
3555 if (entry->tls_type & GOT_TLS_LDM)
3557 /* There are separate mips_got_entry objects for each input bfd
3558 that requires an LDM entry. Make sure that all LDM entries in
3559 a GOT resolve to the same index. */
3560 if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
3562 entry->gotidx = g->tls_ldm_offset;
3563 return 1;
3565 g->tls_ldm_offset = next_index;
3567 entry->gotidx = next_index;
3568 tls_type = entry->tls_type;
3571 /* Account for the entries we've just allocated. */
3572 if (tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
3573 g->tls_assigned_gotno += 2;
3574 if (tls_type & GOT_TLS_IE)
3575 g->tls_assigned_gotno += 1;
3577 return 1;
3580 /* If passed a NULL mips_got_info in the argument, set the marker used
3581 to tell whether a global symbol needs a got entry (in the primary
3582 got) to the given VALUE.
3584 If passed a pointer G to a mips_got_info in the argument (it must
3585 not be the primary GOT), compute the offset from the beginning of
3586 the (primary) GOT section to the entry in G corresponding to the
3587 global symbol. G's assigned_gotno must contain the index of the
3588 first available global GOT entry in G. VALUE must contain the size
3589 of a GOT entry in bytes. For each global GOT entry that requires a
3590 dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
3591 marked as not eligible for lazy resolution through a function
3592 stub. */
3593 static int
3594 mips_elf_set_global_got_offset (void **entryp, void *p)
3596 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3597 struct mips_elf_set_global_got_offset_arg *arg
3598 = (struct mips_elf_set_global_got_offset_arg *)p;
3599 struct mips_got_info *g = arg->g;
3601 if (g && entry->tls_type != GOT_NORMAL)
3602 arg->needed_relocs +=
3603 mips_tls_got_relocs (arg->info, entry->tls_type,
3604 entry->symndx == -1 ? &entry->d.h->root : NULL);
3606 if (entry->abfd != NULL && entry->symndx == -1
3607 && entry->d.h->root.dynindx != -1
3608 && !entry->d.h->forced_local
3609 && entry->d.h->tls_type == GOT_NORMAL)
3611 if (g)
3613 BFD_ASSERT (g->global_gotsym == NULL);
3615 entry->gotidx = arg->value * (long) g->assigned_gotno++;
3616 if (arg->info->shared
3617 || (elf_hash_table (arg->info)->dynamic_sections_created
3618 && entry->d.h->root.def_dynamic
3619 && !entry->d.h->root.def_regular))
3620 ++arg->needed_relocs;
3622 else
3623 entry->d.h->root.got.offset = arg->value;
3626 return 1;
3629 /* Mark any global symbols referenced in the GOT we are iterating over
3630 as inelligible for lazy resolution stubs. */
3631 static int
3632 mips_elf_set_no_stub (void **entryp, void *p ATTRIBUTE_UNUSED)
3634 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3636 if (entry->abfd != NULL
3637 && entry->symndx == -1
3638 && entry->d.h->root.dynindx != -1)
3639 entry->d.h->no_fn_stub = TRUE;
3641 return 1;
3644 /* Follow indirect and warning hash entries so that each got entry
3645 points to the final symbol definition. P must point to a pointer
3646 to the hash table we're traversing. Since this traversal may
3647 modify the hash table, we set this pointer to NULL to indicate
3648 we've made a potentially-destructive change to the hash table, so
3649 the traversal must be restarted. */
3650 static int
3651 mips_elf_resolve_final_got_entry (void **entryp, void *p)
3653 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3654 htab_t got_entries = *(htab_t *)p;
3656 if (entry->abfd != NULL && entry->symndx == -1)
3658 struct mips_elf_link_hash_entry *h = entry->d.h;
3660 while (h->root.root.type == bfd_link_hash_indirect
3661 || h->root.root.type == bfd_link_hash_warning)
3662 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3664 if (entry->d.h == h)
3665 return 1;
3667 entry->d.h = h;
3669 /* If we can't find this entry with the new bfd hash, re-insert
3670 it, and get the traversal restarted. */
3671 if (! htab_find (got_entries, entry))
3673 htab_clear_slot (got_entries, entryp);
3674 entryp = htab_find_slot (got_entries, entry, INSERT);
3675 if (! *entryp)
3676 *entryp = entry;
3677 /* Abort the traversal, since the whole table may have
3678 moved, and leave it up to the parent to restart the
3679 process. */
3680 *(htab_t *)p = NULL;
3681 return 0;
3683 /* We might want to decrement the global_gotno count, but it's
3684 either too early or too late for that at this point. */
3687 return 1;
3690 /* Turn indirect got entries in a got_entries table into their final
3691 locations. */
3692 static void
3693 mips_elf_resolve_final_got_entries (struct mips_got_info *g)
3695 htab_t got_entries;
3699 got_entries = g->got_entries;
3701 htab_traverse (got_entries,
3702 mips_elf_resolve_final_got_entry,
3703 &got_entries);
3705 while (got_entries == NULL);
3708 /* Return the offset of an input bfd IBFD's GOT from the beginning of
3709 the primary GOT. */
3710 static bfd_vma
3711 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
3713 if (g->bfd2got == NULL)
3714 return 0;
3716 g = mips_elf_got_for_ibfd (g, ibfd);
3717 if (! g)
3718 return 0;
3720 BFD_ASSERT (g->next);
3722 g = g->next;
3724 return (g->local_gotno + g->global_gotno + g->tls_gotno)
3725 * MIPS_ELF_GOT_SIZE (abfd);
3728 /* Turn a single GOT that is too big for 16-bit addressing into
3729 a sequence of GOTs, each one 16-bit addressable. */
3731 static bfd_boolean
3732 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
3733 struct mips_got_info *g, asection *got,
3734 bfd_size_type pages)
3736 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
3737 struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
3738 struct mips_got_info *gg;
3739 unsigned int assign;
3741 g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
3742 mips_elf_bfd2got_entry_eq, NULL);
3743 if (g->bfd2got == NULL)
3744 return FALSE;
3746 got_per_bfd_arg.bfd2got = g->bfd2got;
3747 got_per_bfd_arg.obfd = abfd;
3748 got_per_bfd_arg.info = info;
3750 /* Count how many GOT entries each input bfd requires, creating a
3751 map from bfd to got info while at that. */
3752 htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
3753 if (got_per_bfd_arg.obfd == NULL)
3754 return FALSE;
3756 /* Also count how many page entries each input bfd requires. */
3757 htab_traverse (g->got_page_entries, mips_elf_make_got_pages_per_bfd,
3758 &got_per_bfd_arg);
3759 if (got_per_bfd_arg.obfd == NULL)
3760 return FALSE;
3762 got_per_bfd_arg.current = NULL;
3763 got_per_bfd_arg.primary = NULL;
3764 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
3765 / MIPS_ELF_GOT_SIZE (abfd))
3766 - MIPS_RESERVED_GOTNO (info));
3767 got_per_bfd_arg.max_pages = pages;
3768 /* The number of globals that will be included in the primary GOT.
3769 See the calls to mips_elf_set_global_got_offset below for more
3770 information. */
3771 got_per_bfd_arg.global_count = g->global_gotno;
3773 /* Try to merge the GOTs of input bfds together, as long as they
3774 don't seem to exceed the maximum GOT size, choosing one of them
3775 to be the primary GOT. */
3776 htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
3777 if (got_per_bfd_arg.obfd == NULL)
3778 return FALSE;
3780 /* If we do not find any suitable primary GOT, create an empty one. */
3781 if (got_per_bfd_arg.primary == NULL)
3783 g->next = (struct mips_got_info *)
3784 bfd_alloc (abfd, sizeof (struct mips_got_info));
3785 if (g->next == NULL)
3786 return FALSE;
3788 g->next->global_gotsym = NULL;
3789 g->next->global_gotno = 0;
3790 g->next->local_gotno = 0;
3791 g->next->page_gotno = 0;
3792 g->next->tls_gotno = 0;
3793 g->next->assigned_gotno = 0;
3794 g->next->tls_assigned_gotno = 0;
3795 g->next->tls_ldm_offset = MINUS_ONE;
3796 g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
3797 mips_elf_multi_got_entry_eq,
3798 NULL);
3799 if (g->next->got_entries == NULL)
3800 return FALSE;
3801 g->next->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
3802 mips_got_page_entry_eq,
3803 NULL);
3804 if (g->next->got_page_entries == NULL)
3805 return FALSE;
3806 g->next->bfd2got = NULL;
3808 else
3809 g->next = got_per_bfd_arg.primary;
3810 g->next->next = got_per_bfd_arg.current;
3812 /* GG is now the master GOT, and G is the primary GOT. */
3813 gg = g;
3814 g = g->next;
3816 /* Map the output bfd to the primary got. That's what we're going
3817 to use for bfds that use GOT16 or GOT_PAGE relocations that we
3818 didn't mark in check_relocs, and we want a quick way to find it.
3819 We can't just use gg->next because we're going to reverse the
3820 list. */
3822 struct mips_elf_bfd2got_hash *bfdgot;
3823 void **bfdgotp;
3825 bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
3826 (abfd, sizeof (struct mips_elf_bfd2got_hash));
3828 if (bfdgot == NULL)
3829 return FALSE;
3831 bfdgot->bfd = abfd;
3832 bfdgot->g = g;
3833 bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
3835 BFD_ASSERT (*bfdgotp == NULL);
3836 *bfdgotp = bfdgot;
3839 /* The IRIX dynamic linker requires every symbol that is referenced
3840 in a dynamic relocation to be present in the primary GOT, so
3841 arrange for them to appear after those that are actually
3842 referenced.
3844 GNU/Linux could very well do without it, but it would slow down
3845 the dynamic linker, since it would have to resolve every dynamic
3846 symbol referenced in other GOTs more than once, without help from
3847 the cache. Also, knowing that every external symbol has a GOT
3848 helps speed up the resolution of local symbols too, so GNU/Linux
3849 follows IRIX's practice.
3851 The number 2 is used by mips_elf_sort_hash_table_f to count
3852 global GOT symbols that are unreferenced in the primary GOT, with
3853 an initial dynamic index computed from gg->assigned_gotno, where
3854 the number of unreferenced global entries in the primary GOT is
3855 preserved. */
3856 if (1)
3858 gg->assigned_gotno = gg->global_gotno - g->global_gotno;
3859 g->global_gotno = gg->global_gotno;
3860 set_got_offset_arg.value = 2;
3862 else
3864 /* This could be used for dynamic linkers that don't optimize
3865 symbol resolution while applying relocations so as to use
3866 primary GOT entries or assuming the symbol is locally-defined.
3867 With this code, we assign lower dynamic indices to global
3868 symbols that are not referenced in the primary GOT, so that
3869 their entries can be omitted. */
3870 gg->assigned_gotno = 0;
3871 set_got_offset_arg.value = -1;
3874 /* Reorder dynamic symbols as described above (which behavior
3875 depends on the setting of VALUE). */
3876 set_got_offset_arg.g = NULL;
3877 htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
3878 &set_got_offset_arg);
3879 set_got_offset_arg.value = 1;
3880 htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
3881 &set_got_offset_arg);
3882 if (! mips_elf_sort_hash_table (info, 1))
3883 return FALSE;
3885 /* Now go through the GOTs assigning them offset ranges.
3886 [assigned_gotno, local_gotno[ will be set to the range of local
3887 entries in each GOT. We can then compute the end of a GOT by
3888 adding local_gotno to global_gotno. We reverse the list and make
3889 it circular since then we'll be able to quickly compute the
3890 beginning of a GOT, by computing the end of its predecessor. To
3891 avoid special cases for the primary GOT, while still preserving
3892 assertions that are valid for both single- and multi-got links,
3893 we arrange for the main got struct to have the right number of
3894 global entries, but set its local_gotno such that the initial
3895 offset of the primary GOT is zero. Remember that the primary GOT
3896 will become the last item in the circular linked list, so it
3897 points back to the master GOT. */
3898 gg->local_gotno = -g->global_gotno;
3899 gg->global_gotno = g->global_gotno;
3900 gg->tls_gotno = 0;
3901 assign = 0;
3902 gg->next = gg;
3906 struct mips_got_info *gn;
3908 assign += MIPS_RESERVED_GOTNO (info);
3909 g->assigned_gotno = assign;
3910 g->local_gotno += assign;
3911 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
3912 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
3914 /* Take g out of the direct list, and push it onto the reversed
3915 list that gg points to. g->next is guaranteed to be nonnull after
3916 this operation, as required by mips_elf_initialize_tls_index. */
3917 gn = g->next;
3918 g->next = gg->next;
3919 gg->next = g;
3921 /* Set up any TLS entries. We always place the TLS entries after
3922 all non-TLS entries. */
3923 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
3924 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
3926 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
3927 g = gn;
3929 /* Mark global symbols in every non-primary GOT as ineligible for
3930 stubs. */
3931 if (g)
3932 htab_traverse (g->got_entries, mips_elf_set_no_stub, NULL);
3934 while (g);
3936 got->size = (gg->next->local_gotno
3937 + gg->next->global_gotno
3938 + gg->next->tls_gotno) * MIPS_ELF_GOT_SIZE (abfd);
3940 return TRUE;
3944 /* Returns the first relocation of type r_type found, beginning with
3945 RELOCATION. RELEND is one-past-the-end of the relocation table. */
3947 static const Elf_Internal_Rela *
3948 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
3949 const Elf_Internal_Rela *relocation,
3950 const Elf_Internal_Rela *relend)
3952 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
3954 while (relocation < relend)
3956 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
3957 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
3958 return relocation;
3960 ++relocation;
3963 /* We didn't find it. */
3964 return NULL;
3967 /* Return whether a relocation is against a local symbol. */
3969 static bfd_boolean
3970 mips_elf_local_relocation_p (bfd *input_bfd,
3971 const Elf_Internal_Rela *relocation,
3972 asection **local_sections,
3973 bfd_boolean check_forced)
3975 unsigned long r_symndx;
3976 Elf_Internal_Shdr *symtab_hdr;
3977 struct mips_elf_link_hash_entry *h;
3978 size_t extsymoff;
3980 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
3981 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3982 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
3984 if (r_symndx < extsymoff)
3985 return TRUE;
3986 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
3987 return TRUE;
3989 if (check_forced)
3991 /* Look up the hash table to check whether the symbol
3992 was forced local. */
3993 h = (struct mips_elf_link_hash_entry *)
3994 elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
3995 /* Find the real hash-table entry for this symbol. */
3996 while (h->root.root.type == bfd_link_hash_indirect
3997 || h->root.root.type == bfd_link_hash_warning)
3998 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3999 if (h->root.forced_local)
4000 return TRUE;
4003 return FALSE;
4006 /* Sign-extend VALUE, which has the indicated number of BITS. */
4008 bfd_vma
4009 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
4011 if (value & ((bfd_vma) 1 << (bits - 1)))
4012 /* VALUE is negative. */
4013 value |= ((bfd_vma) - 1) << bits;
4015 return value;
4018 /* Return non-zero if the indicated VALUE has overflowed the maximum
4019 range expressible by a signed number with the indicated number of
4020 BITS. */
4022 static bfd_boolean
4023 mips_elf_overflow_p (bfd_vma value, int bits)
4025 bfd_signed_vma svalue = (bfd_signed_vma) value;
4027 if (svalue > (1 << (bits - 1)) - 1)
4028 /* The value is too big. */
4029 return TRUE;
4030 else if (svalue < -(1 << (bits - 1)))
4031 /* The value is too small. */
4032 return TRUE;
4034 /* All is well. */
4035 return FALSE;
4038 /* Calculate the %high function. */
4040 static bfd_vma
4041 mips_elf_high (bfd_vma value)
4043 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4046 /* Calculate the %higher function. */
4048 static bfd_vma
4049 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
4051 #ifdef BFD64
4052 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4053 #else
4054 abort ();
4055 return MINUS_ONE;
4056 #endif
4059 /* Calculate the %highest function. */
4061 static bfd_vma
4062 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
4064 #ifdef BFD64
4065 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
4066 #else
4067 abort ();
4068 return MINUS_ONE;
4069 #endif
4072 /* Create the .compact_rel section. */
4074 static bfd_boolean
4075 mips_elf_create_compact_rel_section
4076 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
4078 flagword flags;
4079 register asection *s;
4081 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
4083 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
4084 | SEC_READONLY);
4086 s = bfd_make_section_with_flags (abfd, ".compact_rel", flags);
4087 if (s == NULL
4088 || ! bfd_set_section_alignment (abfd, s,
4089 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
4090 return FALSE;
4092 s->size = sizeof (Elf32_External_compact_rel);
4095 return TRUE;
4098 /* Create the .got section to hold the global offset table. */
4100 static bfd_boolean
4101 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info,
4102 bfd_boolean maybe_exclude)
4104 flagword flags;
4105 register asection *s;
4106 struct elf_link_hash_entry *h;
4107 struct bfd_link_hash_entry *bh;
4108 struct mips_got_info *g;
4109 bfd_size_type amt;
4110 struct mips_elf_link_hash_table *htab;
4112 htab = mips_elf_hash_table (info);
4114 /* This function may be called more than once. */
4115 s = mips_elf_got_section (abfd, TRUE);
4116 if (s)
4118 if (! maybe_exclude)
4119 s->flags &= ~SEC_EXCLUDE;
4120 return TRUE;
4123 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4124 | SEC_LINKER_CREATED);
4126 if (maybe_exclude)
4127 flags |= SEC_EXCLUDE;
4129 /* We have to use an alignment of 2**4 here because this is hardcoded
4130 in the function stub generation and in the linker script. */
4131 s = bfd_make_section_with_flags (abfd, ".got", flags);
4132 if (s == NULL
4133 || ! bfd_set_section_alignment (abfd, s, 4))
4134 return FALSE;
4136 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
4137 linker script because we don't want to define the symbol if we
4138 are not creating a global offset table. */
4139 bh = NULL;
4140 if (! (_bfd_generic_link_add_one_symbol
4141 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
4142 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
4143 return FALSE;
4145 h = (struct elf_link_hash_entry *) bh;
4146 h->non_elf = 0;
4147 h->def_regular = 1;
4148 h->type = STT_OBJECT;
4149 elf_hash_table (info)->hgot = h;
4151 if (info->shared
4152 && ! bfd_elf_link_record_dynamic_symbol (info, h))
4153 return FALSE;
4155 amt = sizeof (struct mips_got_info);
4156 g = bfd_alloc (abfd, amt);
4157 if (g == NULL)
4158 return FALSE;
4159 g->global_gotsym = NULL;
4160 g->global_gotno = 0;
4161 g->tls_gotno = 0;
4162 g->local_gotno = MIPS_RESERVED_GOTNO (info);
4163 g->page_gotno = 0;
4164 g->assigned_gotno = MIPS_RESERVED_GOTNO (info);
4165 g->bfd2got = NULL;
4166 g->next = NULL;
4167 g->tls_ldm_offset = MINUS_ONE;
4168 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
4169 mips_elf_got_entry_eq, NULL);
4170 if (g->got_entries == NULL)
4171 return FALSE;
4172 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4173 mips_got_page_entry_eq, NULL);
4174 if (g->got_page_entries == NULL)
4175 return FALSE;
4176 mips_elf_section_data (s)->u.got_info = g;
4177 mips_elf_section_data (s)->elf.this_hdr.sh_flags
4178 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
4180 /* VxWorks also needs a .got.plt section. */
4181 if (htab->is_vxworks)
4183 s = bfd_make_section_with_flags (abfd, ".got.plt",
4184 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
4185 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4186 if (s == NULL || !bfd_set_section_alignment (abfd, s, 4))
4187 return FALSE;
4189 htab->sgotplt = s;
4191 return TRUE;
4194 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
4195 __GOTT_INDEX__ symbols. These symbols are only special for
4196 shared objects; they are not used in executables. */
4198 static bfd_boolean
4199 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
4201 return (mips_elf_hash_table (info)->is_vxworks
4202 && info->shared
4203 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
4204 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
4207 /* Calculate the value produced by the RELOCATION (which comes from
4208 the INPUT_BFD). The ADDEND is the addend to use for this
4209 RELOCATION; RELOCATION->R_ADDEND is ignored.
4211 The result of the relocation calculation is stored in VALUEP.
4212 REQUIRE_JALXP indicates whether or not the opcode used with this
4213 relocation must be JALX.
4215 This function returns bfd_reloc_continue if the caller need take no
4216 further action regarding this relocation, bfd_reloc_notsupported if
4217 something goes dramatically wrong, bfd_reloc_overflow if an
4218 overflow occurs, and bfd_reloc_ok to indicate success. */
4220 static bfd_reloc_status_type
4221 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
4222 asection *input_section,
4223 struct bfd_link_info *info,
4224 const Elf_Internal_Rela *relocation,
4225 bfd_vma addend, reloc_howto_type *howto,
4226 Elf_Internal_Sym *local_syms,
4227 asection **local_sections, bfd_vma *valuep,
4228 const char **namep, bfd_boolean *require_jalxp,
4229 bfd_boolean save_addend)
4231 /* The eventual value we will return. */
4232 bfd_vma value;
4233 /* The address of the symbol against which the relocation is
4234 occurring. */
4235 bfd_vma symbol = 0;
4236 /* The final GP value to be used for the relocatable, executable, or
4237 shared object file being produced. */
4238 bfd_vma gp;
4239 /* The place (section offset or address) of the storage unit being
4240 relocated. */
4241 bfd_vma p;
4242 /* The value of GP used to create the relocatable object. */
4243 bfd_vma gp0;
4244 /* The offset into the global offset table at which the address of
4245 the relocation entry symbol, adjusted by the addend, resides
4246 during execution. */
4247 bfd_vma g = MINUS_ONE;
4248 /* The section in which the symbol referenced by the relocation is
4249 located. */
4250 asection *sec = NULL;
4251 struct mips_elf_link_hash_entry *h = NULL;
4252 /* TRUE if the symbol referred to by this relocation is a local
4253 symbol. */
4254 bfd_boolean local_p, was_local_p;
4255 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
4256 bfd_boolean gp_disp_p = FALSE;
4257 /* TRUE if the symbol referred to by this relocation is
4258 "__gnu_local_gp". */
4259 bfd_boolean gnu_local_gp_p = FALSE;
4260 Elf_Internal_Shdr *symtab_hdr;
4261 size_t extsymoff;
4262 unsigned long r_symndx;
4263 int r_type;
4264 /* TRUE if overflow occurred during the calculation of the
4265 relocation value. */
4266 bfd_boolean overflowed_p;
4267 /* TRUE if this relocation refers to a MIPS16 function. */
4268 bfd_boolean target_is_16_bit_code_p = FALSE;
4269 struct mips_elf_link_hash_table *htab;
4270 bfd *dynobj;
4272 dynobj = elf_hash_table (info)->dynobj;
4273 htab = mips_elf_hash_table (info);
4275 /* Parse the relocation. */
4276 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4277 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
4278 p = (input_section->output_section->vma
4279 + input_section->output_offset
4280 + relocation->r_offset);
4282 /* Assume that there will be no overflow. */
4283 overflowed_p = FALSE;
4285 /* Figure out whether or not the symbol is local, and get the offset
4286 used in the array of hash table entries. */
4287 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4288 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
4289 local_sections, FALSE);
4290 was_local_p = local_p;
4291 if (! elf_bad_symtab (input_bfd))
4292 extsymoff = symtab_hdr->sh_info;
4293 else
4295 /* The symbol table does not follow the rule that local symbols
4296 must come before globals. */
4297 extsymoff = 0;
4300 /* Figure out the value of the symbol. */
4301 if (local_p)
4303 Elf_Internal_Sym *sym;
4305 sym = local_syms + r_symndx;
4306 sec = local_sections[r_symndx];
4308 symbol = sec->output_section->vma + sec->output_offset;
4309 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
4310 || (sec->flags & SEC_MERGE))
4311 symbol += sym->st_value;
4312 if ((sec->flags & SEC_MERGE)
4313 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4315 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
4316 addend -= symbol;
4317 addend += sec->output_section->vma + sec->output_offset;
4320 /* MIPS16 text labels should be treated as odd. */
4321 if (ELF_ST_IS_MIPS16 (sym->st_other))
4322 ++symbol;
4324 /* Record the name of this symbol, for our caller. */
4325 *namep = bfd_elf_string_from_elf_section (input_bfd,
4326 symtab_hdr->sh_link,
4327 sym->st_name);
4328 if (*namep == '\0')
4329 *namep = bfd_section_name (input_bfd, sec);
4331 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
4333 else
4335 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
4337 /* For global symbols we look up the symbol in the hash-table. */
4338 h = ((struct mips_elf_link_hash_entry *)
4339 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
4340 /* Find the real hash-table entry for this symbol. */
4341 while (h->root.root.type == bfd_link_hash_indirect
4342 || h->root.root.type == bfd_link_hash_warning)
4343 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4345 /* Record the name of this symbol, for our caller. */
4346 *namep = h->root.root.root.string;
4348 /* See if this is the special _gp_disp symbol. Note that such a
4349 symbol must always be a global symbol. */
4350 if (strcmp (*namep, "_gp_disp") == 0
4351 && ! NEWABI_P (input_bfd))
4353 /* Relocations against _gp_disp are permitted only with
4354 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
4355 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
4356 return bfd_reloc_notsupported;
4358 gp_disp_p = TRUE;
4360 /* See if this is the special _gp symbol. Note that such a
4361 symbol must always be a global symbol. */
4362 else if (strcmp (*namep, "__gnu_local_gp") == 0)
4363 gnu_local_gp_p = TRUE;
4366 /* If this symbol is defined, calculate its address. Note that
4367 _gp_disp is a magic symbol, always implicitly defined by the
4368 linker, so it's inappropriate to check to see whether or not
4369 its defined. */
4370 else if ((h->root.root.type == bfd_link_hash_defined
4371 || h->root.root.type == bfd_link_hash_defweak)
4372 && h->root.root.u.def.section)
4374 sec = h->root.root.u.def.section;
4375 if (sec->output_section)
4376 symbol = (h->root.root.u.def.value
4377 + sec->output_section->vma
4378 + sec->output_offset);
4379 else
4380 symbol = h->root.root.u.def.value;
4382 else if (h->root.root.type == bfd_link_hash_undefweak)
4383 /* We allow relocations against undefined weak symbols, giving
4384 it the value zero, so that you can undefined weak functions
4385 and check to see if they exist by looking at their
4386 addresses. */
4387 symbol = 0;
4388 else if (info->unresolved_syms_in_objects == RM_IGNORE
4389 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
4390 symbol = 0;
4391 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
4392 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
4394 /* If this is a dynamic link, we should have created a
4395 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
4396 in in _bfd_mips_elf_create_dynamic_sections.
4397 Otherwise, we should define the symbol with a value of 0.
4398 FIXME: It should probably get into the symbol table
4399 somehow as well. */
4400 BFD_ASSERT (! info->shared);
4401 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
4402 symbol = 0;
4404 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
4406 /* This is an optional symbol - an Irix specific extension to the
4407 ELF spec. Ignore it for now.
4408 XXX - FIXME - there is more to the spec for OPTIONAL symbols
4409 than simply ignoring them, but we do not handle this for now.
4410 For information see the "64-bit ELF Object File Specification"
4411 which is available from here:
4412 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
4413 symbol = 0;
4415 else
4417 if (! ((*info->callbacks->undefined_symbol)
4418 (info, h->root.root.root.string, input_bfd,
4419 input_section, relocation->r_offset,
4420 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
4421 || ELF_ST_VISIBILITY (h->root.other))))
4422 return bfd_reloc_undefined;
4423 symbol = 0;
4426 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
4429 /* If this is a reference to a 16-bit function with a stub, we need
4430 to redirect the relocation to the stub unless:
4432 (a) the relocation is for a MIPS16 JAL;
4434 (b) the relocation is for a MIPS16 PIC call, and there are no
4435 non-MIPS16 uses of the GOT slot; or
4437 (c) the section allows direct references to MIPS16 functions. */
4438 if (r_type != R_MIPS16_26
4439 && !info->relocatable
4440 && ((h != NULL
4441 && h->fn_stub != NULL
4442 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
4443 || (local_p
4444 && elf_tdata (input_bfd)->local_stubs != NULL
4445 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
4446 && !section_allows_mips16_refs_p (input_section))
4448 /* This is a 32- or 64-bit call to a 16-bit function. We should
4449 have already noticed that we were going to need the
4450 stub. */
4451 if (local_p)
4452 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
4453 else
4455 BFD_ASSERT (h->need_fn_stub);
4456 sec = h->fn_stub;
4459 symbol = sec->output_section->vma + sec->output_offset;
4460 /* The target is 16-bit, but the stub isn't. */
4461 target_is_16_bit_code_p = FALSE;
4463 /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
4464 need to redirect the call to the stub. Note that we specifically
4465 exclude R_MIPS16_CALL16 from this behavior; indirect calls should
4466 use an indirect stub instead. */
4467 else if (r_type == R_MIPS16_26 && !info->relocatable
4468 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
4469 || (local_p
4470 && elf_tdata (input_bfd)->local_call_stubs != NULL
4471 && elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
4472 && !target_is_16_bit_code_p)
4474 if (local_p)
4475 sec = elf_tdata (input_bfd)->local_call_stubs[r_symndx];
4476 else
4478 /* If both call_stub and call_fp_stub are defined, we can figure
4479 out which one to use by checking which one appears in the input
4480 file. */
4481 if (h->call_stub != NULL && h->call_fp_stub != NULL)
4483 asection *o;
4485 sec = NULL;
4486 for (o = input_bfd->sections; o != NULL; o = o->next)
4488 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
4490 sec = h->call_fp_stub;
4491 break;
4494 if (sec == NULL)
4495 sec = h->call_stub;
4497 else if (h->call_stub != NULL)
4498 sec = h->call_stub;
4499 else
4500 sec = h->call_fp_stub;
4503 BFD_ASSERT (sec->size > 0);
4504 symbol = sec->output_section->vma + sec->output_offset;
4507 /* Calls from 16-bit code to 32-bit code and vice versa require the
4508 special jalx instruction. */
4509 *require_jalxp = (!info->relocatable
4510 && (((r_type == R_MIPS16_26) && !target_is_16_bit_code_p)
4511 || ((r_type == R_MIPS_26) && target_is_16_bit_code_p)));
4513 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
4514 local_sections, TRUE);
4516 gp0 = _bfd_get_gp_value (input_bfd);
4517 gp = _bfd_get_gp_value (abfd);
4518 if (dynobj)
4519 gp += mips_elf_adjust_gp (abfd, mips_elf_got_info (dynobj, NULL),
4520 input_bfd);
4522 if (gnu_local_gp_p)
4523 symbol = gp;
4525 /* If we haven't already determined the GOT offset, oand we're going
4526 to need it, get it now. */
4527 switch (r_type)
4529 case R_MIPS_GOT_PAGE:
4530 case R_MIPS_GOT_OFST:
4531 /* We need to decay to GOT_DISP/addend if the symbol doesn't
4532 bind locally. */
4533 local_p = local_p || _bfd_elf_symbol_refs_local_p (&h->root, info, 1);
4534 if (local_p || r_type == R_MIPS_GOT_OFST)
4535 break;
4536 /* Fall through. */
4538 case R_MIPS16_CALL16:
4539 case R_MIPS16_GOT16:
4540 case R_MIPS_CALL16:
4541 case R_MIPS_GOT16:
4542 case R_MIPS_GOT_DISP:
4543 case R_MIPS_GOT_HI16:
4544 case R_MIPS_CALL_HI16:
4545 case R_MIPS_GOT_LO16:
4546 case R_MIPS_CALL_LO16:
4547 case R_MIPS_TLS_GD:
4548 case R_MIPS_TLS_GOTTPREL:
4549 case R_MIPS_TLS_LDM:
4550 /* Find the index into the GOT where this value is located. */
4551 if (r_type == R_MIPS_TLS_LDM)
4553 g = mips_elf_local_got_index (abfd, input_bfd, info,
4554 0, 0, NULL, r_type);
4555 if (g == MINUS_ONE)
4556 return bfd_reloc_outofrange;
4558 else if (!local_p)
4560 /* On VxWorks, CALL relocations should refer to the .got.plt
4561 entry, which is initialized to point at the PLT stub. */
4562 if (htab->is_vxworks
4563 && (r_type == R_MIPS_CALL_HI16
4564 || r_type == R_MIPS_CALL_LO16
4565 || call16_reloc_p (r_type)))
4567 BFD_ASSERT (addend == 0);
4568 BFD_ASSERT (h->root.needs_plt);
4569 g = mips_elf_gotplt_index (info, &h->root);
4571 else
4573 /* GOT_PAGE may take a non-zero addend, that is ignored in a
4574 GOT_PAGE relocation that decays to GOT_DISP because the
4575 symbol turns out to be global. The addend is then added
4576 as GOT_OFST. */
4577 BFD_ASSERT (addend == 0 || r_type == R_MIPS_GOT_PAGE);
4578 g = mips_elf_global_got_index (dynobj, input_bfd,
4579 &h->root, r_type, info);
4580 if (h->tls_type == GOT_NORMAL
4581 && (! elf_hash_table(info)->dynamic_sections_created
4582 || (info->shared
4583 && (info->symbolic || h->root.forced_local)
4584 && h->root.def_regular)))
4586 /* This is a static link or a -Bsymbolic link. The
4587 symbol is defined locally, or was forced to be local.
4588 We must initialize this entry in the GOT. */
4589 asection *sgot = mips_elf_got_section (dynobj, FALSE);
4590 MIPS_ELF_PUT_WORD (dynobj, symbol, sgot->contents + g);
4594 else if (!htab->is_vxworks
4595 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
4596 /* The calculation below does not involve "g". */
4597 break;
4598 else
4600 g = mips_elf_local_got_index (abfd, input_bfd, info,
4601 symbol + addend, r_symndx, h, r_type);
4602 if (g == MINUS_ONE)
4603 return bfd_reloc_outofrange;
4606 /* Convert GOT indices to actual offsets. */
4607 g = mips_elf_got_offset_from_index (dynobj, abfd, input_bfd, g);
4608 break;
4611 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
4612 symbols are resolved by the loader. Add them to .rela.dyn. */
4613 if (h != NULL && is_gott_symbol (info, &h->root))
4615 Elf_Internal_Rela outrel;
4616 bfd_byte *loc;
4617 asection *s;
4619 s = mips_elf_rel_dyn_section (info, FALSE);
4620 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
4622 outrel.r_offset = (input_section->output_section->vma
4623 + input_section->output_offset
4624 + relocation->r_offset);
4625 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
4626 outrel.r_addend = addend;
4627 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
4629 /* If we've written this relocation for a readonly section,
4630 we need to set DF_TEXTREL again, so that we do not delete the
4631 DT_TEXTREL tag. */
4632 if (MIPS_ELF_READONLY_SECTION (input_section))
4633 info->flags |= DF_TEXTREL;
4635 *valuep = 0;
4636 return bfd_reloc_ok;
4639 /* Figure out what kind of relocation is being performed. */
4640 switch (r_type)
4642 case R_MIPS_NONE:
4643 return bfd_reloc_continue;
4645 case R_MIPS_16:
4646 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
4647 overflowed_p = mips_elf_overflow_p (value, 16);
4648 break;
4650 case R_MIPS_32:
4651 case R_MIPS_REL32:
4652 case R_MIPS_64:
4653 if ((info->shared
4654 || (!htab->is_vxworks
4655 && htab->root.dynamic_sections_created
4656 && h != NULL
4657 && h->root.def_dynamic
4658 && !h->root.def_regular))
4659 && r_symndx != 0
4660 && (h == NULL
4661 || h->root.root.type != bfd_link_hash_undefweak
4662 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
4663 && (input_section->flags & SEC_ALLOC) != 0)
4665 /* If we're creating a shared library, or this relocation is
4666 against a symbol in a shared library, then we can't know
4667 where the symbol will end up. So, we create a relocation
4668 record in the output, and leave the job up to the dynamic
4669 linker.
4671 In VxWorks executables, references to external symbols
4672 are handled using copy relocs or PLT stubs, so there's
4673 no need to add a dynamic relocation here. */
4674 value = addend;
4675 if (!mips_elf_create_dynamic_relocation (abfd,
4676 info,
4677 relocation,
4679 sec,
4680 symbol,
4681 &value,
4682 input_section))
4683 return bfd_reloc_undefined;
4685 else
4687 if (r_type != R_MIPS_REL32)
4688 value = symbol + addend;
4689 else
4690 value = addend;
4692 value &= howto->dst_mask;
4693 break;
4695 case R_MIPS_PC32:
4696 value = symbol + addend - p;
4697 value &= howto->dst_mask;
4698 break;
4700 case R_MIPS16_26:
4701 /* The calculation for R_MIPS16_26 is just the same as for an
4702 R_MIPS_26. It's only the storage of the relocated field into
4703 the output file that's different. That's handled in
4704 mips_elf_perform_relocation. So, we just fall through to the
4705 R_MIPS_26 case here. */
4706 case R_MIPS_26:
4707 if (local_p)
4708 value = ((addend | ((p + 4) & 0xf0000000)) + symbol) >> 2;
4709 else
4711 value = (_bfd_mips_elf_sign_extend (addend, 28) + symbol) >> 2;
4712 if (h->root.root.type != bfd_link_hash_undefweak)
4713 overflowed_p = (value >> 26) != ((p + 4) >> 28);
4715 value &= howto->dst_mask;
4716 break;
4718 case R_MIPS_TLS_DTPREL_HI16:
4719 value = (mips_elf_high (addend + symbol - dtprel_base (info))
4720 & howto->dst_mask);
4721 break;
4723 case R_MIPS_TLS_DTPREL_LO16:
4724 case R_MIPS_TLS_DTPREL32:
4725 case R_MIPS_TLS_DTPREL64:
4726 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
4727 break;
4729 case R_MIPS_TLS_TPREL_HI16:
4730 value = (mips_elf_high (addend + symbol - tprel_base (info))
4731 & howto->dst_mask);
4732 break;
4734 case R_MIPS_TLS_TPREL_LO16:
4735 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
4736 break;
4738 case R_MIPS_HI16:
4739 case R_MIPS16_HI16:
4740 if (!gp_disp_p)
4742 value = mips_elf_high (addend + symbol);
4743 value &= howto->dst_mask;
4745 else
4747 /* For MIPS16 ABI code we generate this sequence
4748 0: li $v0,%hi(_gp_disp)
4749 4: addiupc $v1,%lo(_gp_disp)
4750 8: sll $v0,16
4751 12: addu $v0,$v1
4752 14: move $gp,$v0
4753 So the offsets of hi and lo relocs are the same, but the
4754 $pc is four higher than $t9 would be, so reduce
4755 both reloc addends by 4. */
4756 if (r_type == R_MIPS16_HI16)
4757 value = mips_elf_high (addend + gp - p - 4);
4758 else
4759 value = mips_elf_high (addend + gp - p);
4760 overflowed_p = mips_elf_overflow_p (value, 16);
4762 break;
4764 case R_MIPS_LO16:
4765 case R_MIPS16_LO16:
4766 if (!gp_disp_p)
4767 value = (symbol + addend) & howto->dst_mask;
4768 else
4770 /* See the comment for R_MIPS16_HI16 above for the reason
4771 for this conditional. */
4772 if (r_type == R_MIPS16_LO16)
4773 value = addend + gp - p;
4774 else
4775 value = addend + gp - p + 4;
4776 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
4777 for overflow. But, on, say, IRIX5, relocations against
4778 _gp_disp are normally generated from the .cpload
4779 pseudo-op. It generates code that normally looks like
4780 this:
4782 lui $gp,%hi(_gp_disp)
4783 addiu $gp,$gp,%lo(_gp_disp)
4784 addu $gp,$gp,$t9
4786 Here $t9 holds the address of the function being called,
4787 as required by the MIPS ELF ABI. The R_MIPS_LO16
4788 relocation can easily overflow in this situation, but the
4789 R_MIPS_HI16 relocation will handle the overflow.
4790 Therefore, we consider this a bug in the MIPS ABI, and do
4791 not check for overflow here. */
4793 break;
4795 case R_MIPS_LITERAL:
4796 /* Because we don't merge literal sections, we can handle this
4797 just like R_MIPS_GPREL16. In the long run, we should merge
4798 shared literals, and then we will need to additional work
4799 here. */
4801 /* Fall through. */
4803 case R_MIPS16_GPREL:
4804 /* The R_MIPS16_GPREL performs the same calculation as
4805 R_MIPS_GPREL16, but stores the relocated bits in a different
4806 order. We don't need to do anything special here; the
4807 differences are handled in mips_elf_perform_relocation. */
4808 case R_MIPS_GPREL16:
4809 /* Only sign-extend the addend if it was extracted from the
4810 instruction. If the addend was separate, leave it alone,
4811 otherwise we may lose significant bits. */
4812 if (howto->partial_inplace)
4813 addend = _bfd_mips_elf_sign_extend (addend, 16);
4814 value = symbol + addend - gp;
4815 /* If the symbol was local, any earlier relocatable links will
4816 have adjusted its addend with the gp offset, so compensate
4817 for that now. Don't do it for symbols forced local in this
4818 link, though, since they won't have had the gp offset applied
4819 to them before. */
4820 if (was_local_p)
4821 value += gp0;
4822 overflowed_p = mips_elf_overflow_p (value, 16);
4823 break;
4825 case R_MIPS16_GOT16:
4826 case R_MIPS16_CALL16:
4827 case R_MIPS_GOT16:
4828 case R_MIPS_CALL16:
4829 /* VxWorks does not have separate local and global semantics for
4830 R_MIPS*_GOT16; every relocation evaluates to "G". */
4831 if (!htab->is_vxworks && local_p)
4833 bfd_boolean forced;
4835 forced = ! mips_elf_local_relocation_p (input_bfd, relocation,
4836 local_sections, FALSE);
4837 value = mips_elf_got16_entry (abfd, input_bfd, info,
4838 symbol + addend, forced);
4839 if (value == MINUS_ONE)
4840 return bfd_reloc_outofrange;
4841 value
4842 = mips_elf_got_offset_from_index (dynobj, abfd, input_bfd, value);
4843 overflowed_p = mips_elf_overflow_p (value, 16);
4844 break;
4847 /* Fall through. */
4849 case R_MIPS_TLS_GD:
4850 case R_MIPS_TLS_GOTTPREL:
4851 case R_MIPS_TLS_LDM:
4852 case R_MIPS_GOT_DISP:
4853 got_disp:
4854 value = g;
4855 overflowed_p = mips_elf_overflow_p (value, 16);
4856 break;
4858 case R_MIPS_GPREL32:
4859 value = (addend + symbol + gp0 - gp);
4860 if (!save_addend)
4861 value &= howto->dst_mask;
4862 break;
4864 case R_MIPS_PC16:
4865 case R_MIPS_GNU_REL16_S2:
4866 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
4867 overflowed_p = mips_elf_overflow_p (value, 18);
4868 value >>= howto->rightshift;
4869 value &= howto->dst_mask;
4870 break;
4872 case R_MIPS_GOT_HI16:
4873 case R_MIPS_CALL_HI16:
4874 /* We're allowed to handle these two relocations identically.
4875 The dynamic linker is allowed to handle the CALL relocations
4876 differently by creating a lazy evaluation stub. */
4877 value = g;
4878 value = mips_elf_high (value);
4879 value &= howto->dst_mask;
4880 break;
4882 case R_MIPS_GOT_LO16:
4883 case R_MIPS_CALL_LO16:
4884 value = g & howto->dst_mask;
4885 break;
4887 case R_MIPS_GOT_PAGE:
4888 /* GOT_PAGE relocations that reference non-local symbols decay
4889 to GOT_DISP. The corresponding GOT_OFST relocation decays to
4890 0. */
4891 if (! local_p)
4892 goto got_disp;
4893 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
4894 if (value == MINUS_ONE)
4895 return bfd_reloc_outofrange;
4896 value = mips_elf_got_offset_from_index (dynobj, abfd, input_bfd, value);
4897 overflowed_p = mips_elf_overflow_p (value, 16);
4898 break;
4900 case R_MIPS_GOT_OFST:
4901 if (local_p)
4902 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
4903 else
4904 value = addend;
4905 overflowed_p = mips_elf_overflow_p (value, 16);
4906 break;
4908 case R_MIPS_SUB:
4909 value = symbol - addend;
4910 value &= howto->dst_mask;
4911 break;
4913 case R_MIPS_HIGHER:
4914 value = mips_elf_higher (addend + symbol);
4915 value &= howto->dst_mask;
4916 break;
4918 case R_MIPS_HIGHEST:
4919 value = mips_elf_highest (addend + symbol);
4920 value &= howto->dst_mask;
4921 break;
4923 case R_MIPS_SCN_DISP:
4924 value = symbol + addend - sec->output_offset;
4925 value &= howto->dst_mask;
4926 break;
4928 case R_MIPS_JALR:
4929 /* This relocation is only a hint. In some cases, we optimize
4930 it into a bal instruction. But we don't try to optimize
4931 branches to the PLT; that will wind up wasting time. */
4932 if (h != NULL && h->root.plt.offset != (bfd_vma) -1)
4933 return bfd_reloc_continue;
4934 value = symbol + addend;
4935 break;
4937 case R_MIPS_PJUMP:
4938 case R_MIPS_GNU_VTINHERIT:
4939 case R_MIPS_GNU_VTENTRY:
4940 /* We don't do anything with these at present. */
4941 return bfd_reloc_continue;
4943 default:
4944 /* An unrecognized relocation type. */
4945 return bfd_reloc_notsupported;
4948 /* Store the VALUE for our caller. */
4949 *valuep = value;
4950 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
4953 /* Obtain the field relocated by RELOCATION. */
4955 static bfd_vma
4956 mips_elf_obtain_contents (reloc_howto_type *howto,
4957 const Elf_Internal_Rela *relocation,
4958 bfd *input_bfd, bfd_byte *contents)
4960 bfd_vma x;
4961 bfd_byte *location = contents + relocation->r_offset;
4963 /* Obtain the bytes. */
4964 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
4966 return x;
4969 /* It has been determined that the result of the RELOCATION is the
4970 VALUE. Use HOWTO to place VALUE into the output file at the
4971 appropriate position. The SECTION is the section to which the
4972 relocation applies. If REQUIRE_JALX is TRUE, then the opcode used
4973 for the relocation must be either JAL or JALX, and it is
4974 unconditionally converted to JALX.
4976 Returns FALSE if anything goes wrong. */
4978 static bfd_boolean
4979 mips_elf_perform_relocation (struct bfd_link_info *info,
4980 reloc_howto_type *howto,
4981 const Elf_Internal_Rela *relocation,
4982 bfd_vma value, bfd *input_bfd,
4983 asection *input_section, bfd_byte *contents,
4984 bfd_boolean require_jalx)
4986 bfd_vma x;
4987 bfd_byte *location;
4988 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
4990 /* Figure out where the relocation is occurring. */
4991 location = contents + relocation->r_offset;
4993 _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
4995 /* Obtain the current value. */
4996 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
4998 /* Clear the field we are setting. */
4999 x &= ~howto->dst_mask;
5001 /* Set the field. */
5002 x |= (value & howto->dst_mask);
5004 /* If required, turn JAL into JALX. */
5005 if (require_jalx)
5007 bfd_boolean ok;
5008 bfd_vma opcode = x >> 26;
5009 bfd_vma jalx_opcode;
5011 /* Check to see if the opcode is already JAL or JALX. */
5012 if (r_type == R_MIPS16_26)
5014 ok = ((opcode == 0x6) || (opcode == 0x7));
5015 jalx_opcode = 0x7;
5017 else
5019 ok = ((opcode == 0x3) || (opcode == 0x1d));
5020 jalx_opcode = 0x1d;
5023 /* If the opcode is not JAL or JALX, there's a problem. */
5024 if (!ok)
5026 (*_bfd_error_handler)
5027 (_("%B: %A+0x%lx: jump to stub routine which is not jal"),
5028 input_bfd,
5029 input_section,
5030 (unsigned long) relocation->r_offset);
5031 bfd_set_error (bfd_error_bad_value);
5032 return FALSE;
5035 /* Make this the JALX opcode. */
5036 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
5039 /* On the RM9000, bal is faster than jal, because bal uses branch
5040 prediction hardware. If we are linking for the RM9000, and we
5041 see jal, and bal fits, use it instead. Note that this
5042 transformation should be safe for all architectures. */
5043 if (bfd_get_mach (input_bfd) == bfd_mach_mips9000
5044 && !info->relocatable
5045 && !require_jalx
5046 && ((r_type == R_MIPS_26 && (x >> 26) == 0x3) /* jal addr */
5047 || (r_type == R_MIPS_JALR && x == 0x0320f809))) /* jalr t9 */
5049 bfd_vma addr;
5050 bfd_vma dest;
5051 bfd_signed_vma off;
5053 addr = (input_section->output_section->vma
5054 + input_section->output_offset
5055 + relocation->r_offset
5056 + 4);
5057 if (r_type == R_MIPS_26)
5058 dest = (value << 2) | ((addr >> 28) << 28);
5059 else
5060 dest = value;
5061 off = dest - addr;
5062 if (off <= 0x1ffff && off >= -0x20000)
5063 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
5066 /* Put the value into the output. */
5067 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
5069 _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, !info->relocatable,
5070 location);
5072 return TRUE;
5075 /* Add room for N relocations to the .rel(a).dyn section in ABFD. */
5077 static void
5078 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
5079 unsigned int n)
5081 asection *s;
5082 struct mips_elf_link_hash_table *htab;
5084 htab = mips_elf_hash_table (info);
5085 s = mips_elf_rel_dyn_section (info, FALSE);
5086 BFD_ASSERT (s != NULL);
5088 if (htab->is_vxworks)
5089 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
5090 else
5092 if (s->size == 0)
5094 /* Make room for a null element. */
5095 s->size += MIPS_ELF_REL_SIZE (abfd);
5096 ++s->reloc_count;
5098 s->size += n * MIPS_ELF_REL_SIZE (abfd);
5102 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
5103 is the original relocation, which is now being transformed into a
5104 dynamic relocation. The ADDENDP is adjusted if necessary; the
5105 caller should store the result in place of the original addend. */
5107 static bfd_boolean
5108 mips_elf_create_dynamic_relocation (bfd *output_bfd,
5109 struct bfd_link_info *info,
5110 const Elf_Internal_Rela *rel,
5111 struct mips_elf_link_hash_entry *h,
5112 asection *sec, bfd_vma symbol,
5113 bfd_vma *addendp, asection *input_section)
5115 Elf_Internal_Rela outrel[3];
5116 asection *sreloc;
5117 bfd *dynobj;
5118 int r_type;
5119 long indx;
5120 bfd_boolean defined_p;
5121 struct mips_elf_link_hash_table *htab;
5123 htab = mips_elf_hash_table (info);
5124 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
5125 dynobj = elf_hash_table (info)->dynobj;
5126 sreloc = mips_elf_rel_dyn_section (info, FALSE);
5127 BFD_ASSERT (sreloc != NULL);
5128 BFD_ASSERT (sreloc->contents != NULL);
5129 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
5130 < sreloc->size);
5132 outrel[0].r_offset =
5133 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
5134 if (ABI_64_P (output_bfd))
5136 outrel[1].r_offset =
5137 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
5138 outrel[2].r_offset =
5139 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
5142 if (outrel[0].r_offset == MINUS_ONE)
5143 /* The relocation field has been deleted. */
5144 return TRUE;
5146 if (outrel[0].r_offset == MINUS_TWO)
5148 /* The relocation field has been converted into a relative value of
5149 some sort. Functions like _bfd_elf_write_section_eh_frame expect
5150 the field to be fully relocated, so add in the symbol's value. */
5151 *addendp += symbol;
5152 return TRUE;
5155 /* We must now calculate the dynamic symbol table index to use
5156 in the relocation. */
5157 if (h != NULL
5158 && (!h->root.def_regular
5159 || (info->shared && !info->symbolic && !h->root.forced_local)))
5161 indx = h->root.dynindx;
5162 if (SGI_COMPAT (output_bfd))
5163 defined_p = h->root.def_regular;
5164 else
5165 /* ??? glibc's ld.so just adds the final GOT entry to the
5166 relocation field. It therefore treats relocs against
5167 defined symbols in the same way as relocs against
5168 undefined symbols. */
5169 defined_p = FALSE;
5171 else
5173 if (sec != NULL && bfd_is_abs_section (sec))
5174 indx = 0;
5175 else if (sec == NULL || sec->owner == NULL)
5177 bfd_set_error (bfd_error_bad_value);
5178 return FALSE;
5180 else
5182 indx = elf_section_data (sec->output_section)->dynindx;
5183 if (indx == 0)
5185 asection *osec = htab->root.text_index_section;
5186 indx = elf_section_data (osec)->dynindx;
5188 if (indx == 0)
5189 abort ();
5192 /* Instead of generating a relocation using the section
5193 symbol, we may as well make it a fully relative
5194 relocation. We want to avoid generating relocations to
5195 local symbols because we used to generate them
5196 incorrectly, without adding the original symbol value,
5197 which is mandated by the ABI for section symbols. In
5198 order to give dynamic loaders and applications time to
5199 phase out the incorrect use, we refrain from emitting
5200 section-relative relocations. It's not like they're
5201 useful, after all. This should be a bit more efficient
5202 as well. */
5203 /* ??? Although this behavior is compatible with glibc's ld.so,
5204 the ABI says that relocations against STN_UNDEF should have
5205 a symbol value of 0. Irix rld honors this, so relocations
5206 against STN_UNDEF have no effect. */
5207 if (!SGI_COMPAT (output_bfd))
5208 indx = 0;
5209 defined_p = TRUE;
5212 /* If the relocation was previously an absolute relocation and
5213 this symbol will not be referred to by the relocation, we must
5214 adjust it by the value we give it in the dynamic symbol table.
5215 Otherwise leave the job up to the dynamic linker. */
5216 if (defined_p && r_type != R_MIPS_REL32)
5217 *addendp += symbol;
5219 if (htab->is_vxworks)
5220 /* VxWorks uses non-relative relocations for this. */
5221 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
5222 else
5223 /* The relocation is always an REL32 relocation because we don't
5224 know where the shared library will wind up at load-time. */
5225 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
5226 R_MIPS_REL32);
5228 /* For strict adherence to the ABI specification, we should
5229 generate a R_MIPS_64 relocation record by itself before the
5230 _REL32/_64 record as well, such that the addend is read in as
5231 a 64-bit value (REL32 is a 32-bit relocation, after all).
5232 However, since none of the existing ELF64 MIPS dynamic
5233 loaders seems to care, we don't waste space with these
5234 artificial relocations. If this turns out to not be true,
5235 mips_elf_allocate_dynamic_relocation() should be tweaked so
5236 as to make room for a pair of dynamic relocations per
5237 invocation if ABI_64_P, and here we should generate an
5238 additional relocation record with R_MIPS_64 by itself for a
5239 NULL symbol before this relocation record. */
5240 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
5241 ABI_64_P (output_bfd)
5242 ? R_MIPS_64
5243 : R_MIPS_NONE);
5244 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
5246 /* Adjust the output offset of the relocation to reference the
5247 correct location in the output file. */
5248 outrel[0].r_offset += (input_section->output_section->vma
5249 + input_section->output_offset);
5250 outrel[1].r_offset += (input_section->output_section->vma
5251 + input_section->output_offset);
5252 outrel[2].r_offset += (input_section->output_section->vma
5253 + input_section->output_offset);
5255 /* Put the relocation back out. We have to use the special
5256 relocation outputter in the 64-bit case since the 64-bit
5257 relocation format is non-standard. */
5258 if (ABI_64_P (output_bfd))
5260 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
5261 (output_bfd, &outrel[0],
5262 (sreloc->contents
5263 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
5265 else if (htab->is_vxworks)
5267 /* VxWorks uses RELA rather than REL dynamic relocations. */
5268 outrel[0].r_addend = *addendp;
5269 bfd_elf32_swap_reloca_out
5270 (output_bfd, &outrel[0],
5271 (sreloc->contents
5272 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
5274 else
5275 bfd_elf32_swap_reloc_out
5276 (output_bfd, &outrel[0],
5277 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
5279 /* We've now added another relocation. */
5280 ++sreloc->reloc_count;
5282 /* Make sure the output section is writable. The dynamic linker
5283 will be writing to it. */
5284 elf_section_data (input_section->output_section)->this_hdr.sh_flags
5285 |= SHF_WRITE;
5287 /* On IRIX5, make an entry of compact relocation info. */
5288 if (IRIX_COMPAT (output_bfd) == ict_irix5)
5290 asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
5291 bfd_byte *cr;
5293 if (scpt)
5295 Elf32_crinfo cptrel;
5297 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5298 cptrel.vaddr = (rel->r_offset
5299 + input_section->output_section->vma
5300 + input_section->output_offset);
5301 if (r_type == R_MIPS_REL32)
5302 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5303 else
5304 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5305 mips_elf_set_cr_dist2to (cptrel, 0);
5306 cptrel.konst = *addendp;
5308 cr = (scpt->contents
5309 + sizeof (Elf32_External_compact_rel));
5310 mips_elf_set_cr_relvaddr (cptrel, 0);
5311 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5312 ((Elf32_External_crinfo *) cr
5313 + scpt->reloc_count));
5314 ++scpt->reloc_count;
5318 /* If we've written this relocation for a readonly section,
5319 we need to set DF_TEXTREL again, so that we do not delete the
5320 DT_TEXTREL tag. */
5321 if (MIPS_ELF_READONLY_SECTION (input_section))
5322 info->flags |= DF_TEXTREL;
5324 return TRUE;
5327 /* Return the MACH for a MIPS e_flags value. */
5329 unsigned long
5330 _bfd_elf_mips_mach (flagword flags)
5332 switch (flags & EF_MIPS_MACH)
5334 case E_MIPS_MACH_3900:
5335 return bfd_mach_mips3900;
5337 case E_MIPS_MACH_4010:
5338 return bfd_mach_mips4010;
5340 case E_MIPS_MACH_4100:
5341 return bfd_mach_mips4100;
5343 case E_MIPS_MACH_4111:
5344 return bfd_mach_mips4111;
5346 case E_MIPS_MACH_4120:
5347 return bfd_mach_mips4120;
5349 case E_MIPS_MACH_4650:
5350 return bfd_mach_mips4650;
5352 case E_MIPS_MACH_5400:
5353 return bfd_mach_mips5400;
5355 case E_MIPS_MACH_5500:
5356 return bfd_mach_mips5500;
5358 case E_MIPS_MACH_9000:
5359 return bfd_mach_mips9000;
5361 case E_MIPS_MACH_SB1:
5362 return bfd_mach_mips_sb1;
5364 case E_MIPS_MACH_LS2E:
5365 return bfd_mach_mips_loongson_2e;
5367 case E_MIPS_MACH_LS2F:
5368 return bfd_mach_mips_loongson_2f;
5370 case E_MIPS_MACH_OCTEON:
5371 return bfd_mach_mips_octeon;
5373 default:
5374 switch (flags & EF_MIPS_ARCH)
5376 default:
5377 case E_MIPS_ARCH_1:
5378 return bfd_mach_mips3000;
5380 case E_MIPS_ARCH_2:
5381 return bfd_mach_mips6000;
5383 case E_MIPS_ARCH_3:
5384 return bfd_mach_mips4000;
5386 case E_MIPS_ARCH_4:
5387 return bfd_mach_mips8000;
5389 case E_MIPS_ARCH_5:
5390 return bfd_mach_mips5;
5392 case E_MIPS_ARCH_32:
5393 return bfd_mach_mipsisa32;
5395 case E_MIPS_ARCH_64:
5396 return bfd_mach_mipsisa64;
5398 case E_MIPS_ARCH_32R2:
5399 return bfd_mach_mipsisa32r2;
5401 case E_MIPS_ARCH_64R2:
5402 return bfd_mach_mipsisa64r2;
5406 return 0;
5409 /* Return printable name for ABI. */
5411 static INLINE char *
5412 elf_mips_abi_name (bfd *abfd)
5414 flagword flags;
5416 flags = elf_elfheader (abfd)->e_flags;
5417 switch (flags & EF_MIPS_ABI)
5419 case 0:
5420 if (ABI_N32_P (abfd))
5421 return "N32";
5422 else if (ABI_64_P (abfd))
5423 return "64";
5424 else
5425 return "none";
5426 case E_MIPS_ABI_O32:
5427 return "O32";
5428 case E_MIPS_ABI_O64:
5429 return "O64";
5430 case E_MIPS_ABI_EABI32:
5431 return "EABI32";
5432 case E_MIPS_ABI_EABI64:
5433 return "EABI64";
5434 default:
5435 return "unknown abi";
5439 /* MIPS ELF uses two common sections. One is the usual one, and the
5440 other is for small objects. All the small objects are kept
5441 together, and then referenced via the gp pointer, which yields
5442 faster assembler code. This is what we use for the small common
5443 section. This approach is copied from ecoff.c. */
5444 static asection mips_elf_scom_section;
5445 static asymbol mips_elf_scom_symbol;
5446 static asymbol *mips_elf_scom_symbol_ptr;
5448 /* MIPS ELF also uses an acommon section, which represents an
5449 allocated common symbol which may be overridden by a
5450 definition in a shared library. */
5451 static asection mips_elf_acom_section;
5452 static asymbol mips_elf_acom_symbol;
5453 static asymbol *mips_elf_acom_symbol_ptr;
5455 /* This is used for both the 32-bit and the 64-bit ABI. */
5457 void
5458 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
5460 elf_symbol_type *elfsym;
5462 /* Handle the special MIPS section numbers that a symbol may use. */
5463 elfsym = (elf_symbol_type *) asym;
5464 switch (elfsym->internal_elf_sym.st_shndx)
5466 case SHN_MIPS_ACOMMON:
5467 /* This section is used in a dynamically linked executable file.
5468 It is an allocated common section. The dynamic linker can
5469 either resolve these symbols to something in a shared
5470 library, or it can just leave them here. For our purposes,
5471 we can consider these symbols to be in a new section. */
5472 if (mips_elf_acom_section.name == NULL)
5474 /* Initialize the acommon section. */
5475 mips_elf_acom_section.name = ".acommon";
5476 mips_elf_acom_section.flags = SEC_ALLOC;
5477 mips_elf_acom_section.output_section = &mips_elf_acom_section;
5478 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
5479 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
5480 mips_elf_acom_symbol.name = ".acommon";
5481 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
5482 mips_elf_acom_symbol.section = &mips_elf_acom_section;
5483 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
5485 asym->section = &mips_elf_acom_section;
5486 break;
5488 case SHN_COMMON:
5489 /* Common symbols less than the GP size are automatically
5490 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
5491 if (asym->value > elf_gp_size (abfd)
5492 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
5493 || IRIX_COMPAT (abfd) == ict_irix6)
5494 break;
5495 /* Fall through. */
5496 case SHN_MIPS_SCOMMON:
5497 if (mips_elf_scom_section.name == NULL)
5499 /* Initialize the small common section. */
5500 mips_elf_scom_section.name = ".scommon";
5501 mips_elf_scom_section.flags = SEC_IS_COMMON;
5502 mips_elf_scom_section.output_section = &mips_elf_scom_section;
5503 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
5504 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
5505 mips_elf_scom_symbol.name = ".scommon";
5506 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
5507 mips_elf_scom_symbol.section = &mips_elf_scom_section;
5508 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
5510 asym->section = &mips_elf_scom_section;
5511 asym->value = elfsym->internal_elf_sym.st_size;
5512 break;
5514 case SHN_MIPS_SUNDEFINED:
5515 asym->section = bfd_und_section_ptr;
5516 break;
5518 case SHN_MIPS_TEXT:
5520 asection *section = bfd_get_section_by_name (abfd, ".text");
5522 BFD_ASSERT (SGI_COMPAT (abfd));
5523 if (section != NULL)
5525 asym->section = section;
5526 /* MIPS_TEXT is a bit special, the address is not an offset
5527 to the base of the .text section. So substract the section
5528 base address to make it an offset. */
5529 asym->value -= section->vma;
5532 break;
5534 case SHN_MIPS_DATA:
5536 asection *section = bfd_get_section_by_name (abfd, ".data");
5538 BFD_ASSERT (SGI_COMPAT (abfd));
5539 if (section != NULL)
5541 asym->section = section;
5542 /* MIPS_DATA is a bit special, the address is not an offset
5543 to the base of the .data section. So substract the section
5544 base address to make it an offset. */
5545 asym->value -= section->vma;
5548 break;
5551 /* If this is an odd-valued function symbol, assume it's a MIPS16 one. */
5552 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
5553 && (asym->value & 1) != 0)
5555 asym->value--;
5556 elfsym->internal_elf_sym.st_other
5557 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
5561 /* Implement elf_backend_eh_frame_address_size. This differs from
5562 the default in the way it handles EABI64.
5564 EABI64 was originally specified as an LP64 ABI, and that is what
5565 -mabi=eabi normally gives on a 64-bit target. However, gcc has
5566 historically accepted the combination of -mabi=eabi and -mlong32,
5567 and this ILP32 variation has become semi-official over time.
5568 Both forms use elf32 and have pointer-sized FDE addresses.
5570 If an EABI object was generated by GCC 4.0 or above, it will have
5571 an empty .gcc_compiled_longXX section, where XX is the size of longs
5572 in bits. Unfortunately, ILP32 objects generated by earlier compilers
5573 have no special marking to distinguish them from LP64 objects.
5575 We don't want users of the official LP64 ABI to be punished for the
5576 existence of the ILP32 variant, but at the same time, we don't want
5577 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
5578 We therefore take the following approach:
5580 - If ABFD contains a .gcc_compiled_longXX section, use it to
5581 determine the pointer size.
5583 - Otherwise check the type of the first relocation. Assume that
5584 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
5586 - Otherwise punt.
5588 The second check is enough to detect LP64 objects generated by pre-4.0
5589 compilers because, in the kind of output generated by those compilers,
5590 the first relocation will be associated with either a CIE personality
5591 routine or an FDE start address. Furthermore, the compilers never
5592 used a special (non-pointer) encoding for this ABI.
5594 Checking the relocation type should also be safe because there is no
5595 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
5596 did so. */
5598 unsigned int
5599 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
5601 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
5602 return 8;
5603 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
5605 bfd_boolean long32_p, long64_p;
5607 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
5608 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
5609 if (long32_p && long64_p)
5610 return 0;
5611 if (long32_p)
5612 return 4;
5613 if (long64_p)
5614 return 8;
5616 if (sec->reloc_count > 0
5617 && elf_section_data (sec)->relocs != NULL
5618 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
5619 == R_MIPS_64))
5620 return 8;
5622 return 0;
5624 return 4;
5627 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
5628 relocations against two unnamed section symbols to resolve to the
5629 same address. For example, if we have code like:
5631 lw $4,%got_disp(.data)($gp)
5632 lw $25,%got_disp(.text)($gp)
5633 jalr $25
5635 then the linker will resolve both relocations to .data and the program
5636 will jump there rather than to .text.
5638 We can work around this problem by giving names to local section symbols.
5639 This is also what the MIPSpro tools do. */
5641 bfd_boolean
5642 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
5644 return SGI_COMPAT (abfd);
5647 /* Work over a section just before writing it out. This routine is
5648 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
5649 sections that need the SHF_MIPS_GPREL flag by name; there has to be
5650 a better way. */
5652 bfd_boolean
5653 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
5655 if (hdr->sh_type == SHT_MIPS_REGINFO
5656 && hdr->sh_size > 0)
5658 bfd_byte buf[4];
5660 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
5661 BFD_ASSERT (hdr->contents == NULL);
5663 if (bfd_seek (abfd,
5664 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
5665 SEEK_SET) != 0)
5666 return FALSE;
5667 H_PUT_32 (abfd, elf_gp (abfd), buf);
5668 if (bfd_bwrite (buf, 4, abfd) != 4)
5669 return FALSE;
5672 if (hdr->sh_type == SHT_MIPS_OPTIONS
5673 && hdr->bfd_section != NULL
5674 && mips_elf_section_data (hdr->bfd_section) != NULL
5675 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
5677 bfd_byte *contents, *l, *lend;
5679 /* We stored the section contents in the tdata field in the
5680 set_section_contents routine. We save the section contents
5681 so that we don't have to read them again.
5682 At this point we know that elf_gp is set, so we can look
5683 through the section contents to see if there is an
5684 ODK_REGINFO structure. */
5686 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
5687 l = contents;
5688 lend = contents + hdr->sh_size;
5689 while (l + sizeof (Elf_External_Options) <= lend)
5691 Elf_Internal_Options intopt;
5693 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
5694 &intopt);
5695 if (intopt.size < sizeof (Elf_External_Options))
5697 (*_bfd_error_handler)
5698 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
5699 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
5700 break;
5702 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
5704 bfd_byte buf[8];
5706 if (bfd_seek (abfd,
5707 (hdr->sh_offset
5708 + (l - contents)
5709 + sizeof (Elf_External_Options)
5710 + (sizeof (Elf64_External_RegInfo) - 8)),
5711 SEEK_SET) != 0)
5712 return FALSE;
5713 H_PUT_64 (abfd, elf_gp (abfd), buf);
5714 if (bfd_bwrite (buf, 8, abfd) != 8)
5715 return FALSE;
5717 else if (intopt.kind == ODK_REGINFO)
5719 bfd_byte buf[4];
5721 if (bfd_seek (abfd,
5722 (hdr->sh_offset
5723 + (l - contents)
5724 + sizeof (Elf_External_Options)
5725 + (sizeof (Elf32_External_RegInfo) - 4)),
5726 SEEK_SET) != 0)
5727 return FALSE;
5728 H_PUT_32 (abfd, elf_gp (abfd), buf);
5729 if (bfd_bwrite (buf, 4, abfd) != 4)
5730 return FALSE;
5732 l += intopt.size;
5736 if (hdr->bfd_section != NULL)
5738 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
5740 if (strcmp (name, ".sdata") == 0
5741 || strcmp (name, ".lit8") == 0
5742 || strcmp (name, ".lit4") == 0)
5744 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5745 hdr->sh_type = SHT_PROGBITS;
5747 else if (strcmp (name, ".sbss") == 0)
5749 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5750 hdr->sh_type = SHT_NOBITS;
5752 else if (strcmp (name, ".srdata") == 0)
5754 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
5755 hdr->sh_type = SHT_PROGBITS;
5757 else if (strcmp (name, ".compact_rel") == 0)
5759 hdr->sh_flags = 0;
5760 hdr->sh_type = SHT_PROGBITS;
5762 else if (strcmp (name, ".rtproc") == 0)
5764 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
5766 unsigned int adjust;
5768 adjust = hdr->sh_size % hdr->sh_addralign;
5769 if (adjust != 0)
5770 hdr->sh_size += hdr->sh_addralign - adjust;
5775 return TRUE;
5778 /* Handle a MIPS specific section when reading an object file. This
5779 is called when elfcode.h finds a section with an unknown type.
5780 This routine supports both the 32-bit and 64-bit ELF ABI.
5782 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
5783 how to. */
5785 bfd_boolean
5786 _bfd_mips_elf_section_from_shdr (bfd *abfd,
5787 Elf_Internal_Shdr *hdr,
5788 const char *name,
5789 int shindex)
5791 flagword flags = 0;
5793 /* There ought to be a place to keep ELF backend specific flags, but
5794 at the moment there isn't one. We just keep track of the
5795 sections by their name, instead. Fortunately, the ABI gives
5796 suggested names for all the MIPS specific sections, so we will
5797 probably get away with this. */
5798 switch (hdr->sh_type)
5800 case SHT_MIPS_LIBLIST:
5801 if (strcmp (name, ".liblist") != 0)
5802 return FALSE;
5803 break;
5804 case SHT_MIPS_MSYM:
5805 if (strcmp (name, ".msym") != 0)
5806 return FALSE;
5807 break;
5808 case SHT_MIPS_CONFLICT:
5809 if (strcmp (name, ".conflict") != 0)
5810 return FALSE;
5811 break;
5812 case SHT_MIPS_GPTAB:
5813 if (! CONST_STRNEQ (name, ".gptab."))
5814 return FALSE;
5815 break;
5816 case SHT_MIPS_UCODE:
5817 if (strcmp (name, ".ucode") != 0)
5818 return FALSE;
5819 break;
5820 case SHT_MIPS_DEBUG:
5821 if (strcmp (name, ".mdebug") != 0)
5822 return FALSE;
5823 flags = SEC_DEBUGGING;
5824 break;
5825 case SHT_MIPS_REGINFO:
5826 if (strcmp (name, ".reginfo") != 0
5827 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
5828 return FALSE;
5829 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
5830 break;
5831 case SHT_MIPS_IFACE:
5832 if (strcmp (name, ".MIPS.interfaces") != 0)
5833 return FALSE;
5834 break;
5835 case SHT_MIPS_CONTENT:
5836 if (! CONST_STRNEQ (name, ".MIPS.content"))
5837 return FALSE;
5838 break;
5839 case SHT_MIPS_OPTIONS:
5840 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
5841 return FALSE;
5842 break;
5843 case SHT_MIPS_DWARF:
5844 if (! CONST_STRNEQ (name, ".debug_")
5845 && ! CONST_STRNEQ (name, ".zdebug_"))
5846 return FALSE;
5847 break;
5848 case SHT_MIPS_SYMBOL_LIB:
5849 if (strcmp (name, ".MIPS.symlib") != 0)
5850 return FALSE;
5851 break;
5852 case SHT_MIPS_EVENTS:
5853 if (! CONST_STRNEQ (name, ".MIPS.events")
5854 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
5855 return FALSE;
5856 break;
5857 default:
5858 break;
5861 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
5862 return FALSE;
5864 if (flags)
5866 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
5867 (bfd_get_section_flags (abfd,
5868 hdr->bfd_section)
5869 | flags)))
5870 return FALSE;
5873 /* FIXME: We should record sh_info for a .gptab section. */
5875 /* For a .reginfo section, set the gp value in the tdata information
5876 from the contents of this section. We need the gp value while
5877 processing relocs, so we just get it now. The .reginfo section
5878 is not used in the 64-bit MIPS ELF ABI. */
5879 if (hdr->sh_type == SHT_MIPS_REGINFO)
5881 Elf32_External_RegInfo ext;
5882 Elf32_RegInfo s;
5884 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
5885 &ext, 0, sizeof ext))
5886 return FALSE;
5887 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
5888 elf_gp (abfd) = s.ri_gp_value;
5891 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
5892 set the gp value based on what we find. We may see both
5893 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
5894 they should agree. */
5895 if (hdr->sh_type == SHT_MIPS_OPTIONS)
5897 bfd_byte *contents, *l, *lend;
5899 contents = bfd_malloc (hdr->sh_size);
5900 if (contents == NULL)
5901 return FALSE;
5902 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
5903 0, hdr->sh_size))
5905 free (contents);
5906 return FALSE;
5908 l = contents;
5909 lend = contents + hdr->sh_size;
5910 while (l + sizeof (Elf_External_Options) <= lend)
5912 Elf_Internal_Options intopt;
5914 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
5915 &intopt);
5916 if (intopt.size < sizeof (Elf_External_Options))
5918 (*_bfd_error_handler)
5919 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
5920 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
5921 break;
5923 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
5925 Elf64_Internal_RegInfo intreg;
5927 bfd_mips_elf64_swap_reginfo_in
5928 (abfd,
5929 ((Elf64_External_RegInfo *)
5930 (l + sizeof (Elf_External_Options))),
5931 &intreg);
5932 elf_gp (abfd) = intreg.ri_gp_value;
5934 else if (intopt.kind == ODK_REGINFO)
5936 Elf32_RegInfo intreg;
5938 bfd_mips_elf32_swap_reginfo_in
5939 (abfd,
5940 ((Elf32_External_RegInfo *)
5941 (l + sizeof (Elf_External_Options))),
5942 &intreg);
5943 elf_gp (abfd) = intreg.ri_gp_value;
5945 l += intopt.size;
5947 free (contents);
5950 return TRUE;
5953 /* Set the correct type for a MIPS ELF section. We do this by the
5954 section name, which is a hack, but ought to work. This routine is
5955 used by both the 32-bit and the 64-bit ABI. */
5957 bfd_boolean
5958 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
5960 const char *name = bfd_get_section_name (abfd, sec);
5962 if (strcmp (name, ".liblist") == 0)
5964 hdr->sh_type = SHT_MIPS_LIBLIST;
5965 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
5966 /* The sh_link field is set in final_write_processing. */
5968 else if (strcmp (name, ".conflict") == 0)
5969 hdr->sh_type = SHT_MIPS_CONFLICT;
5970 else if (CONST_STRNEQ (name, ".gptab."))
5972 hdr->sh_type = SHT_MIPS_GPTAB;
5973 hdr->sh_entsize = sizeof (Elf32_External_gptab);
5974 /* The sh_info field is set in final_write_processing. */
5976 else if (strcmp (name, ".ucode") == 0)
5977 hdr->sh_type = SHT_MIPS_UCODE;
5978 else if (strcmp (name, ".mdebug") == 0)
5980 hdr->sh_type = SHT_MIPS_DEBUG;
5981 /* In a shared object on IRIX 5.3, the .mdebug section has an
5982 entsize of 0. FIXME: Does this matter? */
5983 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
5984 hdr->sh_entsize = 0;
5985 else
5986 hdr->sh_entsize = 1;
5988 else if (strcmp (name, ".reginfo") == 0)
5990 hdr->sh_type = SHT_MIPS_REGINFO;
5991 /* In a shared object on IRIX 5.3, the .reginfo section has an
5992 entsize of 0x18. FIXME: Does this matter? */
5993 if (SGI_COMPAT (abfd))
5995 if ((abfd->flags & DYNAMIC) != 0)
5996 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
5997 else
5998 hdr->sh_entsize = 1;
6000 else
6001 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6003 else if (SGI_COMPAT (abfd)
6004 && (strcmp (name, ".hash") == 0
6005 || strcmp (name, ".dynamic") == 0
6006 || strcmp (name, ".dynstr") == 0))
6008 if (SGI_COMPAT (abfd))
6009 hdr->sh_entsize = 0;
6010 #if 0
6011 /* This isn't how the IRIX6 linker behaves. */
6012 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
6013 #endif
6015 else if (strcmp (name, ".got") == 0
6016 || strcmp (name, ".srdata") == 0
6017 || strcmp (name, ".sdata") == 0
6018 || strcmp (name, ".sbss") == 0
6019 || strcmp (name, ".lit4") == 0
6020 || strcmp (name, ".lit8") == 0)
6021 hdr->sh_flags |= SHF_MIPS_GPREL;
6022 else if (strcmp (name, ".MIPS.interfaces") == 0)
6024 hdr->sh_type = SHT_MIPS_IFACE;
6025 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6027 else if (CONST_STRNEQ (name, ".MIPS.content"))
6029 hdr->sh_type = SHT_MIPS_CONTENT;
6030 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6031 /* The sh_info field is set in final_write_processing. */
6033 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
6035 hdr->sh_type = SHT_MIPS_OPTIONS;
6036 hdr->sh_entsize = 1;
6037 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6039 else if (CONST_STRNEQ (name, ".debug_")
6040 || CONST_STRNEQ (name, ".zdebug_"))
6042 hdr->sh_type = SHT_MIPS_DWARF;
6044 /* Irix facilities such as libexc expect a single .debug_frame
6045 per executable, the system ones have NOSTRIP set and the linker
6046 doesn't merge sections with different flags so ... */
6047 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
6048 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6050 else if (strcmp (name, ".MIPS.symlib") == 0)
6052 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
6053 /* The sh_link and sh_info fields are set in
6054 final_write_processing. */
6056 else if (CONST_STRNEQ (name, ".MIPS.events")
6057 || CONST_STRNEQ (name, ".MIPS.post_rel"))
6059 hdr->sh_type = SHT_MIPS_EVENTS;
6060 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6061 /* The sh_link field is set in final_write_processing. */
6063 else if (strcmp (name, ".msym") == 0)
6065 hdr->sh_type = SHT_MIPS_MSYM;
6066 hdr->sh_flags |= SHF_ALLOC;
6067 hdr->sh_entsize = 8;
6070 /* The generic elf_fake_sections will set up REL_HDR using the default
6071 kind of relocations. We used to set up a second header for the
6072 non-default kind of relocations here, but only NewABI would use
6073 these, and the IRIX ld doesn't like resulting empty RELA sections.
6074 Thus we create those header only on demand now. */
6076 return TRUE;
6079 /* Given a BFD section, try to locate the corresponding ELF section
6080 index. This is used by both the 32-bit and the 64-bit ABI.
6081 Actually, it's not clear to me that the 64-bit ABI supports these,
6082 but for non-PIC objects we will certainly want support for at least
6083 the .scommon section. */
6085 bfd_boolean
6086 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6087 asection *sec, int *retval)
6089 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
6091 *retval = SHN_MIPS_SCOMMON;
6092 return TRUE;
6094 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
6096 *retval = SHN_MIPS_ACOMMON;
6097 return TRUE;
6099 return FALSE;
6102 /* Hook called by the linker routine which adds symbols from an object
6103 file. We must handle the special MIPS section numbers here. */
6105 bfd_boolean
6106 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
6107 Elf_Internal_Sym *sym, const char **namep,
6108 flagword *flagsp ATTRIBUTE_UNUSED,
6109 asection **secp, bfd_vma *valp)
6111 if (SGI_COMPAT (abfd)
6112 && (abfd->flags & DYNAMIC) != 0
6113 && strcmp (*namep, "_rld_new_interface") == 0)
6115 /* Skip IRIX5 rld entry name. */
6116 *namep = NULL;
6117 return TRUE;
6120 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
6121 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
6122 by setting a DT_NEEDED for the shared object. Since _gp_disp is
6123 a magic symbol resolved by the linker, we ignore this bogus definition
6124 of _gp_disp. New ABI objects do not suffer from this problem so this
6125 is not done for them. */
6126 if (!NEWABI_P(abfd)
6127 && (sym->st_shndx == SHN_ABS)
6128 && (strcmp (*namep, "_gp_disp") == 0))
6130 *namep = NULL;
6131 return TRUE;
6134 switch (sym->st_shndx)
6136 case SHN_COMMON:
6137 /* Common symbols less than the GP size are automatically
6138 treated as SHN_MIPS_SCOMMON symbols. */
6139 if (sym->st_size > elf_gp_size (abfd)
6140 || ELF_ST_TYPE (sym->st_info) == STT_TLS
6141 || IRIX_COMPAT (abfd) == ict_irix6)
6142 break;
6143 /* Fall through. */
6144 case SHN_MIPS_SCOMMON:
6145 *secp = bfd_make_section_old_way (abfd, ".scommon");
6146 (*secp)->flags |= SEC_IS_COMMON;
6147 *valp = sym->st_size;
6148 break;
6150 case SHN_MIPS_TEXT:
6151 /* This section is used in a shared object. */
6152 if (elf_tdata (abfd)->elf_text_section == NULL)
6154 asymbol *elf_text_symbol;
6155 asection *elf_text_section;
6156 bfd_size_type amt = sizeof (asection);
6158 elf_text_section = bfd_zalloc (abfd, amt);
6159 if (elf_text_section == NULL)
6160 return FALSE;
6162 amt = sizeof (asymbol);
6163 elf_text_symbol = bfd_zalloc (abfd, amt);
6164 if (elf_text_symbol == NULL)
6165 return FALSE;
6167 /* Initialize the section. */
6169 elf_tdata (abfd)->elf_text_section = elf_text_section;
6170 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
6172 elf_text_section->symbol = elf_text_symbol;
6173 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
6175 elf_text_section->name = ".text";
6176 elf_text_section->flags = SEC_NO_FLAGS;
6177 elf_text_section->output_section = NULL;
6178 elf_text_section->owner = abfd;
6179 elf_text_symbol->name = ".text";
6180 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6181 elf_text_symbol->section = elf_text_section;
6183 /* This code used to do *secp = bfd_und_section_ptr if
6184 info->shared. I don't know why, and that doesn't make sense,
6185 so I took it out. */
6186 *secp = elf_tdata (abfd)->elf_text_section;
6187 break;
6189 case SHN_MIPS_ACOMMON:
6190 /* Fall through. XXX Can we treat this as allocated data? */
6191 case SHN_MIPS_DATA:
6192 /* This section is used in a shared object. */
6193 if (elf_tdata (abfd)->elf_data_section == NULL)
6195 asymbol *elf_data_symbol;
6196 asection *elf_data_section;
6197 bfd_size_type amt = sizeof (asection);
6199 elf_data_section = bfd_zalloc (abfd, amt);
6200 if (elf_data_section == NULL)
6201 return FALSE;
6203 amt = sizeof (asymbol);
6204 elf_data_symbol = bfd_zalloc (abfd, amt);
6205 if (elf_data_symbol == NULL)
6206 return FALSE;
6208 /* Initialize the section. */
6210 elf_tdata (abfd)->elf_data_section = elf_data_section;
6211 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
6213 elf_data_section->symbol = elf_data_symbol;
6214 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
6216 elf_data_section->name = ".data";
6217 elf_data_section->flags = SEC_NO_FLAGS;
6218 elf_data_section->output_section = NULL;
6219 elf_data_section->owner = abfd;
6220 elf_data_symbol->name = ".data";
6221 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6222 elf_data_symbol->section = elf_data_section;
6224 /* This code used to do *secp = bfd_und_section_ptr if
6225 info->shared. I don't know why, and that doesn't make sense,
6226 so I took it out. */
6227 *secp = elf_tdata (abfd)->elf_data_section;
6228 break;
6230 case SHN_MIPS_SUNDEFINED:
6231 *secp = bfd_und_section_ptr;
6232 break;
6235 if (SGI_COMPAT (abfd)
6236 && ! info->shared
6237 && info->output_bfd->xvec == abfd->xvec
6238 && strcmp (*namep, "__rld_obj_head") == 0)
6240 struct elf_link_hash_entry *h;
6241 struct bfd_link_hash_entry *bh;
6243 /* Mark __rld_obj_head as dynamic. */
6244 bh = NULL;
6245 if (! (_bfd_generic_link_add_one_symbol
6246 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
6247 get_elf_backend_data (abfd)->collect, &bh)))
6248 return FALSE;
6250 h = (struct elf_link_hash_entry *) bh;
6251 h->non_elf = 0;
6252 h->def_regular = 1;
6253 h->type = STT_OBJECT;
6255 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6256 return FALSE;
6258 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
6261 /* If this is a mips16 text symbol, add 1 to the value to make it
6262 odd. This will cause something like .word SYM to come up with
6263 the right value when it is loaded into the PC. */
6264 if (ELF_ST_IS_MIPS16 (sym->st_other))
6265 ++*valp;
6267 return TRUE;
6270 /* This hook function is called before the linker writes out a global
6271 symbol. We mark symbols as small common if appropriate. This is
6272 also where we undo the increment of the value for a mips16 symbol. */
6274 bfd_boolean
6275 _bfd_mips_elf_link_output_symbol_hook
6276 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6277 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
6278 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
6280 /* If we see a common symbol, which implies a relocatable link, then
6281 if a symbol was small common in an input file, mark it as small
6282 common in the output file. */
6283 if (sym->st_shndx == SHN_COMMON
6284 && strcmp (input_sec->name, ".scommon") == 0)
6285 sym->st_shndx = SHN_MIPS_SCOMMON;
6287 if (ELF_ST_IS_MIPS16 (sym->st_other))
6288 sym->st_value &= ~1;
6290 return TRUE;
6293 /* Functions for the dynamic linker. */
6295 /* Create dynamic sections when linking against a dynamic object. */
6297 bfd_boolean
6298 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
6300 struct elf_link_hash_entry *h;
6301 struct bfd_link_hash_entry *bh;
6302 flagword flags;
6303 register asection *s;
6304 const char * const *namep;
6305 struct mips_elf_link_hash_table *htab;
6307 htab = mips_elf_hash_table (info);
6308 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6309 | SEC_LINKER_CREATED | SEC_READONLY);
6311 /* The psABI requires a read-only .dynamic section, but the VxWorks
6312 EABI doesn't. */
6313 if (!htab->is_vxworks)
6315 s = bfd_get_section_by_name (abfd, ".dynamic");
6316 if (s != NULL)
6318 if (! bfd_set_section_flags (abfd, s, flags))
6319 return FALSE;
6323 /* We need to create .got section. */
6324 if (! mips_elf_create_got_section (abfd, info, FALSE))
6325 return FALSE;
6327 if (! mips_elf_rel_dyn_section (info, TRUE))
6328 return FALSE;
6330 /* Create .stub section. */
6331 if (bfd_get_section_by_name (abfd,
6332 MIPS_ELF_STUB_SECTION_NAME (abfd)) == NULL)
6334 s = bfd_make_section_with_flags (abfd,
6335 MIPS_ELF_STUB_SECTION_NAME (abfd),
6336 flags | SEC_CODE);
6337 if (s == NULL
6338 || ! bfd_set_section_alignment (abfd, s,
6339 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6340 return FALSE;
6343 if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
6344 && !info->shared
6345 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
6347 s = bfd_make_section_with_flags (abfd, ".rld_map",
6348 flags &~ (flagword) SEC_READONLY);
6349 if (s == NULL
6350 || ! bfd_set_section_alignment (abfd, s,
6351 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6352 return FALSE;
6355 /* On IRIX5, we adjust add some additional symbols and change the
6356 alignments of several sections. There is no ABI documentation
6357 indicating that this is necessary on IRIX6, nor any evidence that
6358 the linker takes such action. */
6359 if (IRIX_COMPAT (abfd) == ict_irix5)
6361 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
6363 bh = NULL;
6364 if (! (_bfd_generic_link_add_one_symbol
6365 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
6366 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
6367 return FALSE;
6369 h = (struct elf_link_hash_entry *) bh;
6370 h->non_elf = 0;
6371 h->def_regular = 1;
6372 h->type = STT_SECTION;
6374 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6375 return FALSE;
6378 /* We need to create a .compact_rel section. */
6379 if (SGI_COMPAT (abfd))
6381 if (!mips_elf_create_compact_rel_section (abfd, info))
6382 return FALSE;
6385 /* Change alignments of some sections. */
6386 s = bfd_get_section_by_name (abfd, ".hash");
6387 if (s != NULL)
6388 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6389 s = bfd_get_section_by_name (abfd, ".dynsym");
6390 if (s != NULL)
6391 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6392 s = bfd_get_section_by_name (abfd, ".dynstr");
6393 if (s != NULL)
6394 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6395 s = bfd_get_section_by_name (abfd, ".reginfo");
6396 if (s != NULL)
6397 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6398 s = bfd_get_section_by_name (abfd, ".dynamic");
6399 if (s != NULL)
6400 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6403 if (!info->shared)
6405 const char *name;
6407 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
6408 bh = NULL;
6409 if (!(_bfd_generic_link_add_one_symbol
6410 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
6411 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
6412 return FALSE;
6414 h = (struct elf_link_hash_entry *) bh;
6415 h->non_elf = 0;
6416 h->def_regular = 1;
6417 h->type = STT_SECTION;
6419 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6420 return FALSE;
6422 if (! mips_elf_hash_table (info)->use_rld_obj_head)
6424 /* __rld_map is a four byte word located in the .data section
6425 and is filled in by the rtld to contain a pointer to
6426 the _r_debug structure. Its symbol value will be set in
6427 _bfd_mips_elf_finish_dynamic_symbol. */
6428 s = bfd_get_section_by_name (abfd, ".rld_map");
6429 BFD_ASSERT (s != NULL);
6431 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
6432 bh = NULL;
6433 if (!(_bfd_generic_link_add_one_symbol
6434 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
6435 get_elf_backend_data (abfd)->collect, &bh)))
6436 return FALSE;
6438 h = (struct elf_link_hash_entry *) bh;
6439 h->non_elf = 0;
6440 h->def_regular = 1;
6441 h->type = STT_OBJECT;
6443 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6444 return FALSE;
6448 if (htab->is_vxworks)
6450 /* Create the .plt, .rela.plt, .dynbss and .rela.bss sections.
6451 Also create the _PROCEDURE_LINKAGE_TABLE symbol. */
6452 if (!_bfd_elf_create_dynamic_sections (abfd, info))
6453 return FALSE;
6455 /* Cache the sections created above. */
6456 htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
6457 htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
6458 htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
6459 htab->splt = bfd_get_section_by_name (abfd, ".plt");
6460 if (!htab->sdynbss
6461 || (!htab->srelbss && !info->shared)
6462 || !htab->srelplt
6463 || !htab->splt)
6464 abort ();
6466 /* Do the usual VxWorks handling. */
6467 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
6468 return FALSE;
6470 /* Work out the PLT sizes. */
6471 if (info->shared)
6473 htab->plt_header_size
6474 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
6475 htab->plt_entry_size
6476 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
6478 else
6480 htab->plt_header_size
6481 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
6482 htab->plt_entry_size
6483 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
6487 return TRUE;
6490 /* Return true if relocation REL against section SEC is a REL rather than
6491 RELA relocation. RELOCS is the first relocation in the section and
6492 ABFD is the bfd that contains SEC. */
6494 static bfd_boolean
6495 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
6496 const Elf_Internal_Rela *relocs,
6497 const Elf_Internal_Rela *rel)
6499 Elf_Internal_Shdr *rel_hdr;
6500 const struct elf_backend_data *bed;
6502 /* To determine which flavor or relocation this is, we depend on the
6503 fact that the INPUT_SECTION's REL_HDR is read before its REL_HDR2. */
6504 rel_hdr = &elf_section_data (sec)->rel_hdr;
6505 bed = get_elf_backend_data (abfd);
6506 if ((size_t) (rel - relocs)
6507 >= (NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel))
6508 rel_hdr = elf_section_data (sec)->rel_hdr2;
6509 return rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (abfd);
6512 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
6513 HOWTO is the relocation's howto and CONTENTS points to the contents
6514 of the section that REL is against. */
6516 static bfd_vma
6517 mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
6518 reloc_howto_type *howto, bfd_byte *contents)
6520 bfd_byte *location;
6521 unsigned int r_type;
6522 bfd_vma addend;
6524 r_type = ELF_R_TYPE (abfd, rel->r_info);
6525 location = contents + rel->r_offset;
6527 /* Get the addend, which is stored in the input file. */
6528 _bfd_mips16_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
6529 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
6530 _bfd_mips16_elf_reloc_shuffle (abfd, r_type, FALSE, location);
6532 return addend & howto->src_mask;
6535 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
6536 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
6537 and update *ADDEND with the final addend. Return true on success
6538 or false if the LO16 could not be found. RELEND is the exclusive
6539 upper bound on the relocations for REL's section. */
6541 static bfd_boolean
6542 mips_elf_add_lo16_rel_addend (bfd *abfd,
6543 const Elf_Internal_Rela *rel,
6544 const Elf_Internal_Rela *relend,
6545 bfd_byte *contents, bfd_vma *addend)
6547 unsigned int r_type, lo16_type;
6548 const Elf_Internal_Rela *lo16_relocation;
6549 reloc_howto_type *lo16_howto;
6550 bfd_vma l;
6552 r_type = ELF_R_TYPE (abfd, rel->r_info);
6553 if (mips16_reloc_p (r_type))
6554 lo16_type = R_MIPS16_LO16;
6555 else
6556 lo16_type = R_MIPS_LO16;
6558 /* The combined value is the sum of the HI16 addend, left-shifted by
6559 sixteen bits, and the LO16 addend, sign extended. (Usually, the
6560 code does a `lui' of the HI16 value, and then an `addiu' of the
6561 LO16 value.)
6563 Scan ahead to find a matching LO16 relocation.
6565 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
6566 be immediately following. However, for the IRIX6 ABI, the next
6567 relocation may be a composed relocation consisting of several
6568 relocations for the same address. In that case, the R_MIPS_LO16
6569 relocation may occur as one of these. We permit a similar
6570 extension in general, as that is useful for GCC.
6572 In some cases GCC dead code elimination removes the LO16 but keeps
6573 the corresponding HI16. This is strictly speaking a violation of
6574 the ABI but not immediately harmful. */
6575 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
6576 if (lo16_relocation == NULL)
6577 return FALSE;
6579 /* Obtain the addend kept there. */
6580 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
6581 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
6583 l <<= lo16_howto->rightshift;
6584 l = _bfd_mips_elf_sign_extend (l, 16);
6586 *addend <<= 16;
6587 *addend += l;
6588 return TRUE;
6591 /* Try to read the contents of section SEC in bfd ABFD. Return true and
6592 store the contents in *CONTENTS on success. Assume that *CONTENTS
6593 already holds the contents if it is nonull on entry. */
6595 static bfd_boolean
6596 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
6598 if (*contents)
6599 return TRUE;
6601 /* Get cached copy if it exists. */
6602 if (elf_section_data (sec)->this_hdr.contents != NULL)
6604 *contents = elf_section_data (sec)->this_hdr.contents;
6605 return TRUE;
6608 return bfd_malloc_and_get_section (abfd, sec, contents);
6611 /* Look through the relocs for a section during the first phase, and
6612 allocate space in the global offset table. */
6614 bfd_boolean
6615 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
6616 asection *sec, const Elf_Internal_Rela *relocs)
6618 const char *name;
6619 bfd *dynobj;
6620 Elf_Internal_Shdr *symtab_hdr;
6621 struct elf_link_hash_entry **sym_hashes;
6622 struct mips_got_info *g;
6623 size_t extsymoff;
6624 const Elf_Internal_Rela *rel;
6625 const Elf_Internal_Rela *rel_end;
6626 asection *sgot;
6627 asection *sreloc;
6628 const struct elf_backend_data *bed;
6629 struct mips_elf_link_hash_table *htab;
6630 bfd_byte *contents;
6631 bfd_vma addend;
6632 reloc_howto_type *howto;
6634 if (info->relocatable)
6635 return TRUE;
6637 htab = mips_elf_hash_table (info);
6638 dynobj = elf_hash_table (info)->dynobj;
6639 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6640 sym_hashes = elf_sym_hashes (abfd);
6641 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
6643 bed = get_elf_backend_data (abfd);
6644 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
6646 /* Check for the mips16 stub sections. */
6648 name = bfd_get_section_name (abfd, sec);
6649 if (FN_STUB_P (name))
6651 unsigned long r_symndx;
6653 /* Look at the relocation information to figure out which symbol
6654 this is for. */
6656 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
6657 if (r_symndx == 0)
6659 (*_bfd_error_handler)
6660 (_("%B: Warning: cannot determine the target function for"
6661 " stub section `%s'"),
6662 abfd, name);
6663 bfd_set_error (bfd_error_bad_value);
6664 return FALSE;
6667 if (r_symndx < extsymoff
6668 || sym_hashes[r_symndx - extsymoff] == NULL)
6670 asection *o;
6672 /* This stub is for a local symbol. This stub will only be
6673 needed if there is some relocation in this BFD, other
6674 than a 16 bit function call, which refers to this symbol. */
6675 for (o = abfd->sections; o != NULL; o = o->next)
6677 Elf_Internal_Rela *sec_relocs;
6678 const Elf_Internal_Rela *r, *rend;
6680 /* We can ignore stub sections when looking for relocs. */
6681 if ((o->flags & SEC_RELOC) == 0
6682 || o->reloc_count == 0
6683 || section_allows_mips16_refs_p (o))
6684 continue;
6686 sec_relocs
6687 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
6688 info->keep_memory);
6689 if (sec_relocs == NULL)
6690 return FALSE;
6692 rend = sec_relocs + o->reloc_count;
6693 for (r = sec_relocs; r < rend; r++)
6694 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
6695 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
6696 break;
6698 if (elf_section_data (o)->relocs != sec_relocs)
6699 free (sec_relocs);
6701 if (r < rend)
6702 break;
6705 if (o == NULL)
6707 /* There is no non-call reloc for this stub, so we do
6708 not need it. Since this function is called before
6709 the linker maps input sections to output sections, we
6710 can easily discard it by setting the SEC_EXCLUDE
6711 flag. */
6712 sec->flags |= SEC_EXCLUDE;
6713 return TRUE;
6716 /* Record this stub in an array of local symbol stubs for
6717 this BFD. */
6718 if (elf_tdata (abfd)->local_stubs == NULL)
6720 unsigned long symcount;
6721 asection **n;
6722 bfd_size_type amt;
6724 if (elf_bad_symtab (abfd))
6725 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
6726 else
6727 symcount = symtab_hdr->sh_info;
6728 amt = symcount * sizeof (asection *);
6729 n = bfd_zalloc (abfd, amt);
6730 if (n == NULL)
6731 return FALSE;
6732 elf_tdata (abfd)->local_stubs = n;
6735 sec->flags |= SEC_KEEP;
6736 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
6738 /* We don't need to set mips16_stubs_seen in this case.
6739 That flag is used to see whether we need to look through
6740 the global symbol table for stubs. We don't need to set
6741 it here, because we just have a local stub. */
6743 else
6745 struct mips_elf_link_hash_entry *h;
6747 h = ((struct mips_elf_link_hash_entry *)
6748 sym_hashes[r_symndx - extsymoff]);
6750 while (h->root.root.type == bfd_link_hash_indirect
6751 || h->root.root.type == bfd_link_hash_warning)
6752 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
6754 /* H is the symbol this stub is for. */
6756 /* If we already have an appropriate stub for this function, we
6757 don't need another one, so we can discard this one. Since
6758 this function is called before the linker maps input sections
6759 to output sections, we can easily discard it by setting the
6760 SEC_EXCLUDE flag. */
6761 if (h->fn_stub != NULL)
6763 sec->flags |= SEC_EXCLUDE;
6764 return TRUE;
6767 sec->flags |= SEC_KEEP;
6768 h->fn_stub = sec;
6769 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
6772 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
6774 unsigned long r_symndx;
6775 struct mips_elf_link_hash_entry *h;
6776 asection **loc;
6778 /* Look at the relocation information to figure out which symbol
6779 this is for. */
6781 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
6782 if (r_symndx == 0)
6784 (*_bfd_error_handler)
6785 (_("%B: Warning: cannot determine the target function for"
6786 " stub section `%s'"),
6787 abfd, name);
6788 bfd_set_error (bfd_error_bad_value);
6789 return FALSE;
6792 if (r_symndx < extsymoff
6793 || sym_hashes[r_symndx - extsymoff] == NULL)
6795 asection *o;
6797 /* This stub is for a local symbol. This stub will only be
6798 needed if there is some relocation (R_MIPS16_26) in this BFD
6799 that refers to this symbol. */
6800 for (o = abfd->sections; o != NULL; o = o->next)
6802 Elf_Internal_Rela *sec_relocs;
6803 const Elf_Internal_Rela *r, *rend;
6805 /* We can ignore stub sections when looking for relocs. */
6806 if ((o->flags & SEC_RELOC) == 0
6807 || o->reloc_count == 0
6808 || section_allows_mips16_refs_p (o))
6809 continue;
6811 sec_relocs
6812 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
6813 info->keep_memory);
6814 if (sec_relocs == NULL)
6815 return FALSE;
6817 rend = sec_relocs + o->reloc_count;
6818 for (r = sec_relocs; r < rend; r++)
6819 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
6820 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
6821 break;
6823 if (elf_section_data (o)->relocs != sec_relocs)
6824 free (sec_relocs);
6826 if (r < rend)
6827 break;
6830 if (o == NULL)
6832 /* There is no non-call reloc for this stub, so we do
6833 not need it. Since this function is called before
6834 the linker maps input sections to output sections, we
6835 can easily discard it by setting the SEC_EXCLUDE
6836 flag. */
6837 sec->flags |= SEC_EXCLUDE;
6838 return TRUE;
6841 /* Record this stub in an array of local symbol call_stubs for
6842 this BFD. */
6843 if (elf_tdata (abfd)->local_call_stubs == NULL)
6845 unsigned long symcount;
6846 asection **n;
6847 bfd_size_type amt;
6849 if (elf_bad_symtab (abfd))
6850 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
6851 else
6852 symcount = symtab_hdr->sh_info;
6853 amt = symcount * sizeof (asection *);
6854 n = bfd_zalloc (abfd, amt);
6855 if (n == NULL)
6856 return FALSE;
6857 elf_tdata (abfd)->local_call_stubs = n;
6860 sec->flags |= SEC_KEEP;
6861 elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
6863 /* We don't need to set mips16_stubs_seen in this case.
6864 That flag is used to see whether we need to look through
6865 the global symbol table for stubs. We don't need to set
6866 it here, because we just have a local stub. */
6868 else
6870 h = ((struct mips_elf_link_hash_entry *)
6871 sym_hashes[r_symndx - extsymoff]);
6873 /* H is the symbol this stub is for. */
6875 if (CALL_FP_STUB_P (name))
6876 loc = &h->call_fp_stub;
6877 else
6878 loc = &h->call_stub;
6880 /* If we already have an appropriate stub for this function, we
6881 don't need another one, so we can discard this one. Since
6882 this function is called before the linker maps input sections
6883 to output sections, we can easily discard it by setting the
6884 SEC_EXCLUDE flag. */
6885 if (*loc != NULL)
6887 sec->flags |= SEC_EXCLUDE;
6888 return TRUE;
6891 sec->flags |= SEC_KEEP;
6892 *loc = sec;
6893 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
6897 if (dynobj == NULL)
6899 sgot = NULL;
6900 g = NULL;
6902 else
6904 sgot = mips_elf_got_section (dynobj, FALSE);
6905 if (sgot == NULL)
6906 g = NULL;
6907 else
6909 BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
6910 g = mips_elf_section_data (sgot)->u.got_info;
6911 BFD_ASSERT (g != NULL);
6915 sreloc = NULL;
6916 contents = NULL;
6917 for (rel = relocs; rel < rel_end; ++rel)
6919 unsigned long r_symndx;
6920 unsigned int r_type;
6921 struct elf_link_hash_entry *h;
6923 r_symndx = ELF_R_SYM (abfd, rel->r_info);
6924 r_type = ELF_R_TYPE (abfd, rel->r_info);
6926 if (r_symndx < extsymoff)
6927 h = NULL;
6928 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
6930 (*_bfd_error_handler)
6931 (_("%B: Malformed reloc detected for section %s"),
6932 abfd, name);
6933 bfd_set_error (bfd_error_bad_value);
6934 return FALSE;
6936 else
6938 h = sym_hashes[r_symndx - extsymoff];
6940 /* This may be an indirect symbol created because of a version. */
6941 if (h != NULL)
6943 while (h->root.type == bfd_link_hash_indirect)
6944 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6948 /* Some relocs require a global offset table. */
6949 if (dynobj == NULL || sgot == NULL)
6951 switch (r_type)
6953 case R_MIPS16_GOT16:
6954 case R_MIPS16_CALL16:
6955 case R_MIPS_GOT16:
6956 case R_MIPS_CALL16:
6957 case R_MIPS_CALL_HI16:
6958 case R_MIPS_CALL_LO16:
6959 case R_MIPS_GOT_HI16:
6960 case R_MIPS_GOT_LO16:
6961 case R_MIPS_GOT_PAGE:
6962 case R_MIPS_GOT_OFST:
6963 case R_MIPS_GOT_DISP:
6964 case R_MIPS_TLS_GOTTPREL:
6965 case R_MIPS_TLS_GD:
6966 case R_MIPS_TLS_LDM:
6967 if (dynobj == NULL)
6968 elf_hash_table (info)->dynobj = dynobj = abfd;
6969 if (! mips_elf_create_got_section (dynobj, info, FALSE))
6970 return FALSE;
6971 g = mips_elf_got_info (dynobj, &sgot);
6972 if (htab->is_vxworks && !info->shared)
6974 (*_bfd_error_handler)
6975 (_("%B: GOT reloc at 0x%lx not expected in executables"),
6976 abfd, (unsigned long) rel->r_offset);
6977 bfd_set_error (bfd_error_bad_value);
6978 return FALSE;
6980 break;
6982 case R_MIPS_32:
6983 case R_MIPS_REL32:
6984 case R_MIPS_64:
6985 /* In VxWorks executables, references to external symbols
6986 are handled using copy relocs or PLT stubs, so there's
6987 no need to add a dynamic relocation here. */
6988 if (dynobj == NULL
6989 && (info->shared || (h != NULL && !htab->is_vxworks))
6990 && (sec->flags & SEC_ALLOC) != 0)
6991 elf_hash_table (info)->dynobj = dynobj = abfd;
6992 break;
6994 default:
6995 break;
6999 if (h)
7001 ((struct mips_elf_link_hash_entry *) h)->is_relocation_target = TRUE;
7003 /* Relocations against the special VxWorks __GOTT_BASE__ and
7004 __GOTT_INDEX__ symbols must be left to the loader. Allocate
7005 room for them in .rela.dyn. */
7006 if (is_gott_symbol (info, h))
7008 if (sreloc == NULL)
7010 sreloc = mips_elf_rel_dyn_section (info, TRUE);
7011 if (sreloc == NULL)
7012 return FALSE;
7014 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
7015 if (MIPS_ELF_READONLY_SECTION (sec))
7016 /* We tell the dynamic linker that there are
7017 relocations against the text segment. */
7018 info->flags |= DF_TEXTREL;
7021 else if (r_type == R_MIPS_CALL_LO16
7022 || r_type == R_MIPS_GOT_LO16
7023 || r_type == R_MIPS_GOT_DISP
7024 || (got16_reloc_p (r_type) && htab->is_vxworks))
7026 /* We may need a local GOT entry for this relocation. We
7027 don't count R_MIPS_GOT_PAGE because we can estimate the
7028 maximum number of pages needed by looking at the size of
7029 the segment. Similar comments apply to R_MIPS*_GOT16 and
7030 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
7031 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
7032 R_MIPS_CALL_HI16 because these are always followed by an
7033 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
7034 if (! mips_elf_record_local_got_symbol (abfd, r_symndx,
7035 rel->r_addend, g, 0))
7036 return FALSE;
7039 switch (r_type)
7041 case R_MIPS_CALL16:
7042 case R_MIPS16_CALL16:
7043 if (h == NULL)
7045 (*_bfd_error_handler)
7046 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
7047 abfd, (unsigned long) rel->r_offset);
7048 bfd_set_error (bfd_error_bad_value);
7049 return FALSE;
7051 /* Fall through. */
7053 case R_MIPS_CALL_HI16:
7054 case R_MIPS_CALL_LO16:
7055 if (h != NULL)
7057 /* VxWorks call relocations point the function's .got.plt
7058 entry, which will be allocated by adjust_dynamic_symbol.
7059 Otherwise, this symbol requires a global GOT entry. */
7060 if ((!htab->is_vxworks || h->forced_local)
7061 && !mips_elf_record_global_got_symbol (h, abfd, info, g, 0))
7062 return FALSE;
7064 /* We need a stub, not a plt entry for the undefined
7065 function. But we record it as if it needs plt. See
7066 _bfd_elf_adjust_dynamic_symbol. */
7067 h->needs_plt = 1;
7068 h->type = STT_FUNC;
7070 break;
7072 case R_MIPS_GOT_PAGE:
7073 /* If this is a global, overridable symbol, GOT_PAGE will
7074 decay to GOT_DISP, so we'll need a GOT entry for it. */
7075 if (h)
7077 struct mips_elf_link_hash_entry *hmips =
7078 (struct mips_elf_link_hash_entry *) h;
7080 while (hmips->root.root.type == bfd_link_hash_indirect
7081 || hmips->root.root.type == bfd_link_hash_warning)
7082 hmips = (struct mips_elf_link_hash_entry *)
7083 hmips->root.root.u.i.link;
7085 /* This symbol is definitely not overridable. */
7086 if (hmips->root.def_regular
7087 && ! (info->shared && ! info->symbolic
7088 && ! hmips->root.forced_local))
7089 h = NULL;
7091 /* Fall through. */
7093 case R_MIPS16_GOT16:
7094 case R_MIPS_GOT16:
7095 case R_MIPS_GOT_HI16:
7096 case R_MIPS_GOT_LO16:
7097 if (!h || r_type == R_MIPS_GOT_PAGE)
7099 /* This relocation needs (or may need, if h != NULL) a
7100 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
7101 know for sure until we know whether the symbol is
7102 preemptible. */
7103 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
7105 if (!mips_elf_get_section_contents (abfd, sec, &contents))
7106 return FALSE;
7107 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7108 addend = mips_elf_read_rel_addend (abfd, rel,
7109 howto, contents);
7110 if (r_type == R_MIPS_GOT16)
7111 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
7112 contents, &addend);
7113 else
7114 addend <<= howto->rightshift;
7116 else
7117 addend = rel->r_addend;
7118 if (!mips_elf_record_got_page_entry (abfd, r_symndx, addend, g))
7119 return FALSE;
7120 break;
7122 /* Fall through. */
7124 case R_MIPS_GOT_DISP:
7125 if (h && ! mips_elf_record_global_got_symbol (h, abfd, info, g, 0))
7126 return FALSE;
7127 break;
7129 case R_MIPS_TLS_GOTTPREL:
7130 if (info->shared)
7131 info->flags |= DF_STATIC_TLS;
7132 /* Fall through */
7134 case R_MIPS_TLS_LDM:
7135 if (r_type == R_MIPS_TLS_LDM)
7137 r_symndx = 0;
7138 h = NULL;
7140 /* Fall through */
7142 case R_MIPS_TLS_GD:
7143 /* This symbol requires a global offset table entry, or two
7144 for TLS GD relocations. */
7146 unsigned char flag = (r_type == R_MIPS_TLS_GD
7147 ? GOT_TLS_GD
7148 : r_type == R_MIPS_TLS_LDM
7149 ? GOT_TLS_LDM
7150 : GOT_TLS_IE);
7151 if (h != NULL)
7153 struct mips_elf_link_hash_entry *hmips =
7154 (struct mips_elf_link_hash_entry *) h;
7155 hmips->tls_type |= flag;
7157 if (h && ! mips_elf_record_global_got_symbol (h, abfd, info, g, flag))
7158 return FALSE;
7160 else
7162 BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != 0);
7164 if (! mips_elf_record_local_got_symbol (abfd, r_symndx,
7165 rel->r_addend, g, flag))
7166 return FALSE;
7169 break;
7171 case R_MIPS_32:
7172 case R_MIPS_REL32:
7173 case R_MIPS_64:
7174 /* In VxWorks executables, references to external symbols
7175 are handled using copy relocs or PLT stubs, so there's
7176 no need to add a .rela.dyn entry for this relocation. */
7177 if ((info->shared || (h != NULL && !htab->is_vxworks))
7178 && !(h && strcmp (h->root.root.string, "__gnu_local_gp") == 0)
7179 && (sec->flags & SEC_ALLOC) != 0)
7181 if (sreloc == NULL)
7183 sreloc = mips_elf_rel_dyn_section (info, TRUE);
7184 if (sreloc == NULL)
7185 return FALSE;
7187 if (info->shared && h == NULL)
7189 /* When creating a shared object, we must copy these
7190 reloc types into the output file as R_MIPS_REL32
7191 relocs. Make room for this reloc in .rel(a).dyn. */
7192 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
7193 if (MIPS_ELF_READONLY_SECTION (sec))
7194 /* We tell the dynamic linker that there are
7195 relocations against the text segment. */
7196 info->flags |= DF_TEXTREL;
7198 else
7200 struct mips_elf_link_hash_entry *hmips;
7202 /* For a shared object, we must copy this relocation
7203 unless the symbol turns out to be undefined and
7204 weak with non-default visibility, in which case
7205 it will be left as zero.
7207 We could elide R_MIPS_REL32 for locally binding symbols
7208 in shared libraries, but do not yet do so.
7210 For an executable, we only need to copy this
7211 reloc if the symbol is defined in a dynamic
7212 object. */
7213 hmips = (struct mips_elf_link_hash_entry *) h;
7214 ++hmips->possibly_dynamic_relocs;
7215 if (MIPS_ELF_READONLY_SECTION (sec))
7216 /* We need it to tell the dynamic linker if there
7217 are relocations against the text segment. */
7218 hmips->readonly_reloc = TRUE;
7221 /* Even though we don't directly need a GOT entry for
7222 this symbol, a symbol must have a dynamic symbol
7223 table index greater that DT_MIPS_GOTSYM if there are
7224 dynamic relocations against it. This does not apply
7225 to VxWorks, which does not have the usual coupling
7226 between global GOT entries and .dynsym entries. */
7227 if (h != NULL && !htab->is_vxworks)
7229 if (dynobj == NULL)
7230 elf_hash_table (info)->dynobj = dynobj = abfd;
7231 if (! mips_elf_create_got_section (dynobj, info, TRUE))
7232 return FALSE;
7233 g = mips_elf_got_info (dynobj, &sgot);
7234 if (! mips_elf_record_global_got_symbol (h, abfd, info, g, 0))
7235 return FALSE;
7239 if (SGI_COMPAT (abfd))
7240 mips_elf_hash_table (info)->compact_rel_size +=
7241 sizeof (Elf32_External_crinfo);
7242 break;
7244 case R_MIPS_PC16:
7245 if (h)
7246 ((struct mips_elf_link_hash_entry *) h)->is_branch_target = TRUE;
7247 break;
7249 case R_MIPS_26:
7250 if (h)
7251 ((struct mips_elf_link_hash_entry *) h)->is_branch_target = TRUE;
7252 /* Fall through. */
7254 case R_MIPS_GPREL16:
7255 case R_MIPS_LITERAL:
7256 case R_MIPS_GPREL32:
7257 if (SGI_COMPAT (abfd))
7258 mips_elf_hash_table (info)->compact_rel_size +=
7259 sizeof (Elf32_External_crinfo);
7260 break;
7262 /* This relocation describes the C++ object vtable hierarchy.
7263 Reconstruct it for later use during GC. */
7264 case R_MIPS_GNU_VTINHERIT:
7265 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7266 return FALSE;
7267 break;
7269 /* This relocation describes which C++ vtable entries are actually
7270 used. Record for later use during GC. */
7271 case R_MIPS_GNU_VTENTRY:
7272 BFD_ASSERT (h != NULL);
7273 if (h != NULL
7274 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7275 return FALSE;
7276 break;
7278 default:
7279 break;
7282 /* We must not create a stub for a symbol that has relocations
7283 related to taking the function's address. This doesn't apply to
7284 VxWorks, where CALL relocs refer to a .got.plt entry instead of
7285 a normal .got entry. */
7286 if (!htab->is_vxworks && h != NULL)
7287 switch (r_type)
7289 default:
7290 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
7291 break;
7292 case R_MIPS16_CALL16:
7293 case R_MIPS_CALL16:
7294 case R_MIPS_CALL_HI16:
7295 case R_MIPS_CALL_LO16:
7296 case R_MIPS_JALR:
7297 break;
7300 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
7301 if there is one. We only need to handle global symbols here;
7302 we decide whether to keep or delete stubs for local symbols
7303 when processing the stub's relocations. */
7304 if (h != NULL
7305 && !mips16_call_reloc_p (r_type)
7306 && !section_allows_mips16_refs_p (sec))
7308 struct mips_elf_link_hash_entry *mh;
7310 mh = (struct mips_elf_link_hash_entry *) h;
7311 mh->need_fn_stub = TRUE;
7315 return TRUE;
7318 bfd_boolean
7319 _bfd_mips_relax_section (bfd *abfd, asection *sec,
7320 struct bfd_link_info *link_info,
7321 bfd_boolean *again)
7323 Elf_Internal_Rela *internal_relocs;
7324 Elf_Internal_Rela *irel, *irelend;
7325 Elf_Internal_Shdr *symtab_hdr;
7326 bfd_byte *contents = NULL;
7327 size_t extsymoff;
7328 bfd_boolean changed_contents = FALSE;
7329 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
7330 Elf_Internal_Sym *isymbuf = NULL;
7332 /* We are not currently changing any sizes, so only one pass. */
7333 *again = FALSE;
7335 if (link_info->relocatable)
7336 return TRUE;
7338 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7339 link_info->keep_memory);
7340 if (internal_relocs == NULL)
7341 return TRUE;
7343 irelend = internal_relocs + sec->reloc_count
7344 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
7345 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7346 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7348 for (irel = internal_relocs; irel < irelend; irel++)
7350 bfd_vma symval;
7351 bfd_signed_vma sym_offset;
7352 unsigned int r_type;
7353 unsigned long r_symndx;
7354 asection *sym_sec;
7355 unsigned long instruction;
7357 /* Turn jalr into bgezal, and jr into beq, if they're marked
7358 with a JALR relocation, that indicate where they jump to.
7359 This saves some pipeline bubbles. */
7360 r_type = ELF_R_TYPE (abfd, irel->r_info);
7361 if (r_type != R_MIPS_JALR)
7362 continue;
7364 r_symndx = ELF_R_SYM (abfd, irel->r_info);
7365 /* Compute the address of the jump target. */
7366 if (r_symndx >= extsymoff)
7368 struct mips_elf_link_hash_entry *h
7369 = ((struct mips_elf_link_hash_entry *)
7370 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
7372 while (h->root.root.type == bfd_link_hash_indirect
7373 || h->root.root.type == bfd_link_hash_warning)
7374 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7376 /* If a symbol is undefined, or if it may be overridden,
7377 skip it. */
7378 if (! ((h->root.root.type == bfd_link_hash_defined
7379 || h->root.root.type == bfd_link_hash_defweak)
7380 && h->root.root.u.def.section)
7381 || (link_info->shared && ! link_info->symbolic
7382 && !h->root.forced_local))
7383 continue;
7385 sym_sec = h->root.root.u.def.section;
7386 if (sym_sec->output_section)
7387 symval = (h->root.root.u.def.value
7388 + sym_sec->output_section->vma
7389 + sym_sec->output_offset);
7390 else
7391 symval = h->root.root.u.def.value;
7393 else
7395 Elf_Internal_Sym *isym;
7397 /* Read this BFD's symbols if we haven't done so already. */
7398 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
7400 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
7401 if (isymbuf == NULL)
7402 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
7403 symtab_hdr->sh_info, 0,
7404 NULL, NULL, NULL);
7405 if (isymbuf == NULL)
7406 goto relax_return;
7409 isym = isymbuf + r_symndx;
7410 if (isym->st_shndx == SHN_UNDEF)
7411 continue;
7412 else if (isym->st_shndx == SHN_ABS)
7413 sym_sec = bfd_abs_section_ptr;
7414 else if (isym->st_shndx == SHN_COMMON)
7415 sym_sec = bfd_com_section_ptr;
7416 else
7417 sym_sec
7418 = bfd_section_from_elf_index (abfd, isym->st_shndx);
7419 symval = isym->st_value
7420 + sym_sec->output_section->vma
7421 + sym_sec->output_offset;
7424 /* Compute branch offset, from delay slot of the jump to the
7425 branch target. */
7426 sym_offset = (symval + irel->r_addend)
7427 - (sec_start + irel->r_offset + 4);
7429 /* Branch offset must be properly aligned. */
7430 if ((sym_offset & 3) != 0)
7431 continue;
7433 sym_offset >>= 2;
7435 /* Check that it's in range. */
7436 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
7437 continue;
7439 /* Get the section contents if we haven't done so already. */
7440 if (!mips_elf_get_section_contents (abfd, sec, &contents))
7441 goto relax_return;
7443 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
7445 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
7446 if ((instruction & 0xfc1fffff) == 0x0000f809)
7447 instruction = 0x04110000;
7448 /* If it was jr <reg>, turn it into b <target>. */
7449 else if ((instruction & 0xfc1fffff) == 0x00000008)
7450 instruction = 0x10000000;
7451 else
7452 continue;
7454 instruction |= (sym_offset & 0xffff);
7455 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
7456 changed_contents = TRUE;
7459 if (contents != NULL
7460 && elf_section_data (sec)->this_hdr.contents != contents)
7462 if (!changed_contents && !link_info->keep_memory)
7463 free (contents);
7464 else
7466 /* Cache the section contents for elf_link_input_bfd. */
7467 elf_section_data (sec)->this_hdr.contents = contents;
7470 return TRUE;
7472 relax_return:
7473 if (contents != NULL
7474 && elf_section_data (sec)->this_hdr.contents != contents)
7475 free (contents);
7476 return FALSE;
7479 /* Allocate space for global sym dynamic relocs. */
7481 static bfd_boolean
7482 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
7484 struct bfd_link_info *info = inf;
7485 bfd *dynobj;
7486 struct mips_elf_link_hash_entry *hmips;
7487 struct mips_elf_link_hash_table *htab;
7489 htab = mips_elf_hash_table (info);
7490 dynobj = elf_hash_table (info)->dynobj;
7491 hmips = (struct mips_elf_link_hash_entry *) h;
7493 /* VxWorks executables are handled elsewhere; we only need to
7494 allocate relocations in shared objects. */
7495 if (htab->is_vxworks && !info->shared)
7496 return TRUE;
7498 /* If this symbol is defined in a dynamic object, or we are creating
7499 a shared library, we will need to copy any R_MIPS_32 or
7500 R_MIPS_REL32 relocs against it into the output file. */
7501 if (! info->relocatable
7502 && hmips->possibly_dynamic_relocs != 0
7503 && (h->root.type == bfd_link_hash_defweak
7504 || !h->def_regular
7505 || info->shared))
7507 bfd_boolean do_copy = TRUE;
7509 if (h->root.type == bfd_link_hash_undefweak)
7511 /* Do not copy relocations for undefined weak symbols with
7512 non-default visibility. */
7513 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
7514 do_copy = FALSE;
7516 /* Make sure undefined weak symbols are output as a dynamic
7517 symbol in PIEs. */
7518 else if (h->dynindx == -1 && !h->forced_local)
7520 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7521 return FALSE;
7525 if (do_copy)
7527 mips_elf_allocate_dynamic_relocations
7528 (dynobj, info, hmips->possibly_dynamic_relocs);
7529 if (hmips->readonly_reloc)
7530 /* We tell the dynamic linker that there are relocations
7531 against the text segment. */
7532 info->flags |= DF_TEXTREL;
7536 return TRUE;
7539 /* Adjust a symbol defined by a dynamic object and referenced by a
7540 regular object. The current definition is in some section of the
7541 dynamic object, but we're not including those sections. We have to
7542 change the definition to something the rest of the link can
7543 understand. */
7545 bfd_boolean
7546 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7547 struct elf_link_hash_entry *h)
7549 bfd *dynobj;
7550 struct mips_elf_link_hash_entry *hmips;
7551 asection *s;
7552 struct mips_elf_link_hash_table *htab;
7554 htab = mips_elf_hash_table (info);
7555 dynobj = elf_hash_table (info)->dynobj;
7557 /* Make sure we know what is going on here. */
7558 BFD_ASSERT (dynobj != NULL
7559 && (h->needs_plt
7560 || h->u.weakdef != NULL
7561 || (h->def_dynamic
7562 && h->ref_regular
7563 && !h->def_regular)));
7565 hmips = (struct mips_elf_link_hash_entry *) h;
7567 /* For a function, create a stub, if allowed. */
7568 if (! hmips->no_fn_stub
7569 && h->needs_plt)
7571 if (! elf_hash_table (info)->dynamic_sections_created)
7572 return TRUE;
7574 /* If this symbol is not defined in a regular file, then set
7575 the symbol to the stub location. This is required to make
7576 function pointers compare as equal between the normal
7577 executable and the shared library. */
7578 if (!h->def_regular)
7580 /* We need .stub section. */
7581 s = bfd_get_section_by_name (dynobj,
7582 MIPS_ELF_STUB_SECTION_NAME (dynobj));
7583 BFD_ASSERT (s != NULL);
7585 h->root.u.def.section = s;
7586 h->root.u.def.value = s->size;
7588 /* XXX Write this stub address somewhere. */
7589 h->plt.offset = s->size;
7591 /* Make room for this stub code. */
7592 s->size += htab->function_stub_size;
7594 /* The last half word of the stub will be filled with the index
7595 of this symbol in .dynsym section. */
7596 return TRUE;
7599 else if ((h->type == STT_FUNC)
7600 && !h->needs_plt)
7602 /* This will set the entry for this symbol in the GOT to 0, and
7603 the dynamic linker will take care of this. */
7604 h->root.u.def.value = 0;
7605 return TRUE;
7608 /* If this is a weak symbol, and there is a real definition, the
7609 processor independent code will have arranged for us to see the
7610 real definition first, and we can just use the same value. */
7611 if (h->u.weakdef != NULL)
7613 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7614 || h->u.weakdef->root.type == bfd_link_hash_defweak);
7615 h->root.u.def.section = h->u.weakdef->root.u.def.section;
7616 h->root.u.def.value = h->u.weakdef->root.u.def.value;
7617 return TRUE;
7620 /* This is a reference to a symbol defined by a dynamic object which
7621 is not a function. */
7623 return TRUE;
7626 /* Likewise, for VxWorks. */
7628 bfd_boolean
7629 _bfd_mips_vxworks_adjust_dynamic_symbol (struct bfd_link_info *info,
7630 struct elf_link_hash_entry *h)
7632 bfd *dynobj;
7633 struct mips_elf_link_hash_entry *hmips;
7634 struct mips_elf_link_hash_table *htab;
7636 htab = mips_elf_hash_table (info);
7637 dynobj = elf_hash_table (info)->dynobj;
7638 hmips = (struct mips_elf_link_hash_entry *) h;
7640 /* Make sure we know what is going on here. */
7641 BFD_ASSERT (dynobj != NULL
7642 && (h->needs_plt
7643 || h->needs_copy
7644 || h->u.weakdef != NULL
7645 || (h->def_dynamic
7646 && h->ref_regular
7647 && !h->def_regular)));
7649 /* If the symbol is defined by a dynamic object, we need a PLT stub if
7650 either (a) we want to branch to the symbol or (b) we're linking an
7651 executable that needs a canonical function address. In the latter
7652 case, the canonical address will be the address of the executable's
7653 load stub. */
7654 if ((hmips->is_branch_target
7655 || (!info->shared
7656 && h->type == STT_FUNC
7657 && hmips->is_relocation_target))
7658 && h->def_dynamic
7659 && h->ref_regular
7660 && !h->def_regular
7661 && !h->forced_local)
7662 h->needs_plt = 1;
7664 /* Locally-binding symbols do not need a PLT stub; we can refer to
7665 the functions directly. */
7666 else if (h->needs_plt
7667 && (SYMBOL_CALLS_LOCAL (info, h)
7668 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
7669 && h->root.type == bfd_link_hash_undefweak)))
7671 h->needs_plt = 0;
7672 return TRUE;
7675 if (h->needs_plt)
7677 /* If this is the first symbol to need a PLT entry, allocate room
7678 for the header, and for the header's .rela.plt.unloaded entries. */
7679 if (htab->splt->size == 0)
7681 htab->splt->size += htab->plt_header_size;
7682 if (!info->shared)
7683 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
7686 /* Assign the next .plt entry to this symbol. */
7687 h->plt.offset = htab->splt->size;
7688 htab->splt->size += htab->plt_entry_size;
7690 /* If the output file has no definition of the symbol, set the
7691 symbol's value to the address of the stub. Point at the PLT
7692 load stub rather than the lazy resolution stub; this stub
7693 will become the canonical function address. */
7694 if (!info->shared && !h->def_regular)
7696 h->root.u.def.section = htab->splt;
7697 h->root.u.def.value = h->plt.offset;
7698 h->root.u.def.value += 8;
7701 /* Make room for the .got.plt entry and the R_JUMP_SLOT relocation. */
7702 htab->sgotplt->size += 4;
7703 htab->srelplt->size += sizeof (Elf32_External_Rela);
7705 /* Make room for the .rela.plt.unloaded relocations. */
7706 if (!info->shared)
7707 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
7709 return TRUE;
7712 /* If a function symbol is defined by a dynamic object, and we do not
7713 need a PLT stub for it, the symbol's value should be zero. */
7714 if (h->type == STT_FUNC
7715 && h->def_dynamic
7716 && h->ref_regular
7717 && !h->def_regular)
7719 h->root.u.def.value = 0;
7720 return TRUE;
7723 /* If this is a weak symbol, and there is a real definition, the
7724 processor independent code will have arranged for us to see the
7725 real definition first, and we can just use the same value. */
7726 if (h->u.weakdef != NULL)
7728 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7729 || h->u.weakdef->root.type == bfd_link_hash_defweak);
7730 h->root.u.def.section = h->u.weakdef->root.u.def.section;
7731 h->root.u.def.value = h->u.weakdef->root.u.def.value;
7732 return TRUE;
7735 /* This is a reference to a symbol defined by a dynamic object which
7736 is not a function. */
7737 if (info->shared)
7738 return TRUE;
7740 /* We must allocate the symbol in our .dynbss section, which will
7741 become part of the .bss section of the executable. There will be
7742 an entry for this symbol in the .dynsym section. The dynamic
7743 object will contain position independent code, so all references
7744 from the dynamic object to this symbol will go through the global
7745 offset table. The dynamic linker will use the .dynsym entry to
7746 determine the address it must put in the global offset table, so
7747 both the dynamic object and the regular object will refer to the
7748 same memory location for the variable. */
7750 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
7752 htab->srelbss->size += sizeof (Elf32_External_Rela);
7753 h->needs_copy = 1;
7756 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
7759 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
7760 The number might be exact or a worst-case estimate, depending on how
7761 much information is available to elf_backend_omit_section_dynsym at
7762 the current linking stage. */
7764 static bfd_size_type
7765 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
7767 bfd_size_type count;
7769 count = 0;
7770 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
7772 asection *p;
7773 const struct elf_backend_data *bed;
7775 bed = get_elf_backend_data (output_bfd);
7776 for (p = output_bfd->sections; p ; p = p->next)
7777 if ((p->flags & SEC_EXCLUDE) == 0
7778 && (p->flags & SEC_ALLOC) != 0
7779 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
7780 ++count;
7782 return count;
7785 /* This function is called after all the input files have been read,
7786 and the input sections have been assigned to output sections. We
7787 check for any mips16 stub sections that we can discard. */
7789 bfd_boolean
7790 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
7791 struct bfd_link_info *info)
7793 asection *ri;
7795 bfd *dynobj;
7796 asection *s;
7797 struct mips_got_info *g;
7798 int i;
7799 bfd_size_type loadable_size = 0;
7800 bfd_size_type page_gotno;
7801 bfd_size_type dynsymcount;
7802 bfd *sub;
7803 struct mips_elf_count_tls_arg count_tls_arg;
7804 struct mips_elf_link_hash_table *htab;
7806 htab = mips_elf_hash_table (info);
7808 /* The .reginfo section has a fixed size. */
7809 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
7810 if (ri != NULL)
7811 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
7813 if (! (info->relocatable
7814 || ! mips_elf_hash_table (info)->mips16_stubs_seen))
7815 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
7816 mips_elf_check_mips16_stubs, info);
7818 dynobj = elf_hash_table (info)->dynobj;
7819 if (dynobj == NULL)
7820 /* Relocatable links don't have it. */
7821 return TRUE;
7823 g = mips_elf_got_info (dynobj, &s);
7824 if (s == NULL)
7825 return TRUE;
7827 /* Calculate the total loadable size of the output. That
7828 will give us the maximum number of GOT_PAGE entries
7829 required. */
7830 for (sub = info->input_bfds; sub; sub = sub->link_next)
7832 asection *subsection;
7834 for (subsection = sub->sections;
7835 subsection;
7836 subsection = subsection->next)
7838 if ((subsection->flags & SEC_ALLOC) == 0)
7839 continue;
7840 loadable_size += ((subsection->size + 0xf)
7841 &~ (bfd_size_type) 0xf);
7845 /* There has to be a global GOT entry for every symbol with
7846 a dynamic symbol table index of DT_MIPS_GOTSYM or
7847 higher. Therefore, it make sense to put those symbols
7848 that need GOT entries at the end of the symbol table. We
7849 do that here. */
7850 if (! mips_elf_sort_hash_table (info, 1))
7851 return FALSE;
7853 if (g->global_gotsym != NULL)
7854 i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx;
7855 else
7856 /* If there are no global symbols, or none requiring
7857 relocations, then GLOBAL_GOTSYM will be NULL. */
7858 i = 0;
7860 /* Get a worst-case estimate of the number of dynamic symbols needed.
7861 At this point, dynsymcount does not account for section symbols
7862 and count_section_dynsyms may overestimate the number that will
7863 be needed. */
7864 dynsymcount = (elf_hash_table (info)->dynsymcount
7865 + count_section_dynsyms (output_bfd, info));
7867 /* Determine the size of one stub entry. */
7868 htab->function_stub_size = (dynsymcount > 0x10000
7869 ? MIPS_FUNCTION_STUB_BIG_SIZE
7870 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
7872 /* In the worst case, we'll get one stub per dynamic symbol, plus
7873 one to account for the dummy entry at the end required by IRIX
7874 rld. */
7875 loadable_size += htab->function_stub_size * (i + 1);
7877 if (htab->is_vxworks)
7878 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
7879 relocations against local symbols evaluate to "G", and the EABI does
7880 not include R_MIPS_GOT_PAGE. */
7881 page_gotno = 0;
7882 else
7883 /* Assume there are two loadable segments consisting of contiguous
7884 sections. Is 5 enough? */
7885 page_gotno = (loadable_size >> 16) + 5;
7887 /* Choose the smaller of the two estimates; both are intended to be
7888 conservative. */
7889 if (page_gotno > g->page_gotno)
7890 page_gotno = g->page_gotno;
7892 g->local_gotno += page_gotno;
7893 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
7895 g->global_gotno = i;
7896 s->size += i * MIPS_ELF_GOT_SIZE (output_bfd);
7898 /* We need to calculate tls_gotno for global symbols at this point
7899 instead of building it up earlier, to avoid doublecounting
7900 entries for one global symbol from multiple input files. */
7901 count_tls_arg.info = info;
7902 count_tls_arg.needed = 0;
7903 elf_link_hash_traverse (elf_hash_table (info),
7904 mips_elf_count_global_tls_entries,
7905 &count_tls_arg);
7906 g->tls_gotno += count_tls_arg.needed;
7907 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
7909 mips_elf_resolve_final_got_entries (g);
7911 /* VxWorks does not support multiple GOTs. It initializes $gp to
7912 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
7913 dynamic loader. */
7914 if (!htab->is_vxworks && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
7916 if (! mips_elf_multi_got (output_bfd, info, g, s, page_gotno))
7917 return FALSE;
7919 else
7921 /* Set up TLS entries for the first GOT. */
7922 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
7923 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
7925 htab->computed_got_sizes = TRUE;
7927 return TRUE;
7930 /* Set the sizes of the dynamic sections. */
7932 bfd_boolean
7933 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
7934 struct bfd_link_info *info)
7936 bfd *dynobj;
7937 asection *s, *sreldyn;
7938 bfd_boolean reltext;
7939 struct mips_elf_link_hash_table *htab;
7941 htab = mips_elf_hash_table (info);
7942 dynobj = elf_hash_table (info)->dynobj;
7943 BFD_ASSERT (dynobj != NULL);
7945 if (elf_hash_table (info)->dynamic_sections_created)
7947 /* Set the contents of the .interp section to the interpreter. */
7948 if (info->executable)
7950 s = bfd_get_section_by_name (dynobj, ".interp");
7951 BFD_ASSERT (s != NULL);
7952 s->size
7953 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
7954 s->contents
7955 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
7959 /* Allocate space for global sym dynamic relocs. */
7960 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
7962 /* The check_relocs and adjust_dynamic_symbol entry points have
7963 determined the sizes of the various dynamic sections. Allocate
7964 memory for them. */
7965 reltext = FALSE;
7966 sreldyn = NULL;
7967 for (s = dynobj->sections; s != NULL; s = s->next)
7969 const char *name;
7971 /* It's OK to base decisions on the section name, because none
7972 of the dynobj section names depend upon the input files. */
7973 name = bfd_get_section_name (dynobj, s);
7975 if ((s->flags & SEC_LINKER_CREATED) == 0)
7976 continue;
7978 if (CONST_STRNEQ (name, ".rel"))
7980 if (s->size != 0)
7982 const char *outname;
7983 asection *target;
7985 /* If this relocation section applies to a read only
7986 section, then we probably need a DT_TEXTREL entry.
7987 If the relocation section is .rel(a).dyn, we always
7988 assert a DT_TEXTREL entry rather than testing whether
7989 there exists a relocation to a read only section or
7990 not. */
7991 outname = bfd_get_section_name (output_bfd,
7992 s->output_section);
7993 target = bfd_get_section_by_name (output_bfd, outname + 4);
7994 if ((target != NULL
7995 && (target->flags & SEC_READONLY) != 0
7996 && (target->flags & SEC_ALLOC) != 0)
7997 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
7998 reltext = TRUE;
8000 /* We use the reloc_count field as a counter if we need
8001 to copy relocs into the output file. */
8002 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
8003 s->reloc_count = 0;
8005 /* If combreloc is enabled, elf_link_sort_relocs() will
8006 sort relocations, but in a different way than we do,
8007 and before we're done creating relocations. Also, it
8008 will move them around between input sections'
8009 relocation's contents, so our sorting would be
8010 broken, so don't let it run. */
8011 info->combreloc = 0;
8014 else if (htab->is_vxworks && strcmp (name, ".got") == 0)
8016 /* Executables do not need a GOT. */
8017 if (info->shared)
8019 /* Allocate relocations for all but the reserved entries. */
8020 struct mips_got_info *g;
8021 unsigned int count;
8023 g = mips_elf_got_info (dynobj, NULL);
8024 count = (g->global_gotno
8025 + g->local_gotno
8026 - MIPS_RESERVED_GOTNO (info));
8027 mips_elf_allocate_dynamic_relocations (dynobj, info, count);
8030 else if (!htab->is_vxworks && CONST_STRNEQ (name, ".got"))
8032 /* _bfd_mips_elf_always_size_sections() has already done
8033 most of the work, but some symbols may have been mapped
8034 to versions that we must now resolve in the got_entries
8035 hash tables. */
8036 struct mips_got_info *gg = mips_elf_got_info (dynobj, NULL);
8037 struct mips_got_info *g = gg;
8038 struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
8039 unsigned int needed_relocs = 0;
8041 if (gg->next)
8043 set_got_offset_arg.value = MIPS_ELF_GOT_SIZE (output_bfd);
8044 set_got_offset_arg.info = info;
8046 /* NOTE 2005-02-03: How can this call, or the next, ever
8047 find any indirect entries to resolve? They were all
8048 resolved in mips_elf_multi_got. */
8049 mips_elf_resolve_final_got_entries (gg);
8050 for (g = gg->next; g && g->next != gg; g = g->next)
8052 unsigned int save_assign;
8054 mips_elf_resolve_final_got_entries (g);
8056 /* Assign offsets to global GOT entries. */
8057 save_assign = g->assigned_gotno;
8058 g->assigned_gotno = g->local_gotno;
8059 set_got_offset_arg.g = g;
8060 set_got_offset_arg.needed_relocs = 0;
8061 htab_traverse (g->got_entries,
8062 mips_elf_set_global_got_offset,
8063 &set_got_offset_arg);
8064 needed_relocs += set_got_offset_arg.needed_relocs;
8065 BFD_ASSERT (g->assigned_gotno - g->local_gotno
8066 <= g->global_gotno);
8068 g->assigned_gotno = save_assign;
8069 if (info->shared)
8071 needed_relocs += g->local_gotno - g->assigned_gotno;
8072 BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
8073 + g->next->global_gotno
8074 + g->next->tls_gotno
8075 + MIPS_RESERVED_GOTNO (info));
8079 else
8081 struct mips_elf_count_tls_arg arg;
8082 arg.info = info;
8083 arg.needed = 0;
8085 htab_traverse (gg->got_entries, mips_elf_count_local_tls_relocs,
8086 &arg);
8087 elf_link_hash_traverse (elf_hash_table (info),
8088 mips_elf_count_global_tls_relocs,
8089 &arg);
8091 needed_relocs += arg.needed;
8094 if (needed_relocs)
8095 mips_elf_allocate_dynamic_relocations (dynobj, info,
8096 needed_relocs);
8098 else if (strcmp (name, MIPS_ELF_STUB_SECTION_NAME (output_bfd)) == 0)
8100 /* IRIX rld assumes that the function stub isn't at the end
8101 of .text section. So put a dummy. XXX */
8102 s->size += htab->function_stub_size;
8104 else if (! info->shared
8105 && ! mips_elf_hash_table (info)->use_rld_obj_head
8106 && CONST_STRNEQ (name, ".rld_map"))
8108 /* We add a room for __rld_map. It will be filled in by the
8109 rtld to contain a pointer to the _r_debug structure. */
8110 s->size += 4;
8112 else if (SGI_COMPAT (output_bfd)
8113 && CONST_STRNEQ (name, ".compact_rel"))
8114 s->size += mips_elf_hash_table (info)->compact_rel_size;
8115 else if (! CONST_STRNEQ (name, ".init")
8116 && s != htab->sgotplt
8117 && s != htab->splt)
8119 /* It's not one of our sections, so don't allocate space. */
8120 continue;
8123 if (s->size == 0)
8125 s->flags |= SEC_EXCLUDE;
8126 continue;
8129 if ((s->flags & SEC_HAS_CONTENTS) == 0)
8130 continue;
8132 /* Allocate memory for this section last, since we may increase its
8133 size above. */
8134 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) == 0)
8136 sreldyn = s;
8137 continue;
8140 /* Allocate memory for the section contents. */
8141 s->contents = bfd_zalloc (dynobj, s->size);
8142 if (s->contents == NULL)
8144 bfd_set_error (bfd_error_no_memory);
8145 return FALSE;
8149 /* Allocate memory for the .rel(a).dyn section. */
8150 if (sreldyn != NULL)
8152 sreldyn->contents = bfd_zalloc (dynobj, sreldyn->size);
8153 if (sreldyn->contents == NULL)
8155 bfd_set_error (bfd_error_no_memory);
8156 return FALSE;
8160 if (elf_hash_table (info)->dynamic_sections_created)
8162 /* Add some entries to the .dynamic section. We fill in the
8163 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
8164 must add the entries now so that we get the correct size for
8165 the .dynamic section. */
8167 /* SGI object has the equivalence of DT_DEBUG in the
8168 DT_MIPS_RLD_MAP entry. This must come first because glibc
8169 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and GDB only
8170 looks at the first one it sees. */
8171 if (!info->shared
8172 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
8173 return FALSE;
8175 /* The DT_DEBUG entry may be filled in by the dynamic linker and
8176 used by the debugger. */
8177 if (info->executable
8178 && !SGI_COMPAT (output_bfd)
8179 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
8180 return FALSE;
8182 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
8183 info->flags |= DF_TEXTREL;
8185 if ((info->flags & DF_TEXTREL) != 0)
8187 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
8188 return FALSE;
8190 /* Clear the DF_TEXTREL flag. It will be set again if we
8191 write out an actual text relocation; we may not, because
8192 at this point we do not know whether e.g. any .eh_frame
8193 absolute relocations have been converted to PC-relative. */
8194 info->flags &= ~DF_TEXTREL;
8197 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
8198 return FALSE;
8200 if (htab->is_vxworks)
8202 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
8203 use any of the DT_MIPS_* tags. */
8204 if (mips_elf_rel_dyn_section (info, FALSE))
8206 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
8207 return FALSE;
8209 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
8210 return FALSE;
8212 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
8213 return FALSE;
8215 if (htab->splt->size > 0)
8217 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
8218 return FALSE;
8220 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
8221 return FALSE;
8223 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
8224 return FALSE;
8227 else
8229 if (mips_elf_rel_dyn_section (info, FALSE))
8231 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
8232 return FALSE;
8234 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
8235 return FALSE;
8237 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
8238 return FALSE;
8241 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
8242 return FALSE;
8244 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
8245 return FALSE;
8247 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
8248 return FALSE;
8250 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
8251 return FALSE;
8253 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
8254 return FALSE;
8256 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
8257 return FALSE;
8259 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
8260 return FALSE;
8262 if (IRIX_COMPAT (dynobj) == ict_irix5
8263 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
8264 return FALSE;
8266 if (IRIX_COMPAT (dynobj) == ict_irix6
8267 && (bfd_get_section_by_name
8268 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
8269 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
8270 return FALSE;
8272 if (htab->is_vxworks
8273 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
8274 return FALSE;
8277 return TRUE;
8280 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
8281 Adjust its R_ADDEND field so that it is correct for the output file.
8282 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
8283 and sections respectively; both use symbol indexes. */
8285 static void
8286 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
8287 bfd *input_bfd, Elf_Internal_Sym *local_syms,
8288 asection **local_sections, Elf_Internal_Rela *rel)
8290 unsigned int r_type, r_symndx;
8291 Elf_Internal_Sym *sym;
8292 asection *sec;
8294 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
8296 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8297 if (r_type == R_MIPS16_GPREL
8298 || r_type == R_MIPS_GPREL16
8299 || r_type == R_MIPS_GPREL32
8300 || r_type == R_MIPS_LITERAL)
8302 rel->r_addend += _bfd_get_gp_value (input_bfd);
8303 rel->r_addend -= _bfd_get_gp_value (output_bfd);
8306 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
8307 sym = local_syms + r_symndx;
8309 /* Adjust REL's addend to account for section merging. */
8310 if (!info->relocatable)
8312 sec = local_sections[r_symndx];
8313 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
8316 /* This would normally be done by the rela_normal code in elflink.c. */
8317 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8318 rel->r_addend += local_sections[r_symndx]->output_offset;
8322 /* Relocate a MIPS ELF section. */
8324 bfd_boolean
8325 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
8326 bfd *input_bfd, asection *input_section,
8327 bfd_byte *contents, Elf_Internal_Rela *relocs,
8328 Elf_Internal_Sym *local_syms,
8329 asection **local_sections)
8331 Elf_Internal_Rela *rel;
8332 const Elf_Internal_Rela *relend;
8333 bfd_vma addend = 0;
8334 bfd_boolean use_saved_addend_p = FALSE;
8335 const struct elf_backend_data *bed;
8337 bed = get_elf_backend_data (output_bfd);
8338 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
8339 for (rel = relocs; rel < relend; ++rel)
8341 const char *name;
8342 bfd_vma value = 0;
8343 reloc_howto_type *howto;
8344 bfd_boolean require_jalx;
8345 /* TRUE if the relocation is a RELA relocation, rather than a
8346 REL relocation. */
8347 bfd_boolean rela_relocation_p = TRUE;
8348 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8349 const char *msg;
8350 unsigned long r_symndx;
8351 asection *sec;
8352 Elf_Internal_Shdr *symtab_hdr;
8353 struct elf_link_hash_entry *h;
8355 /* Find the relocation howto for this relocation. */
8356 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type,
8357 NEWABI_P (input_bfd)
8358 && (MIPS_RELOC_RELA_P
8359 (input_bfd, input_section,
8360 rel - relocs)));
8362 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
8363 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8364 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
8366 sec = local_sections[r_symndx];
8367 h = NULL;
8369 else
8371 unsigned long extsymoff;
8373 extsymoff = 0;
8374 if (!elf_bad_symtab (input_bfd))
8375 extsymoff = symtab_hdr->sh_info;
8376 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
8377 while (h->root.type == bfd_link_hash_indirect
8378 || h->root.type == bfd_link_hash_warning)
8379 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8381 sec = NULL;
8382 if (h->root.type == bfd_link_hash_defined
8383 || h->root.type == bfd_link_hash_defweak)
8384 sec = h->root.u.def.section;
8387 if (sec != NULL && elf_discarded_section (sec))
8389 /* For relocs against symbols from removed linkonce sections,
8390 or sections discarded by a linker script, we just want the
8391 section contents zeroed. Avoid any special processing. */
8392 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
8393 rel->r_info = 0;
8394 rel->r_addend = 0;
8395 continue;
8398 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
8400 /* Some 32-bit code uses R_MIPS_64. In particular, people use
8401 64-bit code, but make sure all their addresses are in the
8402 lowermost or uppermost 32-bit section of the 64-bit address
8403 space. Thus, when they use an R_MIPS_64 they mean what is
8404 usually meant by R_MIPS_32, with the exception that the
8405 stored value is sign-extended to 64 bits. */
8406 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
8408 /* On big-endian systems, we need to lie about the position
8409 of the reloc. */
8410 if (bfd_big_endian (input_bfd))
8411 rel->r_offset += 4;
8414 if (!use_saved_addend_p)
8416 /* If these relocations were originally of the REL variety,
8417 we must pull the addend out of the field that will be
8418 relocated. Otherwise, we simply use the contents of the
8419 RELA relocation. */
8420 if (mips_elf_rel_relocation_p (input_bfd, input_section,
8421 relocs, rel))
8423 rela_relocation_p = FALSE;
8424 addend = mips_elf_read_rel_addend (input_bfd, rel,
8425 howto, contents);
8426 if (hi16_reloc_p (r_type)
8427 || (got16_reloc_p (r_type)
8428 && mips_elf_local_relocation_p (input_bfd, rel,
8429 local_sections, FALSE)))
8431 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
8432 contents, &addend))
8434 const char *name;
8436 if (h)
8437 name = h->root.root.string;
8438 else
8439 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
8440 local_syms + r_symndx,
8441 sec);
8442 (*_bfd_error_handler)
8443 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
8444 input_bfd, input_section, name, howto->name,
8445 rel->r_offset);
8448 else
8449 addend <<= howto->rightshift;
8451 else
8452 addend = rel->r_addend;
8453 mips_elf_adjust_addend (output_bfd, info, input_bfd,
8454 local_syms, local_sections, rel);
8457 if (info->relocatable)
8459 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
8460 && bfd_big_endian (input_bfd))
8461 rel->r_offset -= 4;
8463 if (!rela_relocation_p && rel->r_addend)
8465 addend += rel->r_addend;
8466 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
8467 addend = mips_elf_high (addend);
8468 else if (r_type == R_MIPS_HIGHER)
8469 addend = mips_elf_higher (addend);
8470 else if (r_type == R_MIPS_HIGHEST)
8471 addend = mips_elf_highest (addend);
8472 else
8473 addend >>= howto->rightshift;
8475 /* We use the source mask, rather than the destination
8476 mask because the place to which we are writing will be
8477 source of the addend in the final link. */
8478 addend &= howto->src_mask;
8480 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
8481 /* See the comment above about using R_MIPS_64 in the 32-bit
8482 ABI. Here, we need to update the addend. It would be
8483 possible to get away with just using the R_MIPS_32 reloc
8484 but for endianness. */
8486 bfd_vma sign_bits;
8487 bfd_vma low_bits;
8488 bfd_vma high_bits;
8490 if (addend & ((bfd_vma) 1 << 31))
8491 #ifdef BFD64
8492 sign_bits = ((bfd_vma) 1 << 32) - 1;
8493 #else
8494 sign_bits = -1;
8495 #endif
8496 else
8497 sign_bits = 0;
8499 /* If we don't know that we have a 64-bit type,
8500 do two separate stores. */
8501 if (bfd_big_endian (input_bfd))
8503 /* Store the sign-bits (which are most significant)
8504 first. */
8505 low_bits = sign_bits;
8506 high_bits = addend;
8508 else
8510 low_bits = addend;
8511 high_bits = sign_bits;
8513 bfd_put_32 (input_bfd, low_bits,
8514 contents + rel->r_offset);
8515 bfd_put_32 (input_bfd, high_bits,
8516 contents + rel->r_offset + 4);
8517 continue;
8520 if (! mips_elf_perform_relocation (info, howto, rel, addend,
8521 input_bfd, input_section,
8522 contents, FALSE))
8523 return FALSE;
8526 /* Go on to the next relocation. */
8527 continue;
8530 /* In the N32 and 64-bit ABIs there may be multiple consecutive
8531 relocations for the same offset. In that case we are
8532 supposed to treat the output of each relocation as the addend
8533 for the next. */
8534 if (rel + 1 < relend
8535 && rel->r_offset == rel[1].r_offset
8536 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
8537 use_saved_addend_p = TRUE;
8538 else
8539 use_saved_addend_p = FALSE;
8541 /* Figure out what value we are supposed to relocate. */
8542 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
8543 input_section, info, rel,
8544 addend, howto, local_syms,
8545 local_sections, &value,
8546 &name, &require_jalx,
8547 use_saved_addend_p))
8549 case bfd_reloc_continue:
8550 /* There's nothing to do. */
8551 continue;
8553 case bfd_reloc_undefined:
8554 /* mips_elf_calculate_relocation already called the
8555 undefined_symbol callback. There's no real point in
8556 trying to perform the relocation at this point, so we
8557 just skip ahead to the next relocation. */
8558 continue;
8560 case bfd_reloc_notsupported:
8561 msg = _("internal error: unsupported relocation error");
8562 info->callbacks->warning
8563 (info, msg, name, input_bfd, input_section, rel->r_offset);
8564 return FALSE;
8566 case bfd_reloc_overflow:
8567 if (use_saved_addend_p)
8568 /* Ignore overflow until we reach the last relocation for
8569 a given location. */
8571 else
8573 struct mips_elf_link_hash_table *htab;
8575 htab = mips_elf_hash_table (info);
8576 BFD_ASSERT (name != NULL);
8577 if (!htab->small_data_overflow_reported
8578 && (howto->type == R_MIPS_GPREL16
8579 || howto->type == R_MIPS_LITERAL))
8581 const char *msg =
8582 _("small-data section exceeds 64KB;"
8583 " lower small-data size limit (see option -G)");
8585 htab->small_data_overflow_reported = TRUE;
8586 (*info->callbacks->einfo) ("%P: %s\n", msg);
8588 if (! ((*info->callbacks->reloc_overflow)
8589 (info, NULL, name, howto->name, (bfd_vma) 0,
8590 input_bfd, input_section, rel->r_offset)))
8591 return FALSE;
8593 break;
8595 case bfd_reloc_ok:
8596 break;
8598 default:
8599 abort ();
8600 break;
8603 /* If we've got another relocation for the address, keep going
8604 until we reach the last one. */
8605 if (use_saved_addend_p)
8607 addend = value;
8608 continue;
8611 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
8612 /* See the comment above about using R_MIPS_64 in the 32-bit
8613 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
8614 that calculated the right value. Now, however, we
8615 sign-extend the 32-bit result to 64-bits, and store it as a
8616 64-bit value. We are especially generous here in that we
8617 go to extreme lengths to support this usage on systems with
8618 only a 32-bit VMA. */
8620 bfd_vma sign_bits;
8621 bfd_vma low_bits;
8622 bfd_vma high_bits;
8624 if (value & ((bfd_vma) 1 << 31))
8625 #ifdef BFD64
8626 sign_bits = ((bfd_vma) 1 << 32) - 1;
8627 #else
8628 sign_bits = -1;
8629 #endif
8630 else
8631 sign_bits = 0;
8633 /* If we don't know that we have a 64-bit type,
8634 do two separate stores. */
8635 if (bfd_big_endian (input_bfd))
8637 /* Undo what we did above. */
8638 rel->r_offset -= 4;
8639 /* Store the sign-bits (which are most significant)
8640 first. */
8641 low_bits = sign_bits;
8642 high_bits = value;
8644 else
8646 low_bits = value;
8647 high_bits = sign_bits;
8649 bfd_put_32 (input_bfd, low_bits,
8650 contents + rel->r_offset);
8651 bfd_put_32 (input_bfd, high_bits,
8652 contents + rel->r_offset + 4);
8653 continue;
8656 /* Actually perform the relocation. */
8657 if (! mips_elf_perform_relocation (info, howto, rel, value,
8658 input_bfd, input_section,
8659 contents, require_jalx))
8660 return FALSE;
8663 return TRUE;
8666 /* If NAME is one of the special IRIX6 symbols defined by the linker,
8667 adjust it appropriately now. */
8669 static void
8670 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
8671 const char *name, Elf_Internal_Sym *sym)
8673 /* The linker script takes care of providing names and values for
8674 these, but we must place them into the right sections. */
8675 static const char* const text_section_symbols[] = {
8676 "_ftext",
8677 "_etext",
8678 "__dso_displacement",
8679 "__elf_header",
8680 "__program_header_table",
8681 NULL
8684 static const char* const data_section_symbols[] = {
8685 "_fdata",
8686 "_edata",
8687 "_end",
8688 "_fbss",
8689 NULL
8692 const char* const *p;
8693 int i;
8695 for (i = 0; i < 2; ++i)
8696 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
8698 ++p)
8699 if (strcmp (*p, name) == 0)
8701 /* All of these symbols are given type STT_SECTION by the
8702 IRIX6 linker. */
8703 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8704 sym->st_other = STO_PROTECTED;
8706 /* The IRIX linker puts these symbols in special sections. */
8707 if (i == 0)
8708 sym->st_shndx = SHN_MIPS_TEXT;
8709 else
8710 sym->st_shndx = SHN_MIPS_DATA;
8712 break;
8716 /* Finish up dynamic symbol handling. We set the contents of various
8717 dynamic sections here. */
8719 bfd_boolean
8720 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
8721 struct bfd_link_info *info,
8722 struct elf_link_hash_entry *h,
8723 Elf_Internal_Sym *sym)
8725 bfd *dynobj;
8726 asection *sgot;
8727 struct mips_got_info *g, *gg;
8728 const char *name;
8729 int idx;
8730 struct mips_elf_link_hash_table *htab;
8731 struct mips_elf_link_hash_entry *hmips;
8733 htab = mips_elf_hash_table (info);
8734 dynobj = elf_hash_table (info)->dynobj;
8735 hmips = (struct mips_elf_link_hash_entry *) h;
8737 if (h->plt.offset != MINUS_ONE)
8739 asection *s;
8740 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
8742 /* This symbol has a stub. Set it up. */
8744 BFD_ASSERT (h->dynindx != -1);
8746 s = bfd_get_section_by_name (dynobj,
8747 MIPS_ELF_STUB_SECTION_NAME (dynobj));
8748 BFD_ASSERT (s != NULL);
8750 BFD_ASSERT ((htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
8751 || (h->dynindx <= 0xffff));
8753 /* Values up to 2^31 - 1 are allowed. Larger values would cause
8754 sign extension at runtime in the stub, resulting in a negative
8755 index value. */
8756 if (h->dynindx & ~0x7fffffff)
8757 return FALSE;
8759 /* Fill the stub. */
8760 idx = 0;
8761 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
8762 idx += 4;
8763 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
8764 idx += 4;
8765 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
8767 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
8768 stub + idx);
8769 idx += 4;
8771 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
8772 idx += 4;
8774 /* If a large stub is not required and sign extension is not a
8775 problem, then use legacy code in the stub. */
8776 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
8777 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff), stub + idx);
8778 else if (h->dynindx & ~0x7fff)
8779 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff), stub + idx);
8780 else
8781 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
8782 stub + idx);
8784 BFD_ASSERT (h->plt.offset <= s->size);
8785 memcpy (s->contents + h->plt.offset, stub, htab->function_stub_size);
8787 /* Mark the symbol as undefined. plt.offset != -1 occurs
8788 only for the referenced symbol. */
8789 sym->st_shndx = SHN_UNDEF;
8791 /* The run-time linker uses the st_value field of the symbol
8792 to reset the global offset table entry for this external
8793 to its stub address when unlinking a shared object. */
8794 sym->st_value = (s->output_section->vma + s->output_offset
8795 + h->plt.offset);
8798 /* If we have a MIPS16 function with a stub, the dynamic symbol must
8799 refer to the stub, since only the stub uses the standard calling
8800 conventions. */
8801 if (h->dynindx != -1 && hmips->fn_stub != NULL)
8803 BFD_ASSERT (hmips->need_fn_stub);
8804 sym->st_value = (hmips->fn_stub->output_section->vma
8805 + hmips->fn_stub->output_offset);
8806 sym->st_size = hmips->fn_stub->size;
8807 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
8810 BFD_ASSERT (h->dynindx != -1
8811 || h->forced_local);
8813 sgot = mips_elf_got_section (dynobj, FALSE);
8814 BFD_ASSERT (sgot != NULL);
8815 BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
8816 g = mips_elf_section_data (sgot)->u.got_info;
8817 BFD_ASSERT (g != NULL);
8819 /* Run through the global symbol table, creating GOT entries for all
8820 the symbols that need them. */
8821 if (g->global_gotsym != NULL
8822 && h->dynindx >= g->global_gotsym->dynindx)
8824 bfd_vma offset;
8825 bfd_vma value;
8827 value = sym->st_value;
8828 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
8829 R_MIPS_GOT16, info);
8830 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
8833 if (g->next && h->dynindx != -1 && h->type != STT_TLS)
8835 struct mips_got_entry e, *p;
8836 bfd_vma entry;
8837 bfd_vma offset;
8839 gg = g;
8841 e.abfd = output_bfd;
8842 e.symndx = -1;
8843 e.d.h = hmips;
8844 e.tls_type = 0;
8846 for (g = g->next; g->next != gg; g = g->next)
8848 if (g->got_entries
8849 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
8850 &e)))
8852 offset = p->gotidx;
8853 if (info->shared
8854 || (elf_hash_table (info)->dynamic_sections_created
8855 && p->d.h != NULL
8856 && p->d.h->root.def_dynamic
8857 && !p->d.h->root.def_regular))
8859 /* Create an R_MIPS_REL32 relocation for this entry. Due to
8860 the various compatibility problems, it's easier to mock
8861 up an R_MIPS_32 or R_MIPS_64 relocation and leave
8862 mips_elf_create_dynamic_relocation to calculate the
8863 appropriate addend. */
8864 Elf_Internal_Rela rel[3];
8866 memset (rel, 0, sizeof (rel));
8867 if (ABI_64_P (output_bfd))
8868 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
8869 else
8870 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
8871 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
8873 entry = 0;
8874 if (! (mips_elf_create_dynamic_relocation
8875 (output_bfd, info, rel,
8876 e.d.h, NULL, sym->st_value, &entry, sgot)))
8877 return FALSE;
8879 else
8880 entry = sym->st_value;
8881 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
8886 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
8887 name = h->root.root.string;
8888 if (strcmp (name, "_DYNAMIC") == 0
8889 || h == elf_hash_table (info)->hgot)
8890 sym->st_shndx = SHN_ABS;
8891 else if (strcmp (name, "_DYNAMIC_LINK") == 0
8892 || strcmp (name, "_DYNAMIC_LINKING") == 0)
8894 sym->st_shndx = SHN_ABS;
8895 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8896 sym->st_value = 1;
8898 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
8900 sym->st_shndx = SHN_ABS;
8901 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8902 sym->st_value = elf_gp (output_bfd);
8904 else if (SGI_COMPAT (output_bfd))
8906 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
8907 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
8909 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8910 sym->st_other = STO_PROTECTED;
8911 sym->st_value = 0;
8912 sym->st_shndx = SHN_MIPS_DATA;
8914 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
8916 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8917 sym->st_other = STO_PROTECTED;
8918 sym->st_value = mips_elf_hash_table (info)->procedure_count;
8919 sym->st_shndx = SHN_ABS;
8921 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
8923 if (h->type == STT_FUNC)
8924 sym->st_shndx = SHN_MIPS_TEXT;
8925 else if (h->type == STT_OBJECT)
8926 sym->st_shndx = SHN_MIPS_DATA;
8930 /* Handle the IRIX6-specific symbols. */
8931 if (IRIX_COMPAT (output_bfd) == ict_irix6)
8932 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
8934 if (! info->shared)
8936 if (! mips_elf_hash_table (info)->use_rld_obj_head
8937 && (strcmp (name, "__rld_map") == 0
8938 || strcmp (name, "__RLD_MAP") == 0))
8940 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
8941 BFD_ASSERT (s != NULL);
8942 sym->st_value = s->output_section->vma + s->output_offset;
8943 bfd_put_32 (output_bfd, 0, s->contents);
8944 if (mips_elf_hash_table (info)->rld_value == 0)
8945 mips_elf_hash_table (info)->rld_value = sym->st_value;
8947 else if (mips_elf_hash_table (info)->use_rld_obj_head
8948 && strcmp (name, "__rld_obj_head") == 0)
8950 /* IRIX6 does not use a .rld_map section. */
8951 if (IRIX_COMPAT (output_bfd) == ict_irix5
8952 || IRIX_COMPAT (output_bfd) == ict_none)
8953 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
8954 != NULL);
8955 mips_elf_hash_table (info)->rld_value = sym->st_value;
8959 /* Keep dynamic MIPS16 symbols odd. This allows the dynamic linker to
8960 treat MIPS16 symbols like any other. */
8961 if (ELF_ST_IS_MIPS16 (sym->st_other))
8963 BFD_ASSERT (sym->st_value & 1);
8964 sym->st_other -= STO_MIPS16;
8967 return TRUE;
8970 /* Likewise, for VxWorks. */
8972 bfd_boolean
8973 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
8974 struct bfd_link_info *info,
8975 struct elf_link_hash_entry *h,
8976 Elf_Internal_Sym *sym)
8978 bfd *dynobj;
8979 asection *sgot;
8980 struct mips_got_info *g;
8981 struct mips_elf_link_hash_table *htab;
8983 htab = mips_elf_hash_table (info);
8984 dynobj = elf_hash_table (info)->dynobj;
8986 if (h->plt.offset != (bfd_vma) -1)
8988 bfd_byte *loc;
8989 bfd_vma plt_address, plt_index, got_address, got_offset, branch_offset;
8990 Elf_Internal_Rela rel;
8991 static const bfd_vma *plt_entry;
8993 BFD_ASSERT (h->dynindx != -1);
8994 BFD_ASSERT (htab->splt != NULL);
8995 BFD_ASSERT (h->plt.offset <= htab->splt->size);
8997 /* Calculate the address of the .plt entry. */
8998 plt_address = (htab->splt->output_section->vma
8999 + htab->splt->output_offset
9000 + h->plt.offset);
9002 /* Calculate the index of the entry. */
9003 plt_index = ((h->plt.offset - htab->plt_header_size)
9004 / htab->plt_entry_size);
9006 /* Calculate the address of the .got.plt entry. */
9007 got_address = (htab->sgotplt->output_section->vma
9008 + htab->sgotplt->output_offset
9009 + plt_index * 4);
9011 /* Calculate the offset of the .got.plt entry from
9012 _GLOBAL_OFFSET_TABLE_. */
9013 got_offset = mips_elf_gotplt_index (info, h);
9015 /* Calculate the offset for the branch at the start of the PLT
9016 entry. The branch jumps to the beginning of .plt. */
9017 branch_offset = -(h->plt.offset / 4 + 1) & 0xffff;
9019 /* Fill in the initial value of the .got.plt entry. */
9020 bfd_put_32 (output_bfd, plt_address,
9021 htab->sgotplt->contents + plt_index * 4);
9023 /* Find out where the .plt entry should go. */
9024 loc = htab->splt->contents + h->plt.offset;
9026 if (info->shared)
9028 plt_entry = mips_vxworks_shared_plt_entry;
9029 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9030 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9032 else
9034 bfd_vma got_address_high, got_address_low;
9036 plt_entry = mips_vxworks_exec_plt_entry;
9037 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9038 got_address_low = got_address & 0xffff;
9040 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9041 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9042 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
9043 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
9044 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9045 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9046 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9047 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9049 loc = (htab->srelplt2->contents
9050 + (plt_index * 3 + 2) * sizeof (Elf32_External_Rela));
9052 /* Emit a relocation for the .got.plt entry. */
9053 rel.r_offset = got_address;
9054 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9055 rel.r_addend = h->plt.offset;
9056 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9058 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
9059 loc += sizeof (Elf32_External_Rela);
9060 rel.r_offset = plt_address + 8;
9061 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9062 rel.r_addend = got_offset;
9063 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9065 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
9066 loc += sizeof (Elf32_External_Rela);
9067 rel.r_offset += 4;
9068 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9069 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9072 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9073 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
9074 rel.r_offset = got_address;
9075 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
9076 rel.r_addend = 0;
9077 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9079 if (!h->def_regular)
9080 sym->st_shndx = SHN_UNDEF;
9083 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
9085 sgot = mips_elf_got_section (dynobj, FALSE);
9086 BFD_ASSERT (sgot != NULL);
9087 BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
9088 g = mips_elf_section_data (sgot)->u.got_info;
9089 BFD_ASSERT (g != NULL);
9091 /* See if this symbol has an entry in the GOT. */
9092 if (g->global_gotsym != NULL
9093 && h->dynindx >= g->global_gotsym->dynindx)
9095 bfd_vma offset;
9096 Elf_Internal_Rela outrel;
9097 bfd_byte *loc;
9098 asection *s;
9100 /* Install the symbol value in the GOT. */
9101 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9102 R_MIPS_GOT16, info);
9103 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
9105 /* Add a dynamic relocation for it. */
9106 s = mips_elf_rel_dyn_section (info, FALSE);
9107 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
9108 outrel.r_offset = (sgot->output_section->vma
9109 + sgot->output_offset
9110 + offset);
9111 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
9112 outrel.r_addend = 0;
9113 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
9116 /* Emit a copy reloc, if needed. */
9117 if (h->needs_copy)
9119 Elf_Internal_Rela rel;
9121 BFD_ASSERT (h->dynindx != -1);
9123 rel.r_offset = (h->root.u.def.section->output_section->vma
9124 + h->root.u.def.section->output_offset
9125 + h->root.u.def.value);
9126 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
9127 rel.r_addend = 0;
9128 bfd_elf32_swap_reloca_out (output_bfd, &rel,
9129 htab->srelbss->contents
9130 + (htab->srelbss->reloc_count
9131 * sizeof (Elf32_External_Rela)));
9132 ++htab->srelbss->reloc_count;
9135 /* If this is a mips16 symbol, force the value to be even. */
9136 if (ELF_ST_IS_MIPS16 (sym->st_other))
9137 sym->st_value &= ~1;
9139 return TRUE;
9142 /* Install the PLT header for a VxWorks executable and finalize the
9143 contents of .rela.plt.unloaded. */
9145 static void
9146 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9148 Elf_Internal_Rela rela;
9149 bfd_byte *loc;
9150 bfd_vma got_value, got_value_high, got_value_low, plt_address;
9151 static const bfd_vma *plt_entry;
9152 struct mips_elf_link_hash_table *htab;
9154 htab = mips_elf_hash_table (info);
9155 plt_entry = mips_vxworks_exec_plt0_entry;
9157 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
9158 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
9159 + htab->root.hgot->root.u.def.section->output_offset
9160 + htab->root.hgot->root.u.def.value);
9162 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
9163 got_value_low = got_value & 0xffff;
9165 /* Calculate the address of the PLT header. */
9166 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
9168 /* Install the PLT header. */
9169 loc = htab->splt->contents;
9170 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
9171 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
9172 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
9173 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9174 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9175 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9177 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
9178 loc = htab->srelplt2->contents;
9179 rela.r_offset = plt_address;
9180 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9181 rela.r_addend = 0;
9182 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
9183 loc += sizeof (Elf32_External_Rela);
9185 /* Output the relocation for the following addiu of
9186 %lo(_GLOBAL_OFFSET_TABLE_). */
9187 rela.r_offset += 4;
9188 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9189 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
9190 loc += sizeof (Elf32_External_Rela);
9192 /* Fix up the remaining relocations. They may have the wrong
9193 symbol index for _G_O_T_ or _P_L_T_ depending on the order
9194 in which symbols were output. */
9195 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
9197 Elf_Internal_Rela rel;
9199 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9200 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9201 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9202 loc += sizeof (Elf32_External_Rela);
9204 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9205 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9206 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9207 loc += sizeof (Elf32_External_Rela);
9209 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9210 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9211 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9212 loc += sizeof (Elf32_External_Rela);
9216 /* Install the PLT header for a VxWorks shared library. */
9218 static void
9219 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
9221 unsigned int i;
9222 struct mips_elf_link_hash_table *htab;
9224 htab = mips_elf_hash_table (info);
9226 /* We just need to copy the entry byte-by-byte. */
9227 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
9228 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
9229 htab->splt->contents + i * 4);
9232 /* Finish up the dynamic sections. */
9234 bfd_boolean
9235 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
9236 struct bfd_link_info *info)
9238 bfd *dynobj;
9239 asection *sdyn;
9240 asection *sgot;
9241 struct mips_got_info *gg, *g;
9242 struct mips_elf_link_hash_table *htab;
9244 htab = mips_elf_hash_table (info);
9245 dynobj = elf_hash_table (info)->dynobj;
9247 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
9249 sgot = mips_elf_got_section (dynobj, FALSE);
9250 if (sgot == NULL)
9251 gg = g = NULL;
9252 else
9254 BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
9255 gg = mips_elf_section_data (sgot)->u.got_info;
9256 BFD_ASSERT (gg != NULL);
9257 g = mips_elf_got_for_ibfd (gg, output_bfd);
9258 BFD_ASSERT (g != NULL);
9261 if (elf_hash_table (info)->dynamic_sections_created)
9263 bfd_byte *b;
9264 int dyn_to_skip = 0, dyn_skipped = 0;
9266 BFD_ASSERT (sdyn != NULL);
9267 BFD_ASSERT (g != NULL);
9269 for (b = sdyn->contents;
9270 b < sdyn->contents + sdyn->size;
9271 b += MIPS_ELF_DYN_SIZE (dynobj))
9273 Elf_Internal_Dyn dyn;
9274 const char *name;
9275 size_t elemsize;
9276 asection *s;
9277 bfd_boolean swap_out_p;
9279 /* Read in the current dynamic entry. */
9280 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
9282 /* Assume that we're going to modify it and write it out. */
9283 swap_out_p = TRUE;
9285 switch (dyn.d_tag)
9287 case DT_RELENT:
9288 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
9289 break;
9291 case DT_RELAENT:
9292 BFD_ASSERT (htab->is_vxworks);
9293 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
9294 break;
9296 case DT_STRSZ:
9297 /* Rewrite DT_STRSZ. */
9298 dyn.d_un.d_val =
9299 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
9300 break;
9302 case DT_PLTGOT:
9303 name = ".got";
9304 if (htab->is_vxworks)
9306 /* _GLOBAL_OFFSET_TABLE_ is defined to be the beginning
9307 of the ".got" section in DYNOBJ. */
9308 s = bfd_get_section_by_name (dynobj, name);
9309 BFD_ASSERT (s != NULL);
9310 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
9312 else
9314 s = bfd_get_section_by_name (output_bfd, name);
9315 BFD_ASSERT (s != NULL);
9316 dyn.d_un.d_ptr = s->vma;
9318 break;
9320 case DT_MIPS_RLD_VERSION:
9321 dyn.d_un.d_val = 1; /* XXX */
9322 break;
9324 case DT_MIPS_FLAGS:
9325 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
9326 break;
9328 case DT_MIPS_TIME_STAMP:
9330 time_t t;
9331 time (&t);
9332 dyn.d_un.d_val = t;
9334 break;
9336 case DT_MIPS_ICHECKSUM:
9337 /* XXX FIXME: */
9338 swap_out_p = FALSE;
9339 break;
9341 case DT_MIPS_IVERSION:
9342 /* XXX FIXME: */
9343 swap_out_p = FALSE;
9344 break;
9346 case DT_MIPS_BASE_ADDRESS:
9347 s = output_bfd->sections;
9348 BFD_ASSERT (s != NULL);
9349 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
9350 break;
9352 case DT_MIPS_LOCAL_GOTNO:
9353 dyn.d_un.d_val = g->local_gotno;
9354 break;
9356 case DT_MIPS_UNREFEXTNO:
9357 /* The index into the dynamic symbol table which is the
9358 entry of the first external symbol that is not
9359 referenced within the same object. */
9360 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
9361 break;
9363 case DT_MIPS_GOTSYM:
9364 if (gg->global_gotsym)
9366 dyn.d_un.d_val = gg->global_gotsym->dynindx;
9367 break;
9369 /* In case if we don't have global got symbols we default
9370 to setting DT_MIPS_GOTSYM to the same value as
9371 DT_MIPS_SYMTABNO, so we just fall through. */
9373 case DT_MIPS_SYMTABNO:
9374 name = ".dynsym";
9375 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
9376 s = bfd_get_section_by_name (output_bfd, name);
9377 BFD_ASSERT (s != NULL);
9379 dyn.d_un.d_val = s->size / elemsize;
9380 break;
9382 case DT_MIPS_HIPAGENO:
9383 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO (info);
9384 break;
9386 case DT_MIPS_RLD_MAP:
9387 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
9388 break;
9390 case DT_MIPS_OPTIONS:
9391 s = (bfd_get_section_by_name
9392 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
9393 dyn.d_un.d_ptr = s->vma;
9394 break;
9396 case DT_RELASZ:
9397 BFD_ASSERT (htab->is_vxworks);
9398 /* The count does not include the JUMP_SLOT relocations. */
9399 if (htab->srelplt)
9400 dyn.d_un.d_val -= htab->srelplt->size;
9401 break;
9403 case DT_PLTREL:
9404 BFD_ASSERT (htab->is_vxworks);
9405 dyn.d_un.d_val = DT_RELA;
9406 break;
9408 case DT_PLTRELSZ:
9409 BFD_ASSERT (htab->is_vxworks);
9410 dyn.d_un.d_val = htab->srelplt->size;
9411 break;
9413 case DT_JMPREL:
9414 BFD_ASSERT (htab->is_vxworks);
9415 dyn.d_un.d_val = (htab->srelplt->output_section->vma
9416 + htab->srelplt->output_offset);
9417 break;
9419 case DT_TEXTREL:
9420 /* If we didn't need any text relocations after all, delete
9421 the dynamic tag. */
9422 if (!(info->flags & DF_TEXTREL))
9424 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
9425 swap_out_p = FALSE;
9427 break;
9429 case DT_FLAGS:
9430 /* If we didn't need any text relocations after all, clear
9431 DF_TEXTREL from DT_FLAGS. */
9432 if (!(info->flags & DF_TEXTREL))
9433 dyn.d_un.d_val &= ~DF_TEXTREL;
9434 else
9435 swap_out_p = FALSE;
9436 break;
9438 default:
9439 swap_out_p = FALSE;
9440 if (htab->is_vxworks
9441 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
9442 swap_out_p = TRUE;
9443 break;
9446 if (swap_out_p || dyn_skipped)
9447 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
9448 (dynobj, &dyn, b - dyn_skipped);
9450 if (dyn_to_skip)
9452 dyn_skipped += dyn_to_skip;
9453 dyn_to_skip = 0;
9457 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
9458 if (dyn_skipped > 0)
9459 memset (b - dyn_skipped, 0, dyn_skipped);
9462 if (sgot != NULL && sgot->size > 0
9463 && !bfd_is_abs_section (sgot->output_section))
9465 if (htab->is_vxworks)
9467 /* The first entry of the global offset table points to the
9468 ".dynamic" section. The second is initialized by the
9469 loader and contains the shared library identifier.
9470 The third is also initialized by the loader and points
9471 to the lazy resolution stub. */
9472 MIPS_ELF_PUT_WORD (output_bfd,
9473 sdyn->output_offset + sdyn->output_section->vma,
9474 sgot->contents);
9475 MIPS_ELF_PUT_WORD (output_bfd, 0,
9476 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
9477 MIPS_ELF_PUT_WORD (output_bfd, 0,
9478 sgot->contents
9479 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
9481 else
9483 /* The first entry of the global offset table will be filled at
9484 runtime. The second entry will be used by some runtime loaders.
9485 This isn't the case of IRIX rld. */
9486 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
9487 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
9488 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
9491 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
9492 = MIPS_ELF_GOT_SIZE (output_bfd);
9495 /* Generate dynamic relocations for the non-primary gots. */
9496 if (gg != NULL && gg->next)
9498 Elf_Internal_Rela rel[3];
9499 bfd_vma addend = 0;
9501 memset (rel, 0, sizeof (rel));
9502 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
9504 for (g = gg->next; g->next != gg; g = g->next)
9506 bfd_vma index = g->next->local_gotno + g->next->global_gotno
9507 + g->next->tls_gotno;
9509 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
9510 + index++ * MIPS_ELF_GOT_SIZE (output_bfd));
9511 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
9512 sgot->contents
9513 + index++ * MIPS_ELF_GOT_SIZE (output_bfd));
9515 if (! info->shared)
9516 continue;
9518 while (index < g->assigned_gotno)
9520 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
9521 = index++ * MIPS_ELF_GOT_SIZE (output_bfd);
9522 if (!(mips_elf_create_dynamic_relocation
9523 (output_bfd, info, rel, NULL,
9524 bfd_abs_section_ptr,
9525 0, &addend, sgot)))
9526 return FALSE;
9527 BFD_ASSERT (addend == 0);
9532 /* The generation of dynamic relocations for the non-primary gots
9533 adds more dynamic relocations. We cannot count them until
9534 here. */
9536 if (elf_hash_table (info)->dynamic_sections_created)
9538 bfd_byte *b;
9539 bfd_boolean swap_out_p;
9541 BFD_ASSERT (sdyn != NULL);
9543 for (b = sdyn->contents;
9544 b < sdyn->contents + sdyn->size;
9545 b += MIPS_ELF_DYN_SIZE (dynobj))
9547 Elf_Internal_Dyn dyn;
9548 asection *s;
9550 /* Read in the current dynamic entry. */
9551 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
9553 /* Assume that we're going to modify it and write it out. */
9554 swap_out_p = TRUE;
9556 switch (dyn.d_tag)
9558 case DT_RELSZ:
9559 /* Reduce DT_RELSZ to account for any relocations we
9560 decided not to make. This is for the n64 irix rld,
9561 which doesn't seem to apply any relocations if there
9562 are trailing null entries. */
9563 s = mips_elf_rel_dyn_section (info, FALSE);
9564 dyn.d_un.d_val = (s->reloc_count
9565 * (ABI_64_P (output_bfd)
9566 ? sizeof (Elf64_Mips_External_Rel)
9567 : sizeof (Elf32_External_Rel)));
9568 /* Adjust the section size too. Tools like the prelinker
9569 can reasonably expect the values to the same. */
9570 elf_section_data (s->output_section)->this_hdr.sh_size
9571 = dyn.d_un.d_val;
9572 break;
9574 default:
9575 swap_out_p = FALSE;
9576 break;
9579 if (swap_out_p)
9580 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
9581 (dynobj, &dyn, b);
9586 asection *s;
9587 Elf32_compact_rel cpt;
9589 if (SGI_COMPAT (output_bfd))
9591 /* Write .compact_rel section out. */
9592 s = bfd_get_section_by_name (dynobj, ".compact_rel");
9593 if (s != NULL)
9595 cpt.id1 = 1;
9596 cpt.num = s->reloc_count;
9597 cpt.id2 = 2;
9598 cpt.offset = (s->output_section->filepos
9599 + sizeof (Elf32_External_compact_rel));
9600 cpt.reserved0 = 0;
9601 cpt.reserved1 = 0;
9602 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
9603 ((Elf32_External_compact_rel *)
9604 s->contents));
9606 /* Clean up a dummy stub function entry in .text. */
9607 s = bfd_get_section_by_name (dynobj,
9608 MIPS_ELF_STUB_SECTION_NAME (dynobj));
9609 if (s != NULL)
9611 file_ptr dummy_offset;
9613 BFD_ASSERT (s->size >= htab->function_stub_size);
9614 dummy_offset = s->size - htab->function_stub_size;
9615 memset (s->contents + dummy_offset, 0,
9616 htab->function_stub_size);
9621 /* The psABI says that the dynamic relocations must be sorted in
9622 increasing order of r_symndx. The VxWorks EABI doesn't require
9623 this, and because the code below handles REL rather than RELA
9624 relocations, using it for VxWorks would be outright harmful. */
9625 if (!htab->is_vxworks)
9627 s = mips_elf_rel_dyn_section (info, FALSE);
9628 if (s != NULL
9629 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
9631 reldyn_sorting_bfd = output_bfd;
9633 if (ABI_64_P (output_bfd))
9634 qsort ((Elf64_External_Rel *) s->contents + 1,
9635 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
9636 sort_dynamic_relocs_64);
9637 else
9638 qsort ((Elf32_External_Rel *) s->contents + 1,
9639 s->reloc_count - 1, sizeof (Elf32_External_Rel),
9640 sort_dynamic_relocs);
9645 if (htab->is_vxworks && htab->splt->size > 0)
9647 if (info->shared)
9648 mips_vxworks_finish_shared_plt (output_bfd, info);
9649 else
9650 mips_vxworks_finish_exec_plt (output_bfd, info);
9652 return TRUE;
9656 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
9658 static void
9659 mips_set_isa_flags (bfd *abfd)
9661 flagword val;
9663 switch (bfd_get_mach (abfd))
9665 default:
9666 case bfd_mach_mips3000:
9667 val = E_MIPS_ARCH_1;
9668 break;
9670 case bfd_mach_mips3900:
9671 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
9672 break;
9674 case bfd_mach_mips6000:
9675 val = E_MIPS_ARCH_2;
9676 break;
9678 case bfd_mach_mips4000:
9679 case bfd_mach_mips4300:
9680 case bfd_mach_mips4400:
9681 case bfd_mach_mips4600:
9682 val = E_MIPS_ARCH_3;
9683 break;
9685 case bfd_mach_mips4010:
9686 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
9687 break;
9689 case bfd_mach_mips4100:
9690 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
9691 break;
9693 case bfd_mach_mips4111:
9694 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
9695 break;
9697 case bfd_mach_mips4120:
9698 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
9699 break;
9701 case bfd_mach_mips4650:
9702 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
9703 break;
9705 case bfd_mach_mips5400:
9706 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
9707 break;
9709 case bfd_mach_mips5500:
9710 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
9711 break;
9713 case bfd_mach_mips9000:
9714 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
9715 break;
9717 case bfd_mach_mips5000:
9718 case bfd_mach_mips7000:
9719 case bfd_mach_mips8000:
9720 case bfd_mach_mips10000:
9721 case bfd_mach_mips12000:
9722 val = E_MIPS_ARCH_4;
9723 break;
9725 case bfd_mach_mips5:
9726 val = E_MIPS_ARCH_5;
9727 break;
9729 case bfd_mach_mips_loongson_2e:
9730 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
9731 break;
9733 case bfd_mach_mips_loongson_2f:
9734 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
9735 break;
9737 case bfd_mach_mips_sb1:
9738 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
9739 break;
9741 case bfd_mach_mips_octeon:
9742 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
9743 break;
9745 case bfd_mach_mipsisa32:
9746 val = E_MIPS_ARCH_32;
9747 break;
9749 case bfd_mach_mipsisa64:
9750 val = E_MIPS_ARCH_64;
9751 break;
9753 case bfd_mach_mipsisa32r2:
9754 val = E_MIPS_ARCH_32R2;
9755 break;
9757 case bfd_mach_mipsisa64r2:
9758 val = E_MIPS_ARCH_64R2;
9759 break;
9761 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
9762 elf_elfheader (abfd)->e_flags |= val;
9767 /* The final processing done just before writing out a MIPS ELF object
9768 file. This gets the MIPS architecture right based on the machine
9769 number. This is used by both the 32-bit and the 64-bit ABI. */
9771 void
9772 _bfd_mips_elf_final_write_processing (bfd *abfd,
9773 bfd_boolean linker ATTRIBUTE_UNUSED)
9775 unsigned int i;
9776 Elf_Internal_Shdr **hdrpp;
9777 const char *name;
9778 asection *sec;
9780 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
9781 is nonzero. This is for compatibility with old objects, which used
9782 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
9783 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
9784 mips_set_isa_flags (abfd);
9786 /* Set the sh_info field for .gptab sections and other appropriate
9787 info for each special section. */
9788 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
9789 i < elf_numsections (abfd);
9790 i++, hdrpp++)
9792 switch ((*hdrpp)->sh_type)
9794 case SHT_MIPS_MSYM:
9795 case SHT_MIPS_LIBLIST:
9796 sec = bfd_get_section_by_name (abfd, ".dynstr");
9797 if (sec != NULL)
9798 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
9799 break;
9801 case SHT_MIPS_GPTAB:
9802 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
9803 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
9804 BFD_ASSERT (name != NULL
9805 && CONST_STRNEQ (name, ".gptab."));
9806 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
9807 BFD_ASSERT (sec != NULL);
9808 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
9809 break;
9811 case SHT_MIPS_CONTENT:
9812 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
9813 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
9814 BFD_ASSERT (name != NULL
9815 && CONST_STRNEQ (name, ".MIPS.content"));
9816 sec = bfd_get_section_by_name (abfd,
9817 name + sizeof ".MIPS.content" - 1);
9818 BFD_ASSERT (sec != NULL);
9819 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
9820 break;
9822 case SHT_MIPS_SYMBOL_LIB:
9823 sec = bfd_get_section_by_name (abfd, ".dynsym");
9824 if (sec != NULL)
9825 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
9826 sec = bfd_get_section_by_name (abfd, ".liblist");
9827 if (sec != NULL)
9828 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
9829 break;
9831 case SHT_MIPS_EVENTS:
9832 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
9833 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
9834 BFD_ASSERT (name != NULL);
9835 if (CONST_STRNEQ (name, ".MIPS.events"))
9836 sec = bfd_get_section_by_name (abfd,
9837 name + sizeof ".MIPS.events" - 1);
9838 else
9840 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
9841 sec = bfd_get_section_by_name (abfd,
9842 (name
9843 + sizeof ".MIPS.post_rel" - 1));
9845 BFD_ASSERT (sec != NULL);
9846 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
9847 break;
9853 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
9854 segments. */
9857 _bfd_mips_elf_additional_program_headers (bfd *abfd,
9858 struct bfd_link_info *info ATTRIBUTE_UNUSED)
9860 asection *s;
9861 int ret = 0;
9863 /* See if we need a PT_MIPS_REGINFO segment. */
9864 s = bfd_get_section_by_name (abfd, ".reginfo");
9865 if (s && (s->flags & SEC_LOAD))
9866 ++ret;
9868 /* See if we need a PT_MIPS_OPTIONS segment. */
9869 if (IRIX_COMPAT (abfd) == ict_irix6
9870 && bfd_get_section_by_name (abfd,
9871 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
9872 ++ret;
9874 /* See if we need a PT_MIPS_RTPROC segment. */
9875 if (IRIX_COMPAT (abfd) == ict_irix5
9876 && bfd_get_section_by_name (abfd, ".dynamic")
9877 && bfd_get_section_by_name (abfd, ".mdebug"))
9878 ++ret;
9880 /* Allocate a PT_NULL header in dynamic objects. See
9881 _bfd_mips_elf_modify_segment_map for details. */
9882 if (!SGI_COMPAT (abfd)
9883 && bfd_get_section_by_name (abfd, ".dynamic"))
9884 ++ret;
9886 return ret;
9889 /* Modify the segment map for an IRIX5 executable. */
9891 bfd_boolean
9892 _bfd_mips_elf_modify_segment_map (bfd *abfd,
9893 struct bfd_link_info *info)
9895 asection *s;
9896 struct elf_segment_map *m, **pm;
9897 bfd_size_type amt;
9899 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
9900 segment. */
9901 s = bfd_get_section_by_name (abfd, ".reginfo");
9902 if (s != NULL && (s->flags & SEC_LOAD) != 0)
9904 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
9905 if (m->p_type == PT_MIPS_REGINFO)
9906 break;
9907 if (m == NULL)
9909 amt = sizeof *m;
9910 m = bfd_zalloc (abfd, amt);
9911 if (m == NULL)
9912 return FALSE;
9914 m->p_type = PT_MIPS_REGINFO;
9915 m->count = 1;
9916 m->sections[0] = s;
9918 /* We want to put it after the PHDR and INTERP segments. */
9919 pm = &elf_tdata (abfd)->segment_map;
9920 while (*pm != NULL
9921 && ((*pm)->p_type == PT_PHDR
9922 || (*pm)->p_type == PT_INTERP))
9923 pm = &(*pm)->next;
9925 m->next = *pm;
9926 *pm = m;
9930 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
9931 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
9932 PT_MIPS_OPTIONS segment immediately following the program header
9933 table. */
9934 if (NEWABI_P (abfd)
9935 /* On non-IRIX6 new abi, we'll have already created a segment
9936 for this section, so don't create another. I'm not sure this
9937 is not also the case for IRIX 6, but I can't test it right
9938 now. */
9939 && IRIX_COMPAT (abfd) == ict_irix6)
9941 for (s = abfd->sections; s; s = s->next)
9942 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
9943 break;
9945 if (s)
9947 struct elf_segment_map *options_segment;
9949 pm = &elf_tdata (abfd)->segment_map;
9950 while (*pm != NULL
9951 && ((*pm)->p_type == PT_PHDR
9952 || (*pm)->p_type == PT_INTERP))
9953 pm = &(*pm)->next;
9955 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
9957 amt = sizeof (struct elf_segment_map);
9958 options_segment = bfd_zalloc (abfd, amt);
9959 options_segment->next = *pm;
9960 options_segment->p_type = PT_MIPS_OPTIONS;
9961 options_segment->p_flags = PF_R;
9962 options_segment->p_flags_valid = TRUE;
9963 options_segment->count = 1;
9964 options_segment->sections[0] = s;
9965 *pm = options_segment;
9969 else
9971 if (IRIX_COMPAT (abfd) == ict_irix5)
9973 /* If there are .dynamic and .mdebug sections, we make a room
9974 for the RTPROC header. FIXME: Rewrite without section names. */
9975 if (bfd_get_section_by_name (abfd, ".interp") == NULL
9976 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
9977 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
9979 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
9980 if (m->p_type == PT_MIPS_RTPROC)
9981 break;
9982 if (m == NULL)
9984 amt = sizeof *m;
9985 m = bfd_zalloc (abfd, amt);
9986 if (m == NULL)
9987 return FALSE;
9989 m->p_type = PT_MIPS_RTPROC;
9991 s = bfd_get_section_by_name (abfd, ".rtproc");
9992 if (s == NULL)
9994 m->count = 0;
9995 m->p_flags = 0;
9996 m->p_flags_valid = 1;
9998 else
10000 m->count = 1;
10001 m->sections[0] = s;
10004 /* We want to put it after the DYNAMIC segment. */
10005 pm = &elf_tdata (abfd)->segment_map;
10006 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
10007 pm = &(*pm)->next;
10008 if (*pm != NULL)
10009 pm = &(*pm)->next;
10011 m->next = *pm;
10012 *pm = m;
10016 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
10017 .dynstr, .dynsym, and .hash sections, and everything in
10018 between. */
10019 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
10020 pm = &(*pm)->next)
10021 if ((*pm)->p_type == PT_DYNAMIC)
10022 break;
10023 m = *pm;
10024 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
10026 /* For a normal mips executable the permissions for the PT_DYNAMIC
10027 segment are read, write and execute. We do that here since
10028 the code in elf.c sets only the read permission. This matters
10029 sometimes for the dynamic linker. */
10030 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
10032 m->p_flags = PF_R | PF_W | PF_X;
10033 m->p_flags_valid = 1;
10036 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
10037 glibc's dynamic linker has traditionally derived the number of
10038 tags from the p_filesz field, and sometimes allocates stack
10039 arrays of that size. An overly-big PT_DYNAMIC segment can
10040 be actively harmful in such cases. Making PT_DYNAMIC contain
10041 other sections can also make life hard for the prelinker,
10042 which might move one of the other sections to a different
10043 PT_LOAD segment. */
10044 if (SGI_COMPAT (abfd)
10045 && m != NULL
10046 && m->count == 1
10047 && strcmp (m->sections[0]->name, ".dynamic") == 0)
10049 static const char *sec_names[] =
10051 ".dynamic", ".dynstr", ".dynsym", ".hash"
10053 bfd_vma low, high;
10054 unsigned int i, c;
10055 struct elf_segment_map *n;
10057 low = ~(bfd_vma) 0;
10058 high = 0;
10059 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
10061 s = bfd_get_section_by_name (abfd, sec_names[i]);
10062 if (s != NULL && (s->flags & SEC_LOAD) != 0)
10064 bfd_size_type sz;
10066 if (low > s->vma)
10067 low = s->vma;
10068 sz = s->size;
10069 if (high < s->vma + sz)
10070 high = s->vma + sz;
10074 c = 0;
10075 for (s = abfd->sections; s != NULL; s = s->next)
10076 if ((s->flags & SEC_LOAD) != 0
10077 && s->vma >= low
10078 && s->vma + s->size <= high)
10079 ++c;
10081 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
10082 n = bfd_zalloc (abfd, amt);
10083 if (n == NULL)
10084 return FALSE;
10085 *n = *m;
10086 n->count = c;
10088 i = 0;
10089 for (s = abfd->sections; s != NULL; s = s->next)
10091 if ((s->flags & SEC_LOAD) != 0
10092 && s->vma >= low
10093 && s->vma + s->size <= high)
10095 n->sections[i] = s;
10096 ++i;
10100 *pm = n;
10104 /* Allocate a spare program header in dynamic objects so that tools
10105 like the prelinker can add an extra PT_LOAD entry.
10107 If the prelinker needs to make room for a new PT_LOAD entry, its
10108 standard procedure is to move the first (read-only) sections into
10109 the new (writable) segment. However, the MIPS ABI requires
10110 .dynamic to be in a read-only segment, and the section will often
10111 start within sizeof (ElfNN_Phdr) bytes of the last program header.
10113 Although the prelinker could in principle move .dynamic to a
10114 writable segment, it seems better to allocate a spare program
10115 header instead, and avoid the need to move any sections.
10116 There is a long tradition of allocating spare dynamic tags,
10117 so allocating a spare program header seems like a natural
10118 extension.
10120 If INFO is NULL, we may be copying an already prelinked binary
10121 with objcopy or strip, so do not add this header. */
10122 if (info != NULL
10123 && !SGI_COMPAT (abfd)
10124 && bfd_get_section_by_name (abfd, ".dynamic"))
10126 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
10127 if ((*pm)->p_type == PT_NULL)
10128 break;
10129 if (*pm == NULL)
10131 m = bfd_zalloc (abfd, sizeof (*m));
10132 if (m == NULL)
10133 return FALSE;
10135 m->p_type = PT_NULL;
10136 *pm = m;
10140 return TRUE;
10143 /* Return the section that should be marked against GC for a given
10144 relocation. */
10146 asection *
10147 _bfd_mips_elf_gc_mark_hook (asection *sec,
10148 struct bfd_link_info *info,
10149 Elf_Internal_Rela *rel,
10150 struct elf_link_hash_entry *h,
10151 Elf_Internal_Sym *sym)
10153 /* ??? Do mips16 stub sections need to be handled special? */
10155 if (h != NULL)
10156 switch (ELF_R_TYPE (sec->owner, rel->r_info))
10158 case R_MIPS_GNU_VTINHERIT:
10159 case R_MIPS_GNU_VTENTRY:
10160 return NULL;
10163 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
10166 /* Update the got entry reference counts for the section being removed. */
10168 bfd_boolean
10169 _bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
10170 struct bfd_link_info *info ATTRIBUTE_UNUSED,
10171 asection *sec ATTRIBUTE_UNUSED,
10172 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
10174 #if 0
10175 Elf_Internal_Shdr *symtab_hdr;
10176 struct elf_link_hash_entry **sym_hashes;
10177 bfd_signed_vma *local_got_refcounts;
10178 const Elf_Internal_Rela *rel, *relend;
10179 unsigned long r_symndx;
10180 struct elf_link_hash_entry *h;
10182 if (info->relocatable)
10183 return TRUE;
10185 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10186 sym_hashes = elf_sym_hashes (abfd);
10187 local_got_refcounts = elf_local_got_refcounts (abfd);
10189 relend = relocs + sec->reloc_count;
10190 for (rel = relocs; rel < relend; rel++)
10191 switch (ELF_R_TYPE (abfd, rel->r_info))
10193 case R_MIPS16_GOT16:
10194 case R_MIPS16_CALL16:
10195 case R_MIPS_GOT16:
10196 case R_MIPS_CALL16:
10197 case R_MIPS_CALL_HI16:
10198 case R_MIPS_CALL_LO16:
10199 case R_MIPS_GOT_HI16:
10200 case R_MIPS_GOT_LO16:
10201 case R_MIPS_GOT_DISP:
10202 case R_MIPS_GOT_PAGE:
10203 case R_MIPS_GOT_OFST:
10204 /* ??? It would seem that the existing MIPS code does no sort
10205 of reference counting or whatnot on its GOT and PLT entries,
10206 so it is not possible to garbage collect them at this time. */
10207 break;
10209 default:
10210 break;
10212 #endif
10214 return TRUE;
10217 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
10218 hiding the old indirect symbol. Process additional relocation
10219 information. Also called for weakdefs, in which case we just let
10220 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
10222 void
10223 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
10224 struct elf_link_hash_entry *dir,
10225 struct elf_link_hash_entry *ind)
10227 struct mips_elf_link_hash_entry *dirmips, *indmips;
10229 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
10231 if (ind->root.type != bfd_link_hash_indirect)
10232 return;
10234 dirmips = (struct mips_elf_link_hash_entry *) dir;
10235 indmips = (struct mips_elf_link_hash_entry *) ind;
10236 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
10237 if (indmips->readonly_reloc)
10238 dirmips->readonly_reloc = TRUE;
10239 if (indmips->no_fn_stub)
10240 dirmips->no_fn_stub = TRUE;
10242 if (dirmips->tls_type == 0)
10243 dirmips->tls_type = indmips->tls_type;
10246 void
10247 _bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
10248 struct elf_link_hash_entry *entry,
10249 bfd_boolean force_local)
10251 bfd *dynobj;
10252 asection *got;
10253 struct mips_got_info *g;
10254 struct mips_elf_link_hash_entry *h;
10255 struct mips_elf_link_hash_table *htab;
10257 h = (struct mips_elf_link_hash_entry *) entry;
10258 if (h->forced_local)
10259 return;
10260 h->forced_local = force_local;
10262 dynobj = elf_hash_table (info)->dynobj;
10263 htab = mips_elf_hash_table (info);
10264 if (dynobj != NULL && force_local && h->root.type != STT_TLS
10265 && (got = mips_elf_got_section (dynobj, TRUE)) != NULL
10266 && (g = mips_elf_section_data (got)->u.got_info) != NULL)
10268 if (g->next)
10270 struct mips_got_entry e;
10271 struct mips_got_info *gg = g;
10273 /* Since we're turning what used to be a global symbol into a
10274 local one, bump up the number of local entries of each GOT
10275 that had an entry for it. This will automatically decrease
10276 the number of global entries, since global_gotno is actually
10277 the upper limit of global entries. */
10278 e.abfd = dynobj;
10279 e.symndx = -1;
10280 e.d.h = h;
10281 e.tls_type = 0;
10283 for (g = g->next; g != gg; g = g->next)
10284 if (htab_find (g->got_entries, &e))
10286 BFD_ASSERT (g->global_gotno > 0);
10287 g->local_gotno++;
10288 g->global_gotno--;
10291 /* If this was a global symbol forced into the primary GOT, we
10292 no longer need an entry for it. We can't release the entry
10293 at this point, but we must at least stop counting it as one
10294 of the symbols that required a forced got entry. */
10295 if (h->root.got.offset == 2)
10297 BFD_ASSERT (gg->assigned_gotno > 0);
10298 gg->assigned_gotno--;
10301 else if (h->root.got.offset == 1)
10303 /* check_relocs didn't know that this symbol would be
10304 forced-local, so add an extra local got entry. */
10305 g->local_gotno++;
10306 if (htab->computed_got_sizes)
10308 /* We'll have treated this symbol as global rather
10309 than local. */
10310 BFD_ASSERT (g->global_gotno > 0);
10311 g->global_gotno--;
10314 else if (htab->is_vxworks && h->root.needs_plt)
10316 /* check_relocs didn't know that this symbol would be
10317 forced-local, so add an extra local got entry. */
10318 g->local_gotno++;
10319 if (htab->computed_got_sizes)
10320 /* The symbol is only used in call relocations, so we'll
10321 have assumed it only needs a .got.plt entry. Increase
10322 the size of .got accordingly. */
10323 got->size += MIPS_ELF_GOT_SIZE (dynobj);
10327 _bfd_elf_link_hash_hide_symbol (info, &h->root, force_local);
10330 #define PDR_SIZE 32
10332 bfd_boolean
10333 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
10334 struct bfd_link_info *info)
10336 asection *o;
10337 bfd_boolean ret = FALSE;
10338 unsigned char *tdata;
10339 size_t i, skip;
10341 o = bfd_get_section_by_name (abfd, ".pdr");
10342 if (! o)
10343 return FALSE;
10344 if (o->size == 0)
10345 return FALSE;
10346 if (o->size % PDR_SIZE != 0)
10347 return FALSE;
10348 if (o->output_section != NULL
10349 && bfd_is_abs_section (o->output_section))
10350 return FALSE;
10352 tdata = bfd_zmalloc (o->size / PDR_SIZE);
10353 if (! tdata)
10354 return FALSE;
10356 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
10357 info->keep_memory);
10358 if (!cookie->rels)
10360 free (tdata);
10361 return FALSE;
10364 cookie->rel = cookie->rels;
10365 cookie->relend = cookie->rels + o->reloc_count;
10367 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
10369 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
10371 tdata[i] = 1;
10372 skip ++;
10376 if (skip != 0)
10378 mips_elf_section_data (o)->u.tdata = tdata;
10379 o->size -= skip * PDR_SIZE;
10380 ret = TRUE;
10382 else
10383 free (tdata);
10385 if (! info->keep_memory)
10386 free (cookie->rels);
10388 return ret;
10391 bfd_boolean
10392 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
10394 if (strcmp (sec->name, ".pdr") == 0)
10395 return TRUE;
10396 return FALSE;
10399 bfd_boolean
10400 _bfd_mips_elf_write_section (bfd *output_bfd,
10401 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
10402 asection *sec, bfd_byte *contents)
10404 bfd_byte *to, *from, *end;
10405 int i;
10407 if (strcmp (sec->name, ".pdr") != 0)
10408 return FALSE;
10410 if (mips_elf_section_data (sec)->u.tdata == NULL)
10411 return FALSE;
10413 to = contents;
10414 end = contents + sec->size;
10415 for (from = contents, i = 0;
10416 from < end;
10417 from += PDR_SIZE, i++)
10419 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
10420 continue;
10421 if (to != from)
10422 memcpy (to, from, PDR_SIZE);
10423 to += PDR_SIZE;
10425 bfd_set_section_contents (output_bfd, sec->output_section, contents,
10426 sec->output_offset, sec->size);
10427 return TRUE;
10430 /* MIPS ELF uses a special find_nearest_line routine in order the
10431 handle the ECOFF debugging information. */
10433 struct mips_elf_find_line
10435 struct ecoff_debug_info d;
10436 struct ecoff_find_line i;
10439 bfd_boolean
10440 _bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
10441 asymbol **symbols, bfd_vma offset,
10442 const char **filename_ptr,
10443 const char **functionname_ptr,
10444 unsigned int *line_ptr)
10446 asection *msec;
10448 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
10449 filename_ptr, functionname_ptr,
10450 line_ptr))
10451 return TRUE;
10453 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
10454 filename_ptr, functionname_ptr,
10455 line_ptr, ABI_64_P (abfd) ? 8 : 0,
10456 &elf_tdata (abfd)->dwarf2_find_line_info))
10457 return TRUE;
10459 msec = bfd_get_section_by_name (abfd, ".mdebug");
10460 if (msec != NULL)
10462 flagword origflags;
10463 struct mips_elf_find_line *fi;
10464 const struct ecoff_debug_swap * const swap =
10465 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
10467 /* If we are called during a link, mips_elf_final_link may have
10468 cleared the SEC_HAS_CONTENTS field. We force it back on here
10469 if appropriate (which it normally will be). */
10470 origflags = msec->flags;
10471 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
10472 msec->flags |= SEC_HAS_CONTENTS;
10474 fi = elf_tdata (abfd)->find_line_info;
10475 if (fi == NULL)
10477 bfd_size_type external_fdr_size;
10478 char *fraw_src;
10479 char *fraw_end;
10480 struct fdr *fdr_ptr;
10481 bfd_size_type amt = sizeof (struct mips_elf_find_line);
10483 fi = bfd_zalloc (abfd, amt);
10484 if (fi == NULL)
10486 msec->flags = origflags;
10487 return FALSE;
10490 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
10492 msec->flags = origflags;
10493 return FALSE;
10496 /* Swap in the FDR information. */
10497 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
10498 fi->d.fdr = bfd_alloc (abfd, amt);
10499 if (fi->d.fdr == NULL)
10501 msec->flags = origflags;
10502 return FALSE;
10504 external_fdr_size = swap->external_fdr_size;
10505 fdr_ptr = fi->d.fdr;
10506 fraw_src = (char *) fi->d.external_fdr;
10507 fraw_end = (fraw_src
10508 + fi->d.symbolic_header.ifdMax * external_fdr_size);
10509 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
10510 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
10512 elf_tdata (abfd)->find_line_info = fi;
10514 /* Note that we don't bother to ever free this information.
10515 find_nearest_line is either called all the time, as in
10516 objdump -l, so the information should be saved, or it is
10517 rarely called, as in ld error messages, so the memory
10518 wasted is unimportant. Still, it would probably be a
10519 good idea for free_cached_info to throw it away. */
10522 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
10523 &fi->i, filename_ptr, functionname_ptr,
10524 line_ptr))
10526 msec->flags = origflags;
10527 return TRUE;
10530 msec->flags = origflags;
10533 /* Fall back on the generic ELF find_nearest_line routine. */
10535 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
10536 filename_ptr, functionname_ptr,
10537 line_ptr);
10540 bfd_boolean
10541 _bfd_mips_elf_find_inliner_info (bfd *abfd,
10542 const char **filename_ptr,
10543 const char **functionname_ptr,
10544 unsigned int *line_ptr)
10546 bfd_boolean found;
10547 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
10548 functionname_ptr, line_ptr,
10549 & elf_tdata (abfd)->dwarf2_find_line_info);
10550 return found;
10554 /* When are writing out the .options or .MIPS.options section,
10555 remember the bytes we are writing out, so that we can install the
10556 GP value in the section_processing routine. */
10558 bfd_boolean
10559 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
10560 const void *location,
10561 file_ptr offset, bfd_size_type count)
10563 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
10565 bfd_byte *c;
10567 if (elf_section_data (section) == NULL)
10569 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
10570 section->used_by_bfd = bfd_zalloc (abfd, amt);
10571 if (elf_section_data (section) == NULL)
10572 return FALSE;
10574 c = mips_elf_section_data (section)->u.tdata;
10575 if (c == NULL)
10577 c = bfd_zalloc (abfd, section->size);
10578 if (c == NULL)
10579 return FALSE;
10580 mips_elf_section_data (section)->u.tdata = c;
10583 memcpy (c + offset, location, count);
10586 return _bfd_elf_set_section_contents (abfd, section, location, offset,
10587 count);
10590 /* This is almost identical to bfd_generic_get_... except that some
10591 MIPS relocations need to be handled specially. Sigh. */
10593 bfd_byte *
10594 _bfd_elf_mips_get_relocated_section_contents
10595 (bfd *abfd,
10596 struct bfd_link_info *link_info,
10597 struct bfd_link_order *link_order,
10598 bfd_byte *data,
10599 bfd_boolean relocatable,
10600 asymbol **symbols)
10602 /* Get enough memory to hold the stuff */
10603 bfd *input_bfd = link_order->u.indirect.section->owner;
10604 asection *input_section = link_order->u.indirect.section;
10605 bfd_size_type sz;
10607 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
10608 arelent **reloc_vector = NULL;
10609 long reloc_count;
10611 if (reloc_size < 0)
10612 goto error_return;
10614 reloc_vector = bfd_malloc (reloc_size);
10615 if (reloc_vector == NULL && reloc_size != 0)
10616 goto error_return;
10618 /* read in the section */
10619 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
10620 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
10621 goto error_return;
10623 reloc_count = bfd_canonicalize_reloc (input_bfd,
10624 input_section,
10625 reloc_vector,
10626 symbols);
10627 if (reloc_count < 0)
10628 goto error_return;
10630 if (reloc_count > 0)
10632 arelent **parent;
10633 /* for mips */
10634 int gp_found;
10635 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
10638 struct bfd_hash_entry *h;
10639 struct bfd_link_hash_entry *lh;
10640 /* Skip all this stuff if we aren't mixing formats. */
10641 if (abfd && input_bfd
10642 && abfd->xvec == input_bfd->xvec)
10643 lh = 0;
10644 else
10646 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
10647 lh = (struct bfd_link_hash_entry *) h;
10649 lookup:
10650 if (lh)
10652 switch (lh->type)
10654 case bfd_link_hash_undefined:
10655 case bfd_link_hash_undefweak:
10656 case bfd_link_hash_common:
10657 gp_found = 0;
10658 break;
10659 case bfd_link_hash_defined:
10660 case bfd_link_hash_defweak:
10661 gp_found = 1;
10662 gp = lh->u.def.value;
10663 break;
10664 case bfd_link_hash_indirect:
10665 case bfd_link_hash_warning:
10666 lh = lh->u.i.link;
10667 /* @@FIXME ignoring warning for now */
10668 goto lookup;
10669 case bfd_link_hash_new:
10670 default:
10671 abort ();
10674 else
10675 gp_found = 0;
10677 /* end mips */
10678 for (parent = reloc_vector; *parent != NULL; parent++)
10680 char *error_message = NULL;
10681 bfd_reloc_status_type r;
10683 /* Specific to MIPS: Deal with relocation types that require
10684 knowing the gp of the output bfd. */
10685 asymbol *sym = *(*parent)->sym_ptr_ptr;
10687 /* If we've managed to find the gp and have a special
10688 function for the relocation then go ahead, else default
10689 to the generic handling. */
10690 if (gp_found
10691 && (*parent)->howto->special_function
10692 == _bfd_mips_elf32_gprel16_reloc)
10693 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
10694 input_section, relocatable,
10695 data, gp);
10696 else
10697 r = bfd_perform_relocation (input_bfd, *parent, data,
10698 input_section,
10699 relocatable ? abfd : NULL,
10700 &error_message);
10702 if (relocatable)
10704 asection *os = input_section->output_section;
10706 /* A partial link, so keep the relocs */
10707 os->orelocation[os->reloc_count] = *parent;
10708 os->reloc_count++;
10711 if (r != bfd_reloc_ok)
10713 switch (r)
10715 case bfd_reloc_undefined:
10716 if (!((*link_info->callbacks->undefined_symbol)
10717 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
10718 input_bfd, input_section, (*parent)->address, TRUE)))
10719 goto error_return;
10720 break;
10721 case bfd_reloc_dangerous:
10722 BFD_ASSERT (error_message != NULL);
10723 if (!((*link_info->callbacks->reloc_dangerous)
10724 (link_info, error_message, input_bfd, input_section,
10725 (*parent)->address)))
10726 goto error_return;
10727 break;
10728 case bfd_reloc_overflow:
10729 if (!((*link_info->callbacks->reloc_overflow)
10730 (link_info, NULL,
10731 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
10732 (*parent)->howto->name, (*parent)->addend,
10733 input_bfd, input_section, (*parent)->address)))
10734 goto error_return;
10735 break;
10736 case bfd_reloc_outofrange:
10737 default:
10738 abort ();
10739 break;
10745 if (reloc_vector != NULL)
10746 free (reloc_vector);
10747 return data;
10749 error_return:
10750 if (reloc_vector != NULL)
10751 free (reloc_vector);
10752 return NULL;
10755 /* Create a MIPS ELF linker hash table. */
10757 struct bfd_link_hash_table *
10758 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
10760 struct mips_elf_link_hash_table *ret;
10761 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
10763 ret = bfd_malloc (amt);
10764 if (ret == NULL)
10765 return NULL;
10767 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
10768 mips_elf_link_hash_newfunc,
10769 sizeof (struct mips_elf_link_hash_entry)))
10771 free (ret);
10772 return NULL;
10775 #if 0
10776 /* We no longer use this. */
10777 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
10778 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
10779 #endif
10780 ret->procedure_count = 0;
10781 ret->compact_rel_size = 0;
10782 ret->use_rld_obj_head = FALSE;
10783 ret->rld_value = 0;
10784 ret->mips16_stubs_seen = FALSE;
10785 ret->computed_got_sizes = FALSE;
10786 ret->is_vxworks = FALSE;
10787 ret->small_data_overflow_reported = FALSE;
10788 ret->srelbss = NULL;
10789 ret->sdynbss = NULL;
10790 ret->srelplt = NULL;
10791 ret->srelplt2 = NULL;
10792 ret->sgotplt = NULL;
10793 ret->splt = NULL;
10794 ret->plt_header_size = 0;
10795 ret->plt_entry_size = 0;
10796 ret->function_stub_size = 0;
10798 return &ret->root.root;
10801 /* Likewise, but indicate that the target is VxWorks. */
10803 struct bfd_link_hash_table *
10804 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
10806 struct bfd_link_hash_table *ret;
10808 ret = _bfd_mips_elf_link_hash_table_create (abfd);
10809 if (ret)
10811 struct mips_elf_link_hash_table *htab;
10813 htab = (struct mips_elf_link_hash_table *) ret;
10814 htab->is_vxworks = 1;
10816 return ret;
10819 /* We need to use a special link routine to handle the .reginfo and
10820 the .mdebug sections. We need to merge all instances of these
10821 sections together, not write them all out sequentially. */
10823 bfd_boolean
10824 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10826 asection *o;
10827 struct bfd_link_order *p;
10828 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
10829 asection *rtproc_sec;
10830 Elf32_RegInfo reginfo;
10831 struct ecoff_debug_info debug;
10832 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10833 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
10834 HDRR *symhdr = &debug.symbolic_header;
10835 void *mdebug_handle = NULL;
10836 asection *s;
10837 EXTR esym;
10838 unsigned int i;
10839 bfd_size_type amt;
10840 struct mips_elf_link_hash_table *htab;
10842 static const char * const secname[] =
10844 ".text", ".init", ".fini", ".data",
10845 ".rodata", ".sdata", ".sbss", ".bss"
10847 static const int sc[] =
10849 scText, scInit, scFini, scData,
10850 scRData, scSData, scSBss, scBss
10853 /* We'd carefully arranged the dynamic symbol indices, and then the
10854 generic size_dynamic_sections renumbered them out from under us.
10855 Rather than trying somehow to prevent the renumbering, just do
10856 the sort again. */
10857 htab = mips_elf_hash_table (info);
10858 if (elf_hash_table (info)->dynamic_sections_created)
10860 bfd *dynobj;
10861 asection *got;
10862 struct mips_got_info *g;
10863 bfd_size_type dynsecsymcount;
10865 /* When we resort, we must tell mips_elf_sort_hash_table what
10866 the lowest index it may use is. That's the number of section
10867 symbols we're going to add. The generic ELF linker only
10868 adds these symbols when building a shared object. Note that
10869 we count the sections after (possibly) removing the .options
10870 section above. */
10872 dynsecsymcount = count_section_dynsyms (abfd, info);
10873 if (! mips_elf_sort_hash_table (info, dynsecsymcount + 1))
10874 return FALSE;
10876 /* Make sure we didn't grow the global .got region. */
10877 dynobj = elf_hash_table (info)->dynobj;
10878 got = mips_elf_got_section (dynobj, FALSE);
10879 g = mips_elf_section_data (got)->u.got_info;
10881 if (g->global_gotsym != NULL)
10882 BFD_ASSERT ((elf_hash_table (info)->dynsymcount
10883 - g->global_gotsym->dynindx)
10884 <= g->global_gotno);
10887 /* Get a value for the GP register. */
10888 if (elf_gp (abfd) == 0)
10890 struct bfd_link_hash_entry *h;
10892 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
10893 if (h != NULL && h->type == bfd_link_hash_defined)
10894 elf_gp (abfd) = (h->u.def.value
10895 + h->u.def.section->output_section->vma
10896 + h->u.def.section->output_offset);
10897 else if (htab->is_vxworks
10898 && (h = bfd_link_hash_lookup (info->hash,
10899 "_GLOBAL_OFFSET_TABLE_",
10900 FALSE, FALSE, TRUE))
10901 && h->type == bfd_link_hash_defined)
10902 elf_gp (abfd) = (h->u.def.section->output_section->vma
10903 + h->u.def.section->output_offset
10904 + h->u.def.value);
10905 else if (info->relocatable)
10907 bfd_vma lo = MINUS_ONE;
10909 /* Find the GP-relative section with the lowest offset. */
10910 for (o = abfd->sections; o != NULL; o = o->next)
10911 if (o->vma < lo
10912 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
10913 lo = o->vma;
10915 /* And calculate GP relative to that. */
10916 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
10918 else
10920 /* If the relocate_section function needs to do a reloc
10921 involving the GP value, it should make a reloc_dangerous
10922 callback to warn that GP is not defined. */
10926 /* Go through the sections and collect the .reginfo and .mdebug
10927 information. */
10928 reginfo_sec = NULL;
10929 mdebug_sec = NULL;
10930 gptab_data_sec = NULL;
10931 gptab_bss_sec = NULL;
10932 for (o = abfd->sections; o != NULL; o = o->next)
10934 if (strcmp (o->name, ".reginfo") == 0)
10936 memset (&reginfo, 0, sizeof reginfo);
10938 /* We have found the .reginfo section in the output file.
10939 Look through all the link_orders comprising it and merge
10940 the information together. */
10941 for (p = o->map_head.link_order; p != NULL; p = p->next)
10943 asection *input_section;
10944 bfd *input_bfd;
10945 Elf32_External_RegInfo ext;
10946 Elf32_RegInfo sub;
10948 if (p->type != bfd_indirect_link_order)
10950 if (p->type == bfd_data_link_order)
10951 continue;
10952 abort ();
10955 input_section = p->u.indirect.section;
10956 input_bfd = input_section->owner;
10958 if (! bfd_get_section_contents (input_bfd, input_section,
10959 &ext, 0, sizeof ext))
10960 return FALSE;
10962 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
10964 reginfo.ri_gprmask |= sub.ri_gprmask;
10965 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
10966 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
10967 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
10968 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
10970 /* ri_gp_value is set by the function
10971 mips_elf32_section_processing when the section is
10972 finally written out. */
10974 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10975 elf_link_input_bfd ignores this section. */
10976 input_section->flags &= ~SEC_HAS_CONTENTS;
10979 /* Size has been set in _bfd_mips_elf_always_size_sections. */
10980 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
10982 /* Skip this section later on (I don't think this currently
10983 matters, but someday it might). */
10984 o->map_head.link_order = NULL;
10986 reginfo_sec = o;
10989 if (strcmp (o->name, ".mdebug") == 0)
10991 struct extsym_info einfo;
10992 bfd_vma last;
10994 /* We have found the .mdebug section in the output file.
10995 Look through all the link_orders comprising it and merge
10996 the information together. */
10997 symhdr->magic = swap->sym_magic;
10998 /* FIXME: What should the version stamp be? */
10999 symhdr->vstamp = 0;
11000 symhdr->ilineMax = 0;
11001 symhdr->cbLine = 0;
11002 symhdr->idnMax = 0;
11003 symhdr->ipdMax = 0;
11004 symhdr->isymMax = 0;
11005 symhdr->ioptMax = 0;
11006 symhdr->iauxMax = 0;
11007 symhdr->issMax = 0;
11008 symhdr->issExtMax = 0;
11009 symhdr->ifdMax = 0;
11010 symhdr->crfd = 0;
11011 symhdr->iextMax = 0;
11013 /* We accumulate the debugging information itself in the
11014 debug_info structure. */
11015 debug.line = NULL;
11016 debug.external_dnr = NULL;
11017 debug.external_pdr = NULL;
11018 debug.external_sym = NULL;
11019 debug.external_opt = NULL;
11020 debug.external_aux = NULL;
11021 debug.ss = NULL;
11022 debug.ssext = debug.ssext_end = NULL;
11023 debug.external_fdr = NULL;
11024 debug.external_rfd = NULL;
11025 debug.external_ext = debug.external_ext_end = NULL;
11027 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
11028 if (mdebug_handle == NULL)
11029 return FALSE;
11031 esym.jmptbl = 0;
11032 esym.cobol_main = 0;
11033 esym.weakext = 0;
11034 esym.reserved = 0;
11035 esym.ifd = ifdNil;
11036 esym.asym.iss = issNil;
11037 esym.asym.st = stLocal;
11038 esym.asym.reserved = 0;
11039 esym.asym.index = indexNil;
11040 last = 0;
11041 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
11043 esym.asym.sc = sc[i];
11044 s = bfd_get_section_by_name (abfd, secname[i]);
11045 if (s != NULL)
11047 esym.asym.value = s->vma;
11048 last = s->vma + s->size;
11050 else
11051 esym.asym.value = last;
11052 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
11053 secname[i], &esym))
11054 return FALSE;
11057 for (p = o->map_head.link_order; p != NULL; p = p->next)
11059 asection *input_section;
11060 bfd *input_bfd;
11061 const struct ecoff_debug_swap *input_swap;
11062 struct ecoff_debug_info input_debug;
11063 char *eraw_src;
11064 char *eraw_end;
11066 if (p->type != bfd_indirect_link_order)
11068 if (p->type == bfd_data_link_order)
11069 continue;
11070 abort ();
11073 input_section = p->u.indirect.section;
11074 input_bfd = input_section->owner;
11076 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
11077 || (get_elf_backend_data (input_bfd)
11078 ->elf_backend_ecoff_debug_swap) == NULL)
11080 /* I don't know what a non MIPS ELF bfd would be
11081 doing with a .mdebug section, but I don't really
11082 want to deal with it. */
11083 continue;
11086 input_swap = (get_elf_backend_data (input_bfd)
11087 ->elf_backend_ecoff_debug_swap);
11089 BFD_ASSERT (p->size == input_section->size);
11091 /* The ECOFF linking code expects that we have already
11092 read in the debugging information and set up an
11093 ecoff_debug_info structure, so we do that now. */
11094 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
11095 &input_debug))
11096 return FALSE;
11098 if (! (bfd_ecoff_debug_accumulate
11099 (mdebug_handle, abfd, &debug, swap, input_bfd,
11100 &input_debug, input_swap, info)))
11101 return FALSE;
11103 /* Loop through the external symbols. For each one with
11104 interesting information, try to find the symbol in
11105 the linker global hash table and save the information
11106 for the output external symbols. */
11107 eraw_src = input_debug.external_ext;
11108 eraw_end = (eraw_src
11109 + (input_debug.symbolic_header.iextMax
11110 * input_swap->external_ext_size));
11111 for (;
11112 eraw_src < eraw_end;
11113 eraw_src += input_swap->external_ext_size)
11115 EXTR ext;
11116 const char *name;
11117 struct mips_elf_link_hash_entry *h;
11119 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
11120 if (ext.asym.sc == scNil
11121 || ext.asym.sc == scUndefined
11122 || ext.asym.sc == scSUndefined)
11123 continue;
11125 name = input_debug.ssext + ext.asym.iss;
11126 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
11127 name, FALSE, FALSE, TRUE);
11128 if (h == NULL || h->esym.ifd != -2)
11129 continue;
11131 if (ext.ifd != -1)
11133 BFD_ASSERT (ext.ifd
11134 < input_debug.symbolic_header.ifdMax);
11135 ext.ifd = input_debug.ifdmap[ext.ifd];
11138 h->esym = ext;
11141 /* Free up the information we just read. */
11142 free (input_debug.line);
11143 free (input_debug.external_dnr);
11144 free (input_debug.external_pdr);
11145 free (input_debug.external_sym);
11146 free (input_debug.external_opt);
11147 free (input_debug.external_aux);
11148 free (input_debug.ss);
11149 free (input_debug.ssext);
11150 free (input_debug.external_fdr);
11151 free (input_debug.external_rfd);
11152 free (input_debug.external_ext);
11154 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11155 elf_link_input_bfd ignores this section. */
11156 input_section->flags &= ~SEC_HAS_CONTENTS;
11159 if (SGI_COMPAT (abfd) && info->shared)
11161 /* Create .rtproc section. */
11162 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
11163 if (rtproc_sec == NULL)
11165 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
11166 | SEC_LINKER_CREATED | SEC_READONLY);
11168 rtproc_sec = bfd_make_section_with_flags (abfd,
11169 ".rtproc",
11170 flags);
11171 if (rtproc_sec == NULL
11172 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
11173 return FALSE;
11176 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
11177 info, rtproc_sec,
11178 &debug))
11179 return FALSE;
11182 /* Build the external symbol information. */
11183 einfo.abfd = abfd;
11184 einfo.info = info;
11185 einfo.debug = &debug;
11186 einfo.swap = swap;
11187 einfo.failed = FALSE;
11188 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
11189 mips_elf_output_extsym, &einfo);
11190 if (einfo.failed)
11191 return FALSE;
11193 /* Set the size of the .mdebug section. */
11194 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
11196 /* Skip this section later on (I don't think this currently
11197 matters, but someday it might). */
11198 o->map_head.link_order = NULL;
11200 mdebug_sec = o;
11203 if (CONST_STRNEQ (o->name, ".gptab."))
11205 const char *subname;
11206 unsigned int c;
11207 Elf32_gptab *tab;
11208 Elf32_External_gptab *ext_tab;
11209 unsigned int j;
11211 /* The .gptab.sdata and .gptab.sbss sections hold
11212 information describing how the small data area would
11213 change depending upon the -G switch. These sections
11214 not used in executables files. */
11215 if (! info->relocatable)
11217 for (p = o->map_head.link_order; p != NULL; p = p->next)
11219 asection *input_section;
11221 if (p->type != bfd_indirect_link_order)
11223 if (p->type == bfd_data_link_order)
11224 continue;
11225 abort ();
11228 input_section = p->u.indirect.section;
11230 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11231 elf_link_input_bfd ignores this section. */
11232 input_section->flags &= ~SEC_HAS_CONTENTS;
11235 /* Skip this section later on (I don't think this
11236 currently matters, but someday it might). */
11237 o->map_head.link_order = NULL;
11239 /* Really remove the section. */
11240 bfd_section_list_remove (abfd, o);
11241 --abfd->section_count;
11243 continue;
11246 /* There is one gptab for initialized data, and one for
11247 uninitialized data. */
11248 if (strcmp (o->name, ".gptab.sdata") == 0)
11249 gptab_data_sec = o;
11250 else if (strcmp (o->name, ".gptab.sbss") == 0)
11251 gptab_bss_sec = o;
11252 else
11254 (*_bfd_error_handler)
11255 (_("%s: illegal section name `%s'"),
11256 bfd_get_filename (abfd), o->name);
11257 bfd_set_error (bfd_error_nonrepresentable_section);
11258 return FALSE;
11261 /* The linker script always combines .gptab.data and
11262 .gptab.sdata into .gptab.sdata, and likewise for
11263 .gptab.bss and .gptab.sbss. It is possible that there is
11264 no .sdata or .sbss section in the output file, in which
11265 case we must change the name of the output section. */
11266 subname = o->name + sizeof ".gptab" - 1;
11267 if (bfd_get_section_by_name (abfd, subname) == NULL)
11269 if (o == gptab_data_sec)
11270 o->name = ".gptab.data";
11271 else
11272 o->name = ".gptab.bss";
11273 subname = o->name + sizeof ".gptab" - 1;
11274 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
11277 /* Set up the first entry. */
11278 c = 1;
11279 amt = c * sizeof (Elf32_gptab);
11280 tab = bfd_malloc (amt);
11281 if (tab == NULL)
11282 return FALSE;
11283 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
11284 tab[0].gt_header.gt_unused = 0;
11286 /* Combine the input sections. */
11287 for (p = o->map_head.link_order; p != NULL; p = p->next)
11289 asection *input_section;
11290 bfd *input_bfd;
11291 bfd_size_type size;
11292 unsigned long last;
11293 bfd_size_type gpentry;
11295 if (p->type != bfd_indirect_link_order)
11297 if (p->type == bfd_data_link_order)
11298 continue;
11299 abort ();
11302 input_section = p->u.indirect.section;
11303 input_bfd = input_section->owner;
11305 /* Combine the gptab entries for this input section one
11306 by one. We know that the input gptab entries are
11307 sorted by ascending -G value. */
11308 size = input_section->size;
11309 last = 0;
11310 for (gpentry = sizeof (Elf32_External_gptab);
11311 gpentry < size;
11312 gpentry += sizeof (Elf32_External_gptab))
11314 Elf32_External_gptab ext_gptab;
11315 Elf32_gptab int_gptab;
11316 unsigned long val;
11317 unsigned long add;
11318 bfd_boolean exact;
11319 unsigned int look;
11321 if (! (bfd_get_section_contents
11322 (input_bfd, input_section, &ext_gptab, gpentry,
11323 sizeof (Elf32_External_gptab))))
11325 free (tab);
11326 return FALSE;
11329 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
11330 &int_gptab);
11331 val = int_gptab.gt_entry.gt_g_value;
11332 add = int_gptab.gt_entry.gt_bytes - last;
11334 exact = FALSE;
11335 for (look = 1; look < c; look++)
11337 if (tab[look].gt_entry.gt_g_value >= val)
11338 tab[look].gt_entry.gt_bytes += add;
11340 if (tab[look].gt_entry.gt_g_value == val)
11341 exact = TRUE;
11344 if (! exact)
11346 Elf32_gptab *new_tab;
11347 unsigned int max;
11349 /* We need a new table entry. */
11350 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
11351 new_tab = bfd_realloc (tab, amt);
11352 if (new_tab == NULL)
11354 free (tab);
11355 return FALSE;
11357 tab = new_tab;
11358 tab[c].gt_entry.gt_g_value = val;
11359 tab[c].gt_entry.gt_bytes = add;
11361 /* Merge in the size for the next smallest -G
11362 value, since that will be implied by this new
11363 value. */
11364 max = 0;
11365 for (look = 1; look < c; look++)
11367 if (tab[look].gt_entry.gt_g_value < val
11368 && (max == 0
11369 || (tab[look].gt_entry.gt_g_value
11370 > tab[max].gt_entry.gt_g_value)))
11371 max = look;
11373 if (max != 0)
11374 tab[c].gt_entry.gt_bytes +=
11375 tab[max].gt_entry.gt_bytes;
11377 ++c;
11380 last = int_gptab.gt_entry.gt_bytes;
11383 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11384 elf_link_input_bfd ignores this section. */
11385 input_section->flags &= ~SEC_HAS_CONTENTS;
11388 /* The table must be sorted by -G value. */
11389 if (c > 2)
11390 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
11392 /* Swap out the table. */
11393 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
11394 ext_tab = bfd_alloc (abfd, amt);
11395 if (ext_tab == NULL)
11397 free (tab);
11398 return FALSE;
11401 for (j = 0; j < c; j++)
11402 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
11403 free (tab);
11405 o->size = c * sizeof (Elf32_External_gptab);
11406 o->contents = (bfd_byte *) ext_tab;
11408 /* Skip this section later on (I don't think this currently
11409 matters, but someday it might). */
11410 o->map_head.link_order = NULL;
11414 /* Invoke the regular ELF backend linker to do all the work. */
11415 if (!bfd_elf_final_link (abfd, info))
11416 return FALSE;
11418 /* Now write out the computed sections. */
11420 if (reginfo_sec != NULL)
11422 Elf32_External_RegInfo ext;
11424 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
11425 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
11426 return FALSE;
11429 if (mdebug_sec != NULL)
11431 BFD_ASSERT (abfd->output_has_begun);
11432 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
11433 swap, info,
11434 mdebug_sec->filepos))
11435 return FALSE;
11437 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
11440 if (gptab_data_sec != NULL)
11442 if (! bfd_set_section_contents (abfd, gptab_data_sec,
11443 gptab_data_sec->contents,
11444 0, gptab_data_sec->size))
11445 return FALSE;
11448 if (gptab_bss_sec != NULL)
11450 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
11451 gptab_bss_sec->contents,
11452 0, gptab_bss_sec->size))
11453 return FALSE;
11456 if (SGI_COMPAT (abfd))
11458 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
11459 if (rtproc_sec != NULL)
11461 if (! bfd_set_section_contents (abfd, rtproc_sec,
11462 rtproc_sec->contents,
11463 0, rtproc_sec->size))
11464 return FALSE;
11468 return TRUE;
11471 /* Structure for saying that BFD machine EXTENSION extends BASE. */
11473 struct mips_mach_extension {
11474 unsigned long extension, base;
11478 /* An array describing how BFD machines relate to one another. The entries
11479 are ordered topologically with MIPS I extensions listed last. */
11481 static const struct mips_mach_extension mips_mach_extensions[] = {
11482 /* MIPS64r2 extensions. */
11483 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
11485 /* MIPS64 extensions. */
11486 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
11487 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
11489 /* MIPS V extensions. */
11490 { bfd_mach_mipsisa64, bfd_mach_mips5 },
11492 /* R10000 extensions. */
11493 { bfd_mach_mips12000, bfd_mach_mips10000 },
11495 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
11496 vr5400 ISA, but doesn't include the multimedia stuff. It seems
11497 better to allow vr5400 and vr5500 code to be merged anyway, since
11498 many libraries will just use the core ISA. Perhaps we could add
11499 some sort of ASE flag if this ever proves a problem. */
11500 { bfd_mach_mips5500, bfd_mach_mips5400 },
11501 { bfd_mach_mips5400, bfd_mach_mips5000 },
11503 /* MIPS IV extensions. */
11504 { bfd_mach_mips5, bfd_mach_mips8000 },
11505 { bfd_mach_mips10000, bfd_mach_mips8000 },
11506 { bfd_mach_mips5000, bfd_mach_mips8000 },
11507 { bfd_mach_mips7000, bfd_mach_mips8000 },
11508 { bfd_mach_mips9000, bfd_mach_mips8000 },
11510 /* VR4100 extensions. */
11511 { bfd_mach_mips4120, bfd_mach_mips4100 },
11512 { bfd_mach_mips4111, bfd_mach_mips4100 },
11514 /* MIPS III extensions. */
11515 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
11516 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
11517 { bfd_mach_mips8000, bfd_mach_mips4000 },
11518 { bfd_mach_mips4650, bfd_mach_mips4000 },
11519 { bfd_mach_mips4600, bfd_mach_mips4000 },
11520 { bfd_mach_mips4400, bfd_mach_mips4000 },
11521 { bfd_mach_mips4300, bfd_mach_mips4000 },
11522 { bfd_mach_mips4100, bfd_mach_mips4000 },
11523 { bfd_mach_mips4010, bfd_mach_mips4000 },
11525 /* MIPS32 extensions. */
11526 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
11528 /* MIPS II extensions. */
11529 { bfd_mach_mips4000, bfd_mach_mips6000 },
11530 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
11532 /* MIPS I extensions. */
11533 { bfd_mach_mips6000, bfd_mach_mips3000 },
11534 { bfd_mach_mips3900, bfd_mach_mips3000 }
11538 /* Return true if bfd machine EXTENSION is an extension of machine BASE. */
11540 static bfd_boolean
11541 mips_mach_extends_p (unsigned long base, unsigned long extension)
11543 size_t i;
11545 if (extension == base)
11546 return TRUE;
11548 if (base == bfd_mach_mipsisa32
11549 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
11550 return TRUE;
11552 if (base == bfd_mach_mipsisa32r2
11553 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
11554 return TRUE;
11556 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
11557 if (extension == mips_mach_extensions[i].extension)
11559 extension = mips_mach_extensions[i].base;
11560 if (extension == base)
11561 return TRUE;
11564 return FALSE;
11568 /* Return true if the given ELF header flags describe a 32-bit binary. */
11570 static bfd_boolean
11571 mips_32bit_flags_p (flagword flags)
11573 return ((flags & EF_MIPS_32BITMODE) != 0
11574 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
11575 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
11576 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
11577 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
11578 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
11579 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
11583 /* Merge object attributes from IBFD into OBFD. Raise an error if
11584 there are conflicting attributes. */
11585 static bfd_boolean
11586 mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
11588 obj_attribute *in_attr;
11589 obj_attribute *out_attr;
11591 if (!elf_known_obj_attributes_proc (obfd)[0].i)
11593 /* This is the first object. Copy the attributes. */
11594 _bfd_elf_copy_obj_attributes (ibfd, obfd);
11596 /* Use the Tag_null value to indicate the attributes have been
11597 initialized. */
11598 elf_known_obj_attributes_proc (obfd)[0].i = 1;
11600 return TRUE;
11603 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
11604 non-conflicting ones. */
11605 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
11606 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
11607 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
11609 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
11610 if (out_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
11611 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
11612 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
11614 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
11615 _bfd_error_handler
11616 (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
11617 in_attr[Tag_GNU_MIPS_ABI_FP].i);
11618 else if (out_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
11619 _bfd_error_handler
11620 (_("Warning: %B uses unknown floating point ABI %d"), obfd,
11621 out_attr[Tag_GNU_MIPS_ABI_FP].i);
11622 else
11623 switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
11625 case 1:
11626 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
11628 case 2:
11629 _bfd_error_handler
11630 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
11631 obfd, ibfd);
11632 break;
11634 case 3:
11635 _bfd_error_handler
11636 (_("Warning: %B uses hard float, %B uses soft float"),
11637 obfd, ibfd);
11638 break;
11640 case 4:
11641 _bfd_error_handler
11642 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
11643 obfd, ibfd);
11644 break;
11646 default:
11647 abort ();
11649 break;
11651 case 2:
11652 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
11654 case 1:
11655 _bfd_error_handler
11656 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
11657 ibfd, obfd);
11658 break;
11660 case 3:
11661 _bfd_error_handler
11662 (_("Warning: %B uses hard float, %B uses soft float"),
11663 obfd, ibfd);
11664 break;
11666 case 4:
11667 _bfd_error_handler
11668 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
11669 obfd, ibfd);
11670 break;
11672 default:
11673 abort ();
11675 break;
11677 case 3:
11678 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
11680 case 1:
11681 case 2:
11682 case 4:
11683 _bfd_error_handler
11684 (_("Warning: %B uses hard float, %B uses soft float"),
11685 ibfd, obfd);
11686 break;
11688 default:
11689 abort ();
11691 break;
11693 case 4:
11694 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
11696 case 1:
11697 _bfd_error_handler
11698 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
11699 ibfd, obfd);
11700 break;
11702 case 2:
11703 _bfd_error_handler
11704 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
11705 ibfd, obfd);
11706 break;
11708 case 3:
11709 _bfd_error_handler
11710 (_("Warning: %B uses hard float, %B uses soft float"),
11711 obfd, ibfd);
11712 break;
11714 default:
11715 abort ();
11717 break;
11719 default:
11720 abort ();
11724 /* Merge Tag_compatibility attributes and any common GNU ones. */
11725 _bfd_elf_merge_object_attributes (ibfd, obfd);
11727 return TRUE;
11730 /* Merge backend specific data from an object file to the output
11731 object file when linking. */
11733 bfd_boolean
11734 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
11736 flagword old_flags;
11737 flagword new_flags;
11738 bfd_boolean ok;
11739 bfd_boolean null_input_bfd = TRUE;
11740 asection *sec;
11742 /* Check if we have the same endianess */
11743 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
11745 (*_bfd_error_handler)
11746 (_("%B: endianness incompatible with that of the selected emulation"),
11747 ibfd);
11748 return FALSE;
11751 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
11752 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
11753 return TRUE;
11755 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
11757 (*_bfd_error_handler)
11758 (_("%B: ABI is incompatible with that of the selected emulation"),
11759 ibfd);
11760 return FALSE;
11763 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
11764 return FALSE;
11766 new_flags = elf_elfheader (ibfd)->e_flags;
11767 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
11768 old_flags = elf_elfheader (obfd)->e_flags;
11770 if (! elf_flags_init (obfd))
11772 elf_flags_init (obfd) = TRUE;
11773 elf_elfheader (obfd)->e_flags = new_flags;
11774 elf_elfheader (obfd)->e_ident[EI_CLASS]
11775 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
11777 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
11778 && (bfd_get_arch_info (obfd)->the_default
11779 || mips_mach_extends_p (bfd_get_mach (obfd),
11780 bfd_get_mach (ibfd))))
11782 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
11783 bfd_get_mach (ibfd)))
11784 return FALSE;
11787 return TRUE;
11790 /* Check flag compatibility. */
11792 new_flags &= ~EF_MIPS_NOREORDER;
11793 old_flags &= ~EF_MIPS_NOREORDER;
11795 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
11796 doesn't seem to matter. */
11797 new_flags &= ~EF_MIPS_XGOT;
11798 old_flags &= ~EF_MIPS_XGOT;
11800 /* MIPSpro generates ucode info in n64 objects. Again, we should
11801 just be able to ignore this. */
11802 new_flags &= ~EF_MIPS_UCODE;
11803 old_flags &= ~EF_MIPS_UCODE;
11805 /* Don't care about the PIC flags from dynamic objects; they are
11806 PIC by design. */
11807 if ((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0
11808 && (ibfd->flags & DYNAMIC) != 0)
11809 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
11811 if (new_flags == old_flags)
11812 return TRUE;
11814 /* Check to see if the input BFD actually contains any sections.
11815 If not, its flags may not have been initialised either, but it cannot
11816 actually cause any incompatibility. */
11817 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
11819 /* Ignore synthetic sections and empty .text, .data and .bss sections
11820 which are automatically generated by gas. */
11821 if (strcmp (sec->name, ".reginfo")
11822 && strcmp (sec->name, ".mdebug")
11823 && (sec->size != 0
11824 || (strcmp (sec->name, ".text")
11825 && strcmp (sec->name, ".data")
11826 && strcmp (sec->name, ".bss"))))
11828 null_input_bfd = FALSE;
11829 break;
11832 if (null_input_bfd)
11833 return TRUE;
11835 ok = TRUE;
11837 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
11838 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
11840 (*_bfd_error_handler)
11841 (_("%B: warning: linking PIC files with non-PIC files"),
11842 ibfd);
11843 ok = TRUE;
11846 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
11847 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
11848 if (! (new_flags & EF_MIPS_PIC))
11849 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
11851 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
11852 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
11854 /* Compare the ISAs. */
11855 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
11857 (*_bfd_error_handler)
11858 (_("%B: linking 32-bit code with 64-bit code"),
11859 ibfd);
11860 ok = FALSE;
11862 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
11864 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
11865 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
11867 /* Copy the architecture info from IBFD to OBFD. Also copy
11868 the 32-bit flag (if set) so that we continue to recognise
11869 OBFD as a 32-bit binary. */
11870 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
11871 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
11872 elf_elfheader (obfd)->e_flags
11873 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
11875 /* Copy across the ABI flags if OBFD doesn't use them
11876 and if that was what caused us to treat IBFD as 32-bit. */
11877 if ((old_flags & EF_MIPS_ABI) == 0
11878 && mips_32bit_flags_p (new_flags)
11879 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
11880 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
11882 else
11884 /* The ISAs aren't compatible. */
11885 (*_bfd_error_handler)
11886 (_("%B: linking %s module with previous %s modules"),
11887 ibfd,
11888 bfd_printable_name (ibfd),
11889 bfd_printable_name (obfd));
11890 ok = FALSE;
11894 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
11895 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
11897 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
11898 does set EI_CLASS differently from any 32-bit ABI. */
11899 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
11900 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
11901 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
11903 /* Only error if both are set (to different values). */
11904 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
11905 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
11906 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
11908 (*_bfd_error_handler)
11909 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
11910 ibfd,
11911 elf_mips_abi_name (ibfd),
11912 elf_mips_abi_name (obfd));
11913 ok = FALSE;
11915 new_flags &= ~EF_MIPS_ABI;
11916 old_flags &= ~EF_MIPS_ABI;
11919 /* For now, allow arbitrary mixing of ASEs (retain the union). */
11920 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
11922 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
11924 new_flags &= ~ EF_MIPS_ARCH_ASE;
11925 old_flags &= ~ EF_MIPS_ARCH_ASE;
11928 /* Warn about any other mismatches */
11929 if (new_flags != old_flags)
11931 (*_bfd_error_handler)
11932 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
11933 ibfd, (unsigned long) new_flags,
11934 (unsigned long) old_flags);
11935 ok = FALSE;
11938 if (! ok)
11940 bfd_set_error (bfd_error_bad_value);
11941 return FALSE;
11944 return TRUE;
11947 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
11949 bfd_boolean
11950 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
11952 BFD_ASSERT (!elf_flags_init (abfd)
11953 || elf_elfheader (abfd)->e_flags == flags);
11955 elf_elfheader (abfd)->e_flags = flags;
11956 elf_flags_init (abfd) = TRUE;
11957 return TRUE;
11960 char *
11961 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
11963 switch (dtag)
11965 default: return "";
11966 case DT_MIPS_RLD_VERSION:
11967 return "MIPS_RLD_VERSION";
11968 case DT_MIPS_TIME_STAMP:
11969 return "MIPS_TIME_STAMP";
11970 case DT_MIPS_ICHECKSUM:
11971 return "MIPS_ICHECKSUM";
11972 case DT_MIPS_IVERSION:
11973 return "MIPS_IVERSION";
11974 case DT_MIPS_FLAGS:
11975 return "MIPS_FLAGS";
11976 case DT_MIPS_BASE_ADDRESS:
11977 return "MIPS_BASE_ADDRESS";
11978 case DT_MIPS_MSYM:
11979 return "MIPS_MSYM";
11980 case DT_MIPS_CONFLICT:
11981 return "MIPS_CONFLICT";
11982 case DT_MIPS_LIBLIST:
11983 return "MIPS_LIBLIST";
11984 case DT_MIPS_LOCAL_GOTNO:
11985 return "MIPS_LOCAL_GOTNO";
11986 case DT_MIPS_CONFLICTNO:
11987 return "MIPS_CONFLICTNO";
11988 case DT_MIPS_LIBLISTNO:
11989 return "MIPS_LIBLISTNO";
11990 case DT_MIPS_SYMTABNO:
11991 return "MIPS_SYMTABNO";
11992 case DT_MIPS_UNREFEXTNO:
11993 return "MIPS_UNREFEXTNO";
11994 case DT_MIPS_GOTSYM:
11995 return "MIPS_GOTSYM";
11996 case DT_MIPS_HIPAGENO:
11997 return "MIPS_HIPAGENO";
11998 case DT_MIPS_RLD_MAP:
11999 return "MIPS_RLD_MAP";
12000 case DT_MIPS_DELTA_CLASS:
12001 return "MIPS_DELTA_CLASS";
12002 case DT_MIPS_DELTA_CLASS_NO:
12003 return "MIPS_DELTA_CLASS_NO";
12004 case DT_MIPS_DELTA_INSTANCE:
12005 return "MIPS_DELTA_INSTANCE";
12006 case DT_MIPS_DELTA_INSTANCE_NO:
12007 return "MIPS_DELTA_INSTANCE_NO";
12008 case DT_MIPS_DELTA_RELOC:
12009 return "MIPS_DELTA_RELOC";
12010 case DT_MIPS_DELTA_RELOC_NO:
12011 return "MIPS_DELTA_RELOC_NO";
12012 case DT_MIPS_DELTA_SYM:
12013 return "MIPS_DELTA_SYM";
12014 case DT_MIPS_DELTA_SYM_NO:
12015 return "MIPS_DELTA_SYM_NO";
12016 case DT_MIPS_DELTA_CLASSSYM:
12017 return "MIPS_DELTA_CLASSSYM";
12018 case DT_MIPS_DELTA_CLASSSYM_NO:
12019 return "MIPS_DELTA_CLASSSYM_NO";
12020 case DT_MIPS_CXX_FLAGS:
12021 return "MIPS_CXX_FLAGS";
12022 case DT_MIPS_PIXIE_INIT:
12023 return "MIPS_PIXIE_INIT";
12024 case DT_MIPS_SYMBOL_LIB:
12025 return "MIPS_SYMBOL_LIB";
12026 case DT_MIPS_LOCALPAGE_GOTIDX:
12027 return "MIPS_LOCALPAGE_GOTIDX";
12028 case DT_MIPS_LOCAL_GOTIDX:
12029 return "MIPS_LOCAL_GOTIDX";
12030 case DT_MIPS_HIDDEN_GOTIDX:
12031 return "MIPS_HIDDEN_GOTIDX";
12032 case DT_MIPS_PROTECTED_GOTIDX:
12033 return "MIPS_PROTECTED_GOT_IDX";
12034 case DT_MIPS_OPTIONS:
12035 return "MIPS_OPTIONS";
12036 case DT_MIPS_INTERFACE:
12037 return "MIPS_INTERFACE";
12038 case DT_MIPS_DYNSTR_ALIGN:
12039 return "DT_MIPS_DYNSTR_ALIGN";
12040 case DT_MIPS_INTERFACE_SIZE:
12041 return "DT_MIPS_INTERFACE_SIZE";
12042 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
12043 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
12044 case DT_MIPS_PERF_SUFFIX:
12045 return "DT_MIPS_PERF_SUFFIX";
12046 case DT_MIPS_COMPACT_SIZE:
12047 return "DT_MIPS_COMPACT_SIZE";
12048 case DT_MIPS_GP_VALUE:
12049 return "DT_MIPS_GP_VALUE";
12050 case DT_MIPS_AUX_DYNAMIC:
12051 return "DT_MIPS_AUX_DYNAMIC";
12055 bfd_boolean
12056 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
12058 FILE *file = ptr;
12060 BFD_ASSERT (abfd != NULL && ptr != NULL);
12062 /* Print normal ELF private data. */
12063 _bfd_elf_print_private_bfd_data (abfd, ptr);
12065 /* xgettext:c-format */
12066 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
12068 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
12069 fprintf (file, _(" [abi=O32]"));
12070 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
12071 fprintf (file, _(" [abi=O64]"));
12072 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
12073 fprintf (file, _(" [abi=EABI32]"));
12074 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
12075 fprintf (file, _(" [abi=EABI64]"));
12076 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
12077 fprintf (file, _(" [abi unknown]"));
12078 else if (ABI_N32_P (abfd))
12079 fprintf (file, _(" [abi=N32]"));
12080 else if (ABI_64_P (abfd))
12081 fprintf (file, _(" [abi=64]"));
12082 else
12083 fprintf (file, _(" [no abi set]"));
12085 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
12086 fprintf (file, " [mips1]");
12087 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
12088 fprintf (file, " [mips2]");
12089 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
12090 fprintf (file, " [mips3]");
12091 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
12092 fprintf (file, " [mips4]");
12093 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
12094 fprintf (file, " [mips5]");
12095 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
12096 fprintf (file, " [mips32]");
12097 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
12098 fprintf (file, " [mips64]");
12099 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
12100 fprintf (file, " [mips32r2]");
12101 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
12102 fprintf (file, " [mips64r2]");
12103 else
12104 fprintf (file, _(" [unknown ISA]"));
12106 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
12107 fprintf (file, " [mdmx]");
12109 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
12110 fprintf (file, " [mips16]");
12112 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
12113 fprintf (file, " [32bitmode]");
12114 else
12115 fprintf (file, _(" [not 32bitmode]"));
12117 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
12118 fprintf (file, " [noreorder]");
12120 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
12121 fprintf (file, " [PIC]");
12123 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
12124 fprintf (file, " [CPIC]");
12126 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
12127 fprintf (file, " [XGOT]");
12129 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
12130 fprintf (file, " [UCODE]");
12132 fputc ('\n', file);
12134 return TRUE;
12137 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
12139 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12140 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12141 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
12142 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12143 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12144 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
12145 { NULL, 0, 0, 0, 0 }
12148 /* Merge non visibility st_other attributes. Ensure that the
12149 STO_OPTIONAL flag is copied into h->other, even if this is not a
12150 definiton of the symbol. */
12151 void
12152 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
12153 const Elf_Internal_Sym *isym,
12154 bfd_boolean definition,
12155 bfd_boolean dynamic ATTRIBUTE_UNUSED)
12157 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
12159 unsigned char other;
12161 other = (definition ? isym->st_other : h->other);
12162 other &= ~ELF_ST_VISIBILITY (-1);
12163 h->other = other | ELF_ST_VISIBILITY (h->other);
12166 if (!definition
12167 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
12168 h->other |= STO_OPTIONAL;
12171 /* Decide whether an undefined symbol is special and can be ignored.
12172 This is the case for OPTIONAL symbols on IRIX. */
12173 bfd_boolean
12174 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
12176 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
12179 bfd_boolean
12180 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
12182 return (sym->st_shndx == SHN_COMMON
12183 || sym->st_shndx == SHN_MIPS_ACOMMON
12184 || sym->st_shndx == SHN_MIPS_SCOMMON);