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