bfd/
[binutils.git] / bfd / elfxx-mips.c
blobc71e3978c9089d428a66cc214098d6414490f2e3
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 asection *sstubs;
367 /* The size of the PLT header in bytes (VxWorks only). */
368 bfd_vma plt_header_size;
369 /* The size of a PLT entry in bytes (VxWorks only). */
370 bfd_vma plt_entry_size;
371 /* The size of a function stub entry in bytes. */
372 bfd_vma function_stub_size;
375 #define TLS_RELOC_P(r_type) \
376 (r_type == R_MIPS_TLS_DTPMOD32 \
377 || r_type == R_MIPS_TLS_DTPMOD64 \
378 || r_type == R_MIPS_TLS_DTPREL32 \
379 || r_type == R_MIPS_TLS_DTPREL64 \
380 || r_type == R_MIPS_TLS_GD \
381 || r_type == R_MIPS_TLS_LDM \
382 || r_type == R_MIPS_TLS_DTPREL_HI16 \
383 || r_type == R_MIPS_TLS_DTPREL_LO16 \
384 || r_type == R_MIPS_TLS_GOTTPREL \
385 || r_type == R_MIPS_TLS_TPREL32 \
386 || r_type == R_MIPS_TLS_TPREL64 \
387 || r_type == R_MIPS_TLS_TPREL_HI16 \
388 || r_type == R_MIPS_TLS_TPREL_LO16)
390 /* Structure used to pass information to mips_elf_output_extsym. */
392 struct extsym_info
394 bfd *abfd;
395 struct bfd_link_info *info;
396 struct ecoff_debug_info *debug;
397 const struct ecoff_debug_swap *swap;
398 bfd_boolean failed;
401 /* The names of the runtime procedure table symbols used on IRIX5. */
403 static const char * const mips_elf_dynsym_rtproc_names[] =
405 "_procedure_table",
406 "_procedure_string_table",
407 "_procedure_table_size",
408 NULL
411 /* These structures are used to generate the .compact_rel section on
412 IRIX5. */
414 typedef struct
416 unsigned long id1; /* Always one? */
417 unsigned long num; /* Number of compact relocation entries. */
418 unsigned long id2; /* Always two? */
419 unsigned long offset; /* The file offset of the first relocation. */
420 unsigned long reserved0; /* Zero? */
421 unsigned long reserved1; /* Zero? */
422 } Elf32_compact_rel;
424 typedef struct
426 bfd_byte id1[4];
427 bfd_byte num[4];
428 bfd_byte id2[4];
429 bfd_byte offset[4];
430 bfd_byte reserved0[4];
431 bfd_byte reserved1[4];
432 } Elf32_External_compact_rel;
434 typedef struct
436 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
437 unsigned int rtype : 4; /* Relocation types. See below. */
438 unsigned int dist2to : 8;
439 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
440 unsigned long konst; /* KONST field. See below. */
441 unsigned long vaddr; /* VADDR to be relocated. */
442 } Elf32_crinfo;
444 typedef struct
446 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
447 unsigned int rtype : 4; /* Relocation types. See below. */
448 unsigned int dist2to : 8;
449 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
450 unsigned long konst; /* KONST field. See below. */
451 } Elf32_crinfo2;
453 typedef struct
455 bfd_byte info[4];
456 bfd_byte konst[4];
457 bfd_byte vaddr[4];
458 } Elf32_External_crinfo;
460 typedef struct
462 bfd_byte info[4];
463 bfd_byte konst[4];
464 } Elf32_External_crinfo2;
466 /* These are the constants used to swap the bitfields in a crinfo. */
468 #define CRINFO_CTYPE (0x1)
469 #define CRINFO_CTYPE_SH (31)
470 #define CRINFO_RTYPE (0xf)
471 #define CRINFO_RTYPE_SH (27)
472 #define CRINFO_DIST2TO (0xff)
473 #define CRINFO_DIST2TO_SH (19)
474 #define CRINFO_RELVADDR (0x7ffff)
475 #define CRINFO_RELVADDR_SH (0)
477 /* A compact relocation info has long (3 words) or short (2 words)
478 formats. A short format doesn't have VADDR field and relvaddr
479 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
480 #define CRF_MIPS_LONG 1
481 #define CRF_MIPS_SHORT 0
483 /* There are 4 types of compact relocation at least. The value KONST
484 has different meaning for each type:
486 (type) (konst)
487 CT_MIPS_REL32 Address in data
488 CT_MIPS_WORD Address in word (XXX)
489 CT_MIPS_GPHI_LO GP - vaddr
490 CT_MIPS_JMPAD Address to jump
493 #define CRT_MIPS_REL32 0xa
494 #define CRT_MIPS_WORD 0xb
495 #define CRT_MIPS_GPHI_LO 0xc
496 #define CRT_MIPS_JMPAD 0xd
498 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
499 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
500 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
501 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
503 /* The structure of the runtime procedure descriptor created by the
504 loader for use by the static exception system. */
506 typedef struct runtime_pdr {
507 bfd_vma adr; /* Memory address of start of procedure. */
508 long regmask; /* Save register mask. */
509 long regoffset; /* Save register offset. */
510 long fregmask; /* Save floating point register mask. */
511 long fregoffset; /* Save floating point register offset. */
512 long frameoffset; /* Frame size. */
513 short framereg; /* Frame pointer register. */
514 short pcreg; /* Offset or reg of return pc. */
515 long irpss; /* Index into the runtime string table. */
516 long reserved;
517 struct exception_info *exception_info;/* Pointer to exception array. */
518 } RPDR, *pRPDR;
519 #define cbRPDR sizeof (RPDR)
520 #define rpdNil ((pRPDR) 0)
522 static struct mips_got_entry *mips_elf_create_local_got_entry
523 (bfd *, struct bfd_link_info *, bfd *, struct mips_got_info *, asection *,
524 bfd_vma, unsigned long, struct mips_elf_link_hash_entry *, int);
525 static bfd_boolean mips_elf_sort_hash_table_f
526 (struct mips_elf_link_hash_entry *, void *);
527 static bfd_vma mips_elf_high
528 (bfd_vma);
529 static bfd_boolean mips_elf_create_dynamic_relocation
530 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
531 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
532 bfd_vma *, asection *);
533 static hashval_t mips_elf_got_entry_hash
534 (const void *);
535 static bfd_vma mips_elf_adjust_gp
536 (bfd *, struct mips_got_info *, bfd *);
537 static struct mips_got_info *mips_elf_got_for_ibfd
538 (struct mips_got_info *, bfd *);
540 /* This will be used when we sort the dynamic relocation records. */
541 static bfd *reldyn_sorting_bfd;
543 /* Nonzero if ABFD is using the N32 ABI. */
544 #define ABI_N32_P(abfd) \
545 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
547 /* Nonzero if ABFD is using the N64 ABI. */
548 #define ABI_64_P(abfd) \
549 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
551 /* Nonzero if ABFD is using NewABI conventions. */
552 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
554 /* The IRIX compatibility level we are striving for. */
555 #define IRIX_COMPAT(abfd) \
556 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
558 /* Whether we are trying to be compatible with IRIX at all. */
559 #define SGI_COMPAT(abfd) \
560 (IRIX_COMPAT (abfd) != ict_none)
562 /* The name of the options section. */
563 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
564 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
566 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
567 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
568 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
569 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
571 /* Whether the section is readonly. */
572 #define MIPS_ELF_READONLY_SECTION(sec) \
573 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
574 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
576 /* The name of the stub section. */
577 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
579 /* The size of an external REL relocation. */
580 #define MIPS_ELF_REL_SIZE(abfd) \
581 (get_elf_backend_data (abfd)->s->sizeof_rel)
583 /* The size of an external RELA relocation. */
584 #define MIPS_ELF_RELA_SIZE(abfd) \
585 (get_elf_backend_data (abfd)->s->sizeof_rela)
587 /* The size of an external dynamic table entry. */
588 #define MIPS_ELF_DYN_SIZE(abfd) \
589 (get_elf_backend_data (abfd)->s->sizeof_dyn)
591 /* The size of a GOT entry. */
592 #define MIPS_ELF_GOT_SIZE(abfd) \
593 (get_elf_backend_data (abfd)->s->arch_size / 8)
595 /* The size of a symbol-table entry. */
596 #define MIPS_ELF_SYM_SIZE(abfd) \
597 (get_elf_backend_data (abfd)->s->sizeof_sym)
599 /* The default alignment for sections, as a power of two. */
600 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
601 (get_elf_backend_data (abfd)->s->log_file_align)
603 /* Get word-sized data. */
604 #define MIPS_ELF_GET_WORD(abfd, ptr) \
605 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
607 /* Put out word-sized data. */
608 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
609 (ABI_64_P (abfd) \
610 ? bfd_put_64 (abfd, val, ptr) \
611 : bfd_put_32 (abfd, val, ptr))
613 /* Add a dynamic symbol table-entry. */
614 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
615 _bfd_elf_add_dynamic_entry (info, tag, val)
617 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
618 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
620 /* Determine whether the internal relocation of index REL_IDX is REL
621 (zero) or RELA (non-zero). The assumption is that, if there are
622 two relocation sections for this section, one of them is REL and
623 the other is RELA. If the index of the relocation we're testing is
624 in range for the first relocation section, check that the external
625 relocation size is that for RELA. It is also assumed that, if
626 rel_idx is not in range for the first section, and this first
627 section contains REL relocs, then the relocation is in the second
628 section, that is RELA. */
629 #define MIPS_RELOC_RELA_P(abfd, sec, rel_idx) \
630 ((NUM_SHDR_ENTRIES (&elf_section_data (sec)->rel_hdr) \
631 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel \
632 > (bfd_vma)(rel_idx)) \
633 == (elf_section_data (sec)->rel_hdr.sh_entsize \
634 == (ABI_64_P (abfd) ? sizeof (Elf64_External_Rela) \
635 : sizeof (Elf32_External_Rela))))
637 /* The name of the dynamic relocation section. */
638 #define MIPS_ELF_REL_DYN_NAME(INFO) \
639 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
641 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
642 from smaller values. Start with zero, widen, *then* decrement. */
643 #define MINUS_ONE (((bfd_vma)0) - 1)
644 #define MINUS_TWO (((bfd_vma)0) - 2)
646 /* The number of local .got entries we reserve. */
647 #define MIPS_RESERVED_GOTNO(INFO) \
648 (mips_elf_hash_table (INFO)->is_vxworks ? 3 : 2)
650 /* The value to write into got[1] for SVR4 targets, to identify it is
651 a GNU object. The dynamic linker can then use got[1] to store the
652 module pointer. */
653 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
654 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
656 /* The offset of $gp from the beginning of the .got section. */
657 #define ELF_MIPS_GP_OFFSET(INFO) \
658 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
660 /* The maximum size of the GOT for it to be addressable using 16-bit
661 offsets from $gp. */
662 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
664 /* Instructions which appear in a stub. */
665 #define STUB_LW(abfd) \
666 ((ABI_64_P (abfd) \
667 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
668 : 0x8f998010)) /* lw t9,0x8010(gp) */
669 #define STUB_MOVE(abfd) \
670 ((ABI_64_P (abfd) \
671 ? 0x03e0782d /* daddu t7,ra */ \
672 : 0x03e07821)) /* addu t7,ra */
673 #define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
674 #define STUB_JALR 0x0320f809 /* jalr t9,ra */
675 #define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
676 #define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
677 #define STUB_LI16S(abfd, VAL) \
678 ((ABI_64_P (abfd) \
679 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
680 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
682 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
683 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
685 /* The name of the dynamic interpreter. This is put in the .interp
686 section. */
688 #define ELF_DYNAMIC_INTERPRETER(abfd) \
689 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
690 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
691 : "/usr/lib/libc.so.1")
693 #ifdef BFD64
694 #define MNAME(bfd,pre,pos) \
695 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
696 #define ELF_R_SYM(bfd, i) \
697 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
698 #define ELF_R_TYPE(bfd, i) \
699 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
700 #define ELF_R_INFO(bfd, s, t) \
701 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
702 #else
703 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
704 #define ELF_R_SYM(bfd, i) \
705 (ELF32_R_SYM (i))
706 #define ELF_R_TYPE(bfd, i) \
707 (ELF32_R_TYPE (i))
708 #define ELF_R_INFO(bfd, s, t) \
709 (ELF32_R_INFO (s, t))
710 #endif
712 /* The mips16 compiler uses a couple of special sections to handle
713 floating point arguments.
715 Section names that look like .mips16.fn.FNNAME contain stubs that
716 copy floating point arguments from the fp regs to the gp regs and
717 then jump to FNNAME. If any 32 bit function calls FNNAME, the
718 call should be redirected to the stub instead. If no 32 bit
719 function calls FNNAME, the stub should be discarded. We need to
720 consider any reference to the function, not just a call, because
721 if the address of the function is taken we will need the stub,
722 since the address might be passed to a 32 bit function.
724 Section names that look like .mips16.call.FNNAME contain stubs
725 that copy floating point arguments from the gp regs to the fp
726 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
727 then any 16 bit function that calls FNNAME should be redirected
728 to the stub instead. If FNNAME is not a 32 bit function, the
729 stub should be discarded.
731 .mips16.call.fp.FNNAME sections are similar, but contain stubs
732 which call FNNAME and then copy the return value from the fp regs
733 to the gp regs. These stubs store the return value in $18 while
734 calling FNNAME; any function which might call one of these stubs
735 must arrange to save $18 around the call. (This case is not
736 needed for 32 bit functions that call 16 bit functions, because
737 16 bit functions always return floating point values in both
738 $f0/$f1 and $2/$3.)
740 Note that in all cases FNNAME might be defined statically.
741 Therefore, FNNAME is not used literally. Instead, the relocation
742 information will indicate which symbol the section is for.
744 We record any stubs that we find in the symbol table. */
746 #define FN_STUB ".mips16.fn."
747 #define CALL_STUB ".mips16.call."
748 #define CALL_FP_STUB ".mips16.call.fp."
750 #define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
751 #define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
752 #define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
754 /* The format of the first PLT entry in a VxWorks executable. */
755 static const bfd_vma mips_vxworks_exec_plt0_entry[] = {
756 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
757 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
758 0x8f390008, /* lw t9, 8(t9) */
759 0x00000000, /* nop */
760 0x03200008, /* jr t9 */
761 0x00000000 /* nop */
764 /* The format of subsequent PLT entries. */
765 static const bfd_vma mips_vxworks_exec_plt_entry[] = {
766 0x10000000, /* b .PLT_resolver */
767 0x24180000, /* li t8, <pltindex> */
768 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
769 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
770 0x8f390000, /* lw t9, 0(t9) */
771 0x00000000, /* nop */
772 0x03200008, /* jr t9 */
773 0x00000000 /* nop */
776 /* The format of the first PLT entry in a VxWorks shared object. */
777 static const bfd_vma mips_vxworks_shared_plt0_entry[] = {
778 0x8f990008, /* lw t9, 8(gp) */
779 0x00000000, /* nop */
780 0x03200008, /* jr t9 */
781 0x00000000, /* nop */
782 0x00000000, /* nop */
783 0x00000000 /* nop */
786 /* The format of subsequent PLT entries. */
787 static const bfd_vma mips_vxworks_shared_plt_entry[] = {
788 0x10000000, /* b .PLT_resolver */
789 0x24180000 /* li t8, <pltindex> */
792 /* Look up an entry in a MIPS ELF linker hash table. */
794 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
795 ((struct mips_elf_link_hash_entry *) \
796 elf_link_hash_lookup (&(table)->root, (string), (create), \
797 (copy), (follow)))
799 /* Traverse a MIPS ELF linker hash table. */
801 #define mips_elf_link_hash_traverse(table, func, info) \
802 (elf_link_hash_traverse \
803 (&(table)->root, \
804 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
805 (info)))
807 /* Get the MIPS ELF linker hash table from a link_info structure. */
809 #define mips_elf_hash_table(p) \
810 ((struct mips_elf_link_hash_table *) ((p)->hash))
812 /* Find the base offsets for thread-local storage in this object,
813 for GD/LD and IE/LE respectively. */
815 #define TP_OFFSET 0x7000
816 #define DTP_OFFSET 0x8000
818 static bfd_vma
819 dtprel_base (struct bfd_link_info *info)
821 /* If tls_sec is NULL, we should have signalled an error already. */
822 if (elf_hash_table (info)->tls_sec == NULL)
823 return 0;
824 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
827 static bfd_vma
828 tprel_base (struct bfd_link_info *info)
830 /* If tls_sec is NULL, we should have signalled an error already. */
831 if (elf_hash_table (info)->tls_sec == NULL)
832 return 0;
833 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
836 /* Create an entry in a MIPS ELF linker hash table. */
838 static struct bfd_hash_entry *
839 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
840 struct bfd_hash_table *table, const char *string)
842 struct mips_elf_link_hash_entry *ret =
843 (struct mips_elf_link_hash_entry *) entry;
845 /* Allocate the structure if it has not already been allocated by a
846 subclass. */
847 if (ret == NULL)
848 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
849 if (ret == NULL)
850 return (struct bfd_hash_entry *) ret;
852 /* Call the allocation method of the superclass. */
853 ret = ((struct mips_elf_link_hash_entry *)
854 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
855 table, string));
856 if (ret != NULL)
858 /* Set local fields. */
859 memset (&ret->esym, 0, sizeof (EXTR));
860 /* We use -2 as a marker to indicate that the information has
861 not been set. -1 means there is no associated ifd. */
862 ret->esym.ifd = -2;
863 ret->possibly_dynamic_relocs = 0;
864 ret->readonly_reloc = FALSE;
865 ret->no_fn_stub = FALSE;
866 ret->fn_stub = NULL;
867 ret->need_fn_stub = FALSE;
868 ret->call_stub = NULL;
869 ret->call_fp_stub = NULL;
870 ret->forced_local = FALSE;
871 ret->is_branch_target = FALSE;
872 ret->is_relocation_target = FALSE;
873 ret->tls_type = GOT_NORMAL;
876 return (struct bfd_hash_entry *) ret;
879 bfd_boolean
880 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
882 if (!sec->used_by_bfd)
884 struct _mips_elf_section_data *sdata;
885 bfd_size_type amt = sizeof (*sdata);
887 sdata = bfd_zalloc (abfd, amt);
888 if (sdata == NULL)
889 return FALSE;
890 sec->used_by_bfd = sdata;
893 return _bfd_elf_new_section_hook (abfd, sec);
896 /* Read ECOFF debugging information from a .mdebug section into a
897 ecoff_debug_info structure. */
899 bfd_boolean
900 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
901 struct ecoff_debug_info *debug)
903 HDRR *symhdr;
904 const struct ecoff_debug_swap *swap;
905 char *ext_hdr;
907 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
908 memset (debug, 0, sizeof (*debug));
910 ext_hdr = bfd_malloc (swap->external_hdr_size);
911 if (ext_hdr == NULL && swap->external_hdr_size != 0)
912 goto error_return;
914 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
915 swap->external_hdr_size))
916 goto error_return;
918 symhdr = &debug->symbolic_header;
919 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
921 /* The symbolic header contains absolute file offsets and sizes to
922 read. */
923 #define READ(ptr, offset, count, size, type) \
924 if (symhdr->count == 0) \
925 debug->ptr = NULL; \
926 else \
928 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
929 debug->ptr = bfd_malloc (amt); \
930 if (debug->ptr == NULL) \
931 goto error_return; \
932 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
933 || bfd_bread (debug->ptr, amt, abfd) != amt) \
934 goto error_return; \
937 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
938 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
939 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
940 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
941 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
942 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
943 union aux_ext *);
944 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
945 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
946 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
947 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
948 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
949 #undef READ
951 debug->fdr = NULL;
953 return TRUE;
955 error_return:
956 if (ext_hdr != NULL)
957 free (ext_hdr);
958 if (debug->line != NULL)
959 free (debug->line);
960 if (debug->external_dnr != NULL)
961 free (debug->external_dnr);
962 if (debug->external_pdr != NULL)
963 free (debug->external_pdr);
964 if (debug->external_sym != NULL)
965 free (debug->external_sym);
966 if (debug->external_opt != NULL)
967 free (debug->external_opt);
968 if (debug->external_aux != NULL)
969 free (debug->external_aux);
970 if (debug->ss != NULL)
971 free (debug->ss);
972 if (debug->ssext != NULL)
973 free (debug->ssext);
974 if (debug->external_fdr != NULL)
975 free (debug->external_fdr);
976 if (debug->external_rfd != NULL)
977 free (debug->external_rfd);
978 if (debug->external_ext != NULL)
979 free (debug->external_ext);
980 return FALSE;
983 /* Swap RPDR (runtime procedure table entry) for output. */
985 static void
986 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
988 H_PUT_S32 (abfd, in->adr, ex->p_adr);
989 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
990 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
991 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
992 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
993 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
995 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
996 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
998 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1001 /* Create a runtime procedure table from the .mdebug section. */
1003 static bfd_boolean
1004 mips_elf_create_procedure_table (void *handle, bfd *abfd,
1005 struct bfd_link_info *info, asection *s,
1006 struct ecoff_debug_info *debug)
1008 const struct ecoff_debug_swap *swap;
1009 HDRR *hdr = &debug->symbolic_header;
1010 RPDR *rpdr, *rp;
1011 struct rpdr_ext *erp;
1012 void *rtproc;
1013 struct pdr_ext *epdr;
1014 struct sym_ext *esym;
1015 char *ss, **sv;
1016 char *str;
1017 bfd_size_type size;
1018 bfd_size_type count;
1019 unsigned long sindex;
1020 unsigned long i;
1021 PDR pdr;
1022 SYMR sym;
1023 const char *no_name_func = _("static procedure (no name)");
1025 epdr = NULL;
1026 rpdr = NULL;
1027 esym = NULL;
1028 ss = NULL;
1029 sv = NULL;
1031 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1033 sindex = strlen (no_name_func) + 1;
1034 count = hdr->ipdMax;
1035 if (count > 0)
1037 size = swap->external_pdr_size;
1039 epdr = bfd_malloc (size * count);
1040 if (epdr == NULL)
1041 goto error_return;
1043 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1044 goto error_return;
1046 size = sizeof (RPDR);
1047 rp = rpdr = bfd_malloc (size * count);
1048 if (rpdr == NULL)
1049 goto error_return;
1051 size = sizeof (char *);
1052 sv = bfd_malloc (size * count);
1053 if (sv == NULL)
1054 goto error_return;
1056 count = hdr->isymMax;
1057 size = swap->external_sym_size;
1058 esym = bfd_malloc (size * count);
1059 if (esym == NULL)
1060 goto error_return;
1062 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1063 goto error_return;
1065 count = hdr->issMax;
1066 ss = bfd_malloc (count);
1067 if (ss == NULL)
1068 goto error_return;
1069 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1070 goto error_return;
1072 count = hdr->ipdMax;
1073 for (i = 0; i < (unsigned long) count; i++, rp++)
1075 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1076 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1077 rp->adr = sym.value;
1078 rp->regmask = pdr.regmask;
1079 rp->regoffset = pdr.regoffset;
1080 rp->fregmask = pdr.fregmask;
1081 rp->fregoffset = pdr.fregoffset;
1082 rp->frameoffset = pdr.frameoffset;
1083 rp->framereg = pdr.framereg;
1084 rp->pcreg = pdr.pcreg;
1085 rp->irpss = sindex;
1086 sv[i] = ss + sym.iss;
1087 sindex += strlen (sv[i]) + 1;
1091 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1092 size = BFD_ALIGN (size, 16);
1093 rtproc = bfd_alloc (abfd, size);
1094 if (rtproc == NULL)
1096 mips_elf_hash_table (info)->procedure_count = 0;
1097 goto error_return;
1100 mips_elf_hash_table (info)->procedure_count = count + 2;
1102 erp = rtproc;
1103 memset (erp, 0, sizeof (struct rpdr_ext));
1104 erp++;
1105 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1106 strcpy (str, no_name_func);
1107 str += strlen (no_name_func) + 1;
1108 for (i = 0; i < count; i++)
1110 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1111 strcpy (str, sv[i]);
1112 str += strlen (sv[i]) + 1;
1114 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1116 /* Set the size and contents of .rtproc section. */
1117 s->size = size;
1118 s->contents = rtproc;
1120 /* Skip this section later on (I don't think this currently
1121 matters, but someday it might). */
1122 s->map_head.link_order = NULL;
1124 if (epdr != NULL)
1125 free (epdr);
1126 if (rpdr != NULL)
1127 free (rpdr);
1128 if (esym != NULL)
1129 free (esym);
1130 if (ss != NULL)
1131 free (ss);
1132 if (sv != NULL)
1133 free (sv);
1135 return TRUE;
1137 error_return:
1138 if (epdr != NULL)
1139 free (epdr);
1140 if (rpdr != NULL)
1141 free (rpdr);
1142 if (esym != NULL)
1143 free (esym);
1144 if (ss != NULL)
1145 free (ss);
1146 if (sv != NULL)
1147 free (sv);
1148 return FALSE;
1151 /* We're about to redefine H. Create a symbol to represent H's
1152 current value and size, to help make the disassembly easier
1153 to read. */
1155 static bfd_boolean
1156 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1157 struct mips_elf_link_hash_entry *h,
1158 const char *prefix)
1160 struct bfd_link_hash_entry *bh;
1161 struct elf_link_hash_entry *elfh;
1162 const char *name;
1163 asection *s;
1164 bfd_vma value;
1166 /* Read the symbol's value. */
1167 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1168 || h->root.root.type == bfd_link_hash_defweak);
1169 s = h->root.root.u.def.section;
1170 value = h->root.root.u.def.value;
1172 /* Create a new symbol. */
1173 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1174 bh = NULL;
1175 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1176 BSF_LOCAL, s, value, NULL,
1177 TRUE, FALSE, &bh))
1178 return FALSE;
1180 /* Make it local and copy the other attributes from H. */
1181 elfh = (struct elf_link_hash_entry *) bh;
1182 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1183 elfh->other = h->root.other;
1184 elfh->size = h->root.size;
1185 elfh->forced_local = 1;
1186 return TRUE;
1189 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1190 function rather than to a hard-float stub. */
1192 static bfd_boolean
1193 section_allows_mips16_refs_p (asection *section)
1195 const char *name;
1197 name = bfd_get_section_name (section->owner, section);
1198 return (FN_STUB_P (name)
1199 || CALL_STUB_P (name)
1200 || CALL_FP_STUB_P (name)
1201 || strcmp (name, ".pdr") == 0);
1204 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1205 stub section of some kind. Return the R_SYMNDX of the target
1206 function, or 0 if we can't decide which function that is. */
1208 static unsigned long
1209 mips16_stub_symndx (asection *sec, const Elf_Internal_Rela *relocs,
1210 const Elf_Internal_Rela *relend)
1212 const Elf_Internal_Rela *rel;
1214 /* Trust the first R_MIPS_NONE relocation, if any. */
1215 for (rel = relocs; rel < relend; rel++)
1216 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1217 return ELF_R_SYM (sec->owner, rel->r_info);
1219 /* Otherwise trust the first relocation, whatever its kind. This is
1220 the traditional behavior. */
1221 if (relocs < relend)
1222 return ELF_R_SYM (sec->owner, relocs->r_info);
1224 return 0;
1227 /* Check the mips16 stubs for a particular symbol, and see if we can
1228 discard them. */
1230 static bfd_boolean
1231 mips_elf_check_mips16_stubs (struct mips_elf_link_hash_entry *h, void *data)
1233 struct bfd_link_info *info;
1235 info = (struct bfd_link_info *) data;
1236 if (h->root.root.type == bfd_link_hash_warning)
1237 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1239 /* Dynamic symbols must use the standard call interface, in case other
1240 objects try to call them. */
1241 if (h->fn_stub != NULL
1242 && h->root.dynindx != -1)
1244 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1245 h->need_fn_stub = TRUE;
1248 if (h->fn_stub != NULL
1249 && ! h->need_fn_stub)
1251 /* We don't need the fn_stub; the only references to this symbol
1252 are 16 bit calls. Clobber the size to 0 to prevent it from
1253 being included in the link. */
1254 h->fn_stub->size = 0;
1255 h->fn_stub->flags &= ~SEC_RELOC;
1256 h->fn_stub->reloc_count = 0;
1257 h->fn_stub->flags |= SEC_EXCLUDE;
1260 if (h->call_stub != NULL
1261 && ELF_ST_IS_MIPS16 (h->root.other))
1263 /* We don't need the call_stub; this is a 16 bit function, so
1264 calls from other 16 bit functions are OK. Clobber the size
1265 to 0 to prevent it from being included in the link. */
1266 h->call_stub->size = 0;
1267 h->call_stub->flags &= ~SEC_RELOC;
1268 h->call_stub->reloc_count = 0;
1269 h->call_stub->flags |= SEC_EXCLUDE;
1272 if (h->call_fp_stub != NULL
1273 && ELF_ST_IS_MIPS16 (h->root.other))
1275 /* We don't need the call_stub; this is a 16 bit function, so
1276 calls from other 16 bit functions are OK. Clobber the size
1277 to 0 to prevent it from being included in the link. */
1278 h->call_fp_stub->size = 0;
1279 h->call_fp_stub->flags &= ~SEC_RELOC;
1280 h->call_fp_stub->reloc_count = 0;
1281 h->call_fp_stub->flags |= SEC_EXCLUDE;
1284 return TRUE;
1287 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1288 Most mips16 instructions are 16 bits, but these instructions
1289 are 32 bits.
1291 The format of these instructions is:
1293 +--------------+--------------------------------+
1294 | JALX | X| Imm 20:16 | Imm 25:21 |
1295 +--------------+--------------------------------+
1296 | Immediate 15:0 |
1297 +-----------------------------------------------+
1299 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1300 Note that the immediate value in the first word is swapped.
1302 When producing a relocatable object file, R_MIPS16_26 is
1303 handled mostly like R_MIPS_26. In particular, the addend is
1304 stored as a straight 26-bit value in a 32-bit instruction.
1305 (gas makes life simpler for itself by never adjusting a
1306 R_MIPS16_26 reloc to be against a section, so the addend is
1307 always zero). However, the 32 bit instruction is stored as 2
1308 16-bit values, rather than a single 32-bit value. In a
1309 big-endian file, the result is the same; in a little-endian
1310 file, the two 16-bit halves of the 32 bit value are swapped.
1311 This is so that a disassembler can recognize the jal
1312 instruction.
1314 When doing a final link, R_MIPS16_26 is treated as a 32 bit
1315 instruction stored as two 16-bit values. The addend A is the
1316 contents of the targ26 field. The calculation is the same as
1317 R_MIPS_26. When storing the calculated value, reorder the
1318 immediate value as shown above, and don't forget to store the
1319 value as two 16-bit values.
1321 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
1322 defined as
1324 big-endian:
1325 +--------+----------------------+
1326 | | |
1327 | | targ26-16 |
1328 |31 26|25 0|
1329 +--------+----------------------+
1331 little-endian:
1332 +----------+------+-------------+
1333 | | | |
1334 | sub1 | | sub2 |
1335 |0 9|10 15|16 31|
1336 +----------+--------------------+
1337 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
1338 ((sub1 << 16) | sub2)).
1340 When producing a relocatable object file, the calculation is
1341 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1342 When producing a fully linked file, the calculation is
1343 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1344 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
1346 The table below lists the other MIPS16 instruction relocations.
1347 Each one is calculated in the same way as the non-MIPS16 relocation
1348 given on the right, but using the extended MIPS16 layout of 16-bit
1349 immediate fields:
1351 R_MIPS16_GPREL R_MIPS_GPREL16
1352 R_MIPS16_GOT16 R_MIPS_GOT16
1353 R_MIPS16_CALL16 R_MIPS_CALL16
1354 R_MIPS16_HI16 R_MIPS_HI16
1355 R_MIPS16_LO16 R_MIPS_LO16
1357 A typical instruction will have a format like this:
1359 +--------------+--------------------------------+
1360 | EXTEND | Imm 10:5 | Imm 15:11 |
1361 +--------------+--------------------------------+
1362 | Major | rx | ry | Imm 4:0 |
1363 +--------------+--------------------------------+
1365 EXTEND is the five bit value 11110. Major is the instruction
1366 opcode.
1368 All we need to do here is shuffle the bits appropriately.
1369 As above, the two 16-bit halves must be swapped on a
1370 little-endian system. */
1372 static inline bfd_boolean
1373 mips16_reloc_p (int r_type)
1375 switch (r_type)
1377 case R_MIPS16_26:
1378 case R_MIPS16_GPREL:
1379 case R_MIPS16_GOT16:
1380 case R_MIPS16_CALL16:
1381 case R_MIPS16_HI16:
1382 case R_MIPS16_LO16:
1383 return TRUE;
1385 default:
1386 return FALSE;
1390 static inline bfd_boolean
1391 got16_reloc_p (int r_type)
1393 return r_type == R_MIPS_GOT16 || r_type == R_MIPS16_GOT16;
1396 static inline bfd_boolean
1397 call16_reloc_p (int r_type)
1399 return r_type == R_MIPS_CALL16 || r_type == R_MIPS16_CALL16;
1402 static inline bfd_boolean
1403 hi16_reloc_p (int r_type)
1405 return r_type == R_MIPS_HI16 || r_type == R_MIPS16_HI16;
1408 static inline bfd_boolean
1409 lo16_reloc_p (int r_type)
1411 return r_type == R_MIPS_LO16 || r_type == R_MIPS16_LO16;
1414 static inline bfd_boolean
1415 mips16_call_reloc_p (int r_type)
1417 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
1420 void
1421 _bfd_mips16_elf_reloc_unshuffle (bfd *abfd, int r_type,
1422 bfd_boolean jal_shuffle, bfd_byte *data)
1424 bfd_vma extend, insn, val;
1426 if (!mips16_reloc_p (r_type))
1427 return;
1429 /* Pick up the mips16 extend instruction and the real instruction. */
1430 extend = bfd_get_16 (abfd, data);
1431 insn = bfd_get_16 (abfd, data + 2);
1432 if (r_type == R_MIPS16_26)
1434 if (jal_shuffle)
1435 val = ((extend & 0xfc00) << 16) | ((extend & 0x3e0) << 11)
1436 | ((extend & 0x1f) << 21) | insn;
1437 else
1438 val = extend << 16 | insn;
1440 else
1441 val = ((extend & 0xf800) << 16) | ((insn & 0xffe0) << 11)
1442 | ((extend & 0x1f) << 11) | (extend & 0x7e0) | (insn & 0x1f);
1443 bfd_put_32 (abfd, val, data);
1446 void
1447 _bfd_mips16_elf_reloc_shuffle (bfd *abfd, int r_type,
1448 bfd_boolean jal_shuffle, bfd_byte *data)
1450 bfd_vma extend, insn, val;
1452 if (!mips16_reloc_p (r_type))
1453 return;
1455 val = bfd_get_32 (abfd, data);
1456 if (r_type == R_MIPS16_26)
1458 if (jal_shuffle)
1460 insn = val & 0xffff;
1461 extend = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
1462 | ((val >> 21) & 0x1f);
1464 else
1466 insn = val & 0xffff;
1467 extend = val >> 16;
1470 else
1472 insn = ((val >> 11) & 0xffe0) | (val & 0x1f);
1473 extend = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
1475 bfd_put_16 (abfd, insn, data + 2);
1476 bfd_put_16 (abfd, extend, data);
1479 bfd_reloc_status_type
1480 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
1481 arelent *reloc_entry, asection *input_section,
1482 bfd_boolean relocatable, void *data, bfd_vma gp)
1484 bfd_vma relocation;
1485 bfd_signed_vma val;
1486 bfd_reloc_status_type status;
1488 if (bfd_is_com_section (symbol->section))
1489 relocation = 0;
1490 else
1491 relocation = symbol->value;
1493 relocation += symbol->section->output_section->vma;
1494 relocation += symbol->section->output_offset;
1496 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1497 return bfd_reloc_outofrange;
1499 /* Set val to the offset into the section or symbol. */
1500 val = reloc_entry->addend;
1502 _bfd_mips_elf_sign_extend (val, 16);
1504 /* Adjust val for the final section location and GP value. If we
1505 are producing relocatable output, we don't want to do this for
1506 an external symbol. */
1507 if (! relocatable
1508 || (symbol->flags & BSF_SECTION_SYM) != 0)
1509 val += relocation - gp;
1511 if (reloc_entry->howto->partial_inplace)
1513 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1514 (bfd_byte *) data
1515 + reloc_entry->address);
1516 if (status != bfd_reloc_ok)
1517 return status;
1519 else
1520 reloc_entry->addend = val;
1522 if (relocatable)
1523 reloc_entry->address += input_section->output_offset;
1525 return bfd_reloc_ok;
1528 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
1529 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
1530 that contains the relocation field and DATA points to the start of
1531 INPUT_SECTION. */
1533 struct mips_hi16
1535 struct mips_hi16 *next;
1536 bfd_byte *data;
1537 asection *input_section;
1538 arelent rel;
1541 /* FIXME: This should not be a static variable. */
1543 static struct mips_hi16 *mips_hi16_list;
1545 /* A howto special_function for REL *HI16 relocations. We can only
1546 calculate the correct value once we've seen the partnering
1547 *LO16 relocation, so just save the information for later.
1549 The ABI requires that the *LO16 immediately follow the *HI16.
1550 However, as a GNU extension, we permit an arbitrary number of
1551 *HI16s to be associated with a single *LO16. This significantly
1552 simplies the relocation handling in gcc. */
1554 bfd_reloc_status_type
1555 _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1556 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
1557 asection *input_section, bfd *output_bfd,
1558 char **error_message ATTRIBUTE_UNUSED)
1560 struct mips_hi16 *n;
1562 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1563 return bfd_reloc_outofrange;
1565 n = bfd_malloc (sizeof *n);
1566 if (n == NULL)
1567 return bfd_reloc_outofrange;
1569 n->next = mips_hi16_list;
1570 n->data = data;
1571 n->input_section = input_section;
1572 n->rel = *reloc_entry;
1573 mips_hi16_list = n;
1575 if (output_bfd != NULL)
1576 reloc_entry->address += input_section->output_offset;
1578 return bfd_reloc_ok;
1581 /* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
1582 like any other 16-bit relocation when applied to global symbols, but is
1583 treated in the same as R_MIPS_HI16 when applied to local symbols. */
1585 bfd_reloc_status_type
1586 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1587 void *data, asection *input_section,
1588 bfd *output_bfd, char **error_message)
1590 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1591 || bfd_is_und_section (bfd_get_section (symbol))
1592 || bfd_is_com_section (bfd_get_section (symbol)))
1593 /* The relocation is against a global symbol. */
1594 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1595 input_section, output_bfd,
1596 error_message);
1598 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1599 input_section, output_bfd, error_message);
1602 /* A howto special_function for REL *LO16 relocations. The *LO16 itself
1603 is a straightforward 16 bit inplace relocation, but we must deal with
1604 any partnering high-part relocations as well. */
1606 bfd_reloc_status_type
1607 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1608 void *data, asection *input_section,
1609 bfd *output_bfd, char **error_message)
1611 bfd_vma vallo;
1612 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
1614 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1615 return bfd_reloc_outofrange;
1617 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
1618 location);
1619 vallo = bfd_get_32 (abfd, location);
1620 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
1621 location);
1623 while (mips_hi16_list != NULL)
1625 bfd_reloc_status_type ret;
1626 struct mips_hi16 *hi;
1628 hi = mips_hi16_list;
1630 /* R_MIPS*_GOT16 relocations are something of a special case. We
1631 want to install the addend in the same way as for a R_MIPS*_HI16
1632 relocation (with a rightshift of 16). However, since GOT16
1633 relocations can also be used with global symbols, their howto
1634 has a rightshift of 0. */
1635 if (hi->rel.howto->type == R_MIPS_GOT16)
1636 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
1637 else if (hi->rel.howto->type == R_MIPS16_GOT16)
1638 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
1640 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
1641 carry or borrow will induce a change of +1 or -1 in the high part. */
1642 hi->rel.addend += (vallo + 0x8000) & 0xffff;
1644 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
1645 hi->input_section, output_bfd,
1646 error_message);
1647 if (ret != bfd_reloc_ok)
1648 return ret;
1650 mips_hi16_list = hi->next;
1651 free (hi);
1654 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1655 input_section, output_bfd,
1656 error_message);
1659 /* A generic howto special_function. This calculates and installs the
1660 relocation itself, thus avoiding the oft-discussed problems in
1661 bfd_perform_relocation and bfd_install_relocation. */
1663 bfd_reloc_status_type
1664 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1665 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
1666 asection *input_section, bfd *output_bfd,
1667 char **error_message ATTRIBUTE_UNUSED)
1669 bfd_signed_vma val;
1670 bfd_reloc_status_type status;
1671 bfd_boolean relocatable;
1673 relocatable = (output_bfd != NULL);
1675 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1676 return bfd_reloc_outofrange;
1678 /* Build up the field adjustment in VAL. */
1679 val = 0;
1680 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
1682 /* Either we're calculating the final field value or we have a
1683 relocation against a section symbol. Add in the section's
1684 offset or address. */
1685 val += symbol->section->output_section->vma;
1686 val += symbol->section->output_offset;
1689 if (!relocatable)
1691 /* We're calculating the final field value. Add in the symbol's value
1692 and, if pc-relative, subtract the address of the field itself. */
1693 val += symbol->value;
1694 if (reloc_entry->howto->pc_relative)
1696 val -= input_section->output_section->vma;
1697 val -= input_section->output_offset;
1698 val -= reloc_entry->address;
1702 /* VAL is now the final adjustment. If we're keeping this relocation
1703 in the output file, and if the relocation uses a separate addend,
1704 we just need to add VAL to that addend. Otherwise we need to add
1705 VAL to the relocation field itself. */
1706 if (relocatable && !reloc_entry->howto->partial_inplace)
1707 reloc_entry->addend += val;
1708 else
1710 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
1712 /* Add in the separate addend, if any. */
1713 val += reloc_entry->addend;
1715 /* Add VAL to the relocation field. */
1716 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
1717 location);
1718 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1719 location);
1720 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
1721 location);
1723 if (status != bfd_reloc_ok)
1724 return status;
1727 if (relocatable)
1728 reloc_entry->address += input_section->output_offset;
1730 return bfd_reloc_ok;
1733 /* Swap an entry in a .gptab section. Note that these routines rely
1734 on the equivalence of the two elements of the union. */
1736 static void
1737 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
1738 Elf32_gptab *in)
1740 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
1741 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
1744 static void
1745 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
1746 Elf32_External_gptab *ex)
1748 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
1749 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
1752 static void
1753 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
1754 Elf32_External_compact_rel *ex)
1756 H_PUT_32 (abfd, in->id1, ex->id1);
1757 H_PUT_32 (abfd, in->num, ex->num);
1758 H_PUT_32 (abfd, in->id2, ex->id2);
1759 H_PUT_32 (abfd, in->offset, ex->offset);
1760 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
1761 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
1764 static void
1765 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
1766 Elf32_External_crinfo *ex)
1768 unsigned long l;
1770 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
1771 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
1772 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
1773 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
1774 H_PUT_32 (abfd, l, ex->info);
1775 H_PUT_32 (abfd, in->konst, ex->konst);
1776 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
1779 /* A .reginfo section holds a single Elf32_RegInfo structure. These
1780 routines swap this structure in and out. They are used outside of
1781 BFD, so they are globally visible. */
1783 void
1784 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
1785 Elf32_RegInfo *in)
1787 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
1788 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
1789 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
1790 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
1791 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
1792 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
1795 void
1796 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
1797 Elf32_External_RegInfo *ex)
1799 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
1800 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
1801 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
1802 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
1803 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
1804 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
1807 /* In the 64 bit ABI, the .MIPS.options section holds register
1808 information in an Elf64_Reginfo structure. These routines swap
1809 them in and out. They are globally visible because they are used
1810 outside of BFD. These routines are here so that gas can call them
1811 without worrying about whether the 64 bit ABI has been included. */
1813 void
1814 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
1815 Elf64_Internal_RegInfo *in)
1817 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
1818 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
1819 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
1820 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
1821 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
1822 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
1823 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
1826 void
1827 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
1828 Elf64_External_RegInfo *ex)
1830 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
1831 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
1832 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
1833 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
1834 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
1835 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
1836 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
1839 /* Swap in an options header. */
1841 void
1842 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
1843 Elf_Internal_Options *in)
1845 in->kind = H_GET_8 (abfd, ex->kind);
1846 in->size = H_GET_8 (abfd, ex->size);
1847 in->section = H_GET_16 (abfd, ex->section);
1848 in->info = H_GET_32 (abfd, ex->info);
1851 /* Swap out an options header. */
1853 void
1854 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
1855 Elf_External_Options *ex)
1857 H_PUT_8 (abfd, in->kind, ex->kind);
1858 H_PUT_8 (abfd, in->size, ex->size);
1859 H_PUT_16 (abfd, in->section, ex->section);
1860 H_PUT_32 (abfd, in->info, ex->info);
1863 /* This function is called via qsort() to sort the dynamic relocation
1864 entries by increasing r_symndx value. */
1866 static int
1867 sort_dynamic_relocs (const void *arg1, const void *arg2)
1869 Elf_Internal_Rela int_reloc1;
1870 Elf_Internal_Rela int_reloc2;
1871 int diff;
1873 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
1874 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
1876 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
1877 if (diff != 0)
1878 return diff;
1880 if (int_reloc1.r_offset < int_reloc2.r_offset)
1881 return -1;
1882 if (int_reloc1.r_offset > int_reloc2.r_offset)
1883 return 1;
1884 return 0;
1887 /* Like sort_dynamic_relocs, but used for elf64 relocations. */
1889 static int
1890 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
1891 const void *arg2 ATTRIBUTE_UNUSED)
1893 #ifdef BFD64
1894 Elf_Internal_Rela int_reloc1[3];
1895 Elf_Internal_Rela int_reloc2[3];
1897 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
1898 (reldyn_sorting_bfd, arg1, int_reloc1);
1899 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
1900 (reldyn_sorting_bfd, arg2, int_reloc2);
1902 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
1903 return -1;
1904 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
1905 return 1;
1907 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
1908 return -1;
1909 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
1910 return 1;
1911 return 0;
1912 #else
1913 abort ();
1914 #endif
1918 /* This routine is used to write out ECOFF debugging external symbol
1919 information. It is called via mips_elf_link_hash_traverse. The
1920 ECOFF external symbol information must match the ELF external
1921 symbol information. Unfortunately, at this point we don't know
1922 whether a symbol is required by reloc information, so the two
1923 tables may wind up being different. We must sort out the external
1924 symbol information before we can set the final size of the .mdebug
1925 section, and we must set the size of the .mdebug section before we
1926 can relocate any sections, and we can't know which symbols are
1927 required by relocation until we relocate the sections.
1928 Fortunately, it is relatively unlikely that any symbol will be
1929 stripped but required by a reloc. In particular, it can not happen
1930 when generating a final executable. */
1932 static bfd_boolean
1933 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
1935 struct extsym_info *einfo = data;
1936 bfd_boolean strip;
1937 asection *sec, *output_section;
1939 if (h->root.root.type == bfd_link_hash_warning)
1940 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1942 if (h->root.indx == -2)
1943 strip = FALSE;
1944 else if ((h->root.def_dynamic
1945 || h->root.ref_dynamic
1946 || h->root.type == bfd_link_hash_new)
1947 && !h->root.def_regular
1948 && !h->root.ref_regular)
1949 strip = TRUE;
1950 else if (einfo->info->strip == strip_all
1951 || (einfo->info->strip == strip_some
1952 && bfd_hash_lookup (einfo->info->keep_hash,
1953 h->root.root.root.string,
1954 FALSE, FALSE) == NULL))
1955 strip = TRUE;
1956 else
1957 strip = FALSE;
1959 if (strip)
1960 return TRUE;
1962 if (h->esym.ifd == -2)
1964 h->esym.jmptbl = 0;
1965 h->esym.cobol_main = 0;
1966 h->esym.weakext = 0;
1967 h->esym.reserved = 0;
1968 h->esym.ifd = ifdNil;
1969 h->esym.asym.value = 0;
1970 h->esym.asym.st = stGlobal;
1972 if (h->root.root.type == bfd_link_hash_undefined
1973 || h->root.root.type == bfd_link_hash_undefweak)
1975 const char *name;
1977 /* Use undefined class. Also, set class and type for some
1978 special symbols. */
1979 name = h->root.root.root.string;
1980 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
1981 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
1983 h->esym.asym.sc = scData;
1984 h->esym.asym.st = stLabel;
1985 h->esym.asym.value = 0;
1987 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
1989 h->esym.asym.sc = scAbs;
1990 h->esym.asym.st = stLabel;
1991 h->esym.asym.value =
1992 mips_elf_hash_table (einfo->info)->procedure_count;
1994 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
1996 h->esym.asym.sc = scAbs;
1997 h->esym.asym.st = stLabel;
1998 h->esym.asym.value = elf_gp (einfo->abfd);
2000 else
2001 h->esym.asym.sc = scUndefined;
2003 else if (h->root.root.type != bfd_link_hash_defined
2004 && h->root.root.type != bfd_link_hash_defweak)
2005 h->esym.asym.sc = scAbs;
2006 else
2008 const char *name;
2010 sec = h->root.root.u.def.section;
2011 output_section = sec->output_section;
2013 /* When making a shared library and symbol h is the one from
2014 the another shared library, OUTPUT_SECTION may be null. */
2015 if (output_section == NULL)
2016 h->esym.asym.sc = scUndefined;
2017 else
2019 name = bfd_section_name (output_section->owner, output_section);
2021 if (strcmp (name, ".text") == 0)
2022 h->esym.asym.sc = scText;
2023 else if (strcmp (name, ".data") == 0)
2024 h->esym.asym.sc = scData;
2025 else if (strcmp (name, ".sdata") == 0)
2026 h->esym.asym.sc = scSData;
2027 else if (strcmp (name, ".rodata") == 0
2028 || strcmp (name, ".rdata") == 0)
2029 h->esym.asym.sc = scRData;
2030 else if (strcmp (name, ".bss") == 0)
2031 h->esym.asym.sc = scBss;
2032 else if (strcmp (name, ".sbss") == 0)
2033 h->esym.asym.sc = scSBss;
2034 else if (strcmp (name, ".init") == 0)
2035 h->esym.asym.sc = scInit;
2036 else if (strcmp (name, ".fini") == 0)
2037 h->esym.asym.sc = scFini;
2038 else
2039 h->esym.asym.sc = scAbs;
2043 h->esym.asym.reserved = 0;
2044 h->esym.asym.index = indexNil;
2047 if (h->root.root.type == bfd_link_hash_common)
2048 h->esym.asym.value = h->root.root.u.c.size;
2049 else if (h->root.root.type == bfd_link_hash_defined
2050 || h->root.root.type == bfd_link_hash_defweak)
2052 if (h->esym.asym.sc == scCommon)
2053 h->esym.asym.sc = scBss;
2054 else if (h->esym.asym.sc == scSCommon)
2055 h->esym.asym.sc = scSBss;
2057 sec = h->root.root.u.def.section;
2058 output_section = sec->output_section;
2059 if (output_section != NULL)
2060 h->esym.asym.value = (h->root.root.u.def.value
2061 + sec->output_offset
2062 + output_section->vma);
2063 else
2064 h->esym.asym.value = 0;
2066 else if (h->root.needs_plt)
2068 struct mips_elf_link_hash_entry *hd = h;
2069 bfd_boolean no_fn_stub = h->no_fn_stub;
2071 while (hd->root.root.type == bfd_link_hash_indirect)
2073 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
2074 no_fn_stub = no_fn_stub || hd->no_fn_stub;
2077 if (!no_fn_stub)
2079 /* Set type and value for a symbol with a function stub. */
2080 h->esym.asym.st = stProc;
2081 sec = hd->root.root.u.def.section;
2082 if (sec == NULL)
2083 h->esym.asym.value = 0;
2084 else
2086 output_section = sec->output_section;
2087 if (output_section != NULL)
2088 h->esym.asym.value = (hd->root.plt.offset
2089 + sec->output_offset
2090 + output_section->vma);
2091 else
2092 h->esym.asym.value = 0;
2097 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2098 h->root.root.root.string,
2099 &h->esym))
2101 einfo->failed = TRUE;
2102 return FALSE;
2105 return TRUE;
2108 /* A comparison routine used to sort .gptab entries. */
2110 static int
2111 gptab_compare (const void *p1, const void *p2)
2113 const Elf32_gptab *a1 = p1;
2114 const Elf32_gptab *a2 = p2;
2116 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2119 /* Functions to manage the got entry hash table. */
2121 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2122 hash number. */
2124 static INLINE hashval_t
2125 mips_elf_hash_bfd_vma (bfd_vma addr)
2127 #ifdef BFD64
2128 return addr + (addr >> 32);
2129 #else
2130 return addr;
2131 #endif
2134 /* got_entries only match if they're identical, except for gotidx, so
2135 use all fields to compute the hash, and compare the appropriate
2136 union members. */
2138 static hashval_t
2139 mips_elf_got_entry_hash (const void *entry_)
2141 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2143 return entry->symndx
2144 + ((entry->tls_type & GOT_TLS_LDM) << 17)
2145 + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
2146 : entry->abfd->id
2147 + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
2148 : entry->d.h->root.root.root.hash));
2151 static int
2152 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
2154 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2155 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2157 /* An LDM entry can only match another LDM entry. */
2158 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2159 return 0;
2161 return e1->abfd == e2->abfd && e1->symndx == e2->symndx
2162 && (! e1->abfd ? e1->d.address == e2->d.address
2163 : e1->symndx >= 0 ? e1->d.addend == e2->d.addend
2164 : e1->d.h == e2->d.h);
2167 /* multi_got_entries are still a match in the case of global objects,
2168 even if the input bfd in which they're referenced differs, so the
2169 hash computation and compare functions are adjusted
2170 accordingly. */
2172 static hashval_t
2173 mips_elf_multi_got_entry_hash (const void *entry_)
2175 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2177 return entry->symndx
2178 + (! entry->abfd
2179 ? mips_elf_hash_bfd_vma (entry->d.address)
2180 : entry->symndx >= 0
2181 ? ((entry->tls_type & GOT_TLS_LDM)
2182 ? (GOT_TLS_LDM << 17)
2183 : (entry->abfd->id
2184 + mips_elf_hash_bfd_vma (entry->d.addend)))
2185 : entry->d.h->root.root.root.hash);
2188 static int
2189 mips_elf_multi_got_entry_eq (const void *entry1, const void *entry2)
2191 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2192 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2194 /* Any two LDM entries match. */
2195 if (e1->tls_type & e2->tls_type & GOT_TLS_LDM)
2196 return 1;
2198 /* Nothing else matches an LDM entry. */
2199 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2200 return 0;
2202 return e1->symndx == e2->symndx
2203 && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
2204 : e1->abfd == NULL || e2->abfd == NULL
2205 ? e1->abfd == e2->abfd && e1->d.address == e2->d.address
2206 : e1->d.h == e2->d.h);
2209 static hashval_t
2210 mips_got_page_entry_hash (const void *entry_)
2212 const struct mips_got_page_entry *entry;
2214 entry = (const struct mips_got_page_entry *) entry_;
2215 return entry->abfd->id + entry->symndx;
2218 static int
2219 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
2221 const struct mips_got_page_entry *entry1, *entry2;
2223 entry1 = (const struct mips_got_page_entry *) entry1_;
2224 entry2 = (const struct mips_got_page_entry *) entry2_;
2225 return entry1->abfd == entry2->abfd && entry1->symndx == entry2->symndx;
2228 /* Return the dynamic relocation section. If it doesn't exist, try to
2229 create a new it if CREATE_P, otherwise return NULL. Also return NULL
2230 if creation fails. */
2232 static asection *
2233 mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
2235 const char *dname;
2236 asection *sreloc;
2237 bfd *dynobj;
2239 dname = MIPS_ELF_REL_DYN_NAME (info);
2240 dynobj = elf_hash_table (info)->dynobj;
2241 sreloc = bfd_get_section_by_name (dynobj, dname);
2242 if (sreloc == NULL && create_p)
2244 sreloc = bfd_make_section_with_flags (dynobj, dname,
2245 (SEC_ALLOC
2246 | SEC_LOAD
2247 | SEC_HAS_CONTENTS
2248 | SEC_IN_MEMORY
2249 | SEC_LINKER_CREATED
2250 | SEC_READONLY));
2251 if (sreloc == NULL
2252 || ! bfd_set_section_alignment (dynobj, sreloc,
2253 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
2254 return NULL;
2256 return sreloc;
2259 /* Returns the GOT section for ABFD. */
2261 static asection *
2262 mips_elf_got_section (bfd *abfd, bfd_boolean maybe_excluded)
2264 asection *sgot = bfd_get_section_by_name (abfd, ".got");
2265 if (sgot == NULL
2266 || (! maybe_excluded && (sgot->flags & SEC_EXCLUDE) != 0))
2267 return NULL;
2268 return sgot;
2271 /* Returns the GOT information associated with the link indicated by
2272 INFO. If SGOTP is non-NULL, it is filled in with the GOT
2273 section. */
2275 static struct mips_got_info *
2276 mips_elf_got_info (bfd *abfd, asection **sgotp)
2278 asection *sgot;
2279 struct mips_got_info *g;
2281 sgot = mips_elf_got_section (abfd, TRUE);
2282 BFD_ASSERT (sgot != NULL);
2283 BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
2284 g = mips_elf_section_data (sgot)->u.got_info;
2285 BFD_ASSERT (g != NULL);
2287 if (sgotp)
2288 *sgotp = (sgot->flags & SEC_EXCLUDE) == 0 ? sgot : NULL;
2290 return g;
2293 /* Count the number of relocations needed for a TLS GOT entry, with
2294 access types from TLS_TYPE, and symbol H (or a local symbol if H
2295 is NULL). */
2297 static int
2298 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
2299 struct elf_link_hash_entry *h)
2301 int indx = 0;
2302 int ret = 0;
2303 bfd_boolean need_relocs = FALSE;
2304 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2306 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2307 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
2308 indx = h->dynindx;
2310 if ((info->shared || indx != 0)
2311 && (h == NULL
2312 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2313 || h->root.type != bfd_link_hash_undefweak))
2314 need_relocs = TRUE;
2316 if (!need_relocs)
2317 return FALSE;
2319 if (tls_type & GOT_TLS_GD)
2321 ret++;
2322 if (indx != 0)
2323 ret++;
2326 if (tls_type & GOT_TLS_IE)
2327 ret++;
2329 if ((tls_type & GOT_TLS_LDM) && info->shared)
2330 ret++;
2332 return ret;
2335 /* Count the number of TLS relocations required for the GOT entry in
2336 ARG1, if it describes a local symbol. */
2338 static int
2339 mips_elf_count_local_tls_relocs (void **arg1, void *arg2)
2341 struct mips_got_entry *entry = * (struct mips_got_entry **) arg1;
2342 struct mips_elf_count_tls_arg *arg = arg2;
2344 if (entry->abfd != NULL && entry->symndx != -1)
2345 arg->needed += mips_tls_got_relocs (arg->info, entry->tls_type, NULL);
2347 return 1;
2350 /* Count the number of TLS GOT entries required for the global (or
2351 forced-local) symbol in ARG1. */
2353 static int
2354 mips_elf_count_global_tls_entries (void *arg1, void *arg2)
2356 struct mips_elf_link_hash_entry *hm
2357 = (struct mips_elf_link_hash_entry *) arg1;
2358 struct mips_elf_count_tls_arg *arg = arg2;
2360 if (hm->tls_type & GOT_TLS_GD)
2361 arg->needed += 2;
2362 if (hm->tls_type & GOT_TLS_IE)
2363 arg->needed += 1;
2365 return 1;
2368 /* Count the number of TLS relocations required for the global (or
2369 forced-local) symbol in ARG1. */
2371 static int
2372 mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
2374 struct mips_elf_link_hash_entry *hm
2375 = (struct mips_elf_link_hash_entry *) arg1;
2376 struct mips_elf_count_tls_arg *arg = arg2;
2378 arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
2380 return 1;
2383 /* Output a simple dynamic relocation into SRELOC. */
2385 static void
2386 mips_elf_output_dynamic_relocation (bfd *output_bfd,
2387 asection *sreloc,
2388 unsigned long indx,
2389 int r_type,
2390 bfd_vma offset)
2392 Elf_Internal_Rela rel[3];
2394 memset (rel, 0, sizeof (rel));
2396 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
2397 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
2399 if (ABI_64_P (output_bfd))
2401 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2402 (output_bfd, &rel[0],
2403 (sreloc->contents
2404 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
2406 else
2407 bfd_elf32_swap_reloc_out
2408 (output_bfd, &rel[0],
2409 (sreloc->contents
2410 + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
2411 ++sreloc->reloc_count;
2414 /* Initialize a set of TLS GOT entries for one symbol. */
2416 static void
2417 mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
2418 unsigned char *tls_type_p,
2419 struct bfd_link_info *info,
2420 struct mips_elf_link_hash_entry *h,
2421 bfd_vma value)
2423 int indx;
2424 asection *sreloc, *sgot;
2425 bfd_vma offset, offset2;
2426 bfd *dynobj;
2427 bfd_boolean need_relocs = FALSE;
2429 dynobj = elf_hash_table (info)->dynobj;
2430 sgot = mips_elf_got_section (dynobj, FALSE);
2432 indx = 0;
2433 if (h != NULL)
2435 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2437 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
2438 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
2439 indx = h->root.dynindx;
2442 if (*tls_type_p & GOT_TLS_DONE)
2443 return;
2445 if ((info->shared || indx != 0)
2446 && (h == NULL
2447 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
2448 || h->root.type != bfd_link_hash_undefweak))
2449 need_relocs = TRUE;
2451 /* MINUS_ONE means the symbol is not defined in this object. It may not
2452 be defined at all; assume that the value doesn't matter in that
2453 case. Otherwise complain if we would use the value. */
2454 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
2455 || h->root.root.type == bfd_link_hash_undefweak);
2457 /* Emit necessary relocations. */
2458 sreloc = mips_elf_rel_dyn_section (info, FALSE);
2460 /* General Dynamic. */
2461 if (*tls_type_p & GOT_TLS_GD)
2463 offset = got_offset;
2464 offset2 = offset + MIPS_ELF_GOT_SIZE (abfd);
2466 if (need_relocs)
2468 mips_elf_output_dynamic_relocation
2469 (abfd, sreloc, indx,
2470 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2471 sgot->output_offset + sgot->output_section->vma + offset);
2473 if (indx)
2474 mips_elf_output_dynamic_relocation
2475 (abfd, sreloc, indx,
2476 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
2477 sgot->output_offset + sgot->output_section->vma + offset2);
2478 else
2479 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2480 sgot->contents + offset2);
2482 else
2484 MIPS_ELF_PUT_WORD (abfd, 1,
2485 sgot->contents + offset);
2486 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2487 sgot->contents + offset2);
2490 got_offset += 2 * MIPS_ELF_GOT_SIZE (abfd);
2493 /* Initial Exec model. */
2494 if (*tls_type_p & GOT_TLS_IE)
2496 offset = got_offset;
2498 if (need_relocs)
2500 if (indx == 0)
2501 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
2502 sgot->contents + offset);
2503 else
2504 MIPS_ELF_PUT_WORD (abfd, 0,
2505 sgot->contents + offset);
2507 mips_elf_output_dynamic_relocation
2508 (abfd, sreloc, indx,
2509 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
2510 sgot->output_offset + sgot->output_section->vma + offset);
2512 else
2513 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
2514 sgot->contents + offset);
2517 if (*tls_type_p & GOT_TLS_LDM)
2519 /* The initial offset is zero, and the LD offsets will include the
2520 bias by DTP_OFFSET. */
2521 MIPS_ELF_PUT_WORD (abfd, 0,
2522 sgot->contents + got_offset
2523 + MIPS_ELF_GOT_SIZE (abfd));
2525 if (!info->shared)
2526 MIPS_ELF_PUT_WORD (abfd, 1,
2527 sgot->contents + got_offset);
2528 else
2529 mips_elf_output_dynamic_relocation
2530 (abfd, sreloc, indx,
2531 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2532 sgot->output_offset + sgot->output_section->vma + got_offset);
2535 *tls_type_p |= GOT_TLS_DONE;
2538 /* Return the GOT index to use for a relocation of type R_TYPE against
2539 a symbol accessed using TLS_TYPE models. The GOT entries for this
2540 symbol in this GOT start at GOT_INDEX. This function initializes the
2541 GOT entries and corresponding relocations. */
2543 static bfd_vma
2544 mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
2545 int r_type, struct bfd_link_info *info,
2546 struct mips_elf_link_hash_entry *h, bfd_vma symbol)
2548 BFD_ASSERT (r_type == R_MIPS_TLS_GOTTPREL || r_type == R_MIPS_TLS_GD
2549 || r_type == R_MIPS_TLS_LDM);
2551 mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
2553 if (r_type == R_MIPS_TLS_GOTTPREL)
2555 BFD_ASSERT (*tls_type & GOT_TLS_IE);
2556 if (*tls_type & GOT_TLS_GD)
2557 return got_index + 2 * MIPS_ELF_GOT_SIZE (abfd);
2558 else
2559 return got_index;
2562 if (r_type == R_MIPS_TLS_GD)
2564 BFD_ASSERT (*tls_type & GOT_TLS_GD);
2565 return got_index;
2568 if (r_type == R_MIPS_TLS_LDM)
2570 BFD_ASSERT (*tls_type & GOT_TLS_LDM);
2571 return got_index;
2574 return got_index;
2577 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
2578 for global symbol H. .got.plt comes before the GOT, so the offset
2579 will be negative. */
2581 static bfd_vma
2582 mips_elf_gotplt_index (struct bfd_link_info *info,
2583 struct elf_link_hash_entry *h)
2585 bfd_vma plt_index, got_address, got_value;
2586 struct mips_elf_link_hash_table *htab;
2588 htab = mips_elf_hash_table (info);
2589 BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
2591 /* Calculate the index of the symbol's PLT entry. */
2592 plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
2594 /* Calculate the address of the associated .got.plt entry. */
2595 got_address = (htab->sgotplt->output_section->vma
2596 + htab->sgotplt->output_offset
2597 + plt_index * 4);
2599 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
2600 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
2601 + htab->root.hgot->root.u.def.section->output_offset
2602 + htab->root.hgot->root.u.def.value);
2604 return got_address - got_value;
2607 /* Return the GOT offset for address VALUE. If there is not yet a GOT
2608 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
2609 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
2610 offset can be found. */
2612 static bfd_vma
2613 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
2614 bfd_vma value, unsigned long r_symndx,
2615 struct mips_elf_link_hash_entry *h, int r_type)
2617 asection *sgot;
2618 struct mips_got_info *g;
2619 struct mips_got_entry *entry;
2621 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
2623 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, g, sgot,
2624 value, r_symndx, h, r_type);
2625 if (!entry)
2626 return MINUS_ONE;
2628 if (TLS_RELOC_P (r_type))
2630 if (entry->symndx == -1 && g->next == NULL)
2631 /* A type (3) entry in the single-GOT case. We use the symbol's
2632 hash table entry to track the index. */
2633 return mips_tls_got_index (abfd, h->tls_got_offset, &h->tls_type,
2634 r_type, info, h, value);
2635 else
2636 return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type,
2637 r_type, info, h, value);
2639 else
2640 return entry->gotidx;
2643 /* Returns the GOT index for the global symbol indicated by H. */
2645 static bfd_vma
2646 mips_elf_global_got_index (bfd *abfd, bfd *ibfd, struct elf_link_hash_entry *h,
2647 int r_type, struct bfd_link_info *info)
2649 bfd_vma index;
2650 asection *sgot;
2651 struct mips_got_info *g, *gg;
2652 long global_got_dynindx = 0;
2654 gg = g = mips_elf_got_info (abfd, &sgot);
2655 if (g->bfd2got && ibfd)
2657 struct mips_got_entry e, *p;
2659 BFD_ASSERT (h->dynindx >= 0);
2661 g = mips_elf_got_for_ibfd (g, ibfd);
2662 if (g->next != gg || TLS_RELOC_P (r_type))
2664 e.abfd = ibfd;
2665 e.symndx = -1;
2666 e.d.h = (struct mips_elf_link_hash_entry *)h;
2667 e.tls_type = 0;
2669 p = htab_find (g->got_entries, &e);
2671 BFD_ASSERT (p->gotidx > 0);
2673 if (TLS_RELOC_P (r_type))
2675 bfd_vma value = MINUS_ONE;
2676 if ((h->root.type == bfd_link_hash_defined
2677 || h->root.type == bfd_link_hash_defweak)
2678 && h->root.u.def.section->output_section)
2679 value = (h->root.u.def.value
2680 + h->root.u.def.section->output_offset
2681 + h->root.u.def.section->output_section->vma);
2683 return mips_tls_got_index (abfd, p->gotidx, &p->tls_type, r_type,
2684 info, e.d.h, value);
2686 else
2687 return p->gotidx;
2691 if (gg->global_gotsym != NULL)
2692 global_got_dynindx = gg->global_gotsym->dynindx;
2694 if (TLS_RELOC_P (r_type))
2696 struct mips_elf_link_hash_entry *hm
2697 = (struct mips_elf_link_hash_entry *) h;
2698 bfd_vma value = MINUS_ONE;
2700 if ((h->root.type == bfd_link_hash_defined
2701 || h->root.type == bfd_link_hash_defweak)
2702 && h->root.u.def.section->output_section)
2703 value = (h->root.u.def.value
2704 + h->root.u.def.section->output_offset
2705 + h->root.u.def.section->output_section->vma);
2707 index = mips_tls_got_index (abfd, hm->tls_got_offset, &hm->tls_type,
2708 r_type, info, hm, value);
2710 else
2712 /* Once we determine the global GOT entry with the lowest dynamic
2713 symbol table index, we must put all dynamic symbols with greater
2714 indices into the GOT. That makes it easy to calculate the GOT
2715 offset. */
2716 BFD_ASSERT (h->dynindx >= global_got_dynindx);
2717 index = ((h->dynindx - global_got_dynindx + g->local_gotno)
2718 * MIPS_ELF_GOT_SIZE (abfd));
2720 BFD_ASSERT (index < sgot->size);
2722 return index;
2725 /* Find a GOT page entry that points to within 32KB of VALUE. These
2726 entries are supposed to be placed at small offsets in the GOT, i.e.,
2727 within 32KB of GP. Return the index of the GOT entry, or -1 if no
2728 entry could be created. If OFFSETP is nonnull, use it to return the
2729 offset of the GOT entry from VALUE. */
2731 static bfd_vma
2732 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
2733 bfd_vma value, bfd_vma *offsetp)
2735 asection *sgot;
2736 struct mips_got_info *g;
2737 bfd_vma page, index;
2738 struct mips_got_entry *entry;
2740 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
2742 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
2743 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, g, sgot,
2744 page, 0, NULL, R_MIPS_GOT_PAGE);
2746 if (!entry)
2747 return MINUS_ONE;
2749 index = entry->gotidx;
2751 if (offsetp)
2752 *offsetp = value - entry->d.address;
2754 return index;
2757 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
2758 EXTERNAL is true if the relocation was against a global symbol
2759 that has been forced local. */
2761 static bfd_vma
2762 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
2763 bfd_vma value, bfd_boolean external)
2765 asection *sgot;
2766 struct mips_got_info *g;
2767 struct mips_got_entry *entry;
2769 /* GOT16 relocations against local symbols are followed by a LO16
2770 relocation; those against global symbols are not. Thus if the
2771 symbol was originally local, the GOT16 relocation should load the
2772 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
2773 if (! external)
2774 value = mips_elf_high (value) << 16;
2776 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
2778 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
2779 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
2780 same in all cases. */
2781 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, g, sgot,
2782 value, 0, NULL, R_MIPS_GOT16);
2783 if (entry)
2784 return entry->gotidx;
2785 else
2786 return MINUS_ONE;
2789 /* Returns the offset for the entry at the INDEXth position
2790 in the GOT. */
2792 static bfd_vma
2793 mips_elf_got_offset_from_index (bfd *dynobj, bfd *output_bfd,
2794 bfd *input_bfd, bfd_vma index)
2796 asection *sgot;
2797 bfd_vma gp;
2798 struct mips_got_info *g;
2800 g = mips_elf_got_info (dynobj, &sgot);
2801 gp = _bfd_get_gp_value (output_bfd)
2802 + mips_elf_adjust_gp (output_bfd, g, input_bfd);
2804 return sgot->output_section->vma + sgot->output_offset + index - gp;
2807 /* Create and return a local GOT entry for VALUE, which was calculated
2808 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
2809 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
2810 instead. */
2812 static struct mips_got_entry *
2813 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
2814 bfd *ibfd, struct mips_got_info *gg,
2815 asection *sgot, bfd_vma value,
2816 unsigned long r_symndx,
2817 struct mips_elf_link_hash_entry *h,
2818 int r_type)
2820 struct mips_got_entry entry, **loc;
2821 struct mips_got_info *g;
2822 struct mips_elf_link_hash_table *htab;
2824 htab = mips_elf_hash_table (info);
2826 entry.abfd = NULL;
2827 entry.symndx = -1;
2828 entry.d.address = value;
2829 entry.tls_type = 0;
2831 g = mips_elf_got_for_ibfd (gg, ibfd);
2832 if (g == NULL)
2834 g = mips_elf_got_for_ibfd (gg, abfd);
2835 BFD_ASSERT (g != NULL);
2838 /* We might have a symbol, H, if it has been forced local. Use the
2839 global entry then. It doesn't matter whether an entry is local
2840 or global for TLS, since the dynamic linker does not
2841 automatically relocate TLS GOT entries. */
2842 BFD_ASSERT (h == NULL || h->root.forced_local);
2843 if (TLS_RELOC_P (r_type))
2845 struct mips_got_entry *p;
2847 entry.abfd = ibfd;
2848 if (r_type == R_MIPS_TLS_LDM)
2850 entry.tls_type = GOT_TLS_LDM;
2851 entry.symndx = 0;
2852 entry.d.addend = 0;
2854 else if (h == NULL)
2856 entry.symndx = r_symndx;
2857 entry.d.addend = 0;
2859 else
2860 entry.d.h = h;
2862 p = (struct mips_got_entry *)
2863 htab_find (g->got_entries, &entry);
2865 BFD_ASSERT (p);
2866 return p;
2869 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
2870 INSERT);
2871 if (*loc)
2872 return *loc;
2874 entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
2875 entry.tls_type = 0;
2877 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
2879 if (! *loc)
2880 return NULL;
2882 memcpy (*loc, &entry, sizeof entry);
2884 if (g->assigned_gotno > g->local_gotno)
2886 (*loc)->gotidx = -1;
2887 /* We didn't allocate enough space in the GOT. */
2888 (*_bfd_error_handler)
2889 (_("not enough GOT space for local GOT entries"));
2890 bfd_set_error (bfd_error_bad_value);
2891 return NULL;
2894 MIPS_ELF_PUT_WORD (abfd, value,
2895 (sgot->contents + entry.gotidx));
2897 /* These GOT entries need a dynamic relocation on VxWorks. */
2898 if (htab->is_vxworks)
2900 Elf_Internal_Rela outrel;
2901 asection *s;
2902 bfd_byte *loc;
2903 bfd_vma got_address;
2905 s = mips_elf_rel_dyn_section (info, FALSE);
2906 got_address = (sgot->output_section->vma
2907 + sgot->output_offset
2908 + entry.gotidx);
2910 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
2911 outrel.r_offset = got_address;
2912 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
2913 outrel.r_addend = value;
2914 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
2917 return *loc;
2920 /* Sort the dynamic symbol table so that symbols that need GOT entries
2921 appear towards the end. This reduces the amount of GOT space
2922 required. MAX_LOCAL is used to set the number of local symbols
2923 known to be in the dynamic symbol table. During
2924 _bfd_mips_elf_size_dynamic_sections, this value is 1. Afterward, the
2925 section symbols are added and the count is higher. */
2927 static bfd_boolean
2928 mips_elf_sort_hash_table (struct bfd_link_info *info, unsigned long max_local)
2930 struct mips_elf_hash_sort_data hsd;
2931 struct mips_got_info *g;
2932 bfd *dynobj;
2934 dynobj = elf_hash_table (info)->dynobj;
2936 g = mips_elf_got_info (dynobj, NULL);
2938 hsd.low = NULL;
2939 hsd.max_unref_got_dynindx =
2940 hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount
2941 /* In the multi-got case, assigned_gotno of the master got_info
2942 indicate the number of entries that aren't referenced in the
2943 primary GOT, but that must have entries because there are
2944 dynamic relocations that reference it. Since they aren't
2945 referenced, we move them to the end of the GOT, so that they
2946 don't prevent other entries that are referenced from getting
2947 too large offsets. */
2948 - (g->next ? g->assigned_gotno : 0);
2949 hsd.max_non_got_dynindx = max_local;
2950 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
2951 elf_hash_table (info)),
2952 mips_elf_sort_hash_table_f,
2953 &hsd);
2955 /* There should have been enough room in the symbol table to
2956 accommodate both the GOT and non-GOT symbols. */
2957 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
2958 BFD_ASSERT ((unsigned long)hsd.max_unref_got_dynindx
2959 <= elf_hash_table (info)->dynsymcount);
2961 /* Now we know which dynamic symbol has the lowest dynamic symbol
2962 table index in the GOT. */
2963 g->global_gotsym = hsd.low;
2965 return TRUE;
2968 /* If H needs a GOT entry, assign it the highest available dynamic
2969 index. Otherwise, assign it the lowest available dynamic
2970 index. */
2972 static bfd_boolean
2973 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
2975 struct mips_elf_hash_sort_data *hsd = data;
2977 if (h->root.root.type == bfd_link_hash_warning)
2978 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
2980 /* Symbols without dynamic symbol table entries aren't interesting
2981 at all. */
2982 if (h->root.dynindx == -1)
2983 return TRUE;
2985 /* Global symbols that need GOT entries that are not explicitly
2986 referenced are marked with got offset 2. Those that are
2987 referenced get a 1, and those that don't need GOT entries get
2988 -1. Forced local symbols may also be marked with got offset 1,
2989 but are never given global GOT entries. */
2990 if (h->root.got.offset == 2)
2992 BFD_ASSERT (h->tls_type == GOT_NORMAL);
2994 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
2995 hsd->low = (struct elf_link_hash_entry *) h;
2996 h->root.dynindx = hsd->max_unref_got_dynindx++;
2998 else if (h->root.got.offset != 1 || h->forced_local)
2999 h->root.dynindx = hsd->max_non_got_dynindx++;
3000 else
3002 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3004 h->root.dynindx = --hsd->min_got_dynindx;
3005 hsd->low = (struct elf_link_hash_entry *) h;
3008 return TRUE;
3011 /* If H is a symbol that needs a global GOT entry, but has a dynamic
3012 symbol table index lower than any we've seen to date, record it for
3013 posterity. */
3015 static bfd_boolean
3016 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3017 bfd *abfd, struct bfd_link_info *info,
3018 struct mips_got_info *g,
3019 unsigned char tls_flag)
3021 struct mips_got_entry entry, **loc;
3023 /* A global symbol in the GOT must also be in the dynamic symbol
3024 table. */
3025 if (h->dynindx == -1)
3027 switch (ELF_ST_VISIBILITY (h->other))
3029 case STV_INTERNAL:
3030 case STV_HIDDEN:
3031 _bfd_mips_elf_hide_symbol (info, h, TRUE);
3032 break;
3034 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3035 return FALSE;
3038 /* Make sure we have a GOT to put this entry into. */
3039 BFD_ASSERT (g != NULL);
3041 entry.abfd = abfd;
3042 entry.symndx = -1;
3043 entry.d.h = (struct mips_elf_link_hash_entry *) h;
3044 entry.tls_type = 0;
3046 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3047 INSERT);
3049 /* If we've already marked this entry as needing GOT space, we don't
3050 need to do it again. */
3051 if (*loc)
3053 (*loc)->tls_type |= tls_flag;
3054 return TRUE;
3057 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3059 if (! *loc)
3060 return FALSE;
3062 entry.gotidx = -1;
3063 entry.tls_type = tls_flag;
3065 memcpy (*loc, &entry, sizeof entry);
3067 if (h->got.offset != MINUS_ONE)
3068 return TRUE;
3070 if (tls_flag == 0)
3072 /* By setting this to a value other than -1, we are indicating that
3073 there needs to be a GOT entry for H. Avoid using zero, as the
3074 generic ELF copy_indirect_symbol tests for <= 0. */
3075 h->got.offset = 1;
3076 if (h->forced_local)
3077 g->local_gotno++;
3080 return TRUE;
3083 /* Reserve space in G for a GOT entry containing the value of symbol
3084 SYMNDX in input bfd ABDF, plus ADDEND. */
3086 static bfd_boolean
3087 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
3088 struct mips_got_info *g,
3089 unsigned char tls_flag)
3091 struct mips_got_entry entry, **loc;
3093 entry.abfd = abfd;
3094 entry.symndx = symndx;
3095 entry.d.addend = addend;
3096 entry.tls_type = tls_flag;
3097 loc = (struct mips_got_entry **)
3098 htab_find_slot (g->got_entries, &entry, INSERT);
3100 if (*loc)
3102 if (tls_flag == GOT_TLS_GD && !((*loc)->tls_type & GOT_TLS_GD))
3104 g->tls_gotno += 2;
3105 (*loc)->tls_type |= tls_flag;
3107 else if (tls_flag == GOT_TLS_IE && !((*loc)->tls_type & GOT_TLS_IE))
3109 g->tls_gotno += 1;
3110 (*loc)->tls_type |= tls_flag;
3112 return TRUE;
3115 if (tls_flag != 0)
3117 entry.gotidx = -1;
3118 entry.tls_type = tls_flag;
3119 if (tls_flag == GOT_TLS_IE)
3120 g->tls_gotno += 1;
3121 else if (tls_flag == GOT_TLS_GD)
3122 g->tls_gotno += 2;
3123 else if (g->tls_ldm_offset == MINUS_ONE)
3125 g->tls_ldm_offset = MINUS_TWO;
3126 g->tls_gotno += 2;
3129 else
3131 entry.gotidx = g->local_gotno++;
3132 entry.tls_type = 0;
3135 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3137 if (! *loc)
3138 return FALSE;
3140 memcpy (*loc, &entry, sizeof entry);
3142 return TRUE;
3145 /* Return the maximum number of GOT page entries required for RANGE. */
3147 static bfd_vma
3148 mips_elf_pages_for_range (const struct mips_got_page_range *range)
3150 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
3153 /* Record that ABFD has a page relocation against symbol SYMNDX and
3154 that ADDEND is the addend for that relocation. G is the GOT
3155 information. This function creates an upper bound on the number of
3156 GOT slots required; no attempt is made to combine references to
3157 non-overridable global symbols across multiple input files. */
3159 static bfd_boolean
3160 mips_elf_record_got_page_entry (bfd *abfd, long symndx, bfd_signed_vma addend,
3161 struct mips_got_info *g)
3163 struct mips_got_page_entry lookup, *entry;
3164 struct mips_got_page_range **range_ptr, *range;
3165 bfd_vma old_pages, new_pages;
3166 void **loc;
3168 /* Find the mips_got_page_entry hash table entry for this symbol. */
3169 lookup.abfd = abfd;
3170 lookup.symndx = symndx;
3171 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
3172 if (loc == NULL)
3173 return FALSE;
3175 /* Create a mips_got_page_entry if this is the first time we've
3176 seen the symbol. */
3177 entry = (struct mips_got_page_entry *) *loc;
3178 if (!entry)
3180 entry = bfd_alloc (abfd, sizeof (*entry));
3181 if (!entry)
3182 return FALSE;
3184 entry->abfd = abfd;
3185 entry->symndx = symndx;
3186 entry->ranges = NULL;
3187 entry->num_pages = 0;
3188 *loc = entry;
3191 /* Skip over ranges whose maximum extent cannot share a page entry
3192 with ADDEND. */
3193 range_ptr = &entry->ranges;
3194 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
3195 range_ptr = &(*range_ptr)->next;
3197 /* If we scanned to the end of the list, or found a range whose
3198 minimum extent cannot share a page entry with ADDEND, create
3199 a new singleton range. */
3200 range = *range_ptr;
3201 if (!range || addend < range->min_addend - 0xffff)
3203 range = bfd_alloc (abfd, sizeof (*range));
3204 if (!range)
3205 return FALSE;
3207 range->next = *range_ptr;
3208 range->min_addend = addend;
3209 range->max_addend = addend;
3211 *range_ptr = range;
3212 entry->num_pages++;
3213 g->page_gotno++;
3214 return TRUE;
3217 /* Remember how many pages the old range contributed. */
3218 old_pages = mips_elf_pages_for_range (range);
3220 /* Update the ranges. */
3221 if (addend < range->min_addend)
3222 range->min_addend = addend;
3223 else if (addend > range->max_addend)
3225 if (range->next && addend >= range->next->min_addend - 0xffff)
3227 old_pages += mips_elf_pages_for_range (range->next);
3228 range->max_addend = range->next->max_addend;
3229 range->next = range->next->next;
3231 else
3232 range->max_addend = addend;
3235 /* Record any change in the total estimate. */
3236 new_pages = mips_elf_pages_for_range (range);
3237 if (old_pages != new_pages)
3239 entry->num_pages += new_pages - old_pages;
3240 g->page_gotno += new_pages - old_pages;
3243 return TRUE;
3246 /* Compute the hash value of the bfd in a bfd2got hash entry. */
3248 static hashval_t
3249 mips_elf_bfd2got_entry_hash (const void *entry_)
3251 const struct mips_elf_bfd2got_hash *entry
3252 = (struct mips_elf_bfd2got_hash *)entry_;
3254 return entry->bfd->id;
3257 /* Check whether two hash entries have the same bfd. */
3259 static int
3260 mips_elf_bfd2got_entry_eq (const void *entry1, const void *entry2)
3262 const struct mips_elf_bfd2got_hash *e1
3263 = (const struct mips_elf_bfd2got_hash *)entry1;
3264 const struct mips_elf_bfd2got_hash *e2
3265 = (const struct mips_elf_bfd2got_hash *)entry2;
3267 return e1->bfd == e2->bfd;
3270 /* In a multi-got link, determine the GOT to be used for IBFD. G must
3271 be the master GOT data. */
3273 static struct mips_got_info *
3274 mips_elf_got_for_ibfd (struct mips_got_info *g, bfd *ibfd)
3276 struct mips_elf_bfd2got_hash e, *p;
3278 if (! g->bfd2got)
3279 return g;
3281 e.bfd = ibfd;
3282 p = htab_find (g->bfd2got, &e);
3283 return p ? p->g : NULL;
3286 /* Use BFD2GOT to find ABFD's got entry, creating one if none exists.
3287 Return NULL if an error occured. */
3289 static struct mips_got_info *
3290 mips_elf_get_got_for_bfd (struct htab *bfd2got, bfd *output_bfd,
3291 bfd *input_bfd)
3293 struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
3294 struct mips_got_info *g;
3295 void **bfdgotp;
3297 bfdgot_entry.bfd = input_bfd;
3298 bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
3299 bfdgot = (struct mips_elf_bfd2got_hash *) *bfdgotp;
3301 if (bfdgot == NULL)
3303 bfdgot = ((struct mips_elf_bfd2got_hash *)
3304 bfd_alloc (output_bfd, sizeof (struct mips_elf_bfd2got_hash)));
3305 if (bfdgot == NULL)
3306 return NULL;
3308 *bfdgotp = bfdgot;
3310 g = ((struct mips_got_info *)
3311 bfd_alloc (output_bfd, sizeof (struct mips_got_info)));
3312 if (g == NULL)
3313 return NULL;
3315 bfdgot->bfd = input_bfd;
3316 bfdgot->g = g;
3318 g->global_gotsym = NULL;
3319 g->global_gotno = 0;
3320 g->local_gotno = 0;
3321 g->page_gotno = 0;
3322 g->assigned_gotno = -1;
3323 g->tls_gotno = 0;
3324 g->tls_assigned_gotno = 0;
3325 g->tls_ldm_offset = MINUS_ONE;
3326 g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
3327 mips_elf_multi_got_entry_eq, NULL);
3328 if (g->got_entries == NULL)
3329 return NULL;
3331 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
3332 mips_got_page_entry_eq, NULL);
3333 if (g->got_page_entries == NULL)
3334 return NULL;
3336 g->bfd2got = NULL;
3337 g->next = NULL;
3340 return bfdgot->g;
3343 /* A htab_traverse callback for the entries in the master got.
3344 Create one separate got for each bfd that has entries in the global
3345 got, such that we can tell how many local and global entries each
3346 bfd requires. */
3348 static int
3349 mips_elf_make_got_per_bfd (void **entryp, void *p)
3351 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3352 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
3353 struct mips_got_info *g;
3355 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
3356 if (g == NULL)
3358 arg->obfd = NULL;
3359 return 0;
3362 /* Insert the GOT entry in the bfd's got entry hash table. */
3363 entryp = htab_find_slot (g->got_entries, entry, INSERT);
3364 if (*entryp != NULL)
3365 return 1;
3367 *entryp = entry;
3369 if (entry->tls_type)
3371 if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
3372 g->tls_gotno += 2;
3373 if (entry->tls_type & GOT_TLS_IE)
3374 g->tls_gotno += 1;
3376 else if (entry->symndx >= 0 || entry->d.h->forced_local)
3377 ++g->local_gotno;
3378 else
3379 ++g->global_gotno;
3381 return 1;
3384 /* A htab_traverse callback for the page entries in the master got.
3385 Associate each page entry with the bfd's got. */
3387 static int
3388 mips_elf_make_got_pages_per_bfd (void **entryp, void *p)
3390 struct mips_got_page_entry *entry = (struct mips_got_page_entry *) *entryp;
3391 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *) p;
3392 struct mips_got_info *g;
3394 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
3395 if (g == NULL)
3397 arg->obfd = NULL;
3398 return 0;
3401 /* Insert the GOT entry in the bfd's got entry hash table. */
3402 entryp = htab_find_slot (g->got_page_entries, entry, INSERT);
3403 if (*entryp != NULL)
3404 return 1;
3406 *entryp = entry;
3407 g->page_gotno += entry->num_pages;
3408 return 1;
3411 /* Consider merging the got described by BFD2GOT with TO, using the
3412 information given by ARG. Return -1 if this would lead to overflow,
3413 1 if they were merged successfully, and 0 if a merge failed due to
3414 lack of memory. (These values are chosen so that nonnegative return
3415 values can be returned by a htab_traverse callback.) */
3417 static int
3418 mips_elf_merge_got_with (struct mips_elf_bfd2got_hash *bfd2got,
3419 struct mips_got_info *to,
3420 struct mips_elf_got_per_bfd_arg *arg)
3422 struct mips_got_info *from = bfd2got->g;
3423 unsigned int estimate;
3425 /* Work out how many page entries we would need for the combined GOT. */
3426 estimate = arg->max_pages;
3427 if (estimate >= from->page_gotno + to->page_gotno)
3428 estimate = from->page_gotno + to->page_gotno;
3430 /* And conservatively estimate how many local, global and TLS entries
3431 would be needed. */
3432 estimate += (from->local_gotno
3433 + from->global_gotno
3434 + from->tls_gotno
3435 + to->local_gotno
3436 + to->global_gotno
3437 + to->tls_gotno);
3439 /* Bail out if the combined GOT might be too big. */
3440 if (estimate > arg->max_count)
3441 return -1;
3443 /* Commit to the merge. Record that TO is now the bfd for this got. */
3444 bfd2got->g = to;
3446 /* Transfer the bfd's got information from FROM to TO. */
3447 htab_traverse (from->got_entries, mips_elf_make_got_per_bfd, arg);
3448 if (arg->obfd == NULL)
3449 return 0;
3451 htab_traverse (from->got_page_entries, mips_elf_make_got_pages_per_bfd, arg);
3452 if (arg->obfd == NULL)
3453 return 0;
3455 /* We don't have to worry about releasing memory of the actual
3456 got entries, since they're all in the master got_entries hash
3457 table anyway. */
3458 htab_delete (from->got_entries);
3459 htab_delete (from->got_page_entries);
3460 return 1;
3463 /* Attempt to merge gots of different input bfds. Try to use as much
3464 as possible of the primary got, since it doesn't require explicit
3465 dynamic relocations, but don't use bfds that would reference global
3466 symbols out of the addressable range. Failing the primary got,
3467 attempt to merge with the current got, or finish the current got
3468 and then make make the new got current. */
3470 static int
3471 mips_elf_merge_gots (void **bfd2got_, void *p)
3473 struct mips_elf_bfd2got_hash *bfd2got
3474 = (struct mips_elf_bfd2got_hash *)*bfd2got_;
3475 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
3476 struct mips_got_info *g;
3477 unsigned int estimate;
3478 int result;
3480 g = bfd2got->g;
3482 /* Work out the number of page, local and TLS entries. */
3483 estimate = arg->max_pages;
3484 if (estimate > g->page_gotno)
3485 estimate = g->page_gotno;
3486 estimate += g->local_gotno + g->tls_gotno;
3488 /* We place TLS GOT entries after both locals and globals. The globals
3489 for the primary GOT may overflow the normal GOT size limit, so be
3490 sure not to merge a GOT which requires TLS with the primary GOT in that
3491 case. This doesn't affect non-primary GOTs. */
3492 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
3494 if (estimate <= arg->max_count)
3496 /* If we don't have a primary GOT, use it as
3497 a starting point for the primary GOT. */
3498 if (!arg->primary)
3500 arg->primary = bfd2got->g;
3501 return 1;
3504 /* Try merging with the primary GOT. */
3505 result = mips_elf_merge_got_with (bfd2got, arg->primary, arg);
3506 if (result >= 0)
3507 return result;
3510 /* If we can merge with the last-created got, do it. */
3511 if (arg->current)
3513 result = mips_elf_merge_got_with (bfd2got, arg->current, arg);
3514 if (result >= 0)
3515 return result;
3518 /* Well, we couldn't merge, so create a new GOT. Don't check if it
3519 fits; if it turns out that it doesn't, we'll get relocation
3520 overflows anyway. */
3521 g->next = arg->current;
3522 arg->current = g;
3524 return 1;
3527 /* Set the TLS GOT index for the GOT entry in ENTRYP. ENTRYP's NEXT field
3528 is null iff there is just a single GOT. */
3530 static int
3531 mips_elf_initialize_tls_index (void **entryp, void *p)
3533 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3534 struct mips_got_info *g = p;
3535 bfd_vma next_index;
3536 unsigned char tls_type;
3538 /* We're only interested in TLS symbols. */
3539 if (entry->tls_type == 0)
3540 return 1;
3542 next_index = MIPS_ELF_GOT_SIZE (entry->abfd) * (long) g->tls_assigned_gotno;
3544 if (entry->symndx == -1 && g->next == NULL)
3546 /* A type (3) got entry in the single-GOT case. We use the symbol's
3547 hash table entry to track its index. */
3548 if (entry->d.h->tls_type & GOT_TLS_OFFSET_DONE)
3549 return 1;
3550 entry->d.h->tls_type |= GOT_TLS_OFFSET_DONE;
3551 entry->d.h->tls_got_offset = next_index;
3552 tls_type = entry->d.h->tls_type;
3554 else
3556 if (entry->tls_type & GOT_TLS_LDM)
3558 /* There are separate mips_got_entry objects for each input bfd
3559 that requires an LDM entry. Make sure that all LDM entries in
3560 a GOT resolve to the same index. */
3561 if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
3563 entry->gotidx = g->tls_ldm_offset;
3564 return 1;
3566 g->tls_ldm_offset = next_index;
3568 entry->gotidx = next_index;
3569 tls_type = entry->tls_type;
3572 /* Account for the entries we've just allocated. */
3573 if (tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
3574 g->tls_assigned_gotno += 2;
3575 if (tls_type & GOT_TLS_IE)
3576 g->tls_assigned_gotno += 1;
3578 return 1;
3581 /* If passed a NULL mips_got_info in the argument, set the marker used
3582 to tell whether a global symbol needs a got entry (in the primary
3583 got) to the given VALUE.
3585 If passed a pointer G to a mips_got_info in the argument (it must
3586 not be the primary GOT), compute the offset from the beginning of
3587 the (primary) GOT section to the entry in G corresponding to the
3588 global symbol. G's assigned_gotno must contain the index of the
3589 first available global GOT entry in G. VALUE must contain the size
3590 of a GOT entry in bytes. For each global GOT entry that requires a
3591 dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
3592 marked as not eligible for lazy resolution through a function
3593 stub. */
3594 static int
3595 mips_elf_set_global_got_offset (void **entryp, void *p)
3597 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3598 struct mips_elf_set_global_got_offset_arg *arg
3599 = (struct mips_elf_set_global_got_offset_arg *)p;
3600 struct mips_got_info *g = arg->g;
3602 if (g && entry->tls_type != GOT_NORMAL)
3603 arg->needed_relocs +=
3604 mips_tls_got_relocs (arg->info, entry->tls_type,
3605 entry->symndx == -1 ? &entry->d.h->root : NULL);
3607 if (entry->abfd != NULL && entry->symndx == -1
3608 && entry->d.h->root.dynindx != -1
3609 && !entry->d.h->forced_local
3610 && entry->d.h->tls_type == GOT_NORMAL)
3612 if (g)
3614 BFD_ASSERT (g->global_gotsym == NULL);
3616 entry->gotidx = arg->value * (long) g->assigned_gotno++;
3617 if (arg->info->shared
3618 || (elf_hash_table (arg->info)->dynamic_sections_created
3619 && entry->d.h->root.def_dynamic
3620 && !entry->d.h->root.def_regular))
3621 ++arg->needed_relocs;
3623 else
3624 entry->d.h->root.got.offset = arg->value;
3627 return 1;
3630 /* Mark any global symbols referenced in the GOT we are iterating over
3631 as inelligible for lazy resolution stubs. */
3632 static int
3633 mips_elf_set_no_stub (void **entryp, void *p ATTRIBUTE_UNUSED)
3635 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3637 if (entry->abfd != NULL
3638 && entry->symndx == -1
3639 && entry->d.h->root.dynindx != -1)
3640 entry->d.h->no_fn_stub = TRUE;
3642 return 1;
3645 /* Follow indirect and warning hash entries so that each got entry
3646 points to the final symbol definition. P must point to a pointer
3647 to the hash table we're traversing. Since this traversal may
3648 modify the hash table, we set this pointer to NULL to indicate
3649 we've made a potentially-destructive change to the hash table, so
3650 the traversal must be restarted. */
3651 static int
3652 mips_elf_resolve_final_got_entry (void **entryp, void *p)
3654 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3655 htab_t got_entries = *(htab_t *)p;
3657 if (entry->abfd != NULL && entry->symndx == -1)
3659 struct mips_elf_link_hash_entry *h = entry->d.h;
3661 while (h->root.root.type == bfd_link_hash_indirect
3662 || h->root.root.type == bfd_link_hash_warning)
3663 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3665 if (entry->d.h == h)
3666 return 1;
3668 entry->d.h = h;
3670 /* If we can't find this entry with the new bfd hash, re-insert
3671 it, and get the traversal restarted. */
3672 if (! htab_find (got_entries, entry))
3674 htab_clear_slot (got_entries, entryp);
3675 entryp = htab_find_slot (got_entries, entry, INSERT);
3676 if (! *entryp)
3677 *entryp = entry;
3678 /* Abort the traversal, since the whole table may have
3679 moved, and leave it up to the parent to restart the
3680 process. */
3681 *(htab_t *)p = NULL;
3682 return 0;
3684 /* We might want to decrement the global_gotno count, but it's
3685 either too early or too late for that at this point. */
3688 return 1;
3691 /* Turn indirect got entries in a got_entries table into their final
3692 locations. */
3693 static void
3694 mips_elf_resolve_final_got_entries (struct mips_got_info *g)
3696 htab_t got_entries;
3700 got_entries = g->got_entries;
3702 htab_traverse (got_entries,
3703 mips_elf_resolve_final_got_entry,
3704 &got_entries);
3706 while (got_entries == NULL);
3709 /* Return the offset of an input bfd IBFD's GOT from the beginning of
3710 the primary GOT. */
3711 static bfd_vma
3712 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
3714 if (g->bfd2got == NULL)
3715 return 0;
3717 g = mips_elf_got_for_ibfd (g, ibfd);
3718 if (! g)
3719 return 0;
3721 BFD_ASSERT (g->next);
3723 g = g->next;
3725 return (g->local_gotno + g->global_gotno + g->tls_gotno)
3726 * MIPS_ELF_GOT_SIZE (abfd);
3729 /* Turn a single GOT that is too big for 16-bit addressing into
3730 a sequence of GOTs, each one 16-bit addressable. */
3732 static bfd_boolean
3733 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
3734 struct mips_got_info *g, asection *got,
3735 bfd_size_type pages)
3737 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
3738 struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
3739 struct mips_got_info *gg;
3740 unsigned int assign;
3742 g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
3743 mips_elf_bfd2got_entry_eq, NULL);
3744 if (g->bfd2got == NULL)
3745 return FALSE;
3747 got_per_bfd_arg.bfd2got = g->bfd2got;
3748 got_per_bfd_arg.obfd = abfd;
3749 got_per_bfd_arg.info = info;
3751 /* Count how many GOT entries each input bfd requires, creating a
3752 map from bfd to got info while at that. */
3753 htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
3754 if (got_per_bfd_arg.obfd == NULL)
3755 return FALSE;
3757 /* Also count how many page entries each input bfd requires. */
3758 htab_traverse (g->got_page_entries, mips_elf_make_got_pages_per_bfd,
3759 &got_per_bfd_arg);
3760 if (got_per_bfd_arg.obfd == NULL)
3761 return FALSE;
3763 got_per_bfd_arg.current = NULL;
3764 got_per_bfd_arg.primary = NULL;
3765 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
3766 / MIPS_ELF_GOT_SIZE (abfd))
3767 - MIPS_RESERVED_GOTNO (info));
3768 got_per_bfd_arg.max_pages = pages;
3769 /* The number of globals that will be included in the primary GOT.
3770 See the calls to mips_elf_set_global_got_offset below for more
3771 information. */
3772 got_per_bfd_arg.global_count = g->global_gotno;
3774 /* Try to merge the GOTs of input bfds together, as long as they
3775 don't seem to exceed the maximum GOT size, choosing one of them
3776 to be the primary GOT. */
3777 htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
3778 if (got_per_bfd_arg.obfd == NULL)
3779 return FALSE;
3781 /* If we do not find any suitable primary GOT, create an empty one. */
3782 if (got_per_bfd_arg.primary == NULL)
3784 g->next = (struct mips_got_info *)
3785 bfd_alloc (abfd, sizeof (struct mips_got_info));
3786 if (g->next == NULL)
3787 return FALSE;
3789 g->next->global_gotsym = NULL;
3790 g->next->global_gotno = 0;
3791 g->next->local_gotno = 0;
3792 g->next->page_gotno = 0;
3793 g->next->tls_gotno = 0;
3794 g->next->assigned_gotno = 0;
3795 g->next->tls_assigned_gotno = 0;
3796 g->next->tls_ldm_offset = MINUS_ONE;
3797 g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
3798 mips_elf_multi_got_entry_eq,
3799 NULL);
3800 if (g->next->got_entries == NULL)
3801 return FALSE;
3802 g->next->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
3803 mips_got_page_entry_eq,
3804 NULL);
3805 if (g->next->got_page_entries == NULL)
3806 return FALSE;
3807 g->next->bfd2got = NULL;
3809 else
3810 g->next = got_per_bfd_arg.primary;
3811 g->next->next = got_per_bfd_arg.current;
3813 /* GG is now the master GOT, and G is the primary GOT. */
3814 gg = g;
3815 g = g->next;
3817 /* Map the output bfd to the primary got. That's what we're going
3818 to use for bfds that use GOT16 or GOT_PAGE relocations that we
3819 didn't mark in check_relocs, and we want a quick way to find it.
3820 We can't just use gg->next because we're going to reverse the
3821 list. */
3823 struct mips_elf_bfd2got_hash *bfdgot;
3824 void **bfdgotp;
3826 bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
3827 (abfd, sizeof (struct mips_elf_bfd2got_hash));
3829 if (bfdgot == NULL)
3830 return FALSE;
3832 bfdgot->bfd = abfd;
3833 bfdgot->g = g;
3834 bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
3836 BFD_ASSERT (*bfdgotp == NULL);
3837 *bfdgotp = bfdgot;
3840 /* The IRIX dynamic linker requires every symbol that is referenced
3841 in a dynamic relocation to be present in the primary GOT, so
3842 arrange for them to appear after those that are actually
3843 referenced.
3845 GNU/Linux could very well do without it, but it would slow down
3846 the dynamic linker, since it would have to resolve every dynamic
3847 symbol referenced in other GOTs more than once, without help from
3848 the cache. Also, knowing that every external symbol has a GOT
3849 helps speed up the resolution of local symbols too, so GNU/Linux
3850 follows IRIX's practice.
3852 The number 2 is used by mips_elf_sort_hash_table_f to count
3853 global GOT symbols that are unreferenced in the primary GOT, with
3854 an initial dynamic index computed from gg->assigned_gotno, where
3855 the number of unreferenced global entries in the primary GOT is
3856 preserved. */
3857 if (1)
3859 gg->assigned_gotno = gg->global_gotno - g->global_gotno;
3860 g->global_gotno = gg->global_gotno;
3861 set_got_offset_arg.value = 2;
3863 else
3865 /* This could be used for dynamic linkers that don't optimize
3866 symbol resolution while applying relocations so as to use
3867 primary GOT entries or assuming the symbol is locally-defined.
3868 With this code, we assign lower dynamic indices to global
3869 symbols that are not referenced in the primary GOT, so that
3870 their entries can be omitted. */
3871 gg->assigned_gotno = 0;
3872 set_got_offset_arg.value = -1;
3875 /* Reorder dynamic symbols as described above (which behavior
3876 depends on the setting of VALUE). */
3877 set_got_offset_arg.g = NULL;
3878 htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
3879 &set_got_offset_arg);
3880 set_got_offset_arg.value = 1;
3881 htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
3882 &set_got_offset_arg);
3883 if (! mips_elf_sort_hash_table (info, 1))
3884 return FALSE;
3886 /* Now go through the GOTs assigning them offset ranges.
3887 [assigned_gotno, local_gotno[ will be set to the range of local
3888 entries in each GOT. We can then compute the end of a GOT by
3889 adding local_gotno to global_gotno. We reverse the list and make
3890 it circular since then we'll be able to quickly compute the
3891 beginning of a GOT, by computing the end of its predecessor. To
3892 avoid special cases for the primary GOT, while still preserving
3893 assertions that are valid for both single- and multi-got links,
3894 we arrange for the main got struct to have the right number of
3895 global entries, but set its local_gotno such that the initial
3896 offset of the primary GOT is zero. Remember that the primary GOT
3897 will become the last item in the circular linked list, so it
3898 points back to the master GOT. */
3899 gg->local_gotno = -g->global_gotno;
3900 gg->global_gotno = g->global_gotno;
3901 gg->tls_gotno = 0;
3902 assign = 0;
3903 gg->next = gg;
3907 struct mips_got_info *gn;
3909 assign += MIPS_RESERVED_GOTNO (info);
3910 g->assigned_gotno = assign;
3911 g->local_gotno += assign;
3912 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
3913 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
3915 /* Take g out of the direct list, and push it onto the reversed
3916 list that gg points to. g->next is guaranteed to be nonnull after
3917 this operation, as required by mips_elf_initialize_tls_index. */
3918 gn = g->next;
3919 g->next = gg->next;
3920 gg->next = g;
3922 /* Set up any TLS entries. We always place the TLS entries after
3923 all non-TLS entries. */
3924 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
3925 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
3927 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
3928 g = gn;
3930 /* Mark global symbols in every non-primary GOT as ineligible for
3931 stubs. */
3932 if (g)
3933 htab_traverse (g->got_entries, mips_elf_set_no_stub, NULL);
3935 while (g);
3937 got->size = (gg->next->local_gotno
3938 + gg->next->global_gotno
3939 + gg->next->tls_gotno) * MIPS_ELF_GOT_SIZE (abfd);
3941 return TRUE;
3945 /* Returns the first relocation of type r_type found, beginning with
3946 RELOCATION. RELEND is one-past-the-end of the relocation table. */
3948 static const Elf_Internal_Rela *
3949 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
3950 const Elf_Internal_Rela *relocation,
3951 const Elf_Internal_Rela *relend)
3953 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
3955 while (relocation < relend)
3957 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
3958 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
3959 return relocation;
3961 ++relocation;
3964 /* We didn't find it. */
3965 return NULL;
3968 /* Return whether a relocation is against a local symbol. */
3970 static bfd_boolean
3971 mips_elf_local_relocation_p (bfd *input_bfd,
3972 const Elf_Internal_Rela *relocation,
3973 asection **local_sections,
3974 bfd_boolean check_forced)
3976 unsigned long r_symndx;
3977 Elf_Internal_Shdr *symtab_hdr;
3978 struct mips_elf_link_hash_entry *h;
3979 size_t extsymoff;
3981 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
3982 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3983 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
3985 if (r_symndx < extsymoff)
3986 return TRUE;
3987 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
3988 return TRUE;
3990 if (check_forced)
3992 /* Look up the hash table to check whether the symbol
3993 was forced local. */
3994 h = (struct mips_elf_link_hash_entry *)
3995 elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
3996 /* Find the real hash-table entry for this symbol. */
3997 while (h->root.root.type == bfd_link_hash_indirect
3998 || h->root.root.type == bfd_link_hash_warning)
3999 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4000 if (h->root.forced_local)
4001 return TRUE;
4004 return FALSE;
4007 /* Sign-extend VALUE, which has the indicated number of BITS. */
4009 bfd_vma
4010 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
4012 if (value & ((bfd_vma) 1 << (bits - 1)))
4013 /* VALUE is negative. */
4014 value |= ((bfd_vma) - 1) << bits;
4016 return value;
4019 /* Return non-zero if the indicated VALUE has overflowed the maximum
4020 range expressible by a signed number with the indicated number of
4021 BITS. */
4023 static bfd_boolean
4024 mips_elf_overflow_p (bfd_vma value, int bits)
4026 bfd_signed_vma svalue = (bfd_signed_vma) value;
4028 if (svalue > (1 << (bits - 1)) - 1)
4029 /* The value is too big. */
4030 return TRUE;
4031 else if (svalue < -(1 << (bits - 1)))
4032 /* The value is too small. */
4033 return TRUE;
4035 /* All is well. */
4036 return FALSE;
4039 /* Calculate the %high function. */
4041 static bfd_vma
4042 mips_elf_high (bfd_vma value)
4044 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4047 /* Calculate the %higher function. */
4049 static bfd_vma
4050 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
4052 #ifdef BFD64
4053 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4054 #else
4055 abort ();
4056 return MINUS_ONE;
4057 #endif
4060 /* Calculate the %highest function. */
4062 static bfd_vma
4063 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
4065 #ifdef BFD64
4066 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
4067 #else
4068 abort ();
4069 return MINUS_ONE;
4070 #endif
4073 /* Create the .compact_rel section. */
4075 static bfd_boolean
4076 mips_elf_create_compact_rel_section
4077 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
4079 flagword flags;
4080 register asection *s;
4082 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
4084 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
4085 | SEC_READONLY);
4087 s = bfd_make_section_with_flags (abfd, ".compact_rel", flags);
4088 if (s == NULL
4089 || ! bfd_set_section_alignment (abfd, s,
4090 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
4091 return FALSE;
4093 s->size = sizeof (Elf32_External_compact_rel);
4096 return TRUE;
4099 /* Create the .got section to hold the global offset table. */
4101 static bfd_boolean
4102 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info,
4103 bfd_boolean maybe_exclude)
4105 flagword flags;
4106 register asection *s;
4107 struct elf_link_hash_entry *h;
4108 struct bfd_link_hash_entry *bh;
4109 struct mips_got_info *g;
4110 bfd_size_type amt;
4111 struct mips_elf_link_hash_table *htab;
4113 htab = mips_elf_hash_table (info);
4115 /* This function may be called more than once. */
4116 s = mips_elf_got_section (abfd, TRUE);
4117 if (s)
4119 if (! maybe_exclude)
4120 s->flags &= ~SEC_EXCLUDE;
4121 return TRUE;
4124 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4125 | SEC_LINKER_CREATED);
4127 if (maybe_exclude)
4128 flags |= SEC_EXCLUDE;
4130 /* We have to use an alignment of 2**4 here because this is hardcoded
4131 in the function stub generation and in the linker script. */
4132 s = bfd_make_section_with_flags (abfd, ".got", flags);
4133 if (s == NULL
4134 || ! bfd_set_section_alignment (abfd, s, 4))
4135 return FALSE;
4137 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
4138 linker script because we don't want to define the symbol if we
4139 are not creating a global offset table. */
4140 bh = NULL;
4141 if (! (_bfd_generic_link_add_one_symbol
4142 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
4143 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
4144 return FALSE;
4146 h = (struct elf_link_hash_entry *) bh;
4147 h->non_elf = 0;
4148 h->def_regular = 1;
4149 h->type = STT_OBJECT;
4150 elf_hash_table (info)->hgot = h;
4152 if (info->shared
4153 && ! bfd_elf_link_record_dynamic_symbol (info, h))
4154 return FALSE;
4156 amt = sizeof (struct mips_got_info);
4157 g = bfd_alloc (abfd, amt);
4158 if (g == NULL)
4159 return FALSE;
4160 g->global_gotsym = NULL;
4161 g->global_gotno = 0;
4162 g->tls_gotno = 0;
4163 g->local_gotno = MIPS_RESERVED_GOTNO (info);
4164 g->page_gotno = 0;
4165 g->assigned_gotno = MIPS_RESERVED_GOTNO (info);
4166 g->bfd2got = NULL;
4167 g->next = NULL;
4168 g->tls_ldm_offset = MINUS_ONE;
4169 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
4170 mips_elf_got_entry_eq, NULL);
4171 if (g->got_entries == NULL)
4172 return FALSE;
4173 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4174 mips_got_page_entry_eq, NULL);
4175 if (g->got_page_entries == NULL)
4176 return FALSE;
4177 mips_elf_section_data (s)->u.got_info = g;
4178 mips_elf_section_data (s)->elf.this_hdr.sh_flags
4179 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
4181 /* VxWorks also needs a .got.plt section. */
4182 if (htab->is_vxworks)
4184 s = bfd_make_section_with_flags (abfd, ".got.plt",
4185 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
4186 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4187 if (s == NULL || !bfd_set_section_alignment (abfd, s, 4))
4188 return FALSE;
4190 htab->sgotplt = s;
4192 return TRUE;
4195 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
4196 __GOTT_INDEX__ symbols. These symbols are only special for
4197 shared objects; they are not used in executables. */
4199 static bfd_boolean
4200 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
4202 return (mips_elf_hash_table (info)->is_vxworks
4203 && info->shared
4204 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
4205 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
4208 /* Calculate the value produced by the RELOCATION (which comes from
4209 the INPUT_BFD). The ADDEND is the addend to use for this
4210 RELOCATION; RELOCATION->R_ADDEND is ignored.
4212 The result of the relocation calculation is stored in VALUEP.
4213 REQUIRE_JALXP indicates whether or not the opcode used with this
4214 relocation must be JALX.
4216 This function returns bfd_reloc_continue if the caller need take no
4217 further action regarding this relocation, bfd_reloc_notsupported if
4218 something goes dramatically wrong, bfd_reloc_overflow if an
4219 overflow occurs, and bfd_reloc_ok to indicate success. */
4221 static bfd_reloc_status_type
4222 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
4223 asection *input_section,
4224 struct bfd_link_info *info,
4225 const Elf_Internal_Rela *relocation,
4226 bfd_vma addend, reloc_howto_type *howto,
4227 Elf_Internal_Sym *local_syms,
4228 asection **local_sections, bfd_vma *valuep,
4229 const char **namep, bfd_boolean *require_jalxp,
4230 bfd_boolean save_addend)
4232 /* The eventual value we will return. */
4233 bfd_vma value;
4234 /* The address of the symbol against which the relocation is
4235 occurring. */
4236 bfd_vma symbol = 0;
4237 /* The final GP value to be used for the relocatable, executable, or
4238 shared object file being produced. */
4239 bfd_vma gp;
4240 /* The place (section offset or address) of the storage unit being
4241 relocated. */
4242 bfd_vma p;
4243 /* The value of GP used to create the relocatable object. */
4244 bfd_vma gp0;
4245 /* The offset into the global offset table at which the address of
4246 the relocation entry symbol, adjusted by the addend, resides
4247 during execution. */
4248 bfd_vma g = MINUS_ONE;
4249 /* The section in which the symbol referenced by the relocation is
4250 located. */
4251 asection *sec = NULL;
4252 struct mips_elf_link_hash_entry *h = NULL;
4253 /* TRUE if the symbol referred to by this relocation is a local
4254 symbol. */
4255 bfd_boolean local_p, was_local_p;
4256 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
4257 bfd_boolean gp_disp_p = FALSE;
4258 /* TRUE if the symbol referred to by this relocation is
4259 "__gnu_local_gp". */
4260 bfd_boolean gnu_local_gp_p = FALSE;
4261 Elf_Internal_Shdr *symtab_hdr;
4262 size_t extsymoff;
4263 unsigned long r_symndx;
4264 int r_type;
4265 /* TRUE if overflow occurred during the calculation of the
4266 relocation value. */
4267 bfd_boolean overflowed_p;
4268 /* TRUE if this relocation refers to a MIPS16 function. */
4269 bfd_boolean target_is_16_bit_code_p = FALSE;
4270 struct mips_elf_link_hash_table *htab;
4271 bfd *dynobj;
4273 dynobj = elf_hash_table (info)->dynobj;
4274 htab = mips_elf_hash_table (info);
4276 /* Parse the relocation. */
4277 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4278 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
4279 p = (input_section->output_section->vma
4280 + input_section->output_offset
4281 + relocation->r_offset);
4283 /* Assume that there will be no overflow. */
4284 overflowed_p = FALSE;
4286 /* Figure out whether or not the symbol is local, and get the offset
4287 used in the array of hash table entries. */
4288 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4289 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
4290 local_sections, FALSE);
4291 was_local_p = local_p;
4292 if (! elf_bad_symtab (input_bfd))
4293 extsymoff = symtab_hdr->sh_info;
4294 else
4296 /* The symbol table does not follow the rule that local symbols
4297 must come before globals. */
4298 extsymoff = 0;
4301 /* Figure out the value of the symbol. */
4302 if (local_p)
4304 Elf_Internal_Sym *sym;
4306 sym = local_syms + r_symndx;
4307 sec = local_sections[r_symndx];
4309 symbol = sec->output_section->vma + sec->output_offset;
4310 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
4311 || (sec->flags & SEC_MERGE))
4312 symbol += sym->st_value;
4313 if ((sec->flags & SEC_MERGE)
4314 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4316 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
4317 addend -= symbol;
4318 addend += sec->output_section->vma + sec->output_offset;
4321 /* MIPS16 text labels should be treated as odd. */
4322 if (ELF_ST_IS_MIPS16 (sym->st_other))
4323 ++symbol;
4325 /* Record the name of this symbol, for our caller. */
4326 *namep = bfd_elf_string_from_elf_section (input_bfd,
4327 symtab_hdr->sh_link,
4328 sym->st_name);
4329 if (*namep == '\0')
4330 *namep = bfd_section_name (input_bfd, sec);
4332 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
4334 else
4336 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
4338 /* For global symbols we look up the symbol in the hash-table. */
4339 h = ((struct mips_elf_link_hash_entry *)
4340 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
4341 /* Find the real hash-table entry for this symbol. */
4342 while (h->root.root.type == bfd_link_hash_indirect
4343 || h->root.root.type == bfd_link_hash_warning)
4344 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4346 /* Record the name of this symbol, for our caller. */
4347 *namep = h->root.root.root.string;
4349 /* See if this is the special _gp_disp symbol. Note that such a
4350 symbol must always be a global symbol. */
4351 if (strcmp (*namep, "_gp_disp") == 0
4352 && ! NEWABI_P (input_bfd))
4354 /* Relocations against _gp_disp are permitted only with
4355 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
4356 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
4357 return bfd_reloc_notsupported;
4359 gp_disp_p = TRUE;
4361 /* See if this is the special _gp symbol. Note that such a
4362 symbol must always be a global symbol. */
4363 else if (strcmp (*namep, "__gnu_local_gp") == 0)
4364 gnu_local_gp_p = TRUE;
4367 /* If this symbol is defined, calculate its address. Note that
4368 _gp_disp is a magic symbol, always implicitly defined by the
4369 linker, so it's inappropriate to check to see whether or not
4370 its defined. */
4371 else if ((h->root.root.type == bfd_link_hash_defined
4372 || h->root.root.type == bfd_link_hash_defweak)
4373 && h->root.root.u.def.section)
4375 sec = h->root.root.u.def.section;
4376 if (sec->output_section)
4377 symbol = (h->root.root.u.def.value
4378 + sec->output_section->vma
4379 + sec->output_offset);
4380 else
4381 symbol = h->root.root.u.def.value;
4383 else if (h->root.root.type == bfd_link_hash_undefweak)
4384 /* We allow relocations against undefined weak symbols, giving
4385 it the value zero, so that you can undefined weak functions
4386 and check to see if they exist by looking at their
4387 addresses. */
4388 symbol = 0;
4389 else if (info->unresolved_syms_in_objects == RM_IGNORE
4390 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
4391 symbol = 0;
4392 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
4393 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
4395 /* If this is a dynamic link, we should have created a
4396 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
4397 in in _bfd_mips_elf_create_dynamic_sections.
4398 Otherwise, we should define the symbol with a value of 0.
4399 FIXME: It should probably get into the symbol table
4400 somehow as well. */
4401 BFD_ASSERT (! info->shared);
4402 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
4403 symbol = 0;
4405 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
4407 /* This is an optional symbol - an Irix specific extension to the
4408 ELF spec. Ignore it for now.
4409 XXX - FIXME - there is more to the spec for OPTIONAL symbols
4410 than simply ignoring them, but we do not handle this for now.
4411 For information see the "64-bit ELF Object File Specification"
4412 which is available from here:
4413 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
4414 symbol = 0;
4416 else
4418 if (! ((*info->callbacks->undefined_symbol)
4419 (info, h->root.root.root.string, input_bfd,
4420 input_section, relocation->r_offset,
4421 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
4422 || ELF_ST_VISIBILITY (h->root.other))))
4423 return bfd_reloc_undefined;
4424 symbol = 0;
4427 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
4430 /* If this is a reference to a 16-bit function with a stub, we need
4431 to redirect the relocation to the stub unless:
4433 (a) the relocation is for a MIPS16 JAL;
4435 (b) the relocation is for a MIPS16 PIC call, and there are no
4436 non-MIPS16 uses of the GOT slot; or
4438 (c) the section allows direct references to MIPS16 functions. */
4439 if (r_type != R_MIPS16_26
4440 && !info->relocatable
4441 && ((h != NULL
4442 && h->fn_stub != NULL
4443 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
4444 || (local_p
4445 && elf_tdata (input_bfd)->local_stubs != NULL
4446 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
4447 && !section_allows_mips16_refs_p (input_section))
4449 /* This is a 32- or 64-bit call to a 16-bit function. We should
4450 have already noticed that we were going to need the
4451 stub. */
4452 if (local_p)
4453 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
4454 else
4456 BFD_ASSERT (h->need_fn_stub);
4457 sec = h->fn_stub;
4460 symbol = sec->output_section->vma + sec->output_offset;
4461 /* The target is 16-bit, but the stub isn't. */
4462 target_is_16_bit_code_p = FALSE;
4464 /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
4465 need to redirect the call to the stub. Note that we specifically
4466 exclude R_MIPS16_CALL16 from this behavior; indirect calls should
4467 use an indirect stub instead. */
4468 else if (r_type == R_MIPS16_26 && !info->relocatable
4469 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
4470 || (local_p
4471 && elf_tdata (input_bfd)->local_call_stubs != NULL
4472 && elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
4473 && !target_is_16_bit_code_p)
4475 if (local_p)
4476 sec = elf_tdata (input_bfd)->local_call_stubs[r_symndx];
4477 else
4479 /* If both call_stub and call_fp_stub are defined, we can figure
4480 out which one to use by checking which one appears in the input
4481 file. */
4482 if (h->call_stub != NULL && h->call_fp_stub != NULL)
4484 asection *o;
4486 sec = NULL;
4487 for (o = input_bfd->sections; o != NULL; o = o->next)
4489 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
4491 sec = h->call_fp_stub;
4492 break;
4495 if (sec == NULL)
4496 sec = h->call_stub;
4498 else if (h->call_stub != NULL)
4499 sec = h->call_stub;
4500 else
4501 sec = h->call_fp_stub;
4504 BFD_ASSERT (sec->size > 0);
4505 symbol = sec->output_section->vma + sec->output_offset;
4508 /* Calls from 16-bit code to 32-bit code and vice versa require the
4509 special jalx instruction. */
4510 *require_jalxp = (!info->relocatable
4511 && (((r_type == R_MIPS16_26) && !target_is_16_bit_code_p)
4512 || ((r_type == R_MIPS_26) && target_is_16_bit_code_p)));
4514 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
4515 local_sections, TRUE);
4517 gp0 = _bfd_get_gp_value (input_bfd);
4518 gp = _bfd_get_gp_value (abfd);
4519 if (dynobj)
4520 gp += mips_elf_adjust_gp (abfd, mips_elf_got_info (dynobj, NULL),
4521 input_bfd);
4523 if (gnu_local_gp_p)
4524 symbol = gp;
4526 /* If we haven't already determined the GOT offset, oand we're going
4527 to need it, get it now. */
4528 switch (r_type)
4530 case R_MIPS_GOT_PAGE:
4531 case R_MIPS_GOT_OFST:
4532 /* We need to decay to GOT_DISP/addend if the symbol doesn't
4533 bind locally. */
4534 local_p = local_p || _bfd_elf_symbol_refs_local_p (&h->root, info, 1);
4535 if (local_p || r_type == R_MIPS_GOT_OFST)
4536 break;
4537 /* Fall through. */
4539 case R_MIPS16_CALL16:
4540 case R_MIPS16_GOT16:
4541 case R_MIPS_CALL16:
4542 case R_MIPS_GOT16:
4543 case R_MIPS_GOT_DISP:
4544 case R_MIPS_GOT_HI16:
4545 case R_MIPS_CALL_HI16:
4546 case R_MIPS_GOT_LO16:
4547 case R_MIPS_CALL_LO16:
4548 case R_MIPS_TLS_GD:
4549 case R_MIPS_TLS_GOTTPREL:
4550 case R_MIPS_TLS_LDM:
4551 /* Find the index into the GOT where this value is located. */
4552 if (r_type == R_MIPS_TLS_LDM)
4554 g = mips_elf_local_got_index (abfd, input_bfd, info,
4555 0, 0, NULL, r_type);
4556 if (g == MINUS_ONE)
4557 return bfd_reloc_outofrange;
4559 else if (!local_p)
4561 /* On VxWorks, CALL relocations should refer to the .got.plt
4562 entry, which is initialized to point at the PLT stub. */
4563 if (htab->is_vxworks
4564 && (r_type == R_MIPS_CALL_HI16
4565 || r_type == R_MIPS_CALL_LO16
4566 || call16_reloc_p (r_type)))
4568 BFD_ASSERT (addend == 0);
4569 BFD_ASSERT (h->root.needs_plt);
4570 g = mips_elf_gotplt_index (info, &h->root);
4572 else
4574 /* GOT_PAGE may take a non-zero addend, that is ignored in a
4575 GOT_PAGE relocation that decays to GOT_DISP because the
4576 symbol turns out to be global. The addend is then added
4577 as GOT_OFST. */
4578 BFD_ASSERT (addend == 0 || r_type == R_MIPS_GOT_PAGE);
4579 g = mips_elf_global_got_index (dynobj, input_bfd,
4580 &h->root, r_type, info);
4581 if (h->tls_type == GOT_NORMAL
4582 && (! elf_hash_table(info)->dynamic_sections_created
4583 || (info->shared
4584 && (info->symbolic || h->root.forced_local)
4585 && h->root.def_regular)))
4587 /* This is a static link or a -Bsymbolic link. The
4588 symbol is defined locally, or was forced to be local.
4589 We must initialize this entry in the GOT. */
4590 asection *sgot = mips_elf_got_section (dynobj, FALSE);
4591 MIPS_ELF_PUT_WORD (dynobj, symbol, sgot->contents + g);
4595 else if (!htab->is_vxworks
4596 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
4597 /* The calculation below does not involve "g". */
4598 break;
4599 else
4601 g = mips_elf_local_got_index (abfd, input_bfd, info,
4602 symbol + addend, r_symndx, h, r_type);
4603 if (g == MINUS_ONE)
4604 return bfd_reloc_outofrange;
4607 /* Convert GOT indices to actual offsets. */
4608 g = mips_elf_got_offset_from_index (dynobj, abfd, input_bfd, g);
4609 break;
4612 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
4613 symbols are resolved by the loader. Add them to .rela.dyn. */
4614 if (h != NULL && is_gott_symbol (info, &h->root))
4616 Elf_Internal_Rela outrel;
4617 bfd_byte *loc;
4618 asection *s;
4620 s = mips_elf_rel_dyn_section (info, FALSE);
4621 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
4623 outrel.r_offset = (input_section->output_section->vma
4624 + input_section->output_offset
4625 + relocation->r_offset);
4626 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
4627 outrel.r_addend = addend;
4628 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
4630 /* If we've written this relocation for a readonly section,
4631 we need to set DF_TEXTREL again, so that we do not delete the
4632 DT_TEXTREL tag. */
4633 if (MIPS_ELF_READONLY_SECTION (input_section))
4634 info->flags |= DF_TEXTREL;
4636 *valuep = 0;
4637 return bfd_reloc_ok;
4640 /* Figure out what kind of relocation is being performed. */
4641 switch (r_type)
4643 case R_MIPS_NONE:
4644 return bfd_reloc_continue;
4646 case R_MIPS_16:
4647 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
4648 overflowed_p = mips_elf_overflow_p (value, 16);
4649 break;
4651 case R_MIPS_32:
4652 case R_MIPS_REL32:
4653 case R_MIPS_64:
4654 if ((info->shared
4655 || (!htab->is_vxworks
4656 && htab->root.dynamic_sections_created
4657 && h != NULL
4658 && h->root.def_dynamic
4659 && !h->root.def_regular))
4660 && r_symndx != 0
4661 && (h == NULL
4662 || h->root.root.type != bfd_link_hash_undefweak
4663 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
4664 && (input_section->flags & SEC_ALLOC) != 0)
4666 /* If we're creating a shared library, or this relocation is
4667 against a symbol in a shared library, then we can't know
4668 where the symbol will end up. So, we create a relocation
4669 record in the output, and leave the job up to the dynamic
4670 linker.
4672 In VxWorks executables, references to external symbols
4673 are handled using copy relocs or PLT stubs, so there's
4674 no need to add a dynamic relocation here. */
4675 value = addend;
4676 if (!mips_elf_create_dynamic_relocation (abfd,
4677 info,
4678 relocation,
4680 sec,
4681 symbol,
4682 &value,
4683 input_section))
4684 return bfd_reloc_undefined;
4686 else
4688 if (r_type != R_MIPS_REL32)
4689 value = symbol + addend;
4690 else
4691 value = addend;
4693 value &= howto->dst_mask;
4694 break;
4696 case R_MIPS_PC32:
4697 value = symbol + addend - p;
4698 value &= howto->dst_mask;
4699 break;
4701 case R_MIPS16_26:
4702 /* The calculation for R_MIPS16_26 is just the same as for an
4703 R_MIPS_26. It's only the storage of the relocated field into
4704 the output file that's different. That's handled in
4705 mips_elf_perform_relocation. So, we just fall through to the
4706 R_MIPS_26 case here. */
4707 case R_MIPS_26:
4708 if (local_p)
4709 value = ((addend | ((p + 4) & 0xf0000000)) + symbol) >> 2;
4710 else
4712 value = (_bfd_mips_elf_sign_extend (addend, 28) + symbol) >> 2;
4713 if (h->root.root.type != bfd_link_hash_undefweak)
4714 overflowed_p = (value >> 26) != ((p + 4) >> 28);
4716 value &= howto->dst_mask;
4717 break;
4719 case R_MIPS_TLS_DTPREL_HI16:
4720 value = (mips_elf_high (addend + symbol - dtprel_base (info))
4721 & howto->dst_mask);
4722 break;
4724 case R_MIPS_TLS_DTPREL_LO16:
4725 case R_MIPS_TLS_DTPREL32:
4726 case R_MIPS_TLS_DTPREL64:
4727 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
4728 break;
4730 case R_MIPS_TLS_TPREL_HI16:
4731 value = (mips_elf_high (addend + symbol - tprel_base (info))
4732 & howto->dst_mask);
4733 break;
4735 case R_MIPS_TLS_TPREL_LO16:
4736 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
4737 break;
4739 case R_MIPS_HI16:
4740 case R_MIPS16_HI16:
4741 if (!gp_disp_p)
4743 value = mips_elf_high (addend + symbol);
4744 value &= howto->dst_mask;
4746 else
4748 /* For MIPS16 ABI code we generate this sequence
4749 0: li $v0,%hi(_gp_disp)
4750 4: addiupc $v1,%lo(_gp_disp)
4751 8: sll $v0,16
4752 12: addu $v0,$v1
4753 14: move $gp,$v0
4754 So the offsets of hi and lo relocs are the same, but the
4755 $pc is four higher than $t9 would be, so reduce
4756 both reloc addends by 4. */
4757 if (r_type == R_MIPS16_HI16)
4758 value = mips_elf_high (addend + gp - p - 4);
4759 else
4760 value = mips_elf_high (addend + gp - p);
4761 overflowed_p = mips_elf_overflow_p (value, 16);
4763 break;
4765 case R_MIPS_LO16:
4766 case R_MIPS16_LO16:
4767 if (!gp_disp_p)
4768 value = (symbol + addend) & howto->dst_mask;
4769 else
4771 /* See the comment for R_MIPS16_HI16 above for the reason
4772 for this conditional. */
4773 if (r_type == R_MIPS16_LO16)
4774 value = addend + gp - p;
4775 else
4776 value = addend + gp - p + 4;
4777 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
4778 for overflow. But, on, say, IRIX5, relocations against
4779 _gp_disp are normally generated from the .cpload
4780 pseudo-op. It generates code that normally looks like
4781 this:
4783 lui $gp,%hi(_gp_disp)
4784 addiu $gp,$gp,%lo(_gp_disp)
4785 addu $gp,$gp,$t9
4787 Here $t9 holds the address of the function being called,
4788 as required by the MIPS ELF ABI. The R_MIPS_LO16
4789 relocation can easily overflow in this situation, but the
4790 R_MIPS_HI16 relocation will handle the overflow.
4791 Therefore, we consider this a bug in the MIPS ABI, and do
4792 not check for overflow here. */
4794 break;
4796 case R_MIPS_LITERAL:
4797 /* Because we don't merge literal sections, we can handle this
4798 just like R_MIPS_GPREL16. In the long run, we should merge
4799 shared literals, and then we will need to additional work
4800 here. */
4802 /* Fall through. */
4804 case R_MIPS16_GPREL:
4805 /* The R_MIPS16_GPREL performs the same calculation as
4806 R_MIPS_GPREL16, but stores the relocated bits in a different
4807 order. We don't need to do anything special here; the
4808 differences are handled in mips_elf_perform_relocation. */
4809 case R_MIPS_GPREL16:
4810 /* Only sign-extend the addend if it was extracted from the
4811 instruction. If the addend was separate, leave it alone,
4812 otherwise we may lose significant bits. */
4813 if (howto->partial_inplace)
4814 addend = _bfd_mips_elf_sign_extend (addend, 16);
4815 value = symbol + addend - gp;
4816 /* If the symbol was local, any earlier relocatable links will
4817 have adjusted its addend with the gp offset, so compensate
4818 for that now. Don't do it for symbols forced local in this
4819 link, though, since they won't have had the gp offset applied
4820 to them before. */
4821 if (was_local_p)
4822 value += gp0;
4823 overflowed_p = mips_elf_overflow_p (value, 16);
4824 break;
4826 case R_MIPS16_GOT16:
4827 case R_MIPS16_CALL16:
4828 case R_MIPS_GOT16:
4829 case R_MIPS_CALL16:
4830 /* VxWorks does not have separate local and global semantics for
4831 R_MIPS*_GOT16; every relocation evaluates to "G". */
4832 if (!htab->is_vxworks && local_p)
4834 bfd_boolean forced;
4836 forced = ! mips_elf_local_relocation_p (input_bfd, relocation,
4837 local_sections, FALSE);
4838 value = mips_elf_got16_entry (abfd, input_bfd, info,
4839 symbol + addend, forced);
4840 if (value == MINUS_ONE)
4841 return bfd_reloc_outofrange;
4842 value
4843 = mips_elf_got_offset_from_index (dynobj, abfd, input_bfd, value);
4844 overflowed_p = mips_elf_overflow_p (value, 16);
4845 break;
4848 /* Fall through. */
4850 case R_MIPS_TLS_GD:
4851 case R_MIPS_TLS_GOTTPREL:
4852 case R_MIPS_TLS_LDM:
4853 case R_MIPS_GOT_DISP:
4854 got_disp:
4855 value = g;
4856 overflowed_p = mips_elf_overflow_p (value, 16);
4857 break;
4859 case R_MIPS_GPREL32:
4860 value = (addend + symbol + gp0 - gp);
4861 if (!save_addend)
4862 value &= howto->dst_mask;
4863 break;
4865 case R_MIPS_PC16:
4866 case R_MIPS_GNU_REL16_S2:
4867 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
4868 overflowed_p = mips_elf_overflow_p (value, 18);
4869 value >>= howto->rightshift;
4870 value &= howto->dst_mask;
4871 break;
4873 case R_MIPS_GOT_HI16:
4874 case R_MIPS_CALL_HI16:
4875 /* We're allowed to handle these two relocations identically.
4876 The dynamic linker is allowed to handle the CALL relocations
4877 differently by creating a lazy evaluation stub. */
4878 value = g;
4879 value = mips_elf_high (value);
4880 value &= howto->dst_mask;
4881 break;
4883 case R_MIPS_GOT_LO16:
4884 case R_MIPS_CALL_LO16:
4885 value = g & howto->dst_mask;
4886 break;
4888 case R_MIPS_GOT_PAGE:
4889 /* GOT_PAGE relocations that reference non-local symbols decay
4890 to GOT_DISP. The corresponding GOT_OFST relocation decays to
4891 0. */
4892 if (! local_p)
4893 goto got_disp;
4894 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
4895 if (value == MINUS_ONE)
4896 return bfd_reloc_outofrange;
4897 value = mips_elf_got_offset_from_index (dynobj, abfd, input_bfd, value);
4898 overflowed_p = mips_elf_overflow_p (value, 16);
4899 break;
4901 case R_MIPS_GOT_OFST:
4902 if (local_p)
4903 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
4904 else
4905 value = addend;
4906 overflowed_p = mips_elf_overflow_p (value, 16);
4907 break;
4909 case R_MIPS_SUB:
4910 value = symbol - addend;
4911 value &= howto->dst_mask;
4912 break;
4914 case R_MIPS_HIGHER:
4915 value = mips_elf_higher (addend + symbol);
4916 value &= howto->dst_mask;
4917 break;
4919 case R_MIPS_HIGHEST:
4920 value = mips_elf_highest (addend + symbol);
4921 value &= howto->dst_mask;
4922 break;
4924 case R_MIPS_SCN_DISP:
4925 value = symbol + addend - sec->output_offset;
4926 value &= howto->dst_mask;
4927 break;
4929 case R_MIPS_JALR:
4930 /* This relocation is only a hint. In some cases, we optimize
4931 it into a bal instruction. But we don't try to optimize
4932 branches to the PLT; that will wind up wasting time. */
4933 if (h != NULL && h->root.plt.offset != (bfd_vma) -1)
4934 return bfd_reloc_continue;
4935 value = symbol + addend;
4936 break;
4938 case R_MIPS_PJUMP:
4939 case R_MIPS_GNU_VTINHERIT:
4940 case R_MIPS_GNU_VTENTRY:
4941 /* We don't do anything with these at present. */
4942 return bfd_reloc_continue;
4944 default:
4945 /* An unrecognized relocation type. */
4946 return bfd_reloc_notsupported;
4949 /* Store the VALUE for our caller. */
4950 *valuep = value;
4951 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
4954 /* Obtain the field relocated by RELOCATION. */
4956 static bfd_vma
4957 mips_elf_obtain_contents (reloc_howto_type *howto,
4958 const Elf_Internal_Rela *relocation,
4959 bfd *input_bfd, bfd_byte *contents)
4961 bfd_vma x;
4962 bfd_byte *location = contents + relocation->r_offset;
4964 /* Obtain the bytes. */
4965 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
4967 return x;
4970 /* It has been determined that the result of the RELOCATION is the
4971 VALUE. Use HOWTO to place VALUE into the output file at the
4972 appropriate position. The SECTION is the section to which the
4973 relocation applies. If REQUIRE_JALX is TRUE, then the opcode used
4974 for the relocation must be either JAL or JALX, and it is
4975 unconditionally converted to JALX.
4977 Returns FALSE if anything goes wrong. */
4979 static bfd_boolean
4980 mips_elf_perform_relocation (struct bfd_link_info *info,
4981 reloc_howto_type *howto,
4982 const Elf_Internal_Rela *relocation,
4983 bfd_vma value, bfd *input_bfd,
4984 asection *input_section, bfd_byte *contents,
4985 bfd_boolean require_jalx)
4987 bfd_vma x;
4988 bfd_byte *location;
4989 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
4991 /* Figure out where the relocation is occurring. */
4992 location = contents + relocation->r_offset;
4994 _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
4996 /* Obtain the current value. */
4997 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
4999 /* Clear the field we are setting. */
5000 x &= ~howto->dst_mask;
5002 /* Set the field. */
5003 x |= (value & howto->dst_mask);
5005 /* If required, turn JAL into JALX. */
5006 if (require_jalx)
5008 bfd_boolean ok;
5009 bfd_vma opcode = x >> 26;
5010 bfd_vma jalx_opcode;
5012 /* Check to see if the opcode is already JAL or JALX. */
5013 if (r_type == R_MIPS16_26)
5015 ok = ((opcode == 0x6) || (opcode == 0x7));
5016 jalx_opcode = 0x7;
5018 else
5020 ok = ((opcode == 0x3) || (opcode == 0x1d));
5021 jalx_opcode = 0x1d;
5024 /* If the opcode is not JAL or JALX, there's a problem. */
5025 if (!ok)
5027 (*_bfd_error_handler)
5028 (_("%B: %A+0x%lx: jump to stub routine which is not jal"),
5029 input_bfd,
5030 input_section,
5031 (unsigned long) relocation->r_offset);
5032 bfd_set_error (bfd_error_bad_value);
5033 return FALSE;
5036 /* Make this the JALX opcode. */
5037 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
5040 /* On the RM9000, bal is faster than jal, because bal uses branch
5041 prediction hardware. If we are linking for the RM9000, and we
5042 see jal, and bal fits, use it instead. Note that this
5043 transformation should be safe for all architectures. */
5044 if (bfd_get_mach (input_bfd) == bfd_mach_mips9000
5045 && !info->relocatable
5046 && !require_jalx
5047 && ((r_type == R_MIPS_26 && (x >> 26) == 0x3) /* jal addr */
5048 || (r_type == R_MIPS_JALR && x == 0x0320f809))) /* jalr t9 */
5050 bfd_vma addr;
5051 bfd_vma dest;
5052 bfd_signed_vma off;
5054 addr = (input_section->output_section->vma
5055 + input_section->output_offset
5056 + relocation->r_offset
5057 + 4);
5058 if (r_type == R_MIPS_26)
5059 dest = (value << 2) | ((addr >> 28) << 28);
5060 else
5061 dest = value;
5062 off = dest - addr;
5063 if (off <= 0x1ffff && off >= -0x20000)
5064 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
5067 /* Put the value into the output. */
5068 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
5070 _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, !info->relocatable,
5071 location);
5073 return TRUE;
5076 /* Add room for N relocations to the .rel(a).dyn section in ABFD. */
5078 static void
5079 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
5080 unsigned int n)
5082 asection *s;
5083 struct mips_elf_link_hash_table *htab;
5085 htab = mips_elf_hash_table (info);
5086 s = mips_elf_rel_dyn_section (info, FALSE);
5087 BFD_ASSERT (s != NULL);
5089 if (htab->is_vxworks)
5090 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
5091 else
5093 if (s->size == 0)
5095 /* Make room for a null element. */
5096 s->size += MIPS_ELF_REL_SIZE (abfd);
5097 ++s->reloc_count;
5099 s->size += n * MIPS_ELF_REL_SIZE (abfd);
5103 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
5104 is the original relocation, which is now being transformed into a
5105 dynamic relocation. The ADDENDP is adjusted if necessary; the
5106 caller should store the result in place of the original addend. */
5108 static bfd_boolean
5109 mips_elf_create_dynamic_relocation (bfd *output_bfd,
5110 struct bfd_link_info *info,
5111 const Elf_Internal_Rela *rel,
5112 struct mips_elf_link_hash_entry *h,
5113 asection *sec, bfd_vma symbol,
5114 bfd_vma *addendp, asection *input_section)
5116 Elf_Internal_Rela outrel[3];
5117 asection *sreloc;
5118 bfd *dynobj;
5119 int r_type;
5120 long indx;
5121 bfd_boolean defined_p;
5122 struct mips_elf_link_hash_table *htab;
5124 htab = mips_elf_hash_table (info);
5125 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
5126 dynobj = elf_hash_table (info)->dynobj;
5127 sreloc = mips_elf_rel_dyn_section (info, FALSE);
5128 BFD_ASSERT (sreloc != NULL);
5129 BFD_ASSERT (sreloc->contents != NULL);
5130 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
5131 < sreloc->size);
5133 outrel[0].r_offset =
5134 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
5135 if (ABI_64_P (output_bfd))
5137 outrel[1].r_offset =
5138 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
5139 outrel[2].r_offset =
5140 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
5143 if (outrel[0].r_offset == MINUS_ONE)
5144 /* The relocation field has been deleted. */
5145 return TRUE;
5147 if (outrel[0].r_offset == MINUS_TWO)
5149 /* The relocation field has been converted into a relative value of
5150 some sort. Functions like _bfd_elf_write_section_eh_frame expect
5151 the field to be fully relocated, so add in the symbol's value. */
5152 *addendp += symbol;
5153 return TRUE;
5156 /* We must now calculate the dynamic symbol table index to use
5157 in the relocation. */
5158 if (h != NULL
5159 && (!h->root.def_regular
5160 || (info->shared && !info->symbolic && !h->root.forced_local)))
5162 indx = h->root.dynindx;
5163 if (SGI_COMPAT (output_bfd))
5164 defined_p = h->root.def_regular;
5165 else
5166 /* ??? glibc's ld.so just adds the final GOT entry to the
5167 relocation field. It therefore treats relocs against
5168 defined symbols in the same way as relocs against
5169 undefined symbols. */
5170 defined_p = FALSE;
5172 else
5174 if (sec != NULL && bfd_is_abs_section (sec))
5175 indx = 0;
5176 else if (sec == NULL || sec->owner == NULL)
5178 bfd_set_error (bfd_error_bad_value);
5179 return FALSE;
5181 else
5183 indx = elf_section_data (sec->output_section)->dynindx;
5184 if (indx == 0)
5186 asection *osec = htab->root.text_index_section;
5187 indx = elf_section_data (osec)->dynindx;
5189 if (indx == 0)
5190 abort ();
5193 /* Instead of generating a relocation using the section
5194 symbol, we may as well make it a fully relative
5195 relocation. We want to avoid generating relocations to
5196 local symbols because we used to generate them
5197 incorrectly, without adding the original symbol value,
5198 which is mandated by the ABI for section symbols. In
5199 order to give dynamic loaders and applications time to
5200 phase out the incorrect use, we refrain from emitting
5201 section-relative relocations. It's not like they're
5202 useful, after all. This should be a bit more efficient
5203 as well. */
5204 /* ??? Although this behavior is compatible with glibc's ld.so,
5205 the ABI says that relocations against STN_UNDEF should have
5206 a symbol value of 0. Irix rld honors this, so relocations
5207 against STN_UNDEF have no effect. */
5208 if (!SGI_COMPAT (output_bfd))
5209 indx = 0;
5210 defined_p = TRUE;
5213 /* If the relocation was previously an absolute relocation and
5214 this symbol will not be referred to by the relocation, we must
5215 adjust it by the value we give it in the dynamic symbol table.
5216 Otherwise leave the job up to the dynamic linker. */
5217 if (defined_p && r_type != R_MIPS_REL32)
5218 *addendp += symbol;
5220 if (htab->is_vxworks)
5221 /* VxWorks uses non-relative relocations for this. */
5222 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
5223 else
5224 /* The relocation is always an REL32 relocation because we don't
5225 know where the shared library will wind up at load-time. */
5226 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
5227 R_MIPS_REL32);
5229 /* For strict adherence to the ABI specification, we should
5230 generate a R_MIPS_64 relocation record by itself before the
5231 _REL32/_64 record as well, such that the addend is read in as
5232 a 64-bit value (REL32 is a 32-bit relocation, after all).
5233 However, since none of the existing ELF64 MIPS dynamic
5234 loaders seems to care, we don't waste space with these
5235 artificial relocations. If this turns out to not be true,
5236 mips_elf_allocate_dynamic_relocation() should be tweaked so
5237 as to make room for a pair of dynamic relocations per
5238 invocation if ABI_64_P, and here we should generate an
5239 additional relocation record with R_MIPS_64 by itself for a
5240 NULL symbol before this relocation record. */
5241 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
5242 ABI_64_P (output_bfd)
5243 ? R_MIPS_64
5244 : R_MIPS_NONE);
5245 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
5247 /* Adjust the output offset of the relocation to reference the
5248 correct location in the output file. */
5249 outrel[0].r_offset += (input_section->output_section->vma
5250 + input_section->output_offset);
5251 outrel[1].r_offset += (input_section->output_section->vma
5252 + input_section->output_offset);
5253 outrel[2].r_offset += (input_section->output_section->vma
5254 + input_section->output_offset);
5256 /* Put the relocation back out. We have to use the special
5257 relocation outputter in the 64-bit case since the 64-bit
5258 relocation format is non-standard. */
5259 if (ABI_64_P (output_bfd))
5261 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
5262 (output_bfd, &outrel[0],
5263 (sreloc->contents
5264 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
5266 else if (htab->is_vxworks)
5268 /* VxWorks uses RELA rather than REL dynamic relocations. */
5269 outrel[0].r_addend = *addendp;
5270 bfd_elf32_swap_reloca_out
5271 (output_bfd, &outrel[0],
5272 (sreloc->contents
5273 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
5275 else
5276 bfd_elf32_swap_reloc_out
5277 (output_bfd, &outrel[0],
5278 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
5280 /* We've now added another relocation. */
5281 ++sreloc->reloc_count;
5283 /* Make sure the output section is writable. The dynamic linker
5284 will be writing to it. */
5285 elf_section_data (input_section->output_section)->this_hdr.sh_flags
5286 |= SHF_WRITE;
5288 /* On IRIX5, make an entry of compact relocation info. */
5289 if (IRIX_COMPAT (output_bfd) == ict_irix5)
5291 asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
5292 bfd_byte *cr;
5294 if (scpt)
5296 Elf32_crinfo cptrel;
5298 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5299 cptrel.vaddr = (rel->r_offset
5300 + input_section->output_section->vma
5301 + input_section->output_offset);
5302 if (r_type == R_MIPS_REL32)
5303 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5304 else
5305 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5306 mips_elf_set_cr_dist2to (cptrel, 0);
5307 cptrel.konst = *addendp;
5309 cr = (scpt->contents
5310 + sizeof (Elf32_External_compact_rel));
5311 mips_elf_set_cr_relvaddr (cptrel, 0);
5312 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5313 ((Elf32_External_crinfo *) cr
5314 + scpt->reloc_count));
5315 ++scpt->reloc_count;
5319 /* If we've written this relocation for a readonly section,
5320 we need to set DF_TEXTREL again, so that we do not delete the
5321 DT_TEXTREL tag. */
5322 if (MIPS_ELF_READONLY_SECTION (input_section))
5323 info->flags |= DF_TEXTREL;
5325 return TRUE;
5328 /* Return the MACH for a MIPS e_flags value. */
5330 unsigned long
5331 _bfd_elf_mips_mach (flagword flags)
5333 switch (flags & EF_MIPS_MACH)
5335 case E_MIPS_MACH_3900:
5336 return bfd_mach_mips3900;
5338 case E_MIPS_MACH_4010:
5339 return bfd_mach_mips4010;
5341 case E_MIPS_MACH_4100:
5342 return bfd_mach_mips4100;
5344 case E_MIPS_MACH_4111:
5345 return bfd_mach_mips4111;
5347 case E_MIPS_MACH_4120:
5348 return bfd_mach_mips4120;
5350 case E_MIPS_MACH_4650:
5351 return bfd_mach_mips4650;
5353 case E_MIPS_MACH_5400:
5354 return bfd_mach_mips5400;
5356 case E_MIPS_MACH_5500:
5357 return bfd_mach_mips5500;
5359 case E_MIPS_MACH_9000:
5360 return bfd_mach_mips9000;
5362 case E_MIPS_MACH_SB1:
5363 return bfd_mach_mips_sb1;
5365 case E_MIPS_MACH_LS2E:
5366 return bfd_mach_mips_loongson_2e;
5368 case E_MIPS_MACH_LS2F:
5369 return bfd_mach_mips_loongson_2f;
5371 case E_MIPS_MACH_OCTEON:
5372 return bfd_mach_mips_octeon;
5374 default:
5375 switch (flags & EF_MIPS_ARCH)
5377 default:
5378 case E_MIPS_ARCH_1:
5379 return bfd_mach_mips3000;
5381 case E_MIPS_ARCH_2:
5382 return bfd_mach_mips6000;
5384 case E_MIPS_ARCH_3:
5385 return bfd_mach_mips4000;
5387 case E_MIPS_ARCH_4:
5388 return bfd_mach_mips8000;
5390 case E_MIPS_ARCH_5:
5391 return bfd_mach_mips5;
5393 case E_MIPS_ARCH_32:
5394 return bfd_mach_mipsisa32;
5396 case E_MIPS_ARCH_64:
5397 return bfd_mach_mipsisa64;
5399 case E_MIPS_ARCH_32R2:
5400 return bfd_mach_mipsisa32r2;
5402 case E_MIPS_ARCH_64R2:
5403 return bfd_mach_mipsisa64r2;
5407 return 0;
5410 /* Return printable name for ABI. */
5412 static INLINE char *
5413 elf_mips_abi_name (bfd *abfd)
5415 flagword flags;
5417 flags = elf_elfheader (abfd)->e_flags;
5418 switch (flags & EF_MIPS_ABI)
5420 case 0:
5421 if (ABI_N32_P (abfd))
5422 return "N32";
5423 else if (ABI_64_P (abfd))
5424 return "64";
5425 else
5426 return "none";
5427 case E_MIPS_ABI_O32:
5428 return "O32";
5429 case E_MIPS_ABI_O64:
5430 return "O64";
5431 case E_MIPS_ABI_EABI32:
5432 return "EABI32";
5433 case E_MIPS_ABI_EABI64:
5434 return "EABI64";
5435 default:
5436 return "unknown abi";
5440 /* MIPS ELF uses two common sections. One is the usual one, and the
5441 other is for small objects. All the small objects are kept
5442 together, and then referenced via the gp pointer, which yields
5443 faster assembler code. This is what we use for the small common
5444 section. This approach is copied from ecoff.c. */
5445 static asection mips_elf_scom_section;
5446 static asymbol mips_elf_scom_symbol;
5447 static asymbol *mips_elf_scom_symbol_ptr;
5449 /* MIPS ELF also uses an acommon section, which represents an
5450 allocated common symbol which may be overridden by a
5451 definition in a shared library. */
5452 static asection mips_elf_acom_section;
5453 static asymbol mips_elf_acom_symbol;
5454 static asymbol *mips_elf_acom_symbol_ptr;
5456 /* This is used for both the 32-bit and the 64-bit ABI. */
5458 void
5459 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
5461 elf_symbol_type *elfsym;
5463 /* Handle the special MIPS section numbers that a symbol may use. */
5464 elfsym = (elf_symbol_type *) asym;
5465 switch (elfsym->internal_elf_sym.st_shndx)
5467 case SHN_MIPS_ACOMMON:
5468 /* This section is used in a dynamically linked executable file.
5469 It is an allocated common section. The dynamic linker can
5470 either resolve these symbols to something in a shared
5471 library, or it can just leave them here. For our purposes,
5472 we can consider these symbols to be in a new section. */
5473 if (mips_elf_acom_section.name == NULL)
5475 /* Initialize the acommon section. */
5476 mips_elf_acom_section.name = ".acommon";
5477 mips_elf_acom_section.flags = SEC_ALLOC;
5478 mips_elf_acom_section.output_section = &mips_elf_acom_section;
5479 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
5480 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
5481 mips_elf_acom_symbol.name = ".acommon";
5482 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
5483 mips_elf_acom_symbol.section = &mips_elf_acom_section;
5484 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
5486 asym->section = &mips_elf_acom_section;
5487 break;
5489 case SHN_COMMON:
5490 /* Common symbols less than the GP size are automatically
5491 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
5492 if (asym->value > elf_gp_size (abfd)
5493 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
5494 || IRIX_COMPAT (abfd) == ict_irix6)
5495 break;
5496 /* Fall through. */
5497 case SHN_MIPS_SCOMMON:
5498 if (mips_elf_scom_section.name == NULL)
5500 /* Initialize the small common section. */
5501 mips_elf_scom_section.name = ".scommon";
5502 mips_elf_scom_section.flags = SEC_IS_COMMON;
5503 mips_elf_scom_section.output_section = &mips_elf_scom_section;
5504 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
5505 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
5506 mips_elf_scom_symbol.name = ".scommon";
5507 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
5508 mips_elf_scom_symbol.section = &mips_elf_scom_section;
5509 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
5511 asym->section = &mips_elf_scom_section;
5512 asym->value = elfsym->internal_elf_sym.st_size;
5513 break;
5515 case SHN_MIPS_SUNDEFINED:
5516 asym->section = bfd_und_section_ptr;
5517 break;
5519 case SHN_MIPS_TEXT:
5521 asection *section = bfd_get_section_by_name (abfd, ".text");
5523 BFD_ASSERT (SGI_COMPAT (abfd));
5524 if (section != NULL)
5526 asym->section = section;
5527 /* MIPS_TEXT is a bit special, the address is not an offset
5528 to the base of the .text section. So substract the section
5529 base address to make it an offset. */
5530 asym->value -= section->vma;
5533 break;
5535 case SHN_MIPS_DATA:
5537 asection *section = bfd_get_section_by_name (abfd, ".data");
5539 BFD_ASSERT (SGI_COMPAT (abfd));
5540 if (section != NULL)
5542 asym->section = section;
5543 /* MIPS_DATA is a bit special, the address is not an offset
5544 to the base of the .data section. So substract the section
5545 base address to make it an offset. */
5546 asym->value -= section->vma;
5549 break;
5552 /* If this is an odd-valued function symbol, assume it's a MIPS16 one. */
5553 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
5554 && (asym->value & 1) != 0)
5556 asym->value--;
5557 elfsym->internal_elf_sym.st_other
5558 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
5562 /* Implement elf_backend_eh_frame_address_size. This differs from
5563 the default in the way it handles EABI64.
5565 EABI64 was originally specified as an LP64 ABI, and that is what
5566 -mabi=eabi normally gives on a 64-bit target. However, gcc has
5567 historically accepted the combination of -mabi=eabi and -mlong32,
5568 and this ILP32 variation has become semi-official over time.
5569 Both forms use elf32 and have pointer-sized FDE addresses.
5571 If an EABI object was generated by GCC 4.0 or above, it will have
5572 an empty .gcc_compiled_longXX section, where XX is the size of longs
5573 in bits. Unfortunately, ILP32 objects generated by earlier compilers
5574 have no special marking to distinguish them from LP64 objects.
5576 We don't want users of the official LP64 ABI to be punished for the
5577 existence of the ILP32 variant, but at the same time, we don't want
5578 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
5579 We therefore take the following approach:
5581 - If ABFD contains a .gcc_compiled_longXX section, use it to
5582 determine the pointer size.
5584 - Otherwise check the type of the first relocation. Assume that
5585 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
5587 - Otherwise punt.
5589 The second check is enough to detect LP64 objects generated by pre-4.0
5590 compilers because, in the kind of output generated by those compilers,
5591 the first relocation will be associated with either a CIE personality
5592 routine or an FDE start address. Furthermore, the compilers never
5593 used a special (non-pointer) encoding for this ABI.
5595 Checking the relocation type should also be safe because there is no
5596 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
5597 did so. */
5599 unsigned int
5600 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
5602 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
5603 return 8;
5604 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
5606 bfd_boolean long32_p, long64_p;
5608 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
5609 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
5610 if (long32_p && long64_p)
5611 return 0;
5612 if (long32_p)
5613 return 4;
5614 if (long64_p)
5615 return 8;
5617 if (sec->reloc_count > 0
5618 && elf_section_data (sec)->relocs != NULL
5619 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
5620 == R_MIPS_64))
5621 return 8;
5623 return 0;
5625 return 4;
5628 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
5629 relocations against two unnamed section symbols to resolve to the
5630 same address. For example, if we have code like:
5632 lw $4,%got_disp(.data)($gp)
5633 lw $25,%got_disp(.text)($gp)
5634 jalr $25
5636 then the linker will resolve both relocations to .data and the program
5637 will jump there rather than to .text.
5639 We can work around this problem by giving names to local section symbols.
5640 This is also what the MIPSpro tools do. */
5642 bfd_boolean
5643 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
5645 return SGI_COMPAT (abfd);
5648 /* Work over a section just before writing it out. This routine is
5649 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
5650 sections that need the SHF_MIPS_GPREL flag by name; there has to be
5651 a better way. */
5653 bfd_boolean
5654 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
5656 if (hdr->sh_type == SHT_MIPS_REGINFO
5657 && hdr->sh_size > 0)
5659 bfd_byte buf[4];
5661 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
5662 BFD_ASSERT (hdr->contents == NULL);
5664 if (bfd_seek (abfd,
5665 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
5666 SEEK_SET) != 0)
5667 return FALSE;
5668 H_PUT_32 (abfd, elf_gp (abfd), buf);
5669 if (bfd_bwrite (buf, 4, abfd) != 4)
5670 return FALSE;
5673 if (hdr->sh_type == SHT_MIPS_OPTIONS
5674 && hdr->bfd_section != NULL
5675 && mips_elf_section_data (hdr->bfd_section) != NULL
5676 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
5678 bfd_byte *contents, *l, *lend;
5680 /* We stored the section contents in the tdata field in the
5681 set_section_contents routine. We save the section contents
5682 so that we don't have to read them again.
5683 At this point we know that elf_gp is set, so we can look
5684 through the section contents to see if there is an
5685 ODK_REGINFO structure. */
5687 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
5688 l = contents;
5689 lend = contents + hdr->sh_size;
5690 while (l + sizeof (Elf_External_Options) <= lend)
5692 Elf_Internal_Options intopt;
5694 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
5695 &intopt);
5696 if (intopt.size < sizeof (Elf_External_Options))
5698 (*_bfd_error_handler)
5699 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
5700 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
5701 break;
5703 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
5705 bfd_byte buf[8];
5707 if (bfd_seek (abfd,
5708 (hdr->sh_offset
5709 + (l - contents)
5710 + sizeof (Elf_External_Options)
5711 + (sizeof (Elf64_External_RegInfo) - 8)),
5712 SEEK_SET) != 0)
5713 return FALSE;
5714 H_PUT_64 (abfd, elf_gp (abfd), buf);
5715 if (bfd_bwrite (buf, 8, abfd) != 8)
5716 return FALSE;
5718 else if (intopt.kind == ODK_REGINFO)
5720 bfd_byte buf[4];
5722 if (bfd_seek (abfd,
5723 (hdr->sh_offset
5724 + (l - contents)
5725 + sizeof (Elf_External_Options)
5726 + (sizeof (Elf32_External_RegInfo) - 4)),
5727 SEEK_SET) != 0)
5728 return FALSE;
5729 H_PUT_32 (abfd, elf_gp (abfd), buf);
5730 if (bfd_bwrite (buf, 4, abfd) != 4)
5731 return FALSE;
5733 l += intopt.size;
5737 if (hdr->bfd_section != NULL)
5739 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
5741 if (strcmp (name, ".sdata") == 0
5742 || strcmp (name, ".lit8") == 0
5743 || strcmp (name, ".lit4") == 0)
5745 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5746 hdr->sh_type = SHT_PROGBITS;
5748 else if (strcmp (name, ".sbss") == 0)
5750 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5751 hdr->sh_type = SHT_NOBITS;
5753 else if (strcmp (name, ".srdata") == 0)
5755 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
5756 hdr->sh_type = SHT_PROGBITS;
5758 else if (strcmp (name, ".compact_rel") == 0)
5760 hdr->sh_flags = 0;
5761 hdr->sh_type = SHT_PROGBITS;
5763 else if (strcmp (name, ".rtproc") == 0)
5765 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
5767 unsigned int adjust;
5769 adjust = hdr->sh_size % hdr->sh_addralign;
5770 if (adjust != 0)
5771 hdr->sh_size += hdr->sh_addralign - adjust;
5776 return TRUE;
5779 /* Handle a MIPS specific section when reading an object file. This
5780 is called when elfcode.h finds a section with an unknown type.
5781 This routine supports both the 32-bit and 64-bit ELF ABI.
5783 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
5784 how to. */
5786 bfd_boolean
5787 _bfd_mips_elf_section_from_shdr (bfd *abfd,
5788 Elf_Internal_Shdr *hdr,
5789 const char *name,
5790 int shindex)
5792 flagword flags = 0;
5794 /* There ought to be a place to keep ELF backend specific flags, but
5795 at the moment there isn't one. We just keep track of the
5796 sections by their name, instead. Fortunately, the ABI gives
5797 suggested names for all the MIPS specific sections, so we will
5798 probably get away with this. */
5799 switch (hdr->sh_type)
5801 case SHT_MIPS_LIBLIST:
5802 if (strcmp (name, ".liblist") != 0)
5803 return FALSE;
5804 break;
5805 case SHT_MIPS_MSYM:
5806 if (strcmp (name, ".msym") != 0)
5807 return FALSE;
5808 break;
5809 case SHT_MIPS_CONFLICT:
5810 if (strcmp (name, ".conflict") != 0)
5811 return FALSE;
5812 break;
5813 case SHT_MIPS_GPTAB:
5814 if (! CONST_STRNEQ (name, ".gptab."))
5815 return FALSE;
5816 break;
5817 case SHT_MIPS_UCODE:
5818 if (strcmp (name, ".ucode") != 0)
5819 return FALSE;
5820 break;
5821 case SHT_MIPS_DEBUG:
5822 if (strcmp (name, ".mdebug") != 0)
5823 return FALSE;
5824 flags = SEC_DEBUGGING;
5825 break;
5826 case SHT_MIPS_REGINFO:
5827 if (strcmp (name, ".reginfo") != 0
5828 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
5829 return FALSE;
5830 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
5831 break;
5832 case SHT_MIPS_IFACE:
5833 if (strcmp (name, ".MIPS.interfaces") != 0)
5834 return FALSE;
5835 break;
5836 case SHT_MIPS_CONTENT:
5837 if (! CONST_STRNEQ (name, ".MIPS.content"))
5838 return FALSE;
5839 break;
5840 case SHT_MIPS_OPTIONS:
5841 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
5842 return FALSE;
5843 break;
5844 case SHT_MIPS_DWARF:
5845 if (! CONST_STRNEQ (name, ".debug_")
5846 && ! CONST_STRNEQ (name, ".zdebug_"))
5847 return FALSE;
5848 break;
5849 case SHT_MIPS_SYMBOL_LIB:
5850 if (strcmp (name, ".MIPS.symlib") != 0)
5851 return FALSE;
5852 break;
5853 case SHT_MIPS_EVENTS:
5854 if (! CONST_STRNEQ (name, ".MIPS.events")
5855 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
5856 return FALSE;
5857 break;
5858 default:
5859 break;
5862 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
5863 return FALSE;
5865 if (flags)
5867 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
5868 (bfd_get_section_flags (abfd,
5869 hdr->bfd_section)
5870 | flags)))
5871 return FALSE;
5874 /* FIXME: We should record sh_info for a .gptab section. */
5876 /* For a .reginfo section, set the gp value in the tdata information
5877 from the contents of this section. We need the gp value while
5878 processing relocs, so we just get it now. The .reginfo section
5879 is not used in the 64-bit MIPS ELF ABI. */
5880 if (hdr->sh_type == SHT_MIPS_REGINFO)
5882 Elf32_External_RegInfo ext;
5883 Elf32_RegInfo s;
5885 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
5886 &ext, 0, sizeof ext))
5887 return FALSE;
5888 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
5889 elf_gp (abfd) = s.ri_gp_value;
5892 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
5893 set the gp value based on what we find. We may see both
5894 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
5895 they should agree. */
5896 if (hdr->sh_type == SHT_MIPS_OPTIONS)
5898 bfd_byte *contents, *l, *lend;
5900 contents = bfd_malloc (hdr->sh_size);
5901 if (contents == NULL)
5902 return FALSE;
5903 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
5904 0, hdr->sh_size))
5906 free (contents);
5907 return FALSE;
5909 l = contents;
5910 lend = contents + hdr->sh_size;
5911 while (l + sizeof (Elf_External_Options) <= lend)
5913 Elf_Internal_Options intopt;
5915 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
5916 &intopt);
5917 if (intopt.size < sizeof (Elf_External_Options))
5919 (*_bfd_error_handler)
5920 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
5921 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
5922 break;
5924 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
5926 Elf64_Internal_RegInfo intreg;
5928 bfd_mips_elf64_swap_reginfo_in
5929 (abfd,
5930 ((Elf64_External_RegInfo *)
5931 (l + sizeof (Elf_External_Options))),
5932 &intreg);
5933 elf_gp (abfd) = intreg.ri_gp_value;
5935 else if (intopt.kind == ODK_REGINFO)
5937 Elf32_RegInfo intreg;
5939 bfd_mips_elf32_swap_reginfo_in
5940 (abfd,
5941 ((Elf32_External_RegInfo *)
5942 (l + sizeof (Elf_External_Options))),
5943 &intreg);
5944 elf_gp (abfd) = intreg.ri_gp_value;
5946 l += intopt.size;
5948 free (contents);
5951 return TRUE;
5954 /* Set the correct type for a MIPS ELF section. We do this by the
5955 section name, which is a hack, but ought to work. This routine is
5956 used by both the 32-bit and the 64-bit ABI. */
5958 bfd_boolean
5959 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
5961 const char *name = bfd_get_section_name (abfd, sec);
5963 if (strcmp (name, ".liblist") == 0)
5965 hdr->sh_type = SHT_MIPS_LIBLIST;
5966 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
5967 /* The sh_link field is set in final_write_processing. */
5969 else if (strcmp (name, ".conflict") == 0)
5970 hdr->sh_type = SHT_MIPS_CONFLICT;
5971 else if (CONST_STRNEQ (name, ".gptab."))
5973 hdr->sh_type = SHT_MIPS_GPTAB;
5974 hdr->sh_entsize = sizeof (Elf32_External_gptab);
5975 /* The sh_info field is set in final_write_processing. */
5977 else if (strcmp (name, ".ucode") == 0)
5978 hdr->sh_type = SHT_MIPS_UCODE;
5979 else if (strcmp (name, ".mdebug") == 0)
5981 hdr->sh_type = SHT_MIPS_DEBUG;
5982 /* In a shared object on IRIX 5.3, the .mdebug section has an
5983 entsize of 0. FIXME: Does this matter? */
5984 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
5985 hdr->sh_entsize = 0;
5986 else
5987 hdr->sh_entsize = 1;
5989 else if (strcmp (name, ".reginfo") == 0)
5991 hdr->sh_type = SHT_MIPS_REGINFO;
5992 /* In a shared object on IRIX 5.3, the .reginfo section has an
5993 entsize of 0x18. FIXME: Does this matter? */
5994 if (SGI_COMPAT (abfd))
5996 if ((abfd->flags & DYNAMIC) != 0)
5997 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
5998 else
5999 hdr->sh_entsize = 1;
6001 else
6002 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6004 else if (SGI_COMPAT (abfd)
6005 && (strcmp (name, ".hash") == 0
6006 || strcmp (name, ".dynamic") == 0
6007 || strcmp (name, ".dynstr") == 0))
6009 if (SGI_COMPAT (abfd))
6010 hdr->sh_entsize = 0;
6011 #if 0
6012 /* This isn't how the IRIX6 linker behaves. */
6013 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
6014 #endif
6016 else if (strcmp (name, ".got") == 0
6017 || strcmp (name, ".srdata") == 0
6018 || strcmp (name, ".sdata") == 0
6019 || strcmp (name, ".sbss") == 0
6020 || strcmp (name, ".lit4") == 0
6021 || strcmp (name, ".lit8") == 0)
6022 hdr->sh_flags |= SHF_MIPS_GPREL;
6023 else if (strcmp (name, ".MIPS.interfaces") == 0)
6025 hdr->sh_type = SHT_MIPS_IFACE;
6026 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6028 else if (CONST_STRNEQ (name, ".MIPS.content"))
6030 hdr->sh_type = SHT_MIPS_CONTENT;
6031 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6032 /* The sh_info field is set in final_write_processing. */
6034 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
6036 hdr->sh_type = SHT_MIPS_OPTIONS;
6037 hdr->sh_entsize = 1;
6038 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6040 else if (CONST_STRNEQ (name, ".debug_")
6041 || CONST_STRNEQ (name, ".zdebug_"))
6043 hdr->sh_type = SHT_MIPS_DWARF;
6045 /* Irix facilities such as libexc expect a single .debug_frame
6046 per executable, the system ones have NOSTRIP set and the linker
6047 doesn't merge sections with different flags so ... */
6048 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
6049 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6051 else if (strcmp (name, ".MIPS.symlib") == 0)
6053 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
6054 /* The sh_link and sh_info fields are set in
6055 final_write_processing. */
6057 else if (CONST_STRNEQ (name, ".MIPS.events")
6058 || CONST_STRNEQ (name, ".MIPS.post_rel"))
6060 hdr->sh_type = SHT_MIPS_EVENTS;
6061 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6062 /* The sh_link field is set in final_write_processing. */
6064 else if (strcmp (name, ".msym") == 0)
6066 hdr->sh_type = SHT_MIPS_MSYM;
6067 hdr->sh_flags |= SHF_ALLOC;
6068 hdr->sh_entsize = 8;
6071 /* The generic elf_fake_sections will set up REL_HDR using the default
6072 kind of relocations. We used to set up a second header for the
6073 non-default kind of relocations here, but only NewABI would use
6074 these, and the IRIX ld doesn't like resulting empty RELA sections.
6075 Thus we create those header only on demand now. */
6077 return TRUE;
6080 /* Given a BFD section, try to locate the corresponding ELF section
6081 index. This is used by both the 32-bit and the 64-bit ABI.
6082 Actually, it's not clear to me that the 64-bit ABI supports these,
6083 but for non-PIC objects we will certainly want support for at least
6084 the .scommon section. */
6086 bfd_boolean
6087 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6088 asection *sec, int *retval)
6090 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
6092 *retval = SHN_MIPS_SCOMMON;
6093 return TRUE;
6095 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
6097 *retval = SHN_MIPS_ACOMMON;
6098 return TRUE;
6100 return FALSE;
6103 /* Hook called by the linker routine which adds symbols from an object
6104 file. We must handle the special MIPS section numbers here. */
6106 bfd_boolean
6107 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
6108 Elf_Internal_Sym *sym, const char **namep,
6109 flagword *flagsp ATTRIBUTE_UNUSED,
6110 asection **secp, bfd_vma *valp)
6112 if (SGI_COMPAT (abfd)
6113 && (abfd->flags & DYNAMIC) != 0
6114 && strcmp (*namep, "_rld_new_interface") == 0)
6116 /* Skip IRIX5 rld entry name. */
6117 *namep = NULL;
6118 return TRUE;
6121 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
6122 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
6123 by setting a DT_NEEDED for the shared object. Since _gp_disp is
6124 a magic symbol resolved by the linker, we ignore this bogus definition
6125 of _gp_disp. New ABI objects do not suffer from this problem so this
6126 is not done for them. */
6127 if (!NEWABI_P(abfd)
6128 && (sym->st_shndx == SHN_ABS)
6129 && (strcmp (*namep, "_gp_disp") == 0))
6131 *namep = NULL;
6132 return TRUE;
6135 switch (sym->st_shndx)
6137 case SHN_COMMON:
6138 /* Common symbols less than the GP size are automatically
6139 treated as SHN_MIPS_SCOMMON symbols. */
6140 if (sym->st_size > elf_gp_size (abfd)
6141 || ELF_ST_TYPE (sym->st_info) == STT_TLS
6142 || IRIX_COMPAT (abfd) == ict_irix6)
6143 break;
6144 /* Fall through. */
6145 case SHN_MIPS_SCOMMON:
6146 *secp = bfd_make_section_old_way (abfd, ".scommon");
6147 (*secp)->flags |= SEC_IS_COMMON;
6148 *valp = sym->st_size;
6149 break;
6151 case SHN_MIPS_TEXT:
6152 /* This section is used in a shared object. */
6153 if (elf_tdata (abfd)->elf_text_section == NULL)
6155 asymbol *elf_text_symbol;
6156 asection *elf_text_section;
6157 bfd_size_type amt = sizeof (asection);
6159 elf_text_section = bfd_zalloc (abfd, amt);
6160 if (elf_text_section == NULL)
6161 return FALSE;
6163 amt = sizeof (asymbol);
6164 elf_text_symbol = bfd_zalloc (abfd, amt);
6165 if (elf_text_symbol == NULL)
6166 return FALSE;
6168 /* Initialize the section. */
6170 elf_tdata (abfd)->elf_text_section = elf_text_section;
6171 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
6173 elf_text_section->symbol = elf_text_symbol;
6174 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
6176 elf_text_section->name = ".text";
6177 elf_text_section->flags = SEC_NO_FLAGS;
6178 elf_text_section->output_section = NULL;
6179 elf_text_section->owner = abfd;
6180 elf_text_symbol->name = ".text";
6181 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6182 elf_text_symbol->section = elf_text_section;
6184 /* This code used to do *secp = bfd_und_section_ptr if
6185 info->shared. I don't know why, and that doesn't make sense,
6186 so I took it out. */
6187 *secp = elf_tdata (abfd)->elf_text_section;
6188 break;
6190 case SHN_MIPS_ACOMMON:
6191 /* Fall through. XXX Can we treat this as allocated data? */
6192 case SHN_MIPS_DATA:
6193 /* This section is used in a shared object. */
6194 if (elf_tdata (abfd)->elf_data_section == NULL)
6196 asymbol *elf_data_symbol;
6197 asection *elf_data_section;
6198 bfd_size_type amt = sizeof (asection);
6200 elf_data_section = bfd_zalloc (abfd, amt);
6201 if (elf_data_section == NULL)
6202 return FALSE;
6204 amt = sizeof (asymbol);
6205 elf_data_symbol = bfd_zalloc (abfd, amt);
6206 if (elf_data_symbol == NULL)
6207 return FALSE;
6209 /* Initialize the section. */
6211 elf_tdata (abfd)->elf_data_section = elf_data_section;
6212 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
6214 elf_data_section->symbol = elf_data_symbol;
6215 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
6217 elf_data_section->name = ".data";
6218 elf_data_section->flags = SEC_NO_FLAGS;
6219 elf_data_section->output_section = NULL;
6220 elf_data_section->owner = abfd;
6221 elf_data_symbol->name = ".data";
6222 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6223 elf_data_symbol->section = elf_data_section;
6225 /* This code used to do *secp = bfd_und_section_ptr if
6226 info->shared. I don't know why, and that doesn't make sense,
6227 so I took it out. */
6228 *secp = elf_tdata (abfd)->elf_data_section;
6229 break;
6231 case SHN_MIPS_SUNDEFINED:
6232 *secp = bfd_und_section_ptr;
6233 break;
6236 if (SGI_COMPAT (abfd)
6237 && ! info->shared
6238 && info->output_bfd->xvec == abfd->xvec
6239 && strcmp (*namep, "__rld_obj_head") == 0)
6241 struct elf_link_hash_entry *h;
6242 struct bfd_link_hash_entry *bh;
6244 /* Mark __rld_obj_head as dynamic. */
6245 bh = NULL;
6246 if (! (_bfd_generic_link_add_one_symbol
6247 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
6248 get_elf_backend_data (abfd)->collect, &bh)))
6249 return FALSE;
6251 h = (struct elf_link_hash_entry *) bh;
6252 h->non_elf = 0;
6253 h->def_regular = 1;
6254 h->type = STT_OBJECT;
6256 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6257 return FALSE;
6259 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
6262 /* If this is a mips16 text symbol, add 1 to the value to make it
6263 odd. This will cause something like .word SYM to come up with
6264 the right value when it is loaded into the PC. */
6265 if (ELF_ST_IS_MIPS16 (sym->st_other))
6266 ++*valp;
6268 return TRUE;
6271 /* This hook function is called before the linker writes out a global
6272 symbol. We mark symbols as small common if appropriate. This is
6273 also where we undo the increment of the value for a mips16 symbol. */
6275 bfd_boolean
6276 _bfd_mips_elf_link_output_symbol_hook
6277 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6278 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
6279 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
6281 /* If we see a common symbol, which implies a relocatable link, then
6282 if a symbol was small common in an input file, mark it as small
6283 common in the output file. */
6284 if (sym->st_shndx == SHN_COMMON
6285 && strcmp (input_sec->name, ".scommon") == 0)
6286 sym->st_shndx = SHN_MIPS_SCOMMON;
6288 if (ELF_ST_IS_MIPS16 (sym->st_other))
6289 sym->st_value &= ~1;
6291 return TRUE;
6294 /* Functions for the dynamic linker. */
6296 /* Create dynamic sections when linking against a dynamic object. */
6298 bfd_boolean
6299 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
6301 struct elf_link_hash_entry *h;
6302 struct bfd_link_hash_entry *bh;
6303 flagword flags;
6304 register asection *s;
6305 const char * const *namep;
6306 struct mips_elf_link_hash_table *htab;
6308 htab = mips_elf_hash_table (info);
6309 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6310 | SEC_LINKER_CREATED | SEC_READONLY);
6312 /* The psABI requires a read-only .dynamic section, but the VxWorks
6313 EABI doesn't. */
6314 if (!htab->is_vxworks)
6316 s = bfd_get_section_by_name (abfd, ".dynamic");
6317 if (s != NULL)
6319 if (! bfd_set_section_flags (abfd, s, flags))
6320 return FALSE;
6324 /* We need to create .got section. */
6325 if (! mips_elf_create_got_section (abfd, info, FALSE))
6326 return FALSE;
6328 if (! mips_elf_rel_dyn_section (info, TRUE))
6329 return FALSE;
6331 /* Create .stub section. */
6332 s = bfd_make_section_with_flags (abfd,
6333 MIPS_ELF_STUB_SECTION_NAME (abfd),
6334 flags | SEC_CODE);
6335 if (s == NULL
6336 || ! bfd_set_section_alignment (abfd, s,
6337 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6338 return FALSE;
6339 htab->sstubs = s;
6341 if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
6342 && !info->shared
6343 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
6345 s = bfd_make_section_with_flags (abfd, ".rld_map",
6346 flags &~ (flagword) SEC_READONLY);
6347 if (s == NULL
6348 || ! bfd_set_section_alignment (abfd, s,
6349 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6350 return FALSE;
6353 /* On IRIX5, we adjust add some additional symbols and change the
6354 alignments of several sections. There is no ABI documentation
6355 indicating that this is necessary on IRIX6, nor any evidence that
6356 the linker takes such action. */
6357 if (IRIX_COMPAT (abfd) == ict_irix5)
6359 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
6361 bh = NULL;
6362 if (! (_bfd_generic_link_add_one_symbol
6363 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
6364 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
6365 return FALSE;
6367 h = (struct elf_link_hash_entry *) bh;
6368 h->non_elf = 0;
6369 h->def_regular = 1;
6370 h->type = STT_SECTION;
6372 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6373 return FALSE;
6376 /* We need to create a .compact_rel section. */
6377 if (SGI_COMPAT (abfd))
6379 if (!mips_elf_create_compact_rel_section (abfd, info))
6380 return FALSE;
6383 /* Change alignments of some sections. */
6384 s = bfd_get_section_by_name (abfd, ".hash");
6385 if (s != NULL)
6386 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6387 s = bfd_get_section_by_name (abfd, ".dynsym");
6388 if (s != NULL)
6389 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6390 s = bfd_get_section_by_name (abfd, ".dynstr");
6391 if (s != NULL)
6392 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6393 s = bfd_get_section_by_name (abfd, ".reginfo");
6394 if (s != NULL)
6395 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6396 s = bfd_get_section_by_name (abfd, ".dynamic");
6397 if (s != NULL)
6398 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6401 if (!info->shared)
6403 const char *name;
6405 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
6406 bh = NULL;
6407 if (!(_bfd_generic_link_add_one_symbol
6408 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
6409 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
6410 return FALSE;
6412 h = (struct elf_link_hash_entry *) bh;
6413 h->non_elf = 0;
6414 h->def_regular = 1;
6415 h->type = STT_SECTION;
6417 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6418 return FALSE;
6420 if (! mips_elf_hash_table (info)->use_rld_obj_head)
6422 /* __rld_map is a four byte word located in the .data section
6423 and is filled in by the rtld to contain a pointer to
6424 the _r_debug structure. Its symbol value will be set in
6425 _bfd_mips_elf_finish_dynamic_symbol. */
6426 s = bfd_get_section_by_name (abfd, ".rld_map");
6427 BFD_ASSERT (s != NULL);
6429 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
6430 bh = NULL;
6431 if (!(_bfd_generic_link_add_one_symbol
6432 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
6433 get_elf_backend_data (abfd)->collect, &bh)))
6434 return FALSE;
6436 h = (struct elf_link_hash_entry *) bh;
6437 h->non_elf = 0;
6438 h->def_regular = 1;
6439 h->type = STT_OBJECT;
6441 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6442 return FALSE;
6446 if (htab->is_vxworks)
6448 /* Create the .plt, .rela.plt, .dynbss and .rela.bss sections.
6449 Also create the _PROCEDURE_LINKAGE_TABLE symbol. */
6450 if (!_bfd_elf_create_dynamic_sections (abfd, info))
6451 return FALSE;
6453 /* Cache the sections created above. */
6454 htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
6455 htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
6456 htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
6457 htab->splt = bfd_get_section_by_name (abfd, ".plt");
6458 if (!htab->sdynbss
6459 || (!htab->srelbss && !info->shared)
6460 || !htab->srelplt
6461 || !htab->splt)
6462 abort ();
6464 /* Do the usual VxWorks handling. */
6465 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
6466 return FALSE;
6468 /* Work out the PLT sizes. */
6469 if (info->shared)
6471 htab->plt_header_size
6472 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
6473 htab->plt_entry_size
6474 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
6476 else
6478 htab->plt_header_size
6479 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
6480 htab->plt_entry_size
6481 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
6485 return TRUE;
6488 /* Return true if relocation REL against section SEC is a REL rather than
6489 RELA relocation. RELOCS is the first relocation in the section and
6490 ABFD is the bfd that contains SEC. */
6492 static bfd_boolean
6493 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
6494 const Elf_Internal_Rela *relocs,
6495 const Elf_Internal_Rela *rel)
6497 Elf_Internal_Shdr *rel_hdr;
6498 const struct elf_backend_data *bed;
6500 /* To determine which flavor or relocation this is, we depend on the
6501 fact that the INPUT_SECTION's REL_HDR is read before its REL_HDR2. */
6502 rel_hdr = &elf_section_data (sec)->rel_hdr;
6503 bed = get_elf_backend_data (abfd);
6504 if ((size_t) (rel - relocs)
6505 >= (NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel))
6506 rel_hdr = elf_section_data (sec)->rel_hdr2;
6507 return rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (abfd);
6510 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
6511 HOWTO is the relocation's howto and CONTENTS points to the contents
6512 of the section that REL is against. */
6514 static bfd_vma
6515 mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
6516 reloc_howto_type *howto, bfd_byte *contents)
6518 bfd_byte *location;
6519 unsigned int r_type;
6520 bfd_vma addend;
6522 r_type = ELF_R_TYPE (abfd, rel->r_info);
6523 location = contents + rel->r_offset;
6525 /* Get the addend, which is stored in the input file. */
6526 _bfd_mips16_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
6527 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
6528 _bfd_mips16_elf_reloc_shuffle (abfd, r_type, FALSE, location);
6530 return addend & howto->src_mask;
6533 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
6534 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
6535 and update *ADDEND with the final addend. Return true on success
6536 or false if the LO16 could not be found. RELEND is the exclusive
6537 upper bound on the relocations for REL's section. */
6539 static bfd_boolean
6540 mips_elf_add_lo16_rel_addend (bfd *abfd,
6541 const Elf_Internal_Rela *rel,
6542 const Elf_Internal_Rela *relend,
6543 bfd_byte *contents, bfd_vma *addend)
6545 unsigned int r_type, lo16_type;
6546 const Elf_Internal_Rela *lo16_relocation;
6547 reloc_howto_type *lo16_howto;
6548 bfd_vma l;
6550 r_type = ELF_R_TYPE (abfd, rel->r_info);
6551 if (mips16_reloc_p (r_type))
6552 lo16_type = R_MIPS16_LO16;
6553 else
6554 lo16_type = R_MIPS_LO16;
6556 /* The combined value is the sum of the HI16 addend, left-shifted by
6557 sixteen bits, and the LO16 addend, sign extended. (Usually, the
6558 code does a `lui' of the HI16 value, and then an `addiu' of the
6559 LO16 value.)
6561 Scan ahead to find a matching LO16 relocation.
6563 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
6564 be immediately following. However, for the IRIX6 ABI, the next
6565 relocation may be a composed relocation consisting of several
6566 relocations for the same address. In that case, the R_MIPS_LO16
6567 relocation may occur as one of these. We permit a similar
6568 extension in general, as that is useful for GCC.
6570 In some cases GCC dead code elimination removes the LO16 but keeps
6571 the corresponding HI16. This is strictly speaking a violation of
6572 the ABI but not immediately harmful. */
6573 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
6574 if (lo16_relocation == NULL)
6575 return FALSE;
6577 /* Obtain the addend kept there. */
6578 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
6579 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
6581 l <<= lo16_howto->rightshift;
6582 l = _bfd_mips_elf_sign_extend (l, 16);
6584 *addend <<= 16;
6585 *addend += l;
6586 return TRUE;
6589 /* Try to read the contents of section SEC in bfd ABFD. Return true and
6590 store the contents in *CONTENTS on success. Assume that *CONTENTS
6591 already holds the contents if it is nonull on entry. */
6593 static bfd_boolean
6594 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
6596 if (*contents)
6597 return TRUE;
6599 /* Get cached copy if it exists. */
6600 if (elf_section_data (sec)->this_hdr.contents != NULL)
6602 *contents = elf_section_data (sec)->this_hdr.contents;
6603 return TRUE;
6606 return bfd_malloc_and_get_section (abfd, sec, contents);
6609 /* Look through the relocs for a section during the first phase, and
6610 allocate space in the global offset table. */
6612 bfd_boolean
6613 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
6614 asection *sec, const Elf_Internal_Rela *relocs)
6616 const char *name;
6617 bfd *dynobj;
6618 Elf_Internal_Shdr *symtab_hdr;
6619 struct elf_link_hash_entry **sym_hashes;
6620 struct mips_got_info *g;
6621 size_t extsymoff;
6622 const Elf_Internal_Rela *rel;
6623 const Elf_Internal_Rela *rel_end;
6624 asection *sgot;
6625 asection *sreloc;
6626 const struct elf_backend_data *bed;
6627 struct mips_elf_link_hash_table *htab;
6628 bfd_byte *contents;
6629 bfd_vma addend;
6630 reloc_howto_type *howto;
6632 if (info->relocatable)
6633 return TRUE;
6635 htab = mips_elf_hash_table (info);
6636 dynobj = elf_hash_table (info)->dynobj;
6637 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6638 sym_hashes = elf_sym_hashes (abfd);
6639 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
6641 bed = get_elf_backend_data (abfd);
6642 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
6644 /* Check for the mips16 stub sections. */
6646 name = bfd_get_section_name (abfd, sec);
6647 if (FN_STUB_P (name))
6649 unsigned long r_symndx;
6651 /* Look at the relocation information to figure out which symbol
6652 this is for. */
6654 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
6655 if (r_symndx == 0)
6657 (*_bfd_error_handler)
6658 (_("%B: Warning: cannot determine the target function for"
6659 " stub section `%s'"),
6660 abfd, name);
6661 bfd_set_error (bfd_error_bad_value);
6662 return FALSE;
6665 if (r_symndx < extsymoff
6666 || sym_hashes[r_symndx - extsymoff] == NULL)
6668 asection *o;
6670 /* This stub is for a local symbol. This stub will only be
6671 needed if there is some relocation in this BFD, other
6672 than a 16 bit function call, which refers to this symbol. */
6673 for (o = abfd->sections; o != NULL; o = o->next)
6675 Elf_Internal_Rela *sec_relocs;
6676 const Elf_Internal_Rela *r, *rend;
6678 /* We can ignore stub sections when looking for relocs. */
6679 if ((o->flags & SEC_RELOC) == 0
6680 || o->reloc_count == 0
6681 || section_allows_mips16_refs_p (o))
6682 continue;
6684 sec_relocs
6685 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
6686 info->keep_memory);
6687 if (sec_relocs == NULL)
6688 return FALSE;
6690 rend = sec_relocs + o->reloc_count;
6691 for (r = sec_relocs; r < rend; r++)
6692 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
6693 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
6694 break;
6696 if (elf_section_data (o)->relocs != sec_relocs)
6697 free (sec_relocs);
6699 if (r < rend)
6700 break;
6703 if (o == NULL)
6705 /* There is no non-call reloc for this stub, so we do
6706 not need it. Since this function is called before
6707 the linker maps input sections to output sections, we
6708 can easily discard it by setting the SEC_EXCLUDE
6709 flag. */
6710 sec->flags |= SEC_EXCLUDE;
6711 return TRUE;
6714 /* Record this stub in an array of local symbol stubs for
6715 this BFD. */
6716 if (elf_tdata (abfd)->local_stubs == NULL)
6718 unsigned long symcount;
6719 asection **n;
6720 bfd_size_type amt;
6722 if (elf_bad_symtab (abfd))
6723 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
6724 else
6725 symcount = symtab_hdr->sh_info;
6726 amt = symcount * sizeof (asection *);
6727 n = bfd_zalloc (abfd, amt);
6728 if (n == NULL)
6729 return FALSE;
6730 elf_tdata (abfd)->local_stubs = n;
6733 sec->flags |= SEC_KEEP;
6734 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
6736 /* We don't need to set mips16_stubs_seen in this case.
6737 That flag is used to see whether we need to look through
6738 the global symbol table for stubs. We don't need to set
6739 it here, because we just have a local stub. */
6741 else
6743 struct mips_elf_link_hash_entry *h;
6745 h = ((struct mips_elf_link_hash_entry *)
6746 sym_hashes[r_symndx - extsymoff]);
6748 while (h->root.root.type == bfd_link_hash_indirect
6749 || h->root.root.type == bfd_link_hash_warning)
6750 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
6752 /* H is the symbol this stub is for. */
6754 /* If we already have an appropriate stub for this function, we
6755 don't need another one, so we can discard this one. Since
6756 this function is called before the linker maps input sections
6757 to output sections, we can easily discard it by setting the
6758 SEC_EXCLUDE flag. */
6759 if (h->fn_stub != NULL)
6761 sec->flags |= SEC_EXCLUDE;
6762 return TRUE;
6765 sec->flags |= SEC_KEEP;
6766 h->fn_stub = sec;
6767 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
6770 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
6772 unsigned long r_symndx;
6773 struct mips_elf_link_hash_entry *h;
6774 asection **loc;
6776 /* Look at the relocation information to figure out which symbol
6777 this is for. */
6779 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
6780 if (r_symndx == 0)
6782 (*_bfd_error_handler)
6783 (_("%B: Warning: cannot determine the target function for"
6784 " stub section `%s'"),
6785 abfd, name);
6786 bfd_set_error (bfd_error_bad_value);
6787 return FALSE;
6790 if (r_symndx < extsymoff
6791 || sym_hashes[r_symndx - extsymoff] == NULL)
6793 asection *o;
6795 /* This stub is for a local symbol. This stub will only be
6796 needed if there is some relocation (R_MIPS16_26) in this BFD
6797 that refers to this symbol. */
6798 for (o = abfd->sections; o != NULL; o = o->next)
6800 Elf_Internal_Rela *sec_relocs;
6801 const Elf_Internal_Rela *r, *rend;
6803 /* We can ignore stub sections when looking for relocs. */
6804 if ((o->flags & SEC_RELOC) == 0
6805 || o->reloc_count == 0
6806 || section_allows_mips16_refs_p (o))
6807 continue;
6809 sec_relocs
6810 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
6811 info->keep_memory);
6812 if (sec_relocs == NULL)
6813 return FALSE;
6815 rend = sec_relocs + o->reloc_count;
6816 for (r = sec_relocs; r < rend; r++)
6817 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
6818 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
6819 break;
6821 if (elf_section_data (o)->relocs != sec_relocs)
6822 free (sec_relocs);
6824 if (r < rend)
6825 break;
6828 if (o == NULL)
6830 /* There is no non-call reloc for this stub, so we do
6831 not need it. Since this function is called before
6832 the linker maps input sections to output sections, we
6833 can easily discard it by setting the SEC_EXCLUDE
6834 flag. */
6835 sec->flags |= SEC_EXCLUDE;
6836 return TRUE;
6839 /* Record this stub in an array of local symbol call_stubs for
6840 this BFD. */
6841 if (elf_tdata (abfd)->local_call_stubs == NULL)
6843 unsigned long symcount;
6844 asection **n;
6845 bfd_size_type amt;
6847 if (elf_bad_symtab (abfd))
6848 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
6849 else
6850 symcount = symtab_hdr->sh_info;
6851 amt = symcount * sizeof (asection *);
6852 n = bfd_zalloc (abfd, amt);
6853 if (n == NULL)
6854 return FALSE;
6855 elf_tdata (abfd)->local_call_stubs = n;
6858 sec->flags |= SEC_KEEP;
6859 elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
6861 /* We don't need to set mips16_stubs_seen in this case.
6862 That flag is used to see whether we need to look through
6863 the global symbol table for stubs. We don't need to set
6864 it here, because we just have a local stub. */
6866 else
6868 h = ((struct mips_elf_link_hash_entry *)
6869 sym_hashes[r_symndx - extsymoff]);
6871 /* H is the symbol this stub is for. */
6873 if (CALL_FP_STUB_P (name))
6874 loc = &h->call_fp_stub;
6875 else
6876 loc = &h->call_stub;
6878 /* If we already have an appropriate stub for this function, we
6879 don't need another one, so we can discard this one. Since
6880 this function is called before the linker maps input sections
6881 to output sections, we can easily discard it by setting the
6882 SEC_EXCLUDE flag. */
6883 if (*loc != NULL)
6885 sec->flags |= SEC_EXCLUDE;
6886 return TRUE;
6889 sec->flags |= SEC_KEEP;
6890 *loc = sec;
6891 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
6895 if (dynobj == NULL)
6897 sgot = NULL;
6898 g = NULL;
6900 else
6902 sgot = mips_elf_got_section (dynobj, FALSE);
6903 if (sgot == NULL)
6904 g = NULL;
6905 else
6907 BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
6908 g = mips_elf_section_data (sgot)->u.got_info;
6909 BFD_ASSERT (g != NULL);
6913 sreloc = NULL;
6914 contents = NULL;
6915 for (rel = relocs; rel < rel_end; ++rel)
6917 unsigned long r_symndx;
6918 unsigned int r_type;
6919 struct elf_link_hash_entry *h;
6921 r_symndx = ELF_R_SYM (abfd, rel->r_info);
6922 r_type = ELF_R_TYPE (abfd, rel->r_info);
6924 if (r_symndx < extsymoff)
6925 h = NULL;
6926 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
6928 (*_bfd_error_handler)
6929 (_("%B: Malformed reloc detected for section %s"),
6930 abfd, name);
6931 bfd_set_error (bfd_error_bad_value);
6932 return FALSE;
6934 else
6936 h = sym_hashes[r_symndx - extsymoff];
6938 /* This may be an indirect symbol created because of a version. */
6939 if (h != NULL)
6941 while (h->root.type == bfd_link_hash_indirect)
6942 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6946 /* Some relocs require a global offset table. */
6947 if (dynobj == NULL || sgot == NULL)
6949 switch (r_type)
6951 case R_MIPS16_GOT16:
6952 case R_MIPS16_CALL16:
6953 case R_MIPS_GOT16:
6954 case R_MIPS_CALL16:
6955 case R_MIPS_CALL_HI16:
6956 case R_MIPS_CALL_LO16:
6957 case R_MIPS_GOT_HI16:
6958 case R_MIPS_GOT_LO16:
6959 case R_MIPS_GOT_PAGE:
6960 case R_MIPS_GOT_OFST:
6961 case R_MIPS_GOT_DISP:
6962 case R_MIPS_TLS_GOTTPREL:
6963 case R_MIPS_TLS_GD:
6964 case R_MIPS_TLS_LDM:
6965 if (dynobj == NULL)
6966 elf_hash_table (info)->dynobj = dynobj = abfd;
6967 if (! mips_elf_create_got_section (dynobj, info, FALSE))
6968 return FALSE;
6969 g = mips_elf_got_info (dynobj, &sgot);
6970 if (htab->is_vxworks && !info->shared)
6972 (*_bfd_error_handler)
6973 (_("%B: GOT reloc at 0x%lx not expected in executables"),
6974 abfd, (unsigned long) rel->r_offset);
6975 bfd_set_error (bfd_error_bad_value);
6976 return FALSE;
6978 break;
6980 case R_MIPS_32:
6981 case R_MIPS_REL32:
6982 case R_MIPS_64:
6983 /* In VxWorks executables, references to external symbols
6984 are handled using copy relocs or PLT stubs, so there's
6985 no need to add a dynamic relocation here. */
6986 if (dynobj == NULL
6987 && (info->shared || (h != NULL && !htab->is_vxworks))
6988 && (sec->flags & SEC_ALLOC) != 0)
6989 elf_hash_table (info)->dynobj = dynobj = abfd;
6990 break;
6992 default:
6993 break;
6997 if (h)
6999 ((struct mips_elf_link_hash_entry *) h)->is_relocation_target = TRUE;
7001 /* Relocations against the special VxWorks __GOTT_BASE__ and
7002 __GOTT_INDEX__ symbols must be left to the loader. Allocate
7003 room for them in .rela.dyn. */
7004 if (is_gott_symbol (info, h))
7006 if (sreloc == NULL)
7008 sreloc = mips_elf_rel_dyn_section (info, TRUE);
7009 if (sreloc == NULL)
7010 return FALSE;
7012 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
7013 if (MIPS_ELF_READONLY_SECTION (sec))
7014 /* We tell the dynamic linker that there are
7015 relocations against the text segment. */
7016 info->flags |= DF_TEXTREL;
7019 else if (r_type == R_MIPS_CALL_LO16
7020 || r_type == R_MIPS_GOT_LO16
7021 || r_type == R_MIPS_GOT_DISP
7022 || (got16_reloc_p (r_type) && htab->is_vxworks))
7024 /* We may need a local GOT entry for this relocation. We
7025 don't count R_MIPS_GOT_PAGE because we can estimate the
7026 maximum number of pages needed by looking at the size of
7027 the segment. Similar comments apply to R_MIPS*_GOT16 and
7028 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
7029 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
7030 R_MIPS_CALL_HI16 because these are always followed by an
7031 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
7032 if (! mips_elf_record_local_got_symbol (abfd, r_symndx,
7033 rel->r_addend, g, 0))
7034 return FALSE;
7037 switch (r_type)
7039 case R_MIPS_CALL16:
7040 case R_MIPS16_CALL16:
7041 if (h == NULL)
7043 (*_bfd_error_handler)
7044 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
7045 abfd, (unsigned long) rel->r_offset);
7046 bfd_set_error (bfd_error_bad_value);
7047 return FALSE;
7049 /* Fall through. */
7051 case R_MIPS_CALL_HI16:
7052 case R_MIPS_CALL_LO16:
7053 if (h != NULL)
7055 /* VxWorks call relocations point the function's .got.plt
7056 entry, which will be allocated by adjust_dynamic_symbol.
7057 Otherwise, this symbol requires a global GOT entry. */
7058 if ((!htab->is_vxworks || h->forced_local)
7059 && !mips_elf_record_global_got_symbol (h, abfd, info, g, 0))
7060 return FALSE;
7062 /* We need a stub, not a plt entry for the undefined
7063 function. But we record it as if it needs plt. See
7064 _bfd_elf_adjust_dynamic_symbol. */
7065 h->needs_plt = 1;
7066 h->type = STT_FUNC;
7068 break;
7070 case R_MIPS_GOT_PAGE:
7071 /* If this is a global, overridable symbol, GOT_PAGE will
7072 decay to GOT_DISP, so we'll need a GOT entry for it. */
7073 if (h)
7075 struct mips_elf_link_hash_entry *hmips =
7076 (struct mips_elf_link_hash_entry *) h;
7078 while (hmips->root.root.type == bfd_link_hash_indirect
7079 || hmips->root.root.type == bfd_link_hash_warning)
7080 hmips = (struct mips_elf_link_hash_entry *)
7081 hmips->root.root.u.i.link;
7083 /* This symbol is definitely not overridable. */
7084 if (hmips->root.def_regular
7085 && ! (info->shared && ! info->symbolic
7086 && ! hmips->root.forced_local))
7087 h = NULL;
7089 /* Fall through. */
7091 case R_MIPS16_GOT16:
7092 case R_MIPS_GOT16:
7093 case R_MIPS_GOT_HI16:
7094 case R_MIPS_GOT_LO16:
7095 if (!h || r_type == R_MIPS_GOT_PAGE)
7097 /* This relocation needs (or may need, if h != NULL) a
7098 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
7099 know for sure until we know whether the symbol is
7100 preemptible. */
7101 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
7103 if (!mips_elf_get_section_contents (abfd, sec, &contents))
7104 return FALSE;
7105 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7106 addend = mips_elf_read_rel_addend (abfd, rel,
7107 howto, contents);
7108 if (r_type == R_MIPS_GOT16)
7109 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
7110 contents, &addend);
7111 else
7112 addend <<= howto->rightshift;
7114 else
7115 addend = rel->r_addend;
7116 if (!mips_elf_record_got_page_entry (abfd, r_symndx, addend, g))
7117 return FALSE;
7118 break;
7120 /* Fall through. */
7122 case R_MIPS_GOT_DISP:
7123 if (h && ! mips_elf_record_global_got_symbol (h, abfd, info, g, 0))
7124 return FALSE;
7125 break;
7127 case R_MIPS_TLS_GOTTPREL:
7128 if (info->shared)
7129 info->flags |= DF_STATIC_TLS;
7130 /* Fall through */
7132 case R_MIPS_TLS_LDM:
7133 if (r_type == R_MIPS_TLS_LDM)
7135 r_symndx = 0;
7136 h = NULL;
7138 /* Fall through */
7140 case R_MIPS_TLS_GD:
7141 /* This symbol requires a global offset table entry, or two
7142 for TLS GD relocations. */
7144 unsigned char flag = (r_type == R_MIPS_TLS_GD
7145 ? GOT_TLS_GD
7146 : r_type == R_MIPS_TLS_LDM
7147 ? GOT_TLS_LDM
7148 : GOT_TLS_IE);
7149 if (h != NULL)
7151 struct mips_elf_link_hash_entry *hmips =
7152 (struct mips_elf_link_hash_entry *) h;
7153 hmips->tls_type |= flag;
7155 if (h && ! mips_elf_record_global_got_symbol (h, abfd, info, g, flag))
7156 return FALSE;
7158 else
7160 BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != 0);
7162 if (! mips_elf_record_local_got_symbol (abfd, r_symndx,
7163 rel->r_addend, g, flag))
7164 return FALSE;
7167 break;
7169 case R_MIPS_32:
7170 case R_MIPS_REL32:
7171 case R_MIPS_64:
7172 /* In VxWorks executables, references to external symbols
7173 are handled using copy relocs or PLT stubs, so there's
7174 no need to add a .rela.dyn entry for this relocation. */
7175 if ((info->shared || (h != NULL && !htab->is_vxworks))
7176 && !(h && strcmp (h->root.root.string, "__gnu_local_gp") == 0)
7177 && (sec->flags & SEC_ALLOC) != 0)
7179 if (sreloc == NULL)
7181 sreloc = mips_elf_rel_dyn_section (info, TRUE);
7182 if (sreloc == NULL)
7183 return FALSE;
7185 if (info->shared && h == NULL)
7187 /* When creating a shared object, we must copy these
7188 reloc types into the output file as R_MIPS_REL32
7189 relocs. Make room for this reloc in .rel(a).dyn. */
7190 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
7191 if (MIPS_ELF_READONLY_SECTION (sec))
7192 /* We tell the dynamic linker that there are
7193 relocations against the text segment. */
7194 info->flags |= DF_TEXTREL;
7196 else
7198 struct mips_elf_link_hash_entry *hmips;
7200 /* For a shared object, we must copy this relocation
7201 unless the symbol turns out to be undefined and
7202 weak with non-default visibility, in which case
7203 it will be left as zero.
7205 We could elide R_MIPS_REL32 for locally binding symbols
7206 in shared libraries, but do not yet do so.
7208 For an executable, we only need to copy this
7209 reloc if the symbol is defined in a dynamic
7210 object. */
7211 hmips = (struct mips_elf_link_hash_entry *) h;
7212 ++hmips->possibly_dynamic_relocs;
7213 if (MIPS_ELF_READONLY_SECTION (sec))
7214 /* We need it to tell the dynamic linker if there
7215 are relocations against the text segment. */
7216 hmips->readonly_reloc = TRUE;
7219 /* Even though we don't directly need a GOT entry for
7220 this symbol, a symbol must have a dynamic symbol
7221 table index greater that DT_MIPS_GOTSYM if there are
7222 dynamic relocations against it. This does not apply
7223 to VxWorks, which does not have the usual coupling
7224 between global GOT entries and .dynsym entries. */
7225 if (h != NULL && !htab->is_vxworks)
7227 if (dynobj == NULL)
7228 elf_hash_table (info)->dynobj = dynobj = abfd;
7229 if (! mips_elf_create_got_section (dynobj, info, TRUE))
7230 return FALSE;
7231 g = mips_elf_got_info (dynobj, &sgot);
7232 if (! mips_elf_record_global_got_symbol (h, abfd, info, g, 0))
7233 return FALSE;
7237 if (SGI_COMPAT (abfd))
7238 mips_elf_hash_table (info)->compact_rel_size +=
7239 sizeof (Elf32_External_crinfo);
7240 break;
7242 case R_MIPS_PC16:
7243 if (h)
7244 ((struct mips_elf_link_hash_entry *) h)->is_branch_target = TRUE;
7245 break;
7247 case R_MIPS_26:
7248 if (h)
7249 ((struct mips_elf_link_hash_entry *) h)->is_branch_target = TRUE;
7250 /* Fall through. */
7252 case R_MIPS_GPREL16:
7253 case R_MIPS_LITERAL:
7254 case R_MIPS_GPREL32:
7255 if (SGI_COMPAT (abfd))
7256 mips_elf_hash_table (info)->compact_rel_size +=
7257 sizeof (Elf32_External_crinfo);
7258 break;
7260 /* This relocation describes the C++ object vtable hierarchy.
7261 Reconstruct it for later use during GC. */
7262 case R_MIPS_GNU_VTINHERIT:
7263 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7264 return FALSE;
7265 break;
7267 /* This relocation describes which C++ vtable entries are actually
7268 used. Record for later use during GC. */
7269 case R_MIPS_GNU_VTENTRY:
7270 BFD_ASSERT (h != NULL);
7271 if (h != NULL
7272 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7273 return FALSE;
7274 break;
7276 default:
7277 break;
7280 /* We must not create a stub for a symbol that has relocations
7281 related to taking the function's address. This doesn't apply to
7282 VxWorks, where CALL relocs refer to a .got.plt entry instead of
7283 a normal .got entry. */
7284 if (!htab->is_vxworks && h != NULL)
7285 switch (r_type)
7287 default:
7288 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
7289 break;
7290 case R_MIPS16_CALL16:
7291 case R_MIPS_CALL16:
7292 case R_MIPS_CALL_HI16:
7293 case R_MIPS_CALL_LO16:
7294 case R_MIPS_JALR:
7295 break;
7298 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
7299 if there is one. We only need to handle global symbols here;
7300 we decide whether to keep or delete stubs for local symbols
7301 when processing the stub's relocations. */
7302 if (h != NULL
7303 && !mips16_call_reloc_p (r_type)
7304 && !section_allows_mips16_refs_p (sec))
7306 struct mips_elf_link_hash_entry *mh;
7308 mh = (struct mips_elf_link_hash_entry *) h;
7309 mh->need_fn_stub = TRUE;
7313 return TRUE;
7316 bfd_boolean
7317 _bfd_mips_relax_section (bfd *abfd, asection *sec,
7318 struct bfd_link_info *link_info,
7319 bfd_boolean *again)
7321 Elf_Internal_Rela *internal_relocs;
7322 Elf_Internal_Rela *irel, *irelend;
7323 Elf_Internal_Shdr *symtab_hdr;
7324 bfd_byte *contents = NULL;
7325 size_t extsymoff;
7326 bfd_boolean changed_contents = FALSE;
7327 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
7328 Elf_Internal_Sym *isymbuf = NULL;
7330 /* We are not currently changing any sizes, so only one pass. */
7331 *again = FALSE;
7333 if (link_info->relocatable)
7334 return TRUE;
7336 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7337 link_info->keep_memory);
7338 if (internal_relocs == NULL)
7339 return TRUE;
7341 irelend = internal_relocs + sec->reloc_count
7342 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
7343 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7344 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7346 for (irel = internal_relocs; irel < irelend; irel++)
7348 bfd_vma symval;
7349 bfd_signed_vma sym_offset;
7350 unsigned int r_type;
7351 unsigned long r_symndx;
7352 asection *sym_sec;
7353 unsigned long instruction;
7355 /* Turn jalr into bgezal, and jr into beq, if they're marked
7356 with a JALR relocation, that indicate where they jump to.
7357 This saves some pipeline bubbles. */
7358 r_type = ELF_R_TYPE (abfd, irel->r_info);
7359 if (r_type != R_MIPS_JALR)
7360 continue;
7362 r_symndx = ELF_R_SYM (abfd, irel->r_info);
7363 /* Compute the address of the jump target. */
7364 if (r_symndx >= extsymoff)
7366 struct mips_elf_link_hash_entry *h
7367 = ((struct mips_elf_link_hash_entry *)
7368 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
7370 while (h->root.root.type == bfd_link_hash_indirect
7371 || h->root.root.type == bfd_link_hash_warning)
7372 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7374 /* If a symbol is undefined, or if it may be overridden,
7375 skip it. */
7376 if (! ((h->root.root.type == bfd_link_hash_defined
7377 || h->root.root.type == bfd_link_hash_defweak)
7378 && h->root.root.u.def.section)
7379 || (link_info->shared && ! link_info->symbolic
7380 && !h->root.forced_local))
7381 continue;
7383 sym_sec = h->root.root.u.def.section;
7384 if (sym_sec->output_section)
7385 symval = (h->root.root.u.def.value
7386 + sym_sec->output_section->vma
7387 + sym_sec->output_offset);
7388 else
7389 symval = h->root.root.u.def.value;
7391 else
7393 Elf_Internal_Sym *isym;
7395 /* Read this BFD's symbols if we haven't done so already. */
7396 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
7398 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
7399 if (isymbuf == NULL)
7400 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
7401 symtab_hdr->sh_info, 0,
7402 NULL, NULL, NULL);
7403 if (isymbuf == NULL)
7404 goto relax_return;
7407 isym = isymbuf + r_symndx;
7408 if (isym->st_shndx == SHN_UNDEF)
7409 continue;
7410 else if (isym->st_shndx == SHN_ABS)
7411 sym_sec = bfd_abs_section_ptr;
7412 else if (isym->st_shndx == SHN_COMMON)
7413 sym_sec = bfd_com_section_ptr;
7414 else
7415 sym_sec
7416 = bfd_section_from_elf_index (abfd, isym->st_shndx);
7417 symval = isym->st_value
7418 + sym_sec->output_section->vma
7419 + sym_sec->output_offset;
7422 /* Compute branch offset, from delay slot of the jump to the
7423 branch target. */
7424 sym_offset = (symval + irel->r_addend)
7425 - (sec_start + irel->r_offset + 4);
7427 /* Branch offset must be properly aligned. */
7428 if ((sym_offset & 3) != 0)
7429 continue;
7431 sym_offset >>= 2;
7433 /* Check that it's in range. */
7434 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
7435 continue;
7437 /* Get the section contents if we haven't done so already. */
7438 if (!mips_elf_get_section_contents (abfd, sec, &contents))
7439 goto relax_return;
7441 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
7443 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
7444 if ((instruction & 0xfc1fffff) == 0x0000f809)
7445 instruction = 0x04110000;
7446 /* If it was jr <reg>, turn it into b <target>. */
7447 else if ((instruction & 0xfc1fffff) == 0x00000008)
7448 instruction = 0x10000000;
7449 else
7450 continue;
7452 instruction |= (sym_offset & 0xffff);
7453 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
7454 changed_contents = TRUE;
7457 if (contents != NULL
7458 && elf_section_data (sec)->this_hdr.contents != contents)
7460 if (!changed_contents && !link_info->keep_memory)
7461 free (contents);
7462 else
7464 /* Cache the section contents for elf_link_input_bfd. */
7465 elf_section_data (sec)->this_hdr.contents = contents;
7468 return TRUE;
7470 relax_return:
7471 if (contents != NULL
7472 && elf_section_data (sec)->this_hdr.contents != contents)
7473 free (contents);
7474 return FALSE;
7477 /* Allocate space for global sym dynamic relocs. */
7479 static bfd_boolean
7480 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
7482 struct bfd_link_info *info = inf;
7483 bfd *dynobj;
7484 struct mips_elf_link_hash_entry *hmips;
7485 struct mips_elf_link_hash_table *htab;
7487 htab = mips_elf_hash_table (info);
7488 dynobj = elf_hash_table (info)->dynobj;
7489 hmips = (struct mips_elf_link_hash_entry *) h;
7491 /* VxWorks executables are handled elsewhere; we only need to
7492 allocate relocations in shared objects. */
7493 if (htab->is_vxworks && !info->shared)
7494 return TRUE;
7496 /* If this symbol is defined in a dynamic object, or we are creating
7497 a shared library, we will need to copy any R_MIPS_32 or
7498 R_MIPS_REL32 relocs against it into the output file. */
7499 if (! info->relocatable
7500 && hmips->possibly_dynamic_relocs != 0
7501 && (h->root.type == bfd_link_hash_defweak
7502 || !h->def_regular
7503 || info->shared))
7505 bfd_boolean do_copy = TRUE;
7507 if (h->root.type == bfd_link_hash_undefweak)
7509 /* Do not copy relocations for undefined weak symbols with
7510 non-default visibility. */
7511 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
7512 do_copy = FALSE;
7514 /* Make sure undefined weak symbols are output as a dynamic
7515 symbol in PIEs. */
7516 else if (h->dynindx == -1 && !h->forced_local)
7518 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7519 return FALSE;
7523 if (do_copy)
7525 mips_elf_allocate_dynamic_relocations
7526 (dynobj, info, hmips->possibly_dynamic_relocs);
7527 if (hmips->readonly_reloc)
7528 /* We tell the dynamic linker that there are relocations
7529 against the text segment. */
7530 info->flags |= DF_TEXTREL;
7534 return TRUE;
7537 /* Adjust a symbol defined by a dynamic object and referenced by a
7538 regular object. The current definition is in some section of the
7539 dynamic object, but we're not including those sections. We have to
7540 change the definition to something the rest of the link can
7541 understand. */
7543 bfd_boolean
7544 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7545 struct elf_link_hash_entry *h)
7547 bfd *dynobj;
7548 struct mips_elf_link_hash_entry *hmips;
7549 struct mips_elf_link_hash_table *htab;
7551 htab = mips_elf_hash_table (info);
7552 dynobj = elf_hash_table (info)->dynobj;
7554 /* Make sure we know what is going on here. */
7555 BFD_ASSERT (dynobj != NULL
7556 && (h->needs_plt
7557 || h->u.weakdef != NULL
7558 || (h->def_dynamic
7559 && h->ref_regular
7560 && !h->def_regular)));
7562 hmips = (struct mips_elf_link_hash_entry *) h;
7564 /* For a function, create a stub, if allowed. */
7565 if (! hmips->no_fn_stub
7566 && h->needs_plt)
7568 if (! elf_hash_table (info)->dynamic_sections_created)
7569 return TRUE;
7571 /* If this symbol is not defined in a regular file, then set
7572 the symbol to the stub location. This is required to make
7573 function pointers compare as equal between the normal
7574 executable and the shared library. */
7575 if (!h->def_regular)
7577 /* We need .stub section. */
7578 h->root.u.def.section = htab->sstubs;
7579 h->root.u.def.value = htab->sstubs->size;
7581 /* XXX Write this stub address somewhere. */
7582 h->plt.offset = htab->sstubs->size;
7584 /* Make room for this stub code. */
7585 htab->sstubs->size += htab->function_stub_size;
7587 /* The last half word of the stub will be filled with the index
7588 of this symbol in .dynsym section. */
7589 return TRUE;
7592 else if ((h->type == STT_FUNC)
7593 && !h->needs_plt)
7595 /* This will set the entry for this symbol in the GOT to 0, and
7596 the dynamic linker will take care of this. */
7597 h->root.u.def.value = 0;
7598 return TRUE;
7601 /* If this is a weak symbol, and there is a real definition, the
7602 processor independent code will have arranged for us to see the
7603 real definition first, and we can just use the same value. */
7604 if (h->u.weakdef != NULL)
7606 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7607 || h->u.weakdef->root.type == bfd_link_hash_defweak);
7608 h->root.u.def.section = h->u.weakdef->root.u.def.section;
7609 h->root.u.def.value = h->u.weakdef->root.u.def.value;
7610 return TRUE;
7613 /* This is a reference to a symbol defined by a dynamic object which
7614 is not a function. */
7616 return TRUE;
7619 /* Likewise, for VxWorks. */
7621 bfd_boolean
7622 _bfd_mips_vxworks_adjust_dynamic_symbol (struct bfd_link_info *info,
7623 struct elf_link_hash_entry *h)
7625 bfd *dynobj;
7626 struct mips_elf_link_hash_entry *hmips;
7627 struct mips_elf_link_hash_table *htab;
7629 htab = mips_elf_hash_table (info);
7630 dynobj = elf_hash_table (info)->dynobj;
7631 hmips = (struct mips_elf_link_hash_entry *) h;
7633 /* Make sure we know what is going on here. */
7634 BFD_ASSERT (dynobj != NULL
7635 && (h->needs_plt
7636 || h->needs_copy
7637 || h->u.weakdef != NULL
7638 || (h->def_dynamic
7639 && h->ref_regular
7640 && !h->def_regular)));
7642 /* If the symbol is defined by a dynamic object, we need a PLT stub if
7643 either (a) we want to branch to the symbol or (b) we're linking an
7644 executable that needs a canonical function address. In the latter
7645 case, the canonical address will be the address of the executable's
7646 load stub. */
7647 if ((hmips->is_branch_target
7648 || (!info->shared
7649 && h->type == STT_FUNC
7650 && hmips->is_relocation_target))
7651 && h->def_dynamic
7652 && h->ref_regular
7653 && !h->def_regular
7654 && !h->forced_local)
7655 h->needs_plt = 1;
7657 /* Locally-binding symbols do not need a PLT stub; we can refer to
7658 the functions directly. */
7659 else if (h->needs_plt
7660 && (SYMBOL_CALLS_LOCAL (info, h)
7661 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
7662 && h->root.type == bfd_link_hash_undefweak)))
7664 h->needs_plt = 0;
7665 return TRUE;
7668 if (h->needs_plt)
7670 /* If this is the first symbol to need a PLT entry, allocate room
7671 for the header, and for the header's .rela.plt.unloaded entries. */
7672 if (htab->splt->size == 0)
7674 htab->splt->size += htab->plt_header_size;
7675 if (!info->shared)
7676 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
7679 /* Assign the next .plt entry to this symbol. */
7680 h->plt.offset = htab->splt->size;
7681 htab->splt->size += htab->plt_entry_size;
7683 /* If the output file has no definition of the symbol, set the
7684 symbol's value to the address of the stub. Point at the PLT
7685 load stub rather than the lazy resolution stub; this stub
7686 will become the canonical function address. */
7687 if (!info->shared && !h->def_regular)
7689 h->root.u.def.section = htab->splt;
7690 h->root.u.def.value = h->plt.offset;
7691 h->root.u.def.value += 8;
7694 /* Make room for the .got.plt entry and the R_JUMP_SLOT relocation. */
7695 htab->sgotplt->size += 4;
7696 htab->srelplt->size += sizeof (Elf32_External_Rela);
7698 /* Make room for the .rela.plt.unloaded relocations. */
7699 if (!info->shared)
7700 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
7702 return TRUE;
7705 /* If a function symbol is defined by a dynamic object, and we do not
7706 need a PLT stub for it, the symbol's value should be zero. */
7707 if (h->type == STT_FUNC
7708 && h->def_dynamic
7709 && h->ref_regular
7710 && !h->def_regular)
7712 h->root.u.def.value = 0;
7713 return TRUE;
7716 /* If this is a weak symbol, and there is a real definition, the
7717 processor independent code will have arranged for us to see the
7718 real definition first, and we can just use the same value. */
7719 if (h->u.weakdef != NULL)
7721 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7722 || h->u.weakdef->root.type == bfd_link_hash_defweak);
7723 h->root.u.def.section = h->u.weakdef->root.u.def.section;
7724 h->root.u.def.value = h->u.weakdef->root.u.def.value;
7725 return TRUE;
7728 /* This is a reference to a symbol defined by a dynamic object which
7729 is not a function. */
7730 if (info->shared)
7731 return TRUE;
7733 /* We must allocate the symbol in our .dynbss section, which will
7734 become part of the .bss section of the executable. There will be
7735 an entry for this symbol in the .dynsym section. The dynamic
7736 object will contain position independent code, so all references
7737 from the dynamic object to this symbol will go through the global
7738 offset table. The dynamic linker will use the .dynsym entry to
7739 determine the address it must put in the global offset table, so
7740 both the dynamic object and the regular object will refer to the
7741 same memory location for the variable. */
7743 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
7745 htab->srelbss->size += sizeof (Elf32_External_Rela);
7746 h->needs_copy = 1;
7749 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
7752 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
7753 The number might be exact or a worst-case estimate, depending on how
7754 much information is available to elf_backend_omit_section_dynsym at
7755 the current linking stage. */
7757 static bfd_size_type
7758 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
7760 bfd_size_type count;
7762 count = 0;
7763 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
7765 asection *p;
7766 const struct elf_backend_data *bed;
7768 bed = get_elf_backend_data (output_bfd);
7769 for (p = output_bfd->sections; p ; p = p->next)
7770 if ((p->flags & SEC_EXCLUDE) == 0
7771 && (p->flags & SEC_ALLOC) != 0
7772 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
7773 ++count;
7775 return count;
7778 /* This function is called after all the input files have been read,
7779 and the input sections have been assigned to output sections. We
7780 check for any mips16 stub sections that we can discard. */
7782 bfd_boolean
7783 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
7784 struct bfd_link_info *info)
7786 asection *ri;
7788 bfd *dynobj;
7789 asection *s;
7790 struct mips_got_info *g;
7791 int i;
7792 bfd_size_type loadable_size = 0;
7793 bfd_size_type page_gotno;
7794 bfd_size_type dynsymcount;
7795 bfd *sub;
7796 struct mips_elf_count_tls_arg count_tls_arg;
7797 struct mips_elf_link_hash_table *htab;
7799 htab = mips_elf_hash_table (info);
7801 /* The .reginfo section has a fixed size. */
7802 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
7803 if (ri != NULL)
7804 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
7806 if (! (info->relocatable
7807 || ! mips_elf_hash_table (info)->mips16_stubs_seen))
7808 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
7809 mips_elf_check_mips16_stubs, info);
7811 dynobj = elf_hash_table (info)->dynobj;
7812 if (dynobj == NULL)
7813 /* Relocatable links don't have it. */
7814 return TRUE;
7816 g = mips_elf_got_info (dynobj, &s);
7817 if (s == NULL)
7818 return TRUE;
7820 /* Calculate the total loadable size of the output. That
7821 will give us the maximum number of GOT_PAGE entries
7822 required. */
7823 for (sub = info->input_bfds; sub; sub = sub->link_next)
7825 asection *subsection;
7827 for (subsection = sub->sections;
7828 subsection;
7829 subsection = subsection->next)
7831 if ((subsection->flags & SEC_ALLOC) == 0)
7832 continue;
7833 loadable_size += ((subsection->size + 0xf)
7834 &~ (bfd_size_type) 0xf);
7838 /* There has to be a global GOT entry for every symbol with
7839 a dynamic symbol table index of DT_MIPS_GOTSYM or
7840 higher. Therefore, it make sense to put those symbols
7841 that need GOT entries at the end of the symbol table. We
7842 do that here. */
7843 if (! mips_elf_sort_hash_table (info, 1))
7844 return FALSE;
7846 if (g->global_gotsym != NULL)
7847 i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx;
7848 else
7849 /* If there are no global symbols, or none requiring
7850 relocations, then GLOBAL_GOTSYM will be NULL. */
7851 i = 0;
7853 /* Get a worst-case estimate of the number of dynamic symbols needed.
7854 At this point, dynsymcount does not account for section symbols
7855 and count_section_dynsyms may overestimate the number that will
7856 be needed. */
7857 dynsymcount = (elf_hash_table (info)->dynsymcount
7858 + count_section_dynsyms (output_bfd, info));
7860 /* Determine the size of one stub entry. */
7861 htab->function_stub_size = (dynsymcount > 0x10000
7862 ? MIPS_FUNCTION_STUB_BIG_SIZE
7863 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
7865 /* In the worst case, we'll get one stub per dynamic symbol, plus
7866 one to account for the dummy entry at the end required by IRIX
7867 rld. */
7868 loadable_size += htab->function_stub_size * (i + 1);
7870 if (htab->is_vxworks)
7871 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
7872 relocations against local symbols evaluate to "G", and the EABI does
7873 not include R_MIPS_GOT_PAGE. */
7874 page_gotno = 0;
7875 else
7876 /* Assume there are two loadable segments consisting of contiguous
7877 sections. Is 5 enough? */
7878 page_gotno = (loadable_size >> 16) + 5;
7880 /* Choose the smaller of the two estimates; both are intended to be
7881 conservative. */
7882 if (page_gotno > g->page_gotno)
7883 page_gotno = g->page_gotno;
7885 g->local_gotno += page_gotno;
7886 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
7888 g->global_gotno = i;
7889 s->size += i * MIPS_ELF_GOT_SIZE (output_bfd);
7891 /* We need to calculate tls_gotno for global symbols at this point
7892 instead of building it up earlier, to avoid doublecounting
7893 entries for one global symbol from multiple input files. */
7894 count_tls_arg.info = info;
7895 count_tls_arg.needed = 0;
7896 elf_link_hash_traverse (elf_hash_table (info),
7897 mips_elf_count_global_tls_entries,
7898 &count_tls_arg);
7899 g->tls_gotno += count_tls_arg.needed;
7900 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
7902 mips_elf_resolve_final_got_entries (g);
7904 /* VxWorks does not support multiple GOTs. It initializes $gp to
7905 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
7906 dynamic loader. */
7907 if (!htab->is_vxworks && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
7909 if (! mips_elf_multi_got (output_bfd, info, g, s, page_gotno))
7910 return FALSE;
7912 else
7914 /* Set up TLS entries for the first GOT. */
7915 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
7916 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
7918 htab->computed_got_sizes = TRUE;
7920 return TRUE;
7923 /* Set the sizes of the dynamic sections. */
7925 bfd_boolean
7926 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
7927 struct bfd_link_info *info)
7929 bfd *dynobj;
7930 asection *s, *sreldyn;
7931 bfd_boolean reltext;
7932 struct mips_elf_link_hash_table *htab;
7934 htab = mips_elf_hash_table (info);
7935 dynobj = elf_hash_table (info)->dynobj;
7936 BFD_ASSERT (dynobj != NULL);
7938 if (elf_hash_table (info)->dynamic_sections_created)
7940 /* Set the contents of the .interp section to the interpreter. */
7941 if (info->executable)
7943 s = bfd_get_section_by_name (dynobj, ".interp");
7944 BFD_ASSERT (s != NULL);
7945 s->size
7946 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
7947 s->contents
7948 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
7952 /* IRIX rld assumes that the function stub isn't at the end
7953 of the .text section, so add a dummy entry to the end. */
7954 if (htab->sstubs && htab->sstubs->size > 0)
7955 htab->sstubs->size += htab->function_stub_size;
7957 /* Allocate space for global sym dynamic relocs. */
7958 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
7960 /* The check_relocs and adjust_dynamic_symbol entry points have
7961 determined the sizes of the various dynamic sections. Allocate
7962 memory for them. */
7963 reltext = FALSE;
7964 sreldyn = NULL;
7965 for (s = dynobj->sections; s != NULL; s = s->next)
7967 const char *name;
7969 /* It's OK to base decisions on the section name, because none
7970 of the dynobj section names depend upon the input files. */
7971 name = bfd_get_section_name (dynobj, s);
7973 if ((s->flags & SEC_LINKER_CREATED) == 0)
7974 continue;
7976 if (CONST_STRNEQ (name, ".rel"))
7978 if (s->size != 0)
7980 const char *outname;
7981 asection *target;
7983 /* If this relocation section applies to a read only
7984 section, then we probably need a DT_TEXTREL entry.
7985 If the relocation section is .rel(a).dyn, we always
7986 assert a DT_TEXTREL entry rather than testing whether
7987 there exists a relocation to a read only section or
7988 not. */
7989 outname = bfd_get_section_name (output_bfd,
7990 s->output_section);
7991 target = bfd_get_section_by_name (output_bfd, outname + 4);
7992 if ((target != NULL
7993 && (target->flags & SEC_READONLY) != 0
7994 && (target->flags & SEC_ALLOC) != 0)
7995 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
7996 reltext = TRUE;
7998 /* We use the reloc_count field as a counter if we need
7999 to copy relocs into the output file. */
8000 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
8001 s->reloc_count = 0;
8003 /* If combreloc is enabled, elf_link_sort_relocs() will
8004 sort relocations, but in a different way than we do,
8005 and before we're done creating relocations. Also, it
8006 will move them around between input sections'
8007 relocation's contents, so our sorting would be
8008 broken, so don't let it run. */
8009 info->combreloc = 0;
8012 else if (htab->is_vxworks && strcmp (name, ".got") == 0)
8014 /* Executables do not need a GOT. */
8015 if (info->shared)
8017 /* Allocate relocations for all but the reserved entries. */
8018 struct mips_got_info *g;
8019 unsigned int count;
8021 g = mips_elf_got_info (dynobj, NULL);
8022 count = (g->global_gotno
8023 + g->local_gotno
8024 - MIPS_RESERVED_GOTNO (info));
8025 mips_elf_allocate_dynamic_relocations (dynobj, info, count);
8028 else if (!htab->is_vxworks && CONST_STRNEQ (name, ".got"))
8030 /* _bfd_mips_elf_always_size_sections() has already done
8031 most of the work, but some symbols may have been mapped
8032 to versions that we must now resolve in the got_entries
8033 hash tables. */
8034 struct mips_got_info *gg = mips_elf_got_info (dynobj, NULL);
8035 struct mips_got_info *g = gg;
8036 struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
8037 unsigned int needed_relocs = 0;
8039 if (gg->next)
8041 set_got_offset_arg.value = MIPS_ELF_GOT_SIZE (output_bfd);
8042 set_got_offset_arg.info = info;
8044 /* NOTE 2005-02-03: How can this call, or the next, ever
8045 find any indirect entries to resolve? They were all
8046 resolved in mips_elf_multi_got. */
8047 mips_elf_resolve_final_got_entries (gg);
8048 for (g = gg->next; g && g->next != gg; g = g->next)
8050 unsigned int save_assign;
8052 mips_elf_resolve_final_got_entries (g);
8054 /* Assign offsets to global GOT entries. */
8055 save_assign = g->assigned_gotno;
8056 g->assigned_gotno = g->local_gotno;
8057 set_got_offset_arg.g = g;
8058 set_got_offset_arg.needed_relocs = 0;
8059 htab_traverse (g->got_entries,
8060 mips_elf_set_global_got_offset,
8061 &set_got_offset_arg);
8062 needed_relocs += set_got_offset_arg.needed_relocs;
8063 BFD_ASSERT (g->assigned_gotno - g->local_gotno
8064 <= g->global_gotno);
8066 g->assigned_gotno = save_assign;
8067 if (info->shared)
8069 needed_relocs += g->local_gotno - g->assigned_gotno;
8070 BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
8071 + g->next->global_gotno
8072 + g->next->tls_gotno
8073 + MIPS_RESERVED_GOTNO (info));
8077 else
8079 struct mips_elf_count_tls_arg arg;
8080 arg.info = info;
8081 arg.needed = 0;
8083 htab_traverse (gg->got_entries, mips_elf_count_local_tls_relocs,
8084 &arg);
8085 elf_link_hash_traverse (elf_hash_table (info),
8086 mips_elf_count_global_tls_relocs,
8087 &arg);
8089 needed_relocs += arg.needed;
8092 if (needed_relocs)
8093 mips_elf_allocate_dynamic_relocations (dynobj, info,
8094 needed_relocs);
8096 else if (! info->shared
8097 && ! mips_elf_hash_table (info)->use_rld_obj_head
8098 && CONST_STRNEQ (name, ".rld_map"))
8100 /* We add a room for __rld_map. It will be filled in by the
8101 rtld to contain a pointer to the _r_debug structure. */
8102 s->size += 4;
8104 else if (SGI_COMPAT (output_bfd)
8105 && CONST_STRNEQ (name, ".compact_rel"))
8106 s->size += mips_elf_hash_table (info)->compact_rel_size;
8107 else if (! CONST_STRNEQ (name, ".init")
8108 && s != htab->sgotplt
8109 && s != htab->splt
8110 && s != htab->sstubs)
8112 /* It's not one of our sections, so don't allocate space. */
8113 continue;
8116 if (s->size == 0)
8118 s->flags |= SEC_EXCLUDE;
8119 continue;
8122 if ((s->flags & SEC_HAS_CONTENTS) == 0)
8123 continue;
8125 /* Allocate memory for this section last, since we may increase its
8126 size above. */
8127 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) == 0)
8129 sreldyn = s;
8130 continue;
8133 /* Allocate memory for the section contents. */
8134 s->contents = bfd_zalloc (dynobj, s->size);
8135 if (s->contents == NULL)
8137 bfd_set_error (bfd_error_no_memory);
8138 return FALSE;
8142 /* Allocate memory for the .rel(a).dyn section. */
8143 if (sreldyn != NULL)
8145 sreldyn->contents = bfd_zalloc (dynobj, sreldyn->size);
8146 if (sreldyn->contents == NULL)
8148 bfd_set_error (bfd_error_no_memory);
8149 return FALSE;
8153 if (elf_hash_table (info)->dynamic_sections_created)
8155 /* Add some entries to the .dynamic section. We fill in the
8156 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
8157 must add the entries now so that we get the correct size for
8158 the .dynamic section. */
8160 /* SGI object has the equivalence of DT_DEBUG in the
8161 DT_MIPS_RLD_MAP entry. This must come first because glibc
8162 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and GDB only
8163 looks at the first one it sees. */
8164 if (!info->shared
8165 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
8166 return FALSE;
8168 /* The DT_DEBUG entry may be filled in by the dynamic linker and
8169 used by the debugger. */
8170 if (info->executable
8171 && !SGI_COMPAT (output_bfd)
8172 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
8173 return FALSE;
8175 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
8176 info->flags |= DF_TEXTREL;
8178 if ((info->flags & DF_TEXTREL) != 0)
8180 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
8181 return FALSE;
8183 /* Clear the DF_TEXTREL flag. It will be set again if we
8184 write out an actual text relocation; we may not, because
8185 at this point we do not know whether e.g. any .eh_frame
8186 absolute relocations have been converted to PC-relative. */
8187 info->flags &= ~DF_TEXTREL;
8190 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
8191 return FALSE;
8193 if (htab->is_vxworks)
8195 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
8196 use any of the DT_MIPS_* tags. */
8197 if (mips_elf_rel_dyn_section (info, FALSE))
8199 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
8200 return FALSE;
8202 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
8203 return FALSE;
8205 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
8206 return FALSE;
8208 if (htab->splt->size > 0)
8210 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
8211 return FALSE;
8213 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
8214 return FALSE;
8216 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
8217 return FALSE;
8220 else
8222 if (mips_elf_rel_dyn_section (info, FALSE))
8224 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
8225 return FALSE;
8227 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
8228 return FALSE;
8230 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
8231 return FALSE;
8234 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
8235 return FALSE;
8237 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
8238 return FALSE;
8240 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
8241 return FALSE;
8243 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
8244 return FALSE;
8246 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
8247 return FALSE;
8249 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
8250 return FALSE;
8252 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
8253 return FALSE;
8255 if (IRIX_COMPAT (dynobj) == ict_irix5
8256 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
8257 return FALSE;
8259 if (IRIX_COMPAT (dynobj) == ict_irix6
8260 && (bfd_get_section_by_name
8261 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
8262 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
8263 return FALSE;
8265 if (htab->is_vxworks
8266 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
8267 return FALSE;
8270 return TRUE;
8273 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
8274 Adjust its R_ADDEND field so that it is correct for the output file.
8275 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
8276 and sections respectively; both use symbol indexes. */
8278 static void
8279 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
8280 bfd *input_bfd, Elf_Internal_Sym *local_syms,
8281 asection **local_sections, Elf_Internal_Rela *rel)
8283 unsigned int r_type, r_symndx;
8284 Elf_Internal_Sym *sym;
8285 asection *sec;
8287 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
8289 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8290 if (r_type == R_MIPS16_GPREL
8291 || r_type == R_MIPS_GPREL16
8292 || r_type == R_MIPS_GPREL32
8293 || r_type == R_MIPS_LITERAL)
8295 rel->r_addend += _bfd_get_gp_value (input_bfd);
8296 rel->r_addend -= _bfd_get_gp_value (output_bfd);
8299 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
8300 sym = local_syms + r_symndx;
8302 /* Adjust REL's addend to account for section merging. */
8303 if (!info->relocatable)
8305 sec = local_sections[r_symndx];
8306 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
8309 /* This would normally be done by the rela_normal code in elflink.c. */
8310 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8311 rel->r_addend += local_sections[r_symndx]->output_offset;
8315 /* Relocate a MIPS ELF section. */
8317 bfd_boolean
8318 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
8319 bfd *input_bfd, asection *input_section,
8320 bfd_byte *contents, Elf_Internal_Rela *relocs,
8321 Elf_Internal_Sym *local_syms,
8322 asection **local_sections)
8324 Elf_Internal_Rela *rel;
8325 const Elf_Internal_Rela *relend;
8326 bfd_vma addend = 0;
8327 bfd_boolean use_saved_addend_p = FALSE;
8328 const struct elf_backend_data *bed;
8330 bed = get_elf_backend_data (output_bfd);
8331 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
8332 for (rel = relocs; rel < relend; ++rel)
8334 const char *name;
8335 bfd_vma value = 0;
8336 reloc_howto_type *howto;
8337 bfd_boolean require_jalx;
8338 /* TRUE if the relocation is a RELA relocation, rather than a
8339 REL relocation. */
8340 bfd_boolean rela_relocation_p = TRUE;
8341 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8342 const char *msg;
8343 unsigned long r_symndx;
8344 asection *sec;
8345 Elf_Internal_Shdr *symtab_hdr;
8346 struct elf_link_hash_entry *h;
8348 /* Find the relocation howto for this relocation. */
8349 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type,
8350 NEWABI_P (input_bfd)
8351 && (MIPS_RELOC_RELA_P
8352 (input_bfd, input_section,
8353 rel - relocs)));
8355 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
8356 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8357 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
8359 sec = local_sections[r_symndx];
8360 h = NULL;
8362 else
8364 unsigned long extsymoff;
8366 extsymoff = 0;
8367 if (!elf_bad_symtab (input_bfd))
8368 extsymoff = symtab_hdr->sh_info;
8369 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
8370 while (h->root.type == bfd_link_hash_indirect
8371 || h->root.type == bfd_link_hash_warning)
8372 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8374 sec = NULL;
8375 if (h->root.type == bfd_link_hash_defined
8376 || h->root.type == bfd_link_hash_defweak)
8377 sec = h->root.u.def.section;
8380 if (sec != NULL && elf_discarded_section (sec))
8382 /* For relocs against symbols from removed linkonce sections,
8383 or sections discarded by a linker script, we just want the
8384 section contents zeroed. Avoid any special processing. */
8385 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
8386 rel->r_info = 0;
8387 rel->r_addend = 0;
8388 continue;
8391 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
8393 /* Some 32-bit code uses R_MIPS_64. In particular, people use
8394 64-bit code, but make sure all their addresses are in the
8395 lowermost or uppermost 32-bit section of the 64-bit address
8396 space. Thus, when they use an R_MIPS_64 they mean what is
8397 usually meant by R_MIPS_32, with the exception that the
8398 stored value is sign-extended to 64 bits. */
8399 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
8401 /* On big-endian systems, we need to lie about the position
8402 of the reloc. */
8403 if (bfd_big_endian (input_bfd))
8404 rel->r_offset += 4;
8407 if (!use_saved_addend_p)
8409 /* If these relocations were originally of the REL variety,
8410 we must pull the addend out of the field that will be
8411 relocated. Otherwise, we simply use the contents of the
8412 RELA relocation. */
8413 if (mips_elf_rel_relocation_p (input_bfd, input_section,
8414 relocs, rel))
8416 rela_relocation_p = FALSE;
8417 addend = mips_elf_read_rel_addend (input_bfd, rel,
8418 howto, contents);
8419 if (hi16_reloc_p (r_type)
8420 || (got16_reloc_p (r_type)
8421 && mips_elf_local_relocation_p (input_bfd, rel,
8422 local_sections, FALSE)))
8424 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
8425 contents, &addend))
8427 const char *name;
8429 if (h)
8430 name = h->root.root.string;
8431 else
8432 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
8433 local_syms + r_symndx,
8434 sec);
8435 (*_bfd_error_handler)
8436 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
8437 input_bfd, input_section, name, howto->name,
8438 rel->r_offset);
8441 else
8442 addend <<= howto->rightshift;
8444 else
8445 addend = rel->r_addend;
8446 mips_elf_adjust_addend (output_bfd, info, input_bfd,
8447 local_syms, local_sections, rel);
8450 if (info->relocatable)
8452 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
8453 && bfd_big_endian (input_bfd))
8454 rel->r_offset -= 4;
8456 if (!rela_relocation_p && rel->r_addend)
8458 addend += rel->r_addend;
8459 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
8460 addend = mips_elf_high (addend);
8461 else if (r_type == R_MIPS_HIGHER)
8462 addend = mips_elf_higher (addend);
8463 else if (r_type == R_MIPS_HIGHEST)
8464 addend = mips_elf_highest (addend);
8465 else
8466 addend >>= howto->rightshift;
8468 /* We use the source mask, rather than the destination
8469 mask because the place to which we are writing will be
8470 source of the addend in the final link. */
8471 addend &= howto->src_mask;
8473 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
8474 /* See the comment above about using R_MIPS_64 in the 32-bit
8475 ABI. Here, we need to update the addend. It would be
8476 possible to get away with just using the R_MIPS_32 reloc
8477 but for endianness. */
8479 bfd_vma sign_bits;
8480 bfd_vma low_bits;
8481 bfd_vma high_bits;
8483 if (addend & ((bfd_vma) 1 << 31))
8484 #ifdef BFD64
8485 sign_bits = ((bfd_vma) 1 << 32) - 1;
8486 #else
8487 sign_bits = -1;
8488 #endif
8489 else
8490 sign_bits = 0;
8492 /* If we don't know that we have a 64-bit type,
8493 do two separate stores. */
8494 if (bfd_big_endian (input_bfd))
8496 /* Store the sign-bits (which are most significant)
8497 first. */
8498 low_bits = sign_bits;
8499 high_bits = addend;
8501 else
8503 low_bits = addend;
8504 high_bits = sign_bits;
8506 bfd_put_32 (input_bfd, low_bits,
8507 contents + rel->r_offset);
8508 bfd_put_32 (input_bfd, high_bits,
8509 contents + rel->r_offset + 4);
8510 continue;
8513 if (! mips_elf_perform_relocation (info, howto, rel, addend,
8514 input_bfd, input_section,
8515 contents, FALSE))
8516 return FALSE;
8519 /* Go on to the next relocation. */
8520 continue;
8523 /* In the N32 and 64-bit ABIs there may be multiple consecutive
8524 relocations for the same offset. In that case we are
8525 supposed to treat the output of each relocation as the addend
8526 for the next. */
8527 if (rel + 1 < relend
8528 && rel->r_offset == rel[1].r_offset
8529 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
8530 use_saved_addend_p = TRUE;
8531 else
8532 use_saved_addend_p = FALSE;
8534 /* Figure out what value we are supposed to relocate. */
8535 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
8536 input_section, info, rel,
8537 addend, howto, local_syms,
8538 local_sections, &value,
8539 &name, &require_jalx,
8540 use_saved_addend_p))
8542 case bfd_reloc_continue:
8543 /* There's nothing to do. */
8544 continue;
8546 case bfd_reloc_undefined:
8547 /* mips_elf_calculate_relocation already called the
8548 undefined_symbol callback. There's no real point in
8549 trying to perform the relocation at this point, so we
8550 just skip ahead to the next relocation. */
8551 continue;
8553 case bfd_reloc_notsupported:
8554 msg = _("internal error: unsupported relocation error");
8555 info->callbacks->warning
8556 (info, msg, name, input_bfd, input_section, rel->r_offset);
8557 return FALSE;
8559 case bfd_reloc_overflow:
8560 if (use_saved_addend_p)
8561 /* Ignore overflow until we reach the last relocation for
8562 a given location. */
8564 else
8566 struct mips_elf_link_hash_table *htab;
8568 htab = mips_elf_hash_table (info);
8569 BFD_ASSERT (name != NULL);
8570 if (!htab->small_data_overflow_reported
8571 && (howto->type == R_MIPS_GPREL16
8572 || howto->type == R_MIPS_LITERAL))
8574 const char *msg =
8575 _("small-data section exceeds 64KB;"
8576 " lower small-data size limit (see option -G)");
8578 htab->small_data_overflow_reported = TRUE;
8579 (*info->callbacks->einfo) ("%P: %s\n", msg);
8581 if (! ((*info->callbacks->reloc_overflow)
8582 (info, NULL, name, howto->name, (bfd_vma) 0,
8583 input_bfd, input_section, rel->r_offset)))
8584 return FALSE;
8586 break;
8588 case bfd_reloc_ok:
8589 break;
8591 default:
8592 abort ();
8593 break;
8596 /* If we've got another relocation for the address, keep going
8597 until we reach the last one. */
8598 if (use_saved_addend_p)
8600 addend = value;
8601 continue;
8604 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
8605 /* See the comment above about using R_MIPS_64 in the 32-bit
8606 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
8607 that calculated the right value. Now, however, we
8608 sign-extend the 32-bit result to 64-bits, and store it as a
8609 64-bit value. We are especially generous here in that we
8610 go to extreme lengths to support this usage on systems with
8611 only a 32-bit VMA. */
8613 bfd_vma sign_bits;
8614 bfd_vma low_bits;
8615 bfd_vma high_bits;
8617 if (value & ((bfd_vma) 1 << 31))
8618 #ifdef BFD64
8619 sign_bits = ((bfd_vma) 1 << 32) - 1;
8620 #else
8621 sign_bits = -1;
8622 #endif
8623 else
8624 sign_bits = 0;
8626 /* If we don't know that we have a 64-bit type,
8627 do two separate stores. */
8628 if (bfd_big_endian (input_bfd))
8630 /* Undo what we did above. */
8631 rel->r_offset -= 4;
8632 /* Store the sign-bits (which are most significant)
8633 first. */
8634 low_bits = sign_bits;
8635 high_bits = value;
8637 else
8639 low_bits = value;
8640 high_bits = sign_bits;
8642 bfd_put_32 (input_bfd, low_bits,
8643 contents + rel->r_offset);
8644 bfd_put_32 (input_bfd, high_bits,
8645 contents + rel->r_offset + 4);
8646 continue;
8649 /* Actually perform the relocation. */
8650 if (! mips_elf_perform_relocation (info, howto, rel, value,
8651 input_bfd, input_section,
8652 contents, require_jalx))
8653 return FALSE;
8656 return TRUE;
8659 /* If NAME is one of the special IRIX6 symbols defined by the linker,
8660 adjust it appropriately now. */
8662 static void
8663 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
8664 const char *name, Elf_Internal_Sym *sym)
8666 /* The linker script takes care of providing names and values for
8667 these, but we must place them into the right sections. */
8668 static const char* const text_section_symbols[] = {
8669 "_ftext",
8670 "_etext",
8671 "__dso_displacement",
8672 "__elf_header",
8673 "__program_header_table",
8674 NULL
8677 static const char* const data_section_symbols[] = {
8678 "_fdata",
8679 "_edata",
8680 "_end",
8681 "_fbss",
8682 NULL
8685 const char* const *p;
8686 int i;
8688 for (i = 0; i < 2; ++i)
8689 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
8691 ++p)
8692 if (strcmp (*p, name) == 0)
8694 /* All of these symbols are given type STT_SECTION by the
8695 IRIX6 linker. */
8696 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8697 sym->st_other = STO_PROTECTED;
8699 /* The IRIX linker puts these symbols in special sections. */
8700 if (i == 0)
8701 sym->st_shndx = SHN_MIPS_TEXT;
8702 else
8703 sym->st_shndx = SHN_MIPS_DATA;
8705 break;
8709 /* Finish up dynamic symbol handling. We set the contents of various
8710 dynamic sections here. */
8712 bfd_boolean
8713 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
8714 struct bfd_link_info *info,
8715 struct elf_link_hash_entry *h,
8716 Elf_Internal_Sym *sym)
8718 bfd *dynobj;
8719 asection *sgot;
8720 struct mips_got_info *g, *gg;
8721 const char *name;
8722 int idx;
8723 struct mips_elf_link_hash_table *htab;
8724 struct mips_elf_link_hash_entry *hmips;
8726 htab = mips_elf_hash_table (info);
8727 dynobj = elf_hash_table (info)->dynobj;
8728 hmips = (struct mips_elf_link_hash_entry *) h;
8730 if (h->plt.offset != MINUS_ONE)
8732 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
8734 /* This symbol has a stub. Set it up. */
8736 BFD_ASSERT (h->dynindx != -1);
8738 BFD_ASSERT ((htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
8739 || (h->dynindx <= 0xffff));
8741 /* Values up to 2^31 - 1 are allowed. Larger values would cause
8742 sign extension at runtime in the stub, resulting in a negative
8743 index value. */
8744 if (h->dynindx & ~0x7fffffff)
8745 return FALSE;
8747 /* Fill the stub. */
8748 idx = 0;
8749 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
8750 idx += 4;
8751 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
8752 idx += 4;
8753 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
8755 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
8756 stub + idx);
8757 idx += 4;
8759 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
8760 idx += 4;
8762 /* If a large stub is not required and sign extension is not a
8763 problem, then use legacy code in the stub. */
8764 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
8765 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff), stub + idx);
8766 else if (h->dynindx & ~0x7fff)
8767 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff), stub + idx);
8768 else
8769 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
8770 stub + idx);
8772 BFD_ASSERT (h->plt.offset <= htab->sstubs->size);
8773 memcpy (htab->sstubs->contents + h->plt.offset,
8774 stub, htab->function_stub_size);
8776 /* Mark the symbol as undefined. plt.offset != -1 occurs
8777 only for the referenced symbol. */
8778 sym->st_shndx = SHN_UNDEF;
8780 /* The run-time linker uses the st_value field of the symbol
8781 to reset the global offset table entry for this external
8782 to its stub address when unlinking a shared object. */
8783 sym->st_value = (htab->sstubs->output_section->vma
8784 + htab->sstubs->output_offset
8785 + h->plt.offset);
8788 /* If we have a MIPS16 function with a stub, the dynamic symbol must
8789 refer to the stub, since only the stub uses the standard calling
8790 conventions. */
8791 if (h->dynindx != -1 && hmips->fn_stub != NULL)
8793 BFD_ASSERT (hmips->need_fn_stub);
8794 sym->st_value = (hmips->fn_stub->output_section->vma
8795 + hmips->fn_stub->output_offset);
8796 sym->st_size = hmips->fn_stub->size;
8797 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
8800 BFD_ASSERT (h->dynindx != -1
8801 || h->forced_local);
8803 sgot = mips_elf_got_section (dynobj, FALSE);
8804 BFD_ASSERT (sgot != NULL);
8805 BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
8806 g = mips_elf_section_data (sgot)->u.got_info;
8807 BFD_ASSERT (g != NULL);
8809 /* Run through the global symbol table, creating GOT entries for all
8810 the symbols that need them. */
8811 if (g->global_gotsym != NULL
8812 && h->dynindx >= g->global_gotsym->dynindx)
8814 bfd_vma offset;
8815 bfd_vma value;
8817 value = sym->st_value;
8818 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
8819 R_MIPS_GOT16, info);
8820 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
8823 if (g->next && h->dynindx != -1 && h->type != STT_TLS)
8825 struct mips_got_entry e, *p;
8826 bfd_vma entry;
8827 bfd_vma offset;
8829 gg = g;
8831 e.abfd = output_bfd;
8832 e.symndx = -1;
8833 e.d.h = hmips;
8834 e.tls_type = 0;
8836 for (g = g->next; g->next != gg; g = g->next)
8838 if (g->got_entries
8839 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
8840 &e)))
8842 offset = p->gotidx;
8843 if (info->shared
8844 || (elf_hash_table (info)->dynamic_sections_created
8845 && p->d.h != NULL
8846 && p->d.h->root.def_dynamic
8847 && !p->d.h->root.def_regular))
8849 /* Create an R_MIPS_REL32 relocation for this entry. Due to
8850 the various compatibility problems, it's easier to mock
8851 up an R_MIPS_32 or R_MIPS_64 relocation and leave
8852 mips_elf_create_dynamic_relocation to calculate the
8853 appropriate addend. */
8854 Elf_Internal_Rela rel[3];
8856 memset (rel, 0, sizeof (rel));
8857 if (ABI_64_P (output_bfd))
8858 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
8859 else
8860 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
8861 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
8863 entry = 0;
8864 if (! (mips_elf_create_dynamic_relocation
8865 (output_bfd, info, rel,
8866 e.d.h, NULL, sym->st_value, &entry, sgot)))
8867 return FALSE;
8869 else
8870 entry = sym->st_value;
8871 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
8876 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
8877 name = h->root.root.string;
8878 if (strcmp (name, "_DYNAMIC") == 0
8879 || h == elf_hash_table (info)->hgot)
8880 sym->st_shndx = SHN_ABS;
8881 else if (strcmp (name, "_DYNAMIC_LINK") == 0
8882 || strcmp (name, "_DYNAMIC_LINKING") == 0)
8884 sym->st_shndx = SHN_ABS;
8885 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8886 sym->st_value = 1;
8888 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
8890 sym->st_shndx = SHN_ABS;
8891 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8892 sym->st_value = elf_gp (output_bfd);
8894 else if (SGI_COMPAT (output_bfd))
8896 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
8897 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
8899 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8900 sym->st_other = STO_PROTECTED;
8901 sym->st_value = 0;
8902 sym->st_shndx = SHN_MIPS_DATA;
8904 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
8906 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8907 sym->st_other = STO_PROTECTED;
8908 sym->st_value = mips_elf_hash_table (info)->procedure_count;
8909 sym->st_shndx = SHN_ABS;
8911 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
8913 if (h->type == STT_FUNC)
8914 sym->st_shndx = SHN_MIPS_TEXT;
8915 else if (h->type == STT_OBJECT)
8916 sym->st_shndx = SHN_MIPS_DATA;
8920 /* Handle the IRIX6-specific symbols. */
8921 if (IRIX_COMPAT (output_bfd) == ict_irix6)
8922 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
8924 if (! info->shared)
8926 if (! mips_elf_hash_table (info)->use_rld_obj_head
8927 && (strcmp (name, "__rld_map") == 0
8928 || strcmp (name, "__RLD_MAP") == 0))
8930 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
8931 BFD_ASSERT (s != NULL);
8932 sym->st_value = s->output_section->vma + s->output_offset;
8933 bfd_put_32 (output_bfd, 0, s->contents);
8934 if (mips_elf_hash_table (info)->rld_value == 0)
8935 mips_elf_hash_table (info)->rld_value = sym->st_value;
8937 else if (mips_elf_hash_table (info)->use_rld_obj_head
8938 && strcmp (name, "__rld_obj_head") == 0)
8940 /* IRIX6 does not use a .rld_map section. */
8941 if (IRIX_COMPAT (output_bfd) == ict_irix5
8942 || IRIX_COMPAT (output_bfd) == ict_none)
8943 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
8944 != NULL);
8945 mips_elf_hash_table (info)->rld_value = sym->st_value;
8949 /* Keep dynamic MIPS16 symbols odd. This allows the dynamic linker to
8950 treat MIPS16 symbols like any other. */
8951 if (ELF_ST_IS_MIPS16 (sym->st_other))
8953 BFD_ASSERT (sym->st_value & 1);
8954 sym->st_other -= STO_MIPS16;
8957 return TRUE;
8960 /* Likewise, for VxWorks. */
8962 bfd_boolean
8963 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
8964 struct bfd_link_info *info,
8965 struct elf_link_hash_entry *h,
8966 Elf_Internal_Sym *sym)
8968 bfd *dynobj;
8969 asection *sgot;
8970 struct mips_got_info *g;
8971 struct mips_elf_link_hash_table *htab;
8973 htab = mips_elf_hash_table (info);
8974 dynobj = elf_hash_table (info)->dynobj;
8976 if (h->plt.offset != (bfd_vma) -1)
8978 bfd_byte *loc;
8979 bfd_vma plt_address, plt_index, got_address, got_offset, branch_offset;
8980 Elf_Internal_Rela rel;
8981 static const bfd_vma *plt_entry;
8983 BFD_ASSERT (h->dynindx != -1);
8984 BFD_ASSERT (htab->splt != NULL);
8985 BFD_ASSERT (h->plt.offset <= htab->splt->size);
8987 /* Calculate the address of the .plt entry. */
8988 plt_address = (htab->splt->output_section->vma
8989 + htab->splt->output_offset
8990 + h->plt.offset);
8992 /* Calculate the index of the entry. */
8993 plt_index = ((h->plt.offset - htab->plt_header_size)
8994 / htab->plt_entry_size);
8996 /* Calculate the address of the .got.plt entry. */
8997 got_address = (htab->sgotplt->output_section->vma
8998 + htab->sgotplt->output_offset
8999 + plt_index * 4);
9001 /* Calculate the offset of the .got.plt entry from
9002 _GLOBAL_OFFSET_TABLE_. */
9003 got_offset = mips_elf_gotplt_index (info, h);
9005 /* Calculate the offset for the branch at the start of the PLT
9006 entry. The branch jumps to the beginning of .plt. */
9007 branch_offset = -(h->plt.offset / 4 + 1) & 0xffff;
9009 /* Fill in the initial value of the .got.plt entry. */
9010 bfd_put_32 (output_bfd, plt_address,
9011 htab->sgotplt->contents + plt_index * 4);
9013 /* Find out where the .plt entry should go. */
9014 loc = htab->splt->contents + h->plt.offset;
9016 if (info->shared)
9018 plt_entry = mips_vxworks_shared_plt_entry;
9019 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9020 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9022 else
9024 bfd_vma got_address_high, got_address_low;
9026 plt_entry = mips_vxworks_exec_plt_entry;
9027 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9028 got_address_low = got_address & 0xffff;
9030 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9031 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9032 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
9033 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
9034 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9035 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9036 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9037 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9039 loc = (htab->srelplt2->contents
9040 + (plt_index * 3 + 2) * sizeof (Elf32_External_Rela));
9042 /* Emit a relocation for the .got.plt entry. */
9043 rel.r_offset = got_address;
9044 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9045 rel.r_addend = h->plt.offset;
9046 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9048 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
9049 loc += sizeof (Elf32_External_Rela);
9050 rel.r_offset = plt_address + 8;
9051 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9052 rel.r_addend = got_offset;
9053 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9055 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
9056 loc += sizeof (Elf32_External_Rela);
9057 rel.r_offset += 4;
9058 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9059 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9062 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9063 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
9064 rel.r_offset = got_address;
9065 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
9066 rel.r_addend = 0;
9067 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9069 if (!h->def_regular)
9070 sym->st_shndx = SHN_UNDEF;
9073 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
9075 sgot = mips_elf_got_section (dynobj, FALSE);
9076 BFD_ASSERT (sgot != NULL);
9077 BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
9078 g = mips_elf_section_data (sgot)->u.got_info;
9079 BFD_ASSERT (g != NULL);
9081 /* See if this symbol has an entry in the GOT. */
9082 if (g->global_gotsym != NULL
9083 && h->dynindx >= g->global_gotsym->dynindx)
9085 bfd_vma offset;
9086 Elf_Internal_Rela outrel;
9087 bfd_byte *loc;
9088 asection *s;
9090 /* Install the symbol value in the GOT. */
9091 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9092 R_MIPS_GOT16, info);
9093 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
9095 /* Add a dynamic relocation for it. */
9096 s = mips_elf_rel_dyn_section (info, FALSE);
9097 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
9098 outrel.r_offset = (sgot->output_section->vma
9099 + sgot->output_offset
9100 + offset);
9101 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
9102 outrel.r_addend = 0;
9103 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
9106 /* Emit a copy reloc, if needed. */
9107 if (h->needs_copy)
9109 Elf_Internal_Rela rel;
9111 BFD_ASSERT (h->dynindx != -1);
9113 rel.r_offset = (h->root.u.def.section->output_section->vma
9114 + h->root.u.def.section->output_offset
9115 + h->root.u.def.value);
9116 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
9117 rel.r_addend = 0;
9118 bfd_elf32_swap_reloca_out (output_bfd, &rel,
9119 htab->srelbss->contents
9120 + (htab->srelbss->reloc_count
9121 * sizeof (Elf32_External_Rela)));
9122 ++htab->srelbss->reloc_count;
9125 /* If this is a mips16 symbol, force the value to be even. */
9126 if (ELF_ST_IS_MIPS16 (sym->st_other))
9127 sym->st_value &= ~1;
9129 return TRUE;
9132 /* Install the PLT header for a VxWorks executable and finalize the
9133 contents of .rela.plt.unloaded. */
9135 static void
9136 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9138 Elf_Internal_Rela rela;
9139 bfd_byte *loc;
9140 bfd_vma got_value, got_value_high, got_value_low, plt_address;
9141 static const bfd_vma *plt_entry;
9142 struct mips_elf_link_hash_table *htab;
9144 htab = mips_elf_hash_table (info);
9145 plt_entry = mips_vxworks_exec_plt0_entry;
9147 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
9148 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
9149 + htab->root.hgot->root.u.def.section->output_offset
9150 + htab->root.hgot->root.u.def.value);
9152 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
9153 got_value_low = got_value & 0xffff;
9155 /* Calculate the address of the PLT header. */
9156 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
9158 /* Install the PLT header. */
9159 loc = htab->splt->contents;
9160 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
9161 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
9162 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
9163 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9164 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9165 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9167 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
9168 loc = htab->srelplt2->contents;
9169 rela.r_offset = plt_address;
9170 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9171 rela.r_addend = 0;
9172 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
9173 loc += sizeof (Elf32_External_Rela);
9175 /* Output the relocation for the following addiu of
9176 %lo(_GLOBAL_OFFSET_TABLE_). */
9177 rela.r_offset += 4;
9178 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9179 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
9180 loc += sizeof (Elf32_External_Rela);
9182 /* Fix up the remaining relocations. They may have the wrong
9183 symbol index for _G_O_T_ or _P_L_T_ depending on the order
9184 in which symbols were output. */
9185 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
9187 Elf_Internal_Rela rel;
9189 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9190 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9191 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9192 loc += sizeof (Elf32_External_Rela);
9194 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9195 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9196 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9197 loc += sizeof (Elf32_External_Rela);
9199 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9200 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9201 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9202 loc += sizeof (Elf32_External_Rela);
9206 /* Install the PLT header for a VxWorks shared library. */
9208 static void
9209 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
9211 unsigned int i;
9212 struct mips_elf_link_hash_table *htab;
9214 htab = mips_elf_hash_table (info);
9216 /* We just need to copy the entry byte-by-byte. */
9217 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
9218 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
9219 htab->splt->contents + i * 4);
9222 /* Finish up the dynamic sections. */
9224 bfd_boolean
9225 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
9226 struct bfd_link_info *info)
9228 bfd *dynobj;
9229 asection *sdyn;
9230 asection *sgot;
9231 struct mips_got_info *gg, *g;
9232 struct mips_elf_link_hash_table *htab;
9234 htab = mips_elf_hash_table (info);
9235 dynobj = elf_hash_table (info)->dynobj;
9237 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
9239 sgot = mips_elf_got_section (dynobj, FALSE);
9240 if (sgot == NULL)
9241 gg = g = NULL;
9242 else
9244 BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
9245 gg = mips_elf_section_data (sgot)->u.got_info;
9246 BFD_ASSERT (gg != NULL);
9247 g = mips_elf_got_for_ibfd (gg, output_bfd);
9248 BFD_ASSERT (g != NULL);
9251 if (elf_hash_table (info)->dynamic_sections_created)
9253 bfd_byte *b;
9254 int dyn_to_skip = 0, dyn_skipped = 0;
9256 BFD_ASSERT (sdyn != NULL);
9257 BFD_ASSERT (g != NULL);
9259 for (b = sdyn->contents;
9260 b < sdyn->contents + sdyn->size;
9261 b += MIPS_ELF_DYN_SIZE (dynobj))
9263 Elf_Internal_Dyn dyn;
9264 const char *name;
9265 size_t elemsize;
9266 asection *s;
9267 bfd_boolean swap_out_p;
9269 /* Read in the current dynamic entry. */
9270 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
9272 /* Assume that we're going to modify it and write it out. */
9273 swap_out_p = TRUE;
9275 switch (dyn.d_tag)
9277 case DT_RELENT:
9278 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
9279 break;
9281 case DT_RELAENT:
9282 BFD_ASSERT (htab->is_vxworks);
9283 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
9284 break;
9286 case DT_STRSZ:
9287 /* Rewrite DT_STRSZ. */
9288 dyn.d_un.d_val =
9289 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
9290 break;
9292 case DT_PLTGOT:
9293 name = ".got";
9294 if (htab->is_vxworks)
9296 /* _GLOBAL_OFFSET_TABLE_ is defined to be the beginning
9297 of the ".got" section in DYNOBJ. */
9298 s = bfd_get_section_by_name (dynobj, name);
9299 BFD_ASSERT (s != NULL);
9300 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
9302 else
9304 s = bfd_get_section_by_name (output_bfd, name);
9305 BFD_ASSERT (s != NULL);
9306 dyn.d_un.d_ptr = s->vma;
9308 break;
9310 case DT_MIPS_RLD_VERSION:
9311 dyn.d_un.d_val = 1; /* XXX */
9312 break;
9314 case DT_MIPS_FLAGS:
9315 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
9316 break;
9318 case DT_MIPS_TIME_STAMP:
9320 time_t t;
9321 time (&t);
9322 dyn.d_un.d_val = t;
9324 break;
9326 case DT_MIPS_ICHECKSUM:
9327 /* XXX FIXME: */
9328 swap_out_p = FALSE;
9329 break;
9331 case DT_MIPS_IVERSION:
9332 /* XXX FIXME: */
9333 swap_out_p = FALSE;
9334 break;
9336 case DT_MIPS_BASE_ADDRESS:
9337 s = output_bfd->sections;
9338 BFD_ASSERT (s != NULL);
9339 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
9340 break;
9342 case DT_MIPS_LOCAL_GOTNO:
9343 dyn.d_un.d_val = g->local_gotno;
9344 break;
9346 case DT_MIPS_UNREFEXTNO:
9347 /* The index into the dynamic symbol table which is the
9348 entry of the first external symbol that is not
9349 referenced within the same object. */
9350 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
9351 break;
9353 case DT_MIPS_GOTSYM:
9354 if (gg->global_gotsym)
9356 dyn.d_un.d_val = gg->global_gotsym->dynindx;
9357 break;
9359 /* In case if we don't have global got symbols we default
9360 to setting DT_MIPS_GOTSYM to the same value as
9361 DT_MIPS_SYMTABNO, so we just fall through. */
9363 case DT_MIPS_SYMTABNO:
9364 name = ".dynsym";
9365 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
9366 s = bfd_get_section_by_name (output_bfd, name);
9367 BFD_ASSERT (s != NULL);
9369 dyn.d_un.d_val = s->size / elemsize;
9370 break;
9372 case DT_MIPS_HIPAGENO:
9373 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO (info);
9374 break;
9376 case DT_MIPS_RLD_MAP:
9377 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
9378 break;
9380 case DT_MIPS_OPTIONS:
9381 s = (bfd_get_section_by_name
9382 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
9383 dyn.d_un.d_ptr = s->vma;
9384 break;
9386 case DT_RELASZ:
9387 BFD_ASSERT (htab->is_vxworks);
9388 /* The count does not include the JUMP_SLOT relocations. */
9389 if (htab->srelplt)
9390 dyn.d_un.d_val -= htab->srelplt->size;
9391 break;
9393 case DT_PLTREL:
9394 BFD_ASSERT (htab->is_vxworks);
9395 dyn.d_un.d_val = DT_RELA;
9396 break;
9398 case DT_PLTRELSZ:
9399 BFD_ASSERT (htab->is_vxworks);
9400 dyn.d_un.d_val = htab->srelplt->size;
9401 break;
9403 case DT_JMPREL:
9404 BFD_ASSERT (htab->is_vxworks);
9405 dyn.d_un.d_val = (htab->srelplt->output_section->vma
9406 + htab->srelplt->output_offset);
9407 break;
9409 case DT_TEXTREL:
9410 /* If we didn't need any text relocations after all, delete
9411 the dynamic tag. */
9412 if (!(info->flags & DF_TEXTREL))
9414 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
9415 swap_out_p = FALSE;
9417 break;
9419 case DT_FLAGS:
9420 /* If we didn't need any text relocations after all, clear
9421 DF_TEXTREL from DT_FLAGS. */
9422 if (!(info->flags & DF_TEXTREL))
9423 dyn.d_un.d_val &= ~DF_TEXTREL;
9424 else
9425 swap_out_p = FALSE;
9426 break;
9428 default:
9429 swap_out_p = FALSE;
9430 if (htab->is_vxworks
9431 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
9432 swap_out_p = TRUE;
9433 break;
9436 if (swap_out_p || dyn_skipped)
9437 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
9438 (dynobj, &dyn, b - dyn_skipped);
9440 if (dyn_to_skip)
9442 dyn_skipped += dyn_to_skip;
9443 dyn_to_skip = 0;
9447 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
9448 if (dyn_skipped > 0)
9449 memset (b - dyn_skipped, 0, dyn_skipped);
9452 if (sgot != NULL && sgot->size > 0
9453 && !bfd_is_abs_section (sgot->output_section))
9455 if (htab->is_vxworks)
9457 /* The first entry of the global offset table points to the
9458 ".dynamic" section. The second is initialized by the
9459 loader and contains the shared library identifier.
9460 The third is also initialized by the loader and points
9461 to the lazy resolution stub. */
9462 MIPS_ELF_PUT_WORD (output_bfd,
9463 sdyn->output_offset + sdyn->output_section->vma,
9464 sgot->contents);
9465 MIPS_ELF_PUT_WORD (output_bfd, 0,
9466 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
9467 MIPS_ELF_PUT_WORD (output_bfd, 0,
9468 sgot->contents
9469 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
9471 else
9473 /* The first entry of the global offset table will be filled at
9474 runtime. The second entry will be used by some runtime loaders.
9475 This isn't the case of IRIX rld. */
9476 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
9477 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
9478 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
9481 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
9482 = MIPS_ELF_GOT_SIZE (output_bfd);
9485 /* Generate dynamic relocations for the non-primary gots. */
9486 if (gg != NULL && gg->next)
9488 Elf_Internal_Rela rel[3];
9489 bfd_vma addend = 0;
9491 memset (rel, 0, sizeof (rel));
9492 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
9494 for (g = gg->next; g->next != gg; g = g->next)
9496 bfd_vma index = g->next->local_gotno + g->next->global_gotno
9497 + g->next->tls_gotno;
9499 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
9500 + index++ * MIPS_ELF_GOT_SIZE (output_bfd));
9501 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
9502 sgot->contents
9503 + index++ * MIPS_ELF_GOT_SIZE (output_bfd));
9505 if (! info->shared)
9506 continue;
9508 while (index < g->assigned_gotno)
9510 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
9511 = index++ * MIPS_ELF_GOT_SIZE (output_bfd);
9512 if (!(mips_elf_create_dynamic_relocation
9513 (output_bfd, info, rel, NULL,
9514 bfd_abs_section_ptr,
9515 0, &addend, sgot)))
9516 return FALSE;
9517 BFD_ASSERT (addend == 0);
9522 /* The generation of dynamic relocations for the non-primary gots
9523 adds more dynamic relocations. We cannot count them until
9524 here. */
9526 if (elf_hash_table (info)->dynamic_sections_created)
9528 bfd_byte *b;
9529 bfd_boolean swap_out_p;
9531 BFD_ASSERT (sdyn != NULL);
9533 for (b = sdyn->contents;
9534 b < sdyn->contents + sdyn->size;
9535 b += MIPS_ELF_DYN_SIZE (dynobj))
9537 Elf_Internal_Dyn dyn;
9538 asection *s;
9540 /* Read in the current dynamic entry. */
9541 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
9543 /* Assume that we're going to modify it and write it out. */
9544 swap_out_p = TRUE;
9546 switch (dyn.d_tag)
9548 case DT_RELSZ:
9549 /* Reduce DT_RELSZ to account for any relocations we
9550 decided not to make. This is for the n64 irix rld,
9551 which doesn't seem to apply any relocations if there
9552 are trailing null entries. */
9553 s = mips_elf_rel_dyn_section (info, FALSE);
9554 dyn.d_un.d_val = (s->reloc_count
9555 * (ABI_64_P (output_bfd)
9556 ? sizeof (Elf64_Mips_External_Rel)
9557 : sizeof (Elf32_External_Rel)));
9558 /* Adjust the section size too. Tools like the prelinker
9559 can reasonably expect the values to the same. */
9560 elf_section_data (s->output_section)->this_hdr.sh_size
9561 = dyn.d_un.d_val;
9562 break;
9564 default:
9565 swap_out_p = FALSE;
9566 break;
9569 if (swap_out_p)
9570 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
9571 (dynobj, &dyn, b);
9576 asection *s;
9577 Elf32_compact_rel cpt;
9579 if (SGI_COMPAT (output_bfd))
9581 /* Write .compact_rel section out. */
9582 s = bfd_get_section_by_name (dynobj, ".compact_rel");
9583 if (s != NULL)
9585 cpt.id1 = 1;
9586 cpt.num = s->reloc_count;
9587 cpt.id2 = 2;
9588 cpt.offset = (s->output_section->filepos
9589 + sizeof (Elf32_External_compact_rel));
9590 cpt.reserved0 = 0;
9591 cpt.reserved1 = 0;
9592 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
9593 ((Elf32_External_compact_rel *)
9594 s->contents));
9596 /* Clean up a dummy stub function entry in .text. */
9597 if (htab->sstubs != NULL)
9599 file_ptr dummy_offset;
9601 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
9602 dummy_offset = htab->sstubs->size - htab->function_stub_size;
9603 memset (htab->sstubs->contents + dummy_offset, 0,
9604 htab->function_stub_size);
9609 /* The psABI says that the dynamic relocations must be sorted in
9610 increasing order of r_symndx. The VxWorks EABI doesn't require
9611 this, and because the code below handles REL rather than RELA
9612 relocations, using it for VxWorks would be outright harmful. */
9613 if (!htab->is_vxworks)
9615 s = mips_elf_rel_dyn_section (info, FALSE);
9616 if (s != NULL
9617 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
9619 reldyn_sorting_bfd = output_bfd;
9621 if (ABI_64_P (output_bfd))
9622 qsort ((Elf64_External_Rel *) s->contents + 1,
9623 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
9624 sort_dynamic_relocs_64);
9625 else
9626 qsort ((Elf32_External_Rel *) s->contents + 1,
9627 s->reloc_count - 1, sizeof (Elf32_External_Rel),
9628 sort_dynamic_relocs);
9633 if (htab->is_vxworks && htab->splt->size > 0)
9635 if (info->shared)
9636 mips_vxworks_finish_shared_plt (output_bfd, info);
9637 else
9638 mips_vxworks_finish_exec_plt (output_bfd, info);
9640 return TRUE;
9644 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
9646 static void
9647 mips_set_isa_flags (bfd *abfd)
9649 flagword val;
9651 switch (bfd_get_mach (abfd))
9653 default:
9654 case bfd_mach_mips3000:
9655 val = E_MIPS_ARCH_1;
9656 break;
9658 case bfd_mach_mips3900:
9659 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
9660 break;
9662 case bfd_mach_mips6000:
9663 val = E_MIPS_ARCH_2;
9664 break;
9666 case bfd_mach_mips4000:
9667 case bfd_mach_mips4300:
9668 case bfd_mach_mips4400:
9669 case bfd_mach_mips4600:
9670 val = E_MIPS_ARCH_3;
9671 break;
9673 case bfd_mach_mips4010:
9674 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
9675 break;
9677 case bfd_mach_mips4100:
9678 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
9679 break;
9681 case bfd_mach_mips4111:
9682 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
9683 break;
9685 case bfd_mach_mips4120:
9686 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
9687 break;
9689 case bfd_mach_mips4650:
9690 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
9691 break;
9693 case bfd_mach_mips5400:
9694 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
9695 break;
9697 case bfd_mach_mips5500:
9698 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
9699 break;
9701 case bfd_mach_mips9000:
9702 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
9703 break;
9705 case bfd_mach_mips5000:
9706 case bfd_mach_mips7000:
9707 case bfd_mach_mips8000:
9708 case bfd_mach_mips10000:
9709 case bfd_mach_mips12000:
9710 val = E_MIPS_ARCH_4;
9711 break;
9713 case bfd_mach_mips5:
9714 val = E_MIPS_ARCH_5;
9715 break;
9717 case bfd_mach_mips_loongson_2e:
9718 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
9719 break;
9721 case bfd_mach_mips_loongson_2f:
9722 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
9723 break;
9725 case bfd_mach_mips_sb1:
9726 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
9727 break;
9729 case bfd_mach_mips_octeon:
9730 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
9731 break;
9733 case bfd_mach_mipsisa32:
9734 val = E_MIPS_ARCH_32;
9735 break;
9737 case bfd_mach_mipsisa64:
9738 val = E_MIPS_ARCH_64;
9739 break;
9741 case bfd_mach_mipsisa32r2:
9742 val = E_MIPS_ARCH_32R2;
9743 break;
9745 case bfd_mach_mipsisa64r2:
9746 val = E_MIPS_ARCH_64R2;
9747 break;
9749 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
9750 elf_elfheader (abfd)->e_flags |= val;
9755 /* The final processing done just before writing out a MIPS ELF object
9756 file. This gets the MIPS architecture right based on the machine
9757 number. This is used by both the 32-bit and the 64-bit ABI. */
9759 void
9760 _bfd_mips_elf_final_write_processing (bfd *abfd,
9761 bfd_boolean linker ATTRIBUTE_UNUSED)
9763 unsigned int i;
9764 Elf_Internal_Shdr **hdrpp;
9765 const char *name;
9766 asection *sec;
9768 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
9769 is nonzero. This is for compatibility with old objects, which used
9770 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
9771 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
9772 mips_set_isa_flags (abfd);
9774 /* Set the sh_info field for .gptab sections and other appropriate
9775 info for each special section. */
9776 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
9777 i < elf_numsections (abfd);
9778 i++, hdrpp++)
9780 switch ((*hdrpp)->sh_type)
9782 case SHT_MIPS_MSYM:
9783 case SHT_MIPS_LIBLIST:
9784 sec = bfd_get_section_by_name (abfd, ".dynstr");
9785 if (sec != NULL)
9786 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
9787 break;
9789 case SHT_MIPS_GPTAB:
9790 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
9791 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
9792 BFD_ASSERT (name != NULL
9793 && CONST_STRNEQ (name, ".gptab."));
9794 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
9795 BFD_ASSERT (sec != NULL);
9796 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
9797 break;
9799 case SHT_MIPS_CONTENT:
9800 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
9801 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
9802 BFD_ASSERT (name != NULL
9803 && CONST_STRNEQ (name, ".MIPS.content"));
9804 sec = bfd_get_section_by_name (abfd,
9805 name + sizeof ".MIPS.content" - 1);
9806 BFD_ASSERT (sec != NULL);
9807 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
9808 break;
9810 case SHT_MIPS_SYMBOL_LIB:
9811 sec = bfd_get_section_by_name (abfd, ".dynsym");
9812 if (sec != NULL)
9813 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
9814 sec = bfd_get_section_by_name (abfd, ".liblist");
9815 if (sec != NULL)
9816 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
9817 break;
9819 case SHT_MIPS_EVENTS:
9820 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
9821 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
9822 BFD_ASSERT (name != NULL);
9823 if (CONST_STRNEQ (name, ".MIPS.events"))
9824 sec = bfd_get_section_by_name (abfd,
9825 name + sizeof ".MIPS.events" - 1);
9826 else
9828 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
9829 sec = bfd_get_section_by_name (abfd,
9830 (name
9831 + sizeof ".MIPS.post_rel" - 1));
9833 BFD_ASSERT (sec != NULL);
9834 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
9835 break;
9841 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
9842 segments. */
9845 _bfd_mips_elf_additional_program_headers (bfd *abfd,
9846 struct bfd_link_info *info ATTRIBUTE_UNUSED)
9848 asection *s;
9849 int ret = 0;
9851 /* See if we need a PT_MIPS_REGINFO segment. */
9852 s = bfd_get_section_by_name (abfd, ".reginfo");
9853 if (s && (s->flags & SEC_LOAD))
9854 ++ret;
9856 /* See if we need a PT_MIPS_OPTIONS segment. */
9857 if (IRIX_COMPAT (abfd) == ict_irix6
9858 && bfd_get_section_by_name (abfd,
9859 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
9860 ++ret;
9862 /* See if we need a PT_MIPS_RTPROC segment. */
9863 if (IRIX_COMPAT (abfd) == ict_irix5
9864 && bfd_get_section_by_name (abfd, ".dynamic")
9865 && bfd_get_section_by_name (abfd, ".mdebug"))
9866 ++ret;
9868 /* Allocate a PT_NULL header in dynamic objects. See
9869 _bfd_mips_elf_modify_segment_map for details. */
9870 if (!SGI_COMPAT (abfd)
9871 && bfd_get_section_by_name (abfd, ".dynamic"))
9872 ++ret;
9874 return ret;
9877 /* Modify the segment map for an IRIX5 executable. */
9879 bfd_boolean
9880 _bfd_mips_elf_modify_segment_map (bfd *abfd,
9881 struct bfd_link_info *info)
9883 asection *s;
9884 struct elf_segment_map *m, **pm;
9885 bfd_size_type amt;
9887 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
9888 segment. */
9889 s = bfd_get_section_by_name (abfd, ".reginfo");
9890 if (s != NULL && (s->flags & SEC_LOAD) != 0)
9892 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
9893 if (m->p_type == PT_MIPS_REGINFO)
9894 break;
9895 if (m == NULL)
9897 amt = sizeof *m;
9898 m = bfd_zalloc (abfd, amt);
9899 if (m == NULL)
9900 return FALSE;
9902 m->p_type = PT_MIPS_REGINFO;
9903 m->count = 1;
9904 m->sections[0] = s;
9906 /* We want to put it after the PHDR and INTERP segments. */
9907 pm = &elf_tdata (abfd)->segment_map;
9908 while (*pm != NULL
9909 && ((*pm)->p_type == PT_PHDR
9910 || (*pm)->p_type == PT_INTERP))
9911 pm = &(*pm)->next;
9913 m->next = *pm;
9914 *pm = m;
9918 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
9919 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
9920 PT_MIPS_OPTIONS segment immediately following the program header
9921 table. */
9922 if (NEWABI_P (abfd)
9923 /* On non-IRIX6 new abi, we'll have already created a segment
9924 for this section, so don't create another. I'm not sure this
9925 is not also the case for IRIX 6, but I can't test it right
9926 now. */
9927 && IRIX_COMPAT (abfd) == ict_irix6)
9929 for (s = abfd->sections; s; s = s->next)
9930 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
9931 break;
9933 if (s)
9935 struct elf_segment_map *options_segment;
9937 pm = &elf_tdata (abfd)->segment_map;
9938 while (*pm != NULL
9939 && ((*pm)->p_type == PT_PHDR
9940 || (*pm)->p_type == PT_INTERP))
9941 pm = &(*pm)->next;
9943 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
9945 amt = sizeof (struct elf_segment_map);
9946 options_segment = bfd_zalloc (abfd, amt);
9947 options_segment->next = *pm;
9948 options_segment->p_type = PT_MIPS_OPTIONS;
9949 options_segment->p_flags = PF_R;
9950 options_segment->p_flags_valid = TRUE;
9951 options_segment->count = 1;
9952 options_segment->sections[0] = s;
9953 *pm = options_segment;
9957 else
9959 if (IRIX_COMPAT (abfd) == ict_irix5)
9961 /* If there are .dynamic and .mdebug sections, we make a room
9962 for the RTPROC header. FIXME: Rewrite without section names. */
9963 if (bfd_get_section_by_name (abfd, ".interp") == NULL
9964 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
9965 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
9967 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
9968 if (m->p_type == PT_MIPS_RTPROC)
9969 break;
9970 if (m == NULL)
9972 amt = sizeof *m;
9973 m = bfd_zalloc (abfd, amt);
9974 if (m == NULL)
9975 return FALSE;
9977 m->p_type = PT_MIPS_RTPROC;
9979 s = bfd_get_section_by_name (abfd, ".rtproc");
9980 if (s == NULL)
9982 m->count = 0;
9983 m->p_flags = 0;
9984 m->p_flags_valid = 1;
9986 else
9988 m->count = 1;
9989 m->sections[0] = s;
9992 /* We want to put it after the DYNAMIC segment. */
9993 pm = &elf_tdata (abfd)->segment_map;
9994 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
9995 pm = &(*pm)->next;
9996 if (*pm != NULL)
9997 pm = &(*pm)->next;
9999 m->next = *pm;
10000 *pm = m;
10004 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
10005 .dynstr, .dynsym, and .hash sections, and everything in
10006 between. */
10007 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
10008 pm = &(*pm)->next)
10009 if ((*pm)->p_type == PT_DYNAMIC)
10010 break;
10011 m = *pm;
10012 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
10014 /* For a normal mips executable the permissions for the PT_DYNAMIC
10015 segment are read, write and execute. We do that here since
10016 the code in elf.c sets only the read permission. This matters
10017 sometimes for the dynamic linker. */
10018 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
10020 m->p_flags = PF_R | PF_W | PF_X;
10021 m->p_flags_valid = 1;
10024 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
10025 glibc's dynamic linker has traditionally derived the number of
10026 tags from the p_filesz field, and sometimes allocates stack
10027 arrays of that size. An overly-big PT_DYNAMIC segment can
10028 be actively harmful in such cases. Making PT_DYNAMIC contain
10029 other sections can also make life hard for the prelinker,
10030 which might move one of the other sections to a different
10031 PT_LOAD segment. */
10032 if (SGI_COMPAT (abfd)
10033 && m != NULL
10034 && m->count == 1
10035 && strcmp (m->sections[0]->name, ".dynamic") == 0)
10037 static const char *sec_names[] =
10039 ".dynamic", ".dynstr", ".dynsym", ".hash"
10041 bfd_vma low, high;
10042 unsigned int i, c;
10043 struct elf_segment_map *n;
10045 low = ~(bfd_vma) 0;
10046 high = 0;
10047 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
10049 s = bfd_get_section_by_name (abfd, sec_names[i]);
10050 if (s != NULL && (s->flags & SEC_LOAD) != 0)
10052 bfd_size_type sz;
10054 if (low > s->vma)
10055 low = s->vma;
10056 sz = s->size;
10057 if (high < s->vma + sz)
10058 high = s->vma + sz;
10062 c = 0;
10063 for (s = abfd->sections; s != NULL; s = s->next)
10064 if ((s->flags & SEC_LOAD) != 0
10065 && s->vma >= low
10066 && s->vma + s->size <= high)
10067 ++c;
10069 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
10070 n = bfd_zalloc (abfd, amt);
10071 if (n == NULL)
10072 return FALSE;
10073 *n = *m;
10074 n->count = c;
10076 i = 0;
10077 for (s = abfd->sections; s != NULL; s = s->next)
10079 if ((s->flags & SEC_LOAD) != 0
10080 && s->vma >= low
10081 && s->vma + s->size <= high)
10083 n->sections[i] = s;
10084 ++i;
10088 *pm = n;
10092 /* Allocate a spare program header in dynamic objects so that tools
10093 like the prelinker can add an extra PT_LOAD entry.
10095 If the prelinker needs to make room for a new PT_LOAD entry, its
10096 standard procedure is to move the first (read-only) sections into
10097 the new (writable) segment. However, the MIPS ABI requires
10098 .dynamic to be in a read-only segment, and the section will often
10099 start within sizeof (ElfNN_Phdr) bytes of the last program header.
10101 Although the prelinker could in principle move .dynamic to a
10102 writable segment, it seems better to allocate a spare program
10103 header instead, and avoid the need to move any sections.
10104 There is a long tradition of allocating spare dynamic tags,
10105 so allocating a spare program header seems like a natural
10106 extension.
10108 If INFO is NULL, we may be copying an already prelinked binary
10109 with objcopy or strip, so do not add this header. */
10110 if (info != NULL
10111 && !SGI_COMPAT (abfd)
10112 && bfd_get_section_by_name (abfd, ".dynamic"))
10114 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
10115 if ((*pm)->p_type == PT_NULL)
10116 break;
10117 if (*pm == NULL)
10119 m = bfd_zalloc (abfd, sizeof (*m));
10120 if (m == NULL)
10121 return FALSE;
10123 m->p_type = PT_NULL;
10124 *pm = m;
10128 return TRUE;
10131 /* Return the section that should be marked against GC for a given
10132 relocation. */
10134 asection *
10135 _bfd_mips_elf_gc_mark_hook (asection *sec,
10136 struct bfd_link_info *info,
10137 Elf_Internal_Rela *rel,
10138 struct elf_link_hash_entry *h,
10139 Elf_Internal_Sym *sym)
10141 /* ??? Do mips16 stub sections need to be handled special? */
10143 if (h != NULL)
10144 switch (ELF_R_TYPE (sec->owner, rel->r_info))
10146 case R_MIPS_GNU_VTINHERIT:
10147 case R_MIPS_GNU_VTENTRY:
10148 return NULL;
10151 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
10154 /* Update the got entry reference counts for the section being removed. */
10156 bfd_boolean
10157 _bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
10158 struct bfd_link_info *info ATTRIBUTE_UNUSED,
10159 asection *sec ATTRIBUTE_UNUSED,
10160 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
10162 #if 0
10163 Elf_Internal_Shdr *symtab_hdr;
10164 struct elf_link_hash_entry **sym_hashes;
10165 bfd_signed_vma *local_got_refcounts;
10166 const Elf_Internal_Rela *rel, *relend;
10167 unsigned long r_symndx;
10168 struct elf_link_hash_entry *h;
10170 if (info->relocatable)
10171 return TRUE;
10173 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10174 sym_hashes = elf_sym_hashes (abfd);
10175 local_got_refcounts = elf_local_got_refcounts (abfd);
10177 relend = relocs + sec->reloc_count;
10178 for (rel = relocs; rel < relend; rel++)
10179 switch (ELF_R_TYPE (abfd, rel->r_info))
10181 case R_MIPS16_GOT16:
10182 case R_MIPS16_CALL16:
10183 case R_MIPS_GOT16:
10184 case R_MIPS_CALL16:
10185 case R_MIPS_CALL_HI16:
10186 case R_MIPS_CALL_LO16:
10187 case R_MIPS_GOT_HI16:
10188 case R_MIPS_GOT_LO16:
10189 case R_MIPS_GOT_DISP:
10190 case R_MIPS_GOT_PAGE:
10191 case R_MIPS_GOT_OFST:
10192 /* ??? It would seem that the existing MIPS code does no sort
10193 of reference counting or whatnot on its GOT and PLT entries,
10194 so it is not possible to garbage collect them at this time. */
10195 break;
10197 default:
10198 break;
10200 #endif
10202 return TRUE;
10205 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
10206 hiding the old indirect symbol. Process additional relocation
10207 information. Also called for weakdefs, in which case we just let
10208 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
10210 void
10211 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
10212 struct elf_link_hash_entry *dir,
10213 struct elf_link_hash_entry *ind)
10215 struct mips_elf_link_hash_entry *dirmips, *indmips;
10217 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
10219 if (ind->root.type != bfd_link_hash_indirect)
10220 return;
10222 dirmips = (struct mips_elf_link_hash_entry *) dir;
10223 indmips = (struct mips_elf_link_hash_entry *) ind;
10224 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
10225 if (indmips->readonly_reloc)
10226 dirmips->readonly_reloc = TRUE;
10227 if (indmips->no_fn_stub)
10228 dirmips->no_fn_stub = TRUE;
10230 if (dirmips->tls_type == 0)
10231 dirmips->tls_type = indmips->tls_type;
10234 void
10235 _bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
10236 struct elf_link_hash_entry *entry,
10237 bfd_boolean force_local)
10239 bfd *dynobj;
10240 asection *got;
10241 struct mips_got_info *g;
10242 struct mips_elf_link_hash_entry *h;
10243 struct mips_elf_link_hash_table *htab;
10245 h = (struct mips_elf_link_hash_entry *) entry;
10246 if (h->forced_local)
10247 return;
10248 h->forced_local = force_local;
10250 dynobj = elf_hash_table (info)->dynobj;
10251 htab = mips_elf_hash_table (info);
10252 if (dynobj != NULL && force_local && h->root.type != STT_TLS
10253 && (got = mips_elf_got_section (dynobj, TRUE)) != NULL
10254 && (g = mips_elf_section_data (got)->u.got_info) != NULL)
10256 if (g->next)
10258 struct mips_got_entry e;
10259 struct mips_got_info *gg = g;
10261 /* Since we're turning what used to be a global symbol into a
10262 local one, bump up the number of local entries of each GOT
10263 that had an entry for it. This will automatically decrease
10264 the number of global entries, since global_gotno is actually
10265 the upper limit of global entries. */
10266 e.abfd = dynobj;
10267 e.symndx = -1;
10268 e.d.h = h;
10269 e.tls_type = 0;
10271 for (g = g->next; g != gg; g = g->next)
10272 if (htab_find (g->got_entries, &e))
10274 BFD_ASSERT (g->global_gotno > 0);
10275 g->local_gotno++;
10276 g->global_gotno--;
10279 /* If this was a global symbol forced into the primary GOT, we
10280 no longer need an entry for it. We can't release the entry
10281 at this point, but we must at least stop counting it as one
10282 of the symbols that required a forced got entry. */
10283 if (h->root.got.offset == 2)
10285 BFD_ASSERT (gg->assigned_gotno > 0);
10286 gg->assigned_gotno--;
10289 else if (h->root.got.offset == 1)
10291 /* check_relocs didn't know that this symbol would be
10292 forced-local, so add an extra local got entry. */
10293 g->local_gotno++;
10294 if (htab->computed_got_sizes)
10296 /* We'll have treated this symbol as global rather
10297 than local. */
10298 BFD_ASSERT (g->global_gotno > 0);
10299 g->global_gotno--;
10302 else if (htab->is_vxworks && h->root.needs_plt)
10304 /* check_relocs didn't know that this symbol would be
10305 forced-local, so add an extra local got entry. */
10306 g->local_gotno++;
10307 if (htab->computed_got_sizes)
10308 /* The symbol is only used in call relocations, so we'll
10309 have assumed it only needs a .got.plt entry. Increase
10310 the size of .got accordingly. */
10311 got->size += MIPS_ELF_GOT_SIZE (dynobj);
10315 _bfd_elf_link_hash_hide_symbol (info, &h->root, force_local);
10318 #define PDR_SIZE 32
10320 bfd_boolean
10321 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
10322 struct bfd_link_info *info)
10324 asection *o;
10325 bfd_boolean ret = FALSE;
10326 unsigned char *tdata;
10327 size_t i, skip;
10329 o = bfd_get_section_by_name (abfd, ".pdr");
10330 if (! o)
10331 return FALSE;
10332 if (o->size == 0)
10333 return FALSE;
10334 if (o->size % PDR_SIZE != 0)
10335 return FALSE;
10336 if (o->output_section != NULL
10337 && bfd_is_abs_section (o->output_section))
10338 return FALSE;
10340 tdata = bfd_zmalloc (o->size / PDR_SIZE);
10341 if (! tdata)
10342 return FALSE;
10344 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
10345 info->keep_memory);
10346 if (!cookie->rels)
10348 free (tdata);
10349 return FALSE;
10352 cookie->rel = cookie->rels;
10353 cookie->relend = cookie->rels + o->reloc_count;
10355 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
10357 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
10359 tdata[i] = 1;
10360 skip ++;
10364 if (skip != 0)
10366 mips_elf_section_data (o)->u.tdata = tdata;
10367 o->size -= skip * PDR_SIZE;
10368 ret = TRUE;
10370 else
10371 free (tdata);
10373 if (! info->keep_memory)
10374 free (cookie->rels);
10376 return ret;
10379 bfd_boolean
10380 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
10382 if (strcmp (sec->name, ".pdr") == 0)
10383 return TRUE;
10384 return FALSE;
10387 bfd_boolean
10388 _bfd_mips_elf_write_section (bfd *output_bfd,
10389 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
10390 asection *sec, bfd_byte *contents)
10392 bfd_byte *to, *from, *end;
10393 int i;
10395 if (strcmp (sec->name, ".pdr") != 0)
10396 return FALSE;
10398 if (mips_elf_section_data (sec)->u.tdata == NULL)
10399 return FALSE;
10401 to = contents;
10402 end = contents + sec->size;
10403 for (from = contents, i = 0;
10404 from < end;
10405 from += PDR_SIZE, i++)
10407 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
10408 continue;
10409 if (to != from)
10410 memcpy (to, from, PDR_SIZE);
10411 to += PDR_SIZE;
10413 bfd_set_section_contents (output_bfd, sec->output_section, contents,
10414 sec->output_offset, sec->size);
10415 return TRUE;
10418 /* MIPS ELF uses a special find_nearest_line routine in order the
10419 handle the ECOFF debugging information. */
10421 struct mips_elf_find_line
10423 struct ecoff_debug_info d;
10424 struct ecoff_find_line i;
10427 bfd_boolean
10428 _bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
10429 asymbol **symbols, bfd_vma offset,
10430 const char **filename_ptr,
10431 const char **functionname_ptr,
10432 unsigned int *line_ptr)
10434 asection *msec;
10436 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
10437 filename_ptr, functionname_ptr,
10438 line_ptr))
10439 return TRUE;
10441 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
10442 filename_ptr, functionname_ptr,
10443 line_ptr, ABI_64_P (abfd) ? 8 : 0,
10444 &elf_tdata (abfd)->dwarf2_find_line_info))
10445 return TRUE;
10447 msec = bfd_get_section_by_name (abfd, ".mdebug");
10448 if (msec != NULL)
10450 flagword origflags;
10451 struct mips_elf_find_line *fi;
10452 const struct ecoff_debug_swap * const swap =
10453 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
10455 /* If we are called during a link, mips_elf_final_link may have
10456 cleared the SEC_HAS_CONTENTS field. We force it back on here
10457 if appropriate (which it normally will be). */
10458 origflags = msec->flags;
10459 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
10460 msec->flags |= SEC_HAS_CONTENTS;
10462 fi = elf_tdata (abfd)->find_line_info;
10463 if (fi == NULL)
10465 bfd_size_type external_fdr_size;
10466 char *fraw_src;
10467 char *fraw_end;
10468 struct fdr *fdr_ptr;
10469 bfd_size_type amt = sizeof (struct mips_elf_find_line);
10471 fi = bfd_zalloc (abfd, amt);
10472 if (fi == NULL)
10474 msec->flags = origflags;
10475 return FALSE;
10478 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
10480 msec->flags = origflags;
10481 return FALSE;
10484 /* Swap in the FDR information. */
10485 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
10486 fi->d.fdr = bfd_alloc (abfd, amt);
10487 if (fi->d.fdr == NULL)
10489 msec->flags = origflags;
10490 return FALSE;
10492 external_fdr_size = swap->external_fdr_size;
10493 fdr_ptr = fi->d.fdr;
10494 fraw_src = (char *) fi->d.external_fdr;
10495 fraw_end = (fraw_src
10496 + fi->d.symbolic_header.ifdMax * external_fdr_size);
10497 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
10498 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
10500 elf_tdata (abfd)->find_line_info = fi;
10502 /* Note that we don't bother to ever free this information.
10503 find_nearest_line is either called all the time, as in
10504 objdump -l, so the information should be saved, or it is
10505 rarely called, as in ld error messages, so the memory
10506 wasted is unimportant. Still, it would probably be a
10507 good idea for free_cached_info to throw it away. */
10510 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
10511 &fi->i, filename_ptr, functionname_ptr,
10512 line_ptr))
10514 msec->flags = origflags;
10515 return TRUE;
10518 msec->flags = origflags;
10521 /* Fall back on the generic ELF find_nearest_line routine. */
10523 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
10524 filename_ptr, functionname_ptr,
10525 line_ptr);
10528 bfd_boolean
10529 _bfd_mips_elf_find_inliner_info (bfd *abfd,
10530 const char **filename_ptr,
10531 const char **functionname_ptr,
10532 unsigned int *line_ptr)
10534 bfd_boolean found;
10535 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
10536 functionname_ptr, line_ptr,
10537 & elf_tdata (abfd)->dwarf2_find_line_info);
10538 return found;
10542 /* When are writing out the .options or .MIPS.options section,
10543 remember the bytes we are writing out, so that we can install the
10544 GP value in the section_processing routine. */
10546 bfd_boolean
10547 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
10548 const void *location,
10549 file_ptr offset, bfd_size_type count)
10551 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
10553 bfd_byte *c;
10555 if (elf_section_data (section) == NULL)
10557 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
10558 section->used_by_bfd = bfd_zalloc (abfd, amt);
10559 if (elf_section_data (section) == NULL)
10560 return FALSE;
10562 c = mips_elf_section_data (section)->u.tdata;
10563 if (c == NULL)
10565 c = bfd_zalloc (abfd, section->size);
10566 if (c == NULL)
10567 return FALSE;
10568 mips_elf_section_data (section)->u.tdata = c;
10571 memcpy (c + offset, location, count);
10574 return _bfd_elf_set_section_contents (abfd, section, location, offset,
10575 count);
10578 /* This is almost identical to bfd_generic_get_... except that some
10579 MIPS relocations need to be handled specially. Sigh. */
10581 bfd_byte *
10582 _bfd_elf_mips_get_relocated_section_contents
10583 (bfd *abfd,
10584 struct bfd_link_info *link_info,
10585 struct bfd_link_order *link_order,
10586 bfd_byte *data,
10587 bfd_boolean relocatable,
10588 asymbol **symbols)
10590 /* Get enough memory to hold the stuff */
10591 bfd *input_bfd = link_order->u.indirect.section->owner;
10592 asection *input_section = link_order->u.indirect.section;
10593 bfd_size_type sz;
10595 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
10596 arelent **reloc_vector = NULL;
10597 long reloc_count;
10599 if (reloc_size < 0)
10600 goto error_return;
10602 reloc_vector = bfd_malloc (reloc_size);
10603 if (reloc_vector == NULL && reloc_size != 0)
10604 goto error_return;
10606 /* read in the section */
10607 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
10608 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
10609 goto error_return;
10611 reloc_count = bfd_canonicalize_reloc (input_bfd,
10612 input_section,
10613 reloc_vector,
10614 symbols);
10615 if (reloc_count < 0)
10616 goto error_return;
10618 if (reloc_count > 0)
10620 arelent **parent;
10621 /* for mips */
10622 int gp_found;
10623 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
10626 struct bfd_hash_entry *h;
10627 struct bfd_link_hash_entry *lh;
10628 /* Skip all this stuff if we aren't mixing formats. */
10629 if (abfd && input_bfd
10630 && abfd->xvec == input_bfd->xvec)
10631 lh = 0;
10632 else
10634 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
10635 lh = (struct bfd_link_hash_entry *) h;
10637 lookup:
10638 if (lh)
10640 switch (lh->type)
10642 case bfd_link_hash_undefined:
10643 case bfd_link_hash_undefweak:
10644 case bfd_link_hash_common:
10645 gp_found = 0;
10646 break;
10647 case bfd_link_hash_defined:
10648 case bfd_link_hash_defweak:
10649 gp_found = 1;
10650 gp = lh->u.def.value;
10651 break;
10652 case bfd_link_hash_indirect:
10653 case bfd_link_hash_warning:
10654 lh = lh->u.i.link;
10655 /* @@FIXME ignoring warning for now */
10656 goto lookup;
10657 case bfd_link_hash_new:
10658 default:
10659 abort ();
10662 else
10663 gp_found = 0;
10665 /* end mips */
10666 for (parent = reloc_vector; *parent != NULL; parent++)
10668 char *error_message = NULL;
10669 bfd_reloc_status_type r;
10671 /* Specific to MIPS: Deal with relocation types that require
10672 knowing the gp of the output bfd. */
10673 asymbol *sym = *(*parent)->sym_ptr_ptr;
10675 /* If we've managed to find the gp and have a special
10676 function for the relocation then go ahead, else default
10677 to the generic handling. */
10678 if (gp_found
10679 && (*parent)->howto->special_function
10680 == _bfd_mips_elf32_gprel16_reloc)
10681 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
10682 input_section, relocatable,
10683 data, gp);
10684 else
10685 r = bfd_perform_relocation (input_bfd, *parent, data,
10686 input_section,
10687 relocatable ? abfd : NULL,
10688 &error_message);
10690 if (relocatable)
10692 asection *os = input_section->output_section;
10694 /* A partial link, so keep the relocs */
10695 os->orelocation[os->reloc_count] = *parent;
10696 os->reloc_count++;
10699 if (r != bfd_reloc_ok)
10701 switch (r)
10703 case bfd_reloc_undefined:
10704 if (!((*link_info->callbacks->undefined_symbol)
10705 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
10706 input_bfd, input_section, (*parent)->address, TRUE)))
10707 goto error_return;
10708 break;
10709 case bfd_reloc_dangerous:
10710 BFD_ASSERT (error_message != NULL);
10711 if (!((*link_info->callbacks->reloc_dangerous)
10712 (link_info, error_message, input_bfd, input_section,
10713 (*parent)->address)))
10714 goto error_return;
10715 break;
10716 case bfd_reloc_overflow:
10717 if (!((*link_info->callbacks->reloc_overflow)
10718 (link_info, NULL,
10719 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
10720 (*parent)->howto->name, (*parent)->addend,
10721 input_bfd, input_section, (*parent)->address)))
10722 goto error_return;
10723 break;
10724 case bfd_reloc_outofrange:
10725 default:
10726 abort ();
10727 break;
10733 if (reloc_vector != NULL)
10734 free (reloc_vector);
10735 return data;
10737 error_return:
10738 if (reloc_vector != NULL)
10739 free (reloc_vector);
10740 return NULL;
10743 /* Create a MIPS ELF linker hash table. */
10745 struct bfd_link_hash_table *
10746 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
10748 struct mips_elf_link_hash_table *ret;
10749 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
10751 ret = bfd_malloc (amt);
10752 if (ret == NULL)
10753 return NULL;
10755 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
10756 mips_elf_link_hash_newfunc,
10757 sizeof (struct mips_elf_link_hash_entry)))
10759 free (ret);
10760 return NULL;
10763 #if 0
10764 /* We no longer use this. */
10765 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
10766 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
10767 #endif
10768 ret->procedure_count = 0;
10769 ret->compact_rel_size = 0;
10770 ret->use_rld_obj_head = FALSE;
10771 ret->rld_value = 0;
10772 ret->mips16_stubs_seen = FALSE;
10773 ret->computed_got_sizes = FALSE;
10774 ret->is_vxworks = FALSE;
10775 ret->small_data_overflow_reported = FALSE;
10776 ret->srelbss = NULL;
10777 ret->sdynbss = NULL;
10778 ret->srelplt = NULL;
10779 ret->srelplt2 = NULL;
10780 ret->sgotplt = NULL;
10781 ret->splt = NULL;
10782 ret->sstubs = NULL;
10783 ret->plt_header_size = 0;
10784 ret->plt_entry_size = 0;
10785 ret->function_stub_size = 0;
10787 return &ret->root.root;
10790 /* Likewise, but indicate that the target is VxWorks. */
10792 struct bfd_link_hash_table *
10793 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
10795 struct bfd_link_hash_table *ret;
10797 ret = _bfd_mips_elf_link_hash_table_create (abfd);
10798 if (ret)
10800 struct mips_elf_link_hash_table *htab;
10802 htab = (struct mips_elf_link_hash_table *) ret;
10803 htab->is_vxworks = 1;
10805 return ret;
10808 /* We need to use a special link routine to handle the .reginfo and
10809 the .mdebug sections. We need to merge all instances of these
10810 sections together, not write them all out sequentially. */
10812 bfd_boolean
10813 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10815 asection *o;
10816 struct bfd_link_order *p;
10817 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
10818 asection *rtproc_sec;
10819 Elf32_RegInfo reginfo;
10820 struct ecoff_debug_info debug;
10821 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10822 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
10823 HDRR *symhdr = &debug.symbolic_header;
10824 void *mdebug_handle = NULL;
10825 asection *s;
10826 EXTR esym;
10827 unsigned int i;
10828 bfd_size_type amt;
10829 struct mips_elf_link_hash_table *htab;
10831 static const char * const secname[] =
10833 ".text", ".init", ".fini", ".data",
10834 ".rodata", ".sdata", ".sbss", ".bss"
10836 static const int sc[] =
10838 scText, scInit, scFini, scData,
10839 scRData, scSData, scSBss, scBss
10842 /* We'd carefully arranged the dynamic symbol indices, and then the
10843 generic size_dynamic_sections renumbered them out from under us.
10844 Rather than trying somehow to prevent the renumbering, just do
10845 the sort again. */
10846 htab = mips_elf_hash_table (info);
10847 if (elf_hash_table (info)->dynamic_sections_created)
10849 bfd *dynobj;
10850 asection *got;
10851 struct mips_got_info *g;
10852 bfd_size_type dynsecsymcount;
10854 /* When we resort, we must tell mips_elf_sort_hash_table what
10855 the lowest index it may use is. That's the number of section
10856 symbols we're going to add. The generic ELF linker only
10857 adds these symbols when building a shared object. Note that
10858 we count the sections after (possibly) removing the .options
10859 section above. */
10861 dynsecsymcount = count_section_dynsyms (abfd, info);
10862 if (! mips_elf_sort_hash_table (info, dynsecsymcount + 1))
10863 return FALSE;
10865 /* Make sure we didn't grow the global .got region. */
10866 dynobj = elf_hash_table (info)->dynobj;
10867 got = mips_elf_got_section (dynobj, FALSE);
10868 g = mips_elf_section_data (got)->u.got_info;
10870 if (g->global_gotsym != NULL)
10871 BFD_ASSERT ((elf_hash_table (info)->dynsymcount
10872 - g->global_gotsym->dynindx)
10873 <= g->global_gotno);
10876 /* Get a value for the GP register. */
10877 if (elf_gp (abfd) == 0)
10879 struct bfd_link_hash_entry *h;
10881 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
10882 if (h != NULL && h->type == bfd_link_hash_defined)
10883 elf_gp (abfd) = (h->u.def.value
10884 + h->u.def.section->output_section->vma
10885 + h->u.def.section->output_offset);
10886 else if (htab->is_vxworks
10887 && (h = bfd_link_hash_lookup (info->hash,
10888 "_GLOBAL_OFFSET_TABLE_",
10889 FALSE, FALSE, TRUE))
10890 && h->type == bfd_link_hash_defined)
10891 elf_gp (abfd) = (h->u.def.section->output_section->vma
10892 + h->u.def.section->output_offset
10893 + h->u.def.value);
10894 else if (info->relocatable)
10896 bfd_vma lo = MINUS_ONE;
10898 /* Find the GP-relative section with the lowest offset. */
10899 for (o = abfd->sections; o != NULL; o = o->next)
10900 if (o->vma < lo
10901 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
10902 lo = o->vma;
10904 /* And calculate GP relative to that. */
10905 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
10907 else
10909 /* If the relocate_section function needs to do a reloc
10910 involving the GP value, it should make a reloc_dangerous
10911 callback to warn that GP is not defined. */
10915 /* Go through the sections and collect the .reginfo and .mdebug
10916 information. */
10917 reginfo_sec = NULL;
10918 mdebug_sec = NULL;
10919 gptab_data_sec = NULL;
10920 gptab_bss_sec = NULL;
10921 for (o = abfd->sections; o != NULL; o = o->next)
10923 if (strcmp (o->name, ".reginfo") == 0)
10925 memset (&reginfo, 0, sizeof reginfo);
10927 /* We have found the .reginfo section in the output file.
10928 Look through all the link_orders comprising it and merge
10929 the information together. */
10930 for (p = o->map_head.link_order; p != NULL; p = p->next)
10932 asection *input_section;
10933 bfd *input_bfd;
10934 Elf32_External_RegInfo ext;
10935 Elf32_RegInfo sub;
10937 if (p->type != bfd_indirect_link_order)
10939 if (p->type == bfd_data_link_order)
10940 continue;
10941 abort ();
10944 input_section = p->u.indirect.section;
10945 input_bfd = input_section->owner;
10947 if (! bfd_get_section_contents (input_bfd, input_section,
10948 &ext, 0, sizeof ext))
10949 return FALSE;
10951 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
10953 reginfo.ri_gprmask |= sub.ri_gprmask;
10954 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
10955 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
10956 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
10957 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
10959 /* ri_gp_value is set by the function
10960 mips_elf32_section_processing when the section is
10961 finally written out. */
10963 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10964 elf_link_input_bfd ignores this section. */
10965 input_section->flags &= ~SEC_HAS_CONTENTS;
10968 /* Size has been set in _bfd_mips_elf_always_size_sections. */
10969 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
10971 /* Skip this section later on (I don't think this currently
10972 matters, but someday it might). */
10973 o->map_head.link_order = NULL;
10975 reginfo_sec = o;
10978 if (strcmp (o->name, ".mdebug") == 0)
10980 struct extsym_info einfo;
10981 bfd_vma last;
10983 /* We have found the .mdebug section in the output file.
10984 Look through all the link_orders comprising it and merge
10985 the information together. */
10986 symhdr->magic = swap->sym_magic;
10987 /* FIXME: What should the version stamp be? */
10988 symhdr->vstamp = 0;
10989 symhdr->ilineMax = 0;
10990 symhdr->cbLine = 0;
10991 symhdr->idnMax = 0;
10992 symhdr->ipdMax = 0;
10993 symhdr->isymMax = 0;
10994 symhdr->ioptMax = 0;
10995 symhdr->iauxMax = 0;
10996 symhdr->issMax = 0;
10997 symhdr->issExtMax = 0;
10998 symhdr->ifdMax = 0;
10999 symhdr->crfd = 0;
11000 symhdr->iextMax = 0;
11002 /* We accumulate the debugging information itself in the
11003 debug_info structure. */
11004 debug.line = NULL;
11005 debug.external_dnr = NULL;
11006 debug.external_pdr = NULL;
11007 debug.external_sym = NULL;
11008 debug.external_opt = NULL;
11009 debug.external_aux = NULL;
11010 debug.ss = NULL;
11011 debug.ssext = debug.ssext_end = NULL;
11012 debug.external_fdr = NULL;
11013 debug.external_rfd = NULL;
11014 debug.external_ext = debug.external_ext_end = NULL;
11016 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
11017 if (mdebug_handle == NULL)
11018 return FALSE;
11020 esym.jmptbl = 0;
11021 esym.cobol_main = 0;
11022 esym.weakext = 0;
11023 esym.reserved = 0;
11024 esym.ifd = ifdNil;
11025 esym.asym.iss = issNil;
11026 esym.asym.st = stLocal;
11027 esym.asym.reserved = 0;
11028 esym.asym.index = indexNil;
11029 last = 0;
11030 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
11032 esym.asym.sc = sc[i];
11033 s = bfd_get_section_by_name (abfd, secname[i]);
11034 if (s != NULL)
11036 esym.asym.value = s->vma;
11037 last = s->vma + s->size;
11039 else
11040 esym.asym.value = last;
11041 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
11042 secname[i], &esym))
11043 return FALSE;
11046 for (p = o->map_head.link_order; p != NULL; p = p->next)
11048 asection *input_section;
11049 bfd *input_bfd;
11050 const struct ecoff_debug_swap *input_swap;
11051 struct ecoff_debug_info input_debug;
11052 char *eraw_src;
11053 char *eraw_end;
11055 if (p->type != bfd_indirect_link_order)
11057 if (p->type == bfd_data_link_order)
11058 continue;
11059 abort ();
11062 input_section = p->u.indirect.section;
11063 input_bfd = input_section->owner;
11065 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
11066 || (get_elf_backend_data (input_bfd)
11067 ->elf_backend_ecoff_debug_swap) == NULL)
11069 /* I don't know what a non MIPS ELF bfd would be
11070 doing with a .mdebug section, but I don't really
11071 want to deal with it. */
11072 continue;
11075 input_swap = (get_elf_backend_data (input_bfd)
11076 ->elf_backend_ecoff_debug_swap);
11078 BFD_ASSERT (p->size == input_section->size);
11080 /* The ECOFF linking code expects that we have already
11081 read in the debugging information and set up an
11082 ecoff_debug_info structure, so we do that now. */
11083 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
11084 &input_debug))
11085 return FALSE;
11087 if (! (bfd_ecoff_debug_accumulate
11088 (mdebug_handle, abfd, &debug, swap, input_bfd,
11089 &input_debug, input_swap, info)))
11090 return FALSE;
11092 /* Loop through the external symbols. For each one with
11093 interesting information, try to find the symbol in
11094 the linker global hash table and save the information
11095 for the output external symbols. */
11096 eraw_src = input_debug.external_ext;
11097 eraw_end = (eraw_src
11098 + (input_debug.symbolic_header.iextMax
11099 * input_swap->external_ext_size));
11100 for (;
11101 eraw_src < eraw_end;
11102 eraw_src += input_swap->external_ext_size)
11104 EXTR ext;
11105 const char *name;
11106 struct mips_elf_link_hash_entry *h;
11108 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
11109 if (ext.asym.sc == scNil
11110 || ext.asym.sc == scUndefined
11111 || ext.asym.sc == scSUndefined)
11112 continue;
11114 name = input_debug.ssext + ext.asym.iss;
11115 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
11116 name, FALSE, FALSE, TRUE);
11117 if (h == NULL || h->esym.ifd != -2)
11118 continue;
11120 if (ext.ifd != -1)
11122 BFD_ASSERT (ext.ifd
11123 < input_debug.symbolic_header.ifdMax);
11124 ext.ifd = input_debug.ifdmap[ext.ifd];
11127 h->esym = ext;
11130 /* Free up the information we just read. */
11131 free (input_debug.line);
11132 free (input_debug.external_dnr);
11133 free (input_debug.external_pdr);
11134 free (input_debug.external_sym);
11135 free (input_debug.external_opt);
11136 free (input_debug.external_aux);
11137 free (input_debug.ss);
11138 free (input_debug.ssext);
11139 free (input_debug.external_fdr);
11140 free (input_debug.external_rfd);
11141 free (input_debug.external_ext);
11143 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11144 elf_link_input_bfd ignores this section. */
11145 input_section->flags &= ~SEC_HAS_CONTENTS;
11148 if (SGI_COMPAT (abfd) && info->shared)
11150 /* Create .rtproc section. */
11151 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
11152 if (rtproc_sec == NULL)
11154 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
11155 | SEC_LINKER_CREATED | SEC_READONLY);
11157 rtproc_sec = bfd_make_section_with_flags (abfd,
11158 ".rtproc",
11159 flags);
11160 if (rtproc_sec == NULL
11161 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
11162 return FALSE;
11165 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
11166 info, rtproc_sec,
11167 &debug))
11168 return FALSE;
11171 /* Build the external symbol information. */
11172 einfo.abfd = abfd;
11173 einfo.info = info;
11174 einfo.debug = &debug;
11175 einfo.swap = swap;
11176 einfo.failed = FALSE;
11177 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
11178 mips_elf_output_extsym, &einfo);
11179 if (einfo.failed)
11180 return FALSE;
11182 /* Set the size of the .mdebug section. */
11183 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
11185 /* Skip this section later on (I don't think this currently
11186 matters, but someday it might). */
11187 o->map_head.link_order = NULL;
11189 mdebug_sec = o;
11192 if (CONST_STRNEQ (o->name, ".gptab."))
11194 const char *subname;
11195 unsigned int c;
11196 Elf32_gptab *tab;
11197 Elf32_External_gptab *ext_tab;
11198 unsigned int j;
11200 /* The .gptab.sdata and .gptab.sbss sections hold
11201 information describing how the small data area would
11202 change depending upon the -G switch. These sections
11203 not used in executables files. */
11204 if (! info->relocatable)
11206 for (p = o->map_head.link_order; p != NULL; p = p->next)
11208 asection *input_section;
11210 if (p->type != bfd_indirect_link_order)
11212 if (p->type == bfd_data_link_order)
11213 continue;
11214 abort ();
11217 input_section = p->u.indirect.section;
11219 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11220 elf_link_input_bfd ignores this section. */
11221 input_section->flags &= ~SEC_HAS_CONTENTS;
11224 /* Skip this section later on (I don't think this
11225 currently matters, but someday it might). */
11226 o->map_head.link_order = NULL;
11228 /* Really remove the section. */
11229 bfd_section_list_remove (abfd, o);
11230 --abfd->section_count;
11232 continue;
11235 /* There is one gptab for initialized data, and one for
11236 uninitialized data. */
11237 if (strcmp (o->name, ".gptab.sdata") == 0)
11238 gptab_data_sec = o;
11239 else if (strcmp (o->name, ".gptab.sbss") == 0)
11240 gptab_bss_sec = o;
11241 else
11243 (*_bfd_error_handler)
11244 (_("%s: illegal section name `%s'"),
11245 bfd_get_filename (abfd), o->name);
11246 bfd_set_error (bfd_error_nonrepresentable_section);
11247 return FALSE;
11250 /* The linker script always combines .gptab.data and
11251 .gptab.sdata into .gptab.sdata, and likewise for
11252 .gptab.bss and .gptab.sbss. It is possible that there is
11253 no .sdata or .sbss section in the output file, in which
11254 case we must change the name of the output section. */
11255 subname = o->name + sizeof ".gptab" - 1;
11256 if (bfd_get_section_by_name (abfd, subname) == NULL)
11258 if (o == gptab_data_sec)
11259 o->name = ".gptab.data";
11260 else
11261 o->name = ".gptab.bss";
11262 subname = o->name + sizeof ".gptab" - 1;
11263 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
11266 /* Set up the first entry. */
11267 c = 1;
11268 amt = c * sizeof (Elf32_gptab);
11269 tab = bfd_malloc (amt);
11270 if (tab == NULL)
11271 return FALSE;
11272 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
11273 tab[0].gt_header.gt_unused = 0;
11275 /* Combine the input sections. */
11276 for (p = o->map_head.link_order; p != NULL; p = p->next)
11278 asection *input_section;
11279 bfd *input_bfd;
11280 bfd_size_type size;
11281 unsigned long last;
11282 bfd_size_type gpentry;
11284 if (p->type != bfd_indirect_link_order)
11286 if (p->type == bfd_data_link_order)
11287 continue;
11288 abort ();
11291 input_section = p->u.indirect.section;
11292 input_bfd = input_section->owner;
11294 /* Combine the gptab entries for this input section one
11295 by one. We know that the input gptab entries are
11296 sorted by ascending -G value. */
11297 size = input_section->size;
11298 last = 0;
11299 for (gpentry = sizeof (Elf32_External_gptab);
11300 gpentry < size;
11301 gpentry += sizeof (Elf32_External_gptab))
11303 Elf32_External_gptab ext_gptab;
11304 Elf32_gptab int_gptab;
11305 unsigned long val;
11306 unsigned long add;
11307 bfd_boolean exact;
11308 unsigned int look;
11310 if (! (bfd_get_section_contents
11311 (input_bfd, input_section, &ext_gptab, gpentry,
11312 sizeof (Elf32_External_gptab))))
11314 free (tab);
11315 return FALSE;
11318 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
11319 &int_gptab);
11320 val = int_gptab.gt_entry.gt_g_value;
11321 add = int_gptab.gt_entry.gt_bytes - last;
11323 exact = FALSE;
11324 for (look = 1; look < c; look++)
11326 if (tab[look].gt_entry.gt_g_value >= val)
11327 tab[look].gt_entry.gt_bytes += add;
11329 if (tab[look].gt_entry.gt_g_value == val)
11330 exact = TRUE;
11333 if (! exact)
11335 Elf32_gptab *new_tab;
11336 unsigned int max;
11338 /* We need a new table entry. */
11339 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
11340 new_tab = bfd_realloc (tab, amt);
11341 if (new_tab == NULL)
11343 free (tab);
11344 return FALSE;
11346 tab = new_tab;
11347 tab[c].gt_entry.gt_g_value = val;
11348 tab[c].gt_entry.gt_bytes = add;
11350 /* Merge in the size for the next smallest -G
11351 value, since that will be implied by this new
11352 value. */
11353 max = 0;
11354 for (look = 1; look < c; look++)
11356 if (tab[look].gt_entry.gt_g_value < val
11357 && (max == 0
11358 || (tab[look].gt_entry.gt_g_value
11359 > tab[max].gt_entry.gt_g_value)))
11360 max = look;
11362 if (max != 0)
11363 tab[c].gt_entry.gt_bytes +=
11364 tab[max].gt_entry.gt_bytes;
11366 ++c;
11369 last = int_gptab.gt_entry.gt_bytes;
11372 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11373 elf_link_input_bfd ignores this section. */
11374 input_section->flags &= ~SEC_HAS_CONTENTS;
11377 /* The table must be sorted by -G value. */
11378 if (c > 2)
11379 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
11381 /* Swap out the table. */
11382 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
11383 ext_tab = bfd_alloc (abfd, amt);
11384 if (ext_tab == NULL)
11386 free (tab);
11387 return FALSE;
11390 for (j = 0; j < c; j++)
11391 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
11392 free (tab);
11394 o->size = c * sizeof (Elf32_External_gptab);
11395 o->contents = (bfd_byte *) ext_tab;
11397 /* Skip this section later on (I don't think this currently
11398 matters, but someday it might). */
11399 o->map_head.link_order = NULL;
11403 /* Invoke the regular ELF backend linker to do all the work. */
11404 if (!bfd_elf_final_link (abfd, info))
11405 return FALSE;
11407 /* Now write out the computed sections. */
11409 if (reginfo_sec != NULL)
11411 Elf32_External_RegInfo ext;
11413 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
11414 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
11415 return FALSE;
11418 if (mdebug_sec != NULL)
11420 BFD_ASSERT (abfd->output_has_begun);
11421 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
11422 swap, info,
11423 mdebug_sec->filepos))
11424 return FALSE;
11426 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
11429 if (gptab_data_sec != NULL)
11431 if (! bfd_set_section_contents (abfd, gptab_data_sec,
11432 gptab_data_sec->contents,
11433 0, gptab_data_sec->size))
11434 return FALSE;
11437 if (gptab_bss_sec != NULL)
11439 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
11440 gptab_bss_sec->contents,
11441 0, gptab_bss_sec->size))
11442 return FALSE;
11445 if (SGI_COMPAT (abfd))
11447 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
11448 if (rtproc_sec != NULL)
11450 if (! bfd_set_section_contents (abfd, rtproc_sec,
11451 rtproc_sec->contents,
11452 0, rtproc_sec->size))
11453 return FALSE;
11457 return TRUE;
11460 /* Structure for saying that BFD machine EXTENSION extends BASE. */
11462 struct mips_mach_extension {
11463 unsigned long extension, base;
11467 /* An array describing how BFD machines relate to one another. The entries
11468 are ordered topologically with MIPS I extensions listed last. */
11470 static const struct mips_mach_extension mips_mach_extensions[] = {
11471 /* MIPS64r2 extensions. */
11472 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
11474 /* MIPS64 extensions. */
11475 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
11476 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
11478 /* MIPS V extensions. */
11479 { bfd_mach_mipsisa64, bfd_mach_mips5 },
11481 /* R10000 extensions. */
11482 { bfd_mach_mips12000, bfd_mach_mips10000 },
11484 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
11485 vr5400 ISA, but doesn't include the multimedia stuff. It seems
11486 better to allow vr5400 and vr5500 code to be merged anyway, since
11487 many libraries will just use the core ISA. Perhaps we could add
11488 some sort of ASE flag if this ever proves a problem. */
11489 { bfd_mach_mips5500, bfd_mach_mips5400 },
11490 { bfd_mach_mips5400, bfd_mach_mips5000 },
11492 /* MIPS IV extensions. */
11493 { bfd_mach_mips5, bfd_mach_mips8000 },
11494 { bfd_mach_mips10000, bfd_mach_mips8000 },
11495 { bfd_mach_mips5000, bfd_mach_mips8000 },
11496 { bfd_mach_mips7000, bfd_mach_mips8000 },
11497 { bfd_mach_mips9000, bfd_mach_mips8000 },
11499 /* VR4100 extensions. */
11500 { bfd_mach_mips4120, bfd_mach_mips4100 },
11501 { bfd_mach_mips4111, bfd_mach_mips4100 },
11503 /* MIPS III extensions. */
11504 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
11505 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
11506 { bfd_mach_mips8000, bfd_mach_mips4000 },
11507 { bfd_mach_mips4650, bfd_mach_mips4000 },
11508 { bfd_mach_mips4600, bfd_mach_mips4000 },
11509 { bfd_mach_mips4400, bfd_mach_mips4000 },
11510 { bfd_mach_mips4300, bfd_mach_mips4000 },
11511 { bfd_mach_mips4100, bfd_mach_mips4000 },
11512 { bfd_mach_mips4010, bfd_mach_mips4000 },
11514 /* MIPS32 extensions. */
11515 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
11517 /* MIPS II extensions. */
11518 { bfd_mach_mips4000, bfd_mach_mips6000 },
11519 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
11521 /* MIPS I extensions. */
11522 { bfd_mach_mips6000, bfd_mach_mips3000 },
11523 { bfd_mach_mips3900, bfd_mach_mips3000 }
11527 /* Return true if bfd machine EXTENSION is an extension of machine BASE. */
11529 static bfd_boolean
11530 mips_mach_extends_p (unsigned long base, unsigned long extension)
11532 size_t i;
11534 if (extension == base)
11535 return TRUE;
11537 if (base == bfd_mach_mipsisa32
11538 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
11539 return TRUE;
11541 if (base == bfd_mach_mipsisa32r2
11542 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
11543 return TRUE;
11545 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
11546 if (extension == mips_mach_extensions[i].extension)
11548 extension = mips_mach_extensions[i].base;
11549 if (extension == base)
11550 return TRUE;
11553 return FALSE;
11557 /* Return true if the given ELF header flags describe a 32-bit binary. */
11559 static bfd_boolean
11560 mips_32bit_flags_p (flagword flags)
11562 return ((flags & EF_MIPS_32BITMODE) != 0
11563 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
11564 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
11565 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
11566 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
11567 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
11568 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
11572 /* Merge object attributes from IBFD into OBFD. Raise an error if
11573 there are conflicting attributes. */
11574 static bfd_boolean
11575 mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
11577 obj_attribute *in_attr;
11578 obj_attribute *out_attr;
11580 if (!elf_known_obj_attributes_proc (obfd)[0].i)
11582 /* This is the first object. Copy the attributes. */
11583 _bfd_elf_copy_obj_attributes (ibfd, obfd);
11585 /* Use the Tag_null value to indicate the attributes have been
11586 initialized. */
11587 elf_known_obj_attributes_proc (obfd)[0].i = 1;
11589 return TRUE;
11592 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
11593 non-conflicting ones. */
11594 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
11595 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
11596 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
11598 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
11599 if (out_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
11600 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
11601 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
11603 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
11604 _bfd_error_handler
11605 (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
11606 in_attr[Tag_GNU_MIPS_ABI_FP].i);
11607 else if (out_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
11608 _bfd_error_handler
11609 (_("Warning: %B uses unknown floating point ABI %d"), obfd,
11610 out_attr[Tag_GNU_MIPS_ABI_FP].i);
11611 else
11612 switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
11614 case 1:
11615 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
11617 case 2:
11618 _bfd_error_handler
11619 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
11620 obfd, ibfd);
11621 break;
11623 case 3:
11624 _bfd_error_handler
11625 (_("Warning: %B uses hard float, %B uses soft float"),
11626 obfd, ibfd);
11627 break;
11629 case 4:
11630 _bfd_error_handler
11631 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
11632 obfd, ibfd);
11633 break;
11635 default:
11636 abort ();
11638 break;
11640 case 2:
11641 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
11643 case 1:
11644 _bfd_error_handler
11645 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
11646 ibfd, obfd);
11647 break;
11649 case 3:
11650 _bfd_error_handler
11651 (_("Warning: %B uses hard float, %B uses soft float"),
11652 obfd, ibfd);
11653 break;
11655 case 4:
11656 _bfd_error_handler
11657 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
11658 obfd, ibfd);
11659 break;
11661 default:
11662 abort ();
11664 break;
11666 case 3:
11667 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
11669 case 1:
11670 case 2:
11671 case 4:
11672 _bfd_error_handler
11673 (_("Warning: %B uses hard float, %B uses soft float"),
11674 ibfd, obfd);
11675 break;
11677 default:
11678 abort ();
11680 break;
11682 case 4:
11683 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
11685 case 1:
11686 _bfd_error_handler
11687 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
11688 ibfd, obfd);
11689 break;
11691 case 2:
11692 _bfd_error_handler
11693 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
11694 ibfd, obfd);
11695 break;
11697 case 3:
11698 _bfd_error_handler
11699 (_("Warning: %B uses hard float, %B uses soft float"),
11700 obfd, ibfd);
11701 break;
11703 default:
11704 abort ();
11706 break;
11708 default:
11709 abort ();
11713 /* Merge Tag_compatibility attributes and any common GNU ones. */
11714 _bfd_elf_merge_object_attributes (ibfd, obfd);
11716 return TRUE;
11719 /* Merge backend specific data from an object file to the output
11720 object file when linking. */
11722 bfd_boolean
11723 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
11725 flagword old_flags;
11726 flagword new_flags;
11727 bfd_boolean ok;
11728 bfd_boolean null_input_bfd = TRUE;
11729 asection *sec;
11731 /* Check if we have the same endianess */
11732 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
11734 (*_bfd_error_handler)
11735 (_("%B: endianness incompatible with that of the selected emulation"),
11736 ibfd);
11737 return FALSE;
11740 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
11741 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
11742 return TRUE;
11744 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
11746 (*_bfd_error_handler)
11747 (_("%B: ABI is incompatible with that of the selected emulation"),
11748 ibfd);
11749 return FALSE;
11752 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
11753 return FALSE;
11755 new_flags = elf_elfheader (ibfd)->e_flags;
11756 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
11757 old_flags = elf_elfheader (obfd)->e_flags;
11759 if (! elf_flags_init (obfd))
11761 elf_flags_init (obfd) = TRUE;
11762 elf_elfheader (obfd)->e_flags = new_flags;
11763 elf_elfheader (obfd)->e_ident[EI_CLASS]
11764 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
11766 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
11767 && (bfd_get_arch_info (obfd)->the_default
11768 || mips_mach_extends_p (bfd_get_mach (obfd),
11769 bfd_get_mach (ibfd))))
11771 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
11772 bfd_get_mach (ibfd)))
11773 return FALSE;
11776 return TRUE;
11779 /* Check flag compatibility. */
11781 new_flags &= ~EF_MIPS_NOREORDER;
11782 old_flags &= ~EF_MIPS_NOREORDER;
11784 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
11785 doesn't seem to matter. */
11786 new_flags &= ~EF_MIPS_XGOT;
11787 old_flags &= ~EF_MIPS_XGOT;
11789 /* MIPSpro generates ucode info in n64 objects. Again, we should
11790 just be able to ignore this. */
11791 new_flags &= ~EF_MIPS_UCODE;
11792 old_flags &= ~EF_MIPS_UCODE;
11794 /* Don't care about the PIC flags from dynamic objects; they are
11795 PIC by design. */
11796 if ((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0
11797 && (ibfd->flags & DYNAMIC) != 0)
11798 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
11800 if (new_flags == old_flags)
11801 return TRUE;
11803 /* Check to see if the input BFD actually contains any sections.
11804 If not, its flags may not have been initialised either, but it cannot
11805 actually cause any incompatibility. */
11806 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
11808 /* Ignore synthetic sections and empty .text, .data and .bss sections
11809 which are automatically generated by gas. */
11810 if (strcmp (sec->name, ".reginfo")
11811 && strcmp (sec->name, ".mdebug")
11812 && (sec->size != 0
11813 || (strcmp (sec->name, ".text")
11814 && strcmp (sec->name, ".data")
11815 && strcmp (sec->name, ".bss"))))
11817 null_input_bfd = FALSE;
11818 break;
11821 if (null_input_bfd)
11822 return TRUE;
11824 ok = TRUE;
11826 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
11827 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
11829 (*_bfd_error_handler)
11830 (_("%B: warning: linking PIC files with non-PIC files"),
11831 ibfd);
11832 ok = TRUE;
11835 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
11836 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
11837 if (! (new_flags & EF_MIPS_PIC))
11838 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
11840 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
11841 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
11843 /* Compare the ISAs. */
11844 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
11846 (*_bfd_error_handler)
11847 (_("%B: linking 32-bit code with 64-bit code"),
11848 ibfd);
11849 ok = FALSE;
11851 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
11853 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
11854 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
11856 /* Copy the architecture info from IBFD to OBFD. Also copy
11857 the 32-bit flag (if set) so that we continue to recognise
11858 OBFD as a 32-bit binary. */
11859 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
11860 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
11861 elf_elfheader (obfd)->e_flags
11862 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
11864 /* Copy across the ABI flags if OBFD doesn't use them
11865 and if that was what caused us to treat IBFD as 32-bit. */
11866 if ((old_flags & EF_MIPS_ABI) == 0
11867 && mips_32bit_flags_p (new_flags)
11868 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
11869 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
11871 else
11873 /* The ISAs aren't compatible. */
11874 (*_bfd_error_handler)
11875 (_("%B: linking %s module with previous %s modules"),
11876 ibfd,
11877 bfd_printable_name (ibfd),
11878 bfd_printable_name (obfd));
11879 ok = FALSE;
11883 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
11884 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
11886 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
11887 does set EI_CLASS differently from any 32-bit ABI. */
11888 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
11889 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
11890 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
11892 /* Only error if both are set (to different values). */
11893 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
11894 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
11895 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
11897 (*_bfd_error_handler)
11898 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
11899 ibfd,
11900 elf_mips_abi_name (ibfd),
11901 elf_mips_abi_name (obfd));
11902 ok = FALSE;
11904 new_flags &= ~EF_MIPS_ABI;
11905 old_flags &= ~EF_MIPS_ABI;
11908 /* For now, allow arbitrary mixing of ASEs (retain the union). */
11909 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
11911 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
11913 new_flags &= ~ EF_MIPS_ARCH_ASE;
11914 old_flags &= ~ EF_MIPS_ARCH_ASE;
11917 /* Warn about any other mismatches */
11918 if (new_flags != old_flags)
11920 (*_bfd_error_handler)
11921 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
11922 ibfd, (unsigned long) new_flags,
11923 (unsigned long) old_flags);
11924 ok = FALSE;
11927 if (! ok)
11929 bfd_set_error (bfd_error_bad_value);
11930 return FALSE;
11933 return TRUE;
11936 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
11938 bfd_boolean
11939 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
11941 BFD_ASSERT (!elf_flags_init (abfd)
11942 || elf_elfheader (abfd)->e_flags == flags);
11944 elf_elfheader (abfd)->e_flags = flags;
11945 elf_flags_init (abfd) = TRUE;
11946 return TRUE;
11949 char *
11950 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
11952 switch (dtag)
11954 default: return "";
11955 case DT_MIPS_RLD_VERSION:
11956 return "MIPS_RLD_VERSION";
11957 case DT_MIPS_TIME_STAMP:
11958 return "MIPS_TIME_STAMP";
11959 case DT_MIPS_ICHECKSUM:
11960 return "MIPS_ICHECKSUM";
11961 case DT_MIPS_IVERSION:
11962 return "MIPS_IVERSION";
11963 case DT_MIPS_FLAGS:
11964 return "MIPS_FLAGS";
11965 case DT_MIPS_BASE_ADDRESS:
11966 return "MIPS_BASE_ADDRESS";
11967 case DT_MIPS_MSYM:
11968 return "MIPS_MSYM";
11969 case DT_MIPS_CONFLICT:
11970 return "MIPS_CONFLICT";
11971 case DT_MIPS_LIBLIST:
11972 return "MIPS_LIBLIST";
11973 case DT_MIPS_LOCAL_GOTNO:
11974 return "MIPS_LOCAL_GOTNO";
11975 case DT_MIPS_CONFLICTNO:
11976 return "MIPS_CONFLICTNO";
11977 case DT_MIPS_LIBLISTNO:
11978 return "MIPS_LIBLISTNO";
11979 case DT_MIPS_SYMTABNO:
11980 return "MIPS_SYMTABNO";
11981 case DT_MIPS_UNREFEXTNO:
11982 return "MIPS_UNREFEXTNO";
11983 case DT_MIPS_GOTSYM:
11984 return "MIPS_GOTSYM";
11985 case DT_MIPS_HIPAGENO:
11986 return "MIPS_HIPAGENO";
11987 case DT_MIPS_RLD_MAP:
11988 return "MIPS_RLD_MAP";
11989 case DT_MIPS_DELTA_CLASS:
11990 return "MIPS_DELTA_CLASS";
11991 case DT_MIPS_DELTA_CLASS_NO:
11992 return "MIPS_DELTA_CLASS_NO";
11993 case DT_MIPS_DELTA_INSTANCE:
11994 return "MIPS_DELTA_INSTANCE";
11995 case DT_MIPS_DELTA_INSTANCE_NO:
11996 return "MIPS_DELTA_INSTANCE_NO";
11997 case DT_MIPS_DELTA_RELOC:
11998 return "MIPS_DELTA_RELOC";
11999 case DT_MIPS_DELTA_RELOC_NO:
12000 return "MIPS_DELTA_RELOC_NO";
12001 case DT_MIPS_DELTA_SYM:
12002 return "MIPS_DELTA_SYM";
12003 case DT_MIPS_DELTA_SYM_NO:
12004 return "MIPS_DELTA_SYM_NO";
12005 case DT_MIPS_DELTA_CLASSSYM:
12006 return "MIPS_DELTA_CLASSSYM";
12007 case DT_MIPS_DELTA_CLASSSYM_NO:
12008 return "MIPS_DELTA_CLASSSYM_NO";
12009 case DT_MIPS_CXX_FLAGS:
12010 return "MIPS_CXX_FLAGS";
12011 case DT_MIPS_PIXIE_INIT:
12012 return "MIPS_PIXIE_INIT";
12013 case DT_MIPS_SYMBOL_LIB:
12014 return "MIPS_SYMBOL_LIB";
12015 case DT_MIPS_LOCALPAGE_GOTIDX:
12016 return "MIPS_LOCALPAGE_GOTIDX";
12017 case DT_MIPS_LOCAL_GOTIDX:
12018 return "MIPS_LOCAL_GOTIDX";
12019 case DT_MIPS_HIDDEN_GOTIDX:
12020 return "MIPS_HIDDEN_GOTIDX";
12021 case DT_MIPS_PROTECTED_GOTIDX:
12022 return "MIPS_PROTECTED_GOT_IDX";
12023 case DT_MIPS_OPTIONS:
12024 return "MIPS_OPTIONS";
12025 case DT_MIPS_INTERFACE:
12026 return "MIPS_INTERFACE";
12027 case DT_MIPS_DYNSTR_ALIGN:
12028 return "DT_MIPS_DYNSTR_ALIGN";
12029 case DT_MIPS_INTERFACE_SIZE:
12030 return "DT_MIPS_INTERFACE_SIZE";
12031 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
12032 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
12033 case DT_MIPS_PERF_SUFFIX:
12034 return "DT_MIPS_PERF_SUFFIX";
12035 case DT_MIPS_COMPACT_SIZE:
12036 return "DT_MIPS_COMPACT_SIZE";
12037 case DT_MIPS_GP_VALUE:
12038 return "DT_MIPS_GP_VALUE";
12039 case DT_MIPS_AUX_DYNAMIC:
12040 return "DT_MIPS_AUX_DYNAMIC";
12044 bfd_boolean
12045 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
12047 FILE *file = ptr;
12049 BFD_ASSERT (abfd != NULL && ptr != NULL);
12051 /* Print normal ELF private data. */
12052 _bfd_elf_print_private_bfd_data (abfd, ptr);
12054 /* xgettext:c-format */
12055 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
12057 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
12058 fprintf (file, _(" [abi=O32]"));
12059 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
12060 fprintf (file, _(" [abi=O64]"));
12061 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
12062 fprintf (file, _(" [abi=EABI32]"));
12063 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
12064 fprintf (file, _(" [abi=EABI64]"));
12065 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
12066 fprintf (file, _(" [abi unknown]"));
12067 else if (ABI_N32_P (abfd))
12068 fprintf (file, _(" [abi=N32]"));
12069 else if (ABI_64_P (abfd))
12070 fprintf (file, _(" [abi=64]"));
12071 else
12072 fprintf (file, _(" [no abi set]"));
12074 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
12075 fprintf (file, " [mips1]");
12076 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
12077 fprintf (file, " [mips2]");
12078 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
12079 fprintf (file, " [mips3]");
12080 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
12081 fprintf (file, " [mips4]");
12082 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
12083 fprintf (file, " [mips5]");
12084 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
12085 fprintf (file, " [mips32]");
12086 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
12087 fprintf (file, " [mips64]");
12088 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
12089 fprintf (file, " [mips32r2]");
12090 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
12091 fprintf (file, " [mips64r2]");
12092 else
12093 fprintf (file, _(" [unknown ISA]"));
12095 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
12096 fprintf (file, " [mdmx]");
12098 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
12099 fprintf (file, " [mips16]");
12101 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
12102 fprintf (file, " [32bitmode]");
12103 else
12104 fprintf (file, _(" [not 32bitmode]"));
12106 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
12107 fprintf (file, " [noreorder]");
12109 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
12110 fprintf (file, " [PIC]");
12112 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
12113 fprintf (file, " [CPIC]");
12115 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
12116 fprintf (file, " [XGOT]");
12118 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
12119 fprintf (file, " [UCODE]");
12121 fputc ('\n', file);
12123 return TRUE;
12126 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
12128 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12129 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12130 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
12131 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12132 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12133 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
12134 { NULL, 0, 0, 0, 0 }
12137 /* Merge non visibility st_other attributes. Ensure that the
12138 STO_OPTIONAL flag is copied into h->other, even if this is not a
12139 definiton of the symbol. */
12140 void
12141 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
12142 const Elf_Internal_Sym *isym,
12143 bfd_boolean definition,
12144 bfd_boolean dynamic ATTRIBUTE_UNUSED)
12146 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
12148 unsigned char other;
12150 other = (definition ? isym->st_other : h->other);
12151 other &= ~ELF_ST_VISIBILITY (-1);
12152 h->other = other | ELF_ST_VISIBILITY (h->other);
12155 if (!definition
12156 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
12157 h->other |= STO_OPTIONAL;
12160 /* Decide whether an undefined symbol is special and can be ignored.
12161 This is the case for OPTIONAL symbols on IRIX. */
12162 bfd_boolean
12163 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
12165 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
12168 bfd_boolean
12169 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
12171 return (sym->st_shndx == SHN_COMMON
12172 || sym->st_shndx == SHN_MIPS_ACOMMON
12173 || sym->st_shndx == SHN_MIPS_SCOMMON);