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