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