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
7279 if (SGI_COMPAT (abfd))
7280 mips_elf_hash_table (info)->compact_rel_size +=
7281 sizeof (Elf32_External_crinfo);
7282 break;
7283
7284 case R_MIPS_PC16:
7285 if (h)
7286 ((struct mips_elf_link_hash_entry *) h)->is_branch_target = TRUE;
7287 break;
7288
7289 case R_MIPS_26:
7290 if (h)
7291 ((struct mips_elf_link_hash_entry *) h)->is_branch_target = TRUE;
7292 /* Fall through. */
7293
7294 case R_MIPS_GPREL16:
7295 case R_MIPS_LITERAL:
7296 case R_MIPS_GPREL32:
7297 if (SGI_COMPAT (abfd))
7298 mips_elf_hash_table (info)->compact_rel_size +=
7299 sizeof (Elf32_External_crinfo);
7300 break;
7301
7302 /* This relocation describes the C++ object vtable hierarchy.
7303 Reconstruct it for later use during GC. */
7304 case R_MIPS_GNU_VTINHERIT:
7305 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7306 return FALSE;
7307 break;
7308
7309 /* This relocation describes which C++ vtable entries are actually
7310 used. Record for later use during GC. */
7311 case R_MIPS_GNU_VTENTRY:
7312 BFD_ASSERT (h != NULL);
7313 if (h != NULL
7314 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7315 return FALSE;
7316 break;
7317
7318 default:
7319 break;
7320 }
7321
7322 /* We must not create a stub for a symbol that has relocations
7323 related to taking the function's address. This doesn't apply to
7324 VxWorks, where CALL relocs refer to a .got.plt entry instead of
7325 a normal .got entry. */
7326 if (!htab->is_vxworks && h != NULL)
7327 switch (r_type)
7328 {
7329 default:
7330 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
7331 break;
7332 case R_MIPS16_CALL16:
7333 case R_MIPS_CALL16:
7334 case R_MIPS_CALL_HI16:
7335 case R_MIPS_CALL_LO16:
7336 case R_MIPS_JALR:
7337 break;
7338 }
7339
7340 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
7341 if there is one. We only need to handle global symbols here;
7342 we decide whether to keep or delete stubs for local symbols
7343 when processing the stub's relocations. */
7344 if (h != NULL
7345 && !mips16_call_reloc_p (r_type)
7346 && !section_allows_mips16_refs_p (sec))
7347 {
7348 struct mips_elf_link_hash_entry *mh;
7349
7350 mh = (struct mips_elf_link_hash_entry *) h;
7351 mh->need_fn_stub = TRUE;
7352 }
7353 }
7354
7355 return TRUE;
7356 }
7357 \f
7358 bfd_boolean
7359 _bfd_mips_relax_section (bfd *abfd, asection *sec,
7360 struct bfd_link_info *link_info,
7361 bfd_boolean *again)
7362 {
7363 Elf_Internal_Rela *internal_relocs;
7364 Elf_Internal_Rela *irel, *irelend;
7365 Elf_Internal_Shdr *symtab_hdr;
7366 bfd_byte *contents = NULL;
7367 size_t extsymoff;
7368 bfd_boolean changed_contents = FALSE;
7369 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
7370 Elf_Internal_Sym *isymbuf = NULL;
7371
7372 /* We are not currently changing any sizes, so only one pass. */
7373 *again = FALSE;
7374
7375 if (link_info->relocatable)
7376 return TRUE;
7377
7378 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7379 link_info->keep_memory);
7380 if (internal_relocs == NULL)
7381 return TRUE;
7382
7383 irelend = internal_relocs + sec->reloc_count
7384 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
7385 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7386 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7387
7388 for (irel = internal_relocs; irel < irelend; irel++)
7389 {
7390 bfd_vma symval;
7391 bfd_signed_vma sym_offset;
7392 unsigned int r_type;
7393 unsigned long r_symndx;
7394 asection *sym_sec;
7395 unsigned long instruction;
7396
7397 /* Turn jalr into bgezal, and jr into beq, if they're marked
7398 with a JALR relocation, that indicate where they jump to.
7399 This saves some pipeline bubbles. */
7400 r_type = ELF_R_TYPE (abfd, irel->r_info);
7401 if (r_type != R_MIPS_JALR)
7402 continue;
7403
7404 r_symndx = ELF_R_SYM (abfd, irel->r_info);
7405 /* Compute the address of the jump target. */
7406 if (r_symndx >= extsymoff)
7407 {
7408 struct mips_elf_link_hash_entry *h
7409 = ((struct mips_elf_link_hash_entry *)
7410 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
7411
7412 while (h->root.root.type == bfd_link_hash_indirect
7413 || h->root.root.type == bfd_link_hash_warning)
7414 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7415
7416 /* If a symbol is undefined, or if it may be overridden,
7417 skip it. */
7418 if (! ((h->root.root.type == bfd_link_hash_defined
7419 || h->root.root.type == bfd_link_hash_defweak)
7420 && h->root.root.u.def.section)
7421 || (link_info->shared && ! link_info->symbolic
7422 && !h->root.forced_local))
7423 continue;
7424
7425 sym_sec = h->root.root.u.def.section;
7426 if (sym_sec->output_section)
7427 symval = (h->root.root.u.def.value
7428 + sym_sec->output_section->vma
7429 + sym_sec->output_offset);
7430 else
7431 symval = h->root.root.u.def.value;
7432 }
7433 else
7434 {
7435 Elf_Internal_Sym *isym;
7436
7437 /* Read this BFD's symbols if we haven't done so already. */
7438 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
7439 {
7440 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
7441 if (isymbuf == NULL)
7442 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
7443 symtab_hdr->sh_info, 0,
7444 NULL, NULL, NULL);
7445 if (isymbuf == NULL)
7446 goto relax_return;
7447 }
7448
7449 isym = isymbuf + r_symndx;
7450 if (isym->st_shndx == SHN_UNDEF)
7451 continue;
7452 else if (isym->st_shndx == SHN_ABS)
7453 sym_sec = bfd_abs_section_ptr;
7454 else if (isym->st_shndx == SHN_COMMON)
7455 sym_sec = bfd_com_section_ptr;
7456 else
7457 sym_sec
7458 = bfd_section_from_elf_index (abfd, isym->st_shndx);
7459 symval = isym->st_value
7460 + sym_sec->output_section->vma
7461 + sym_sec->output_offset;
7462 }
7463
7464 /* Compute branch offset, from delay slot of the jump to the
7465 branch target. */
7466 sym_offset = (symval + irel->r_addend)
7467 - (sec_start + irel->r_offset + 4);
7468
7469 /* Branch offset must be properly aligned. */
7470 if ((sym_offset & 3) != 0)
7471 continue;
7472
7473 sym_offset >>= 2;
7474
7475 /* Check that it's in range. */
7476 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
7477 continue;
7478
7479 /* Get the section contents if we haven't done so already. */
7480 if (!mips_elf_get_section_contents (abfd, sec, &contents))
7481 goto relax_return;
7482
7483 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
7484
7485 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
7486 if ((instruction & 0xfc1fffff) == 0x0000f809)
7487 instruction = 0x04110000;
7488 /* If it was jr <reg>, turn it into b <target>. */
7489 else if ((instruction & 0xfc1fffff) == 0x00000008)
7490 instruction = 0x10000000;
7491 else
7492 continue;
7493
7494 instruction |= (sym_offset & 0xffff);
7495 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
7496 changed_contents = TRUE;
7497 }
7498
7499 if (contents != NULL
7500 && elf_section_data (sec)->this_hdr.contents != contents)
7501 {
7502 if (!changed_contents && !link_info->keep_memory)
7503 free (contents);
7504 else
7505 {
7506 /* Cache the section contents for elf_link_input_bfd. */
7507 elf_section_data (sec)->this_hdr.contents = contents;
7508 }
7509 }
7510 return TRUE;
7511
7512 relax_return:
7513 if (contents != NULL
7514 && elf_section_data (sec)->this_hdr.contents != contents)
7515 free (contents);
7516 return FALSE;
7517 }
7518 \f
7519 /* Allocate space for global sym dynamic relocs. */
7520
7521 static bfd_boolean
7522 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
7523 {
7524 struct bfd_link_info *info = inf;
7525 bfd *dynobj;
7526 struct mips_elf_link_hash_entry *hmips;
7527 struct mips_elf_link_hash_table *htab;
7528
7529 htab = mips_elf_hash_table (info);
7530 dynobj = elf_hash_table (info)->dynobj;
7531 hmips = (struct mips_elf_link_hash_entry *) h;
7532
7533 /* VxWorks executables are handled elsewhere; we only need to
7534 allocate relocations in shared objects. */
7535 if (htab->is_vxworks && !info->shared)
7536 return TRUE;
7537
7538 /* Ignore indirect and warning symbols. All relocations against
7539 such symbols will be redirected to the target symbol. */
7540 if (h->root.type == bfd_link_hash_indirect
7541 || h->root.type == bfd_link_hash_warning)
7542 return TRUE;
7543
7544 /* If this symbol is defined in a dynamic object, or we are creating
7545 a shared library, we will need to copy any R_MIPS_32 or
7546 R_MIPS_REL32 relocs against it into the output file. */
7547 if (! info->relocatable
7548 && hmips->possibly_dynamic_relocs != 0
7549 && (h->root.type == bfd_link_hash_defweak
7550 || !h->def_regular
7551 || info->shared))
7552 {
7553 bfd_boolean do_copy = TRUE;
7554
7555 if (h->root.type == bfd_link_hash_undefweak)
7556 {
7557 /* Do not copy relocations for undefined weak symbols with
7558 non-default visibility. */
7559 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
7560 do_copy = FALSE;
7561
7562 /* Make sure undefined weak symbols are output as a dynamic
7563 symbol in PIEs. */
7564 else if (h->dynindx == -1 && !h->forced_local)
7565 {
7566 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7567 return FALSE;
7568 }
7569 }
7570
7571 if (do_copy)
7572 {
7573 /* Even though we don't directly need a GOT entry for this symbol,
7574 a symbol must have a dynamic symbol table index greater that
7575 DT_MIPS_GOTSYM if there are dynamic relocations against it. */
7576 if (hmips->global_got_area > GGA_RELOC_ONLY)
7577 hmips->global_got_area = GGA_RELOC_ONLY;
7578
7579 mips_elf_allocate_dynamic_relocations
7580 (dynobj, info, hmips->possibly_dynamic_relocs);
7581 if (hmips->readonly_reloc)
7582 /* We tell the dynamic linker that there are relocations
7583 against the text segment. */
7584 info->flags |= DF_TEXTREL;
7585 }
7586 }
7587
7588 return TRUE;
7589 }
7590
7591 /* Adjust a symbol defined by a dynamic object and referenced by a
7592 regular object. The current definition is in some section of the
7593 dynamic object, but we're not including those sections. We have to
7594 change the definition to something the rest of the link can
7595 understand. */
7596
7597 bfd_boolean
7598 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7599 struct elf_link_hash_entry *h)
7600 {
7601 bfd *dynobj;
7602 struct mips_elf_link_hash_entry *hmips;
7603 struct mips_elf_link_hash_table *htab;
7604
7605 htab = mips_elf_hash_table (info);
7606 dynobj = elf_hash_table (info)->dynobj;
7607
7608 /* Make sure we know what is going on here. */
7609 BFD_ASSERT (dynobj != NULL
7610 && (h->needs_plt
7611 || h->u.weakdef != NULL
7612 || (h->def_dynamic
7613 && h->ref_regular
7614 && !h->def_regular)));
7615
7616 hmips = (struct mips_elf_link_hash_entry *) h;
7617
7618 /* For a function, create a stub, if allowed. */
7619 if (! hmips->no_fn_stub
7620 && h->needs_plt)
7621 {
7622 if (! elf_hash_table (info)->dynamic_sections_created)
7623 return TRUE;
7624
7625 /* If this symbol is not defined in a regular file, then set
7626 the symbol to the stub location. This is required to make
7627 function pointers compare as equal between the normal
7628 executable and the shared library. */
7629 if (!h->def_regular)
7630 {
7631 hmips->needs_lazy_stub = TRUE;
7632 htab->lazy_stub_count++;
7633 return TRUE;
7634 }
7635 }
7636 else if ((h->type == STT_FUNC)
7637 && !h->needs_plt)
7638 {
7639 /* This will set the entry for this symbol in the GOT to 0, and
7640 the dynamic linker will take care of this. */
7641 h->root.u.def.value = 0;
7642 return TRUE;
7643 }
7644
7645 /* If this is a weak symbol, and there is a real definition, the
7646 processor independent code will have arranged for us to see the
7647 real definition first, and we can just use the same value. */
7648 if (h->u.weakdef != NULL)
7649 {
7650 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7651 || h->u.weakdef->root.type == bfd_link_hash_defweak);
7652 h->root.u.def.section = h->u.weakdef->root.u.def.section;
7653 h->root.u.def.value = h->u.weakdef->root.u.def.value;
7654 return TRUE;
7655 }
7656
7657 /* This is a reference to a symbol defined by a dynamic object which
7658 is not a function. */
7659
7660 return TRUE;
7661 }
7662
7663 /* Likewise, for VxWorks. */
7664
7665 bfd_boolean
7666 _bfd_mips_vxworks_adjust_dynamic_symbol (struct bfd_link_info *info,
7667 struct elf_link_hash_entry *h)
7668 {
7669 bfd *dynobj;
7670 struct mips_elf_link_hash_entry *hmips;
7671 struct mips_elf_link_hash_table *htab;
7672
7673 htab = mips_elf_hash_table (info);
7674 dynobj = elf_hash_table (info)->dynobj;
7675 hmips = (struct mips_elf_link_hash_entry *) h;
7676
7677 /* Make sure we know what is going on here. */
7678 BFD_ASSERT (dynobj != NULL
7679 && (h->needs_plt
7680 || h->needs_copy
7681 || h->u.weakdef != NULL
7682 || (h->def_dynamic
7683 && h->ref_regular
7684 && !h->def_regular)));
7685
7686 /* If the symbol is defined by a dynamic object, we need a PLT stub if
7687 either (a) we want to branch to the symbol or (b) we're linking an
7688 executable that needs a canonical function address. In the latter
7689 case, the canonical address will be the address of the executable's
7690 load stub. */
7691 if ((hmips->is_branch_target
7692 || (!info->shared
7693 && h->type == STT_FUNC
7694 && hmips->is_relocation_target))
7695 && h->def_dynamic
7696 && h->ref_regular
7697 && !h->def_regular
7698 && !h->forced_local)
7699 h->needs_plt = 1;
7700
7701 /* Locally-binding symbols do not need a PLT stub; we can refer to
7702 the functions directly. */
7703 else if (h->needs_plt
7704 && (SYMBOL_CALLS_LOCAL (info, h)
7705 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
7706 && h->root.type == bfd_link_hash_undefweak)))
7707 {
7708 h->needs_plt = 0;
7709 return TRUE;
7710 }
7711
7712 if (h->needs_plt)
7713 {
7714 /* If this is the first symbol to need a PLT entry, allocate room
7715 for the header, and for the header's .rela.plt.unloaded entries. */
7716 if (htab->splt->size == 0)
7717 {
7718 htab->splt->size += htab->plt_header_size;
7719 if (!info->shared)
7720 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
7721 }
7722
7723 /* Assign the next .plt entry to this symbol. */
7724 h->plt.offset = htab->splt->size;
7725 htab->splt->size += htab->plt_entry_size;
7726
7727 /* If the output file has no definition of the symbol, set the
7728 symbol's value to the address of the stub. Point at the PLT
7729 load stub rather than the lazy resolution stub; this stub
7730 will become the canonical function address. */
7731 if (!info->shared && !h->def_regular)
7732 {
7733 h->root.u.def.section = htab->splt;
7734 h->root.u.def.value = h->plt.offset;
7735 h->root.u.def.value += 8;
7736 }
7737
7738 /* Make room for the .got.plt entry and the R_JUMP_SLOT relocation. */
7739 htab->sgotplt->size += 4;
7740 htab->srelplt->size += sizeof (Elf32_External_Rela);
7741
7742 /* Make room for the .rela.plt.unloaded relocations. */
7743 if (!info->shared)
7744 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
7745
7746 return TRUE;
7747 }
7748
7749 /* If a function symbol is defined by a dynamic object, and we do not
7750 need a PLT stub for it, the symbol's value should be zero. */
7751 if (h->type == STT_FUNC
7752 && h->def_dynamic
7753 && h->ref_regular
7754 && !h->def_regular)
7755 {
7756 h->root.u.def.value = 0;
7757 return TRUE;
7758 }
7759
7760 /* If this is a weak symbol, and there is a real definition, the
7761 processor independent code will have arranged for us to see the
7762 real definition first, and we can just use the same value. */
7763 if (h->u.weakdef != NULL)
7764 {
7765 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7766 || h->u.weakdef->root.type == bfd_link_hash_defweak);
7767 h->root.u.def.section = h->u.weakdef->root.u.def.section;
7768 h->root.u.def.value = h->u.weakdef->root.u.def.value;
7769 return TRUE;
7770 }
7771
7772 /* This is a reference to a symbol defined by a dynamic object which
7773 is not a function. */
7774 if (info->shared)
7775 return TRUE;
7776
7777 /* We must allocate the symbol in our .dynbss section, which will
7778 become part of the .bss section of the executable. There will be
7779 an entry for this symbol in the .dynsym section. The dynamic
7780 object will contain position independent code, so all references
7781 from the dynamic object to this symbol will go through the global
7782 offset table. The dynamic linker will use the .dynsym entry to
7783 determine the address it must put in the global offset table, so
7784 both the dynamic object and the regular object will refer to the
7785 same memory location for the variable. */
7786
7787 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
7788 {
7789 htab->srelbss->size += sizeof (Elf32_External_Rela);
7790 h->needs_copy = 1;
7791 }
7792
7793 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
7794 }
7795 \f
7796 /* This function is called after all the input files have been read,
7797 and the input sections have been assigned to output sections. We
7798 check for any mips16 stub sections that we can discard. */
7799
7800 bfd_boolean
7801 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
7802 struct bfd_link_info *info)
7803 {
7804 asection *ri;
7805 struct mips_elf_link_hash_table *htab;
7806
7807 htab = mips_elf_hash_table (info);
7808
7809 /* The .reginfo section has a fixed size. */
7810 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
7811 if (ri != NULL)
7812 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
7813
7814 if (! (info->relocatable
7815 || ! mips_elf_hash_table (info)->mips16_stubs_seen))
7816 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
7817 mips_elf_check_mips16_stubs, info);
7818
7819 return TRUE;
7820 }
7821
7822 /* If the link uses a GOT, lay it out and work out its size. */
7823
7824 static bfd_boolean
7825 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
7826 {
7827 bfd *dynobj;
7828 asection *s;
7829 struct mips_got_info *g;
7830 bfd_size_type loadable_size = 0;
7831 bfd_size_type page_gotno;
7832 bfd *sub;
7833 struct mips_elf_count_tls_arg count_tls_arg;
7834 struct mips_elf_link_hash_table *htab;
7835
7836 htab = mips_elf_hash_table (info);
7837 s = htab->sgot;
7838 if (s == NULL)
7839 return TRUE;
7840
7841 dynobj = elf_hash_table (info)->dynobj;
7842 g = htab->got_info;
7843
7844 /* Replace entries for indirect and warning symbols with entries for
7845 the target symbol. */
7846 if (!mips_elf_resolve_final_got_entries (g))
7847 return FALSE;
7848
7849 /* Count the number of GOT symbols. */
7850 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, g);
7851
7852 /* Calculate the total loadable size of the output. That
7853 will give us the maximum number of GOT_PAGE entries
7854 required. */
7855 for (sub = info->input_bfds; sub; sub = sub->link_next)
7856 {
7857 asection *subsection;
7858
7859 for (subsection = sub->sections;
7860 subsection;
7861 subsection = subsection->next)
7862 {
7863 if ((subsection->flags & SEC_ALLOC) == 0)
7864 continue;
7865 loadable_size += ((subsection->size + 0xf)
7866 &~ (bfd_size_type) 0xf);
7867 }
7868 }
7869
7870 if (htab->is_vxworks)
7871 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
7872 relocations against local symbols evaluate to "G", and the EABI does
7873 not include R_MIPS_GOT_PAGE. */
7874 page_gotno = 0;
7875 else
7876 /* Assume there are two loadable segments consisting of contiguous
7877 sections. Is 5 enough? */
7878 page_gotno = (loadable_size >> 16) + 5;
7879
7880 /* Choose the smaller of the two estimates; both are intended to be
7881 conservative. */
7882 if (page_gotno > g->page_gotno)
7883 page_gotno = g->page_gotno;
7884
7885 g->local_gotno += page_gotno;
7886 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
7887 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
7888
7889 /* We need to calculate tls_gotno for global symbols at this point
7890 instead of building it up earlier, to avoid doublecounting
7891 entries for one global symbol from multiple input files. */
7892 count_tls_arg.info = info;
7893 count_tls_arg.needed = 0;
7894 elf_link_hash_traverse (elf_hash_table (info),
7895 mips_elf_count_global_tls_entries,
7896 &count_tls_arg);
7897 g->tls_gotno += count_tls_arg.needed;
7898 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
7899
7900 /* VxWorks does not support multiple GOTs. It initializes $gp to
7901 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
7902 dynamic loader. */
7903 if (htab->is_vxworks)
7904 {
7905 /* VxWorks executables do not need a GOT. */
7906 if (info->shared)
7907 {
7908 /* Each VxWorks GOT entry needs an explicit relocation. */
7909 unsigned int count;
7910
7911 count = g->global_gotno + g->local_gotno - MIPS_RESERVED_GOTNO (info);
7912 if (count)
7913 mips_elf_allocate_dynamic_relocations (dynobj, info, count);
7914 }
7915 }
7916 else if (s->size > MIPS_ELF_GOT_MAX_SIZE (info))
7917 {
7918 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
7919 return FALSE;
7920 }
7921 else
7922 {
7923 struct mips_elf_count_tls_arg arg;
7924
7925 /* Set up TLS entries. */
7926 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
7927 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
7928
7929 /* Allocate room for the TLS relocations. */
7930 arg.info = info;
7931 arg.needed = 0;
7932 htab_traverse (g->got_entries, mips_elf_count_local_tls_relocs, &arg);
7933 elf_link_hash_traverse (elf_hash_table (info),
7934 mips_elf_count_global_tls_relocs,
7935 &arg);
7936 if (arg.needed)
7937 mips_elf_allocate_dynamic_relocations (dynobj, info, arg.needed);
7938 }
7939
7940 return TRUE;
7941 }
7942
7943 /* Estimate the size of the .MIPS.stubs section. */
7944
7945 static void
7946 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
7947 {
7948 struct mips_elf_link_hash_table *htab;
7949 bfd_size_type dynsymcount;
7950
7951 htab = mips_elf_hash_table (info);
7952 if (htab->lazy_stub_count == 0)
7953 return;
7954
7955 /* IRIX rld assumes that a function stub isn't at the end of the .text
7956 section, so add a dummy entry to the end. */
7957 htab->lazy_stub_count++;
7958
7959 /* Get a worst-case estimate of the number of dynamic symbols needed.
7960 At this point, dynsymcount does not account for section symbols
7961 and count_section_dynsyms may overestimate the number that will
7962 be needed. */
7963 dynsymcount = (elf_hash_table (info)->dynsymcount
7964 + count_section_dynsyms (output_bfd, info));
7965
7966 /* Determine the size of one stub entry. */
7967 htab->function_stub_size = (dynsymcount > 0x10000
7968 ? MIPS_FUNCTION_STUB_BIG_SIZE
7969 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
7970
7971 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
7972 }
7973
7974 /* A mips_elf_link_hash_traverse callback for which DATA points to the
7975 MIPS hash table. If H needs a traditional MIPS lazy-binding stub,
7976 allocate an entry in the stubs section. */
7977
7978 static bfd_boolean
7979 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void **data)
7980 {
7981 struct mips_elf_link_hash_table *htab;
7982
7983 htab = (struct mips_elf_link_hash_table *) data;
7984 if (h->needs_lazy_stub)
7985 {
7986 h->root.root.u.def.section = htab->sstubs;
7987 h->root.root.u.def.value = htab->sstubs->size;
7988 h->root.plt.offset = htab->sstubs->size;
7989 htab->sstubs->size += htab->function_stub_size;
7990 }
7991 return TRUE;
7992 }
7993
7994 /* Allocate offsets in the stubs section to each symbol that needs one.
7995 Set the final size of the .MIPS.stub section. */
7996
7997 static void
7998 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
7999 {
8000 struct mips_elf_link_hash_table *htab;
8001
8002 htab = mips_elf_hash_table (info);
8003 if (htab->lazy_stub_count == 0)
8004 return;
8005
8006 htab->sstubs->size = 0;
8007 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8008 mips_elf_allocate_lazy_stub, htab);
8009 htab->sstubs->size += htab->function_stub_size;
8010 BFD_ASSERT (htab->sstubs->size
8011 == htab->lazy_stub_count * htab->function_stub_size);
8012 }
8013
8014 /* Set the sizes of the dynamic sections. */
8015
8016 bfd_boolean
8017 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
8018 struct bfd_link_info *info)
8019 {
8020 bfd *dynobj;
8021 asection *s;
8022 bfd_boolean reltext;
8023 struct mips_elf_link_hash_table *htab;
8024
8025 htab = mips_elf_hash_table (info);
8026 dynobj = elf_hash_table (info)->dynobj;
8027 BFD_ASSERT (dynobj != NULL);
8028
8029 if (elf_hash_table (info)->dynamic_sections_created)
8030 {
8031 /* Set the contents of the .interp section to the interpreter. */
8032 if (info->executable)
8033 {
8034 s = bfd_get_section_by_name (dynobj, ".interp");
8035 BFD_ASSERT (s != NULL);
8036 s->size
8037 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
8038 s->contents
8039 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
8040 }
8041 }
8042
8043 /* Allocate space for global sym dynamic relocs. */
8044 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
8045
8046 mips_elf_estimate_stub_size (output_bfd, info);
8047
8048 if (!mips_elf_lay_out_got (output_bfd, info))
8049 return FALSE;
8050
8051 mips_elf_lay_out_lazy_stubs (info);
8052
8053 /* The check_relocs and adjust_dynamic_symbol entry points have
8054 determined the sizes of the various dynamic sections. Allocate
8055 memory for them. */
8056 reltext = FALSE;
8057 for (s = dynobj->sections; s != NULL; s = s->next)
8058 {
8059 const char *name;
8060
8061 /* It's OK to base decisions on the section name, because none
8062 of the dynobj section names depend upon the input files. */
8063 name = bfd_get_section_name (dynobj, s);
8064
8065 if ((s->flags & SEC_LINKER_CREATED) == 0)
8066 continue;
8067
8068 if (CONST_STRNEQ (name, ".rel"))
8069 {
8070 if (s->size != 0)
8071 {
8072 const char *outname;
8073 asection *target;
8074
8075 /* If this relocation section applies to a read only
8076 section, then we probably need a DT_TEXTREL entry.
8077 If the relocation section is .rel(a).dyn, we always
8078 assert a DT_TEXTREL entry rather than testing whether
8079 there exists a relocation to a read only section or
8080 not. */
8081 outname = bfd_get_section_name (output_bfd,
8082 s->output_section);
8083 target = bfd_get_section_by_name (output_bfd, outname + 4);
8084 if ((target != NULL
8085 && (target->flags & SEC_READONLY) != 0
8086 && (target->flags & SEC_ALLOC) != 0)
8087 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
8088 reltext = TRUE;
8089
8090 /* We use the reloc_count field as a counter if we need
8091 to copy relocs into the output file. */
8092 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
8093 s->reloc_count = 0;
8094
8095 /* If combreloc is enabled, elf_link_sort_relocs() will
8096 sort relocations, but in a different way than we do,
8097 and before we're done creating relocations. Also, it
8098 will move them around between input sections'
8099 relocation's contents, so our sorting would be
8100 broken, so don't let it run. */
8101 info->combreloc = 0;
8102 }
8103 }
8104 else if (! info->shared
8105 && ! mips_elf_hash_table (info)->use_rld_obj_head
8106 && CONST_STRNEQ (name, ".rld_map"))
8107 {
8108 /* We add a room for __rld_map. It will be filled in by the
8109 rtld to contain a pointer to the _r_debug structure. */
8110 s->size += 4;
8111 }
8112 else if (SGI_COMPAT (output_bfd)
8113 && CONST_STRNEQ (name, ".compact_rel"))
8114 s->size += mips_elf_hash_table (info)->compact_rel_size;
8115 else if (! CONST_STRNEQ (name, ".init")
8116 && s != htab->sgot
8117 && s != htab->sgotplt
8118 && s != htab->splt
8119 && s != htab->sstubs)
8120 {
8121 /* It's not one of our sections, so don't allocate space. */
8122 continue;
8123 }
8124
8125 if (s->size == 0)
8126 {
8127 s->flags |= SEC_EXCLUDE;
8128 continue;
8129 }
8130
8131 if ((s->flags & SEC_HAS_CONTENTS) == 0)
8132 continue;
8133
8134 /* Allocate memory for the section contents. */
8135 s->contents = bfd_zalloc (dynobj, s->size);
8136 if (s->contents == NULL)
8137 {
8138 bfd_set_error (bfd_error_no_memory);
8139 return FALSE;
8140 }
8141 }
8142
8143 if (elf_hash_table (info)->dynamic_sections_created)
8144 {
8145 /* Add some entries to the .dynamic section. We fill in the
8146 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
8147 must add the entries now so that we get the correct size for
8148 the .dynamic section. */
8149
8150 /* SGI object has the equivalence of DT_DEBUG in the
8151 DT_MIPS_RLD_MAP entry. This must come first because glibc
8152 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and GDB only
8153 looks at the first one it sees. */
8154 if (!info->shared
8155 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
8156 return FALSE;
8157
8158 /* The DT_DEBUG entry may be filled in by the dynamic linker and
8159 used by the debugger. */
8160 if (info->executable
8161 && !SGI_COMPAT (output_bfd)
8162 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
8163 return FALSE;
8164
8165 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
8166 info->flags |= DF_TEXTREL;
8167
8168 if ((info->flags & DF_TEXTREL) != 0)
8169 {
8170 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
8171 return FALSE;
8172
8173 /* Clear the DF_TEXTREL flag. It will be set again if we
8174 write out an actual text relocation; we may not, because
8175 at this point we do not know whether e.g. any .eh_frame
8176 absolute relocations have been converted to PC-relative. */
8177 info->flags &= ~DF_TEXTREL;
8178 }
8179
8180 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
8181 return FALSE;
8182
8183 if (htab->is_vxworks)
8184 {
8185 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
8186 use any of the DT_MIPS_* tags. */
8187 if (mips_elf_rel_dyn_section (info, FALSE))
8188 {
8189 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
8190 return FALSE;
8191
8192 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
8193 return FALSE;
8194
8195 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
8196 return FALSE;
8197 }
8198 if (htab->splt->size > 0)
8199 {
8200 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
8201 return FALSE;
8202
8203 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
8204 return FALSE;
8205
8206 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
8207 return FALSE;
8208 }
8209 }
8210 else
8211 {
8212 if (mips_elf_rel_dyn_section (info, FALSE))
8213 {
8214 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
8215 return FALSE;
8216
8217 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
8218 return FALSE;
8219
8220 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
8221 return FALSE;
8222 }
8223
8224 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
8225 return FALSE;
8226
8227 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
8228 return FALSE;
8229
8230 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
8231 return FALSE;
8232
8233 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
8234 return FALSE;
8235
8236 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
8237 return FALSE;
8238
8239 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
8240 return FALSE;
8241
8242 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
8243 return FALSE;
8244
8245 if (IRIX_COMPAT (dynobj) == ict_irix5
8246 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
8247 return FALSE;
8248
8249 if (IRIX_COMPAT (dynobj) == ict_irix6
8250 && (bfd_get_section_by_name
8251 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
8252 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
8253 return FALSE;
8254 }
8255 if (htab->is_vxworks
8256 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
8257 return FALSE;
8258 }
8259
8260 return TRUE;
8261 }
8262 \f
8263 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
8264 Adjust its R_ADDEND field so that it is correct for the output file.
8265 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
8266 and sections respectively; both use symbol indexes. */
8267
8268 static void
8269 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
8270 bfd *input_bfd, Elf_Internal_Sym *local_syms,
8271 asection **local_sections, Elf_Internal_Rela *rel)
8272 {
8273 unsigned int r_type, r_symndx;
8274 Elf_Internal_Sym *sym;
8275 asection *sec;
8276
8277 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
8278 {
8279 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8280 if (r_type == R_MIPS16_GPREL
8281 || r_type == R_MIPS_GPREL16
8282 || r_type == R_MIPS_GPREL32
8283 || r_type == R_MIPS_LITERAL)
8284 {
8285 rel->r_addend += _bfd_get_gp_value (input_bfd);
8286 rel->r_addend -= _bfd_get_gp_value (output_bfd);
8287 }
8288
8289 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
8290 sym = local_syms + r_symndx;
8291
8292 /* Adjust REL's addend to account for section merging. */
8293 if (!info->relocatable)
8294 {
8295 sec = local_sections[r_symndx];
8296 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
8297 }
8298
8299 /* This would normally be done by the rela_normal code in elflink.c. */
8300 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8301 rel->r_addend += local_sections[r_symndx]->output_offset;
8302 }
8303 }
8304
8305 /* Relocate a MIPS ELF section. */
8306
8307 bfd_boolean
8308 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
8309 bfd *input_bfd, asection *input_section,
8310 bfd_byte *contents, Elf_Internal_Rela *relocs,
8311 Elf_Internal_Sym *local_syms,
8312 asection **local_sections)
8313 {
8314 Elf_Internal_Rela *rel;
8315 const Elf_Internal_Rela *relend;
8316 bfd_vma addend = 0;
8317 bfd_boolean use_saved_addend_p = FALSE;
8318 const struct elf_backend_data *bed;
8319
8320 bed = get_elf_backend_data (output_bfd);
8321 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
8322 for (rel = relocs; rel < relend; ++rel)
8323 {
8324 const char *name;
8325 bfd_vma value = 0;
8326 reloc_howto_type *howto;
8327 bfd_boolean require_jalx;
8328 /* TRUE if the relocation is a RELA relocation, rather than a
8329 REL relocation. */
8330 bfd_boolean rela_relocation_p = TRUE;
8331 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8332 const char *msg;
8333 unsigned long r_symndx;
8334 asection *sec;
8335 Elf_Internal_Shdr *symtab_hdr;
8336 struct elf_link_hash_entry *h;
8337
8338 /* Find the relocation howto for this relocation. */
8339 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type,
8340 NEWABI_P (input_bfd)
8341 && (MIPS_RELOC_RELA_P
8342 (input_bfd, input_section,
8343 rel - relocs)));
8344
8345 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
8346 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8347 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
8348 {
8349 sec = local_sections[r_symndx];
8350 h = NULL;
8351 }
8352 else
8353 {
8354 unsigned long extsymoff;
8355
8356 extsymoff = 0;
8357 if (!elf_bad_symtab (input_bfd))
8358 extsymoff = symtab_hdr->sh_info;
8359 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
8360 while (h->root.type == bfd_link_hash_indirect
8361 || h->root.type == bfd_link_hash_warning)
8362 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8363
8364 sec = NULL;
8365 if (h->root.type == bfd_link_hash_defined
8366 || h->root.type == bfd_link_hash_defweak)
8367 sec = h->root.u.def.section;
8368 }
8369
8370 if (sec != NULL && elf_discarded_section (sec))
8371 {
8372 /* For relocs against symbols from removed linkonce sections,
8373 or sections discarded by a linker script, we just want the
8374 section contents zeroed. Avoid any special processing. */
8375 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
8376 rel->r_info = 0;
8377 rel->r_addend = 0;
8378 continue;
8379 }
8380
8381 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
8382 {
8383 /* Some 32-bit code uses R_MIPS_64. In particular, people use
8384 64-bit code, but make sure all their addresses are in the
8385 lowermost or uppermost 32-bit section of the 64-bit address
8386 space. Thus, when they use an R_MIPS_64 they mean what is
8387 usually meant by R_MIPS_32, with the exception that the
8388 stored value is sign-extended to 64 bits. */
8389 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
8390
8391 /* On big-endian systems, we need to lie about the position
8392 of the reloc. */
8393 if (bfd_big_endian (input_bfd))
8394 rel->r_offset += 4;
8395 }
8396
8397 if (!use_saved_addend_p)
8398 {
8399 /* If these relocations were originally of the REL variety,
8400 we must pull the addend out of the field that will be
8401 relocated. Otherwise, we simply use the contents of the
8402 RELA relocation. */
8403 if (mips_elf_rel_relocation_p (input_bfd, input_section,
8404 relocs, rel))
8405 {
8406 rela_relocation_p = FALSE;
8407 addend = mips_elf_read_rel_addend (input_bfd, rel,
8408 howto, contents);
8409 if (hi16_reloc_p (r_type)
8410 || (got16_reloc_p (r_type)
8411 && mips_elf_local_relocation_p (input_bfd, rel,
8412 local_sections, FALSE)))
8413 {
8414 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
8415 contents, &addend))
8416 {
8417 const char *name;
8418
8419 if (h)
8420 name = h->root.root.string;
8421 else
8422 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
8423 local_syms + r_symndx,
8424 sec);
8425 (*_bfd_error_handler)
8426 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
8427 input_bfd, input_section, name, howto->name,
8428 rel->r_offset);
8429 }
8430 }
8431 else
8432 addend <<= howto->rightshift;
8433 }
8434 else
8435 addend = rel->r_addend;
8436 mips_elf_adjust_addend (output_bfd, info, input_bfd,
8437 local_syms, local_sections, rel);
8438 }
8439
8440 if (info->relocatable)
8441 {
8442 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
8443 && bfd_big_endian (input_bfd))
8444 rel->r_offset -= 4;
8445
8446 if (!rela_relocation_p && rel->r_addend)
8447 {
8448 addend += rel->r_addend;
8449 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
8450 addend = mips_elf_high (addend);
8451 else if (r_type == R_MIPS_HIGHER)
8452 addend = mips_elf_higher (addend);
8453 else if (r_type == R_MIPS_HIGHEST)
8454 addend = mips_elf_highest (addend);
8455 else
8456 addend >>= howto->rightshift;
8457
8458 /* We use the source mask, rather than the destination
8459 mask because the place to which we are writing will be
8460 source of the addend in the final link. */
8461 addend &= howto->src_mask;
8462
8463 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
8464 /* See the comment above about using R_MIPS_64 in the 32-bit
8465 ABI. Here, we need to update the addend. It would be
8466 possible to get away with just using the R_MIPS_32 reloc
8467 but for endianness. */
8468 {
8469 bfd_vma sign_bits;
8470 bfd_vma low_bits;
8471 bfd_vma high_bits;
8472
8473 if (addend & ((bfd_vma) 1 << 31))
8474 #ifdef BFD64
8475 sign_bits = ((bfd_vma) 1 << 32) - 1;
8476 #else
8477 sign_bits = -1;
8478 #endif
8479 else
8480 sign_bits = 0;
8481
8482 /* If we don't know that we have a 64-bit type,
8483 do two separate stores. */
8484 if (bfd_big_endian (input_bfd))
8485 {
8486 /* Store the sign-bits (which are most significant)
8487 first. */
8488 low_bits = sign_bits;
8489 high_bits = addend;
8490 }
8491 else
8492 {
8493 low_bits = addend;
8494 high_bits = sign_bits;
8495 }
8496 bfd_put_32 (input_bfd, low_bits,
8497 contents + rel->r_offset);
8498 bfd_put_32 (input_bfd, high_bits,
8499 contents + rel->r_offset + 4);
8500 continue;
8501 }
8502
8503 if (! mips_elf_perform_relocation (info, howto, rel, addend,
8504 input_bfd, input_section,
8505 contents, FALSE))
8506 return FALSE;
8507 }
8508
8509 /* Go on to the next relocation. */
8510 continue;
8511 }
8512
8513 /* In the N32 and 64-bit ABIs there may be multiple consecutive
8514 relocations for the same offset. In that case we are
8515 supposed to treat the output of each relocation as the addend
8516 for the next. */
8517 if (rel + 1 < relend
8518 && rel->r_offset == rel[1].r_offset
8519 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
8520 use_saved_addend_p = TRUE;
8521 else
8522 use_saved_addend_p = FALSE;
8523
8524 /* Figure out what value we are supposed to relocate. */
8525 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
8526 input_section, info, rel,
8527 addend, howto, local_syms,
8528 local_sections, &value,
8529 &name, &require_jalx,
8530 use_saved_addend_p))
8531 {
8532 case bfd_reloc_continue:
8533 /* There's nothing to do. */
8534 continue;
8535
8536 case bfd_reloc_undefined:
8537 /* mips_elf_calculate_relocation already called the
8538 undefined_symbol callback. There's no real point in
8539 trying to perform the relocation at this point, so we
8540 just skip ahead to the next relocation. */
8541 continue;
8542
8543 case bfd_reloc_notsupported:
8544 msg = _("internal error: unsupported relocation error");
8545 info->callbacks->warning
8546 (info, msg, name, input_bfd, input_section, rel->r_offset);
8547 return FALSE;
8548
8549 case bfd_reloc_overflow:
8550 if (use_saved_addend_p)
8551 /* Ignore overflow until we reach the last relocation for
8552 a given location. */
8553 ;
8554 else
8555 {
8556 struct mips_elf_link_hash_table *htab;
8557
8558 htab = mips_elf_hash_table (info);
8559 BFD_ASSERT (name != NULL);
8560 if (!htab->small_data_overflow_reported
8561 && (howto->type == R_MIPS_GPREL16
8562 || howto->type == R_MIPS_LITERAL))
8563 {
8564 const char *msg =
8565 _("small-data section exceeds 64KB;"
8566 " lower small-data size limit (see option -G)");
8567
8568 htab->small_data_overflow_reported = TRUE;
8569 (*info->callbacks->einfo) ("%P: %s\n", msg);
8570 }
8571 if (! ((*info->callbacks->reloc_overflow)
8572 (info, NULL, name, howto->name, (bfd_vma) 0,
8573 input_bfd, input_section, rel->r_offset)))
8574 return FALSE;
8575 }
8576 break;
8577
8578 case bfd_reloc_ok:
8579 break;
8580
8581 default:
8582 abort ();
8583 break;
8584 }
8585
8586 /* If we've got another relocation for the address, keep going
8587 until we reach the last one. */
8588 if (use_saved_addend_p)
8589 {
8590 addend = value;
8591 continue;
8592 }
8593
8594 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
8595 /* See the comment above about using R_MIPS_64 in the 32-bit
8596 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
8597 that calculated the right value. Now, however, we
8598 sign-extend the 32-bit result to 64-bits, and store it as a
8599 64-bit value. We are especially generous here in that we
8600 go to extreme lengths to support this usage on systems with
8601 only a 32-bit VMA. */
8602 {
8603 bfd_vma sign_bits;
8604 bfd_vma low_bits;
8605 bfd_vma high_bits;
8606
8607 if (value & ((bfd_vma) 1 << 31))
8608 #ifdef BFD64
8609 sign_bits = ((bfd_vma) 1 << 32) - 1;
8610 #else
8611 sign_bits = -1;
8612 #endif
8613 else
8614 sign_bits = 0;
8615
8616 /* If we don't know that we have a 64-bit type,
8617 do two separate stores. */
8618 if (bfd_big_endian (input_bfd))
8619 {
8620 /* Undo what we did above. */
8621 rel->r_offset -= 4;
8622 /* Store the sign-bits (which are most significant)
8623 first. */
8624 low_bits = sign_bits;
8625 high_bits = value;
8626 }
8627 else
8628 {
8629 low_bits = value;
8630 high_bits = sign_bits;
8631 }
8632 bfd_put_32 (input_bfd, low_bits,
8633 contents + rel->r_offset);
8634 bfd_put_32 (input_bfd, high_bits,
8635 contents + rel->r_offset + 4);
8636 continue;
8637 }
8638
8639 /* Actually perform the relocation. */
8640 if (! mips_elf_perform_relocation (info, howto, rel, value,
8641 input_bfd, input_section,
8642 contents, require_jalx))
8643 return FALSE;
8644 }
8645
8646 return TRUE;
8647 }
8648 \f
8649 /* If NAME is one of the special IRIX6 symbols defined by the linker,
8650 adjust it appropriately now. */
8651
8652 static void
8653 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
8654 const char *name, Elf_Internal_Sym *sym)
8655 {
8656 /* The linker script takes care of providing names and values for
8657 these, but we must place them into the right sections. */
8658 static const char* const text_section_symbols[] = {
8659 "_ftext",
8660 "_etext",
8661 "__dso_displacement",
8662 "__elf_header",
8663 "__program_header_table",
8664 NULL
8665 };
8666
8667 static const char* const data_section_symbols[] = {
8668 "_fdata",
8669 "_edata",
8670 "_end",
8671 "_fbss",
8672 NULL
8673 };
8674
8675 const char* const *p;
8676 int i;
8677
8678 for (i = 0; i < 2; ++i)
8679 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
8680 *p;
8681 ++p)
8682 if (strcmp (*p, name) == 0)
8683 {
8684 /* All of these symbols are given type STT_SECTION by the
8685 IRIX6 linker. */
8686 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8687 sym->st_other = STO_PROTECTED;
8688
8689 /* The IRIX linker puts these symbols in special sections. */
8690 if (i == 0)
8691 sym->st_shndx = SHN_MIPS_TEXT;
8692 else
8693 sym->st_shndx = SHN_MIPS_DATA;
8694
8695 break;
8696 }
8697 }
8698
8699 /* Finish up dynamic symbol handling. We set the contents of various
8700 dynamic sections here. */
8701
8702 bfd_boolean
8703 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
8704 struct bfd_link_info *info,
8705 struct elf_link_hash_entry *h,
8706 Elf_Internal_Sym *sym)
8707 {
8708 bfd *dynobj;
8709 asection *sgot;
8710 struct mips_got_info *g, *gg;
8711 const char *name;
8712 int idx;
8713 struct mips_elf_link_hash_table *htab;
8714 struct mips_elf_link_hash_entry *hmips;
8715
8716 htab = mips_elf_hash_table (info);
8717 dynobj = elf_hash_table (info)->dynobj;
8718 hmips = (struct mips_elf_link_hash_entry *) h;
8719
8720 if (h->plt.offset != MINUS_ONE)
8721 {
8722 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
8723
8724 /* This symbol has a stub. Set it up. */
8725
8726 BFD_ASSERT (h->dynindx != -1);
8727
8728 BFD_ASSERT ((htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
8729 || (h->dynindx <= 0xffff));
8730
8731 /* Values up to 2^31 - 1 are allowed. Larger values would cause
8732 sign extension at runtime in the stub, resulting in a negative
8733 index value. */
8734 if (h->dynindx & ~0x7fffffff)
8735 return FALSE;
8736
8737 /* Fill the stub. */
8738 idx = 0;
8739 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
8740 idx += 4;
8741 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
8742 idx += 4;
8743 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
8744 {
8745 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
8746 stub + idx);
8747 idx += 4;
8748 }
8749 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
8750 idx += 4;
8751
8752 /* If a large stub is not required and sign extension is not a
8753 problem, then use legacy code in the stub. */
8754 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
8755 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff), stub + idx);
8756 else if (h->dynindx & ~0x7fff)
8757 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff), stub + idx);
8758 else
8759 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
8760 stub + idx);
8761
8762 BFD_ASSERT (h->plt.offset <= htab->sstubs->size);
8763 memcpy (htab->sstubs->contents + h->plt.offset,
8764 stub, htab->function_stub_size);
8765
8766 /* Mark the symbol as undefined. plt.offset != -1 occurs
8767 only for the referenced symbol. */
8768 sym->st_shndx = SHN_UNDEF;
8769
8770 /* The run-time linker uses the st_value field of the symbol
8771 to reset the global offset table entry for this external
8772 to its stub address when unlinking a shared object. */
8773 sym->st_value = (htab->sstubs->output_section->vma
8774 + htab->sstubs->output_offset
8775 + h->plt.offset);
8776 }
8777
8778 /* If we have a MIPS16 function with a stub, the dynamic symbol must
8779 refer to the stub, since only the stub uses the standard calling
8780 conventions. */
8781 if (h->dynindx != -1 && hmips->fn_stub != NULL)
8782 {
8783 BFD_ASSERT (hmips->need_fn_stub);
8784 sym->st_value = (hmips->fn_stub->output_section->vma
8785 + hmips->fn_stub->output_offset);
8786 sym->st_size = hmips->fn_stub->size;
8787 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
8788 }
8789
8790 BFD_ASSERT (h->dynindx != -1
8791 || h->forced_local);
8792
8793 sgot = htab->sgot;
8794 g = htab->got_info;
8795 BFD_ASSERT (g != NULL);
8796
8797 /* Run through the global symbol table, creating GOT entries for all
8798 the symbols that need them. */
8799 if (g->global_gotsym != NULL
8800 && h->dynindx >= g->global_gotsym->dynindx)
8801 {
8802 bfd_vma offset;
8803 bfd_vma value;
8804
8805 value = sym->st_value;
8806 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
8807 R_MIPS_GOT16, info);
8808 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
8809 }
8810
8811 if (g->next && h->dynindx != -1 && h->type != STT_TLS)
8812 {
8813 struct mips_got_entry e, *p;
8814 bfd_vma entry;
8815 bfd_vma offset;
8816
8817 gg = g;
8818
8819 e.abfd = output_bfd;
8820 e.symndx = -1;
8821 e.d.h = hmips;
8822 e.tls_type = 0;
8823
8824 for (g = g->next; g->next != gg; g = g->next)
8825 {
8826 if (g->got_entries
8827 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
8828 &e)))
8829 {
8830 offset = p->gotidx;
8831 if (info->shared
8832 || (elf_hash_table (info)->dynamic_sections_created
8833 && p->d.h != NULL
8834 && p->d.h->root.def_dynamic
8835 && !p->d.h->root.def_regular))
8836 {
8837 /* Create an R_MIPS_REL32 relocation for this entry. Due to
8838 the various compatibility problems, it's easier to mock
8839 up an R_MIPS_32 or R_MIPS_64 relocation and leave
8840 mips_elf_create_dynamic_relocation to calculate the
8841 appropriate addend. */
8842 Elf_Internal_Rela rel[3];
8843
8844 memset (rel, 0, sizeof (rel));
8845 if (ABI_64_P (output_bfd))
8846 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
8847 else
8848 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
8849 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
8850
8851 entry = 0;
8852 if (! (mips_elf_create_dynamic_relocation
8853 (output_bfd, info, rel,
8854 e.d.h, NULL, sym->st_value, &entry, sgot)))
8855 return FALSE;
8856 }
8857 else
8858 entry = sym->st_value;
8859 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
8860 }
8861 }
8862 }
8863
8864 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
8865 name = h->root.root.string;
8866 if (strcmp (name, "_DYNAMIC") == 0
8867 || h == elf_hash_table (info)->hgot)
8868 sym->st_shndx = SHN_ABS;
8869 else if (strcmp (name, "_DYNAMIC_LINK") == 0
8870 || strcmp (name, "_DYNAMIC_LINKING") == 0)
8871 {
8872 sym->st_shndx = SHN_ABS;
8873 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8874 sym->st_value = 1;
8875 }
8876 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
8877 {
8878 sym->st_shndx = SHN_ABS;
8879 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8880 sym->st_value = elf_gp (output_bfd);
8881 }
8882 else if (SGI_COMPAT (output_bfd))
8883 {
8884 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
8885 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
8886 {
8887 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8888 sym->st_other = STO_PROTECTED;
8889 sym->st_value = 0;
8890 sym->st_shndx = SHN_MIPS_DATA;
8891 }
8892 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
8893 {
8894 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8895 sym->st_other = STO_PROTECTED;
8896 sym->st_value = mips_elf_hash_table (info)->procedure_count;
8897 sym->st_shndx = SHN_ABS;
8898 }
8899 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
8900 {
8901 if (h->type == STT_FUNC)
8902 sym->st_shndx = SHN_MIPS_TEXT;
8903 else if (h->type == STT_OBJECT)
8904 sym->st_shndx = SHN_MIPS_DATA;
8905 }
8906 }
8907
8908 /* Handle the IRIX6-specific symbols. */
8909 if (IRIX_COMPAT (output_bfd) == ict_irix6)
8910 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
8911
8912 if (! info->shared)
8913 {
8914 if (! mips_elf_hash_table (info)->use_rld_obj_head
8915 && (strcmp (name, "__rld_map") == 0
8916 || strcmp (name, "__RLD_MAP") == 0))
8917 {
8918 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
8919 BFD_ASSERT (s != NULL);
8920 sym->st_value = s->output_section->vma + s->output_offset;
8921 bfd_put_32 (output_bfd, 0, s->contents);
8922 if (mips_elf_hash_table (info)->rld_value == 0)
8923 mips_elf_hash_table (info)->rld_value = sym->st_value;
8924 }
8925 else if (mips_elf_hash_table (info)->use_rld_obj_head
8926 && strcmp (name, "__rld_obj_head") == 0)
8927 {
8928 /* IRIX6 does not use a .rld_map section. */
8929 if (IRIX_COMPAT (output_bfd) == ict_irix5
8930 || IRIX_COMPAT (output_bfd) == ict_none)
8931 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
8932 != NULL);
8933 mips_elf_hash_table (info)->rld_value = sym->st_value;
8934 }
8935 }
8936
8937 /* Keep dynamic MIPS16 symbols odd. This allows the dynamic linker to
8938 treat MIPS16 symbols like any other. */
8939 if (ELF_ST_IS_MIPS16 (sym->st_other))
8940 {
8941 BFD_ASSERT (sym->st_value & 1);
8942 sym->st_other -= STO_MIPS16;
8943 }
8944
8945 return TRUE;
8946 }
8947
8948 /* Likewise, for VxWorks. */
8949
8950 bfd_boolean
8951 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
8952 struct bfd_link_info *info,
8953 struct elf_link_hash_entry *h,
8954 Elf_Internal_Sym *sym)
8955 {
8956 bfd *dynobj;
8957 asection *sgot;
8958 struct mips_got_info *g;
8959 struct mips_elf_link_hash_table *htab;
8960
8961 htab = mips_elf_hash_table (info);
8962 dynobj = elf_hash_table (info)->dynobj;
8963
8964 if (h->plt.offset != (bfd_vma) -1)
8965 {
8966 bfd_byte *loc;
8967 bfd_vma plt_address, plt_index, got_address, got_offset, branch_offset;
8968 Elf_Internal_Rela rel;
8969 static const bfd_vma *plt_entry;
8970
8971 BFD_ASSERT (h->dynindx != -1);
8972 BFD_ASSERT (htab->splt != NULL);
8973 BFD_ASSERT (h->plt.offset <= htab->splt->size);
8974
8975 /* Calculate the address of the .plt entry. */
8976 plt_address = (htab->splt->output_section->vma
8977 + htab->splt->output_offset
8978 + h->plt.offset);
8979
8980 /* Calculate the index of the entry. */
8981 plt_index = ((h->plt.offset - htab->plt_header_size)
8982 / htab->plt_entry_size);
8983
8984 /* Calculate the address of the .got.plt entry. */
8985 got_address = (htab->sgotplt->output_section->vma
8986 + htab->sgotplt->output_offset
8987 + plt_index * 4);
8988
8989 /* Calculate the offset of the .got.plt entry from
8990 _GLOBAL_OFFSET_TABLE_. */
8991 got_offset = mips_elf_gotplt_index (info, h);
8992
8993 /* Calculate the offset for the branch at the start of the PLT
8994 entry. The branch jumps to the beginning of .plt. */
8995 branch_offset = -(h->plt.offset / 4 + 1) & 0xffff;
8996
8997 /* Fill in the initial value of the .got.plt entry. */
8998 bfd_put_32 (output_bfd, plt_address,
8999 htab->sgotplt->contents + plt_index * 4);
9000
9001 /* Find out where the .plt entry should go. */
9002 loc = htab->splt->contents + h->plt.offset;
9003
9004 if (info->shared)
9005 {
9006 plt_entry = mips_vxworks_shared_plt_entry;
9007 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9008 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9009 }
9010 else
9011 {
9012 bfd_vma got_address_high, got_address_low;
9013
9014 plt_entry = mips_vxworks_exec_plt_entry;
9015 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9016 got_address_low = got_address & 0xffff;
9017
9018 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9019 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9020 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
9021 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
9022 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9023 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9024 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9025 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9026
9027 loc = (htab->srelplt2->contents
9028 + (plt_index * 3 + 2) * sizeof (Elf32_External_Rela));
9029
9030 /* Emit a relocation for the .got.plt entry. */
9031 rel.r_offset = got_address;
9032 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9033 rel.r_addend = h->plt.offset;
9034 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9035
9036 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
9037 loc += sizeof (Elf32_External_Rela);
9038 rel.r_offset = plt_address + 8;
9039 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9040 rel.r_addend = got_offset;
9041 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9042
9043 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
9044 loc += sizeof (Elf32_External_Rela);
9045 rel.r_offset += 4;
9046 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9047 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9048 }
9049
9050 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9051 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
9052 rel.r_offset = got_address;
9053 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
9054 rel.r_addend = 0;
9055 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9056
9057 if (!h->def_regular)
9058 sym->st_shndx = SHN_UNDEF;
9059 }
9060
9061 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
9062
9063 sgot = htab->sgot;
9064 g = htab->got_info;
9065 BFD_ASSERT (g != NULL);
9066
9067 /* See if this symbol has an entry in the GOT. */
9068 if (g->global_gotsym != NULL
9069 && h->dynindx >= g->global_gotsym->dynindx)
9070 {
9071 bfd_vma offset;
9072 Elf_Internal_Rela outrel;
9073 bfd_byte *loc;
9074 asection *s;
9075
9076 /* Install the symbol value in the GOT. */
9077 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9078 R_MIPS_GOT16, info);
9079 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
9080
9081 /* Add a dynamic relocation for it. */
9082 s = mips_elf_rel_dyn_section (info, FALSE);
9083 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
9084 outrel.r_offset = (sgot->output_section->vma
9085 + sgot->output_offset
9086 + offset);
9087 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
9088 outrel.r_addend = 0;
9089 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
9090 }
9091
9092 /* Emit a copy reloc, if needed. */
9093 if (h->needs_copy)
9094 {
9095 Elf_Internal_Rela rel;
9096
9097 BFD_ASSERT (h->dynindx != -1);
9098
9099 rel.r_offset = (h->root.u.def.section->output_section->vma
9100 + h->root.u.def.section->output_offset
9101 + h->root.u.def.value);
9102 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
9103 rel.r_addend = 0;
9104 bfd_elf32_swap_reloca_out (output_bfd, &rel,
9105 htab->srelbss->contents
9106 + (htab->srelbss->reloc_count
9107 * sizeof (Elf32_External_Rela)));
9108 ++htab->srelbss->reloc_count;
9109 }
9110
9111 /* If this is a mips16 symbol, force the value to be even. */
9112 if (ELF_ST_IS_MIPS16 (sym->st_other))
9113 sym->st_value &= ~1;
9114
9115 return TRUE;
9116 }
9117
9118 /* Install the PLT header for a VxWorks executable and finalize the
9119 contents of .rela.plt.unloaded. */
9120
9121 static void
9122 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9123 {
9124 Elf_Internal_Rela rela;
9125 bfd_byte *loc;
9126 bfd_vma got_value, got_value_high, got_value_low, plt_address;
9127 static const bfd_vma *plt_entry;
9128 struct mips_elf_link_hash_table *htab;
9129
9130 htab = mips_elf_hash_table (info);
9131 plt_entry = mips_vxworks_exec_plt0_entry;
9132
9133 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
9134 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
9135 + htab->root.hgot->root.u.def.section->output_offset
9136 + htab->root.hgot->root.u.def.value);
9137
9138 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
9139 got_value_low = got_value & 0xffff;
9140
9141 /* Calculate the address of the PLT header. */
9142 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
9143
9144 /* Install the PLT header. */
9145 loc = htab->splt->contents;
9146 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
9147 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
9148 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
9149 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9150 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9151 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9152
9153 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
9154 loc = htab->srelplt2->contents;
9155 rela.r_offset = plt_address;
9156 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9157 rela.r_addend = 0;
9158 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
9159 loc += sizeof (Elf32_External_Rela);
9160
9161 /* Output the relocation for the following addiu of
9162 %lo(_GLOBAL_OFFSET_TABLE_). */
9163 rela.r_offset += 4;
9164 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9165 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
9166 loc += sizeof (Elf32_External_Rela);
9167
9168 /* Fix up the remaining relocations. They may have the wrong
9169 symbol index for _G_O_T_ or _P_L_T_ depending on the order
9170 in which symbols were output. */
9171 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
9172 {
9173 Elf_Internal_Rela rel;
9174
9175 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9176 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9177 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9178 loc += sizeof (Elf32_External_Rela);
9179
9180 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9181 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9182 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9183 loc += sizeof (Elf32_External_Rela);
9184
9185 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9186 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9187 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9188 loc += sizeof (Elf32_External_Rela);
9189 }
9190 }
9191
9192 /* Install the PLT header for a VxWorks shared library. */
9193
9194 static void
9195 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
9196 {
9197 unsigned int i;
9198 struct mips_elf_link_hash_table *htab;
9199
9200 htab = mips_elf_hash_table (info);
9201
9202 /* We just need to copy the entry byte-by-byte. */
9203 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
9204 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
9205 htab->splt->contents + i * 4);
9206 }
9207
9208 /* Finish up the dynamic sections. */
9209
9210 bfd_boolean
9211 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
9212 struct bfd_link_info *info)
9213 {
9214 bfd *dynobj;
9215 asection *sdyn;
9216 asection *sgot;
9217 struct mips_got_info *gg, *g;
9218 struct mips_elf_link_hash_table *htab;
9219
9220 htab = mips_elf_hash_table (info);
9221 dynobj = elf_hash_table (info)->dynobj;
9222
9223 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
9224
9225 sgot = htab->sgot;
9226 gg = htab->got_info;
9227
9228 if (elf_hash_table (info)->dynamic_sections_created)
9229 {
9230 bfd_byte *b;
9231 int dyn_to_skip = 0, dyn_skipped = 0;
9232
9233 BFD_ASSERT (sdyn != NULL);
9234 BFD_ASSERT (gg != NULL);
9235
9236 g = mips_elf_got_for_ibfd (gg, output_bfd);
9237 BFD_ASSERT (g != NULL);
9238
9239 for (b = sdyn->contents;
9240 b < sdyn->contents + sdyn->size;
9241 b += MIPS_ELF_DYN_SIZE (dynobj))
9242 {
9243 Elf_Internal_Dyn dyn;
9244 const char *name;
9245 size_t elemsize;
9246 asection *s;
9247 bfd_boolean swap_out_p;
9248
9249 /* Read in the current dynamic entry. */
9250 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
9251
9252 /* Assume that we're going to modify it and write it out. */
9253 swap_out_p = TRUE;
9254
9255 switch (dyn.d_tag)
9256 {
9257 case DT_RELENT:
9258 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
9259 break;
9260
9261 case DT_RELAENT:
9262 BFD_ASSERT (htab->is_vxworks);
9263 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
9264 break;
9265
9266 case DT_STRSZ:
9267 /* Rewrite DT_STRSZ. */
9268 dyn.d_un.d_val =
9269 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
9270 break;
9271
9272 case DT_PLTGOT:
9273 name = ".got";
9274 if (htab->is_vxworks)
9275 {
9276 /* _GLOBAL_OFFSET_TABLE_ is defined to be the beginning
9277 of the ".got" section in DYNOBJ. */
9278 s = bfd_get_section_by_name (dynobj, name);
9279 BFD_ASSERT (s != NULL);
9280 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
9281 }
9282 else
9283 {
9284 s = bfd_get_section_by_name (output_bfd, name);
9285 BFD_ASSERT (s != NULL);
9286 dyn.d_un.d_ptr = s->vma;
9287 }
9288 break;
9289
9290 case DT_MIPS_RLD_VERSION:
9291 dyn.d_un.d_val = 1; /* XXX */
9292 break;
9293
9294 case DT_MIPS_FLAGS:
9295 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
9296 break;
9297
9298 case DT_MIPS_TIME_STAMP:
9299 {
9300 time_t t;
9301 time (&t);
9302 dyn.d_un.d_val = t;
9303 }
9304 break;
9305
9306 case DT_MIPS_ICHECKSUM:
9307 /* XXX FIXME: */
9308 swap_out_p = FALSE;
9309 break;
9310
9311 case DT_MIPS_IVERSION:
9312 /* XXX FIXME: */
9313 swap_out_p = FALSE;
9314 break;
9315
9316 case DT_MIPS_BASE_ADDRESS:
9317 s = output_bfd->sections;
9318 BFD_ASSERT (s != NULL);
9319 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
9320 break;
9321
9322 case DT_MIPS_LOCAL_GOTNO:
9323 dyn.d_un.d_val = g->local_gotno;
9324 break;
9325
9326 case DT_MIPS_UNREFEXTNO:
9327 /* The index into the dynamic symbol table which is the
9328 entry of the first external symbol that is not
9329 referenced within the same object. */
9330 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
9331 break;
9332
9333 case DT_MIPS_GOTSYM:
9334 if (gg->global_gotsym)
9335 {
9336 dyn.d_un.d_val = gg->global_gotsym->dynindx;
9337 break;
9338 }
9339 /* In case if we don't have global got symbols we default
9340 to setting DT_MIPS_GOTSYM to the same value as
9341 DT_MIPS_SYMTABNO, so we just fall through. */
9342
9343 case DT_MIPS_SYMTABNO:
9344 name = ".dynsym";
9345 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
9346 s = bfd_get_section_by_name (output_bfd, name);
9347 BFD_ASSERT (s != NULL);
9348
9349 dyn.d_un.d_val = s->size / elemsize;
9350 break;
9351
9352 case DT_MIPS_HIPAGENO:
9353 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO (info);
9354 break;
9355
9356 case DT_MIPS_RLD_MAP:
9357 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
9358 break;
9359
9360 case DT_MIPS_OPTIONS:
9361 s = (bfd_get_section_by_name
9362 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
9363 dyn.d_un.d_ptr = s->vma;
9364 break;
9365
9366 case DT_RELASZ:
9367 BFD_ASSERT (htab->is_vxworks);
9368 /* The count does not include the JUMP_SLOT relocations. */
9369 if (htab->srelplt)
9370 dyn.d_un.d_val -= htab->srelplt->size;
9371 break;
9372
9373 case DT_PLTREL:
9374 BFD_ASSERT (htab->is_vxworks);
9375 dyn.d_un.d_val = DT_RELA;
9376 break;
9377
9378 case DT_PLTRELSZ:
9379 BFD_ASSERT (htab->is_vxworks);
9380 dyn.d_un.d_val = htab->srelplt->size;
9381 break;
9382
9383 case DT_JMPREL:
9384 BFD_ASSERT (htab->is_vxworks);
9385 dyn.d_un.d_val = (htab->srelplt->output_section->vma
9386 + htab->srelplt->output_offset);
9387 break;
9388
9389 case DT_TEXTREL:
9390 /* If we didn't need any text relocations after all, delete
9391 the dynamic tag. */
9392 if (!(info->flags & DF_TEXTREL))
9393 {
9394 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
9395 swap_out_p = FALSE;
9396 }
9397 break;
9398
9399 case DT_FLAGS:
9400 /* If we didn't need any text relocations after all, clear
9401 DF_TEXTREL from DT_FLAGS. */
9402 if (!(info->flags & DF_TEXTREL))
9403 dyn.d_un.d_val &= ~DF_TEXTREL;
9404 else
9405 swap_out_p = FALSE;
9406 break;
9407
9408 default:
9409 swap_out_p = FALSE;
9410 if (htab->is_vxworks
9411 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
9412 swap_out_p = TRUE;
9413 break;
9414 }
9415
9416 if (swap_out_p || dyn_skipped)
9417 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
9418 (dynobj, &dyn, b - dyn_skipped);
9419
9420 if (dyn_to_skip)
9421 {
9422 dyn_skipped += dyn_to_skip;
9423 dyn_to_skip = 0;
9424 }
9425 }
9426
9427 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
9428 if (dyn_skipped > 0)
9429 memset (b - dyn_skipped, 0, dyn_skipped);
9430 }
9431
9432 if (sgot != NULL && sgot->size > 0
9433 && !bfd_is_abs_section (sgot->output_section))
9434 {
9435 if (htab->is_vxworks)
9436 {
9437 /* The first entry of the global offset table points to the
9438 ".dynamic" section. The second is initialized by the
9439 loader and contains the shared library identifier.
9440 The third is also initialized by the loader and points
9441 to the lazy resolution stub. */
9442 MIPS_ELF_PUT_WORD (output_bfd,
9443 sdyn->output_offset + sdyn->output_section->vma,
9444 sgot->contents);
9445 MIPS_ELF_PUT_WORD (output_bfd, 0,
9446 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
9447 MIPS_ELF_PUT_WORD (output_bfd, 0,
9448 sgot->contents
9449 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
9450 }
9451 else
9452 {
9453 /* The first entry of the global offset table will be filled at
9454 runtime. The second entry will be used by some runtime loaders.
9455 This isn't the case of IRIX rld. */
9456 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
9457 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
9458 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
9459 }
9460
9461 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
9462 = MIPS_ELF_GOT_SIZE (output_bfd);
9463 }
9464
9465 /* Generate dynamic relocations for the non-primary gots. */
9466 if (gg != NULL && gg->next)
9467 {
9468 Elf_Internal_Rela rel[3];
9469 bfd_vma addend = 0;
9470
9471 memset (rel, 0, sizeof (rel));
9472 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
9473
9474 for (g = gg->next; g->next != gg; g = g->next)
9475 {
9476 bfd_vma index = g->next->local_gotno + g->next->global_gotno
9477 + g->next->tls_gotno;
9478
9479 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
9480 + index++ * MIPS_ELF_GOT_SIZE (output_bfd));
9481 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
9482 sgot->contents
9483 + index++ * MIPS_ELF_GOT_SIZE (output_bfd));
9484
9485 if (! info->shared)
9486 continue;
9487
9488 while (index < g->assigned_gotno)
9489 {
9490 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
9491 = index++ * MIPS_ELF_GOT_SIZE (output_bfd);
9492 if (!(mips_elf_create_dynamic_relocation
9493 (output_bfd, info, rel, NULL,
9494 bfd_abs_section_ptr,
9495 0, &addend, sgot)))
9496 return FALSE;
9497 BFD_ASSERT (addend == 0);
9498 }
9499 }
9500 }
9501
9502 /* The generation of dynamic relocations for the non-primary gots
9503 adds more dynamic relocations. We cannot count them until
9504 here. */
9505
9506 if (elf_hash_table (info)->dynamic_sections_created)
9507 {
9508 bfd_byte *b;
9509 bfd_boolean swap_out_p;
9510
9511 BFD_ASSERT (sdyn != NULL);
9512
9513 for (b = sdyn->contents;
9514 b < sdyn->contents + sdyn->size;
9515 b += MIPS_ELF_DYN_SIZE (dynobj))
9516 {
9517 Elf_Internal_Dyn dyn;
9518 asection *s;
9519
9520 /* Read in the current dynamic entry. */
9521 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
9522
9523 /* Assume that we're going to modify it and write it out. */
9524 swap_out_p = TRUE;
9525
9526 switch (dyn.d_tag)
9527 {
9528 case DT_RELSZ:
9529 /* Reduce DT_RELSZ to account for any relocations we
9530 decided not to make. This is for the n64 irix rld,
9531 which doesn't seem to apply any relocations if there
9532 are trailing null entries. */
9533 s = mips_elf_rel_dyn_section (info, FALSE);
9534 dyn.d_un.d_val = (s->reloc_count
9535 * (ABI_64_P (output_bfd)
9536 ? sizeof (Elf64_Mips_External_Rel)
9537 : sizeof (Elf32_External_Rel)));
9538 /* Adjust the section size too. Tools like the prelinker
9539 can reasonably expect the values to the same. */
9540 elf_section_data (s->output_section)->this_hdr.sh_size
9541 = dyn.d_un.d_val;
9542 break;
9543
9544 default:
9545 swap_out_p = FALSE;
9546 break;
9547 }
9548
9549 if (swap_out_p)
9550 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
9551 (dynobj, &dyn, b);
9552 }
9553 }
9554
9555 {
9556 asection *s;
9557 Elf32_compact_rel cpt;
9558
9559 if (SGI_COMPAT (output_bfd))
9560 {
9561 /* Write .compact_rel section out. */
9562 s = bfd_get_section_by_name (dynobj, ".compact_rel");
9563 if (s != NULL)
9564 {
9565 cpt.id1 = 1;
9566 cpt.num = s->reloc_count;
9567 cpt.id2 = 2;
9568 cpt.offset = (s->output_section->filepos
9569 + sizeof (Elf32_External_compact_rel));
9570 cpt.reserved0 = 0;
9571 cpt.reserved1 = 0;
9572 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
9573 ((Elf32_External_compact_rel *)
9574 s->contents));
9575
9576 /* Clean up a dummy stub function entry in .text. */
9577 if (htab->sstubs != NULL)
9578 {
9579 file_ptr dummy_offset;
9580
9581 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
9582 dummy_offset = htab->sstubs->size - htab->function_stub_size;
9583 memset (htab->sstubs->contents + dummy_offset, 0,
9584 htab->function_stub_size);
9585 }
9586 }
9587 }
9588
9589 /* The psABI says that the dynamic relocations must be sorted in
9590 increasing order of r_symndx. The VxWorks EABI doesn't require
9591 this, and because the code below handles REL rather than RELA
9592 relocations, using it for VxWorks would be outright harmful. */
9593 if (!htab->is_vxworks)
9594 {
9595 s = mips_elf_rel_dyn_section (info, FALSE);
9596 if (s != NULL
9597 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
9598 {
9599 reldyn_sorting_bfd = output_bfd;
9600
9601 if (ABI_64_P (output_bfd))
9602 qsort ((Elf64_External_Rel *) s->contents + 1,
9603 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
9604 sort_dynamic_relocs_64);
9605 else
9606 qsort ((Elf32_External_Rel *) s->contents + 1,
9607 s->reloc_count - 1, sizeof (Elf32_External_Rel),
9608 sort_dynamic_relocs);
9609 }
9610 }
9611 }
9612
9613 if (htab->is_vxworks && htab->splt->size > 0)
9614 {
9615 if (info->shared)
9616 mips_vxworks_finish_shared_plt (output_bfd, info);
9617 else
9618 mips_vxworks_finish_exec_plt (output_bfd, info);
9619 }
9620 return TRUE;
9621 }
9622
9623
9624 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
9625
9626 static void
9627 mips_set_isa_flags (bfd *abfd)
9628 {
9629 flagword val;
9630
9631 switch (bfd_get_mach (abfd))
9632 {
9633 default:
9634 case bfd_mach_mips3000:
9635 val = E_MIPS_ARCH_1;
9636 break;
9637
9638 case bfd_mach_mips3900:
9639 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
9640 break;
9641
9642 case bfd_mach_mips6000:
9643 val = E_MIPS_ARCH_2;
9644 break;
9645
9646 case bfd_mach_mips4000:
9647 case bfd_mach_mips4300:
9648 case bfd_mach_mips4400:
9649 case bfd_mach_mips4600:
9650 val = E_MIPS_ARCH_3;
9651 break;
9652
9653 case bfd_mach_mips4010:
9654 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
9655 break;
9656
9657 case bfd_mach_mips4100:
9658 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
9659 break;
9660
9661 case bfd_mach_mips4111:
9662 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
9663 break;
9664
9665 case bfd_mach_mips4120:
9666 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
9667 break;
9668
9669 case bfd_mach_mips4650:
9670 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
9671 break;
9672
9673 case bfd_mach_mips5400:
9674 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
9675 break;
9676
9677 case bfd_mach_mips5500:
9678 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
9679 break;
9680
9681 case bfd_mach_mips9000:
9682 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
9683 break;
9684
9685 case bfd_mach_mips5000:
9686 case bfd_mach_mips7000:
9687 case bfd_mach_mips8000:
9688 case bfd_mach_mips10000:
9689 case bfd_mach_mips12000:
9690 val = E_MIPS_ARCH_4;
9691 break;
9692
9693 case bfd_mach_mips5:
9694 val = E_MIPS_ARCH_5;
9695 break;
9696
9697 case bfd_mach_mips_loongson_2e:
9698 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
9699 break;
9700
9701 case bfd_mach_mips_loongson_2f:
9702 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
9703 break;
9704
9705 case bfd_mach_mips_sb1:
9706 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
9707 break;
9708
9709 case bfd_mach_mips_octeon:
9710 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
9711 break;
9712
9713 case bfd_mach_mipsisa32:
9714 val = E_MIPS_ARCH_32;
9715 break;
9716
9717 case bfd_mach_mipsisa64:
9718 val = E_MIPS_ARCH_64;
9719 break;
9720
9721 case bfd_mach_mipsisa32r2:
9722 val = E_MIPS_ARCH_32R2;
9723 break;
9724
9725 case bfd_mach_mipsisa64r2:
9726 val = E_MIPS_ARCH_64R2;
9727 break;
9728 }
9729 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
9730 elf_elfheader (abfd)->e_flags |= val;
9731
9732 }
9733
9734
9735 /* The final processing done just before writing out a MIPS ELF object
9736 file. This gets the MIPS architecture right based on the machine
9737 number. This is used by both the 32-bit and the 64-bit ABI. */
9738
9739 void
9740 _bfd_mips_elf_final_write_processing (bfd *abfd,
9741 bfd_boolean linker ATTRIBUTE_UNUSED)
9742 {
9743 unsigned int i;
9744 Elf_Internal_Shdr **hdrpp;
9745 const char *name;
9746 asection *sec;
9747
9748 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
9749 is nonzero. This is for compatibility with old objects, which used
9750 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
9751 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
9752 mips_set_isa_flags (abfd);
9753
9754 /* Set the sh_info field for .gptab sections and other appropriate
9755 info for each special section. */
9756 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
9757 i < elf_numsections (abfd);
9758 i++, hdrpp++)
9759 {
9760 switch ((*hdrpp)->sh_type)
9761 {
9762 case SHT_MIPS_MSYM:
9763 case SHT_MIPS_LIBLIST:
9764 sec = bfd_get_section_by_name (abfd, ".dynstr");
9765 if (sec != NULL)
9766 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
9767 break;
9768
9769 case SHT_MIPS_GPTAB:
9770 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
9771 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
9772 BFD_ASSERT (name != NULL
9773 && CONST_STRNEQ (name, ".gptab."));
9774 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
9775 BFD_ASSERT (sec != NULL);
9776 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
9777 break;
9778
9779 case SHT_MIPS_CONTENT:
9780 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
9781 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
9782 BFD_ASSERT (name != NULL
9783 && CONST_STRNEQ (name, ".MIPS.content"));
9784 sec = bfd_get_section_by_name (abfd,
9785 name + sizeof ".MIPS.content" - 1);
9786 BFD_ASSERT (sec != NULL);
9787 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
9788 break;
9789
9790 case SHT_MIPS_SYMBOL_LIB:
9791 sec = bfd_get_section_by_name (abfd, ".dynsym");
9792 if (sec != NULL)
9793 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
9794 sec = bfd_get_section_by_name (abfd, ".liblist");
9795 if (sec != NULL)
9796 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
9797 break;
9798
9799 case SHT_MIPS_EVENTS:
9800 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
9801 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
9802 BFD_ASSERT (name != NULL);
9803 if (CONST_STRNEQ (name, ".MIPS.events"))
9804 sec = bfd_get_section_by_name (abfd,
9805 name + sizeof ".MIPS.events" - 1);
9806 else
9807 {
9808 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
9809 sec = bfd_get_section_by_name (abfd,
9810 (name
9811 + sizeof ".MIPS.post_rel" - 1));
9812 }
9813 BFD_ASSERT (sec != NULL);
9814 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
9815 break;
9816
9817 }
9818 }
9819 }
9820 \f
9821 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
9822 segments. */
9823
9824 int
9825 _bfd_mips_elf_additional_program_headers (bfd *abfd,
9826 struct bfd_link_info *info ATTRIBUTE_UNUSED)
9827 {
9828 asection *s;
9829 int ret = 0;
9830
9831 /* See if we need a PT_MIPS_REGINFO segment. */
9832 s = bfd_get_section_by_name (abfd, ".reginfo");
9833 if (s && (s->flags & SEC_LOAD))
9834 ++ret;
9835
9836 /* See if we need a PT_MIPS_OPTIONS segment. */
9837 if (IRIX_COMPAT (abfd) == ict_irix6
9838 && bfd_get_section_by_name (abfd,
9839 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
9840 ++ret;
9841
9842 /* See if we need a PT_MIPS_RTPROC segment. */
9843 if (IRIX_COMPAT (abfd) == ict_irix5
9844 && bfd_get_section_by_name (abfd, ".dynamic")
9845 && bfd_get_section_by_name (abfd, ".mdebug"))
9846 ++ret;
9847
9848 /* Allocate a PT_NULL header in dynamic objects. See
9849 _bfd_mips_elf_modify_segment_map for details. */
9850 if (!SGI_COMPAT (abfd)
9851 && bfd_get_section_by_name (abfd, ".dynamic"))
9852 ++ret;
9853
9854 return ret;
9855 }
9856
9857 /* Modify the segment map for an IRIX5 executable. */
9858
9859 bfd_boolean
9860 _bfd_mips_elf_modify_segment_map (bfd *abfd,
9861 struct bfd_link_info *info)
9862 {
9863 asection *s;
9864 struct elf_segment_map *m, **pm;
9865 bfd_size_type amt;
9866
9867 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
9868 segment. */
9869 s = bfd_get_section_by_name (abfd, ".reginfo");
9870 if (s != NULL && (s->flags & SEC_LOAD) != 0)
9871 {
9872 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
9873 if (m->p_type == PT_MIPS_REGINFO)
9874 break;
9875 if (m == NULL)
9876 {
9877 amt = sizeof *m;
9878 m = bfd_zalloc (abfd, amt);
9879 if (m == NULL)
9880 return FALSE;
9881
9882 m->p_type = PT_MIPS_REGINFO;
9883 m->count = 1;
9884 m->sections[0] = s;
9885
9886 /* We want to put it after the PHDR and INTERP segments. */
9887 pm = &elf_tdata (abfd)->segment_map;
9888 while (*pm != NULL
9889 && ((*pm)->p_type == PT_PHDR
9890 || (*pm)->p_type == PT_INTERP))
9891 pm = &(*pm)->next;
9892
9893 m->next = *pm;
9894 *pm = m;
9895 }
9896 }
9897
9898 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
9899 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
9900 PT_MIPS_OPTIONS segment immediately following the program header
9901 table. */
9902 if (NEWABI_P (abfd)
9903 /* On non-IRIX6 new abi, we'll have already created a segment
9904 for this section, so don't create another. I'm not sure this
9905 is not also the case for IRIX 6, but I can't test it right
9906 now. */
9907 && IRIX_COMPAT (abfd) == ict_irix6)
9908 {
9909 for (s = abfd->sections; s; s = s->next)
9910 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
9911 break;
9912
9913 if (s)
9914 {
9915 struct elf_segment_map *options_segment;
9916
9917 pm = &elf_tdata (abfd)->segment_map;
9918 while (*pm != NULL
9919 && ((*pm)->p_type == PT_PHDR
9920 || (*pm)->p_type == PT_INTERP))
9921 pm = &(*pm)->next;
9922
9923 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
9924 {
9925 amt = sizeof (struct elf_segment_map);
9926 options_segment = bfd_zalloc (abfd, amt);
9927 options_segment->next = *pm;
9928 options_segment->p_type = PT_MIPS_OPTIONS;
9929 options_segment->p_flags = PF_R;
9930 options_segment->p_flags_valid = TRUE;
9931 options_segment->count = 1;
9932 options_segment->sections[0] = s;
9933 *pm = options_segment;
9934 }
9935 }
9936 }
9937 else
9938 {
9939 if (IRIX_COMPAT (abfd) == ict_irix5)
9940 {
9941 /* If there are .dynamic and .mdebug sections, we make a room
9942 for the RTPROC header. FIXME: Rewrite without section names. */
9943 if (bfd_get_section_by_name (abfd, ".interp") == NULL
9944 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
9945 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
9946 {
9947 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
9948 if (m->p_type == PT_MIPS_RTPROC)
9949 break;
9950 if (m == NULL)
9951 {
9952 amt = sizeof *m;
9953 m = bfd_zalloc (abfd, amt);
9954 if (m == NULL)
9955 return FALSE;
9956
9957 m->p_type = PT_MIPS_RTPROC;
9958
9959 s = bfd_get_section_by_name (abfd, ".rtproc");
9960 if (s == NULL)
9961 {
9962 m->count = 0;
9963 m->p_flags = 0;
9964 m->p_flags_valid = 1;
9965 }
9966 else
9967 {
9968 m->count = 1;
9969 m->sections[0] = s;
9970 }
9971
9972 /* We want to put it after the DYNAMIC segment. */
9973 pm = &elf_tdata (abfd)->segment_map;
9974 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
9975 pm = &(*pm)->next;
9976 if (*pm != NULL)
9977 pm = &(*pm)->next;
9978
9979 m->next = *pm;
9980 *pm = m;
9981 }
9982 }
9983 }
9984 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
9985 .dynstr, .dynsym, and .hash sections, and everything in
9986 between. */
9987 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
9988 pm = &(*pm)->next)
9989 if ((*pm)->p_type == PT_DYNAMIC)
9990 break;
9991 m = *pm;
9992 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
9993 {
9994 /* For a normal mips executable the permissions for the PT_DYNAMIC
9995 segment are read, write and execute. We do that here since
9996 the code in elf.c sets only the read permission. This matters
9997 sometimes for the dynamic linker. */
9998 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
9999 {
10000 m->p_flags = PF_R | PF_W | PF_X;
10001 m->p_flags_valid = 1;
10002 }
10003 }
10004 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
10005 glibc's dynamic linker has traditionally derived the number of
10006 tags from the p_filesz field, and sometimes allocates stack
10007 arrays of that size. An overly-big PT_DYNAMIC segment can
10008 be actively harmful in such cases. Making PT_DYNAMIC contain
10009 other sections can also make life hard for the prelinker,
10010 which might move one of the other sections to a different
10011 PT_LOAD segment. */
10012 if (SGI_COMPAT (abfd)
10013 && m != NULL
10014 && m->count == 1
10015 && strcmp (m->sections[0]->name, ".dynamic") == 0)
10016 {
10017 static const char *sec_names[] =
10018 {
10019 ".dynamic", ".dynstr", ".dynsym", ".hash"
10020 };
10021 bfd_vma low, high;
10022 unsigned int i, c;
10023 struct elf_segment_map *n;
10024
10025 low = ~(bfd_vma) 0;
10026 high = 0;
10027 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
10028 {
10029 s = bfd_get_section_by_name (abfd, sec_names[i]);
10030 if (s != NULL && (s->flags & SEC_LOAD) != 0)
10031 {
10032 bfd_size_type sz;
10033
10034 if (low > s->vma)
10035 low = s->vma;
10036 sz = s->size;
10037 if (high < s->vma + sz)
10038 high = s->vma + sz;
10039 }
10040 }
10041
10042 c = 0;
10043 for (s = abfd->sections; s != NULL; s = s->next)
10044 if ((s->flags & SEC_LOAD) != 0
10045 && s->vma >= low
10046 && s->vma + s->size <= high)
10047 ++c;
10048
10049 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
10050 n = bfd_zalloc (abfd, amt);
10051 if (n == NULL)
10052 return FALSE;
10053 *n = *m;
10054 n->count = c;
10055
10056 i = 0;
10057 for (s = abfd->sections; s != NULL; s = s->next)
10058 {
10059 if ((s->flags & SEC_LOAD) != 0
10060 && s->vma >= low
10061 && s->vma + s->size <= high)
10062 {
10063 n->sections[i] = s;
10064 ++i;
10065 }
10066 }
10067
10068 *pm = n;
10069 }
10070 }
10071
10072 /* Allocate a spare program header in dynamic objects so that tools
10073 like the prelinker can add an extra PT_LOAD entry.
10074
10075 If the prelinker needs to make room for a new PT_LOAD entry, its
10076 standard procedure is to move the first (read-only) sections into
10077 the new (writable) segment. However, the MIPS ABI requires
10078 .dynamic to be in a read-only segment, and the section will often
10079 start within sizeof (ElfNN_Phdr) bytes of the last program header.
10080
10081 Although the prelinker could in principle move .dynamic to a
10082 writable segment, it seems better to allocate a spare program
10083 header instead, and avoid the need to move any sections.
10084 There is a long tradition of allocating spare dynamic tags,
10085 so allocating a spare program header seems like a natural
10086 extension.
10087
10088 If INFO is NULL, we may be copying an already prelinked binary
10089 with objcopy or strip, so do not add this header. */
10090 if (info != NULL
10091 && !SGI_COMPAT (abfd)
10092 && bfd_get_section_by_name (abfd, ".dynamic"))
10093 {
10094 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
10095 if ((*pm)->p_type == PT_NULL)
10096 break;
10097 if (*pm == NULL)
10098 {
10099 m = bfd_zalloc (abfd, sizeof (*m));
10100 if (m == NULL)
10101 return FALSE;
10102
10103 m->p_type = PT_NULL;
10104 *pm = m;
10105 }
10106 }
10107
10108 return TRUE;
10109 }
10110 \f
10111 /* Return the section that should be marked against GC for a given
10112 relocation. */
10113
10114 asection *
10115 _bfd_mips_elf_gc_mark_hook (asection *sec,
10116 struct bfd_link_info *info,
10117 Elf_Internal_Rela *rel,
10118 struct elf_link_hash_entry *h,
10119 Elf_Internal_Sym *sym)
10120 {
10121 /* ??? Do mips16 stub sections need to be handled special? */
10122
10123 if (h != NULL)
10124 switch (ELF_R_TYPE (sec->owner, rel->r_info))
10125 {
10126 case R_MIPS_GNU_VTINHERIT:
10127 case R_MIPS_GNU_VTENTRY:
10128 return NULL;
10129 }
10130
10131 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
10132 }
10133
10134 /* Update the got entry reference counts for the section being removed. */
10135
10136 bfd_boolean
10137 _bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
10138 struct bfd_link_info *info ATTRIBUTE_UNUSED,
10139 asection *sec ATTRIBUTE_UNUSED,
10140 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
10141 {
10142 #if 0
10143 Elf_Internal_Shdr *symtab_hdr;
10144 struct elf_link_hash_entry **sym_hashes;
10145 bfd_signed_vma *local_got_refcounts;
10146 const Elf_Internal_Rela *rel, *relend;
10147 unsigned long r_symndx;
10148 struct elf_link_hash_entry *h;
10149
10150 if (info->relocatable)
10151 return TRUE;
10152
10153 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10154 sym_hashes = elf_sym_hashes (abfd);
10155 local_got_refcounts = elf_local_got_refcounts (abfd);
10156
10157 relend = relocs + sec->reloc_count;
10158 for (rel = relocs; rel < relend; rel++)
10159 switch (ELF_R_TYPE (abfd, rel->r_info))
10160 {
10161 case R_MIPS16_GOT16:
10162 case R_MIPS16_CALL16:
10163 case R_MIPS_GOT16:
10164 case R_MIPS_CALL16:
10165 case R_MIPS_CALL_HI16:
10166 case R_MIPS_CALL_LO16:
10167 case R_MIPS_GOT_HI16:
10168 case R_MIPS_GOT_LO16:
10169 case R_MIPS_GOT_DISP:
10170 case R_MIPS_GOT_PAGE:
10171 case R_MIPS_GOT_OFST:
10172 /* ??? It would seem that the existing MIPS code does no sort
10173 of reference counting or whatnot on its GOT and PLT entries,
10174 so it is not possible to garbage collect them at this time. */
10175 break;
10176
10177 default:
10178 break;
10179 }
10180 #endif
10181
10182 return TRUE;
10183 }
10184 \f
10185 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
10186 hiding the old indirect symbol. Process additional relocation
10187 information. Also called for weakdefs, in which case we just let
10188 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
10189
10190 void
10191 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
10192 struct elf_link_hash_entry *dir,
10193 struct elf_link_hash_entry *ind)
10194 {
10195 struct mips_elf_link_hash_entry *dirmips, *indmips;
10196
10197 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
10198
10199 if (ind->root.type != bfd_link_hash_indirect)
10200 return;
10201
10202 dirmips = (struct mips_elf_link_hash_entry *) dir;
10203 indmips = (struct mips_elf_link_hash_entry *) ind;
10204 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
10205 if (indmips->readonly_reloc)
10206 dirmips->readonly_reloc = TRUE;
10207 if (indmips->no_fn_stub)
10208 dirmips->no_fn_stub = TRUE;
10209 if (indmips->global_got_area < dirmips->global_got_area)
10210 dirmips->global_got_area = indmips->global_got_area;
10211 if (indmips->global_got_area < GGA_NONE)
10212 indmips->global_got_area = GGA_NONE;
10213
10214 if (dirmips->tls_type == 0)
10215 dirmips->tls_type = indmips->tls_type;
10216 }
10217 \f
10218 #define PDR_SIZE 32
10219
10220 bfd_boolean
10221 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
10222 struct bfd_link_info *info)
10223 {
10224 asection *o;
10225 bfd_boolean ret = FALSE;
10226 unsigned char *tdata;
10227 size_t i, skip;
10228
10229 o = bfd_get_section_by_name (abfd, ".pdr");
10230 if (! o)
10231 return FALSE;
10232 if (o->size == 0)
10233 return FALSE;
10234 if (o->size % PDR_SIZE != 0)
10235 return FALSE;
10236 if (o->output_section != NULL
10237 && bfd_is_abs_section (o->output_section))
10238 return FALSE;
10239
10240 tdata = bfd_zmalloc (o->size / PDR_SIZE);
10241 if (! tdata)
10242 return FALSE;
10243
10244 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
10245 info->keep_memory);
10246 if (!cookie->rels)
10247 {
10248 free (tdata);
10249 return FALSE;
10250 }
10251
10252 cookie->rel = cookie->rels;
10253 cookie->relend = cookie->rels + o->reloc_count;
10254
10255 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
10256 {
10257 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
10258 {
10259 tdata[i] = 1;
10260 skip ++;
10261 }
10262 }
10263
10264 if (skip != 0)
10265 {
10266 mips_elf_section_data (o)->u.tdata = tdata;
10267 o->size -= skip * PDR_SIZE;
10268 ret = TRUE;
10269 }
10270 else
10271 free (tdata);
10272
10273 if (! info->keep_memory)
10274 free (cookie->rels);
10275
10276 return ret;
10277 }
10278
10279 bfd_boolean
10280 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
10281 {
10282 if (strcmp (sec->name, ".pdr") == 0)
10283 return TRUE;
10284 return FALSE;
10285 }
10286
10287 bfd_boolean
10288 _bfd_mips_elf_write_section (bfd *output_bfd,
10289 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
10290 asection *sec, bfd_byte *contents)
10291 {
10292 bfd_byte *to, *from, *end;
10293 int i;
10294
10295 if (strcmp (sec->name, ".pdr") != 0)
10296 return FALSE;
10297
10298 if (mips_elf_section_data (sec)->u.tdata == NULL)
10299 return FALSE;
10300
10301 to = contents;
10302 end = contents + sec->size;
10303 for (from = contents, i = 0;
10304 from < end;
10305 from += PDR_SIZE, i++)
10306 {
10307 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
10308 continue;
10309 if (to != from)
10310 memcpy (to, from, PDR_SIZE);
10311 to += PDR_SIZE;
10312 }
10313 bfd_set_section_contents (output_bfd, sec->output_section, contents,
10314 sec->output_offset, sec->size);
10315 return TRUE;
10316 }
10317 \f
10318 /* MIPS ELF uses a special find_nearest_line routine in order the
10319 handle the ECOFF debugging information. */
10320
10321 struct mips_elf_find_line
10322 {
10323 struct ecoff_debug_info d;
10324 struct ecoff_find_line i;
10325 };
10326
10327 bfd_boolean
10328 _bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
10329 asymbol **symbols, bfd_vma offset,
10330 const char **filename_ptr,
10331 const char **functionname_ptr,
10332 unsigned int *line_ptr)
10333 {
10334 asection *msec;
10335
10336 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
10337 filename_ptr, functionname_ptr,
10338 line_ptr))
10339 return TRUE;
10340
10341 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
10342 filename_ptr, functionname_ptr,
10343 line_ptr, ABI_64_P (abfd) ? 8 : 0,
10344 &elf_tdata (abfd)->dwarf2_find_line_info))
10345 return TRUE;
10346
10347 msec = bfd_get_section_by_name (abfd, ".mdebug");
10348 if (msec != NULL)
10349 {
10350 flagword origflags;
10351 struct mips_elf_find_line *fi;
10352 const struct ecoff_debug_swap * const swap =
10353 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
10354
10355 /* If we are called during a link, mips_elf_final_link may have
10356 cleared the SEC_HAS_CONTENTS field. We force it back on here
10357 if appropriate (which it normally will be). */
10358 origflags = msec->flags;
10359 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
10360 msec->flags |= SEC_HAS_CONTENTS;
10361
10362 fi = elf_tdata (abfd)->find_line_info;
10363 if (fi == NULL)
10364 {
10365 bfd_size_type external_fdr_size;
10366 char *fraw_src;
10367 char *fraw_end;
10368 struct fdr *fdr_ptr;
10369 bfd_size_type amt = sizeof (struct mips_elf_find_line);
10370
10371 fi = bfd_zalloc (abfd, amt);
10372 if (fi == NULL)
10373 {
10374 msec->flags = origflags;
10375 return FALSE;
10376 }
10377
10378 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
10379 {
10380 msec->flags = origflags;
10381 return FALSE;
10382 }
10383
10384 /* Swap in the FDR information. */
10385 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
10386 fi->d.fdr = bfd_alloc (abfd, amt);
10387 if (fi->d.fdr == NULL)
10388 {
10389 msec->flags = origflags;
10390 return FALSE;
10391 }
10392 external_fdr_size = swap->external_fdr_size;
10393 fdr_ptr = fi->d.fdr;
10394 fraw_src = (char *) fi->d.external_fdr;
10395 fraw_end = (fraw_src
10396 + fi->d.symbolic_header.ifdMax * external_fdr_size);
10397 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
10398 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
10399
10400 elf_tdata (abfd)->find_line_info = fi;
10401
10402 /* Note that we don't bother to ever free this information.
10403 find_nearest_line is either called all the time, as in
10404 objdump -l, so the information should be saved, or it is
10405 rarely called, as in ld error messages, so the memory
10406 wasted is unimportant. Still, it would probably be a
10407 good idea for free_cached_info to throw it away. */
10408 }
10409
10410 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
10411 &fi->i, filename_ptr, functionname_ptr,
10412 line_ptr))
10413 {
10414 msec->flags = origflags;
10415 return TRUE;
10416 }
10417
10418 msec->flags = origflags;
10419 }
10420
10421 /* Fall back on the generic ELF find_nearest_line routine. */
10422
10423 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
10424 filename_ptr, functionname_ptr,
10425 line_ptr);
10426 }
10427
10428 bfd_boolean
10429 _bfd_mips_elf_find_inliner_info (bfd *abfd,
10430 const char **filename_ptr,
10431 const char **functionname_ptr,
10432 unsigned int *line_ptr)
10433 {
10434 bfd_boolean found;
10435 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
10436 functionname_ptr, line_ptr,
10437 & elf_tdata (abfd)->dwarf2_find_line_info);
10438 return found;
10439 }
10440
10441 \f
10442 /* When are writing out the .options or .MIPS.options section,
10443 remember the bytes we are writing out, so that we can install the
10444 GP value in the section_processing routine. */
10445
10446 bfd_boolean
10447 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
10448 const void *location,
10449 file_ptr offset, bfd_size_type count)
10450 {
10451 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
10452 {
10453 bfd_byte *c;
10454
10455 if (elf_section_data (section) == NULL)
10456 {
10457 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
10458 section->used_by_bfd = bfd_zalloc (abfd, amt);
10459 if (elf_section_data (section) == NULL)
10460 return FALSE;
10461 }
10462 c = mips_elf_section_data (section)->u.tdata;
10463 if (c == NULL)
10464 {
10465 c = bfd_zalloc (abfd, section->size);
10466 if (c == NULL)
10467 return FALSE;
10468 mips_elf_section_data (section)->u.tdata = c;
10469 }
10470
10471 memcpy (c + offset, location, count);
10472 }
10473
10474 return _bfd_elf_set_section_contents (abfd, section, location, offset,
10475 count);
10476 }
10477
10478 /* This is almost identical to bfd_generic_get_... except that some
10479 MIPS relocations need to be handled specially. Sigh. */
10480
10481 bfd_byte *
10482 _bfd_elf_mips_get_relocated_section_contents
10483 (bfd *abfd,
10484 struct bfd_link_info *link_info,
10485 struct bfd_link_order *link_order,
10486 bfd_byte *data,
10487 bfd_boolean relocatable,
10488 asymbol **symbols)
10489 {
10490 /* Get enough memory to hold the stuff */
10491 bfd *input_bfd = link_order->u.indirect.section->owner;
10492 asection *input_section = link_order->u.indirect.section;
10493 bfd_size_type sz;
10494
10495 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
10496 arelent **reloc_vector = NULL;
10497 long reloc_count;
10498
10499 if (reloc_size < 0)
10500 goto error_return;
10501
10502 reloc_vector = bfd_malloc (reloc_size);
10503 if (reloc_vector == NULL && reloc_size != 0)
10504 goto error_return;
10505
10506 /* read in the section */
10507 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
10508 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
10509 goto error_return;
10510
10511 reloc_count = bfd_canonicalize_reloc (input_bfd,
10512 input_section,
10513 reloc_vector,
10514 symbols);
10515 if (reloc_count < 0)
10516 goto error_return;
10517
10518 if (reloc_count > 0)
10519 {
10520 arelent **parent;
10521 /* for mips */
10522 int gp_found;
10523 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
10524
10525 {
10526 struct bfd_hash_entry *h;
10527 struct bfd_link_hash_entry *lh;
10528 /* Skip all this stuff if we aren't mixing formats. */
10529 if (abfd && input_bfd
10530 && abfd->xvec == input_bfd->xvec)
10531 lh = 0;
10532 else
10533 {
10534 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
10535 lh = (struct bfd_link_hash_entry *) h;
10536 }
10537 lookup:
10538 if (lh)
10539 {
10540 switch (lh->type)
10541 {
10542 case bfd_link_hash_undefined:
10543 case bfd_link_hash_undefweak:
10544 case bfd_link_hash_common:
10545 gp_found = 0;
10546 break;
10547 case bfd_link_hash_defined:
10548 case bfd_link_hash_defweak:
10549 gp_found = 1;
10550 gp = lh->u.def.value;
10551 break;
10552 case bfd_link_hash_indirect:
10553 case bfd_link_hash_warning:
10554 lh = lh->u.i.link;
10555 /* @@FIXME ignoring warning for now */
10556 goto lookup;
10557 case bfd_link_hash_new:
10558 default:
10559 abort ();
10560 }
10561 }
10562 else
10563 gp_found = 0;
10564 }
10565 /* end mips */
10566 for (parent = reloc_vector; *parent != NULL; parent++)
10567 {
10568 char *error_message = NULL;
10569 bfd_reloc_status_type r;
10570
10571 /* Specific to MIPS: Deal with relocation types that require
10572 knowing the gp of the output bfd. */
10573 asymbol *sym = *(*parent)->sym_ptr_ptr;
10574
10575 /* If we've managed to find the gp and have a special
10576 function for the relocation then go ahead, else default
10577 to the generic handling. */
10578 if (gp_found
10579 && (*parent)->howto->special_function
10580 == _bfd_mips_elf32_gprel16_reloc)
10581 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
10582 input_section, relocatable,
10583 data, gp);
10584 else
10585 r = bfd_perform_relocation (input_bfd, *parent, data,
10586 input_section,
10587 relocatable ? abfd : NULL,
10588 &error_message);
10589
10590 if (relocatable)
10591 {
10592 asection *os = input_section->output_section;
10593
10594 /* A partial link, so keep the relocs */
10595 os->orelocation[os->reloc_count] = *parent;
10596 os->reloc_count++;
10597 }
10598
10599 if (r != bfd_reloc_ok)
10600 {
10601 switch (r)
10602 {
10603 case bfd_reloc_undefined:
10604 if (!((*link_info->callbacks->undefined_symbol)
10605 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
10606 input_bfd, input_section, (*parent)->address, TRUE)))
10607 goto error_return;
10608 break;
10609 case bfd_reloc_dangerous:
10610 BFD_ASSERT (error_message != NULL);
10611 if (!((*link_info->callbacks->reloc_dangerous)
10612 (link_info, error_message, input_bfd, input_section,
10613 (*parent)->address)))
10614 goto error_return;
10615 break;
10616 case bfd_reloc_overflow:
10617 if (!((*link_info->callbacks->reloc_overflow)
10618 (link_info, NULL,
10619 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
10620 (*parent)->howto->name, (*parent)->addend,
10621 input_bfd, input_section, (*parent)->address)))
10622 goto error_return;
10623 break;
10624 case bfd_reloc_outofrange:
10625 default:
10626 abort ();
10627 break;
10628 }
10629
10630 }
10631 }
10632 }
10633 if (reloc_vector != NULL)
10634 free (reloc_vector);
10635 return data;
10636
10637 error_return:
10638 if (reloc_vector != NULL)
10639 free (reloc_vector);
10640 return NULL;
10641 }
10642 \f
10643 /* Create a MIPS ELF linker hash table. */
10644
10645 struct bfd_link_hash_table *
10646 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
10647 {
10648 struct mips_elf_link_hash_table *ret;
10649 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
10650
10651 ret = bfd_malloc (amt);
10652 if (ret == NULL)
10653 return NULL;
10654
10655 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
10656 mips_elf_link_hash_newfunc,
10657 sizeof (struct mips_elf_link_hash_entry)))
10658 {
10659 free (ret);
10660 return NULL;
10661 }
10662
10663 #if 0
10664 /* We no longer use this. */
10665 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
10666 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
10667 #endif
10668 ret->procedure_count = 0;
10669 ret->compact_rel_size = 0;
10670 ret->use_rld_obj_head = FALSE;
10671 ret->rld_value = 0;
10672 ret->mips16_stubs_seen = FALSE;
10673 ret->is_vxworks = FALSE;
10674 ret->small_data_overflow_reported = FALSE;
10675 ret->srelbss = NULL;
10676 ret->sdynbss = NULL;
10677 ret->srelplt = NULL;
10678 ret->srelplt2 = NULL;
10679 ret->sgotplt = NULL;
10680 ret->splt = NULL;
10681 ret->sstubs = NULL;
10682 ret->sgot = NULL;
10683 ret->got_info = NULL;
10684 ret->plt_header_size = 0;
10685 ret->plt_entry_size = 0;
10686 ret->lazy_stub_count = 0;
10687 ret->function_stub_size = 0;
10688
10689 return &ret->root.root;
10690 }
10691
10692 /* Likewise, but indicate that the target is VxWorks. */
10693
10694 struct bfd_link_hash_table *
10695 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
10696 {
10697 struct bfd_link_hash_table *ret;
10698
10699 ret = _bfd_mips_elf_link_hash_table_create (abfd);
10700 if (ret)
10701 {
10702 struct mips_elf_link_hash_table *htab;
10703
10704 htab = (struct mips_elf_link_hash_table *) ret;
10705 htab->is_vxworks = 1;
10706 }
10707 return ret;
10708 }
10709 \f
10710 /* We need to use a special link routine to handle the .reginfo and
10711 the .mdebug sections. We need to merge all instances of these
10712 sections together, not write them all out sequentially. */
10713
10714 bfd_boolean
10715 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10716 {
10717 asection *o;
10718 struct bfd_link_order *p;
10719 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
10720 asection *rtproc_sec;
10721 Elf32_RegInfo reginfo;
10722 struct ecoff_debug_info debug;
10723 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10724 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
10725 HDRR *symhdr = &debug.symbolic_header;
10726 void *mdebug_handle = NULL;
10727 asection *s;
10728 EXTR esym;
10729 unsigned int i;
10730 bfd_size_type amt;
10731 struct mips_elf_link_hash_table *htab;
10732
10733 static const char * const secname[] =
10734 {
10735 ".text", ".init", ".fini", ".data",
10736 ".rodata", ".sdata", ".sbss", ".bss"
10737 };
10738 static const int sc[] =
10739 {
10740 scText, scInit, scFini, scData,
10741 scRData, scSData, scSBss, scBss
10742 };
10743
10744 /* Sort the dynamic symbols so that those with GOT entries come after
10745 those without. */
10746 htab = mips_elf_hash_table (info);
10747 if (!mips_elf_sort_hash_table (abfd, info))
10748 return FALSE;
10749
10750 /* Get a value for the GP register. */
10751 if (elf_gp (abfd) == 0)
10752 {
10753 struct bfd_link_hash_entry *h;
10754
10755 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
10756 if (h != NULL && h->type == bfd_link_hash_defined)
10757 elf_gp (abfd) = (h->u.def.value
10758 + h->u.def.section->output_section->vma
10759 + h->u.def.section->output_offset);
10760 else if (htab->is_vxworks
10761 && (h = bfd_link_hash_lookup (info->hash,
10762 "_GLOBAL_OFFSET_TABLE_",
10763 FALSE, FALSE, TRUE))
10764 && h->type == bfd_link_hash_defined)
10765 elf_gp (abfd) = (h->u.def.section->output_section->vma
10766 + h->u.def.section->output_offset
10767 + h->u.def.value);
10768 else if (info->relocatable)
10769 {
10770 bfd_vma lo = MINUS_ONE;
10771
10772 /* Find the GP-relative section with the lowest offset. */
10773 for (o = abfd->sections; o != NULL; o = o->next)
10774 if (o->vma < lo
10775 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
10776 lo = o->vma;
10777
10778 /* And calculate GP relative to that. */
10779 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
10780 }
10781 else
10782 {
10783 /* If the relocate_section function needs to do a reloc
10784 involving the GP value, it should make a reloc_dangerous
10785 callback to warn that GP is not defined. */
10786 }
10787 }
10788
10789 /* Go through the sections and collect the .reginfo and .mdebug
10790 information. */
10791 reginfo_sec = NULL;
10792 mdebug_sec = NULL;
10793 gptab_data_sec = NULL;
10794 gptab_bss_sec = NULL;
10795 for (o = abfd->sections; o != NULL; o = o->next)
10796 {
10797 if (strcmp (o->name, ".reginfo") == 0)
10798 {
10799 memset (&reginfo, 0, sizeof reginfo);
10800
10801 /* We have found the .reginfo section in the output file.
10802 Look through all the link_orders comprising it and merge
10803 the information together. */
10804 for (p = o->map_head.link_order; p != NULL; p = p->next)
10805 {
10806 asection *input_section;
10807 bfd *input_bfd;
10808 Elf32_External_RegInfo ext;
10809 Elf32_RegInfo sub;
10810
10811 if (p->type != bfd_indirect_link_order)
10812 {
10813 if (p->type == bfd_data_link_order)
10814 continue;
10815 abort ();
10816 }
10817
10818 input_section = p->u.indirect.section;
10819 input_bfd = input_section->owner;
10820
10821 if (! bfd_get_section_contents (input_bfd, input_section,
10822 &ext, 0, sizeof ext))
10823 return FALSE;
10824
10825 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
10826
10827 reginfo.ri_gprmask |= sub.ri_gprmask;
10828 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
10829 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
10830 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
10831 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
10832
10833 /* ri_gp_value is set by the function
10834 mips_elf32_section_processing when the section is
10835 finally written out. */
10836
10837 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10838 elf_link_input_bfd ignores this section. */
10839 input_section->flags &= ~SEC_HAS_CONTENTS;
10840 }
10841
10842 /* Size has been set in _bfd_mips_elf_always_size_sections. */
10843 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
10844
10845 /* Skip this section later on (I don't think this currently
10846 matters, but someday it might). */
10847 o->map_head.link_order = NULL;
10848
10849 reginfo_sec = o;
10850 }
10851
10852 if (strcmp (o->name, ".mdebug") == 0)
10853 {
10854 struct extsym_info einfo;
10855 bfd_vma last;
10856
10857 /* We have found the .mdebug section in the output file.
10858 Look through all the link_orders comprising it and merge
10859 the information together. */
10860 symhdr->magic = swap->sym_magic;
10861 /* FIXME: What should the version stamp be? */
10862 symhdr->vstamp = 0;
10863 symhdr->ilineMax = 0;
10864 symhdr->cbLine = 0;
10865 symhdr->idnMax = 0;
10866 symhdr->ipdMax = 0;
10867 symhdr->isymMax = 0;
10868 symhdr->ioptMax = 0;
10869 symhdr->iauxMax = 0;
10870 symhdr->issMax = 0;
10871 symhdr->issExtMax = 0;
10872 symhdr->ifdMax = 0;
10873 symhdr->crfd = 0;
10874 symhdr->iextMax = 0;
10875
10876 /* We accumulate the debugging information itself in the
10877 debug_info structure. */
10878 debug.line = NULL;
10879 debug.external_dnr = NULL;
10880 debug.external_pdr = NULL;
10881 debug.external_sym = NULL;
10882 debug.external_opt = NULL;
10883 debug.external_aux = NULL;
10884 debug.ss = NULL;
10885 debug.ssext = debug.ssext_end = NULL;
10886 debug.external_fdr = NULL;
10887 debug.external_rfd = NULL;
10888 debug.external_ext = debug.external_ext_end = NULL;
10889
10890 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
10891 if (mdebug_handle == NULL)
10892 return FALSE;
10893
10894 esym.jmptbl = 0;
10895 esym.cobol_main = 0;
10896 esym.weakext = 0;
10897 esym.reserved = 0;
10898 esym.ifd = ifdNil;
10899 esym.asym.iss = issNil;
10900 esym.asym.st = stLocal;
10901 esym.asym.reserved = 0;
10902 esym.asym.index = indexNil;
10903 last = 0;
10904 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
10905 {
10906 esym.asym.sc = sc[i];
10907 s = bfd_get_section_by_name (abfd, secname[i]);
10908 if (s != NULL)
10909 {
10910 esym.asym.value = s->vma;
10911 last = s->vma + s->size;
10912 }
10913 else
10914 esym.asym.value = last;
10915 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
10916 secname[i], &esym))
10917 return FALSE;
10918 }
10919
10920 for (p = o->map_head.link_order; p != NULL; p = p->next)
10921 {
10922 asection *input_section;
10923 bfd *input_bfd;
10924 const struct ecoff_debug_swap *input_swap;
10925 struct ecoff_debug_info input_debug;
10926 char *eraw_src;
10927 char *eraw_end;
10928
10929 if (p->type != bfd_indirect_link_order)
10930 {
10931 if (p->type == bfd_data_link_order)
10932 continue;
10933 abort ();
10934 }
10935
10936 input_section = p->u.indirect.section;
10937 input_bfd = input_section->owner;
10938
10939 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
10940 || (get_elf_backend_data (input_bfd)
10941 ->elf_backend_ecoff_debug_swap) == NULL)
10942 {
10943 /* I don't know what a non MIPS ELF bfd would be
10944 doing with a .mdebug section, but I don't really
10945 want to deal with it. */
10946 continue;
10947 }
10948
10949 input_swap = (get_elf_backend_data (input_bfd)
10950 ->elf_backend_ecoff_debug_swap);
10951
10952 BFD_ASSERT (p->size == input_section->size);
10953
10954 /* The ECOFF linking code expects that we have already
10955 read in the debugging information and set up an
10956 ecoff_debug_info structure, so we do that now. */
10957 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
10958 &input_debug))
10959 return FALSE;
10960
10961 if (! (bfd_ecoff_debug_accumulate
10962 (mdebug_handle, abfd, &debug, swap, input_bfd,
10963 &input_debug, input_swap, info)))
10964 return FALSE;
10965
10966 /* Loop through the external symbols. For each one with
10967 interesting information, try to find the symbol in
10968 the linker global hash table and save the information
10969 for the output external symbols. */
10970 eraw_src = input_debug.external_ext;
10971 eraw_end = (eraw_src
10972 + (input_debug.symbolic_header.iextMax
10973 * input_swap->external_ext_size));
10974 for (;
10975 eraw_src < eraw_end;
10976 eraw_src += input_swap->external_ext_size)
10977 {
10978 EXTR ext;
10979 const char *name;
10980 struct mips_elf_link_hash_entry *h;
10981
10982 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
10983 if (ext.asym.sc == scNil
10984 || ext.asym.sc == scUndefined
10985 || ext.asym.sc == scSUndefined)
10986 continue;
10987
10988 name = input_debug.ssext + ext.asym.iss;
10989 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
10990 name, FALSE, FALSE, TRUE);
10991 if (h == NULL || h->esym.ifd != -2)
10992 continue;
10993
10994 if (ext.ifd != -1)
10995 {
10996 BFD_ASSERT (ext.ifd
10997 < input_debug.symbolic_header.ifdMax);
10998 ext.ifd = input_debug.ifdmap[ext.ifd];
10999 }
11000
11001 h->esym = ext;
11002 }
11003
11004 /* Free up the information we just read. */
11005 free (input_debug.line);
11006 free (input_debug.external_dnr);
11007 free (input_debug.external_pdr);
11008 free (input_debug.external_sym);
11009 free (input_debug.external_opt);
11010 free (input_debug.external_aux);
11011 free (input_debug.ss);
11012 free (input_debug.ssext);
11013 free (input_debug.external_fdr);
11014 free (input_debug.external_rfd);
11015 free (input_debug.external_ext);
11016
11017 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11018 elf_link_input_bfd ignores this section. */
11019 input_section->flags &= ~SEC_HAS_CONTENTS;
11020 }
11021
11022 if (SGI_COMPAT (abfd) && info->shared)
11023 {
11024 /* Create .rtproc section. */
11025 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
11026 if (rtproc_sec == NULL)
11027 {
11028 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
11029 | SEC_LINKER_CREATED | SEC_READONLY);
11030
11031 rtproc_sec = bfd_make_section_with_flags (abfd,
11032 ".rtproc",
11033 flags);
11034 if (rtproc_sec == NULL
11035 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
11036 return FALSE;
11037 }
11038
11039 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
11040 info, rtproc_sec,
11041 &debug))
11042 return FALSE;
11043 }
11044
11045 /* Build the external symbol information. */
11046 einfo.abfd = abfd;
11047 einfo.info = info;
11048 einfo.debug = &debug;
11049 einfo.swap = swap;
11050 einfo.failed = FALSE;
11051 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
11052 mips_elf_output_extsym, &einfo);
11053 if (einfo.failed)
11054 return FALSE;
11055
11056 /* Set the size of the .mdebug section. */
11057 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
11058
11059 /* Skip this section later on (I don't think this currently
11060 matters, but someday it might). */
11061 o->map_head.link_order = NULL;
11062
11063 mdebug_sec = o;
11064 }
11065
11066 if (CONST_STRNEQ (o->name, ".gptab."))
11067 {
11068 const char *subname;
11069 unsigned int c;
11070 Elf32_gptab *tab;
11071 Elf32_External_gptab *ext_tab;
11072 unsigned int j;
11073
11074 /* The .gptab.sdata and .gptab.sbss sections hold
11075 information describing how the small data area would
11076 change depending upon the -G switch. These sections
11077 not used in executables files. */
11078 if (! info->relocatable)
11079 {
11080 for (p = o->map_head.link_order; p != NULL; p = p->next)
11081 {
11082 asection *input_section;
11083
11084 if (p->type != bfd_indirect_link_order)
11085 {
11086 if (p->type == bfd_data_link_order)
11087 continue;
11088 abort ();
11089 }
11090
11091 input_section = p->u.indirect.section;
11092
11093 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11094 elf_link_input_bfd ignores this section. */
11095 input_section->flags &= ~SEC_HAS_CONTENTS;
11096 }
11097
11098 /* Skip this section later on (I don't think this
11099 currently matters, but someday it might). */
11100 o->map_head.link_order = NULL;
11101
11102 /* Really remove the section. */
11103 bfd_section_list_remove (abfd, o);
11104 --abfd->section_count;
11105
11106 continue;
11107 }
11108
11109 /* There is one gptab for initialized data, and one for
11110 uninitialized data. */
11111 if (strcmp (o->name, ".gptab.sdata") == 0)
11112 gptab_data_sec = o;
11113 else if (strcmp (o->name, ".gptab.sbss") == 0)
11114 gptab_bss_sec = o;
11115 else
11116 {
11117 (*_bfd_error_handler)
11118 (_("%s: illegal section name `%s'"),
11119 bfd_get_filename (abfd), o->name);
11120 bfd_set_error (bfd_error_nonrepresentable_section);
11121 return FALSE;
11122 }
11123
11124 /* The linker script always combines .gptab.data and
11125 .gptab.sdata into .gptab.sdata, and likewise for
11126 .gptab.bss and .gptab.sbss. It is possible that there is
11127 no .sdata or .sbss section in the output file, in which
11128 case we must change the name of the output section. */
11129 subname = o->name + sizeof ".gptab" - 1;
11130 if (bfd_get_section_by_name (abfd, subname) == NULL)
11131 {
11132 if (o == gptab_data_sec)
11133 o->name = ".gptab.data";
11134 else
11135 o->name = ".gptab.bss";
11136 subname = o->name + sizeof ".gptab" - 1;
11137 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
11138 }
11139
11140 /* Set up the first entry. */
11141 c = 1;
11142 amt = c * sizeof (Elf32_gptab);
11143 tab = bfd_malloc (amt);
11144 if (tab == NULL)
11145 return FALSE;
11146 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
11147 tab[0].gt_header.gt_unused = 0;
11148
11149 /* Combine the input sections. */
11150 for (p = o->map_head.link_order; p != NULL; p = p->next)
11151 {
11152 asection *input_section;
11153 bfd *input_bfd;
11154 bfd_size_type size;
11155 unsigned long last;
11156 bfd_size_type gpentry;
11157
11158 if (p->type != bfd_indirect_link_order)
11159 {
11160 if (p->type == bfd_data_link_order)
11161 continue;
11162 abort ();
11163 }
11164
11165 input_section = p->u.indirect.section;
11166 input_bfd = input_section->owner;
11167
11168 /* Combine the gptab entries for this input section one
11169 by one. We know that the input gptab entries are
11170 sorted by ascending -G value. */
11171 size = input_section->size;
11172 last = 0;
11173 for (gpentry = sizeof (Elf32_External_gptab);
11174 gpentry < size;
11175 gpentry += sizeof (Elf32_External_gptab))
11176 {
11177 Elf32_External_gptab ext_gptab;
11178 Elf32_gptab int_gptab;
11179 unsigned long val;
11180 unsigned long add;
11181 bfd_boolean exact;
11182 unsigned int look;
11183
11184 if (! (bfd_get_section_contents
11185 (input_bfd, input_section, &ext_gptab, gpentry,
11186 sizeof (Elf32_External_gptab))))
11187 {
11188 free (tab);
11189 return FALSE;
11190 }
11191
11192 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
11193 &int_gptab);
11194 val = int_gptab.gt_entry.gt_g_value;
11195 add = int_gptab.gt_entry.gt_bytes - last;
11196
11197 exact = FALSE;
11198 for (look = 1; look < c; look++)
11199 {
11200 if (tab[look].gt_entry.gt_g_value >= val)
11201 tab[look].gt_entry.gt_bytes += add;
11202
11203 if (tab[look].gt_entry.gt_g_value == val)
11204 exact = TRUE;
11205 }
11206
11207 if (! exact)
11208 {
11209 Elf32_gptab *new_tab;
11210 unsigned int max;
11211
11212 /* We need a new table entry. */
11213 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
11214 new_tab = bfd_realloc (tab, amt);
11215 if (new_tab == NULL)
11216 {
11217 free (tab);
11218 return FALSE;
11219 }
11220 tab = new_tab;
11221 tab[c].gt_entry.gt_g_value = val;
11222 tab[c].gt_entry.gt_bytes = add;
11223
11224 /* Merge in the size for the next smallest -G
11225 value, since that will be implied by this new
11226 value. */
11227 max = 0;
11228 for (look = 1; look < c; look++)
11229 {
11230 if (tab[look].gt_entry.gt_g_value < val
11231 && (max == 0
11232 || (tab[look].gt_entry.gt_g_value
11233 > tab[max].gt_entry.gt_g_value)))
11234 max = look;
11235 }
11236 if (max != 0)
11237 tab[c].gt_entry.gt_bytes +=
11238 tab[max].gt_entry.gt_bytes;
11239
11240 ++c;
11241 }
11242
11243 last = int_gptab.gt_entry.gt_bytes;
11244 }
11245
11246 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11247 elf_link_input_bfd ignores this section. */
11248 input_section->flags &= ~SEC_HAS_CONTENTS;
11249 }
11250
11251 /* The table must be sorted by -G value. */
11252 if (c > 2)
11253 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
11254
11255 /* Swap out the table. */
11256 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
11257 ext_tab = bfd_alloc (abfd, amt);
11258 if (ext_tab == NULL)
11259 {
11260 free (tab);
11261 return FALSE;
11262 }
11263
11264 for (j = 0; j < c; j++)
11265 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
11266 free (tab);
11267
11268 o->size = c * sizeof (Elf32_External_gptab);
11269 o->contents = (bfd_byte *) ext_tab;
11270
11271 /* Skip this section later on (I don't think this currently
11272 matters, but someday it might). */
11273 o->map_head.link_order = NULL;
11274 }
11275 }
11276
11277 /* Invoke the regular ELF backend linker to do all the work. */
11278 if (!bfd_elf_final_link (abfd, info))
11279 return FALSE;
11280
11281 /* Now write out the computed sections. */
11282
11283 if (reginfo_sec != NULL)
11284 {
11285 Elf32_External_RegInfo ext;
11286
11287 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
11288 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
11289 return FALSE;
11290 }
11291
11292 if (mdebug_sec != NULL)
11293 {
11294 BFD_ASSERT (abfd->output_has_begun);
11295 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
11296 swap, info,
11297 mdebug_sec->filepos))
11298 return FALSE;
11299
11300 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
11301 }
11302
11303 if (gptab_data_sec != NULL)
11304 {
11305 if (! bfd_set_section_contents (abfd, gptab_data_sec,
11306 gptab_data_sec->contents,
11307 0, gptab_data_sec->size))
11308 return FALSE;
11309 }
11310
11311 if (gptab_bss_sec != NULL)
11312 {
11313 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
11314 gptab_bss_sec->contents,
11315 0, gptab_bss_sec->size))
11316 return FALSE;
11317 }
11318
11319 if (SGI_COMPAT (abfd))
11320 {
11321 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
11322 if (rtproc_sec != NULL)
11323 {
11324 if (! bfd_set_section_contents (abfd, rtproc_sec,
11325 rtproc_sec->contents,
11326 0, rtproc_sec->size))
11327 return FALSE;
11328 }
11329 }
11330
11331 return TRUE;
11332 }
11333 \f
11334 /* Structure for saying that BFD machine EXTENSION extends BASE. */
11335
11336 struct mips_mach_extension {
11337 unsigned long extension, base;
11338 };
11339
11340
11341 /* An array describing how BFD machines relate to one another. The entries
11342 are ordered topologically with MIPS I extensions listed last. */
11343
11344 static const struct mips_mach_extension mips_mach_extensions[] = {
11345 /* MIPS64r2 extensions. */
11346 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
11347
11348 /* MIPS64 extensions. */
11349 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
11350 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
11351
11352 /* MIPS V extensions. */
11353 { bfd_mach_mipsisa64, bfd_mach_mips5 },
11354
11355 /* R10000 extensions. */
11356 { bfd_mach_mips12000, bfd_mach_mips10000 },
11357
11358 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
11359 vr5400 ISA, but doesn't include the multimedia stuff. It seems
11360 better to allow vr5400 and vr5500 code to be merged anyway, since
11361 many libraries will just use the core ISA. Perhaps we could add
11362 some sort of ASE flag if this ever proves a problem. */
11363 { bfd_mach_mips5500, bfd_mach_mips5400 },
11364 { bfd_mach_mips5400, bfd_mach_mips5000 },
11365
11366 /* MIPS IV extensions. */
11367 { bfd_mach_mips5, bfd_mach_mips8000 },
11368 { bfd_mach_mips10000, bfd_mach_mips8000 },
11369 { bfd_mach_mips5000, bfd_mach_mips8000 },
11370 { bfd_mach_mips7000, bfd_mach_mips8000 },
11371 { bfd_mach_mips9000, bfd_mach_mips8000 },
11372
11373 /* VR4100 extensions. */
11374 { bfd_mach_mips4120, bfd_mach_mips4100 },
11375 { bfd_mach_mips4111, bfd_mach_mips4100 },
11376
11377 /* MIPS III extensions. */
11378 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
11379 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
11380 { bfd_mach_mips8000, bfd_mach_mips4000 },
11381 { bfd_mach_mips4650, bfd_mach_mips4000 },
11382 { bfd_mach_mips4600, bfd_mach_mips4000 },
11383 { bfd_mach_mips4400, bfd_mach_mips4000 },
11384 { bfd_mach_mips4300, bfd_mach_mips4000 },
11385 { bfd_mach_mips4100, bfd_mach_mips4000 },
11386 { bfd_mach_mips4010, bfd_mach_mips4000 },
11387
11388 /* MIPS32 extensions. */
11389 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
11390
11391 /* MIPS II extensions. */
11392 { bfd_mach_mips4000, bfd_mach_mips6000 },
11393 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
11394
11395 /* MIPS I extensions. */
11396 { bfd_mach_mips6000, bfd_mach_mips3000 },
11397 { bfd_mach_mips3900, bfd_mach_mips3000 }
11398 };
11399
11400
11401 /* Return true if bfd machine EXTENSION is an extension of machine BASE. */
11402
11403 static bfd_boolean
11404 mips_mach_extends_p (unsigned long base, unsigned long extension)
11405 {
11406 size_t i;
11407
11408 if (extension == base)
11409 return TRUE;
11410
11411 if (base == bfd_mach_mipsisa32
11412 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
11413 return TRUE;
11414
11415 if (base == bfd_mach_mipsisa32r2
11416 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
11417 return TRUE;
11418
11419 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
11420 if (extension == mips_mach_extensions[i].extension)
11421 {
11422 extension = mips_mach_extensions[i].base;
11423 if (extension == base)
11424 return TRUE;
11425 }
11426
11427 return FALSE;
11428 }
11429
11430
11431 /* Return true if the given ELF header flags describe a 32-bit binary. */
11432
11433 static bfd_boolean
11434 mips_32bit_flags_p (flagword flags)
11435 {
11436 return ((flags & EF_MIPS_32BITMODE) != 0
11437 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
11438 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
11439 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
11440 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
11441 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
11442 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
11443 }
11444
11445
11446 /* Merge object attributes from IBFD into OBFD. Raise an error if
11447 there are conflicting attributes. */
11448 static bfd_boolean
11449 mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
11450 {
11451 obj_attribute *in_attr;
11452 obj_attribute *out_attr;
11453
11454 if (!elf_known_obj_attributes_proc (obfd)[0].i)
11455 {
11456 /* This is the first object. Copy the attributes. */
11457 _bfd_elf_copy_obj_attributes (ibfd, obfd);
11458
11459 /* Use the Tag_null value to indicate the attributes have been
11460 initialized. */
11461 elf_known_obj_attributes_proc (obfd)[0].i = 1;
11462
11463 return TRUE;
11464 }
11465
11466 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
11467 non-conflicting ones. */
11468 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
11469 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
11470 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
11471 {
11472 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
11473 if (out_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
11474 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
11475 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
11476 ;
11477 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
11478 _bfd_error_handler
11479 (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
11480 in_attr[Tag_GNU_MIPS_ABI_FP].i);
11481 else if (out_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
11482 _bfd_error_handler
11483 (_("Warning: %B uses unknown floating point ABI %d"), obfd,
11484 out_attr[Tag_GNU_MIPS_ABI_FP].i);
11485 else
11486 switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
11487 {
11488 case 1:
11489 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
11490 {
11491 case 2:
11492 _bfd_error_handler
11493 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
11494 obfd, ibfd);
11495 break;
11496
11497 case 3:
11498 _bfd_error_handler
11499 (_("Warning: %B uses hard float, %B uses soft float"),
11500 obfd, ibfd);
11501 break;
11502
11503 case 4:
11504 _bfd_error_handler
11505 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
11506 obfd, ibfd);
11507 break;
11508
11509 default:
11510 abort ();
11511 }
11512 break;
11513
11514 case 2:
11515 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
11516 {
11517 case 1:
11518 _bfd_error_handler
11519 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
11520 ibfd, obfd);
11521 break;
11522
11523 case 3:
11524 _bfd_error_handler
11525 (_("Warning: %B uses hard float, %B uses soft float"),
11526 obfd, ibfd);
11527 break;
11528
11529 case 4:
11530 _bfd_error_handler
11531 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
11532 obfd, ibfd);
11533 break;
11534
11535 default:
11536 abort ();
11537 }
11538 break;
11539
11540 case 3:
11541 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
11542 {
11543 case 1:
11544 case 2:
11545 case 4:
11546 _bfd_error_handler
11547 (_("Warning: %B uses hard float, %B uses soft float"),
11548 ibfd, obfd);
11549 break;
11550
11551 default:
11552 abort ();
11553 }
11554 break;
11555
11556 case 4:
11557 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
11558 {
11559 case 1:
11560 _bfd_error_handler
11561 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
11562 ibfd, obfd);
11563 break;
11564
11565 case 2:
11566 _bfd_error_handler
11567 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
11568 ibfd, obfd);
11569 break;
11570
11571 case 3:
11572 _bfd_error_handler
11573 (_("Warning: %B uses hard float, %B uses soft float"),
11574 obfd, ibfd);
11575 break;
11576
11577 default:
11578 abort ();
11579 }
11580 break;
11581
11582 default:
11583 abort ();
11584 }
11585 }
11586
11587 /* Merge Tag_compatibility attributes and any common GNU ones. */
11588 _bfd_elf_merge_object_attributes (ibfd, obfd);
11589
11590 return TRUE;
11591 }
11592
11593 /* Merge backend specific data from an object file to the output
11594 object file when linking. */
11595
11596 bfd_boolean
11597 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
11598 {
11599 flagword old_flags;
11600 flagword new_flags;
11601 bfd_boolean ok;
11602 bfd_boolean null_input_bfd = TRUE;
11603 asection *sec;
11604
11605 /* Check if we have the same endianess */
11606 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
11607 {
11608 (*_bfd_error_handler)
11609 (_("%B: endianness incompatible with that of the selected emulation"),
11610 ibfd);
11611 return FALSE;
11612 }
11613
11614 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
11615 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
11616 return TRUE;
11617
11618 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
11619 {
11620 (*_bfd_error_handler)
11621 (_("%B: ABI is incompatible with that of the selected emulation"),
11622 ibfd);
11623 return FALSE;
11624 }
11625
11626 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
11627 return FALSE;
11628
11629 new_flags = elf_elfheader (ibfd)->e_flags;
11630 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
11631 old_flags = elf_elfheader (obfd)->e_flags;
11632
11633 if (! elf_flags_init (obfd))
11634 {
11635 elf_flags_init (obfd) = TRUE;
11636 elf_elfheader (obfd)->e_flags = new_flags;
11637 elf_elfheader (obfd)->e_ident[EI_CLASS]
11638 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
11639
11640 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
11641 && (bfd_get_arch_info (obfd)->the_default
11642 || mips_mach_extends_p (bfd_get_mach (obfd),
11643 bfd_get_mach (ibfd))))
11644 {
11645 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
11646 bfd_get_mach (ibfd)))
11647 return FALSE;
11648 }
11649
11650 return TRUE;
11651 }
11652
11653 /* Check flag compatibility. */
11654
11655 new_flags &= ~EF_MIPS_NOREORDER;
11656 old_flags &= ~EF_MIPS_NOREORDER;
11657
11658 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
11659 doesn't seem to matter. */
11660 new_flags &= ~EF_MIPS_XGOT;
11661 old_flags &= ~EF_MIPS_XGOT;
11662
11663 /* MIPSpro generates ucode info in n64 objects. Again, we should
11664 just be able to ignore this. */
11665 new_flags &= ~EF_MIPS_UCODE;
11666 old_flags &= ~EF_MIPS_UCODE;
11667
11668 /* Don't care about the PIC flags from dynamic objects; they are
11669 PIC by design. */
11670 if ((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0
11671 && (ibfd->flags & DYNAMIC) != 0)
11672 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
11673
11674 if (new_flags == old_flags)
11675 return TRUE;
11676
11677 /* Check to see if the input BFD actually contains any sections.
11678 If not, its flags may not have been initialised either, but it cannot
11679 actually cause any incompatibility. */
11680 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
11681 {
11682 /* Ignore synthetic sections and empty .text, .data and .bss sections
11683 which are automatically generated by gas. */
11684 if (strcmp (sec->name, ".reginfo")
11685 && strcmp (sec->name, ".mdebug")
11686 && (sec->size != 0
11687 || (strcmp (sec->name, ".text")
11688 && strcmp (sec->name, ".data")
11689 && strcmp (sec->name, ".bss"))))
11690 {
11691 null_input_bfd = FALSE;
11692 break;
11693 }
11694 }
11695 if (null_input_bfd)
11696 return TRUE;
11697
11698 ok = TRUE;
11699
11700 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
11701 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
11702 {
11703 (*_bfd_error_handler)
11704 (_("%B: warning: linking PIC files with non-PIC files"),
11705 ibfd);
11706 ok = TRUE;
11707 }
11708
11709 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
11710 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
11711 if (! (new_flags & EF_MIPS_PIC))
11712 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
11713
11714 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
11715 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
11716
11717 /* Compare the ISAs. */
11718 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
11719 {
11720 (*_bfd_error_handler)
11721 (_("%B: linking 32-bit code with 64-bit code"),
11722 ibfd);
11723 ok = FALSE;
11724 }
11725 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
11726 {
11727 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
11728 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
11729 {
11730 /* Copy the architecture info from IBFD to OBFD. Also copy
11731 the 32-bit flag (if set) so that we continue to recognise
11732 OBFD as a 32-bit binary. */
11733 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
11734 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
11735 elf_elfheader (obfd)->e_flags
11736 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
11737
11738 /* Copy across the ABI flags if OBFD doesn't use them
11739 and if that was what caused us to treat IBFD as 32-bit. */
11740 if ((old_flags & EF_MIPS_ABI) == 0
11741 && mips_32bit_flags_p (new_flags)
11742 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
11743 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
11744 }
11745 else
11746 {
11747 /* The ISAs aren't compatible. */
11748 (*_bfd_error_handler)
11749 (_("%B: linking %s module with previous %s modules"),
11750 ibfd,
11751 bfd_printable_name (ibfd),
11752 bfd_printable_name (obfd));
11753 ok = FALSE;
11754 }
11755 }
11756
11757 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
11758 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
11759
11760 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
11761 does set EI_CLASS differently from any 32-bit ABI. */
11762 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
11763 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
11764 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
11765 {
11766 /* Only error if both are set (to different values). */
11767 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
11768 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
11769 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
11770 {
11771 (*_bfd_error_handler)
11772 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
11773 ibfd,
11774 elf_mips_abi_name (ibfd),
11775 elf_mips_abi_name (obfd));
11776 ok = FALSE;
11777 }
11778 new_flags &= ~EF_MIPS_ABI;
11779 old_flags &= ~EF_MIPS_ABI;
11780 }
11781
11782 /* For now, allow arbitrary mixing of ASEs (retain the union). */
11783 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
11784 {
11785 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
11786
11787 new_flags &= ~ EF_MIPS_ARCH_ASE;
11788 old_flags &= ~ EF_MIPS_ARCH_ASE;
11789 }
11790
11791 /* Warn about any other mismatches */
11792 if (new_flags != old_flags)
11793 {
11794 (*_bfd_error_handler)
11795 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
11796 ibfd, (unsigned long) new_flags,
11797 (unsigned long) old_flags);
11798 ok = FALSE;
11799 }
11800
11801 if (! ok)
11802 {
11803 bfd_set_error (bfd_error_bad_value);
11804 return FALSE;
11805 }
11806
11807 return TRUE;
11808 }
11809
11810 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
11811
11812 bfd_boolean
11813 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
11814 {
11815 BFD_ASSERT (!elf_flags_init (abfd)
11816 || elf_elfheader (abfd)->e_flags == flags);
11817
11818 elf_elfheader (abfd)->e_flags = flags;
11819 elf_flags_init (abfd) = TRUE;
11820 return TRUE;
11821 }
11822
11823 char *
11824 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
11825 {
11826 switch (dtag)
11827 {
11828 default: return "";
11829 case DT_MIPS_RLD_VERSION:
11830 return "MIPS_RLD_VERSION";
11831 case DT_MIPS_TIME_STAMP:
11832 return "MIPS_TIME_STAMP";
11833 case DT_MIPS_ICHECKSUM:
11834 return "MIPS_ICHECKSUM";
11835 case DT_MIPS_IVERSION:
11836 return "MIPS_IVERSION";
11837 case DT_MIPS_FLAGS:
11838 return "MIPS_FLAGS";
11839 case DT_MIPS_BASE_ADDRESS:
11840 return "MIPS_BASE_ADDRESS";
11841 case DT_MIPS_MSYM:
11842 return "MIPS_MSYM";
11843 case DT_MIPS_CONFLICT:
11844 return "MIPS_CONFLICT";
11845 case DT_MIPS_LIBLIST:
11846 return "MIPS_LIBLIST";
11847 case DT_MIPS_LOCAL_GOTNO:
11848 return "MIPS_LOCAL_GOTNO";
11849 case DT_MIPS_CONFLICTNO:
11850 return "MIPS_CONFLICTNO";
11851 case DT_MIPS_LIBLISTNO:
11852 return "MIPS_LIBLISTNO";
11853 case DT_MIPS_SYMTABNO:
11854 return "MIPS_SYMTABNO";
11855 case DT_MIPS_UNREFEXTNO:
11856 return "MIPS_UNREFEXTNO";
11857 case DT_MIPS_GOTSYM:
11858 return "MIPS_GOTSYM";
11859 case DT_MIPS_HIPAGENO:
11860 return "MIPS_HIPAGENO";
11861 case DT_MIPS_RLD_MAP:
11862 return "MIPS_RLD_MAP";
11863 case DT_MIPS_DELTA_CLASS:
11864 return "MIPS_DELTA_CLASS";
11865 case DT_MIPS_DELTA_CLASS_NO:
11866 return "MIPS_DELTA_CLASS_NO";
11867 case DT_MIPS_DELTA_INSTANCE:
11868 return "MIPS_DELTA_INSTANCE";
11869 case DT_MIPS_DELTA_INSTANCE_NO:
11870 return "MIPS_DELTA_INSTANCE_NO";
11871 case DT_MIPS_DELTA_RELOC:
11872 return "MIPS_DELTA_RELOC";
11873 case DT_MIPS_DELTA_RELOC_NO:
11874 return "MIPS_DELTA_RELOC_NO";
11875 case DT_MIPS_DELTA_SYM:
11876 return "MIPS_DELTA_SYM";
11877 case DT_MIPS_DELTA_SYM_NO:
11878 return "MIPS_DELTA_SYM_NO";
11879 case DT_MIPS_DELTA_CLASSSYM:
11880 return "MIPS_DELTA_CLASSSYM";
11881 case DT_MIPS_DELTA_CLASSSYM_NO:
11882 return "MIPS_DELTA_CLASSSYM_NO";
11883 case DT_MIPS_CXX_FLAGS:
11884 return "MIPS_CXX_FLAGS";
11885 case DT_MIPS_PIXIE_INIT:
11886 return "MIPS_PIXIE_INIT";
11887 case DT_MIPS_SYMBOL_LIB:
11888 return "MIPS_SYMBOL_LIB";
11889 case DT_MIPS_LOCALPAGE_GOTIDX:
11890 return "MIPS_LOCALPAGE_GOTIDX";
11891 case DT_MIPS_LOCAL_GOTIDX:
11892 return "MIPS_LOCAL_GOTIDX";
11893 case DT_MIPS_HIDDEN_GOTIDX:
11894 return "MIPS_HIDDEN_GOTIDX";
11895 case DT_MIPS_PROTECTED_GOTIDX:
11896 return "MIPS_PROTECTED_GOT_IDX";
11897 case DT_MIPS_OPTIONS:
11898 return "MIPS_OPTIONS";
11899 case DT_MIPS_INTERFACE:
11900 return "MIPS_INTERFACE";
11901 case DT_MIPS_DYNSTR_ALIGN:
11902 return "DT_MIPS_DYNSTR_ALIGN";
11903 case DT_MIPS_INTERFACE_SIZE:
11904 return "DT_MIPS_INTERFACE_SIZE";
11905 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
11906 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
11907 case DT_MIPS_PERF_SUFFIX:
11908 return "DT_MIPS_PERF_SUFFIX";
11909 case DT_MIPS_COMPACT_SIZE:
11910 return "DT_MIPS_COMPACT_SIZE";
11911 case DT_MIPS_GP_VALUE:
11912 return "DT_MIPS_GP_VALUE";
11913 case DT_MIPS_AUX_DYNAMIC:
11914 return "DT_MIPS_AUX_DYNAMIC";
11915 }
11916 }
11917
11918 bfd_boolean
11919 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
11920 {
11921 FILE *file = ptr;
11922
11923 BFD_ASSERT (abfd != NULL && ptr != NULL);
11924
11925 /* Print normal ELF private data. */
11926 _bfd_elf_print_private_bfd_data (abfd, ptr);
11927
11928 /* xgettext:c-format */
11929 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
11930
11931 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
11932 fprintf (file, _(" [abi=O32]"));
11933 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
11934 fprintf (file, _(" [abi=O64]"));
11935 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
11936 fprintf (file, _(" [abi=EABI32]"));
11937 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
11938 fprintf (file, _(" [abi=EABI64]"));
11939 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
11940 fprintf (file, _(" [abi unknown]"));
11941 else if (ABI_N32_P (abfd))
11942 fprintf (file, _(" [abi=N32]"));
11943 else if (ABI_64_P (abfd))
11944 fprintf (file, _(" [abi=64]"));
11945 else
11946 fprintf (file, _(" [no abi set]"));
11947
11948 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
11949 fprintf (file, " [mips1]");
11950 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
11951 fprintf (file, " [mips2]");
11952 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
11953 fprintf (file, " [mips3]");
11954 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
11955 fprintf (file, " [mips4]");
11956 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
11957 fprintf (file, " [mips5]");
11958 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
11959 fprintf (file, " [mips32]");
11960 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
11961 fprintf (file, " [mips64]");
11962 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
11963 fprintf (file, " [mips32r2]");
11964 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
11965 fprintf (file, " [mips64r2]");
11966 else
11967 fprintf (file, _(" [unknown ISA]"));
11968
11969 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
11970 fprintf (file, " [mdmx]");
11971
11972 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
11973 fprintf (file, " [mips16]");
11974
11975 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
11976 fprintf (file, " [32bitmode]");
11977 else
11978 fprintf (file, _(" [not 32bitmode]"));
11979
11980 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
11981 fprintf (file, " [noreorder]");
11982
11983 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
11984 fprintf (file, " [PIC]");
11985
11986 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
11987 fprintf (file, " [CPIC]");
11988
11989 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
11990 fprintf (file, " [XGOT]");
11991
11992 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
11993 fprintf (file, " [UCODE]");
11994
11995 fputc ('\n', file);
11996
11997 return TRUE;
11998 }
11999
12000 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
12001 {
12002 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12003 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12004 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
12005 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12006 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12007 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
12008 { NULL, 0, 0, 0, 0 }
12009 };
12010
12011 /* Merge non visibility st_other attributes. Ensure that the
12012 STO_OPTIONAL flag is copied into h->other, even if this is not a
12013 definiton of the symbol. */
12014 void
12015 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
12016 const Elf_Internal_Sym *isym,
12017 bfd_boolean definition,
12018 bfd_boolean dynamic ATTRIBUTE_UNUSED)
12019 {
12020 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
12021 {
12022 unsigned char other;
12023
12024 other = (definition ? isym->st_other : h->other);
12025 other &= ~ELF_ST_VISIBILITY (-1);
12026 h->other = other | ELF_ST_VISIBILITY (h->other);
12027 }
12028
12029 if (!definition
12030 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
12031 h->other |= STO_OPTIONAL;
12032 }
12033
12034 /* Decide whether an undefined symbol is special and can be ignored.
12035 This is the case for OPTIONAL symbols on IRIX. */
12036 bfd_boolean
12037 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
12038 {
12039 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
12040 }
12041
12042 bfd_boolean
12043 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
12044 {
12045 return (sym->st_shndx == SHN_COMMON
12046 || sym->st_shndx == SHN_MIPS_ACOMMON
12047 || sym->st_shndx == SHN_MIPS_SCOMMON);
12048 }
This page took 0.300898 seconds and 4 git commands to generate.