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