Coverage Report

Created: 2024-05-21 06:29

/src/binutils-gdb/bfd/elfxx-mips.c
Line
Count
Source (jump to first uncovered line)
1
/* MIPS-specific support for ELF
2
   Copyright (C) 1993-2024 Free Software Foundation, Inc.
3
4
   Most of the information added by Ian Lance Taylor, Cygnus Support,
5
   <ian@cygnus.com>.
6
   N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
7
   <mark@codesourcery.com>
8
   Traditional MIPS targets support added by Koundinya.K, Dansk Data
9
   Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
10
11
   This file is part of BFD, the Binary File Descriptor library.
12
13
   This program is free software; you can redistribute it and/or modify
14
   it under the terms of the GNU General Public License as published by
15
   the Free Software Foundation; either version 3 of the License, or
16
   (at your option) any later version.
17
18
   This program is distributed in the hope that it will be useful,
19
   but WITHOUT ANY WARRANTY; without even the implied warranty of
20
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
   GNU General Public License for more details.
22
23
   You should have received a copy of the GNU General Public License
24
   along with this program; if not, write to the Free Software
25
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
26
   MA 02110-1301, USA.  */
27
28
29
/* This file handles functionality common to the different MIPS ABI's.  */
30
31
#include "sysdep.h"
32
#include "bfd.h"
33
#include "libbfd.h"
34
#include "libiberty.h"
35
#include "elf-bfd.h"
36
#include "ecoff-bfd.h"
37
#include "elfxx-mips.h"
38
#include "elf/mips.h"
39
#include "elf-vxworks.h"
40
#include "dwarf2.h"
41
42
/* Get the ECOFF swapping routines.  */
43
#include "coff/sym.h"
44
#include "coff/symconst.h"
45
#include "coff/ecoff.h"
46
#include "coff/mips.h"
47
48
#include "hashtab.h"
49
50
/* Types of TLS GOT entry.  */
51
enum mips_got_tls_type {
52
  GOT_TLS_NONE,
53
  GOT_TLS_GD,
54
  GOT_TLS_LDM,
55
  GOT_TLS_IE
56
};
57
58
/* This structure is used to hold information about one GOT entry.
59
   There are four types of entry:
60
61
      (1) an absolute address
62
      requires: abfd == NULL
63
      fields: d.address
64
65
      (2) a SYMBOL + OFFSET address, where SYMBOL is local to an input bfd
66
      requires: abfd != NULL, symndx >= 0, tls_type != GOT_TLS_LDM
67
      fields: abfd, symndx, d.addend, tls_type
68
69
      (3) a SYMBOL address, where SYMBOL is not local to an input bfd
70
      requires: abfd != NULL, symndx == -1
71
      fields: d.h, tls_type
72
73
      (4) a TLS LDM slot
74
      requires: abfd != NULL, symndx == 0, tls_type == GOT_TLS_LDM
75
      fields: none; there's only one of these per GOT.  */
76
struct mips_got_entry
77
{
78
  /* One input bfd that needs the GOT entry.  */
79
  bfd *abfd;
80
  /* The index of the symbol, as stored in the relocation r_info, if
81
     we have a local symbol; -1 otherwise.  */
82
  long symndx;
83
  union
84
  {
85
    /* If abfd == NULL, an address that must be stored in the got.  */
86
    bfd_vma address;
87
    /* If abfd != NULL && symndx != -1, the addend of the relocation
88
       that should be added to the symbol value.  */
89
    bfd_vma addend;
90
    /* If abfd != NULL && symndx == -1, the hash table entry
91
       corresponding to a symbol in the GOT.  The symbol's entry
92
       is in the local area if h->global_got_area is GGA_NONE,
93
       otherwise it is in the global area.  */
94
    struct mips_elf_link_hash_entry *h;
95
  } d;
96
97
  /* The TLS type of this GOT entry.  An LDM GOT entry will be a local
98
     symbol entry with r_symndx == 0.  */
99
  unsigned char tls_type;
100
101
  /* True if we have filled in the GOT contents for a TLS entry,
102
     and created the associated relocations.  */
103
  unsigned char tls_initialized;
104
105
  /* The offset from the beginning of the .got section to the entry
106
     corresponding to this symbol+addend.  If it's a global symbol
107
     whose offset is yet to be decided, it's going to be -1.  */
108
  long gotidx;
109
};
110
111
/* This structure represents a GOT page reference from an input bfd.
112
   Each instance represents a symbol + ADDEND, where the representation
113
   of the symbol depends on whether it is local to the input bfd.
114
   If it is, then SYMNDX >= 0, and the symbol has index SYMNDX in U.ABFD.
115
   Otherwise, SYMNDX < 0 and U.H points to the symbol's hash table entry.
116
117
   Page references with SYMNDX >= 0 always become page references
118
   in the output.  Page references with SYMNDX < 0 only become page
119
   references if the symbol binds locally; in other cases, the page
120
   reference decays to a global GOT reference.  */
121
struct mips_got_page_ref
122
{
123
  long symndx;
124
  union
125
  {
126
    struct mips_elf_link_hash_entry *h;
127
    bfd *abfd;
128
  } u;
129
  bfd_vma addend;
130
};
131
132
/* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
133
   The structures form a non-overlapping list that is sorted by increasing
134
   MIN_ADDEND.  */
135
struct mips_got_page_range
136
{
137
  struct mips_got_page_range *next;
138
  bfd_signed_vma min_addend;
139
  bfd_signed_vma max_addend;
140
};
141
142
/* This structure describes the range of addends that are applied to page
143
   relocations against a given section.  */
144
struct mips_got_page_entry
145
{
146
  /* The section that these entries are based on.  */
147
  asection *sec;
148
  /* The ranges for this page entry.  */
149
  struct mips_got_page_range *ranges;
150
  /* The maximum number of page entries needed for RANGES.  */
151
  bfd_vma num_pages;
152
};
153
154
/* This structure is used to hold .got information when linking.  */
155
156
struct mips_got_info
157
{
158
  /* The number of global .got entries.  */
159
  unsigned int global_gotno;
160
  /* The number of global .got entries that are in the GGA_RELOC_ONLY area.  */
161
  unsigned int reloc_only_gotno;
162
  /* The number of .got slots used for TLS.  */
163
  unsigned int tls_gotno;
164
  /* The first unused TLS .got entry.  Used only during
165
     mips_elf_initialize_tls_index.  */
166
  unsigned int tls_assigned_gotno;
167
  /* The number of local .got entries, eventually including page entries.  */
168
  unsigned int local_gotno;
169
  /* The maximum number of page entries needed.  */
170
  unsigned int page_gotno;
171
  /* The number of relocations needed for the GOT entries.  */
172
  unsigned int relocs;
173
  /* The first unused local .got entry.  */
174
  unsigned int assigned_low_gotno;
175
  /* The last unused local .got entry.  */
176
  unsigned int assigned_high_gotno;
177
  /* A hash table holding members of the got.  */
178
  struct htab *got_entries;
179
  /* A hash table holding mips_got_page_ref structures.  */
180
  struct htab *got_page_refs;
181
  /* A hash table of mips_got_page_entry structures.  */
182
  struct htab *got_page_entries;
183
  /* In multi-got links, a pointer to the next got (err, rather, most
184
     of the time, it points to the previous got).  */
185
  struct mips_got_info *next;
186
};
187
188
/* Structure passed when merging bfds' gots.  */
189
190
struct mips_elf_got_per_bfd_arg
191
{
192
  /* The output bfd.  */
193
  bfd *obfd;
194
  /* The link information.  */
195
  struct bfd_link_info *info;
196
  /* A pointer to the primary got, i.e., the one that's going to get
197
     the implicit relocations from DT_MIPS_LOCAL_GOTNO and
198
     DT_MIPS_GOTSYM.  */
199
  struct mips_got_info *primary;
200
  /* A non-primary got we're trying to merge with other input bfd's
201
     gots.  */
202
  struct mips_got_info *current;
203
  /* The maximum number of got entries that can be addressed with a
204
     16-bit offset.  */
205
  unsigned int max_count;
206
  /* The maximum number of page entries needed by each got.  */
207
  unsigned int max_pages;
208
  /* The total number of global entries which will live in the
209
     primary got and be automatically relocated.  This includes
210
     those not referenced by the primary GOT but included in
211
     the "master" GOT.  */
212
  unsigned int global_count;
213
};
214
215
/* A structure used to pass information to htab_traverse callbacks
216
   when laying out the GOT.  */
217
218
struct mips_elf_traverse_got_arg
219
{
220
  struct bfd_link_info *info;
221
  struct mips_got_info *g;
222
  int value;
223
};
224
225
struct _mips_elf_section_data
226
{
227
  struct bfd_elf_section_data elf;
228
  union
229
  {
230
    bfd_byte *tdata;
231
  } u;
232
};
233
234
#define mips_elf_section_data(sec) \
235
0
  ((struct _mips_elf_section_data *) elf_section_data (sec))
236
237
#define is_mips_elf(bfd)        \
238
0
  (bfd_get_flavour (bfd) == bfd_target_elf_flavour  \
239
0
   && elf_tdata (bfd) != NULL        \
240
0
   && elf_object_id (bfd) == MIPS_ELF_DATA)
241
242
/* The ABI says that every symbol used by dynamic relocations must have
243
   a global GOT entry.  Among other things, this provides the dynamic
244
   linker with a free, directly-indexed cache.  The GOT can therefore
245
   contain symbols that are not referenced by GOT relocations themselves
246
   (in other words, it may have symbols that are not referenced by things
247
   like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
248
249
   GOT relocations are less likely to overflow if we put the associated
250
   GOT entries towards the beginning.  We therefore divide the global
251
   GOT entries into two areas: "normal" and "reloc-only".  Entries in
252
   the first area can be used for both dynamic relocations and GP-relative
253
   accesses, while those in the "reloc-only" area are for dynamic
254
   relocations only.
255
256
   These GGA_* ("Global GOT Area") values are organised so that lower
257
   values are more general than higher values.  Also, non-GGA_NONE
258
   values are ordered by the position of the area in the GOT.  */
259
0
#define GGA_NORMAL 0
260
0
#define GGA_RELOC_ONLY 1
261
0
#define GGA_NONE 2
262
263
/* Information about a non-PIC interface to a PIC function.  There are
264
   two ways of creating these interfaces.  The first is to add:
265
266
  lui $25,%hi(func)
267
  addiu $25,$25,%lo(func)
268
269
   immediately before a PIC function "func".  The second is to add:
270
271
  lui $25,%hi(func)
272
  j func
273
  addiu $25,$25,%lo(func)
274
275
   to a separate trampoline section.
276
277
   Stubs of the first kind go in a new section immediately before the
278
   target function.  Stubs of the second kind go in a single section
279
   pointed to by the hash table's "strampoline" field.  */
280
struct mips_elf_la25_stub {
281
  /* The generated section that contains this stub.  */
282
  asection *stub_section;
283
284
  /* The offset of the stub from the start of STUB_SECTION.  */
285
  bfd_vma offset;
286
287
  /* One symbol for the original function.  Its location is available
288
     in H->root.root.u.def.  */
289
  struct mips_elf_link_hash_entry *h;
290
};
291
292
/* Macros for populating a mips_elf_la25_stub.  */
293
294
#define LA25_LUI(VAL) (0x3c190000 | (VAL))  /* lui t9,VAL */
295
#define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
296
#define LA25_BC(VAL) (0xc8000000 | (((VAL) >> 2) & 0x3ffffff)) /* bc VAL */
297
#define LA25_ADDIU(VAL) (0x27390000 | (VAL))  /* addiu t9,t9,VAL */
298
#define LA25_LUI_MICROMIPS(VAL)           \
299
0
  (0x41b90000 | (VAL))        /* lui t9,VAL */
300
#define LA25_J_MICROMIPS(VAL)           \
301
0
  (0xd4000000 | (((VAL) >> 1) & 0x3ffffff))  /* j VAL */
302
#define LA25_ADDIU_MICROMIPS(VAL)         \
303
0
  (0x33390000 | (VAL))        /* addiu t9,t9,VAL */
304
305
/* This structure is passed to mips_elf_sort_hash_table_f when sorting
306
   the dynamic symbols.  */
307
308
struct mips_elf_hash_sort_data
309
{
310
  /* The symbol in the global GOT with the lowest dynamic symbol table
311
     index.  */
312
  struct elf_link_hash_entry *low;
313
  /* The least dynamic symbol table index corresponding to a non-TLS
314
     symbol with a GOT entry.  */
315
  bfd_size_type min_got_dynindx;
316
  /* The greatest dynamic symbol table index corresponding to a symbol
317
     with a GOT entry that is not referenced (e.g., a dynamic symbol
318
     with dynamic relocations pointing to it from non-primary GOTs).  */
319
  bfd_size_type max_unref_got_dynindx;
320
  /* The greatest dynamic symbol table index corresponding to a local
321
     symbol.  */
322
  bfd_size_type max_local_dynindx;
323
  /* The greatest dynamic symbol table index corresponding to an external
324
     symbol without a GOT entry.  */
325
  bfd_size_type max_non_got_dynindx;
326
  /* If non-NULL, output BFD for .MIPS.xhash finalization.  */
327
  bfd *output_bfd;
328
  /* If non-NULL, pointer to contents of .MIPS.xhash for filling in
329
     real final dynindx.  */
330
  bfd_byte *mipsxhash;
331
};
332
333
/* We make up to two PLT entries if needed, one for standard MIPS code
334
   and one for compressed code, either a MIPS16 or microMIPS one.  We
335
   keep a separate record of traditional lazy-binding stubs, for easier
336
   processing.  */
337
338
struct plt_entry
339
{
340
  /* Traditional SVR4 stub offset, or -1 if none.  */
341
  bfd_vma stub_offset;
342
343
  /* Standard PLT entry offset, or -1 if none.  */
344
  bfd_vma mips_offset;
345
346
  /* Compressed PLT entry offset, or -1 if none.  */
347
  bfd_vma comp_offset;
348
349
  /* The corresponding .got.plt index, or -1 if none.  */
350
  bfd_vma gotplt_index;
351
352
  /* Whether we need a standard PLT entry.  */
353
  unsigned int need_mips : 1;
354
355
  /* Whether we need a compressed PLT entry.  */
356
  unsigned int need_comp : 1;
357
};
358
359
/* The MIPS ELF linker needs additional information for each symbol in
360
   the global hash table.  */
361
362
struct mips_elf_link_hash_entry
363
{
364
  struct elf_link_hash_entry root;
365
366
  /* External symbol information.  */
367
  EXTR esym;
368
369
  /* The la25 stub we have created for ths symbol, if any.  */
370
  struct mips_elf_la25_stub *la25_stub;
371
372
  /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
373
     this symbol.  */
374
  unsigned int possibly_dynamic_relocs;
375
376
  /* If there is a stub that 32 bit functions should use to call this
377
     16 bit function, this points to the section containing the stub.  */
378
  asection *fn_stub;
379
380
  /* If there is a stub that 16 bit functions should use to call this
381
     32 bit function, this points to the section containing the stub.  */
382
  asection *call_stub;
383
384
  /* This is like the call_stub field, but it is used if the function
385
     being called returns a floating point value.  */
386
  asection *call_fp_stub;
387
388
  /* If non-zero, location in .MIPS.xhash to write real final dynindx.  */
389
  bfd_vma mipsxhash_loc;
390
391
  /* The highest GGA_* value that satisfies all references to this symbol.  */
392
  unsigned int global_got_area : 2;
393
394
  /* True if all GOT relocations against this symbol are for calls.  This is
395
     a looser condition than no_fn_stub below, because there may be other
396
     non-call non-GOT relocations against the symbol.  */
397
  unsigned int got_only_for_calls : 1;
398
399
  /* True if one of the relocations described by possibly_dynamic_relocs
400
     is against a readonly section.  */
401
  unsigned int readonly_reloc : 1;
402
403
  /* True if there is a relocation against this symbol that must be
404
     resolved by the static linker (in other words, if the relocation
405
     cannot possibly be made dynamic).  */
406
  unsigned int has_static_relocs : 1;
407
408
  /* True if we must not create a .MIPS.stubs entry for this symbol.
409
     This is set, for example, if there are relocations related to
410
     taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
411
     See "MIPS ABI Supplement, 3rd Edition", p. 4-20.  */
412
  unsigned int no_fn_stub : 1;
413
414
  /* Whether we need the fn_stub; this is true if this symbol appears
415
     in any relocs other than a 16 bit call.  */
416
  unsigned int need_fn_stub : 1;
417
418
  /* True if this symbol is referenced by branch relocations from
419
     any non-PIC input file.  This is used to determine whether an
420
     la25 stub is required.  */
421
  unsigned int has_nonpic_branches : 1;
422
423
  /* Does this symbol need a traditional MIPS lazy-binding stub
424
     (as opposed to a PLT entry)?  */
425
  unsigned int needs_lazy_stub : 1;
426
427
  /* Does this symbol resolve to a PLT entry?  */
428
  unsigned int use_plt_entry : 1;
429
};
430
431
/* MIPS ELF linker hash table.  */
432
433
struct mips_elf_link_hash_table
434
{
435
  struct elf_link_hash_table root;
436
437
  /* The number of .rtproc entries.  */
438
  bfd_size_type procedure_count;
439
440
  /* The size of the .compact_rel section (if SGI_COMPAT).  */
441
  bfd_size_type compact_rel_size;
442
443
  /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
444
     is set to the address of __rld_obj_head as in IRIX5 and IRIX6.  */
445
  bool use_rld_obj_head;
446
447
  /* The  __rld_map or __rld_obj_head symbol. */
448
  struct elf_link_hash_entry *rld_symbol;
449
450
  /* This is set if we see any mips16 stub sections.  */
451
  bool mips16_stubs_seen;
452
453
  /* True if we can generate copy relocs and PLTs.  */
454
  bool use_plts_and_copy_relocs;
455
456
  /* True if we can only use 32-bit microMIPS instructions.  */
457
  bool insn32;
458
459
  /* True if we suppress checks for invalid branches between ISA modes.  */
460
  bool ignore_branch_isa;
461
462
  /* True if we are targetting R6 compact branches.  */
463
  bool compact_branches;
464
465
  /* True if we already reported the small-data section overflow.  */
466
  bool small_data_overflow_reported;
467
468
  /* True if we use the special `__gnu_absolute_zero' symbol.  */
469
  bool use_absolute_zero;
470
471
  /* True if we have been configured for a GNU target.  */
472
  bool gnu_target;
473
474
  /* Shortcuts to some dynamic sections, or NULL if they are not
475
     being used.  */
476
  asection *srelplt2;
477
  asection *sstubs;
478
479
  /* The master GOT information.  */
480
  struct mips_got_info *got_info;
481
482
  /* The global symbol in the GOT with the lowest index in the dynamic
483
     symbol table.  */
484
  struct elf_link_hash_entry *global_gotsym;
485
486
  /* The size of the PLT header in bytes.  */
487
  bfd_vma plt_header_size;
488
489
  /* The size of a standard PLT entry in bytes.  */
490
  bfd_vma plt_mips_entry_size;
491
492
  /* The size of a compressed PLT entry in bytes.  */
493
  bfd_vma plt_comp_entry_size;
494
495
  /* The offset of the next standard PLT entry to create.  */
496
  bfd_vma plt_mips_offset;
497
498
  /* The offset of the next compressed PLT entry to create.  */
499
  bfd_vma plt_comp_offset;
500
501
  /* The index of the next .got.plt entry to create.  */
502
  bfd_vma plt_got_index;
503
504
  /* The number of functions that need a lazy-binding stub.  */
505
  bfd_vma lazy_stub_count;
506
507
  /* The size of a function stub entry in bytes.  */
508
  bfd_vma function_stub_size;
509
510
  /* The number of reserved entries at the beginning of the GOT.  */
511
  unsigned int reserved_gotno;
512
513
  /* The section used for mips_elf_la25_stub trampolines.
514
     See the comment above that structure for details.  */
515
  asection *strampoline;
516
517
  /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
518
     pairs.  */
519
  htab_t la25_stubs;
520
521
  /* A function FN (NAME, IS, OS) that creates a new input section
522
     called NAME and links it to output section OS.  If IS is nonnull,
523
     the new section should go immediately before it, otherwise it
524
     should go at the (current) beginning of OS.
525
526
     The function returns the new section on success, otherwise it
527
     returns null.  */
528
  asection *(*add_stub_section) (const char *, asection *, asection *);
529
530
  /* Is the PLT header compressed?  */
531
  unsigned int plt_header_is_comp : 1;
532
};
533
534
/* Get the MIPS ELF linker hash table from a link_info structure.  */
535
536
#define mips_elf_hash_table(p) \
537
0
  ((is_elf_hash_table ((p)->hash)          \
538
0
    && elf_hash_table_id (elf_hash_table (p)) == MIPS_ELF_DATA)   \
539
0
   ? (struct mips_elf_link_hash_table *) (p)->hash : NULL)
540
541
/* A structure used to communicate with htab_traverse callbacks.  */
542
struct mips_htab_traverse_info
543
{
544
  /* The usual link-wide information.  */
545
  struct bfd_link_info *info;
546
  bfd *output_bfd;
547
548
  /* Starts off FALSE and is set to TRUE if the link should be aborted.  */
549
  bool error;
550
};
551
552
/* Used to store a REL high-part relocation such as R_MIPS_HI16 or
553
   R_MIPS_GOT16.  REL is the relocation, INPUT_SECTION is the section
554
   that contains the relocation field and DATA points to the start of
555
   INPUT_SECTION.  */
556
557
struct mips_hi16
558
{
559
  struct mips_hi16 *next;
560
  bfd_byte *data;
561
  asection *input_section;
562
  arelent rel;
563
};
564
565
/* MIPS ELF private object data.  */
566
567
struct mips_elf_obj_tdata
568
{
569
  /* Generic ELF private object data.  */
570
  struct elf_obj_tdata root;
571
572
  /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output.  */
573
  bfd *abi_fp_bfd;
574
575
  /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output.  */
576
  bfd *abi_msa_bfd;
577
578
  /* The abiflags for this object.  */
579
  Elf_Internal_ABIFlags_v0 abiflags;
580
  bool abiflags_valid;
581
582
  /* The GOT requirements of input bfds.  */
583
  struct mips_got_info *got;
584
585
  /* Used by _bfd_mips_elf_find_nearest_line.  The structure could be
586
     included directly in this one, but there's no point to wasting
587
     the memory just for the infrequently called find_nearest_line.  */
588
  struct mips_elf_find_line *find_line_info;
589
590
  /* An array of stub sections indexed by symbol number.  */
591
  asection **local_stubs;
592
  asection **local_call_stubs;
593
594
  /* The Irix 5 support uses two virtual sections, which represent
595
     text/data symbols defined in dynamic objects.  */
596
  asymbol *elf_data_symbol;
597
  asymbol *elf_text_symbol;
598
  asection *elf_data_section;
599
  asection *elf_text_section;
600
601
  struct mips_hi16 *mips_hi16_list;
602
};
603
604
/* Get MIPS ELF private object data from BFD's tdata.  */
605
606
#define mips_elf_tdata(bfd) \
607
631k
  ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
608
609
#define TLS_RELOC_P(r_type) \
610
0
  (r_type == R_MIPS_TLS_DTPMOD32    \
611
0
   || r_type == R_MIPS_TLS_DTPMOD64    \
612
0
   || r_type == R_MIPS_TLS_DTPREL32    \
613
0
   || r_type == R_MIPS_TLS_DTPREL64    \
614
0
   || r_type == R_MIPS_TLS_GD      \
615
0
   || r_type == R_MIPS_TLS_LDM      \
616
0
   || r_type == R_MIPS_TLS_DTPREL_HI16    \
617
0
   || r_type == R_MIPS_TLS_DTPREL_LO16    \
618
0
   || r_type == R_MIPS_TLS_GOTTPREL    \
619
0
   || r_type == R_MIPS_TLS_TPREL32    \
620
0
   || r_type == R_MIPS_TLS_TPREL64    \
621
0
   || r_type == R_MIPS_TLS_TPREL_HI16    \
622
0
   || r_type == R_MIPS_TLS_TPREL_LO16    \
623
0
   || r_type == R_MIPS16_TLS_GD      \
624
0
   || r_type == R_MIPS16_TLS_LDM    \
625
0
   || r_type == R_MIPS16_TLS_DTPREL_HI16  \
626
0
   || r_type == R_MIPS16_TLS_DTPREL_LO16  \
627
0
   || r_type == R_MIPS16_TLS_GOTTPREL    \
628
0
   || r_type == R_MIPS16_TLS_TPREL_HI16    \
629
0
   || r_type == R_MIPS16_TLS_TPREL_LO16    \
630
0
   || r_type == R_MICROMIPS_TLS_GD    \
631
0
   || r_type == R_MICROMIPS_TLS_LDM    \
632
0
   || r_type == R_MICROMIPS_TLS_DTPREL_HI16  \
633
0
   || r_type == R_MICROMIPS_TLS_DTPREL_LO16  \
634
0
   || r_type == R_MICROMIPS_TLS_GOTTPREL  \
635
0
   || r_type == R_MICROMIPS_TLS_TPREL_HI16  \
636
0
   || r_type == R_MICROMIPS_TLS_TPREL_LO16)
637
638
/* Structure used to pass information to mips_elf_output_extsym.  */
639
640
struct extsym_info
641
{
642
  bfd *abfd;
643
  struct bfd_link_info *info;
644
  struct ecoff_debug_info *debug;
645
  const struct ecoff_debug_swap *swap;
646
  bool failed;
647
};
648
649
/* The names of the runtime procedure table symbols used on IRIX5.  */
650
651
static const char * const mips_elf_dynsym_rtproc_names[] =
652
{
653
  "_procedure_table",
654
  "_procedure_string_table",
655
  "_procedure_table_size",
656
  NULL
657
};
658
659
/* These structures are used to generate the .compact_rel section on
660
   IRIX5.  */
661
662
typedef struct
663
{
664
  unsigned long id1;    /* Always one?  */
665
  unsigned long num;    /* Number of compact relocation entries.  */
666
  unsigned long id2;    /* Always two?  */
667
  unsigned long offset;   /* The file offset of the first relocation.  */
668
  unsigned long reserved0;  /* Zero?  */
669
  unsigned long reserved1;  /* Zero?  */
670
} Elf32_compact_rel;
671
672
typedef struct
673
{
674
  bfd_byte id1[4];
675
  bfd_byte num[4];
676
  bfd_byte id2[4];
677
  bfd_byte offset[4];
678
  bfd_byte reserved0[4];
679
  bfd_byte reserved1[4];
680
} Elf32_External_compact_rel;
681
682
typedef struct
683
{
684
  unsigned int ctype : 1; /* 1: long 0: short format. See below.  */
685
  unsigned int rtype : 4; /* Relocation types. See below.  */
686
  unsigned int dist2to : 8;
687
  unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
688
  unsigned long konst;    /* KONST field. See below.  */
689
  unsigned long vaddr;    /* VADDR to be relocated.  */
690
} Elf32_crinfo;
691
692
typedef struct
693
{
694
  unsigned int ctype : 1; /* 1: long 0: short format. See below.  */
695
  unsigned int rtype : 4; /* Relocation types. See below.  */
696
  unsigned int dist2to : 8;
697
  unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
698
  unsigned long konst;    /* KONST field. See below.  */
699
} Elf32_crinfo2;
700
701
typedef struct
702
{
703
  bfd_byte info[4];
704
  bfd_byte konst[4];
705
  bfd_byte vaddr[4];
706
} Elf32_External_crinfo;
707
708
typedef struct
709
{
710
  bfd_byte info[4];
711
  bfd_byte konst[4];
712
} Elf32_External_crinfo2;
713
714
/* These are the constants used to swap the bitfields in a crinfo.  */
715
716
0
#define CRINFO_CTYPE (0x1U)
717
0
#define CRINFO_CTYPE_SH (31)
718
0
#define CRINFO_RTYPE (0xfU)
719
0
#define CRINFO_RTYPE_SH (27)
720
0
#define CRINFO_DIST2TO (0xffU)
721
0
#define CRINFO_DIST2TO_SH (19)
722
0
#define CRINFO_RELVADDR (0x7ffffU)
723
0
#define CRINFO_RELVADDR_SH (0)
724
725
/* A compact relocation info has long (3 words) or short (2 words)
726
   formats.  A short format doesn't have VADDR field and relvaddr
727
   fields contains ((VADDR - vaddr of the previous entry) >> 2).  */
728
#define CRF_MIPS_LONG     1
729
#define CRF_MIPS_SHORT      0
730
731
/* There are 4 types of compact relocation at least. The value KONST
732
   has different meaning for each type:
733
734
   (type)   (konst)
735
   CT_MIPS_REL32  Address in data
736
   CT_MIPS_WORD   Address in word (XXX)
737
   CT_MIPS_GPHI_LO  GP - vaddr
738
   CT_MIPS_JMPAD  Address to jump
739
   */
740
741
#define CRT_MIPS_REL32      0xa
742
#define CRT_MIPS_WORD     0xb
743
#define CRT_MIPS_GPHI_LO    0xc
744
#define CRT_MIPS_JMPAD      0xd
745
746
0
#define mips_elf_set_cr_format(x,format)  ((x).ctype = (format))
747
0
#define mips_elf_set_cr_type(x,type)    ((x).rtype = (type))
748
0
#define mips_elf_set_cr_dist2to(x,v)    ((x).dist2to = (v))
749
0
#define mips_elf_set_cr_relvaddr(x,d)   ((x).relvaddr = (d)<<2)
750

751
/* The structure of the runtime procedure descriptor created by the
752
   loader for use by the static exception system.  */
753
754
typedef struct runtime_pdr {
755
  bfd_vma adr;    /* Memory address of start of procedure.  */
756
  long  regmask;  /* Save register mask.  */
757
  long  regoffset;  /* Save register offset.  */
758
  long  fregmask; /* Save floating point register mask.  */
759
  long  fregoffset; /* Save floating point register offset.  */
760
  long  frameoffset;  /* Frame size.  */
761
  short framereg; /* Frame pointer register.  */
762
  short pcreg;    /* Offset or reg of return pc.  */
763
  long  irpss;    /* Index into the runtime string table.  */
764
  long  reserved;
765
  struct exception_info *exception_info;/* Pointer to exception array.  */
766
} RPDR, *pRPDR;
767
#define cbRPDR sizeof (RPDR)
768
#define rpdNil ((pRPDR) 0)
769

770
static struct mips_got_entry *mips_elf_create_local_got_entry
771
  (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
772
   struct mips_elf_link_hash_entry *, int);
773
static bool mips_elf_sort_hash_table_f
774
  (struct mips_elf_link_hash_entry *, void *);
775
static bfd_vma mips_elf_high
776
  (bfd_vma);
777
static bool mips_elf_create_dynamic_relocation
778
  (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
779
   struct mips_elf_link_hash_entry *, asection *, bfd_vma,
780
   bfd_vma *, asection *);
781
static bfd_vma mips_elf_adjust_gp
782
  (bfd *, struct mips_got_info *, bfd *);
783
784
/* This will be used when we sort the dynamic relocation records.  */
785
static bfd *reldyn_sorting_bfd;
786
787
/* True if ABFD is for CPUs with load interlocking that include
788
   non-MIPS1 CPUs and R3900.  */
789
#define LOAD_INTERLOCKS_P(abfd) \
790
0
  (   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_1) \
791
0
   || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == EF_MIPS_MACH_3900))
792
793
/* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
794
   This should be safe for all architectures.  We enable this predicate
795
   for RM9000 for now.  */
796
#define JAL_TO_BAL_P(abfd) \
797
0
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == EF_MIPS_MACH_9000)
798
799
/* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
800
   This should be safe for all architectures.  We enable this predicate for
801
   all CPUs.  */
802
0
#define JALR_TO_BAL_P(abfd) 1
803
804
/* True if ABFD is for CPUs that are faster if JR is converted to B.
805
   This should be safe for all architectures.  We enable this predicate for
806
   all CPUs.  */
807
0
#define JR_TO_B_P(abfd) 1
808
809
/* True if ABFD is a PIC object.  */
810
#define PIC_OBJECT_P(abfd) \
811
0
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
812
813
/* Nonzero if ABFD is using the O32 ABI.  */
814
#define ABI_O32_P(abfd) \
815
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32)
816
817
/* Nonzero if ABFD is using the N32 ABI.  */
818
#define ABI_N32_P(abfd) \
819
156
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
820
821
/* Nonzero if ABFD is using the N64 ABI.  */
822
#define ABI_64_P(abfd) \
823
124
  (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
824
825
/* Nonzero if ABFD is using NewABI conventions.  */
826
20
#define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
827
828
/* Nonzero if ABFD has microMIPS code.  */
829
#define MICROMIPS_P(abfd) \
830
171
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
831
832
/* Nonzero if ABFD is MIPS R6.  */
833
#define MIPSR6_P(abfd) \
834
0
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6 \
835
0
    || (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R6)
836
837
/* The IRIX compatibility level we are striving for.  */
838
#define IRIX_COMPAT(abfd) \
839
39
  (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
840
841
/* Whether we are trying to be compatible with IRIX at all.  */
842
#define SGI_COMPAT(abfd) \
843
61
  (IRIX_COMPAT (abfd) != ict_none)
844
845
/* The name of the options section.  */
846
#define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
847
18
  (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
848
849
/* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
850
   Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME.  */
851
#define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
852
1.40k
  (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
853
854
/* True if NAME is the recognized name of any SHT_MIPS_ABIFLAGS section.  */
855
#define MIPS_ELF_ABIFLAGS_SECTION_NAME_P(NAME) \
856
23
  (strcmp (NAME, ".MIPS.abiflags") == 0)
857
858
/* Whether the section is readonly.  */
859
#define MIPS_ELF_READONLY_SECTION(sec) \
860
0
  ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY))    \
861
0
   == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
862
863
/* The name of the stub section.  */
864
0
#define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
865
866
/* The size of an external REL relocation.  */
867
#define MIPS_ELF_REL_SIZE(abfd) \
868
0
  (get_elf_backend_data (abfd)->s->sizeof_rel)
869
870
/* The size of an external RELA relocation.  */
871
#define MIPS_ELF_RELA_SIZE(abfd) \
872
0
  (get_elf_backend_data (abfd)->s->sizeof_rela)
873
874
/* The size of an external dynamic table entry.  */
875
#define MIPS_ELF_DYN_SIZE(abfd) \
876
0
  (get_elf_backend_data (abfd)->s->sizeof_dyn)
877
878
/* The size of a GOT entry.  */
879
#define MIPS_ELF_GOT_SIZE(abfd) \
880
0
  (get_elf_backend_data (abfd)->s->arch_size / 8)
881
882
/* The size of the .rld_map section. */
883
#define MIPS_ELF_RLD_MAP_SIZE(abfd) \
884
0
  (get_elf_backend_data (abfd)->s->arch_size / 8)
885
886
/* The size of a symbol-table entry.  */
887
#define MIPS_ELF_SYM_SIZE(abfd) \
888
0
  (get_elf_backend_data (abfd)->s->sizeof_sym)
889
890
/* The default alignment for sections, as a power of two.  */
891
#define MIPS_ELF_LOG_FILE_ALIGN(abfd)       \
892
0
  (get_elf_backend_data (abfd)->s->log_file_align)
893
894
/* Get word-sized data.  */
895
#define MIPS_ELF_GET_WORD(abfd, ptr) \
896
  (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
897
898
/* Put out word-sized data.  */
899
#define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
900
0
  (ABI_64_P (abfd)       \
901
0
   ? bfd_put_64 (abfd, val, ptr)   \
902
0
   : bfd_put_32 (abfd, val, ptr))
903
904
/* The opcode for word-sized loads (LW or LD).  */
905
#define MIPS_ELF_LOAD_WORD(abfd) \
906
0
  (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
907
908
/* Add a dynamic symbol table-entry.  */
909
#define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val)  \
910
0
  _bfd_elf_add_dynamic_entry (info, tag, val)
911
912
#define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela)      \
913
6
  (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (abfd, rtype, rela))
914
915
/* The name of the dynamic relocation section.  */
916
#define MIPS_ELF_REL_DYN_NAME(INFO) \
917
0
  (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
918
0
   ? ".rela.dyn" : ".rel.dyn")
919
920
/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
921
   from smaller values.  Start with zero, widen, *then* decrement.  */
922
0
#define MINUS_ONE (((bfd_vma)0) - 1)
923
0
#define MINUS_TWO (((bfd_vma)0) - 2)
924
925
/* The value to write into got[1] for SVR4 targets, to identify it is
926
   a GNU object.  The dynamic linker can then use got[1] to store the
927
   module pointer.  */
928
#define MIPS_ELF_GNU_GOT1_MASK(abfd) \
929
  ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
930
931
/* The offset of $gp from the beginning of the .got section.  */
932
#define ELF_MIPS_GP_OFFSET(INFO) \
933
0
  (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
934
0
   ? 0x0 : 0x7ff0)
935
936
/* The maximum size of the GOT for it to be addressable using 16-bit
937
   offsets from $gp.  */
938
0
#define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
939
940
/* Instructions which appear in a stub.  */
941
#define STUB_LW(abfd)             \
942
  ((ABI_64_P (abfd)             \
943
    ? 0xdf998010        /* ld t9,0x8010(gp) */  \
944
    : 0x8f998010))        /* lw t9,0x8010(gp) */
945
#define STUB_MOVE 0x03e07825      /* or t7,ra,zero */
946
#define STUB_LUI(VAL) (0x3c180000 + (VAL))  /* lui t8,VAL */
947
#define STUB_JALR 0x0320f809      /* jalr ra,t9 */
948
#define STUB_JALRC 0xf8190000     /* jalrc ra,t9 */
949
#define STUB_ORI(VAL) (0x37180000 + (VAL))  /* ori t8,t8,VAL */
950
#define STUB_LI16U(VAL) (0x34180000 + (VAL))  /* ori t8,zero,VAL unsigned */
951
#define STUB_LI16S(abfd, VAL)           \
952
   ((ABI_64_P (abfd)              \
953
    ? (0x64180000 + (VAL))  /* daddiu t8,zero,VAL sign extended */  \
954
    : (0x24180000 + (VAL))))  /* addiu t8,zero,VAL sign extended */
955
956
/* Likewise for the microMIPS ASE.  */
957
#define STUB_LW_MICROMIPS(abfd)           \
958
0
  (ABI_64_P (abfd)             \
959
0
   ? 0xdf3c8010          /* ld t9,0x8010(gp) */  \
960
0
   : 0xff3c8010)        /* lw t9,0x8010(gp) */
961
#define STUB_MOVE_MICROMIPS 0x0dff    /* move t7,ra */
962
0
#define STUB_MOVE32_MICROMIPS 0x001f7a90  /* or t7,ra,zero */
963
#define STUB_LUI_MICROMIPS(VAL)           \
964
0
   (0x41b80000 + (VAL))        /* lui t8,VAL */
965
#define STUB_JALR_MICROMIPS 0x45d9    /* jalr t9 */
966
0
#define STUB_JALR32_MICROMIPS 0x03f90f3c  /* jalr ra,t9 */
967
#define STUB_ORI_MICROMIPS(VAL)           \
968
0
  (0x53180000 + (VAL))        /* ori t8,t8,VAL */
969
#define STUB_LI16U_MICROMIPS(VAL)         \
970
0
  (0x53000000 + (VAL))        /* ori t8,zero,VAL unsigned */
971
#define STUB_LI16S_MICROMIPS(abfd, VAL)         \
972
0
   (ABI_64_P (abfd)             \
973
0
    ? 0x5f000000 + (VAL)  /* daddiu t8,zero,VAL sign extended */  \
974
0
    : 0x33000000 + (VAL))  /* addiu t8,zero,VAL sign extended */
975
976
0
#define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
977
0
#define MIPS_FUNCTION_STUB_BIG_SIZE 20
978
0
#define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
979
0
#define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
980
0
#define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
981
0
#define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
982
983
/* The name of the dynamic interpreter.  This is put in the .interp
984
   section.  */
985
986
#define ELF_DYNAMIC_INTERPRETER(abfd)   \
987
0
   (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1"  \
988
0
    : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1"  \
989
0
    : "/usr/lib/libc.so.1")
990
991
#ifdef BFD64
992
#define MNAME(bfd,pre,pos) \
993
  (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
994
#define ELF_R_SYM(bfd, i)         \
995
0
  (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
996
#define ELF_R_TYPE(bfd, i)          \
997
0
  (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
998
#define ELF_R_INFO(bfd, s, t)         \
999
0
  (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
1000
#else
1001
#define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
1002
#define ELF_R_SYM(bfd, i)         \
1003
  (ELF32_R_SYM (i))
1004
#define ELF_R_TYPE(bfd, i)          \
1005
  (ELF32_R_TYPE (i))
1006
#define ELF_R_INFO(bfd, s, t)         \
1007
  (ELF32_R_INFO (s, t))
1008
#endif
1009

1010
  /* The mips16 compiler uses a couple of special sections to handle
1011
     floating point arguments.
1012
1013
     Section names that look like .mips16.fn.FNNAME contain stubs that
1014
     copy floating point arguments from the fp regs to the gp regs and
1015
     then jump to FNNAME.  If any 32 bit function calls FNNAME, the
1016
     call should be redirected to the stub instead.  If no 32 bit
1017
     function calls FNNAME, the stub should be discarded.  We need to
1018
     consider any reference to the function, not just a call, because
1019
     if the address of the function is taken we will need the stub,
1020
     since the address might be passed to a 32 bit function.
1021
1022
     Section names that look like .mips16.call.FNNAME contain stubs
1023
     that copy floating point arguments from the gp regs to the fp
1024
     regs and then jump to FNNAME.  If FNNAME is a 32 bit function,
1025
     then any 16 bit function that calls FNNAME should be redirected
1026
     to the stub instead.  If FNNAME is not a 32 bit function, the
1027
     stub should be discarded.
1028
1029
     .mips16.call.fp.FNNAME sections are similar, but contain stubs
1030
     which call FNNAME and then copy the return value from the fp regs
1031
     to the gp regs.  These stubs store the return value in $18 while
1032
     calling FNNAME; any function which might call one of these stubs
1033
     must arrange to save $18 around the call.  (This case is not
1034
     needed for 32 bit functions that call 16 bit functions, because
1035
     16 bit functions always return floating point values in both
1036
     $f0/$f1 and $2/$3.)
1037
1038
     Note that in all cases FNNAME might be defined statically.
1039
     Therefore, FNNAME is not used literally.  Instead, the relocation
1040
     information will indicate which symbol the section is for.
1041
1042
     We record any stubs that we find in the symbol table.  */
1043
1044
0
#define FN_STUB ".mips16.fn."
1045
0
#define CALL_STUB ".mips16.call."
1046
0
#define CALL_FP_STUB ".mips16.call.fp."
1047
1048
0
#define FN_STUB_P(name) startswith (name, FN_STUB)
1049
0
#define CALL_STUB_P(name) startswith (name, CALL_STUB)
1050
0
#define CALL_FP_STUB_P(name) startswith (name, CALL_FP_STUB)
1051

1052
/* The format of the first PLT entry in an O32 executable.  */
1053
static const bfd_vma mips_o32_exec_plt0_entry[] =
1054
{
1055
  0x3c1c0000, /* lui $28, %hi(&GOTPLT[0])       */
1056
  0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28)       */
1057
  0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0])      */
1058
  0x031cc023, /* subu $24, $24, $28         */
1059
  0x03e07825, /* or t7, ra, zero          */
1060
  0x0018c082, /* srl $24, $24, 2          */
1061
  0x0320f809, /* jalr $25           */
1062
  0x2718fffe  /* subu $24, $24, 2         */
1063
};
1064
1065
/* The format of the first PLT entry in an O32 executable using compact
1066
   jumps.  */
1067
static const bfd_vma mipsr6_o32_exec_plt0_entry_compact[] =
1068
{
1069
  0x3c1c0000, /* lui $28, %hi(&GOTPLT[0])       */
1070
  0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28)       */
1071
  0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0])      */
1072
  0x031cc023, /* subu $24, $24, $28         */
1073
  0x03e07821, /* move $15, $31  # 32-bit move (addu)    */
1074
  0x0018c082, /* srl $24, $24, 2          */
1075
  0x2718fffe, /* subu $24, $24, 2         */
1076
  0xf8190000  /* jalrc $25            */
1077
};
1078
1079
/* The format of the first PLT entry in an N32 executable.  Different
1080
   because gp ($28) is not available; we use t2 ($14) instead.  */
1081
static const bfd_vma mips_n32_exec_plt0_entry[] =
1082
{
1083
  0x3c0e0000, /* lui $14, %hi(&GOTPLT[0])       */
1084
  0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14)       */
1085
  0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0])      */
1086
  0x030ec023, /* subu $24, $24, $14         */
1087
  0x03e07825, /* or t7, ra, zero          */
1088
  0x0018c082, /* srl $24, $24, 2          */
1089
  0x0320f809, /* jalr $25           */
1090
  0x2718fffe  /* subu $24, $24, 2         */
1091
};
1092
1093
/* The format of the first PLT entry in an N32 executable using compact
1094
   jumps.  Different because gp ($28) is not available; we use t2 ($14)
1095
   instead.  */
1096
static const bfd_vma mipsr6_n32_exec_plt0_entry_compact[] =
1097
{
1098
  0x3c0e0000, /* lui $14, %hi(&GOTPLT[0])       */
1099
  0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14)       */
1100
  0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0])      */
1101
  0x030ec023, /* subu $24, $24, $14         */
1102
  0x03e07821, /* move $15, $31  # 32-bit move (addu)    */
1103
  0x0018c082, /* srl $24, $24, 2          */
1104
  0x2718fffe, /* subu $24, $24, 2         */
1105
  0xf8190000  /* jalrc $25            */
1106
};
1107
1108
/* The format of the first PLT entry in an N64 executable.  Different
1109
   from N32 because of the increased size of GOT entries.  */
1110
static const bfd_vma mips_n64_exec_plt0_entry[] =
1111
{
1112
  0x3c0e0000, /* lui $14, %hi(&GOTPLT[0])       */
1113
  0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14)       */
1114
  0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0])      */
1115
  0x030ec023, /* subu $24, $24, $14         */
1116
  0x03e07825, /* or t7, ra, zero          */
1117
  0x0018c0c2, /* srl $24, $24, 3          */
1118
  0x0320f809, /* jalr $25           */
1119
  0x2718fffe  /* subu $24, $24, 2         */
1120
};
1121
1122
/* The format of the first PLT entry in an N64 executable using compact
1123
   jumps.  Different from N32 because of the increased size of GOT
1124
   entries.  */
1125
static const bfd_vma mipsr6_n64_exec_plt0_entry_compact[] =
1126
{
1127
  0x3c0e0000, /* lui $14, %hi(&GOTPLT[0])       */
1128
  0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14)       */
1129
  0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0])      */
1130
  0x030ec023, /* subu $24, $24, $14         */
1131
  0x03e0782d, /* move $15, $31  # 64-bit move (daddu)   */
1132
  0x0018c0c2, /* srl $24, $24, 3          */
1133
  0x2718fffe, /* subu $24, $24, 2         */
1134
  0xf8190000  /* jalrc $25            */
1135
};
1136
1137
1138
/* The format of the microMIPS first PLT entry in an O32 executable.
1139
   We rely on v0 ($2) rather than t8 ($24) to contain the address
1140
   of the GOTPLT entry handled, so this stub may only be used when
1141
   all the subsequent PLT entries are microMIPS code too.
1142
1143
   The trailing NOP is for alignment and correct disassembly only.  */
1144
static const bfd_vma micromips_o32_exec_plt0_entry[] =
1145
{
1146
  0x7980, 0x0000, /* addiupc $3, (&GOTPLT[0]) - .     */
1147
  0xff23, 0x0000, /* lw $25, 0($3)        */
1148
  0x0535,   /* subu $2, $2, $3        */
1149
  0x2525,   /* srl $2, $2, 2        */
1150
  0x3302, 0xfffe, /* subu $24, $2, 2        */
1151
  0x0dff,   /* move $15, $31        */
1152
  0x45f9,   /* jalrs $25          */
1153
  0x0f83,   /* move $28, $3         */
1154
  0x0c00    /* nop            */
1155
};
1156
1157
/* The format of the microMIPS first PLT entry in an O32 executable
1158
   in the insn32 mode.  */
1159
static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
1160
{
1161
  0x41bc, 0x0000, /* lui $28, %hi(&GOTPLT[0])     */
1162
  0xff3c, 0x0000, /* lw $25, %lo(&GOTPLT[0])($28)     */
1163
  0x339c, 0x0000, /* addiu $28, $28, %lo(&GOTPLT[0])    */
1164
  0x0398, 0xc1d0, /* subu $24, $24, $28       */
1165
  0x001f, 0x7a90, /* or $15, $31, zero        */
1166
  0x0318, 0x1040, /* srl $24, $24, 2        */
1167
  0x03f9, 0x0f3c, /* jalr $25         */
1168
  0x3318, 0xfffe  /* subu $24, $24, 2       */
1169
};
1170
1171
/* The format of subsequent standard PLT entries.  */
1172
static const bfd_vma mips_exec_plt_entry[] =
1173
{
1174
  0x3c0f0000, /* lui $15, %hi(.got.plt entry)     */
1175
  0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15)    */
1176
  0x25f80000, /* addiu $24, $15, %lo(.got.plt entry)    */
1177
  0x03200008  /* jr $25         */
1178
};
1179
1180
static const bfd_vma mipsr6_exec_plt_entry[] =
1181
{
1182
  0x3c0f0000, /* lui $15, %hi(.got.plt entry)     */
1183
  0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15)    */
1184
  0x25f80000, /* addiu $24, $15, %lo(.got.plt entry)    */
1185
  0x03200009  /* jr $25         */
1186
};
1187
1188
static const bfd_vma mipsr6_exec_plt_entry_compact[] =
1189
{
1190
  0x3c0f0000, /* lui $15, %hi(.got.plt entry)     */
1191
  0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15)    */
1192
  0x25f80000, /* addiu $24, $15, %lo(.got.plt entry)    */
1193
  0xd8190000  /* jic $25, 0         */
1194
};
1195
1196
/* The format of subsequent MIPS16 o32 PLT entries.  We use v0 ($2)
1197
   and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
1198
   directly addressable.  */
1199
static const bfd_vma mips16_o32_exec_plt_entry[] =
1200
{
1201
  0xb203,   /* lw $2, 12($pc)     */
1202
  0x9a60,   /* lw $3, 0($2)       */
1203
  0x651a,   /* move $24, $2       */
1204
  0xeb00,   /* jr $3        */
1205
  0x653b,   /* move $25, $3       */
1206
  0x6500,   /* nop          */
1207
  0x0000, 0x0000  /* .word (.got.plt entry)   */
1208
};
1209
1210
/* The format of subsequent microMIPS o32 PLT entries.  We use v0 ($2)
1211
   as a temporary because t8 ($24) is not addressable with ADDIUPC.  */
1212
static const bfd_vma micromips_o32_exec_plt_entry[] =
1213
{
1214
  0x7900, 0x0000, /* addiupc $2, (.got.plt entry) - . */
1215
  0xff22, 0x0000, /* lw $25, 0($2)      */
1216
  0x4599,   /* jr $25       */
1217
  0x0f02    /* move $24, $2       */
1218
};
1219
1220
/* The format of subsequent microMIPS o32 PLT entries in the insn32 mode.  */
1221
static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
1222
{
1223
  0x41af, 0x0000, /* lui $15, %hi(.got.plt entry)   */
1224
  0xff2f, 0x0000, /* lw $25, %lo(.got.plt entry)($15) */
1225
  0x0019, 0x0f3c, /* jr $25       */
1226
  0x330f, 0x0000  /* addiu $24, $15, %lo(.got.plt entry)  */
1227
};
1228
1229
/* The format of the first PLT entry in a VxWorks executable.  */
1230
static const bfd_vma mips_vxworks_exec_plt0_entry[] =
1231
{
1232
  0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_)   */
1233
  0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
1234
  0x8f390008, /* lw t9, 8(t9)         */
1235
  0x00000000, /* nop            */
1236
  0x03200008, /* jr t9          */
1237
  0x00000000  /* nop            */
1238
};
1239
1240
/* The format of subsequent PLT entries.  */
1241
static const bfd_vma mips_vxworks_exec_plt_entry[] =
1242
{
1243
  0x10000000, /* b .PLT_resolver      */
1244
  0x24180000, /* li t8, <pltindex>      */
1245
  0x3c190000, /* lui t9, %hi(<.got.plt slot>)   */
1246
  0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
1247
  0x8f390000, /* lw t9, 0(t9)       */
1248
  0x00000000, /* nop          */
1249
  0x03200008, /* jr t9        */
1250
  0x00000000  /* nop          */
1251
};
1252
1253
/* The format of the first PLT entry in a VxWorks shared object.  */
1254
static const bfd_vma mips_vxworks_shared_plt0_entry[] =
1255
{
1256
  0x8f990008, /* lw t9, 8(gp)   */
1257
  0x00000000, /* nop      */
1258
  0x03200008, /* jr t9    */
1259
  0x00000000, /* nop      */
1260
  0x00000000, /* nop      */
1261
  0x00000000  /* nop      */
1262
};
1263
1264
/* The format of subsequent PLT entries.  */
1265
static const bfd_vma mips_vxworks_shared_plt_entry[] =
1266
{
1267
  0x10000000, /* b .PLT_resolver  */
1268
  0x24180000  /* li t8, <pltindex>  */
1269
};
1270

1271
/* microMIPS 32-bit opcode helper installer.  */
1272
1273
static void
1274
bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1275
0
{
1276
0
  bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
1277
0
  bfd_put_16 (abfd,  opcode    & 0xffff, ptr + 2);
1278
0
}
1279
1280
/* microMIPS 32-bit opcode helper retriever.  */
1281
1282
static bfd_vma
1283
bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1284
95
{
1285
95
  return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1286
95
}
1287

1288
/* Look up an entry in a MIPS ELF linker hash table.  */
1289
1290
#define mips_elf_link_hash_lookup(table, string, create, copy, follow)  \
1291
0
  ((struct mips_elf_link_hash_entry *)          \
1292
0
   elf_link_hash_lookup (&(table)->root, (string), (create),    \
1293
0
       (copy), (follow)))
1294
1295
/* Traverse a MIPS ELF linker hash table.  */
1296
1297
#define mips_elf_link_hash_traverse(table, func, info)      \
1298
0
  (elf_link_hash_traverse           \
1299
0
   (&(table)->root,              \
1300
0
    (bool (*) (struct elf_link_hash_entry *, void *)) (func),   \
1301
0
    (info)))
1302
1303
/* Find the base offsets for thread-local storage in this object,
1304
   for GD/LD and IE/LE respectively.  */
1305
1306
0
#define TP_OFFSET 0x7000
1307
0
#define DTP_OFFSET 0x8000
1308
1309
static bfd_vma
1310
dtprel_base (struct bfd_link_info *info)
1311
0
{
1312
  /* If tls_sec is NULL, we should have signalled an error already.  */
1313
0
  if (elf_hash_table (info)->tls_sec == NULL)
1314
0
    return 0;
1315
0
  return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1316
0
}
1317
1318
static bfd_vma
1319
tprel_base (struct bfd_link_info *info)
1320
0
{
1321
  /* If tls_sec is NULL, we should have signalled an error already.  */
1322
0
  if (elf_hash_table (info)->tls_sec == NULL)
1323
0
    return 0;
1324
0
  return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1325
0
}
1326
1327
/* Create an entry in a MIPS ELF linker hash table.  */
1328
1329
static struct bfd_hash_entry *
1330
mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1331
          struct bfd_hash_table *table, const char *string)
1332
0
{
1333
0
  struct mips_elf_link_hash_entry *ret =
1334
0
    (struct mips_elf_link_hash_entry *) entry;
1335
1336
  /* Allocate the structure if it has not already been allocated by a
1337
     subclass.  */
1338
0
  if (ret == NULL)
1339
0
    ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1340
0
  if (ret == NULL)
1341
0
    return (struct bfd_hash_entry *) ret;
1342
1343
  /* Call the allocation method of the superclass.  */
1344
0
  ret = ((struct mips_elf_link_hash_entry *)
1345
0
   _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1346
0
             table, string));
1347
0
  if (ret != NULL)
1348
0
    {
1349
      /* Set local fields.  */
1350
0
      memset (&ret->esym, 0, sizeof (EXTR));
1351
      /* We use -2 as a marker to indicate that the information has
1352
   not been set.  -1 means there is no associated ifd.  */
1353
0
      ret->esym.ifd = -2;
1354
0
      ret->la25_stub = 0;
1355
0
      ret->possibly_dynamic_relocs = 0;
1356
0
      ret->fn_stub = NULL;
1357
0
      ret->call_stub = NULL;
1358
0
      ret->call_fp_stub = NULL;
1359
0
      ret->mipsxhash_loc = 0;
1360
0
      ret->global_got_area = GGA_NONE;
1361
0
      ret->got_only_for_calls = true;
1362
0
      ret->readonly_reloc = false;
1363
0
      ret->has_static_relocs = false;
1364
0
      ret->no_fn_stub = false;
1365
0
      ret->need_fn_stub = false;
1366
0
      ret->has_nonpic_branches = false;
1367
0
      ret->needs_lazy_stub = false;
1368
0
      ret->use_plt_entry = false;
1369
0
    }
1370
1371
0
  return (struct bfd_hash_entry *) ret;
1372
0
}
1373
1374
/* Allocate MIPS ELF private object data.  */
1375
1376
bool
1377
_bfd_mips_elf_mkobject (bfd *abfd)
1378
1.57M
{
1379
1.57M
  return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
1380
1.57M
          MIPS_ELF_DATA);
1381
1.57M
}
1382
1383
/* MIPS ELF uses a special find_nearest_line routine in order the
1384
   handle the ECOFF debugging information.  */
1385
1386
struct mips_elf_find_line
1387
{
1388
  struct ecoff_debug_info d;
1389
  struct ecoff_find_line i;
1390
};
1391
1392
bool
1393
_bfd_mips_elf_free_cached_info (bfd *abfd)
1394
58.9k
{
1395
58.9k
  struct mips_elf_obj_tdata *tdata;
1396
1397
58.9k
  if ((bfd_get_format (abfd) == bfd_object
1398
58.9k
       || bfd_get_format (abfd) == bfd_core)
1399
58.9k
      && (tdata = mips_elf_tdata (abfd)) != NULL)
1400
24.1k
    {
1401
24.1k
      BFD_ASSERT (tdata->root.object_id == MIPS_ELF_DATA);
1402
24.1k
      while (tdata->mips_hi16_list != NULL)
1403
10
  {
1404
10
    struct mips_hi16 *hi = tdata->mips_hi16_list;
1405
10
    tdata->mips_hi16_list = hi->next;
1406
10
    free (hi);
1407
10
  }
1408
24.1k
      if (tdata->find_line_info != NULL)
1409
124
  _bfd_ecoff_free_ecoff_debug_info (&tdata->find_line_info->d);
1410
24.1k
    }
1411
58.9k
  return _bfd_elf_free_cached_info (abfd);
1412
58.9k
}
1413
1414
bool
1415
_bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1416
307k
{
1417
307k
  if (!sec->used_by_bfd)
1418
307k
    {
1419
307k
      struct _mips_elf_section_data *sdata;
1420
307k
      size_t amt = sizeof (*sdata);
1421
1422
307k
      sdata = bfd_zalloc (abfd, amt);
1423
307k
      if (sdata == NULL)
1424
0
  return false;
1425
307k
      sec->used_by_bfd = sdata;
1426
307k
    }
1427
1428
307k
  return _bfd_elf_new_section_hook (abfd, sec);
1429
307k
}
1430

1431
/* Read ECOFF debugging information from a .mdebug section into a
1432
   ecoff_debug_info structure.  */
1433
1434
bool
1435
_bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1436
             struct ecoff_debug_info *debug)
1437
1.34k
{
1438
1.34k
  HDRR *symhdr;
1439
1.34k
  const struct ecoff_debug_swap *swap;
1440
1.34k
  char *ext_hdr;
1441
1442
1.34k
  swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1443
1.34k
  memset (debug, 0, sizeof (*debug));
1444
1445
1.34k
  ext_hdr = bfd_malloc (swap->external_hdr_size);
1446
1.34k
  if (ext_hdr == NULL && swap->external_hdr_size != 0)
1447
0
    goto error_return;
1448
1449
1.34k
  if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1450
1.34k
          swap->external_hdr_size))
1451
66
    goto error_return;
1452
1453
1.28k
  symhdr = &debug->symbolic_header;
1454
1.28k
  (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1455
1.28k
  free (ext_hdr);
1456
1.28k
  ext_hdr = NULL;
1457
1458
  /* The symbolic header contains absolute file offsets and sizes to
1459
     read.  */
1460
1.28k
#define READ(ptr, offset, count, size)          \
1461
6.64k
  do                  \
1462
6.64k
    {                 \
1463
6.64k
      size_t amt;             \
1464
6.64k
      debug->ptr = NULL;            \
1465
6.64k
      if (symhdr->count == 0)           \
1466
6.64k
  break;               \
1467
6.64k
      if (_bfd_mul_overflow (size, symhdr->count, &amt))   \
1468
3.55k
  {               \
1469
0
    bfd_set_error (bfd_error_file_too_big);     \
1470
0
    goto error_return;            \
1471
0
  }                \
1472
3.55k
      if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0)   \
1473
3.55k
  goto error_return;           \
1474
3.55k
      debug->ptr = _bfd_malloc_and_read (abfd, amt + 1, amt);   \
1475
3.01k
      if (debug->ptr == NULL)           \
1476
3.01k
  goto error_return;           \
1477
3.01k
      ((char *) debug->ptr)[amt] = 0;         \
1478
2.40k
    } while (0)
1479
1480
1.28k
  READ (line, cbLineOffset, cbLine, sizeof (unsigned char));
1481
965
  READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size);
1482
822
  READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size);
1483
729
  READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size);
1484
646
  READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size);
1485
572
  READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext));
1486
488
  READ (ss, cbSsOffset, issMax, sizeof (char));
1487
405
  READ (ssext, cbSsExtOffset, issExtMax, sizeof (char));
1488
323
  READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size);
1489
238
  READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size);
1490
179
  READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size);
1491
124
#undef READ
1492
1493
124
  return true;
1494
1495
1.22k
 error_return:
1496
1.22k
  free (ext_hdr);
1497
1.22k
  _bfd_ecoff_free_ecoff_debug_info (debug);
1498
1.22k
  return false;
1499
179
}
1500

1501
/* Swap RPDR (runtime procedure table entry) for output.  */
1502
1503
static void
1504
ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1505
0
{
1506
0
  H_PUT_S32 (abfd, in->adr, ex->p_adr);
1507
0
  H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1508
0
  H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1509
0
  H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1510
0
  H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1511
0
  H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1512
1513
0
  H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1514
0
  H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1515
1516
0
  H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1517
0
}
1518
1519
/* Create a runtime procedure table from the .mdebug section.  */
1520
1521
static bool
1522
mips_elf_create_procedure_table (void *handle, bfd *abfd,
1523
         struct bfd_link_info *info, asection *s,
1524
         struct ecoff_debug_info *debug)
1525
0
{
1526
0
  const struct ecoff_debug_swap *swap;
1527
0
  HDRR *hdr = &debug->symbolic_header;
1528
0
  RPDR *rpdr, *rp;
1529
0
  struct rpdr_ext *erp;
1530
0
  void *rtproc;
1531
0
  struct pdr_ext *epdr;
1532
0
  struct sym_ext *esym;
1533
0
  char *ss, **sv;
1534
0
  char *str;
1535
0
  bfd_size_type size;
1536
0
  bfd_size_type count;
1537
0
  unsigned long sindex;
1538
0
  unsigned long i;
1539
0
  PDR pdr;
1540
0
  SYMR sym;
1541
0
  const char *no_name_func = _("static procedure (no name)");
1542
1543
0
  epdr = NULL;
1544
0
  rpdr = NULL;
1545
0
  esym = NULL;
1546
0
  ss = NULL;
1547
0
  sv = NULL;
1548
1549
0
  swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1550
1551
0
  sindex = strlen (no_name_func) + 1;
1552
0
  count = hdr->ipdMax;
1553
0
  if (count > 0)
1554
0
    {
1555
0
      size = swap->external_pdr_size;
1556
1557
0
      epdr = bfd_malloc (size * count);
1558
0
      if (epdr == NULL)
1559
0
  goto error_return;
1560
1561
0
      if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1562
0
  goto error_return;
1563
1564
0
      size = sizeof (RPDR);
1565
0
      rp = rpdr = bfd_malloc (size * count);
1566
0
      if (rpdr == NULL)
1567
0
  goto error_return;
1568
1569
0
      size = sizeof (char *);
1570
0
      sv = bfd_malloc (size * count);
1571
0
      if (sv == NULL)
1572
0
  goto error_return;
1573
1574
0
      count = hdr->isymMax;
1575
0
      size = swap->external_sym_size;
1576
0
      esym = bfd_malloc (size * count);
1577
0
      if (esym == NULL)
1578
0
  goto error_return;
1579
1580
0
      if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1581
0
  goto error_return;
1582
1583
0
      count = hdr->issMax;
1584
0
      ss = bfd_malloc (count);
1585
0
      if (ss == NULL)
1586
0
  goto error_return;
1587
0
      if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1588
0
  goto error_return;
1589
1590
0
      count = hdr->ipdMax;
1591
0
      for (i = 0; i < (unsigned long) count; i++, rp++)
1592
0
  {
1593
0
    (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1594
0
    (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1595
0
    rp->adr = sym.value;
1596
0
    rp->regmask = pdr.regmask;
1597
0
    rp->regoffset = pdr.regoffset;
1598
0
    rp->fregmask = pdr.fregmask;
1599
0
    rp->fregoffset = pdr.fregoffset;
1600
0
    rp->frameoffset = pdr.frameoffset;
1601
0
    rp->framereg = pdr.framereg;
1602
0
    rp->pcreg = pdr.pcreg;
1603
0
    rp->irpss = sindex;
1604
0
    sv[i] = ss + sym.iss;
1605
0
    sindex += strlen (sv[i]) + 1;
1606
0
  }
1607
0
    }
1608
1609
0
  size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1610
0
  size = BFD_ALIGN (size, 16);
1611
0
  rtproc = bfd_alloc (abfd, size);
1612
0
  if (rtproc == NULL)
1613
0
    {
1614
0
      mips_elf_hash_table (info)->procedure_count = 0;
1615
0
      goto error_return;
1616
0
    }
1617
1618
0
  mips_elf_hash_table (info)->procedure_count = count + 2;
1619
1620
0
  erp = rtproc;
1621
0
  memset (erp, 0, sizeof (struct rpdr_ext));
1622
0
  erp++;
1623
0
  str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1624
0
  strcpy (str, no_name_func);
1625
0
  str += strlen (no_name_func) + 1;
1626
0
  for (i = 0; i < count; i++)
1627
0
    {
1628
0
      ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1629
0
      strcpy (str, sv[i]);
1630
0
      str += strlen (sv[i]) + 1;
1631
0
    }
1632
0
  H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1633
1634
  /* Set the size and contents of .rtproc section.  */
1635
0
  s->size = size;
1636
0
  s->contents = rtproc;
1637
1638
  /* Skip this section later on (I don't think this currently
1639
     matters, but someday it might).  */
1640
0
  s->map_head.link_order = NULL;
1641
1642
0
  free (epdr);
1643
0
  free (rpdr);
1644
0
  free (esym);
1645
0
  free (ss);
1646
0
  free (sv);
1647
0
  return true;
1648
1649
0
 error_return:
1650
0
  free (epdr);
1651
0
  free (rpdr);
1652
0
  free (esym);
1653
0
  free (ss);
1654
0
  free (sv);
1655
0
  return false;
1656
0
}
1657

1658
/* We're going to create a stub for H.  Create a symbol for the stub's
1659
   value and size, to help make the disassembly easier to read.  */
1660
1661
static bool
1662
mips_elf_create_stub_symbol (struct bfd_link_info *info,
1663
           struct mips_elf_link_hash_entry *h,
1664
           const char *prefix, asection *s, bfd_vma value,
1665
           bfd_vma size)
1666
0
{
1667
0
  bool micromips_p = ELF_ST_IS_MICROMIPS (h->root.other);
1668
0
  struct bfd_link_hash_entry *bh;
1669
0
  struct elf_link_hash_entry *elfh;
1670
0
  char *name;
1671
0
  bool res;
1672
1673
0
  if (micromips_p)
1674
0
    value |= 1;
1675
1676
  /* Create a new symbol.  */
1677
0
  name = concat (prefix, h->root.root.root.string, NULL);
1678
0
  bh = NULL;
1679
0
  res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1680
0
            BSF_LOCAL, s, value, NULL,
1681
0
            true, false, &bh);
1682
0
  free (name);
1683
0
  if (! res)
1684
0
    return false;
1685
1686
  /* Make it a local function.  */
1687
0
  elfh = (struct elf_link_hash_entry *) bh;
1688
0
  elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1689
0
  elfh->size = size;
1690
0
  elfh->forced_local = 1;
1691
0
  if (micromips_p)
1692
0
    elfh->other = ELF_ST_SET_MICROMIPS (elfh->other);
1693
0
  return true;
1694
0
}
1695
1696
/* We're about to redefine H.  Create a symbol to represent H's
1697
   current value and size, to help make the disassembly easier
1698
   to read.  */
1699
1700
static bool
1701
mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1702
             struct mips_elf_link_hash_entry *h,
1703
             const char *prefix)
1704
0
{
1705
0
  struct bfd_link_hash_entry *bh;
1706
0
  struct elf_link_hash_entry *elfh;
1707
0
  char *name;
1708
0
  asection *s;
1709
0
  bfd_vma value;
1710
0
  bool res;
1711
1712
  /* Read the symbol's value.  */
1713
0
  BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1714
0
        || h->root.root.type == bfd_link_hash_defweak);
1715
0
  s = h->root.root.u.def.section;
1716
0
  value = h->root.root.u.def.value;
1717
1718
  /* Create a new symbol.  */
1719
0
  name = concat (prefix, h->root.root.root.string, NULL);
1720
0
  bh = NULL;
1721
0
  res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1722
0
            BSF_LOCAL, s, value, NULL,
1723
0
            true, false, &bh);
1724
0
  free (name);
1725
0
  if (! res)
1726
0
    return false;
1727
1728
  /* Make it local and copy the other attributes from H.  */
1729
0
  elfh = (struct elf_link_hash_entry *) bh;
1730
0
  elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1731
0
  elfh->other = h->root.other;
1732
0
  elfh->size = h->root.size;
1733
0
  elfh->forced_local = 1;
1734
0
  return true;
1735
0
}
1736
1737
/* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1738
   function rather than to a hard-float stub.  */
1739
1740
static bool
1741
section_allows_mips16_refs_p (asection *section)
1742
0
{
1743
0
  const char *name;
1744
1745
0
  name = bfd_section_name (section);
1746
0
  return (FN_STUB_P (name)
1747
0
    || CALL_STUB_P (name)
1748
0
    || CALL_FP_STUB_P (name)
1749
0
    || strcmp (name, ".pdr") == 0);
1750
0
}
1751
1752
/* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1753
   stub section of some kind.  Return the R_SYMNDX of the target
1754
   function, or 0 if we can't decide which function that is.  */
1755
1756
static unsigned long
1757
mips16_stub_symndx (const struct elf_backend_data *bed,
1758
        asection *sec ATTRIBUTE_UNUSED,
1759
        const Elf_Internal_Rela *relocs,
1760
        const Elf_Internal_Rela *relend)
1761
0
{
1762
0
  int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
1763
0
  const Elf_Internal_Rela *rel;
1764
1765
  /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1766
     one in a compound relocation.  */
1767
0
  for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
1768
0
    if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1769
0
      return ELF_R_SYM (sec->owner, rel->r_info);
1770
1771
  /* Otherwise trust the first relocation, whatever its kind.  This is
1772
     the traditional behavior.  */
1773
0
  if (relocs < relend)
1774
0
    return ELF_R_SYM (sec->owner, relocs->r_info);
1775
1776
0
  return 0;
1777
0
}
1778
1779
/* Check the mips16 stubs for a particular symbol, and see if we can
1780
   discard them.  */
1781
1782
static void
1783
mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1784
           struct mips_elf_link_hash_entry *h)
1785
0
{
1786
  /* Dynamic symbols must use the standard call interface, in case other
1787
     objects try to call them.  */
1788
0
  if (h->fn_stub != NULL
1789
0
      && h->root.dynindx != -1)
1790
0
    {
1791
0
      mips_elf_create_shadow_symbol (info, h, ".mips16.");
1792
0
      h->need_fn_stub = true;
1793
0
    }
1794
1795
0
  if (h->fn_stub != NULL
1796
0
      && ! h->need_fn_stub)
1797
0
    {
1798
      /* We don't need the fn_stub; the only references to this symbol
1799
   are 16 bit calls.  Clobber the size to 0 to prevent it from
1800
   being included in the link.  */
1801
0
      h->fn_stub->size = 0;
1802
0
      h->fn_stub->flags &= ~SEC_RELOC;
1803
0
      h->fn_stub->reloc_count = 0;
1804
0
      h->fn_stub->flags |= SEC_EXCLUDE;
1805
0
      h->fn_stub->output_section = bfd_abs_section_ptr;
1806
0
    }
1807
1808
0
  if (h->call_stub != NULL
1809
0
      && ELF_ST_IS_MIPS16 (h->root.other))
1810
0
    {
1811
      /* We don't need the call_stub; this is a 16 bit function, so
1812
   calls from other 16 bit functions are OK.  Clobber the size
1813
   to 0 to prevent it from being included in the link.  */
1814
0
      h->call_stub->size = 0;
1815
0
      h->call_stub->flags &= ~SEC_RELOC;
1816
0
      h->call_stub->reloc_count = 0;
1817
0
      h->call_stub->flags |= SEC_EXCLUDE;
1818
0
      h->call_stub->output_section = bfd_abs_section_ptr;
1819
0
    }
1820
1821
0
  if (h->call_fp_stub != NULL
1822
0
      && ELF_ST_IS_MIPS16 (h->root.other))
1823
0
    {
1824
      /* We don't need the call_stub; this is a 16 bit function, so
1825
   calls from other 16 bit functions are OK.  Clobber the size
1826
   to 0 to prevent it from being included in the link.  */
1827
0
      h->call_fp_stub->size = 0;
1828
0
      h->call_fp_stub->flags &= ~SEC_RELOC;
1829
0
      h->call_fp_stub->reloc_count = 0;
1830
0
      h->call_fp_stub->flags |= SEC_EXCLUDE;
1831
0
      h->call_fp_stub->output_section = bfd_abs_section_ptr;
1832
0
    }
1833
0
}
1834
1835
/* Hashtable callbacks for mips_elf_la25_stubs.  */
1836
1837
static hashval_t
1838
mips_elf_la25_stub_hash (const void *entry_)
1839
0
{
1840
0
  const struct mips_elf_la25_stub *entry;
1841
1842
0
  entry = (struct mips_elf_la25_stub *) entry_;
1843
0
  return entry->h->root.root.u.def.section->id
1844
0
    + entry->h->root.root.u.def.value;
1845
0
}
1846
1847
static int
1848
mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1849
0
{
1850
0
  const struct mips_elf_la25_stub *entry1, *entry2;
1851
1852
0
  entry1 = (struct mips_elf_la25_stub *) entry1_;
1853
0
  entry2 = (struct mips_elf_la25_stub *) entry2_;
1854
0
  return ((entry1->h->root.root.u.def.section
1855
0
     == entry2->h->root.root.u.def.section)
1856
0
    && (entry1->h->root.root.u.def.value
1857
0
        == entry2->h->root.root.u.def.value));
1858
0
}
1859
1860
/* Called by the linker to set up the la25 stub-creation code.  FN is
1861
   the linker's implementation of add_stub_function.  Return true on
1862
   success.  */
1863
1864
bool
1865
_bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1866
        asection *(*fn) (const char *, asection *,
1867
             asection *))
1868
0
{
1869
0
  struct mips_elf_link_hash_table *htab;
1870
1871
0
  htab = mips_elf_hash_table (info);
1872
0
  if (htab == NULL)
1873
0
    return false;
1874
1875
0
  htab->add_stub_section = fn;
1876
0
  htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1877
0
              mips_elf_la25_stub_eq, NULL);
1878
0
  if (htab->la25_stubs == NULL)
1879
0
    return false;
1880
1881
0
  return true;
1882
0
}
1883
1884
/* Return true if H is a locally-defined PIC function, in the sense
1885
   that it or its fn_stub might need $25 to be valid on entry.
1886
   Note that MIPS16 functions set up $gp using PC-relative instructions,
1887
   so they themselves never need $25 to be valid.  Only non-MIPS16
1888
   entry points are of interest here.  */
1889
1890
static bool
1891
mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1892
0
{
1893
0
  return ((h->root.root.type == bfd_link_hash_defined
1894
0
     || h->root.root.type == bfd_link_hash_defweak)
1895
0
    && h->root.def_regular
1896
0
    && !bfd_is_abs_section (h->root.root.u.def.section)
1897
0
    && !bfd_is_und_section (h->root.root.u.def.section)
1898
0
    && (!ELF_ST_IS_MIPS16 (h->root.other)
1899
0
        || (h->fn_stub && h->need_fn_stub))
1900
0
    && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1901
0
        || ELF_ST_IS_MIPS_PIC (h->root.other)));
1902
0
}
1903
1904
/* Set *SEC to the input section that contains the target of STUB.
1905
   Return the offset of the target from the start of that section.  */
1906
1907
static bfd_vma
1908
mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1909
        asection **sec)
1910
0
{
1911
0
  if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1912
0
    {
1913
0
      BFD_ASSERT (stub->h->need_fn_stub);
1914
0
      *sec = stub->h->fn_stub;
1915
0
      return 0;
1916
0
    }
1917
0
  else
1918
0
    {
1919
0
      *sec = stub->h->root.root.u.def.section;
1920
0
      return stub->h->root.root.u.def.value;
1921
0
    }
1922
0
}
1923
1924
/* STUB describes an la25 stub that we have decided to implement
1925
   by inserting an LUI/ADDIU pair before the target function.
1926
   Create the section and redirect the function symbol to it.  */
1927
1928
static bool
1929
mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1930
       struct bfd_link_info *info)
1931
0
{
1932
0
  struct mips_elf_link_hash_table *htab;
1933
0
  char *name;
1934
0
  asection *s, *input_section;
1935
0
  unsigned int align;
1936
1937
0
  htab = mips_elf_hash_table (info);
1938
0
  if (htab == NULL)
1939
0
    return false;
1940
1941
  /* Create a unique name for the new section.  */
1942
0
  name = bfd_malloc (11 + sizeof (".text.stub."));
1943
0
  if (name == NULL)
1944
0
    return false;
1945
0
  sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1946
1947
  /* Create the section.  */
1948
0
  mips_elf_get_la25_target (stub, &input_section);
1949
0
  s = htab->add_stub_section (name, input_section,
1950
0
            input_section->output_section);
1951
0
  if (s == NULL)
1952
0
    return false;
1953
1954
  /* Make sure that any padding goes before the stub.  */
1955
0
  align = input_section->alignment_power;
1956
0
  if (!bfd_set_section_alignment (s, align))
1957
0
    return false;
1958
0
  if (align > 3)
1959
0
    s->size = (1 << align) - 8;
1960
1961
  /* Create a symbol for the stub.  */
1962
0
  mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1963
0
  stub->stub_section = s;
1964
0
  stub->offset = s->size;
1965
1966
  /* Allocate room for it.  */
1967
0
  s->size += 8;
1968
0
  return true;
1969
0
}
1970
1971
/* STUB describes an la25 stub that we have decided to implement
1972
   with a separate trampoline.  Allocate room for it and redirect
1973
   the function symbol to it.  */
1974
1975
static bool
1976
mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1977
            struct bfd_link_info *info)
1978
0
{
1979
0
  struct mips_elf_link_hash_table *htab;
1980
0
  asection *s;
1981
1982
0
  htab = mips_elf_hash_table (info);
1983
0
  if (htab == NULL)
1984
0
    return false;
1985
1986
  /* Create a trampoline section, if we haven't already.  */
1987
0
  s = htab->strampoline;
1988
0
  if (s == NULL)
1989
0
    {
1990
0
      asection *input_section = stub->h->root.root.u.def.section;
1991
0
      s = htab->add_stub_section (".text", NULL,
1992
0
          input_section->output_section);
1993
0
      if (s == NULL || !bfd_set_section_alignment (s, 4))
1994
0
  return false;
1995
0
      htab->strampoline = s;
1996
0
    }
1997
1998
  /* Create a symbol for the stub.  */
1999
0
  mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
2000
0
  stub->stub_section = s;
2001
0
  stub->offset = s->size;
2002
2003
  /* Allocate room for it.  */
2004
0
  s->size += 16;
2005
0
  return true;
2006
0
}
2007
2008
/* H describes a symbol that needs an la25 stub.  Make sure that an
2009
   appropriate stub exists and point H at it.  */
2010
2011
static bool
2012
mips_elf_add_la25_stub (struct bfd_link_info *info,
2013
      struct mips_elf_link_hash_entry *h)
2014
0
{
2015
0
  struct mips_elf_link_hash_table *htab;
2016
0
  struct mips_elf_la25_stub search, *stub;
2017
0
  bool use_trampoline_p;
2018
0
  asection *s;
2019
0
  bfd_vma value;
2020
0
  void **slot;
2021
2022
  /* Describe the stub we want.  */
2023
0
  search.stub_section = NULL;
2024
0
  search.offset = 0;
2025
0
  search.h = h;
2026
2027
  /* See if we've already created an equivalent stub.  */
2028
0
  htab = mips_elf_hash_table (info);
2029
0
  if (htab == NULL)
2030
0
    return false;
2031
2032
0
  slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
2033
0
  if (slot == NULL)
2034
0
    return false;
2035
2036
0
  stub = (struct mips_elf_la25_stub *) *slot;
2037
0
  if (stub != NULL)
2038
0
    {
2039
      /* We can reuse the existing stub.  */
2040
0
      h->la25_stub = stub;
2041
0
      return true;
2042
0
    }
2043
2044
  /* Create a permanent copy of ENTRY and add it to the hash table.  */
2045
0
  stub = bfd_malloc (sizeof (search));
2046
0
  if (stub == NULL)
2047
0
    return false;
2048
0
  *stub = search;
2049
0
  *slot = stub;
2050
2051
  /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
2052
     of the section and if we would need no more than 2 nops.  */
2053
0
  value = mips_elf_get_la25_target (stub, &s);
2054
0
  if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
2055
0
    value &= ~1;
2056
0
  use_trampoline_p = (value != 0 || s->alignment_power > 4);
2057
2058
0
  h->la25_stub = stub;
2059
0
  return (use_trampoline_p
2060
0
    ? mips_elf_add_la25_trampoline (stub, info)
2061
0
    : mips_elf_add_la25_intro (stub, info));
2062
0
}
2063
2064
/* A mips_elf_link_hash_traverse callback that is called before sizing
2065
   sections.  DATA points to a mips_htab_traverse_info structure.  */
2066
2067
static bool
2068
mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
2069
0
{
2070
0
  struct mips_htab_traverse_info *hti;
2071
2072
0
  hti = (struct mips_htab_traverse_info *) data;
2073
0
  if (!bfd_link_relocatable (hti->info))
2074
0
    mips_elf_check_mips16_stubs (hti->info, h);
2075
2076
0
  if (mips_elf_local_pic_function_p (h))
2077
0
    {
2078
      /* PR 12845: If H is in a section that has been garbage
2079
   collected it will have its output section set to *ABS*.  */
2080
0
      if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
2081
0
  return true;
2082
2083
      /* H is a function that might need $25 to be valid on entry.
2084
   If we're creating a non-PIC relocatable object, mark H as
2085
   being PIC.  If we're creating a non-relocatable object with
2086
   non-PIC branches and jumps to H, make sure that H has an la25
2087
   stub.  */
2088
0
      if (bfd_link_relocatable (hti->info))
2089
0
  {
2090
0
    if (!PIC_OBJECT_P (hti->output_bfd))
2091
0
      h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
2092
0
  }
2093
0
      else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
2094
0
  {
2095
0
    hti->error = true;
2096
0
    return false;
2097
0
  }
2098
0
    }
2099
0
  return true;
2100
0
}
2101

2102
/* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
2103
   Most mips16 instructions are 16 bits, but these instructions
2104
   are 32 bits.
2105
2106
   The format of these instructions is:
2107
2108
   +--------------+--------------------------------+
2109
   |     JALX     | X|   Imm 20:16  |   Imm 25:21  |
2110
   +--------------+--------------------------------+
2111
   |        Immediate  15:0      |
2112
   +-----------------------------------------------+
2113
2114
   JALX is the 5-bit value 00011.  X is 0 for jal, 1 for jalx.
2115
   Note that the immediate value in the first word is swapped.
2116
2117
   When producing a relocatable object file, R_MIPS16_26 is
2118
   handled mostly like R_MIPS_26.  In particular, the addend is
2119
   stored as a straight 26-bit value in a 32-bit instruction.
2120
   (gas makes life simpler for itself by never adjusting a
2121
   R_MIPS16_26 reloc to be against a section, so the addend is
2122
   always zero).  However, the 32 bit instruction is stored as 2
2123
   16-bit values, rather than a single 32-bit value.  In a
2124
   big-endian file, the result is the same; in a little-endian
2125
   file, the two 16-bit halves of the 32 bit value are swapped.
2126
   This is so that a disassembler can recognize the jal
2127
   instruction.
2128
2129
   When doing a final link, R_MIPS16_26 is treated as a 32 bit
2130
   instruction stored as two 16-bit values.  The addend A is the
2131
   contents of the targ26 field.  The calculation is the same as
2132
   R_MIPS_26.  When storing the calculated value, reorder the
2133
   immediate value as shown above, and don't forget to store the
2134
   value as two 16-bit values.
2135
2136
   To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2137
   defined as
2138
2139
   big-endian:
2140
   +--------+----------------------+
2141
   |      |        |
2142
   |      |  targ26-16     |
2143
   |31    26|25       0|
2144
   +--------+----------------------+
2145
2146
   little-endian:
2147
   +----------+------+-------------+
2148
   |        |      |       |
2149
   |  sub1    |      |     sub2    |
2150
   |0      9|10  15|16   31|
2151
   +----------+--------------------+
2152
   where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2153
   ((sub1 << 16) | sub2)).
2154
2155
   When producing a relocatable object file, the calculation is
2156
   (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2157
   When producing a fully linked file, the calculation is
2158
   let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2159
   ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2160
2161
   The table below lists the other MIPS16 instruction relocations.
2162
   Each one is calculated in the same way as the non-MIPS16 relocation
2163
   given on the right, but using the extended MIPS16 layout of 16-bit
2164
   immediate fields:
2165
2166
  R_MIPS16_GPREL    R_MIPS_GPREL16
2167
  R_MIPS16_GOT16    R_MIPS_GOT16
2168
  R_MIPS16_CALL16   R_MIPS_CALL16
2169
  R_MIPS16_HI16   R_MIPS_HI16
2170
  R_MIPS16_LO16   R_MIPS_LO16
2171
2172
   A typical instruction will have a format like this:
2173
2174
   +--------------+--------------------------------+
2175
   |    EXTEND    |     Imm 10:5    |   Imm 15:11  |
2176
   +--------------+--------------------------------+
2177
   |    Major     |   rx   |   ry   |   Imm  4:0   |
2178
   +--------------+--------------------------------+
2179
2180
   EXTEND is the five bit value 11110.  Major is the instruction
2181
   opcode.
2182
2183
   All we need to do here is shuffle the bits appropriately.
2184
   As above, the two 16-bit halves must be swapped on a
2185
   little-endian system.
2186
2187
   Finally R_MIPS16_PC16_S1 corresponds to R_MIPS_PC16, however the
2188
   relocatable field is shifted by 1 rather than 2 and the same bit
2189
   shuffling is done as with the relocations above.  */
2190
2191
static inline bool
2192
mips16_reloc_p (int r_type)
2193
6.26k
{
2194
6.26k
  switch (r_type)
2195
6.26k
    {
2196
46
    case R_MIPS16_26:
2197
50
    case R_MIPS16_GPREL:
2198
50
    case R_MIPS16_GOT16:
2199
60
    case R_MIPS16_CALL16:
2200
62
    case R_MIPS16_HI16:
2201
114
    case R_MIPS16_LO16:
2202
114
    case R_MIPS16_TLS_GD:
2203
114
    case R_MIPS16_TLS_LDM:
2204
114
    case R_MIPS16_TLS_DTPREL_HI16:
2205
114
    case R_MIPS16_TLS_DTPREL_LO16:
2206
116
    case R_MIPS16_TLS_GOTTPREL:
2207
116
    case R_MIPS16_TLS_TPREL_HI16:
2208
116
    case R_MIPS16_TLS_TPREL_LO16:
2209
116
    case R_MIPS16_PC16_S1:
2210
116
      return true;
2211
2212
6.14k
    default:
2213
6.14k
      return false;
2214
6.26k
    }
2215
6.26k
}
2216
2217
/* Check if a microMIPS reloc.  */
2218
2219
static inline bool
2220
micromips_reloc_p (unsigned int r_type)
2221
6.34k
{
2222
6.34k
  return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2223
6.34k
}
2224
2225
/* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2226
   on a little-endian system.  This does not apply to R_MICROMIPS_PC7_S1,
2227
   R_MICROMIPS_PC10_S1 and R_MICROMIPS_GPREL7_S2 relocs that apply to
2228
   16-bit instructions.  */
2229
2230
static inline bool
2231
micromips_reloc_shuffle_p (unsigned int r_type)
2232
6.14k
{
2233
6.14k
  return (micromips_reloc_p (r_type)
2234
6.14k
    && r_type != R_MICROMIPS_PC7_S1
2235
6.14k
    && r_type != R_MICROMIPS_PC10_S1
2236
6.14k
    && r_type != R_MICROMIPS_GPREL7_S2);
2237
6.14k
}
2238
2239
static inline bool
2240
got16_reloc_p (int r_type)
2241
0
{
2242
0
  return (r_type == R_MIPS_GOT16
2243
0
    || r_type == R_MIPS16_GOT16
2244
0
    || r_type == R_MICROMIPS_GOT16);
2245
0
}
2246
2247
static inline bool
2248
call16_reloc_p (int r_type)
2249
0
{
2250
0
  return (r_type == R_MIPS_CALL16
2251
0
    || r_type == R_MIPS16_CALL16
2252
0
    || r_type == R_MICROMIPS_CALL16);
2253
0
}
2254
2255
static inline bool
2256
got_disp_reloc_p (unsigned int r_type)
2257
0
{
2258
0
  return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2259
0
}
2260
2261
static inline bool
2262
got_page_reloc_p (unsigned int r_type)
2263
0
{
2264
0
  return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2265
0
}
2266
2267
static inline bool
2268
got_lo16_reloc_p (unsigned int r_type)
2269
0
{
2270
0
  return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2271
0
}
2272
2273
static inline bool
2274
call_hi16_reloc_p (unsigned int r_type)
2275
0
{
2276
0
  return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2277
0
}
2278
2279
static inline bool
2280
call_lo16_reloc_p (unsigned int r_type)
2281
0
{
2282
0
  return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
2283
0
}
2284
2285
static inline bool
2286
hi16_reloc_p (int r_type)
2287
0
{
2288
0
  return (r_type == R_MIPS_HI16
2289
0
    || r_type == R_MIPS16_HI16
2290
0
    || r_type == R_MICROMIPS_HI16
2291
0
    || r_type == R_MIPS_PCHI16);
2292
0
}
2293
2294
static inline bool
2295
lo16_reloc_p (int r_type)
2296
0
{
2297
0
  return (r_type == R_MIPS_LO16
2298
0
    || r_type == R_MIPS16_LO16
2299
0
    || r_type == R_MICROMIPS_LO16
2300
0
    || r_type == R_MIPS_PCLO16);
2301
0
}
2302
2303
static inline bool
2304
mips16_call_reloc_p (int r_type)
2305
0
{
2306
0
  return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2307
0
}
2308
2309
static inline bool
2310
jal_reloc_p (int r_type)
2311
0
{
2312
0
  return (r_type == R_MIPS_26
2313
0
    || r_type == R_MIPS16_26
2314
0
    || r_type == R_MICROMIPS_26_S1);
2315
0
}
2316
2317
static inline bool
2318
b_reloc_p (int r_type)
2319
0
{
2320
0
  return (r_type == R_MIPS_PC26_S2
2321
0
    || r_type == R_MIPS_PC21_S2
2322
0
    || r_type == R_MIPS_PC16
2323
0
    || r_type == R_MIPS_GNU_REL16_S2
2324
0
    || r_type == R_MIPS16_PC16_S1
2325
0
    || r_type == R_MICROMIPS_PC16_S1
2326
0
    || r_type == R_MICROMIPS_PC10_S1
2327
0
    || r_type == R_MICROMIPS_PC7_S1);
2328
0
}
2329
2330
static inline bool
2331
aligned_pcrel_reloc_p (int r_type)
2332
0
{
2333
0
  return (r_type == R_MIPS_PC18_S3
2334
0
    || r_type == R_MIPS_PC19_S2);
2335
0
}
2336
2337
static inline bool
2338
branch_reloc_p (int r_type)
2339
0
{
2340
0
  return (r_type == R_MIPS_26
2341
0
    || r_type == R_MIPS_PC26_S2
2342
0
    || r_type == R_MIPS_PC21_S2
2343
0
    || r_type == R_MIPS_PC16
2344
0
    || r_type == R_MIPS_GNU_REL16_S2);
2345
0
}
2346
2347
static inline bool
2348
mips16_branch_reloc_p (int r_type)
2349
0
{
2350
0
  return (r_type == R_MIPS16_26
2351
0
    || r_type == R_MIPS16_PC16_S1);
2352
0
}
2353
2354
static inline bool
2355
micromips_branch_reloc_p (int r_type)
2356
0
{
2357
0
  return (r_type == R_MICROMIPS_26_S1
2358
0
    || r_type == R_MICROMIPS_PC16_S1
2359
0
    || r_type == R_MICROMIPS_PC10_S1
2360
0
    || r_type == R_MICROMIPS_PC7_S1);
2361
0
}
2362
2363
static inline bool
2364
tls_gd_reloc_p (unsigned int r_type)
2365
0
{
2366
0
  return (r_type == R_MIPS_TLS_GD
2367
0
    || r_type == R_MIPS16_TLS_GD
2368
0
    || r_type == R_MICROMIPS_TLS_GD);
2369
0
}
2370
2371
static inline bool
2372
tls_ldm_reloc_p (unsigned int r_type)
2373
0
{
2374
0
  return (r_type == R_MIPS_TLS_LDM
2375
0
    || r_type == R_MIPS16_TLS_LDM
2376
0
    || r_type == R_MICROMIPS_TLS_LDM);
2377
0
}
2378
2379
static inline bool
2380
tls_gottprel_reloc_p (unsigned int r_type)
2381
0
{
2382
0
  return (r_type == R_MIPS_TLS_GOTTPREL
2383
0
    || r_type == R_MIPS16_TLS_GOTTPREL
2384
0
    || r_type == R_MICROMIPS_TLS_GOTTPREL);
2385
0
}
2386
2387
static inline bool
2388
needs_shuffle (int r_type)
2389
6.26k
{
2390
6.26k
  return mips16_reloc_p (r_type) || micromips_reloc_shuffle_p (r_type);
2391
6.26k
}
2392
2393
void
2394
_bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2395
             bool jal_shuffle, bfd_byte *data)
2396
3.12k
{
2397
3.12k
  bfd_vma first, second, val;
2398
2399
3.12k
  if (!needs_shuffle (r_type))
2400
3.02k
    return;
2401
2402
  /* Pick up the first and second halfwords of the instruction.  */
2403
98
  first = bfd_get_16 (abfd, data);
2404
98
  second = bfd_get_16 (abfd, data + 2);
2405
98
  if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2406
65
    val = first << 16 | second;
2407
33
  else if (r_type != R_MIPS16_26)
2408
33
    val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2409
33
     | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
2410
0
  else
2411
0
    val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2412
0
     | ((first & 0x1f) << 21) | second);
2413
98
  bfd_put_32 (abfd, val, data);
2414
98
}
2415
2416
void
2417
_bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2418
           bool jal_shuffle, bfd_byte *data)
2419
3.12k
{
2420
3.12k
  bfd_vma first, second, val;
2421
2422
3.12k
  if (!needs_shuffle (r_type))
2423
3.02k
    return;
2424
2425
98
  val = bfd_get_32 (abfd, data);
2426
98
  if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2427
65
    {
2428
65
      second = val & 0xffff;
2429
65
      first = val >> 16;
2430
65
    }
2431
33
  else if (r_type != R_MIPS16_26)
2432
33
    {
2433
33
      second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2434
33
      first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
2435
33
    }
2436
0
  else
2437
0
    {
2438
0
      second = val & 0xffff;
2439
0
      first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2440
0
         | ((val >> 21) & 0x1f);
2441
0
    }
2442
98
  bfd_put_16 (abfd, second, data + 2);
2443
98
  bfd_put_16 (abfd, first, data);
2444
98
}
2445
2446
/* Perform reloc offset checking.
2447
   We can only use bfd_reloc_offset_in_range, which takes into account
2448
   the size of the field being relocated, when section contents will
2449
   be accessed because mips object files may use relocations that seem
2450
   to access beyond section limits.
2451
   gas/testsuite/gas/mips/dla-reloc.s is an example that puts
2452
   R_MIPS_SUB, a 64-bit relocation, on the last instruction in the
2453
   section.  The R_MIPS_SUB applies to the addend for the next reloc
2454
   rather than the section contents.
2455
2456
   CHECK is CHECK_STD for the standard bfd_reloc_offset_in_range check,
2457
   CHECK_INPLACE to only check partial_inplace relocs, and
2458
   CHECK_SHUFFLE to only check relocs that shuffle/unshuffle.  */
2459
2460
bool
2461
_bfd_mips_reloc_offset_in_range (bfd *abfd, asection *input_section,
2462
         arelent *reloc_entry, enum reloc_check check)
2463
3.16k
{
2464
3.16k
  if (check == check_inplace && !reloc_entry->howto->partial_inplace)
2465
3
    return true;
2466
3.16k
  if (check == check_shuffle && !needs_shuffle (reloc_entry->howto->type))
2467
4
    return true;
2468
3.16k
  return bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
2469
3.16k
            input_section, reloc_entry->address);
2470
3.16k
}
2471
2472
bfd_reloc_status_type
2473
_bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2474
             arelent *reloc_entry, asection *input_section,
2475
             bool relocatable, void *data, bfd_vma gp)
2476
9
{
2477
9
  bfd_vma relocation;
2478
9
  bfd_signed_vma val;
2479
9
  bfd_reloc_status_type status;
2480
2481
9
  if (bfd_is_com_section (symbol->section))
2482
0
    relocation = 0;
2483
9
  else
2484
9
    relocation = symbol->value;
2485
2486
9
  if (symbol->section->output_section != NULL)
2487
9
    {
2488
9
      relocation += symbol->section->output_section->vma;
2489
9
      relocation += symbol->section->output_offset;
2490
9
    }
2491
2492
  /* Set val to the offset into the section or symbol.  */
2493
9
  val = reloc_entry->addend;
2494
2495
9
  _bfd_mips_elf_sign_extend (val, 16);
2496
2497
  /* Adjust val for the final section location and GP value.  If we
2498
     are producing relocatable output, we don't want to do this for
2499
     an external symbol.  */
2500
9
  if (! relocatable
2501
9
      || (symbol->flags & BSF_SECTION_SYM) != 0)
2502
9
    val += relocation - gp;
2503
2504
9
  if (reloc_entry->howto->partial_inplace)
2505
0
    {
2506
0
      if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2507
0
              reloc_entry->address))
2508
0
  return bfd_reloc_outofrange;
2509
2510
0
      status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2511
0
               (bfd_byte *) data
2512
0
               + reloc_entry->address);
2513
0
      if (status != bfd_reloc_ok)
2514
0
  return status;
2515
0
    }
2516
9
  else
2517
9
    reloc_entry->addend = val;
2518
2519
9
  if (relocatable)
2520
0
    reloc_entry->address += input_section->output_offset;
2521
2522
9
  return bfd_reloc_ok;
2523
9
}
2524
2525
/* A howto special_function for REL *HI16 relocations.  We can only
2526
   calculate the correct value once we've seen the partnering
2527
   *LO16 relocation, so just save the information for later.
2528
2529
   The ABI requires that the *LO16 immediately follow the *HI16.
2530
   However, as a GNU extension, we permit an arbitrary number of
2531
   *HI16s to be associated with a single *LO16.  This significantly
2532
   simplies the relocation handling in gcc.  */
2533
2534
bfd_reloc_status_type
2535
_bfd_mips_elf_hi16_reloc (bfd *abfd, arelent *reloc_entry,
2536
        asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2537
        asection *input_section, bfd *output_bfd,
2538
        char **error_message ATTRIBUTE_UNUSED)
2539
20
{
2540
20
  struct mips_hi16 *n;
2541
20
  struct mips_elf_obj_tdata *tdata;
2542
2543
20
  if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2544
0
    return bfd_reloc_outofrange;
2545
2546
20
  n = bfd_malloc (sizeof *n);
2547
20
  if (n == NULL)
2548
0
    return bfd_reloc_outofrange;
2549
2550
20
  tdata = mips_elf_tdata (abfd);
2551
20
  n->next = tdata->mips_hi16_list;
2552
20
  n->data = data;
2553
20
  n->input_section = input_section;
2554
20
  n->rel = *reloc_entry;
2555
20
  tdata->mips_hi16_list = n;
2556
2557
20
  if (output_bfd != NULL)
2558
0
    reloc_entry->address += input_section->output_offset;
2559
2560
20
  return bfd_reloc_ok;
2561
20
}
2562
2563
/* A howto special_function for REL R_MIPS*_GOT16 relocations.  This is just
2564
   like any other 16-bit relocation when applied to global symbols, but is
2565
   treated in the same as R_MIPS_HI16 when applied to local symbols.  */
2566
2567
bfd_reloc_status_type
2568
_bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2569
         void *data, asection *input_section,
2570
         bfd *output_bfd, char **error_message)
2571
34
{
2572
34
  if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2573
34
      || bfd_is_und_section (bfd_asymbol_section (symbol))
2574
34
      || bfd_is_com_section (bfd_asymbol_section (symbol)))
2575
    /* The relocation is against a global symbol.  */
2576
20
    return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2577
20
          input_section, output_bfd,
2578
20
          error_message);
2579
2580
14
  return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2581
14
           input_section, output_bfd, error_message);
2582
34
}
2583
2584
/* A howto special_function for REL *LO16 relocations.  The *LO16 itself
2585
   is a straightforward 16 bit inplace relocation, but we must deal with
2586
   any partnering high-part relocations as well.  */
2587
2588
bfd_reloc_status_type
2589
_bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2590
        void *data, asection *input_section,
2591
        bfd *output_bfd, char **error_message)
2592
21
{
2593
21
  bfd_vma vallo;
2594
21
  bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2595
21
  struct mips_elf_obj_tdata *tdata;
2596
2597
21
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2598
21
          reloc_entry->address))
2599
1
    return bfd_reloc_outofrange;
2600
2601
20
  _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
2602
20
         location);
2603
  /* The high 16 bits of the addend are stored in the high insn, the
2604
     low 16 bits in the low insn, but there is a catch:  You can't
2605
     just concatenate the high and low parts.  The high part of the
2606
     addend is adjusted for the fact that the low part is sign
2607
     extended.  For example, an addend of 0x38000 would have 0x0004 in
2608
     the high part and 0x8000 (=0xff..f8000) in the low part.
2609
     To extract the actual addend, calculate (a)
2610
     ((hi & 0xffff) << 16) + ((lo & 0xffff) ^ 0x8000) - 0x8000.
2611
     We will be applying (symbol + addend) & 0xffff to the low insn,
2612
     and we want to apply (b) (symbol + addend + 0x8000) >> 16 to the
2613
     high insn (the +0x8000 adjusting for when the applied low part is
2614
     negative).  Substituting (a) into (b) and recognising that
2615
     (hi & 0xffff) is already in the high insn gives a high part
2616
     addend adjustment of (lo & 0xffff) ^ 0x8000.  */
2617
20
  vallo = (bfd_get_32 (abfd, location) & 0xffff) ^ 0x8000;
2618
20
  _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
2619
20
             location);
2620
2621
20
  tdata = mips_elf_tdata (abfd);
2622
26
  while (tdata->mips_hi16_list != NULL)
2623
6
    {
2624
6
      bfd_reloc_status_type ret;
2625
6
      struct mips_hi16 *hi;
2626
2627
6
      hi = tdata->mips_hi16_list;
2628
2629
      /* R_MIPS*_GOT16 relocations are something of a special case.  We
2630
   want to install the addend in the same way as for a R_MIPS*_HI16
2631
   relocation (with a rightshift of 16).  However, since GOT16
2632
   relocations can also be used with global symbols, their howto
2633
   has a rightshift of 0.  */
2634
6
      if (hi->rel.howto->type == R_MIPS_GOT16)
2635
0
  hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, false);
2636
6
      else if (hi->rel.howto->type == R_MIPS16_GOT16)
2637
1
  hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, false);
2638
5
      else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2639
5
  hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, false);
2640
2641
6
      hi->rel.addend += vallo;
2642
2643
6
      ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2644
6
           hi->input_section, output_bfd,
2645
6
           error_message);
2646
6
      if (ret != bfd_reloc_ok)
2647
0
  return ret;
2648
2649
6
      tdata->mips_hi16_list = hi->next;
2650
6
      free (hi);
2651
6
    }
2652
2653
20
  return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2654
20
              input_section, output_bfd,
2655
20
              error_message);
2656
20
}
2657
2658
/* A generic howto special_function.  This calculates and installs the
2659
   relocation itself, thus avoiding the oft-discussed problems in
2660
   bfd_perform_relocation and bfd_install_relocation.  */
2661
2662
bfd_reloc_status_type
2663
_bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2664
           asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2665
           asection *input_section, bfd *output_bfd,
2666
           char **error_message ATTRIBUTE_UNUSED)
2667
3.15k
{
2668
3.15k
  bfd_signed_vma val;
2669
3.15k
  bfd_reloc_status_type status;
2670
3.15k
  bool relocatable;
2671
2672
3.15k
  relocatable = (output_bfd != NULL);
2673
2674
3.15k
  if (!_bfd_mips_reloc_offset_in_range (abfd, input_section, reloc_entry,
2675
3.15k
          (relocatable
2676
3.15k
           ? check_inplace : check_std)))
2677
54
    return bfd_reloc_outofrange;
2678
2679
  /* Build up the field adjustment in VAL.  */
2680
3.10k
  val = 0;
2681
3.10k
  if ((!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2682
3.10k
      && symbol->section->output_section != NULL)
2683
3.10k
    {
2684
      /* Either we're calculating the final field value or we have a
2685
   relocation against a section symbol.  Add in the section's
2686
   offset or address.  */
2687
3.10k
      val += symbol->section->output_section->vma;
2688
3.10k
      val += symbol->section->output_offset;
2689
3.10k
    }
2690
2691
3.10k
  if (!relocatable)
2692
3.10k
    {
2693
      /* We're calculating the final field value.  Add in the symbol's value
2694
   and, if pc-relative, subtract the address of the field itself.  */
2695
3.10k
      val += symbol->value;
2696
3.10k
      if (reloc_entry->howto->pc_relative)
2697
12
  {
2698
12
    val -= input_section->output_section->vma;
2699
12
    val -= input_section->output_offset;
2700
12
    val -= reloc_entry->address;
2701
12
  }
2702
3.10k
    }
2703
2704
  /* VAL is now the final adjustment.  If we're keeping this relocation
2705
     in the output file, and if the relocation uses a separate addend,
2706
     we just need to add VAL to that addend.  Otherwise we need to add
2707
     VAL to the relocation field itself.  */
2708
3.10k
  if (relocatable && !reloc_entry->howto->partial_inplace)
2709
0
    reloc_entry->addend += val;
2710
3.10k
  else
2711
3.10k
    {
2712
3.10k
      bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2713
2714
      /* Add in the separate addend, if any.  */
2715
3.10k
      val += reloc_entry->addend;
2716
2717
      /* Add VAL to the relocation field.  */
2718
3.10k
      _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
2719
3.10k
             location);
2720
3.10k
      status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2721
3.10k
               location);
2722
3.10k
      _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
2723
3.10k
           location);
2724
2725
3.10k
      if (status != bfd_reloc_ok)
2726
47
  return status;
2727
3.10k
    }
2728
2729
3.05k
  if (relocatable)
2730
0
    reloc_entry->address += input_section->output_offset;
2731
2732
3.05k
  return bfd_reloc_ok;
2733
3.10k
}
2734

2735
/* Swap an entry in a .gptab section.  Note that these routines rely
2736
   on the equivalence of the two elements of the union.  */
2737
2738
static void
2739
bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2740
            Elf32_gptab *in)
2741
0
{
2742
0
  in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2743
0
  in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2744
0
}
2745
2746
static void
2747
bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2748
             Elf32_External_gptab *ex)
2749
0
{
2750
0
  H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2751
0
  H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2752
0
}
2753
2754
static void
2755
bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2756
        Elf32_External_compact_rel *ex)
2757
0
{
2758
0
  H_PUT_32 (abfd, in->id1, ex->id1);
2759
0
  H_PUT_32 (abfd, in->num, ex->num);
2760
0
  H_PUT_32 (abfd, in->id2, ex->id2);
2761
0
  H_PUT_32 (abfd, in->offset, ex->offset);
2762
0
  H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2763
0
  H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2764
0
}
2765
2766
static void
2767
bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2768
         Elf32_External_crinfo *ex)
2769
0
{
2770
0
  unsigned long l;
2771
2772
0
  l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2773
0
       | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2774
0
       | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2775
0
       | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2776
0
  H_PUT_32 (abfd, l, ex->info);
2777
0
  H_PUT_32 (abfd, in->konst, ex->konst);
2778
0
  H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2779
0
}
2780

2781
/* A .reginfo section holds a single Elf32_RegInfo structure.  These
2782
   routines swap this structure in and out.  They are used outside of
2783
   BFD, so they are globally visible.  */
2784
2785
void
2786
bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2787
        Elf32_RegInfo *in)
2788
36
{
2789
36
  in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2790
36
  in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2791
36
  in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2792
36
  in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2793
36
  in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2794
36
  in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2795
36
}
2796
2797
void
2798
bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2799
         Elf32_External_RegInfo *ex)
2800
0
{
2801
0
  H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2802
0
  H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2803
0
  H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2804
0
  H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2805
0
  H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2806
0
  H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2807
0
}
2808
2809
/* In the 64 bit ABI, the .MIPS.options section holds register
2810
   information in an Elf64_Reginfo structure.  These routines swap
2811
   them in and out.  They are globally visible because they are used
2812
   outside of BFD.  These routines are here so that gas can call them
2813
   without worrying about whether the 64 bit ABI has been included.  */
2814
2815
void
2816
bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2817
        Elf64_Internal_RegInfo *in)
2818
0
{
2819
0
  in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2820
0
  in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2821
0
  in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2822
0
  in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2823
0
  in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2824
0
  in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2825
0
  in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2826
0
}
2827
2828
void
2829
bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2830
         Elf64_External_RegInfo *ex)
2831
0
{
2832
0
  H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2833
0
  H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2834
0
  H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2835
0
  H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2836
0
  H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2837
0
  H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2838
0
  H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2839
0
}
2840
2841
/* Swap in an options header.  */
2842
2843
void
2844
bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2845
            Elf_Internal_Options *in)
2846
48
{
2847
48
  in->kind = H_GET_8 (abfd, ex->kind);
2848
48
  in->size = H_GET_8 (abfd, ex->size);
2849
48
  in->section = H_GET_16 (abfd, ex->section);
2850
48
  in->info = H_GET_32 (abfd, ex->info);
2851
48
}
2852
2853
/* Swap out an options header.  */
2854
2855
void
2856
bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2857
             Elf_External_Options *ex)
2858
0
{
2859
0
  H_PUT_8 (abfd, in->kind, ex->kind);
2860
0
  H_PUT_8 (abfd, in->size, ex->size);
2861
0
  H_PUT_16 (abfd, in->section, ex->section);
2862
0
  H_PUT_32 (abfd, in->info, ex->info);
2863
0
}
2864
2865
/* Swap in an abiflags structure.  */
2866
2867
void
2868
bfd_mips_elf_swap_abiflags_v0_in (bfd *abfd,
2869
          const Elf_External_ABIFlags_v0 *ex,
2870
          Elf_Internal_ABIFlags_v0 *in)
2871
0
{
2872
0
  in->version = H_GET_16 (abfd, ex->version);
2873
0
  in->isa_level = H_GET_8 (abfd, ex->isa_level);
2874
0
  in->isa_rev = H_GET_8 (abfd, ex->isa_rev);
2875
0
  in->gpr_size = H_GET_8 (abfd, ex->gpr_size);
2876
0
  in->cpr1_size = H_GET_8 (abfd, ex->cpr1_size);
2877
0
  in->cpr2_size = H_GET_8 (abfd, ex->cpr2_size);
2878
0
  in->fp_abi = H_GET_8 (abfd, ex->fp_abi);
2879
0
  in->isa_ext = H_GET_32 (abfd, ex->isa_ext);
2880
0
  in->ases = H_GET_32 (abfd, ex->ases);
2881
0
  in->flags1 = H_GET_32 (abfd, ex->flags1);
2882
0
  in->flags2 = H_GET_32 (abfd, ex->flags2);
2883
0
}
2884
2885
/* Swap out an abiflags structure.  */
2886
2887
void
2888
bfd_mips_elf_swap_abiflags_v0_out (bfd *abfd,
2889
           const Elf_Internal_ABIFlags_v0 *in,
2890
           Elf_External_ABIFlags_v0 *ex)
2891
0
{
2892
0
  H_PUT_16 (abfd, in->version, ex->version);
2893
0
  H_PUT_8 (abfd, in->isa_level, ex->isa_level);
2894
0
  H_PUT_8 (abfd, in->isa_rev, ex->isa_rev);
2895
0
  H_PUT_8 (abfd, in->gpr_size, ex->gpr_size);
2896
0
  H_PUT_8 (abfd, in->cpr1_size, ex->cpr1_size);
2897
0
  H_PUT_8 (abfd, in->cpr2_size, ex->cpr2_size);
2898
0
  H_PUT_8 (abfd, in->fp_abi, ex->fp_abi);
2899
0
  H_PUT_32 (abfd, in->isa_ext, ex->isa_ext);
2900
0
  H_PUT_32 (abfd, in->ases, ex->ases);
2901
0
  H_PUT_32 (abfd, in->flags1, ex->flags1);
2902
0
  H_PUT_32 (abfd, in->flags2, ex->flags2);
2903
0
}
2904

2905
/* This function is called via qsort() to sort the dynamic relocation
2906
   entries by increasing r_symndx value.  */
2907
2908
static int
2909
sort_dynamic_relocs (const void *arg1, const void *arg2)
2910
0
{
2911
0
  Elf_Internal_Rela int_reloc1;
2912
0
  Elf_Internal_Rela int_reloc2;
2913
0
  int diff;
2914
2915
0
  bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2916
0
  bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2917
2918
0
  diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2919
0
  if (diff != 0)
2920
0
    return diff;
2921
2922
0
  if (int_reloc1.r_offset < int_reloc2.r_offset)
2923
0
    return -1;
2924
0
  if (int_reloc1.r_offset > int_reloc2.r_offset)
2925
0
    return 1;
2926
0
  return 0;
2927
0
}
2928
2929
/* Like sort_dynamic_relocs, but used for elf64 relocations.  */
2930
2931
static int
2932
sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2933
      const void *arg2 ATTRIBUTE_UNUSED)
2934
0
{
2935
0
#ifdef BFD64
2936
0
  Elf_Internal_Rela int_reloc1[3];
2937
0
  Elf_Internal_Rela int_reloc2[3];
2938
2939
0
  (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2940
0
    (reldyn_sorting_bfd, arg1, int_reloc1);
2941
0
  (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2942
0
    (reldyn_sorting_bfd, arg2, int_reloc2);
2943
2944
0
  if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2945
0
    return -1;
2946
0
  if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2947
0
    return 1;
2948
2949
0
  if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2950
0
    return -1;
2951
0
  if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2952
0
    return 1;
2953
0
  return 0;
2954
#else
2955
  abort ();
2956
#endif
2957
0
}
2958
2959
2960
/* This routine is used to write out ECOFF debugging external symbol
2961
   information.  It is called via mips_elf_link_hash_traverse.  The
2962
   ECOFF external symbol information must match the ELF external
2963
   symbol information.  Unfortunately, at this point we don't know
2964
   whether a symbol is required by reloc information, so the two
2965
   tables may wind up being different.  We must sort out the external
2966
   symbol information before we can set the final size of the .mdebug
2967
   section, and we must set the size of the .mdebug section before we
2968
   can relocate any sections, and we can't know which symbols are
2969
   required by relocation until we relocate the sections.
2970
   Fortunately, it is relatively unlikely that any symbol will be
2971
   stripped but required by a reloc.  In particular, it can not happen
2972
   when generating a final executable.  */
2973
2974
static bool
2975
mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2976
0
{
2977
0
  struct extsym_info *einfo = data;
2978
0
  bool strip;
2979
0
  asection *sec, *output_section;
2980
2981
0
  if (h->root.indx == -2)
2982
0
    strip = false;
2983
0
  else if ((h->root.def_dynamic
2984
0
      || h->root.ref_dynamic
2985
0
      || h->root.type == bfd_link_hash_new)
2986
0
     && !h->root.def_regular
2987
0
     && !h->root.ref_regular)
2988
0
    strip = true;
2989
0
  else if (einfo->info->strip == strip_all
2990
0
     || (einfo->info->strip == strip_some
2991
0
         && bfd_hash_lookup (einfo->info->keep_hash,
2992
0
           h->root.root.root.string,
2993
0
           false, false) == NULL))
2994
0
    strip = true;
2995
0
  else
2996
0
    strip = false;
2997
2998
0
  if (strip)
2999
0
    return true;
3000
3001
0
  if (h->esym.ifd == -2)
3002
0
    {
3003
0
      h->esym.jmptbl = 0;
3004
0
      h->esym.cobol_main = 0;
3005
0
      h->esym.weakext = 0;
3006
0
      h->esym.reserved = 0;
3007
0
      h->esym.ifd = ifdNil;
3008
0
      h->esym.asym.value = 0;
3009
0
      h->esym.asym.st = stGlobal;
3010
3011
0
      if (h->root.root.type == bfd_link_hash_undefined
3012
0
    || h->root.root.type == bfd_link_hash_undefweak)
3013
0
  {
3014
0
    const char *name;
3015
3016
    /* Use undefined class.  Also, set class and type for some
3017
       special symbols.  */
3018
0
    name = h->root.root.root.string;
3019
0
    if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
3020
0
        || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
3021
0
      {
3022
0
        h->esym.asym.sc = scData;
3023
0
        h->esym.asym.st = stLabel;
3024
0
        h->esym.asym.value = 0;
3025
0
      }
3026
0
    else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
3027
0
      {
3028
0
        h->esym.asym.sc = scAbs;
3029
0
        h->esym.asym.st = stLabel;
3030
0
        h->esym.asym.value =
3031
0
    mips_elf_hash_table (einfo->info)->procedure_count;
3032
0
      }
3033
0
    else
3034
0
      h->esym.asym.sc = scUndefined;
3035
0
  }
3036
0
      else if (h->root.root.type != bfd_link_hash_defined
3037
0
    && h->root.root.type != bfd_link_hash_defweak)
3038
0
  h->esym.asym.sc = scAbs;
3039
0
      else
3040
0
  {
3041
0
    const char *name;
3042
3043
0
    sec = h->root.root.u.def.section;
3044
0
    output_section = sec->output_section;
3045
3046
    /* When making a shared library and symbol h is the one from
3047
       the another shared library, OUTPUT_SECTION may be null.  */
3048
0
    if (output_section == NULL)
3049
0
      h->esym.asym.sc = scUndefined;
3050
0
    else
3051
0
      {
3052
0
        name = bfd_section_name (output_section);
3053
3054
0
        if (strcmp (name, ".text") == 0)
3055
0
    h->esym.asym.sc = scText;
3056
0
        else if (strcmp (name, ".data") == 0)
3057
0
    h->esym.asym.sc = scData;
3058
0
        else if (strcmp (name, ".sdata") == 0)
3059
0
    h->esym.asym.sc = scSData;
3060
0
        else if (strcmp (name, ".rodata") == 0
3061
0
           || strcmp (name, ".rdata") == 0)
3062
0
    h->esym.asym.sc = scRData;
3063
0
        else if (strcmp (name, ".bss") == 0)
3064
0
    h->esym.asym.sc = scBss;
3065
0
        else if (strcmp (name, ".sbss") == 0)
3066
0
    h->esym.asym.sc = scSBss;
3067
0
        else if (strcmp (name, ".init") == 0)
3068
0
    h->esym.asym.sc = scInit;
3069
0
        else if (strcmp (name, ".fini") == 0)
3070
0
    h->esym.asym.sc = scFini;
3071
0
        else
3072
0
    h->esym.asym.sc = scAbs;
3073
0
      }
3074
0
  }
3075
3076
0
      h->esym.asym.reserved = 0;
3077
0
      h->esym.asym.index = indexNil;
3078
0
    }
3079
3080
0
  if (h->root.root.type == bfd_link_hash_common)
3081
0
    h->esym.asym.value = h->root.root.u.c.size;
3082
0
  else if (h->root.root.type == bfd_link_hash_defined
3083
0
     || h->root.root.type == bfd_link_hash_defweak)
3084
0
    {
3085
0
      if (h->esym.asym.sc == scCommon)
3086
0
  h->esym.asym.sc = scBss;
3087
0
      else if (h->esym.asym.sc == scSCommon)
3088
0
  h->esym.asym.sc = scSBss;
3089
3090
0
      sec = h->root.root.u.def.section;
3091
0
      output_section = sec->output_section;
3092
0
      if (output_section != NULL)
3093
0
  h->esym.asym.value = (h->root.root.u.def.value
3094
0
            + sec->output_offset
3095
0
            + output_section->vma);
3096
0
      else
3097
0
  h->esym.asym.value = 0;
3098
0
    }
3099
0
  else
3100
0
    {
3101
0
      struct mips_elf_link_hash_entry *hd = h;
3102
3103
0
      while (hd->root.root.type == bfd_link_hash_indirect)
3104
0
  hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
3105
3106
0
      if (hd->needs_lazy_stub)
3107
0
  {
3108
0
    BFD_ASSERT (hd->root.plt.plist != NULL);
3109
0
    BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
3110
    /* Set type and value for a symbol with a function stub.  */
3111
0
    h->esym.asym.st = stProc;
3112
0
    sec = hd->root.root.u.def.section;
3113
0
    if (sec == NULL)
3114
0
      h->esym.asym.value = 0;
3115
0
    else
3116
0
      {
3117
0
        output_section = sec->output_section;
3118
0
        if (output_section != NULL)
3119
0
    h->esym.asym.value = (hd->root.plt.plist->stub_offset
3120
0
              + sec->output_offset
3121
0
              + output_section->vma);
3122
0
        else
3123
0
    h->esym.asym.value = 0;
3124
0
      }
3125
0
  }
3126
0
    }
3127
3128
0
  if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3129
0
              h->root.root.root.string,
3130
0
              &h->esym))
3131
0
    {
3132
0
      einfo->failed = true;
3133
0
      return false;
3134
0
    }
3135
3136
0
  return true;
3137
0
}
3138
3139
/* A comparison routine used to sort .gptab entries.  */
3140
3141
static int
3142
gptab_compare (const void *p1, const void *p2)
3143
0
{
3144
0
  const Elf32_gptab *a1 = p1;
3145
0
  const Elf32_gptab *a2 = p2;
3146
3147
0
  return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
3148
0
}
3149

3150
/* Functions to manage the got entry hash table.  */
3151
3152
/* Use all 64 bits of a bfd_vma for the computation of a 32-bit
3153
   hash number.  */
3154
3155
static inline hashval_t
3156
mips_elf_hash_bfd_vma (bfd_vma addr)
3157
0
{
3158
0
#ifdef BFD64
3159
0
  return addr + (addr >> 32);
3160
#else
3161
  return addr;
3162
#endif
3163
0
}
3164
3165
static hashval_t
3166
mips_elf_got_entry_hash (const void *entry_)
3167
0
{
3168
0
  const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
3169
3170
0
  return (entry->symndx
3171
0
    + ((entry->tls_type == GOT_TLS_LDM) << 18)
3172
0
    + (entry->tls_type == GOT_TLS_LDM ? 0
3173
0
       : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
3174
0
       : entry->symndx >= 0 ? (entry->abfd->id
3175
0
             + mips_elf_hash_bfd_vma (entry->d.addend))
3176
0
       : entry->d.h->root.root.root.hash));
3177
0
}
3178
3179
static int
3180
mips_elf_got_entry_eq (const void *entry1, const void *entry2)
3181
0
{
3182
0
  const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
3183
0
  const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
3184
3185
0
  return (e1->symndx == e2->symndx
3186
0
    && e1->tls_type == e2->tls_type
3187
0
    && (e1->tls_type == GOT_TLS_LDM ? true
3188
0
        : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
3189
0
        : e1->symndx >= 0 ? (e1->abfd == e2->abfd
3190
0
           && e1->d.addend == e2->d.addend)
3191
0
        : e2->abfd && e1->d.h == e2->d.h));
3192
0
}
3193
3194
static hashval_t
3195
mips_got_page_ref_hash (const void *ref_)
3196
0
{
3197
0
  const struct mips_got_page_ref *ref;
3198
3199
0
  ref = (const struct mips_got_page_ref *) ref_;
3200
0
  return ((ref->symndx >= 0
3201
0
     ? (hashval_t) (ref->u.abfd->id + ref->symndx)
3202
0
     : ref->u.h->root.root.root.hash)
3203
0
    + mips_elf_hash_bfd_vma (ref->addend));
3204
0
}
3205
3206
static int
3207
mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
3208
0
{
3209
0
  const struct mips_got_page_ref *ref1, *ref2;
3210
3211
0
  ref1 = (const struct mips_got_page_ref *) ref1_;
3212
0
  ref2 = (const struct mips_got_page_ref *) ref2_;
3213
0
  return (ref1->symndx == ref2->symndx
3214
0
    && (ref1->symndx < 0
3215
0
        ? ref1->u.h == ref2->u.h
3216
0
        : ref1->u.abfd == ref2->u.abfd)
3217
0
    && ref1->addend == ref2->addend);
3218
0
}
3219
3220
static hashval_t
3221
mips_got_page_entry_hash (const void *entry_)
3222
0
{
3223
0
  const struct mips_got_page_entry *entry;
3224
3225
0
  entry = (const struct mips_got_page_entry *) entry_;
3226
0
  return entry->sec->id;
3227
0
}
3228
3229
static int
3230
mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
3231
0
{
3232
0
  const struct mips_got_page_entry *entry1, *entry2;
3233
3234
0
  entry1 = (const struct mips_got_page_entry *) entry1_;
3235
0
  entry2 = (const struct mips_got_page_entry *) entry2_;
3236
0
  return entry1->sec == entry2->sec;
3237
0
}
3238

3239
/* Create and return a new mips_got_info structure.  */
3240
3241
static struct mips_got_info *
3242
mips_elf_create_got_info (bfd *abfd)
3243
0
{
3244
0
  struct mips_got_info *g;
3245
3246
0
  g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3247
0
  if (g == NULL)
3248
0
    return NULL;
3249
3250
0
  g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3251
0
            mips_elf_got_entry_eq, NULL);
3252
0
  if (g->got_entries == NULL)
3253
0
    return NULL;
3254
3255
0
  g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3256
0
              mips_got_page_ref_eq, NULL);
3257
0
  if (g->got_page_refs == NULL)
3258
0
    return NULL;
3259
3260
0
  return g;
3261
0
}
3262
3263
/* Return the GOT info for input bfd ABFD, trying to create a new one if
3264
   CREATE_P and if ABFD doesn't already have a GOT.  */
3265
3266
static struct mips_got_info *
3267
mips_elf_bfd_got (bfd *abfd, bool create_p)
3268
0
{
3269
0
  struct mips_elf_obj_tdata *tdata;
3270
3271
0
  if (!is_mips_elf (abfd))
3272
0
    return NULL;
3273
3274
0
  tdata = mips_elf_tdata (abfd);
3275
0
  if (!tdata->got && create_p)
3276
0
    tdata->got = mips_elf_create_got_info (abfd);
3277
0
  return tdata->got;
3278
0
}
3279
3280
/* Record that ABFD should use output GOT G.  */
3281
3282
static void
3283
mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3284
0
{
3285
0
  struct mips_elf_obj_tdata *tdata;
3286
3287
0
  BFD_ASSERT (is_mips_elf (abfd));
3288
0
  tdata = mips_elf_tdata (abfd);
3289
0
  if (tdata->got)
3290
0
    {
3291
      /* The GOT structure itself and the hash table entries are
3292
   allocated to a bfd, but the hash tables aren't.  */
3293
0
      htab_delete (tdata->got->got_entries);
3294
0
      htab_delete (tdata->got->got_page_refs);
3295
0
      if (tdata->got->got_page_entries)
3296
0
  htab_delete (tdata->got->got_page_entries);
3297
0
    }
3298
0
  tdata->got = g;
3299
0
}
3300
3301
/* Return the dynamic relocation section.  If it doesn't exist, try to
3302
   create a new it if CREATE_P, otherwise return NULL.  Also return NULL
3303
   if creation fails.  */
3304
3305
static asection *
3306
mips_elf_rel_dyn_section (struct bfd_link_info *info, bool create_p)
3307
0
{
3308
0
  const char *dname;
3309
0
  asection *sreloc;
3310
0
  bfd *dynobj;
3311
3312
0
  dname = MIPS_ELF_REL_DYN_NAME (info);
3313
0
  dynobj = elf_hash_table (info)->dynobj;
3314
0
  sreloc = bfd_get_linker_section (dynobj, dname);
3315
0
  if (sreloc == NULL && create_p)
3316
0
    {
3317
0
      sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3318
0
               (SEC_ALLOC
3319
0
                | SEC_LOAD
3320
0
                | SEC_HAS_CONTENTS
3321
0
                | SEC_IN_MEMORY
3322
0
                | SEC_LINKER_CREATED
3323
0
                | SEC_READONLY));
3324
0
      if (sreloc == NULL
3325
0
    || !bfd_set_section_alignment (sreloc,
3326
0
           MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
3327
0
  return NULL;
3328
0
    }
3329
0
  return sreloc;
3330
0
}
3331
3332
/* Return the GOT_TLS_* type required by relocation type R_TYPE.  */
3333
3334
static int
3335
mips_elf_reloc_tls_type (unsigned int r_type)
3336
0
{
3337
0
  if (tls_gd_reloc_p (r_type))
3338
0
    return GOT_TLS_GD;
3339
3340
0
  if (tls_ldm_reloc_p (r_type))
3341
0
    return GOT_TLS_LDM;
3342
3343
0
  if (tls_gottprel_reloc_p (r_type))
3344
0
    return GOT_TLS_IE;
3345
3346
0
  return GOT_TLS_NONE;
3347
0
}
3348
3349
/* Return the number of GOT slots needed for GOT TLS type TYPE.  */
3350
3351
static int
3352
mips_tls_got_entries (unsigned int type)
3353
0
{
3354
0
  switch (type)
3355
0
    {
3356
0
    case GOT_TLS_GD:
3357
0
    case GOT_TLS_LDM:
3358
0
      return 2;
3359
3360
0
    case GOT_TLS_IE:
3361
0
      return 1;
3362
3363
0
    case GOT_TLS_NONE:
3364
0
      return 0;
3365
0
    }
3366
0
  abort ();
3367
0
}
3368
3369
/* Count the number of relocations needed for a TLS GOT entry, with
3370
   access types from TLS_TYPE, and symbol H (or a local symbol if H
3371
   is NULL).  */
3372
3373
static int
3374
mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3375
         struct elf_link_hash_entry *h)
3376
0
{
3377
0
  int indx = 0;
3378
0
  bool need_relocs = false;
3379
0
  bool dyn = elf_hash_table (info)->dynamic_sections_created;
3380
3381
0
  if (h != NULL
3382
0
      && h->dynindx != -1
3383
0
      && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
3384
0
      && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
3385
0
    indx = h->dynindx;
3386
3387
0
  if ((bfd_link_dll (info) || indx != 0)
3388
0
      && (h == NULL
3389
0
    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3390
0
    || h->root.type != bfd_link_hash_undefweak))
3391
0
    need_relocs = true;
3392
3393
0
  if (!need_relocs)
3394
0
    return 0;
3395
3396
0
  switch (tls_type)
3397
0
    {
3398
0
    case GOT_TLS_GD:
3399
0
      return indx != 0 ? 2 : 1;
3400
3401
0
    case GOT_TLS_IE:
3402
0
      return 1;
3403
3404
0
    case GOT_TLS_LDM:
3405
0
      return bfd_link_dll (info) ? 1 : 0;
3406
3407
0
    default:
3408
0
      return 0;
3409
0
    }
3410
0
}
3411
3412
/* Add the number of GOT entries and TLS relocations required by ENTRY
3413
   to G.  */
3414
3415
static void
3416
mips_elf_count_got_entry (struct bfd_link_info *info,
3417
        struct mips_got_info *g,
3418
        struct mips_got_entry *entry)
3419
0
{
3420
0
  if (entry->tls_type)
3421
0
    {
3422
0
      g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3423
0
      g->relocs += mips_tls_got_relocs (info, entry->tls_type,
3424
0
          entry->symndx < 0
3425
0
          ? &entry->d.h->root : NULL);
3426
0
    }
3427
0
  else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3428
0
    g->local_gotno += 1;
3429
0
  else
3430
0
    g->global_gotno += 1;
3431
0
}
3432
3433
/* Output a simple dynamic relocation into SRELOC.  */
3434
3435
static void
3436
mips_elf_output_dynamic_relocation (bfd *output_bfd,
3437
            asection *sreloc,
3438
            unsigned long reloc_index,
3439
            unsigned long indx,
3440
            int r_type,
3441
            bfd_vma offset)
3442
0
{
3443
0
  Elf_Internal_Rela rel[3];
3444
3445
0
  memset (rel, 0, sizeof (rel));
3446
3447
0
  rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3448
0
  rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3449
3450
0
  if (ABI_64_P (output_bfd))
3451
0
    {
3452
0
      (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3453
0
  (output_bfd, &rel[0],
3454
0
   (sreloc->contents
3455
0
    + reloc_index * sizeof (Elf64_Mips_External_Rel)));
3456
0
    }
3457
0
  else
3458
0
    bfd_elf32_swap_reloc_out
3459
0
      (output_bfd, &rel[0],
3460
0
       (sreloc->contents
3461
0
  + reloc_index * sizeof (Elf32_External_Rel)));
3462
0
}
3463
3464
/* Initialize a set of TLS GOT entries for one symbol.  */
3465
3466
static void
3467
mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3468
             struct mips_got_entry *entry,
3469
             struct mips_elf_link_hash_entry *h,
3470
             bfd_vma value)
3471
0
{
3472
0
  bool dyn = elf_hash_table (info)->dynamic_sections_created;
3473
0
  struct mips_elf_link_hash_table *htab;
3474
0
  int indx;
3475
0
  asection *sreloc, *sgot;
3476
0
  bfd_vma got_offset, got_offset2;
3477
0
  bool need_relocs = false;
3478
3479
0
  htab = mips_elf_hash_table (info);
3480
0
  if (htab == NULL)
3481
0
    return;
3482
3483
0
  sgot = htab->root.sgot;
3484
3485
0
  indx = 0;
3486
0
  if (h != NULL
3487
0
      && h->root.dynindx != -1
3488
0
      && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), &h->root)
3489
0
      && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3490
0
    indx = h->root.dynindx;
3491
3492
0
  if (entry->tls_initialized)
3493
0
    return;
3494
3495
0
  if ((bfd_link_dll (info) || indx != 0)
3496
0
      && (h == NULL
3497
0
    || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3498
0
    || h->root.type != bfd_link_hash_undefweak))
3499
0
    need_relocs = true;
3500
3501
  /* MINUS_ONE means the symbol is not defined in this object.  It may not
3502
     be defined at all; assume that the value doesn't matter in that
3503
     case.  Otherwise complain if we would use the value.  */
3504
0
  BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3505
0
        || h->root.root.type == bfd_link_hash_undefweak);
3506
3507
  /* Emit necessary relocations.  */
3508
0
  sreloc = mips_elf_rel_dyn_section (info, false);
3509
0
  got_offset = entry->gotidx;
3510
3511
0
  switch (entry->tls_type)
3512
0
    {
3513
0
    case GOT_TLS_GD:
3514
      /* General Dynamic.  */
3515
0
      got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
3516
3517
0
      if (need_relocs)
3518
0
  {
3519
0
    mips_elf_output_dynamic_relocation
3520
0
      (abfd, sreloc, sreloc->reloc_count++, indx,
3521
0
       ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3522
0
       sgot->output_offset + sgot->output_section->vma + got_offset);
3523
3524
0
    if (indx)
3525
0
      mips_elf_output_dynamic_relocation
3526
0
        (abfd, sreloc, sreloc->reloc_count++, indx,
3527
0
         ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
3528
0
         sgot->output_offset + sgot->output_section->vma + got_offset2);
3529
0
    else
3530
0
      MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3531
0
             sgot->contents + got_offset2);
3532
0
  }
3533
0
      else
3534
0
  {
3535
0
    MIPS_ELF_PUT_WORD (abfd, 1,
3536
0
           sgot->contents + got_offset);
3537
0
    MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3538
0
           sgot->contents + got_offset2);
3539
0
  }
3540
0
      break;
3541
3542
0
    case GOT_TLS_IE:
3543
      /* Initial Exec model.  */
3544
0
      if (need_relocs)
3545
0
  {
3546
0
    if (indx == 0)
3547
0
      MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
3548
0
             sgot->contents + got_offset);
3549
0
    else
3550
0
      MIPS_ELF_PUT_WORD (abfd, 0,
3551
0
             sgot->contents + got_offset);
3552
3553
0
    mips_elf_output_dynamic_relocation
3554
0
      (abfd, sreloc, sreloc->reloc_count++, indx,
3555
0
       ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
3556
0
       sgot->output_offset + sgot->output_section->vma + got_offset);
3557
0
  }
3558
0
      else
3559
0
  MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
3560
0
         sgot->contents + got_offset);
3561
0
      break;
3562
3563
0
    case GOT_TLS_LDM:
3564
      /* The initial offset is zero, and the LD offsets will include the
3565
   bias by DTP_OFFSET.  */
3566
0
      MIPS_ELF_PUT_WORD (abfd, 0,
3567
0
       sgot->contents + got_offset
3568
0
       + MIPS_ELF_GOT_SIZE (abfd));
3569
3570
0
      if (!bfd_link_dll (info))
3571
0
  MIPS_ELF_PUT_WORD (abfd, 1,
3572
0
         sgot->contents + got_offset);
3573
0
      else
3574
0
  mips_elf_output_dynamic_relocation
3575
0
    (abfd, sreloc, sreloc->reloc_count++, indx,
3576
0
     ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3577
0
     sgot->output_offset + sgot->output_section->vma + got_offset);
3578
0
      break;
3579
3580
0
    default:
3581
0
      abort ();
3582
0
    }
3583
3584
0
  entry->tls_initialized = true;
3585
0
}
3586
3587
/* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3588
   for global symbol H.  .got.plt comes before the GOT, so the offset
3589
   will be negative.  */
3590
3591
static bfd_vma
3592
mips_elf_gotplt_index (struct bfd_link_info *info,
3593
           struct elf_link_hash_entry *h)
3594
0
{
3595
0
  bfd_vma got_address, got_value;
3596
0
  struct mips_elf_link_hash_table *htab;
3597
3598
0
  htab = mips_elf_hash_table (info);
3599
0
  BFD_ASSERT (htab != NULL);
3600
3601
0
  BFD_ASSERT (h->plt.plist != NULL);
3602
0
  BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
3603
3604
  /* Calculate the address of the associated .got.plt entry.  */
3605
0
  got_address = (htab->root.sgotplt->output_section->vma
3606
0
     + htab->root.sgotplt->output_offset
3607
0
     + (h->plt.plist->gotplt_index
3608
0
        * MIPS_ELF_GOT_SIZE (info->output_bfd)));
3609
3610
  /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
3611
0
  got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3612
0
         + htab->root.hgot->root.u.def.section->output_offset
3613
0
         + htab->root.hgot->root.u.def.value);
3614
3615
0
  return got_address - got_value;
3616
0
}
3617
3618
/* Return the GOT offset for address VALUE.   If there is not yet a GOT
3619
   entry for this value, create one.  If R_SYMNDX refers to a TLS symbol,
3620
   create a TLS GOT entry instead.  Return -1 if no satisfactory GOT
3621
   offset can be found.  */
3622
3623
static bfd_vma
3624
mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3625
        bfd_vma value, unsigned long r_symndx,
3626
        struct mips_elf_link_hash_entry *h, int r_type)
3627
0
{
3628
0
  struct mips_elf_link_hash_table *htab;
3629
0
  struct mips_got_entry *entry;
3630
3631
0
  htab = mips_elf_hash_table (info);
3632
0
  BFD_ASSERT (htab != NULL);
3633
3634
0
  entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3635
0
             r_symndx, h, r_type);
3636
0
  if (!entry)
3637
0
    return MINUS_ONE;
3638
3639
0
  if (entry->tls_type)
3640
0
    mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3641
0
  return entry->gotidx;
3642
0
}
3643
3644
/* Return the GOT index of global symbol H in the primary GOT.  */
3645
3646
static bfd_vma
3647
mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3648
           struct elf_link_hash_entry *h)
3649
0
{
3650
0
  struct mips_elf_link_hash_table *htab;
3651
0
  long global_got_dynindx;
3652
0
  struct mips_got_info *g;
3653
0
  bfd_vma got_index;
3654
3655
0
  htab = mips_elf_hash_table (info);
3656
0
  BFD_ASSERT (htab != NULL);
3657
3658
0
  global_got_dynindx = 0;
3659
0
  if (htab->global_gotsym != NULL)
3660
0
    global_got_dynindx = htab->global_gotsym->dynindx;
3661
3662
  /* Once we determine the global GOT entry with the lowest dynamic
3663
     symbol table index, we must put all dynamic symbols with greater
3664
     indices into the primary GOT.  That makes it easy to calculate the
3665
     GOT offset.  */
3666
0
  BFD_ASSERT (h->dynindx >= global_got_dynindx);
3667
0
  g = mips_elf_bfd_got (obfd, false);
3668
0
  got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3669
0
         * MIPS_ELF_GOT_SIZE (obfd));
3670
0
  BFD_ASSERT (got_index < htab->root.sgot->size);
3671
3672
0
  return got_index;
3673
0
}
3674
3675
/* Return the GOT index for the global symbol indicated by H, which is
3676
   referenced by a relocation of type R_TYPE in IBFD.  */
3677
3678
static bfd_vma
3679
mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3680
         struct elf_link_hash_entry *h, int r_type)
3681
0
{
3682
0
  struct mips_elf_link_hash_table *htab;
3683
0
  struct mips_got_info *g;
3684
0
  struct mips_got_entry lookup, *entry;
3685
0
  bfd_vma gotidx;
3686
3687
0
  htab = mips_elf_hash_table (info);
3688
0
  BFD_ASSERT (htab != NULL);
3689
3690
0
  g = mips_elf_bfd_got (ibfd, false);
3691
0
  BFD_ASSERT (g);
3692
3693
0
  lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3694
0
  if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, false))
3695
0
    return mips_elf_primary_global_got_index (obfd, info, h);
3696
3697
0
  lookup.abfd = ibfd;
3698
0
  lookup.symndx = -1;
3699
0
  lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3700
0
  entry = htab_find (g->got_entries, &lookup);
3701
0
  BFD_ASSERT (entry);
3702
3703
0
  gotidx = entry->gotidx;
3704
0
  BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3705
3706
0
  if (lookup.tls_type)
3707
0
    {
3708
0
      bfd_vma value = MINUS_ONE;
3709
3710
0
      if ((h->root.type == bfd_link_hash_defined
3711
0
     || h->root.type == bfd_link_hash_defweak)
3712
0
    && h->root.u.def.section->output_section)
3713
0
  value = (h->root.u.def.value
3714
0
     + h->root.u.def.section->output_offset
3715
0
     + h->root.u.def.section->output_section->vma);
3716
3717
0
      mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
3718
0
    }
3719
0
  return gotidx;
3720
0
}
3721
3722
/* Find a GOT page entry that points to within 32KB of VALUE.  These
3723
   entries are supposed to be placed at small offsets in the GOT, i.e.,
3724
   within 32KB of GP.  Return the index of the GOT entry, or -1 if no
3725
   entry could be created.  If OFFSETP is nonnull, use it to return the
3726
   offset of the GOT entry from VALUE.  */
3727
3728
static bfd_vma
3729
mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3730
       bfd_vma value, bfd_vma *offsetp)
3731
0
{
3732
0
  bfd_vma page, got_index;
3733
0
  struct mips_got_entry *entry;
3734
3735
0
  page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3736
0
  entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3737
0
             NULL, R_MIPS_GOT_PAGE);
3738
3739
0
  if (!entry)
3740
0
    return MINUS_ONE;
3741
3742
0
  got_index = entry->gotidx;
3743
3744
0
  if (offsetp)
3745
0
    *offsetp = value - entry->d.address;
3746
3747
0
  return got_index;
3748
0
}
3749
3750
/* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3751
   EXTERNAL is true if the relocation was originally against a global
3752
   symbol that binds locally.  */
3753
3754
static bfd_vma
3755
mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3756
          bfd_vma value, bool external)
3757
0
{
3758
0
  struct mips_got_entry *entry;
3759
3760
  /* GOT16 relocations against local symbols are followed by a LO16
3761
     relocation; those against global symbols are not.  Thus if the
3762
     symbol was originally local, the GOT16 relocation should load the
3763
     equivalent of %hi(VALUE), otherwise it should load VALUE itself.  */
3764
0
  if (! external)
3765
0
    value = mips_elf_high (value) << 16;
3766
3767
  /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3768
     R_MIPS16_GOT16, R_MIPS_CALL16, etc.  The format of the entry is the
3769
     same in all cases.  */
3770
0
  entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3771
0
             NULL, R_MIPS_GOT16);
3772
0
  if (entry)
3773
0
    return entry->gotidx;
3774
0
  else
3775
0
    return MINUS_ONE;
3776
0
}
3777
3778
/* Returns the offset for the entry at the INDEXth position
3779
   in the GOT.  */
3780
3781
static bfd_vma
3782
mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3783
        bfd *input_bfd, bfd_vma got_index)
3784
0
{
3785
0
  struct mips_elf_link_hash_table *htab;
3786
0
  asection *sgot;
3787
0
  bfd_vma gp;
3788
3789
0
  htab = mips_elf_hash_table (info);
3790
0
  BFD_ASSERT (htab != NULL);
3791
3792
0
  sgot = htab->root.sgot;
3793
0
  gp = _bfd_get_gp_value (output_bfd)
3794
0
    + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3795
3796
0
  return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3797
0
}
3798
3799
/* Create and return a local GOT entry for VALUE, which was calculated
3800
   from a symbol belonging to INPUT_SECTON.  Return NULL if it could not
3801
   be created.  If R_SYMNDX refers to a TLS symbol, create a TLS entry
3802
   instead.  */
3803
3804
static struct mips_got_entry *
3805
mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3806
         bfd *ibfd, bfd_vma value,
3807
         unsigned long r_symndx,
3808
         struct mips_elf_link_hash_entry *h,
3809
         int r_type)
3810
0
{
3811
0
  struct mips_got_entry lookup, *entry;
3812
0
  void **loc;
3813
0
  struct mips_got_info *g;
3814
0
  struct mips_elf_link_hash_table *htab;
3815
0
  bfd_vma gotidx;
3816
3817
0
  htab = mips_elf_hash_table (info);
3818
0
  BFD_ASSERT (htab != NULL);
3819
3820
0
  g = mips_elf_bfd_got (ibfd, false);
3821
0
  if (g == NULL)
3822
0
    {
3823
0
      g = mips_elf_bfd_got (abfd, false);
3824
0
      BFD_ASSERT (g != NULL);
3825
0
    }
3826
3827
  /* This function shouldn't be called for symbols that live in the global
3828
     area of the GOT.  */
3829
0
  BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
3830
3831
0
  lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3832
0
  if (lookup.tls_type)
3833
0
    {
3834
0
      lookup.abfd = ibfd;
3835
0
      if (tls_ldm_reloc_p (r_type))
3836
0
  {
3837
0
    lookup.symndx = 0;
3838
0
    lookup.d.addend = 0;
3839
0
  }
3840
0
      else if (h == NULL)
3841
0
  {
3842
0
    lookup.symndx = r_symndx;
3843
0
    lookup.d.addend = 0;
3844
0
  }
3845
0
      else
3846
0
  {
3847
0
    lookup.symndx = -1;
3848
0
    lookup.d.h = h;
3849
0
  }
3850
3851
0
      entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3852
0
      BFD_ASSERT (entry);
3853
3854
0
      gotidx = entry->gotidx;
3855
0
      BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3856
3857
0
      return entry;
3858
0
    }
3859
3860
0
  lookup.abfd = NULL;
3861
0
  lookup.symndx = -1;
3862
0
  lookup.d.address = value;
3863
0
  loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3864
0
  if (!loc)
3865
0
    return NULL;
3866
3867
0
  entry = (struct mips_got_entry *) *loc;
3868
0
  if (entry)
3869
0
    return entry;
3870
3871
0
  if (g->assigned_low_gotno > g->assigned_high_gotno)
3872
0
    {
3873
      /* We didn't allocate enough space in the GOT.  */
3874
0
      _bfd_error_handler
3875
0
  (_("not enough GOT space for local GOT entries"));
3876
0
      bfd_set_error (bfd_error_bad_value);
3877
0
      return NULL;
3878
0
    }
3879
3880
0
  entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3881
0
  if (!entry)
3882
0
    return NULL;
3883
3884
0
  if (got16_reloc_p (r_type)
3885
0
      || call16_reloc_p (r_type)
3886
0
      || got_page_reloc_p (r_type)
3887
0
      || got_disp_reloc_p (r_type))
3888
0
    lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
3889
0
  else
3890
0
    lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
3891
3892
0
  *entry = lookup;
3893
0
  *loc = entry;
3894
3895
0
  MIPS_ELF_PUT_WORD (abfd, value, htab->root.sgot->contents + entry->gotidx);
3896
3897
  /* These GOT entries need a dynamic relocation on VxWorks.  */
3898
0
  if (htab->root.target_os == is_vxworks)
3899
0
    {
3900
0
      Elf_Internal_Rela outrel;
3901
0
      asection *s;
3902
0
      bfd_byte *rloc;
3903
0
      bfd_vma got_address;
3904
3905
0
      s = mips_elf_rel_dyn_section (info, false);
3906
0
      got_address = (htab->root.sgot->output_section->vma
3907
0
         + htab->root.sgot->output_offset
3908
0
         + entry->gotidx);
3909
3910
0
      rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3911
0
      outrel.r_offset = got_address;
3912
0
      outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3913
0
      outrel.r_addend = value;
3914
0
      bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3915
0
    }
3916
3917
0
  return entry;
3918
0
}
3919
3920
/* Return the number of dynamic section symbols required by OUTPUT_BFD.
3921
   The number might be exact or a worst-case estimate, depending on how
3922
   much information is available to elf_backend_omit_section_dynsym at
3923
   the current linking stage.  */
3924
3925
static bfd_size_type
3926
count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3927
0
{
3928
0
  bfd_size_type count;
3929
3930
0
  count = 0;
3931
0
  if (bfd_link_pic (info))
3932
0
    {
3933
0
      asection *p;
3934
0
      const struct elf_backend_data *bed;
3935
3936
0
      bed = get_elf_backend_data (output_bfd);
3937
0
      for (p = output_bfd->sections; p ; p = p->next)
3938
0
  if ((p->flags & SEC_EXCLUDE) == 0
3939
0
      && (p->flags & SEC_ALLOC) != 0
3940
0
      && elf_hash_table (info)->dynamic_relocs
3941
0
      && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3942
0
    ++count;
3943
0
    }
3944
0
  return count;
3945
0
}
3946
3947
/* Sort the dynamic symbol table so that symbols that need GOT entries
3948
   appear towards the end.  */
3949
3950
static bool
3951
mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3952
0
{
3953
0
  struct mips_elf_link_hash_table *htab;
3954
0
  struct mips_elf_hash_sort_data hsd;
3955
0
  struct mips_got_info *g;
3956
3957
0
  htab = mips_elf_hash_table (info);
3958
0
  BFD_ASSERT (htab != NULL);
3959
3960
0
  if (htab->root.dynsymcount == 0)
3961
0
    return true;
3962
3963
0
  g = htab->got_info;
3964
0
  if (g == NULL)
3965
0
    return true;
3966
3967
0
  hsd.low = NULL;
3968
0
  hsd.max_unref_got_dynindx
3969
0
    = hsd.min_got_dynindx
3970
0
    = (htab->root.dynsymcount - g->reloc_only_gotno);
3971
  /* Add 1 to local symbol indices to account for the mandatory NULL entry
3972
     at the head of the table; see `_bfd_elf_link_renumber_dynsyms'.  */
3973
0
  hsd.max_local_dynindx = count_section_dynsyms (abfd, info) + 1;
3974
0
  hsd.max_non_got_dynindx = htab->root.local_dynsymcount + 1;
3975
0
  hsd.output_bfd = abfd;
3976
0
  if (htab->root.dynobj != NULL
3977
0
      && htab->root.dynamic_sections_created
3978
0
      && info->emit_gnu_hash)
3979
0
    {
3980
0
      asection *s = bfd_get_linker_section (htab->root.dynobj, ".MIPS.xhash");
3981
0
      BFD_ASSERT (s != NULL);
3982
0
      hsd.mipsxhash = s->contents;
3983
0
      BFD_ASSERT (hsd.mipsxhash != NULL);
3984
0
    }
3985
0
  else
3986
0
    hsd.mipsxhash = NULL;
3987
0
  mips_elf_link_hash_traverse (htab, mips_elf_sort_hash_table_f, &hsd);
3988
3989
  /* There should have been enough room in the symbol table to
3990
     accommodate both the GOT and non-GOT symbols.  */
3991
0
  BFD_ASSERT (hsd.max_local_dynindx <= htab->root.local_dynsymcount + 1);
3992
0
  BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3993
0
  BFD_ASSERT (hsd.max_unref_got_dynindx == htab->root.dynsymcount);
3994
0
  BFD_ASSERT (htab->root.dynsymcount - hsd.min_got_dynindx == g->global_gotno);
3995
3996
  /* Now we know which dynamic symbol has the lowest dynamic symbol
3997
     table index in the GOT.  */
3998
0
  htab->global_gotsym = hsd.low;
3999
4000
0
  return true;
4001
0
}
4002
4003
/* If H needs a GOT entry, assign it the highest available dynamic
4004
   index.  Otherwise, assign it the lowest available dynamic
4005
   index.  */
4006
4007
static bool
4008
mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
4009
0
{
4010
0
  struct mips_elf_hash_sort_data *hsd = data;
4011
4012
  /* Symbols without dynamic symbol table entries aren't interesting
4013
     at all.  */
4014
0
  if (h->root.dynindx == -1)
4015
0
    return true;
4016
4017
0
  switch (h->global_got_area)
4018
0
    {
4019
0
    case GGA_NONE:
4020
0
      if (h->root.forced_local)
4021
0
  h->root.dynindx = hsd->max_local_dynindx++;
4022
0
      else
4023
0
  h->root.dynindx = hsd->max_non_got_dynindx++;
4024
0
      break;
4025
4026
0
    case GGA_NORMAL:
4027
0
      h->root.dynindx = --hsd->min_got_dynindx;
4028
0
      hsd->low = (struct elf_link_hash_entry *) h;
4029
0
      break;
4030
4031
0
    case GGA_RELOC_ONLY:
4032
0
      if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
4033
0
  hsd->low = (struct elf_link_hash_entry *) h;
4034
0
      h->root.dynindx = hsd->max_unref_got_dynindx++;
4035
0
      break;
4036
0
    }
4037
4038
  /* Populate the .MIPS.xhash translation table entry with
4039
     the symbol dynindx.  */
4040
0
  if (h->mipsxhash_loc != 0 && hsd->mipsxhash != NULL)
4041
0
    bfd_put_32 (hsd->output_bfd, h->root.dynindx,
4042
0
    hsd->mipsxhash + h->mipsxhash_loc);
4043
4044
0
  return true;
4045
0
}
4046
4047
/* Record that input bfd ABFD requires a GOT entry like *LOOKUP
4048
   (which is owned by the caller and shouldn't be added to the
4049
   hash table directly).  */
4050
4051
static bool
4052
mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
4053
         struct mips_got_entry *lookup)
4054
0
{
4055
0
  struct mips_elf_link_hash_table *htab;
4056
0
  struct mips_got_entry *entry;
4057
0
  struct mips_got_info *g;
4058
0
  void **loc, **bfd_loc;
4059
4060
  /* Make sure there's a slot for this entry in the master GOT.  */
4061
0
  htab = mips_elf_hash_table (info);
4062
0
  g = htab->got_info;
4063
0
  loc = htab_find_slot (g->got_entries, lookup, INSERT);
4064
0
  if (!loc)
4065
0
    return false;
4066
4067
  /* Populate the entry if it isn't already.  */
4068
0
  entry = (struct mips_got_entry *) *loc;
4069
0
  if (!entry)
4070
0
    {
4071
0
      entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
4072
0
      if (!entry)
4073
0
  return false;
4074
4075
0
      lookup->tls_initialized = false;
4076
0
      lookup->gotidx = -1;
4077
0
      *entry = *lookup;
4078
0
      *loc = entry;
4079
0
    }
4080
4081
  /* Reuse the same GOT entry for the BFD's GOT.  */
4082
0
  g = mips_elf_bfd_got (abfd, true);
4083
0
  if (!g)
4084
0
    return false;
4085
4086
0
  bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
4087
0
  if (!bfd_loc)
4088
0
    return false;
4089
4090
0
  if (!*bfd_loc)
4091
0
    *bfd_loc = entry;
4092
0
  return true;
4093
0
}
4094
4095
/* ABFD has a GOT relocation of type R_TYPE against H.  Reserve a GOT
4096
   entry for it.  FOR_CALL is true if the caller is only interested in
4097
   using the GOT entry for calls.  */
4098
4099
static bool
4100
mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
4101
           bfd *abfd, struct bfd_link_info *info,
4102
           bool for_call, int r_type)
4103
0
{
4104
0
  struct mips_elf_link_hash_table *htab;
4105
0
  struct mips_elf_link_hash_entry *hmips;
4106
0
  struct mips_got_entry entry;
4107
0
  unsigned char tls_type;
4108
4109
0
  htab = mips_elf_hash_table (info);
4110
0
  BFD_ASSERT (htab != NULL);
4111
4112
0
  hmips = (struct mips_elf_link_hash_entry *) h;
4113
0
  if (!for_call)
4114
0
    hmips->got_only_for_calls = false;
4115
4116
  /* A global symbol in the GOT must also be in the dynamic symbol
4117
     table.  */
4118
0
  if (h->dynindx == -1)
4119
0
    {
4120
0
      switch (ELF_ST_VISIBILITY (h->other))
4121
0
  {
4122
0
  case STV_INTERNAL:
4123
0
  case STV_HIDDEN:
4124
0
    _bfd_mips_elf_hide_symbol (info, h, true);
4125
0
    break;
4126
0
  }
4127
0
      if (!bfd_elf_link_record_dynamic_symbol (info, h))
4128
0
  return false;
4129
0
    }
4130
4131
0
  tls_type = mips_elf_reloc_tls_type (r_type);
4132
0
  if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
4133
0
    hmips->global_got_area = GGA_NORMAL;
4134
4135
0
  entry.abfd = abfd;
4136
0
  entry.symndx = -1;
4137
0
  entry.d.h = (struct mips_elf_link_hash_entry *) h;
4138
0
  entry.tls_type = tls_type;
4139
0
  return mips_elf_record_got_entry (info, abfd, &entry);
4140
0
}
4141
4142
/* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
4143
   where SYMNDX is a local symbol.  Reserve a GOT entry for it.  */
4144
4145
static bool
4146
mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
4147
          struct bfd_link_info *info, int r_type)
4148
0
{
4149
0
  struct mips_elf_link_hash_table *htab;
4150
0
  struct mips_got_info *g;
4151
0
  struct mips_got_entry entry;
4152
4153
0
  htab = mips_elf_hash_table (info);
4154
0
  BFD_ASSERT (htab != NULL);
4155
4156
0
  g = htab->got_info;
4157
0
  BFD_ASSERT (g != NULL);
4158
4159
0
  entry.abfd = abfd;
4160
0
  entry.symndx = symndx;
4161
0
  entry.d.addend = addend;
4162
0
  entry.tls_type = mips_elf_reloc_tls_type (r_type);
4163
0
  return mips_elf_record_got_entry (info, abfd, &entry);
4164
0
}
4165
4166
/* Record that ABFD has a page relocation against SYMNDX + ADDEND.
4167
   H is the symbol's hash table entry, or null if SYMNDX is local
4168
   to ABFD.  */
4169
4170
static bool
4171
mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
4172
            long symndx, struct elf_link_hash_entry *h,
4173
            bfd_signed_vma addend)
4174
0
{
4175
0
  struct mips_elf_link_hash_table *htab;
4176
0
  struct mips_got_info *g1, *g2;
4177
0
  struct mips_got_page_ref lookup, *entry;
4178
0
  void **loc, **bfd_loc;
4179
4180
0
  htab = mips_elf_hash_table (info);
4181
0
  BFD_ASSERT (htab != NULL);
4182
4183
0
  g1 = htab->got_info;
4184
0
  BFD_ASSERT (g1 != NULL);
4185
4186
0
  if (h)
4187
0
    {
4188
0
      lookup.symndx = -1;
4189
0
      lookup.u.h = (struct mips_elf_link_hash_entry *) h;
4190
0
    }
4191
0
  else
4192
0
    {
4193
0
      lookup.symndx = symndx;
4194
0
      lookup.u.abfd = abfd;
4195
0
    }
4196
0
  lookup.addend = addend;
4197
0
  loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
4198
0
  if (loc == NULL)
4199
0
    return false;
4200
4201
0
  entry = (struct mips_got_page_ref *) *loc;
4202
0
  if (!entry)
4203
0
    {
4204
0
      entry = bfd_alloc (abfd, sizeof (*entry));
4205
0
      if (!entry)
4206
0
  return false;
4207
4208
0
      *entry = lookup;
4209
0
      *loc = entry;
4210
0
    }
4211
4212
  /* Add the same entry to the BFD's GOT.  */
4213
0
  g2 = mips_elf_bfd_got (abfd, true);
4214
0
  if (!g2)
4215
0
    return false;
4216
4217
0
  bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
4218
0
  if (!bfd_loc)
4219
0
    return false;
4220
4221
0
  if (!*bfd_loc)
4222
0
    *bfd_loc = entry;
4223
4224
0
  return true;
4225
0
}
4226
4227
/* Add room for N relocations to the .rel(a).dyn section in ABFD.  */
4228
4229
static void
4230
mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
4231
               unsigned int n)
4232
0
{
4233
0
  asection *s;
4234
0
  struct mips_elf_link_hash_table *htab;
4235
4236
0
  htab = mips_elf_hash_table (info);
4237
0
  BFD_ASSERT (htab != NULL);
4238
4239
0
  s = mips_elf_rel_dyn_section (info, false);
4240
0
  BFD_ASSERT (s != NULL);
4241
4242
0
  if (htab->root.target_os == is_vxworks)
4243
0
    s->size += n * MIPS_ELF_RELA_SIZE (abfd);
4244
0
  else
4245
0
    {
4246
0
      if (s->size == 0)
4247
0
  {
4248
    /* Make room for a null element.  */
4249
0
    s->size += MIPS_ELF_REL_SIZE (abfd);
4250
0
    ++s->reloc_count;
4251
0
  }
4252
0
      s->size += n * MIPS_ELF_REL_SIZE (abfd);
4253
0
    }
4254
0
}
4255

4256
/* A htab_traverse callback for GOT entries, with DATA pointing to a
4257
   mips_elf_traverse_got_arg structure.  Count the number of GOT
4258
   entries and TLS relocs.  Set DATA->value to true if we need
4259
   to resolve indirect or warning symbols and then recreate the GOT.  */
4260
4261
static int
4262
mips_elf_check_recreate_got (void **entryp, void *data)
4263
0
{
4264
0
  struct mips_got_entry *entry;
4265
0
  struct mips_elf_traverse_got_arg *arg;
4266
4267
0
  entry = (struct mips_got_entry *) *entryp;
4268
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4269
0
  if (entry->abfd != NULL && entry->symndx == -1)
4270
0
    {
4271
0
      struct mips_elf_link_hash_entry *h;
4272
4273
0
      h = entry->d.h;
4274
0
      if (h->root.root.type == bfd_link_hash_indirect
4275
0
    || h->root.root.type == bfd_link_hash_warning)
4276
0
  {
4277
0
    arg->value = true;
4278
0
    return 0;
4279
0
  }
4280
0
    }
4281
0
  mips_elf_count_got_entry (arg->info, arg->g, entry);
4282
0
  return 1;
4283
0
}
4284
4285
/* A htab_traverse callback for GOT entries, with DATA pointing to a
4286
   mips_elf_traverse_got_arg structure.  Add all entries to DATA->g,
4287
   converting entries for indirect and warning symbols into entries
4288
   for the target symbol.  Set DATA->g to null on error.  */
4289
4290
static int
4291
mips_elf_recreate_got (void **entryp, void *data)
4292
0
{
4293
0
  struct mips_got_entry new_entry, *entry;
4294
0
  struct mips_elf_traverse_got_arg *arg;
4295
0
  void **slot;
4296
4297
0
  entry = (struct mips_got_entry *) *entryp;
4298
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4299
0
  if (entry->abfd != NULL
4300
0
      && entry->symndx == -1
4301
0
      && (entry->d.h->root.root.type == bfd_link_hash_indirect
4302
0
    || entry->d.h->root.root.type == bfd_link_hash_warning))
4303
0
    {
4304
0
      struct mips_elf_link_hash_entry *h;
4305
4306
0
      new_entry = *entry;
4307
0
      entry = &new_entry;
4308
0
      h = entry->d.h;
4309
0
      do
4310
0
  {
4311
0
    BFD_ASSERT (h->global_got_area == GGA_NONE);
4312
0
    h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4313
0
  }
4314
0
      while (h->root.root.type == bfd_link_hash_indirect
4315
0
       || h->root.root.type == bfd_link_hash_warning);
4316
0
      entry->d.h = h;
4317
0
    }
4318
0
  slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4319
0
  if (slot == NULL)
4320
0
    {
4321
0
      arg->g = NULL;
4322
0
      return 0;
4323
0
    }
4324
0
  if (*slot == NULL)
4325
0
    {
4326
0
      if (entry == &new_entry)
4327
0
  {
4328
0
    entry = bfd_alloc (entry->abfd, sizeof (*entry));
4329
0
    if (!entry)
4330
0
      {
4331
0
        arg->g = NULL;
4332
0
        return 0;
4333
0
      }
4334
0
    *entry = new_entry;
4335
0
  }
4336
0
      *slot = entry;
4337
0
      mips_elf_count_got_entry (arg->info, arg->g, entry);
4338
0
    }
4339
0
  return 1;
4340
0
}
4341
4342
/* Return the maximum number of GOT page entries required for RANGE.  */
4343
4344
static bfd_vma
4345
mips_elf_pages_for_range (const struct mips_got_page_range *range)
4346
0
{
4347
0
  return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4348
0
}
4349
4350
/* Record that G requires a page entry that can reach SEC + ADDEND.  */
4351
4352
static bool
4353
mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
4354
        asection *sec, bfd_signed_vma addend)
4355
0
{
4356
0
  struct mips_got_info *g = arg->g;
4357
0
  struct mips_got_page_entry lookup, *entry;
4358
0
  struct mips_got_page_range **range_ptr, *range;
4359
0
  bfd_vma old_pages, new_pages;
4360
0
  void **loc;
4361
4362
  /* Find the mips_got_page_entry hash table entry for this section.  */
4363
0
  lookup.sec = sec;
4364
0
  loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4365
0
  if (loc == NULL)
4366
0
    return false;
4367
4368
  /* Create a mips_got_page_entry if this is the first time we've
4369
     seen the section.  */
4370
0
  entry = (struct mips_got_page_entry *) *loc;
4371
0
  if (!entry)
4372
0
    {
4373
0
      entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
4374
0
      if (!entry)
4375
0
  return false;
4376
4377
0
      entry->sec = sec;
4378
0
      *loc = entry;
4379
0
    }
4380
4381
  /* Skip over ranges whose maximum extent cannot share a page entry
4382
     with ADDEND.  */
4383
0
  range_ptr = &entry->ranges;
4384
0
  while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4385
0
    range_ptr = &(*range_ptr)->next;
4386
4387
  /* If we scanned to the end of the list, or found a range whose
4388
     minimum extent cannot share a page entry with ADDEND, create
4389
     a new singleton range.  */
4390
0
  range = *range_ptr;
4391
0
  if (!range || addend < range->min_addend - 0xffff)
4392
0
    {
4393
0
      range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
4394
0
      if (!range)
4395
0
  return false;
4396
4397
0
      range->next = *range_ptr;
4398
0
      range->min_addend = addend;
4399
0
      range->max_addend = addend;
4400
4401
0
      *range_ptr = range;
4402
0
      entry->num_pages++;
4403
0
      g->page_gotno++;
4404
0
      return true;
4405
0
    }
4406
4407
  /* Remember how many pages the old range contributed.  */
4408
0
  old_pages = mips_elf_pages_for_range (range);
4409
4410
  /* Update the ranges.  */
4411
0
  if (addend < range->min_addend)
4412
0
    range->min_addend = addend;
4413
0
  else if (addend > range->max_addend)
4414
0
    {
4415
0
      if (range->next && addend >= range->next->min_addend - 0xffff)
4416
0
  {
4417
0
    old_pages += mips_elf_pages_for_range (range->next);
4418
0
    range->max_addend = range->next->max_addend;
4419
0
    range->next = range->next->next;
4420
0
  }
4421
0
      else
4422
0
  range->max_addend = addend;
4423
0
    }
4424
4425
  /* Record any change in the total estimate.  */
4426
0
  new_pages = mips_elf_pages_for_range (range);
4427
0
  if (old_pages != new_pages)
4428
0
    {
4429
0
      entry->num_pages += new_pages - old_pages;
4430
0
      g->page_gotno += new_pages - old_pages;
4431
0
    }
4432
4433
0
  return true;
4434
0
}
4435
4436
/* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4437
   and for which DATA points to a mips_elf_traverse_got_arg.  Work out
4438
   whether the page reference described by *REFP needs a GOT page entry,
4439
   and record that entry in DATA->g if so.  Set DATA->g to null on failure.  */
4440
4441
static int
4442
mips_elf_resolve_got_page_ref (void **refp, void *data)
4443
0
{
4444
0
  struct mips_got_page_ref *ref;
4445
0
  struct mips_elf_traverse_got_arg *arg;
4446
0
  struct mips_elf_link_hash_table *htab;
4447
0
  asection *sec;
4448
0
  bfd_vma addend;
4449
4450
0
  ref = (struct mips_got_page_ref *) *refp;
4451
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4452
0
  htab = mips_elf_hash_table (arg->info);
4453
4454
0
  if (ref->symndx < 0)
4455
0
    {
4456
0
      struct mips_elf_link_hash_entry *h;
4457
4458
      /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries.  */
4459
0
      h = ref->u.h;
4460
0
      if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4461
0
  return 1;
4462
4463
      /* Ignore undefined symbols; we'll issue an error later if
4464
   appropriate.  */
4465
0
      if (!((h->root.root.type == bfd_link_hash_defined
4466
0
       || h->root.root.type == bfd_link_hash_defweak)
4467
0
      && h->root.root.u.def.section))
4468
0
  return 1;
4469
4470
0
      sec = h->root.root.u.def.section;
4471
0
      addend = h->root.root.u.def.value + ref->addend;
4472
0
    }
4473
0
  else
4474
0
    {
4475
0
      Elf_Internal_Sym *isym;
4476
4477
      /* Read in the symbol.  */
4478
0
      isym = bfd_sym_from_r_symndx (&htab->root.sym_cache, ref->u.abfd,
4479
0
            ref->symndx);
4480
0
      if (isym == NULL)
4481
0
  {
4482
0
    arg->g = NULL;
4483
0
    return 0;
4484
0
  }
4485
4486
      /* Get the associated input section.  */
4487
0
      sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4488
0
      if (sec == NULL)
4489
0
  {
4490
0
    arg->g = NULL;
4491
0
    return 0;
4492
0
  }
4493
4494
      /* If this is a mergable section, work out the section and offset
4495
   of the merged data.  For section symbols, the addend specifies
4496
   of the offset _of_ the first byte in the data, otherwise it
4497
   specifies the offset _from_ the first byte.  */
4498
0
      if (sec->flags & SEC_MERGE)
4499
0
  {
4500
0
    void *secinfo;
4501
4502
0
    secinfo = elf_section_data (sec)->sec_info;
4503
0
    if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4504
0
      addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4505
0
             isym->st_value + ref->addend);
4506
0
    else
4507
0
      addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4508
0
             isym->st_value) + ref->addend;
4509
0
  }
4510
0
      else
4511
0
  addend = isym->st_value + ref->addend;
4512
0
    }
4513
0
  if (!mips_elf_record_got_page_entry (arg, sec, addend))
4514
0
    {
4515
0
      arg->g = NULL;
4516
0
      return 0;
4517
0
    }
4518
0
  return 1;
4519
0
}
4520
4521
/* If any entries in G->got_entries are for indirect or warning symbols,
4522
   replace them with entries for the target symbol.  Convert g->got_page_refs
4523
   into got_page_entry structures and estimate the number of page entries
4524
   that they require.  */
4525
4526
static bool
4527
mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4528
            struct mips_got_info *g)
4529
0
{
4530
0
  struct mips_elf_traverse_got_arg tga;
4531
0
  struct mips_got_info oldg;
4532
4533
0
  oldg = *g;
4534
4535
0
  tga.info = info;
4536
0
  tga.g = g;
4537
0
  tga.value = false;
4538
0
  htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4539
0
  if (tga.value)
4540
0
    {
4541
0
      *g = oldg;
4542
0
      g->got_entries = htab_create (htab_size (oldg.got_entries),
4543
0
            mips_elf_got_entry_hash,
4544
0
            mips_elf_got_entry_eq, NULL);
4545
0
      if (!g->got_entries)
4546
0
  return false;
4547
4548
0
      htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4549
0
      if (!tga.g)
4550
0
  return false;
4551
4552
0
      htab_delete (oldg.got_entries);
4553
0
    }
4554
4555
0
  g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4556
0
           mips_got_page_entry_eq, NULL);
4557
0
  if (g->got_page_entries == NULL)
4558
0
    return false;
4559
4560
0
  tga.info = info;
4561
0
  tga.g = g;
4562
0
  htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4563
4564
0
  return true;
4565
0
}
4566
4567
/* Return true if a GOT entry for H should live in the local rather than
4568
   global GOT area.  */
4569
4570
static bool
4571
mips_use_local_got_p (struct bfd_link_info *info,
4572
          struct mips_elf_link_hash_entry *h)
4573
0
{
4574
  /* Symbols that aren't in the dynamic symbol table must live in the
4575
     local GOT.  This includes symbols that are completely undefined
4576
     and which therefore don't bind locally.  We'll report undefined
4577
     symbols later if appropriate.  */
4578
0
  if (h->root.dynindx == -1)
4579
0
    return true;
4580
4581
  /* Absolute symbols, if ever they need a GOT entry, cannot ever go
4582
     to the local GOT, as they would be implicitly relocated by the
4583
     base address by the dynamic loader.  */
4584
0
  if (bfd_is_abs_symbol (&h->root.root))
4585
0
    return false;
4586
4587
  /* Symbols that bind locally can (and in the case of forced-local
4588
     symbols, must) live in the local GOT.  */
4589
0
  if (h->got_only_for_calls
4590
0
      ? SYMBOL_CALLS_LOCAL (info, &h->root)
4591
0
      : SYMBOL_REFERENCES_LOCAL (info, &h->root))
4592
0
    return true;
4593
4594
  /* If this is an executable that must provide a definition of the symbol,
4595
     either though PLTs or copy relocations, then that address should go in
4596
     the local rather than global GOT.  */
4597
0
  if (bfd_link_executable (info) && h->has_static_relocs)
4598
0
    return true;
4599
4600
0
  return false;
4601
0
}
4602
4603
/* A mips_elf_link_hash_traverse callback for which DATA points to the
4604
   link_info structure.  Decide whether the hash entry needs an entry in
4605
   the global part of the primary GOT, setting global_got_area accordingly.
4606
   Count the number of global symbols that are in the primary GOT only
4607
   because they have relocations against them (reloc_only_gotno).  */
4608
4609
static bool
4610
mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
4611
0
{
4612
0
  struct bfd_link_info *info;
4613
0
  struct mips_elf_link_hash_table *htab;
4614
0
  struct mips_got_info *g;
4615
4616
0
  info = (struct bfd_link_info *) data;
4617
0
  htab = mips_elf_hash_table (info);
4618
0
  g = htab->got_info;
4619
0
  if (h->global_got_area != GGA_NONE)
4620
0
    {
4621
      /* Make a final decision about whether the symbol belongs in the
4622
   local or global GOT.  */
4623
0
      if (mips_use_local_got_p (info, h))
4624
  /* The symbol belongs in the local GOT.  We no longer need this
4625
     entry if it was only used for relocations; those relocations
4626
     will be against the null or section symbol instead of H.  */
4627
0
  h->global_got_area = GGA_NONE;
4628
0
      else if (htab->root.target_os == is_vxworks
4629
0
         && h->got_only_for_calls
4630
0
         && h->root.plt.plist->mips_offset != MINUS_ONE)
4631
  /* On VxWorks, calls can refer directly to the .got.plt entry;
4632
     they don't need entries in the regular GOT.  .got.plt entries
4633
     will be allocated by _bfd_mips_elf_adjust_dynamic_symbol.  */
4634
0
  h->global_got_area = GGA_NONE;
4635
0
      else if (h->global_got_area == GGA_RELOC_ONLY)
4636
0
  {
4637
0
    g->reloc_only_gotno++;
4638
0
    g->global_gotno++;
4639
0
  }
4640
0
    }
4641
0
  return 1;
4642
0
}
4643

4644
/* A htab_traverse callback for GOT entries.  Add each one to the GOT
4645
   given in mips_elf_traverse_got_arg DATA.  Clear DATA->G on error.  */
4646
4647
static int
4648
mips_elf_add_got_entry (void **entryp, void *data)
4649
0
{
4650
0
  struct mips_got_entry *entry;
4651
0
  struct mips_elf_traverse_got_arg *arg;
4652
0
  void **slot;
4653
4654
0
  entry = (struct mips_got_entry *) *entryp;
4655
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4656
0
  slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4657
0
  if (!slot)
4658
0
    {
4659
0
      arg->g = NULL;
4660
0
      return 0;
4661
0
    }
4662
0
  if (!*slot)
4663
0
    {
4664
0
      *slot = entry;
4665
0
      mips_elf_count_got_entry (arg->info, arg->g, entry);
4666
0
    }
4667
0
  return 1;
4668
0
}
4669
4670
/* A htab_traverse callback for GOT page entries.  Add each one to the GOT
4671
   given in mips_elf_traverse_got_arg DATA.  Clear DATA->G on error.  */
4672
4673
static int
4674
mips_elf_add_got_page_entry (void **entryp, void *data)
4675
0
{
4676
0
  struct mips_got_page_entry *entry;
4677
0
  struct mips_elf_traverse_got_arg *arg;
4678
0
  void **slot;
4679
4680
0
  entry = (struct mips_got_page_entry *) *entryp;
4681
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4682
0
  slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4683
0
  if (!slot)
4684
0
    {
4685
0
      arg->g = NULL;
4686
0
      return 0;
4687
0
    }
4688
0
  if (!*slot)
4689
0
    {
4690
0
      *slot = entry;
4691
0
      arg->g->page_gotno += entry->num_pages;
4692
0
    }
4693
0
  return 1;
4694
0
}
4695
4696
/* Consider merging FROM, which is ABFD's GOT, into TO.  Return -1 if
4697
   this would lead to overflow, 1 if they were merged successfully,
4698
   and 0 if a merge failed due to lack of memory.  (These values are chosen
4699
   so that nonnegative return values can be returned by a htab_traverse
4700
   callback.)  */
4701
4702
static int
4703
mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
4704
       struct mips_got_info *to,
4705
       struct mips_elf_got_per_bfd_arg *arg)
4706
0
{
4707
0
  struct mips_elf_traverse_got_arg tga;
4708
0
  unsigned int estimate;
4709
4710
  /* Work out how many page entries we would need for the combined GOT.  */
4711
0
  estimate = arg->max_pages;
4712
0
  if (estimate >= from->page_gotno + to->page_gotno)
4713
0
    estimate = from->page_gotno + to->page_gotno;
4714
4715
  /* And conservatively estimate how many local and TLS entries
4716
     would be needed.  */
4717
0
  estimate += from->local_gotno + to->local_gotno;
4718
0
  estimate += from->tls_gotno + to->tls_gotno;
4719
4720
  /* If we're merging with the primary got, any TLS relocations will
4721
     come after the full set of global entries.  Otherwise estimate those
4722
     conservatively as well.  */
4723
0
  if (to == arg->primary && from->tls_gotno + to->tls_gotno)
4724
0
    estimate += arg->global_count;
4725
0
  else
4726
0
    estimate += from->global_gotno + to->global_gotno;
4727
4728
  /* Bail out if the combined GOT might be too big.  */
4729
0
  if (estimate > arg->max_count)
4730
0
    return -1;
4731
4732
  /* Transfer the bfd's got information from FROM to TO.  */
4733
0
  tga.info = arg->info;
4734
0
  tga.g = to;
4735
0
  htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4736
0
  if (!tga.g)
4737
0
    return 0;
4738
4739
0
  htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4740
0
  if (!tga.g)
4741
0
    return 0;
4742
4743
0
  mips_elf_replace_bfd_got (abfd, to);
4744
0
  return 1;
4745
0
}
4746
4747
/* Attempt to merge GOT G, which belongs to ABFD.  Try to use as much
4748
   as possible of the primary got, since it doesn't require explicit
4749
   dynamic relocations, but don't use bfds that would reference global
4750
   symbols out of the addressable range.  Failing the primary got,
4751
   attempt to merge with the current got, or finish the current got
4752
   and then make make the new got current.  */
4753
4754
static bool
4755
mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4756
        struct mips_elf_got_per_bfd_arg *arg)
4757
0
{
4758
0
  unsigned int estimate;
4759
0
  int result;
4760
4761
0
  if (!mips_elf_resolve_final_got_entries (arg->info, g))
4762
0
    return false;
4763
4764
  /* Work out the number of page, local and TLS entries.  */
4765
0
  estimate = arg->max_pages;
4766
0
  if (estimate > g->page_gotno)
4767
0
    estimate = g->page_gotno;
4768
0
  estimate += g->local_gotno + g->tls_gotno;
4769
4770
  /* We place TLS GOT entries after both locals and globals.  The globals
4771
     for the primary GOT may overflow the normal GOT size limit, so be
4772
     sure not to merge a GOT which requires TLS with the primary GOT in that
4773
     case.  This doesn't affect non-primary GOTs.  */
4774
0
  estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4775
4776
0
  if (estimate <= arg->max_count)
4777
0
    {
4778
      /* If we don't have a primary GOT, use it as
4779
   a starting point for the primary GOT.  */
4780
0
      if (!arg->primary)
4781
0
  {
4782
0
    arg->primary = g;
4783
0
    return true;
4784
0
  }
4785
4786
      /* Try merging with the primary GOT.  */
4787
0
      result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
4788
0
      if (result >= 0)
4789
0
  return result;
4790
0
    }
4791
4792
  /* If we can merge with the last-created got, do it.  */
4793
0
  if (arg->current)
4794
0
    {
4795
0
      result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
4796
0
      if (result >= 0)
4797
0
  return result;
4798
0
    }
4799
4800
  /* Well, we couldn't merge, so create a new GOT.  Don't check if it
4801
     fits; if it turns out that it doesn't, we'll get relocation
4802
     overflows anyway.  */
4803
0
  g->next = arg->current;
4804
0
  arg->current = g;
4805
4806
0
  return true;
4807
0
}
4808
4809
/* ENTRYP is a hash table entry for a mips_got_entry.  Set its gotidx
4810
   to GOTIDX, duplicating the entry if it has already been assigned
4811
   an index in a different GOT.  */
4812
4813
static bool
4814
mips_elf_set_gotidx (void **entryp, long gotidx)
4815
0
{
4816
0
  struct mips_got_entry *entry;
4817
4818
0
  entry = (struct mips_got_entry *) *entryp;
4819
0
  if (entry->gotidx > 0)
4820
0
    {
4821
0
      struct mips_got_entry *new_entry;
4822
4823
0
      new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4824
0
      if (!new_entry)
4825
0
  return false;
4826
4827
0
      *new_entry = *entry;
4828
0
      *entryp = new_entry;
4829
0
      entry = new_entry;
4830
0
    }
4831
0
  entry->gotidx = gotidx;
4832
0
  return true;
4833
0
}
4834
4835
/* Set the TLS GOT index for the GOT entry in ENTRYP.  DATA points to a
4836
   mips_elf_traverse_got_arg in which DATA->value is the size of one
4837
   GOT entry.  Set DATA->g to null on failure.  */
4838
4839
static int
4840
mips_elf_initialize_tls_index (void **entryp, void *data)
4841
0
{
4842
0
  struct mips_got_entry *entry;
4843
0
  struct mips_elf_traverse_got_arg *arg;
4844
4845
  /* We're only interested in TLS symbols.  */
4846
0
  entry = (struct mips_got_entry *) *entryp;
4847
0
  if (entry->tls_type == GOT_TLS_NONE)
4848
0
    return 1;
4849
4850
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4851
0
  if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
4852
0
    {
4853
0
      arg->g = NULL;
4854
0
      return 0;
4855
0
    }
4856
4857
  /* Account for the entries we've just allocated.  */
4858
0
  arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
4859
0
  return 1;
4860
0
}
4861
4862
/* A htab_traverse callback for GOT entries, where DATA points to a
4863
   mips_elf_traverse_got_arg.  Set the global_got_area of each global
4864
   symbol to DATA->value.  */
4865
4866
static int
4867
mips_elf_set_global_got_area (void **entryp, void *data)
4868
0
{
4869
0
  struct mips_got_entry *entry;
4870
0
  struct mips_elf_traverse_got_arg *arg;
4871
4872
0
  entry = (struct mips_got_entry *) *entryp;
4873
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4874
0
  if (entry->abfd != NULL
4875
0
      && entry->symndx == -1
4876
0
      && entry->d.h->global_got_area != GGA_NONE)
4877
0
    entry->d.h->global_got_area = arg->value;
4878
0
  return 1;
4879
0
}
4880
4881
/* A htab_traverse callback for secondary GOT entries, where DATA points
4882
   to a mips_elf_traverse_got_arg.  Assign GOT indices to global entries
4883
   and record the number of relocations they require.  DATA->value is
4884
   the size of one GOT entry.  Set DATA->g to null on failure.  */
4885
4886
static int
4887
mips_elf_set_global_gotidx (void **entryp, void *data)
4888
0
{
4889
0
  struct mips_got_entry *entry;
4890
0
  struct mips_elf_traverse_got_arg *arg;
4891
4892
0
  entry = (struct mips_got_entry *) *entryp;
4893
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4894
0
  if (entry->abfd != NULL
4895
0
      && entry->symndx == -1
4896
0
      && entry->d.h->global_got_area != GGA_NONE)
4897
0
    {
4898
0
      if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
4899
0
  {
4900
0
    arg->g = NULL;
4901
0
    return 0;
4902
0
  }
4903
0
      arg->g->assigned_low_gotno += 1;
4904
4905
0
      if (bfd_link_pic (arg->info)
4906
0
    || (elf_hash_table (arg->info)->dynamic_sections_created
4907
0
        && entry->d.h->root.def_dynamic
4908
0
        && !entry->d.h->root.def_regular))
4909
0
  arg->g->relocs += 1;
4910
0
    }
4911
4912
0
  return 1;
4913
0
}
4914
4915
/* A htab_traverse callback for GOT entries for which DATA is the
4916
   bfd_link_info.  Forbid any global symbols from having traditional
4917
   lazy-binding stubs.  */
4918
4919
static int
4920
mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4921
0
{
4922
0
  struct bfd_link_info *info;
4923
0
  struct mips_elf_link_hash_table *htab;
4924
0
  struct mips_got_entry *entry;
4925
4926
0
  entry = (struct mips_got_entry *) *entryp;
4927
0
  info = (struct bfd_link_info *) data;
4928
0
  htab = mips_elf_hash_table (info);
4929
0
  BFD_ASSERT (htab != NULL);
4930
4931
0
  if (entry->abfd != NULL
4932
0
      && entry->symndx == -1
4933
0
      && entry->d.h->needs_lazy_stub)
4934
0
    {
4935
0
      entry->d.h->needs_lazy_stub = false;
4936
0
      htab->lazy_stub_count--;
4937
0
    }
4938
4939
0
  return 1;
4940
0
}
4941
4942
/* Return the offset of an input bfd IBFD's GOT from the beginning of
4943
   the primary GOT.  */
4944
static bfd_vma
4945
mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4946
0
{
4947
0
  if (!g->next)
4948
0
    return 0;
4949
4950
0
  g = mips_elf_bfd_got (ibfd, false);
4951
0
  if (! g)
4952
0
    return 0;
4953
4954
0
  BFD_ASSERT (g->next);
4955
4956
0
  g = g->next;
4957
4958
0
  return (g->local_gotno + g->global_gotno + g->tls_gotno)
4959
0
    * MIPS_ELF_GOT_SIZE (abfd);
4960
0
}
4961
4962
/* Turn a single GOT that is too big for 16-bit addressing into
4963
   a sequence of GOTs, each one 16-bit addressable.  */
4964
4965
static bool
4966
mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4967
        asection *got, bfd_size_type pages)
4968
0
{
4969
0
  struct mips_elf_link_hash_table *htab;
4970
0
  struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4971
0
  struct mips_elf_traverse_got_arg tga;
4972
0
  struct mips_got_info *g, *gg;
4973
0
  unsigned int assign, needed_relocs;
4974
0
  bfd *dynobj, *ibfd;
4975
4976
0
  dynobj = elf_hash_table (info)->dynobj;
4977
0
  htab = mips_elf_hash_table (info);
4978
0
  BFD_ASSERT (htab != NULL);
4979
4980
0
  g = htab->got_info;
4981
4982
0
  got_per_bfd_arg.obfd = abfd;
4983
0
  got_per_bfd_arg.info = info;
4984
0
  got_per_bfd_arg.current = NULL;
4985
0
  got_per_bfd_arg.primary = NULL;
4986
0
  got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4987
0
        / MIPS_ELF_GOT_SIZE (abfd))
4988
0
             - htab->reserved_gotno);
4989
0
  got_per_bfd_arg.max_pages = pages;
4990
  /* The number of globals that will be included in the primary GOT.
4991
     See the calls to mips_elf_set_global_got_area below for more
4992
     information.  */
4993
0
  got_per_bfd_arg.global_count = g->global_gotno;
4994
4995
  /* Try to merge the GOTs of input bfds together, as long as they
4996
     don't seem to exceed the maximum GOT size, choosing one of them
4997
     to be the primary GOT.  */
4998
0
  for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
4999
0
    {
5000
0
      gg = mips_elf_bfd_got (ibfd, false);
5001
0
      if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
5002
0
  return false;
5003
0
    }
5004
5005
  /* If we do not find any suitable primary GOT, create an empty one.  */
5006
0
  if (got_per_bfd_arg.primary == NULL)
5007
0
    g->next = mips_elf_create_got_info (abfd);
5008
0
  else
5009
0
    g->next = got_per_bfd_arg.primary;
5010
0
  g->next->next = got_per_bfd_arg.current;
5011
5012
  /* GG is now the master GOT, and G is the primary GOT.  */
5013
0
  gg = g;
5014
0
  g = g->next;
5015
5016
  /* Map the output bfd to the primary got.  That's what we're going
5017
     to use for bfds that use GOT16 or GOT_PAGE relocations that we
5018
     didn't mark in check_relocs, and we want a quick way to find it.
5019
     We can't just use gg->next because we're going to reverse the
5020
     list.  */
5021
0
  mips_elf_replace_bfd_got (abfd, g);
5022
5023
  /* Every symbol that is referenced in a dynamic relocation must be
5024
     present in the primary GOT, so arrange for them to appear after
5025
     those that are actually referenced.  */
5026
0
  gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
5027
0
  g->global_gotno = gg->global_gotno;
5028
5029
0
  tga.info = info;
5030
0
  tga.value = GGA_RELOC_ONLY;
5031
0
  htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
5032
0
  tga.value = GGA_NORMAL;
5033
0
  htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
5034
5035
  /* Now go through the GOTs assigning them offset ranges.
5036
     [assigned_low_gotno, local_gotno[ will be set to the range of local
5037
     entries in each GOT.  We can then compute the end of a GOT by
5038
     adding local_gotno to global_gotno.  We reverse the list and make
5039
     it circular since then we'll be able to quickly compute the
5040
     beginning of a GOT, by computing the end of its predecessor.  To
5041
     avoid special cases for the primary GOT, while still preserving
5042
     assertions that are valid for both single- and multi-got links,
5043
     we arrange for the main got struct to have the right number of
5044
     global entries, but set its local_gotno such that the initial
5045
     offset of the primary GOT is zero.  Remember that the primary GOT
5046
     will become the last item in the circular linked list, so it
5047
     points back to the master GOT.  */
5048
0
  gg->local_gotno = -g->global_gotno;
5049
0
  gg->global_gotno = g->global_gotno;
5050
0
  gg->tls_gotno = 0;
5051
0
  assign = 0;
5052
0
  gg->next = gg;
5053
5054
0
  do
5055
0
    {
5056
0
      struct mips_got_info *gn;
5057
5058
0
      assign += htab->reserved_gotno;
5059
0
      g->assigned_low_gotno = assign;
5060
0
      g->local_gotno += assign;
5061
0
      g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
5062
0
      g->assigned_high_gotno = g->local_gotno - 1;
5063
0
      assign = g->local_gotno + g->global_gotno + g->tls_gotno;
5064
5065
      /* Take g out of the direct list, and push it onto the reversed
5066
   list that gg points to.  g->next is guaranteed to be nonnull after
5067
   this operation, as required by mips_elf_initialize_tls_index. */
5068
0
      gn = g->next;
5069
0
      g->next = gg->next;
5070
0
      gg->next = g;
5071
5072
      /* Set up any TLS entries.  We always place the TLS entries after
5073
   all non-TLS entries.  */
5074
0
      g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
5075
0
      tga.g = g;
5076
0
      tga.value = MIPS_ELF_GOT_SIZE (abfd);
5077
0
      htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
5078
0
      if (!tga.g)
5079
0
  return false;
5080
0
      BFD_ASSERT (g->tls_assigned_gotno == assign);
5081
5082
      /* Move onto the next GOT.  It will be a secondary GOT if nonull.  */
5083
0
      g = gn;
5084
5085
      /* Forbid global symbols in every non-primary GOT from having
5086
   lazy-binding stubs.  */
5087
0
      if (g)
5088
0
  htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
5089
0
    }
5090
0
  while (g);
5091
5092
0
  got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
5093
5094
0
  needed_relocs = 0;
5095
0
  for (g = gg->next; g && g->next != gg; g = g->next)
5096
0
    {
5097
0
      unsigned int save_assign;
5098
5099
      /* Assign offsets to global GOT entries and count how many
5100
   relocations they need.  */
5101
0
      save_assign = g->assigned_low_gotno;
5102
0
      g->assigned_low_gotno = g->local_gotno;
5103
0
      tga.info = info;
5104
0
      tga.value = MIPS_ELF_GOT_SIZE (abfd);
5105
0
      tga.g = g;
5106
0
      htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
5107
0
      if (!tga.g)
5108
0
  return false;
5109
0
      BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
5110
0
      g->assigned_low_gotno = save_assign;
5111
5112
0
      if (bfd_link_pic (info))
5113
0
  {
5114
0
    g->relocs += g->local_gotno - g->assigned_low_gotno;
5115
0
    BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
5116
0
          + g->next->global_gotno
5117
0
          + g->next->tls_gotno
5118
0
          + htab->reserved_gotno);
5119
0
  }
5120
0
      needed_relocs += g->relocs;
5121
0
    }
5122
0
  needed_relocs += g->relocs;
5123
5124
0
  if (needed_relocs)
5125
0
    mips_elf_allocate_dynamic_relocations (dynobj, info,
5126
0
             needed_relocs);
5127
5128
0
  return true;
5129
0
}
5130
5131

5132
/* Returns the first relocation of type r_type found, beginning with
5133
   RELOCATION.  RELEND is one-past-the-end of the relocation table.  */
5134
5135
static const Elf_Internal_Rela *
5136
mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
5137
        const Elf_Internal_Rela *relocation,
5138
        const Elf_Internal_Rela *relend)
5139
0
{
5140
0
  unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
5141
5142
0
  while (relocation < relend)
5143
0
    {
5144
0
      if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
5145
0
    && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
5146
0
  return relocation;
5147
5148
0
      ++relocation;
5149
0
    }
5150
5151
  /* We didn't find it.  */
5152
0
  return NULL;
5153
0
}
5154
5155
/* Return whether an input relocation is against a local symbol.  */
5156
5157
static bool
5158
mips_elf_local_relocation_p (bfd *input_bfd,
5159
           const Elf_Internal_Rela *relocation,
5160
           asection **local_sections)
5161
0
{
5162
0
  unsigned long r_symndx;
5163
0
  Elf_Internal_Shdr *symtab_hdr;
5164
0
  size_t extsymoff;
5165
5166
0
  r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5167
0
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5168
0
  extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
5169
5170
0
  if (r_symndx < extsymoff)
5171
0
    return true;
5172
0
  if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
5173
0
    return true;
5174
5175
0
  return false;
5176
0
}
5177

5178
/* Sign-extend VALUE, which has the indicated number of BITS.  */
5179
5180
bfd_vma
5181
_bfd_mips_elf_sign_extend (bfd_vma value, int bits)
5182
9
{
5183
9
  if (value & ((bfd_vma) 1 << (bits - 1)))
5184
    /* VALUE is negative.  */
5185
1
    value |= ((bfd_vma) - 1) << bits;
5186
5187
9
  return value;
5188
9
}
5189
5190
/* Return non-zero if the indicated VALUE has overflowed the maximum
5191
   range expressible by a signed number with the indicated number of
5192
   BITS.  */
5193
5194
static bool
5195
mips_elf_overflow_p (bfd_vma value, int bits)
5196
0
{
5197
0
  bfd_signed_vma svalue = (bfd_signed_vma) value;
5198
5199
0
  if (svalue > (1 << (bits - 1)) - 1)
5200
    /* The value is too big.  */
5201
0
    return true;
5202
0
  else if (svalue < -(1 << (bits - 1)))
5203
    /* The value is too small.  */
5204
0
    return true;
5205
5206
  /* All is well.  */
5207
0
  return false;
5208
0
}
5209
5210
/* Calculate the %high function.  */
5211
5212
static bfd_vma
5213
mips_elf_high (bfd_vma value)
5214
0
{
5215
0
  return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5216
0
}
5217
5218
/* Calculate the %higher function.  */
5219
5220
static bfd_vma
5221
mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
5222
0
{
5223
0
#ifdef BFD64
5224
0
  return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5225
#else
5226
  abort ();
5227
  return MINUS_ONE;
5228
#endif
5229
0
}
5230
5231
/* Calculate the %highest function.  */
5232
5233
static bfd_vma
5234
mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
5235
0
{
5236
0
#ifdef BFD64
5237
0
  return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
5238
#else
5239
  abort ();
5240
  return MINUS_ONE;
5241
#endif
5242
0
}
5243

5244
/* Create the .compact_rel section.  */
5245
5246
static bool
5247
mips_elf_create_compact_rel_section
5248
  (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
5249
0
{
5250
0
  flagword flags;
5251
0
  register asection *s;
5252
5253
0
  if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
5254
0
    {
5255
0
      flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5256
0
         | SEC_READONLY);
5257
5258
0
      s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
5259
0
      if (s == NULL
5260
0
    || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5261
0
  return false;
5262
5263
0
      s->size = sizeof (Elf32_External_compact_rel);
5264
0
    }
5265
5266
0
  return true;
5267
0
}
5268
5269
/* Create the .got section to hold the global offset table.  */
5270
5271
static bool
5272
mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
5273
0
{
5274
0
  flagword flags;
5275
0
  register asection *s;
5276
0
  struct elf_link_hash_entry *h;
5277
0
  struct bfd_link_hash_entry *bh;
5278
0
  struct mips_elf_link_hash_table *htab;
5279
5280
0
  htab = mips_elf_hash_table (info);
5281
0
  BFD_ASSERT (htab != NULL);
5282
5283
  /* This function may be called more than once.  */
5284
0
  if (htab->root.sgot)
5285
0
    return true;
5286
5287
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5288
0
     | SEC_LINKER_CREATED);
5289
5290
  /* We have to use an alignment of 2**4 here because this is hardcoded
5291
     in the function stub generation and in the linker script.  */
5292
0
  s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
5293
0
  if (s == NULL
5294
0
      || !bfd_set_section_alignment (s, 4))
5295
0
    return false;
5296
0
  htab->root.sgot = s;
5297
5298
  /* Define the symbol _GLOBAL_OFFSET_TABLE_.  We don't do this in the
5299
     linker script because we don't want to define the symbol if we
5300
     are not creating a global offset table.  */
5301
0
  bh = NULL;
5302
0
  if (! (_bfd_generic_link_add_one_symbol
5303
0
   (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5304
0
    0, NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
5305
0
    return false;
5306
5307
0
  h = (struct elf_link_hash_entry *) bh;
5308
0
  h->non_elf = 0;
5309
0
  h->def_regular = 1;
5310
0
  h->type = STT_OBJECT;
5311
0
  h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
5312
0
  elf_hash_table (info)->hgot = h;
5313
5314
0
  if (bfd_link_pic (info)
5315
0
      && ! bfd_elf_link_record_dynamic_symbol (info, h))
5316
0
    return false;
5317
5318
0
  htab->got_info = mips_elf_create_got_info (abfd);
5319
0
  mips_elf_section_data (s)->elf.this_hdr.sh_flags
5320
0
    |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5321
5322
  /* We also need a .got.plt section when generating PLTs.  */
5323
0
  s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5324
0
            SEC_ALLOC | SEC_LOAD
5325
0
            | SEC_HAS_CONTENTS
5326
0
            | SEC_IN_MEMORY
5327
0
            | SEC_LINKER_CREATED);
5328
0
  if (s == NULL)
5329
0
    return false;
5330
0
  htab->root.sgotplt = s;
5331
5332
0
  return true;
5333
0
}
5334

5335
/* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5336
   __GOTT_INDEX__ symbols.  These symbols are only special for
5337
   shared objects; they are not used in executables.  */
5338
5339
static bool
5340
is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5341
0
{
5342
0
  return (mips_elf_hash_table (info)->root.target_os == is_vxworks
5343
0
    && bfd_link_pic (info)
5344
0
    && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5345
0
        || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5346
0
}
5347
5348
/* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5349
   require an la25 stub.  See also mips_elf_local_pic_function_p,
5350
   which determines whether the destination function ever requires a
5351
   stub.  */
5352
5353
static bool
5354
mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
5355
             bool target_is_16_bit_code_p)
5356
0
{
5357
  /* We specifically ignore branches and jumps from EF_PIC objects,
5358
     where the onus is on the compiler or programmer to perform any
5359
     necessary initialization of $25.  Sometimes such initialization
5360
     is unnecessary; for example, -mno-shared functions do not use
5361
     the incoming value of $25, and may therefore be called directly.  */
5362
0
  if (PIC_OBJECT_P (input_bfd))
5363
0
    return false;
5364
5365
0
  switch (r_type)
5366
0
    {
5367
0
    case R_MIPS_26:
5368
0
    case R_MIPS_PC16:
5369
0
    case R_MIPS_PC21_S2:
5370
0
    case R_MIPS_PC26_S2:
5371
0
    case R_MICROMIPS_26_S1:
5372
0
    case R_MICROMIPS_PC7_S1:
5373
0
    case R_MICROMIPS_PC10_S1:
5374
0
    case R_MICROMIPS_PC16_S1:
5375
0
    case R_MICROMIPS_PC23_S2:
5376
0
      return true;
5377
5378
0
    case R_MIPS16_26:
5379
0
      return !target_is_16_bit_code_p;
5380
5381
0
    default:
5382
0
      return false;
5383
0
    }
5384
0
}
5385

5386
/* Obtain the field relocated by RELOCATION.  */
5387
5388
static bfd_vma
5389
mips_elf_obtain_contents (reloc_howto_type *howto,
5390
        const Elf_Internal_Rela *relocation,
5391
        bfd *input_bfd, bfd_byte *contents)
5392
0
{
5393
0
  bfd_vma x = 0;
5394
0
  bfd_byte *location = contents + relocation->r_offset;
5395
0
  unsigned int size = bfd_get_reloc_size (howto);
5396
5397
  /* Obtain the bytes.  */
5398
0
  if (size != 0)
5399
0
    x = bfd_get (8 * size, input_bfd, location);
5400
5401
0
  return x;
5402
0
}
5403
5404
/* Store the field relocated by RELOCATION.  */
5405
5406
static void
5407
mips_elf_store_contents (reloc_howto_type *howto,
5408
       const Elf_Internal_Rela *relocation,
5409
       bfd *input_bfd, bfd_byte *contents, bfd_vma x)
5410
0
{
5411
0
  bfd_byte *location = contents + relocation->r_offset;
5412
0
  unsigned int size = bfd_get_reloc_size (howto);
5413
5414
  /* Put the value into the output.  */
5415
0
  if (size != 0)
5416
0
    bfd_put (8 * size, input_bfd, x, location);
5417
0
}
5418
5419
/* Try to patch a load from GOT instruction in CONTENTS pointed to by
5420
   RELOCATION described by HOWTO, with a move of 0 to the load target
5421
   register, returning TRUE if that is successful and FALSE otherwise.
5422
   If DOIT is FALSE, then only determine it patching is possible and
5423
   return status without actually changing CONTENTS.
5424
*/
5425
5426
static bool
5427
mips_elf_nullify_got_load (bfd *input_bfd, bfd_byte *contents,
5428
         const Elf_Internal_Rela *relocation,
5429
         reloc_howto_type *howto, bool doit)
5430
0
{
5431
0
  int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5432
0
  bfd_byte *location = contents + relocation->r_offset;
5433
0
  bool nullified = true;
5434
0
  bfd_vma x;
5435
5436
0
  _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
5437
5438
  /* Obtain the current value.  */
5439
0
  x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5440
5441
  /* Note that in the unshuffled MIPS16 encoding RX is at bits [21:19]
5442
     while RY is at bits [18:16] of the combined 32-bit instruction word.  */
5443
0
  if (mips16_reloc_p (r_type)
5444
0
      && (((x >> 22) & 0x3ff) == 0x3d3        /* LW */
5445
0
    || ((x >> 22) & 0x3ff) == 0x3c7))      /* LD */
5446
0
    x = (0x3cdU << 22) | (x & (7 << 16)) << 3;     /* LI */
5447
0
  else if (micromips_reloc_p (r_type)
5448
0
     && ((x >> 26) & 0x37) == 0x37)     /* LW/LD */
5449
0
    x = (0xc << 26) | (x & (0x1f << 21));     /* ADDIU */
5450
0
  else if (((x >> 26) & 0x3f) == 0x23        /* LW */
5451
0
     || ((x >> 26) & 0x3f) == 0x37)     /* LD */
5452
0
    x = (0x9 << 26) | (x & (0x1f << 16));     /* ADDIU */
5453
0
  else
5454
0
    nullified = false;
5455
5456
  /* Put the value into the output.  */
5457
0
  if (doit && nullified)
5458
0
    mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
5459
5460
0
  _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, false, location);
5461
5462
0
  return nullified;
5463
0
}
5464
5465
/* Calculate the value produced by the RELOCATION (which comes from
5466
   the INPUT_BFD).  The ADDEND is the addend to use for this
5467
   RELOCATION; RELOCATION->R_ADDEND is ignored.
5468
5469
   The result of the relocation calculation is stored in VALUEP.
5470
   On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
5471
   is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
5472
5473
   This function returns bfd_reloc_continue if the caller need take no
5474
   further action regarding this relocation, bfd_reloc_notsupported if
5475
   something goes dramatically wrong, bfd_reloc_overflow if an
5476
   overflow occurs, and bfd_reloc_ok to indicate success.  */
5477
5478
static bfd_reloc_status_type
5479
mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
5480
             asection *input_section, bfd_byte *contents,
5481
             struct bfd_link_info *info,
5482
             const Elf_Internal_Rela *relocation,
5483
             bfd_vma addend, reloc_howto_type *howto,
5484
             Elf_Internal_Sym *local_syms,
5485
             asection **local_sections, bfd_vma *valuep,
5486
             const char **namep,
5487
             bool *cross_mode_jump_p,
5488
             bool save_addend)
5489
0
{
5490
  /* The eventual value we will return.  */
5491
0
  bfd_vma value;
5492
  /* The address of the symbol against which the relocation is
5493
     occurring.  */
5494
0
  bfd_vma symbol = 0;
5495
  /* The final GP value to be used for the relocatable, executable, or
5496
     shared object file being produced.  */
5497
0
  bfd_vma gp;
5498
  /* The place (section offset or address) of the storage unit being
5499
     relocated.  */
5500
0
  bfd_vma p;
5501
  /* The value of GP used to create the relocatable object.  */
5502
0
  bfd_vma gp0;
5503
  /* The offset into the global offset table at which the address of
5504
     the relocation entry symbol, adjusted by the addend, resides
5505
     during execution.  */
5506
0
  bfd_vma g = MINUS_ONE;
5507
  /* The section in which the symbol referenced by the relocation is
5508
     located.  */
5509
0
  asection *sec = NULL;
5510
0
  struct mips_elf_link_hash_entry *h = NULL;
5511
  /* TRUE if the symbol referred to by this relocation is a local
5512
     symbol.  */
5513
0
  bool local_p, was_local_p;
5514
  /* TRUE if the symbol referred to by this relocation is a section
5515
     symbol.  */
5516
0
  bool section_p = false;
5517
  /* TRUE if the symbol referred to by this relocation is "_gp_disp".  */
5518
0
  bool gp_disp_p = false;
5519
  /* TRUE if the symbol referred to by this relocation is
5520
     "__gnu_local_gp".  */
5521
0
  bool gnu_local_gp_p = false;
5522
0
  Elf_Internal_Shdr *symtab_hdr;
5523
0
  size_t extsymoff;
5524
0
  unsigned long r_symndx;
5525
0
  int r_type;
5526
  /* TRUE if overflow occurred during the calculation of the
5527
     relocation value.  */
5528
0
  bool overflowed_p;
5529
  /* TRUE if this relocation refers to a MIPS16 function.  */
5530
0
  bool target_is_16_bit_code_p = false;
5531
0
  bool target_is_micromips_code_p = false;
5532
0
  struct mips_elf_link_hash_table *htab;
5533
0
  bfd *dynobj;
5534
0
  bool resolved_to_zero;
5535
5536
0
  dynobj = elf_hash_table (info)->dynobj;
5537
0
  htab = mips_elf_hash_table (info);
5538
0
  BFD_ASSERT (htab != NULL);
5539
5540
  /* Parse the relocation.  */
5541
0
  r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5542
0
  r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5543
0
  p = (input_section->output_section->vma
5544
0
       + input_section->output_offset
5545
0
       + relocation->r_offset);
5546
5547
  /* Assume that there will be no overflow.  */
5548
0
  overflowed_p = false;
5549
5550
  /* Figure out whether or not the symbol is local, and get the offset
5551
     used in the array of hash table entries.  */
5552
0
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5553
0
  local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5554
0
           local_sections);
5555
0
  was_local_p = local_p;
5556
0
  if (! elf_bad_symtab (input_bfd))
5557
0
    extsymoff = symtab_hdr->sh_info;
5558
0
  else
5559
0
    {
5560
      /* The symbol table does not follow the rule that local symbols
5561
   must come before globals.  */
5562
0
      extsymoff = 0;
5563
0
    }
5564
5565
  /* Figure out the value of the symbol.  */
5566
0
  if (local_p)
5567
0
    {
5568
0
      bool micromips_p = MICROMIPS_P (abfd);
5569
0
      Elf_Internal_Sym *sym;
5570
5571
0
      sym = local_syms + r_symndx;
5572
0
      sec = local_sections[r_symndx];
5573
5574
0
      section_p = ELF_ST_TYPE (sym->st_info) == STT_SECTION;
5575
5576
0
      symbol = sec->output_section->vma + sec->output_offset;
5577
0
      if (!section_p || (sec->flags & SEC_MERGE))
5578
0
  symbol += sym->st_value;
5579
0
      if ((sec->flags & SEC_MERGE) && section_p)
5580
0
  {
5581
0
    addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5582
0
    addend -= symbol;
5583
0
    addend += sec->output_section->vma + sec->output_offset;
5584
0
  }
5585
5586
      /* MIPS16/microMIPS text labels should be treated as odd.  */
5587
0
      if (ELF_ST_IS_COMPRESSED (sym->st_other))
5588
0
  ++symbol;
5589
5590
      /* Record the name of this symbol, for our caller.  */
5591
0
      *namep = bfd_elf_string_from_elf_section (input_bfd,
5592
0
            symtab_hdr->sh_link,
5593
0
            sym->st_name);
5594
0
      if (*namep == NULL || **namep == '\0')
5595
0
  *namep = bfd_section_name (sec);
5596
5597
      /* For relocations against a section symbol and ones against no
5598
   symbol (absolute relocations) infer the ISA mode from the addend.  */
5599
0
      if (section_p || r_symndx == STN_UNDEF)
5600
0
  {
5601
0
    target_is_16_bit_code_p = (addend & 1) && !micromips_p;
5602
0
    target_is_micromips_code_p = (addend & 1) && micromips_p;
5603
0
  }
5604
      /* For relocations against an absolute symbol infer the ISA mode
5605
   from the value of the symbol plus addend.  */
5606
0
      else if (bfd_is_abs_section (sec))
5607
0
  {
5608
0
    target_is_16_bit_code_p = ((symbol + addend) & 1) && !micromips_p;
5609
0
    target_is_micromips_code_p = ((symbol + addend) & 1) && micromips_p;
5610
0
  }
5611
      /* Otherwise just use the regular symbol annotation available.  */
5612
0
      else
5613
0
  {
5614
0
    target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5615
0
    target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5616
0
  }
5617
0
    }
5618
0
  else
5619
0
    {
5620
      /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ?  */
5621
5622
      /* For global symbols we look up the symbol in the hash-table.  */
5623
0
      h = ((struct mips_elf_link_hash_entry *)
5624
0
     elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5625
      /* Find the real hash-table entry for this symbol.  */
5626
0
      while (h->root.root.type == bfd_link_hash_indirect
5627
0
       || h->root.root.type == bfd_link_hash_warning)
5628
0
  h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5629
5630
      /* Record the name of this symbol, for our caller.  */
5631
0
      *namep = h->root.root.root.string;
5632
5633
      /* See if this is the special _gp_disp symbol.  Note that such a
5634
   symbol must always be a global symbol.  */
5635
0
      if (strcmp (*namep, "_gp_disp") == 0
5636
0
    && ! NEWABI_P (input_bfd))
5637
0
  {
5638
    /* Relocations against _gp_disp are permitted only with
5639
       R_MIPS_HI16 and R_MIPS_LO16 relocations.  */
5640
0
    if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
5641
0
      return bfd_reloc_notsupported;
5642
5643
0
    gp_disp_p = true;
5644
0
  }
5645
      /* See if this is the special _gp symbol.  Note that such a
5646
   symbol must always be a global symbol.  */
5647
0
      else if (strcmp (*namep, "__gnu_local_gp") == 0)
5648
0
  gnu_local_gp_p = true;
5649
5650
5651
      /* If this symbol is defined, calculate its address.  Note that
5652
   _gp_disp is a magic symbol, always implicitly defined by the
5653
   linker, so it's inappropriate to check to see whether or not
5654
   its defined.  */
5655
0
      else if ((h->root.root.type == bfd_link_hash_defined
5656
0
    || h->root.root.type == bfd_link_hash_defweak)
5657
0
         && h->root.root.u.def.section)
5658
0
  {
5659
0
    sec = h->root.root.u.def.section;
5660
0
    if (sec->output_section)
5661
0
      symbol = (h->root.root.u.def.value
5662
0
          + sec->output_section->vma
5663
0
          + sec->output_offset);
5664
0
    else
5665
0
      symbol = h->root.root.u.def.value;
5666
0
  }
5667
0
      else if (h->root.root.type == bfd_link_hash_undefweak)
5668
  /* We allow relocations against undefined weak symbols, giving
5669
     it the value zero, so that you can undefined weak functions
5670
     and check to see if they exist by looking at their
5671
     addresses.  */
5672
0
  symbol = 0;
5673
0
      else if (info->unresolved_syms_in_objects == RM_IGNORE
5674
0
         && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5675
0
  symbol = 0;
5676
0
      else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5677
0
           ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5678
0
  {
5679
    /* If this is a dynamic link, we should have created a
5680
       _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5681
       in _bfd_mips_elf_create_dynamic_sections.
5682
       Otherwise, we should define the symbol with a value of 0.
5683
       FIXME: It should probably get into the symbol table
5684
       somehow as well.  */
5685
0
    BFD_ASSERT (! bfd_link_pic (info));
5686
0
    BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5687
0
    symbol = 0;
5688
0
  }
5689
0
      else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5690
0
  {
5691
    /* This is an optional symbol - an Irix specific extension to the
5692
       ELF spec.  Ignore it for now.
5693
       XXX - FIXME - there is more to the spec for OPTIONAL symbols
5694
       than simply ignoring them, but we do not handle this for now.
5695
       For information see the "64-bit ELF Object File Specification"
5696
       which is available from here:
5697
       http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf  */
5698
0
    symbol = 0;
5699
0
  }
5700
0
      else
5701
0
  {
5702
0
          bool reject_undefined
5703
0
      = ((info->unresolved_syms_in_objects == RM_DIAGNOSE
5704
0
    && !info->warn_unresolved_syms)
5705
0
         || ELF_ST_VISIBILITY (h->root.other) != STV_DEFAULT);
5706
5707
0
    info->callbacks->undefined_symbol
5708
0
      (info, h->root.root.root.string, input_bfd,
5709
0
       input_section, relocation->r_offset, reject_undefined);
5710
5711
0
    if (reject_undefined)
5712
0
      return bfd_reloc_undefined;
5713
5714
0
    symbol = 0;
5715
0
  }
5716
5717
0
      target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5718
0
      target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
5719
0
    }
5720
5721
  /* If this is a reference to a 16-bit function with a stub, we need
5722
     to redirect the relocation to the stub unless:
5723
5724
     (a) the relocation is for a MIPS16 JAL;
5725
5726
     (b) the relocation is for a MIPS16 PIC call, and there are no
5727
   non-MIPS16 uses of the GOT slot; or
5728
5729
     (c) the section allows direct references to MIPS16 functions.  */
5730
0
  if (r_type != R_MIPS16_26
5731
0
      && !bfd_link_relocatable (info)
5732
0
      && ((h != NULL
5733
0
     && h->fn_stub != NULL
5734
0
     && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5735
0
    || (local_p
5736
0
        && mips_elf_tdata (input_bfd)->local_stubs != NULL
5737
0
        && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5738
0
      && !section_allows_mips16_refs_p (input_section))
5739
0
    {
5740
      /* This is a 32- or 64-bit call to a 16-bit function.  We should
5741
   have already noticed that we were going to need the
5742
   stub.  */
5743
0
      if (local_p)
5744
0
  {
5745
0
    sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
5746
0
    value = 0;
5747
0
  }
5748
0
      else
5749
0
  {
5750
0
    BFD_ASSERT (h->need_fn_stub);
5751
0
    if (h->la25_stub)
5752
0
      {
5753
        /* If a LA25 header for the stub itself exists, point to the
5754
     prepended LUI/ADDIU sequence.  */
5755
0
        sec = h->la25_stub->stub_section;
5756
0
        value = h->la25_stub->offset;
5757
0
      }
5758
0
    else
5759
0
      {
5760
0
        sec = h->fn_stub;
5761
0
        value = 0;
5762
0
      }
5763
0
  }
5764
5765
0
      symbol = sec->output_section->vma + sec->output_offset + value;
5766
      /* The target is 16-bit, but the stub isn't.  */
5767
0
      target_is_16_bit_code_p = false;
5768
0
    }
5769
  /* If this is a MIPS16 call with a stub, that is made through the PLT or
5770
     to a standard MIPS function, we need to redirect the call to the stub.
5771
     Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5772
     indirect calls should use an indirect stub instead.  */
5773
0
  else if (r_type == R_MIPS16_26 && !bfd_link_relocatable (info)
5774
0
     && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5775
0
         || (local_p
5776
0
       && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5777
0
       && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5778
0
     && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
5779
0
    {
5780
0
      if (local_p)
5781
0
  sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5782
0
      else
5783
0
  {
5784
    /* If both call_stub and call_fp_stub are defined, we can figure
5785
       out which one to use by checking which one appears in the input
5786
       file.  */
5787
0
    if (h->call_stub != NULL && h->call_fp_stub != NULL)
5788
0
      {
5789
0
        asection *o;
5790
5791
0
        sec = NULL;
5792
0
        for (o = input_bfd->sections; o != NULL; o = o->next)
5793
0
    {
5794
0
      if (CALL_FP_STUB_P (bfd_section_name (o)))
5795
0
        {
5796
0
          sec = h->call_fp_stub;
5797
0
          break;
5798
0
        }
5799
0
    }
5800
0
        if (sec == NULL)
5801
0
    sec = h->call_stub;
5802
0
      }
5803
0
    else if (h->call_stub != NULL)
5804
0
      sec = h->call_stub;
5805
0
    else
5806
0
      sec = h->call_fp_stub;
5807
0
  }
5808
5809
0
      BFD_ASSERT (sec->size > 0);
5810
0
      symbol = sec->output_section->vma + sec->output_offset;
5811
0
    }
5812
  /* If this is a direct call to a PIC function, redirect to the
5813
     non-PIC stub.  */
5814
0
  else if (h != NULL && h->la25_stub
5815
0
     && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5816
0
               target_is_16_bit_code_p))
5817
0
    {
5818
0
  symbol = (h->la25_stub->stub_section->output_section->vma
5819
0
      + h->la25_stub->stub_section->output_offset
5820
0
      + h->la25_stub->offset);
5821
0
  if (ELF_ST_IS_MICROMIPS (h->root.other))
5822
0
    symbol |= 1;
5823
0
    }
5824
  /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5825
     entry is used if a standard PLT entry has also been made.  In this
5826
     case the symbol will have been set by mips_elf_set_plt_sym_value
5827
     to point to the standard PLT entry, so redirect to the compressed
5828
     one.  */
5829
0
  else if ((mips16_branch_reloc_p (r_type)
5830
0
      || micromips_branch_reloc_p (r_type))
5831
0
     && !bfd_link_relocatable (info)
5832
0
     && h != NULL
5833
0
     && h->use_plt_entry
5834
0
     && h->root.plt.plist->comp_offset != MINUS_ONE
5835
0
     && h->root.plt.plist->mips_offset != MINUS_ONE)
5836
0
    {
5837
0
      bool micromips_p = MICROMIPS_P (abfd);
5838
5839
0
      sec = htab->root.splt;
5840
0
      symbol = (sec->output_section->vma
5841
0
    + sec->output_offset
5842
0
    + htab->plt_header_size
5843
0
    + htab->plt_mips_offset
5844
0
    + h->root.plt.plist->comp_offset
5845
0
    + 1);
5846
5847
0
      target_is_16_bit_code_p = !micromips_p;
5848
0
      target_is_micromips_code_p = micromips_p;
5849
0
    }
5850
5851
  /* Make sure MIPS16 and microMIPS are not used together.  */
5852
0
  if ((mips16_branch_reloc_p (r_type) && target_is_micromips_code_p)
5853
0
      || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5854
0
   {
5855
0
      _bfd_error_handler
5856
0
  (_("MIPS16 and microMIPS functions cannot call each other"));
5857
0
      return bfd_reloc_notsupported;
5858
0
   }
5859
5860
  /* Calls from 16-bit code to 32-bit code and vice versa require the
5861
     mode change.  However, we can ignore calls to undefined weak symbols,
5862
     which should never be executed at runtime.  This exception is important
5863
     because the assembly writer may have "known" that any definition of the
5864
     symbol would be 16-bit code, and that direct jumps were therefore
5865
     acceptable.  */
5866
0
  *cross_mode_jump_p = (!bfd_link_relocatable (info)
5867
0
      && !(h && h->root.root.type == bfd_link_hash_undefweak)
5868
0
      && ((mips16_branch_reloc_p (r_type)
5869
0
           && !target_is_16_bit_code_p)
5870
0
          || (micromips_branch_reloc_p (r_type)
5871
0
        && !target_is_micromips_code_p)
5872
0
          || ((branch_reloc_p (r_type)
5873
0
         || r_type == R_MIPS_JALR)
5874
0
        && (target_is_16_bit_code_p
5875
0
            || target_is_micromips_code_p))));
5876
5877
0
  resolved_to_zero = (h != NULL
5878
0
          && UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->root));
5879
5880
0
  switch (r_type)
5881
0
    {
5882
0
    case R_MIPS16_CALL16:
5883
0
    case R_MIPS16_GOT16:
5884
0
    case R_MIPS_CALL16:
5885
0
    case R_MIPS_GOT16:
5886
0
    case R_MIPS_GOT_PAGE:
5887
0
    case R_MIPS_GOT_DISP:
5888
0
    case R_MIPS_GOT_LO16:
5889
0
    case R_MIPS_CALL_LO16:
5890
0
    case R_MICROMIPS_CALL16:
5891
0
    case R_MICROMIPS_GOT16:
5892
0
    case R_MICROMIPS_GOT_PAGE:
5893
0
    case R_MICROMIPS_GOT_DISP:
5894
0
    case R_MICROMIPS_GOT_LO16:
5895
0
    case R_MICROMIPS_CALL_LO16:
5896
0
      if (resolved_to_zero
5897
0
    && !bfd_link_relocatable (info)
5898
0
    && bfd_reloc_offset_in_range (howto, input_bfd, input_section,
5899
0
          relocation->r_offset)
5900
0
    && mips_elf_nullify_got_load (input_bfd, contents,
5901
0
          relocation, howto, true))
5902
0
  return bfd_reloc_continue;
5903
5904
      /* Fall through.  */
5905
0
    case R_MIPS_GOT_HI16:
5906
0
    case R_MIPS_CALL_HI16:
5907
0
    case R_MICROMIPS_GOT_HI16:
5908
0
    case R_MICROMIPS_CALL_HI16:
5909
0
      if (resolved_to_zero
5910
0
    && htab->use_absolute_zero
5911
0
    && bfd_link_pic (info))
5912
0
  {
5913
    /* Redirect to the special `__gnu_absolute_zero' symbol.  */
5914
0
    h = mips_elf_link_hash_lookup (htab, "__gnu_absolute_zero",
5915
0
           false, false, false);
5916
0
    BFD_ASSERT (h != NULL);
5917
0
  }
5918
0
      break;
5919
0
    }
5920
5921
0
  local_p = (h == NULL || mips_use_local_got_p (info, h));
5922
5923
0
  gp0 = _bfd_get_gp_value (input_bfd);
5924
0
  gp = _bfd_get_gp_value (abfd);
5925
0
  if (htab->got_info)
5926
0
    gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5927
5928
0
  if (gnu_local_gp_p)
5929
0
    symbol = gp;
5930
5931
  /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5932
     to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP.  The addend is applied by the
5933
     corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.  */
5934
0
  if (got_page_reloc_p (r_type) && !local_p)
5935
0
    {
5936
0
      r_type = (micromips_reloc_p (r_type)
5937
0
    ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
5938
0
      addend = 0;
5939
0
    }
5940
5941
  /* If we haven't already determined the GOT offset, and we're going
5942
     to need it, get it now.  */
5943
0
  switch (r_type)
5944
0
    {
5945
0
    case R_MIPS16_CALL16:
5946
0
    case R_MIPS16_GOT16:
5947
0
    case R_MIPS_CALL16:
5948
0
    case R_MIPS_GOT16:
5949
0
    case R_MIPS_GOT_DISP:
5950
0
    case R_MIPS_GOT_HI16:
5951
0
    case R_MIPS_CALL_HI16:
5952
0
    case R_MIPS_GOT_LO16:
5953
0
    case R_MIPS_CALL_LO16:
5954
0
    case R_MICROMIPS_CALL16:
5955
0
    case R_MICROMIPS_GOT16:
5956
0
    case R_MICROMIPS_GOT_DISP:
5957
0
    case R_MICROMIPS_GOT_HI16:
5958
0
    case R_MICROMIPS_CALL_HI16:
5959
0
    case R_MICROMIPS_GOT_LO16:
5960
0
    case R_MICROMIPS_CALL_LO16:
5961
0
    case R_MIPS_TLS_GD:
5962
0
    case R_MIPS_TLS_GOTTPREL:
5963
0
    case R_MIPS_TLS_LDM:
5964
0
    case R_MIPS16_TLS_GD:
5965
0
    case R_MIPS16_TLS_GOTTPREL:
5966
0
    case R_MIPS16_TLS_LDM:
5967
0
    case R_MICROMIPS_TLS_GD:
5968
0
    case R_MICROMIPS_TLS_GOTTPREL:
5969
0
    case R_MICROMIPS_TLS_LDM:
5970
      /* Find the index into the GOT where this value is located.  */
5971
0
      if (tls_ldm_reloc_p (r_type))
5972
0
  {
5973
0
    g = mips_elf_local_got_index (abfd, input_bfd, info,
5974
0
          0, 0, NULL, r_type);
5975
0
    if (g == MINUS_ONE)
5976
0
      return bfd_reloc_outofrange;
5977
0
  }
5978
0
      else if (!local_p)
5979
0
  {
5980
    /* On VxWorks, CALL relocations should refer to the .got.plt
5981
       entry, which is initialized to point at the PLT stub.  */
5982
0
    if (htab->root.target_os == is_vxworks
5983
0
        && (call_hi16_reloc_p (r_type)
5984
0
      || call_lo16_reloc_p (r_type)
5985
0
      || call16_reloc_p (r_type)))
5986
0
      {
5987
0
        BFD_ASSERT (addend == 0);
5988
0
        BFD_ASSERT (h->root.needs_plt);
5989
0
        g = mips_elf_gotplt_index (info, &h->root);
5990
0
      }
5991
0
    else
5992
0
      {
5993
0
        BFD_ASSERT (addend == 0);
5994
0
        g = mips_elf_global_got_index (abfd, info, input_bfd,
5995
0
               &h->root, r_type);
5996
0
        if (!TLS_RELOC_P (r_type)
5997
0
      && !elf_hash_table (info)->dynamic_sections_created)
5998
    /* This is a static link.  We must initialize the GOT entry.  */
5999
0
    MIPS_ELF_PUT_WORD (dynobj, symbol, htab->root.sgot->contents + g);
6000
0
      }
6001
0
  }
6002
0
      else if (htab->root.target_os != is_vxworks
6003
0
         && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
6004
  /* The calculation below does not involve "g".  */
6005
0
  break;
6006
0
      else
6007
0
  {
6008
0
    g = mips_elf_local_got_index (abfd, input_bfd, info,
6009
0
          symbol + addend, r_symndx, h, r_type);
6010
0
    if (g == MINUS_ONE)
6011
0
      return bfd_reloc_outofrange;
6012
0
  }
6013
6014
      /* Convert GOT indices to actual offsets.  */
6015
0
      g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
6016
0
      break;
6017
0
    }
6018
6019
  /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
6020
     symbols are resolved by the loader.  Add them to .rela.dyn.  */
6021
0
  if (h != NULL && is_gott_symbol (info, &h->root))
6022
0
    {
6023
0
      Elf_Internal_Rela outrel;
6024
0
      bfd_byte *loc;
6025
0
      asection *s;
6026
6027
0
      s = mips_elf_rel_dyn_section (info, false);
6028
0
      loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
6029
6030
0
      outrel.r_offset = (input_section->output_section->vma
6031
0
       + input_section->output_offset
6032
0
       + relocation->r_offset);
6033
0
      outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
6034
0
      outrel.r_addend = addend;
6035
0
      bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
6036
6037
      /* If we've written this relocation for a readonly section,
6038
   we need to set DF_TEXTREL again, so that we do not delete the
6039
   DT_TEXTREL tag.  */
6040
0
      if (MIPS_ELF_READONLY_SECTION (input_section))
6041
0
  info->flags |= DF_TEXTREL;
6042
6043
0
      *valuep = 0;
6044
0
      return bfd_reloc_ok;
6045
0
    }
6046
6047
  /* Figure out what kind of relocation is being performed.  */
6048
0
  switch (r_type)
6049
0
    {
6050
0
    case R_MIPS_NONE:
6051
0
      return bfd_reloc_continue;
6052
6053
0
    case R_MIPS_16:
6054
0
      if (howto->partial_inplace)
6055
0
  addend = _bfd_mips_elf_sign_extend (addend, 16);
6056
0
      value = symbol + addend;
6057
0
      overflowed_p = mips_elf_overflow_p (value, 16);
6058
0
      break;
6059
6060
0
    case R_MIPS_32:
6061
0
    case R_MIPS_REL32:
6062
0
    case R_MIPS_64:
6063
0
      if ((bfd_link_pic (info)
6064
0
     || (htab->root.dynamic_sections_created
6065
0
         && h != NULL
6066
0
         && h->root.def_dynamic
6067
0
         && !h->root.def_regular
6068
0
         && !h->has_static_relocs))
6069
0
    && r_symndx != STN_UNDEF
6070
0
    && (h == NULL
6071
0
        || h->root.root.type != bfd_link_hash_undefweak
6072
0
        || (ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
6073
0
      && !resolved_to_zero))
6074
0
    && (input_section->flags & SEC_ALLOC) != 0)
6075
0
  {
6076
    /* If we're creating a shared library, then we can't know
6077
       where the symbol will end up.  So, we create a relocation
6078
       record in the output, and leave the job up to the dynamic
6079
       linker.  We must do the same for executable references to
6080
       shared library symbols, unless we've decided to use copy
6081
       relocs or PLTs instead.  */
6082
0
    value = addend;
6083
0
    if (!mips_elf_create_dynamic_relocation (abfd,
6084
0
               info,
6085
0
               relocation,
6086
0
               h,
6087
0
               sec,
6088
0
               symbol,
6089
0
               &value,
6090
0
               input_section))
6091
0
      return bfd_reloc_undefined;
6092
0
  }
6093
0
      else
6094
0
  {
6095
0
    if (r_type != R_MIPS_REL32)
6096
0
      value = symbol + addend;
6097
0
    else
6098
0
      value = addend;
6099
0
  }
6100
0
      value &= howto->dst_mask;
6101
0
      break;
6102
6103
0
    case R_MIPS_PC32:
6104
0
      value = symbol + addend - p;
6105
0
      value &= howto->dst_mask;
6106
0
      break;
6107
6108
0
    case R_MIPS16_26:
6109
      /* The calculation for R_MIPS16_26 is just the same as for an
6110
   R_MIPS_26.  It's only the storage of the relocated field into
6111
   the output file that's different.  That's handled in
6112
   mips_elf_perform_relocation.  So, we just fall through to the
6113
   R_MIPS_26 case here.  */
6114
0
    case R_MIPS_26:
6115
0
    case R_MICROMIPS_26_S1:
6116
0
      {
6117
0
  unsigned int shift;
6118
6119
  /* Shift is 2, unusually, for microMIPS JALX.  */
6120
0
  shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
6121
6122
0
  if (howto->partial_inplace && !section_p)
6123
0
    value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
6124
0
  else
6125
0
    value = addend;
6126
0
  value += symbol;
6127
6128
  /* Make sure the target of a jump is suitably aligned.  Bit 0 must
6129
     be the correct ISA mode selector except for weak undefined
6130
     symbols.  */
6131
0
  if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6132
0
      && (*cross_mode_jump_p
6133
0
    ? (value & 3) != (r_type == R_MIPS_26)
6134
0
    : (value & ((1 << shift) - 1)) != (r_type != R_MIPS_26)))
6135
0
    return bfd_reloc_outofrange;
6136
6137
0
  value >>= shift;
6138
0
  if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6139
0
    overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
6140
0
  value &= howto->dst_mask;
6141
0
      }
6142
0
      break;
6143
6144
0
    case R_MIPS_TLS_DTPREL_HI16:
6145
0
    case R_MIPS16_TLS_DTPREL_HI16:
6146
0
    case R_MICROMIPS_TLS_DTPREL_HI16:
6147
0
      value = (mips_elf_high (addend + symbol - dtprel_base (info))
6148
0
         & howto->dst_mask);
6149
0
      break;
6150
6151
0
    case R_MIPS_TLS_DTPREL_LO16:
6152
0
    case R_MIPS_TLS_DTPREL32:
6153
0
    case R_MIPS_TLS_DTPREL64:
6154
0
    case R_MIPS16_TLS_DTPREL_LO16:
6155
0
    case R_MICROMIPS_TLS_DTPREL_LO16:
6156
0
      value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
6157
0
      break;
6158
6159
0
    case R_MIPS_TLS_TPREL_HI16:
6160
0
    case R_MIPS16_TLS_TPREL_HI16:
6161
0
    case R_MICROMIPS_TLS_TPREL_HI16:
6162
0
      value = (mips_elf_high (addend + symbol - tprel_base (info))
6163
0
         & howto->dst_mask);
6164
0
      break;
6165
6166
0
    case R_MIPS_TLS_TPREL_LO16:
6167
0
    case R_MIPS_TLS_TPREL32:
6168
0
    case R_MIPS_TLS_TPREL64:
6169
0
    case R_MIPS16_TLS_TPREL_LO16:
6170
0
    case R_MICROMIPS_TLS_TPREL_LO16:
6171
0
      value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
6172
0
      break;
6173
6174
0
    case R_MIPS_HI16:
6175
0
    case R_MIPS16_HI16:
6176
0
    case R_MICROMIPS_HI16:
6177
0
      if (!gp_disp_p)
6178
0
  {
6179
0
    value = mips_elf_high (addend + symbol);
6180
0
    value &= howto->dst_mask;
6181
0
  }
6182
0
      else
6183
0
  {
6184
    /* For MIPS16 ABI code we generate this sequence
6185
    0: li      $v0,%hi(_gp_disp)
6186
    4: addiupc $v1,%lo(_gp_disp)
6187
    8: sll     $v0,16
6188
         12: addu    $v0,$v1
6189
         14: move    $gp,$v0
6190
       So the offsets of hi and lo relocs are the same, but the
6191
       base $pc is that used by the ADDIUPC instruction at $t9 + 4.
6192
       ADDIUPC clears the low two bits of the instruction address,
6193
       so the base is ($t9 + 4) & ~3.  */
6194
0
    if (r_type == R_MIPS16_HI16)
6195
0
      value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
6196
    /* The microMIPS .cpload sequence uses the same assembly
6197
       instructions as the traditional psABI version, but the
6198
       incoming $t9 has the low bit set.  */
6199
0
    else if (r_type == R_MICROMIPS_HI16)
6200
0
      value = mips_elf_high (addend + gp - p - 1);
6201
0
    else
6202
0
      value = mips_elf_high (addend + gp - p);
6203
0
  }
6204
0
      break;
6205
6206
0
    case R_MIPS_LO16:
6207
0
    case R_MIPS16_LO16:
6208
0
    case R_MICROMIPS_LO16:
6209
0
    case R_MICROMIPS_HI0_LO16:
6210
0
      if (!gp_disp_p)
6211
0
  value = (symbol + addend) & howto->dst_mask;
6212
0
      else
6213
0
  {
6214
    /* See the comment for R_MIPS16_HI16 above for the reason
6215
       for this conditional.  */
6216
0
    if (r_type == R_MIPS16_LO16)
6217
0
      value = addend + gp - (p & ~(bfd_vma) 0x3);
6218
0
    else if (r_type == R_MICROMIPS_LO16
6219
0
       || r_type == R_MICROMIPS_HI0_LO16)
6220
0
      value = addend + gp - p + 3;
6221
0
    else
6222
0
      value = addend + gp - p + 4;
6223
    /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
6224
       for overflow.  But, on, say, IRIX5, relocations against
6225
       _gp_disp are normally generated from the .cpload
6226
       pseudo-op.  It generates code that normally looks like
6227
       this:
6228
6229
         lui    $gp,%hi(_gp_disp)
6230
         addiu  $gp,$gp,%lo(_gp_disp)
6231
         addu   $gp,$gp,$t9
6232
6233
       Here $t9 holds the address of the function being called,
6234
       as required by the MIPS ELF ABI.  The R_MIPS_LO16
6235
       relocation can easily overflow in this situation, but the
6236
       R_MIPS_HI16 relocation will handle the overflow.
6237
       Therefore, we consider this a bug in the MIPS ABI, and do
6238
       not check for overflow here.  */
6239
0
  }
6240
0
      break;
6241
6242
0
    case R_MIPS_LITERAL:
6243
0
    case R_MICROMIPS_LITERAL:
6244
      /* Because we don't merge literal sections, we can handle this
6245
   just like R_MIPS_GPREL16.  In the long run, we should merge
6246
   shared literals, and then we will need to additional work
6247
   here.  */
6248
6249
      /* Fall through.  */
6250
6251
0
    case R_MIPS16_GPREL:
6252
      /* The R_MIPS16_GPREL performs the same calculation as
6253
   R_MIPS_GPREL16, but stores the relocated bits in a different
6254
   order.  We don't need to do anything special here; the
6255
   differences are handled in mips_elf_perform_relocation.  */
6256
0
    case R_MIPS_GPREL16:
6257
0
    case R_MICROMIPS_GPREL7_S2:
6258
0
    case R_MICROMIPS_GPREL16:
6259
0
      {
6260
0
  int bits = howto->bitsize + howto->rightshift;
6261
  /* Only sign-extend the addend if it was extracted from the
6262
     instruction.  If the addend was separate, leave it alone,
6263
     otherwise we may lose significant bits.  */
6264
0
  if (howto->partial_inplace)
6265
0
    addend = _bfd_mips_elf_sign_extend (addend, bits);
6266
0
  value = symbol + addend - gp;
6267
  /* If the symbol was local, any earlier relocatable links will
6268
     have adjusted its addend with the gp offset, so compensate
6269
     for that now.  Don't do it for symbols forced local in this
6270
     link, though, since they won't have had the gp offset applied
6271
     to them before.  */
6272
0
  if (was_local_p)
6273
0
    value += gp0;
6274
0
  if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6275
0
    overflowed_p = mips_elf_overflow_p (value, bits);
6276
0
      }
6277
0
      break;
6278
6279
0
    case R_MIPS16_GOT16:
6280
0
    case R_MIPS16_CALL16:
6281
0
    case R_MIPS_GOT16:
6282
0
    case R_MIPS_CALL16:
6283
0
    case R_MICROMIPS_GOT16:
6284
0
    case R_MICROMIPS_CALL16:
6285
      /* VxWorks does not have separate local and global semantics for
6286
   R_MIPS*_GOT16; every relocation evaluates to "G".  */
6287
0
      if (htab->root.target_os != is_vxworks && local_p)
6288
0
  {
6289
0
    value = mips_elf_got16_entry (abfd, input_bfd, info,
6290
0
          symbol + addend, !was_local_p);
6291
0
    if (value == MINUS_ONE)
6292
0
      return bfd_reloc_outofrange;
6293
0
    value
6294
0
      = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6295
0
    overflowed_p = mips_elf_overflow_p (value, 16);
6296
0
    break;
6297
0
  }
6298
6299
      /* Fall through.  */
6300
6301
0
    case R_MIPS_TLS_GD:
6302
0
    case R_MIPS_TLS_GOTTPREL:
6303
0
    case R_MIPS_TLS_LDM:
6304
0
    case R_MIPS_GOT_DISP:
6305
0
    case R_MIPS16_TLS_GD:
6306
0
    case R_MIPS16_TLS_GOTTPREL:
6307
0
    case R_MIPS16_TLS_LDM:
6308
0
    case R_MICROMIPS_TLS_GD:
6309
0
    case R_MICROMIPS_TLS_GOTTPREL:
6310
0
    case R_MICROMIPS_TLS_LDM:
6311
0
    case R_MICROMIPS_GOT_DISP:
6312
0
      value = g;
6313
0
      overflowed_p = mips_elf_overflow_p (value, 16);
6314
0
      break;
6315
6316
0
    case R_MIPS_GPREL32:
6317
0
      value = (addend + symbol + gp0 - gp);
6318
0
      if (!save_addend)
6319
0
  value &= howto->dst_mask;
6320
0
      break;
6321
6322
0
    case R_MIPS_PC16:
6323
0
    case R_MIPS_GNU_REL16_S2:
6324
0
      if (howto->partial_inplace)
6325
0
  addend = _bfd_mips_elf_sign_extend (addend, 18);
6326
6327
      /* No need to exclude weak undefined symbols here as they resolve
6328
   to 0 and never set `*cross_mode_jump_p', so this alignment check
6329
   will never trigger for them.  */
6330
0
      if (*cross_mode_jump_p
6331
0
    ? ((symbol + addend) & 3) != 1
6332
0
    : ((symbol + addend) & 3) != 0)
6333
0
  return bfd_reloc_outofrange;
6334
6335
0
      value = symbol + addend - p;
6336
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6337
0
  overflowed_p = mips_elf_overflow_p (value, 18);
6338
0
      value >>= howto->rightshift;
6339
0
      value &= howto->dst_mask;
6340
0
      break;
6341
6342
0
    case R_MIPS16_PC16_S1:
6343
0
      if (howto->partial_inplace)
6344
0
  addend = _bfd_mips_elf_sign_extend (addend, 17);
6345
6346
0
      if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6347
0
    && (*cross_mode_jump_p
6348
0
        ? ((symbol + addend) & 3) != 0
6349
0
        : ((symbol + addend) & 1) == 0))
6350
0
  return bfd_reloc_outofrange;
6351
6352
0
      value = symbol + addend - p;
6353
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6354
0
  overflowed_p = mips_elf_overflow_p (value, 17);
6355
0
      value >>= howto->rightshift;
6356
0
      value &= howto->dst_mask;
6357
0
      break;
6358
6359
0
    case R_MIPS_PC21_S2:
6360
0
      if (howto->partial_inplace)
6361
0
  addend = _bfd_mips_elf_sign_extend (addend, 23);
6362
6363
0
      if ((symbol + addend) & 3)
6364
0
  return bfd_reloc_outofrange;
6365
6366
0
      value = symbol + addend - p;
6367
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6368
0
  overflowed_p = mips_elf_overflow_p (value, 23);
6369
0
      value >>= howto->rightshift;
6370
0
      value &= howto->dst_mask;
6371
0
      break;
6372
6373
0
    case R_MIPS_PC26_S2:
6374
0
      if (howto->partial_inplace)
6375
0
  addend = _bfd_mips_elf_sign_extend (addend, 28);
6376
6377
0
      if ((symbol + addend) & 3)
6378
0
  return bfd_reloc_outofrange;
6379
6380
0
      value = symbol + addend - p;
6381
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6382
0
  overflowed_p = mips_elf_overflow_p (value, 28);
6383
0
      value >>= howto->rightshift;
6384
0
      value &= howto->dst_mask;
6385
0
      break;
6386
6387
0
    case R_MIPS_PC18_S3:
6388
0
      if (howto->partial_inplace)
6389
0
  addend = _bfd_mips_elf_sign_extend (addend, 21);
6390
6391
0
      if ((symbol + addend) & 7)
6392
0
  return bfd_reloc_outofrange;
6393
6394
0
      value = symbol + addend - ((p | 7) ^ 7);
6395
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6396
0
  overflowed_p = mips_elf_overflow_p (value, 21);
6397
0
      value >>= howto->rightshift;
6398
0
      value &= howto->dst_mask;
6399
0
      break;
6400
6401
0
    case R_MIPS_PC19_S2:
6402
0
      if (howto->partial_inplace)
6403
0
  addend = _bfd_mips_elf_sign_extend (addend, 21);
6404
6405
0
      if ((symbol + addend) & 3)
6406
0
  return bfd_reloc_outofrange;
6407
6408
0
      value = symbol + addend - p;
6409
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6410
0
  overflowed_p = mips_elf_overflow_p (value, 21);
6411
0
      value >>= howto->rightshift;
6412
0
      value &= howto->dst_mask;
6413
0
      break;
6414
6415
0
    case R_MIPS_PCHI16:
6416
0
      value = mips_elf_high (symbol + addend - p);
6417
0
      value &= howto->dst_mask;
6418
0
      break;
6419
6420
0
    case R_MIPS_PCLO16:
6421
0
      if (howto->partial_inplace)
6422
0
  addend = _bfd_mips_elf_sign_extend (addend, 16);
6423
0
      value = symbol + addend - p;
6424
0
      value &= howto->dst_mask;
6425
0
      break;
6426
6427
0
    case R_MICROMIPS_PC7_S1:
6428
0
      if (howto->partial_inplace)
6429
0
  addend = _bfd_mips_elf_sign_extend (addend, 8);
6430
6431
0
      if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6432
0
    && (*cross_mode_jump_p
6433
0
        ? ((symbol + addend + 2) & 3) != 0
6434
0
        : ((symbol + addend + 2) & 1) == 0))
6435
0
  return bfd_reloc_outofrange;
6436
6437
0
      value = symbol + addend - p;
6438
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6439
0
  overflowed_p = mips_elf_overflow_p (value, 8);
6440
0
      value >>= howto->rightshift;
6441
0
      value &= howto->dst_mask;
6442
0
      break;
6443
6444
0
    case R_MICROMIPS_PC10_S1:
6445
0
      if (howto->partial_inplace)
6446
0
  addend = _bfd_mips_elf_sign_extend (addend, 11);
6447
6448
0
      if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6449
0
    && (*cross_mode_jump_p
6450
0
        ? ((symbol + addend + 2) & 3) != 0
6451
0
        : ((symbol + addend + 2) & 1) == 0))
6452
0
  return bfd_reloc_outofrange;
6453
6454
0
      value = symbol + addend - p;
6455
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6456
0
  overflowed_p = mips_elf_overflow_p (value, 11);
6457
0
      value >>= howto->rightshift;
6458
0
      value &= howto->dst_mask;
6459
0
      break;
6460
6461
0
    case R_MICROMIPS_PC16_S1:
6462
0
      if (howto->partial_inplace)
6463
0
  addend = _bfd_mips_elf_sign_extend (addend, 17);
6464
6465
0
      if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6466
0
    && (*cross_mode_jump_p
6467
0
        ? ((symbol + addend) & 3) != 0
6468
0
        : ((symbol + addend) & 1) == 0))
6469
0
  return bfd_reloc_outofrange;
6470
6471
0
      value = symbol + addend - p;
6472
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6473
0
  overflowed_p = mips_elf_overflow_p (value, 17);
6474
0
      value >>= howto->rightshift;
6475
0
      value &= howto->dst_mask;
6476
0
      break;
6477
6478
0
    case R_MICROMIPS_PC23_S2:
6479
0
      if (howto->partial_inplace)
6480
0
  addend = _bfd_mips_elf_sign_extend (addend, 25);
6481
0
      value = symbol + addend - ((p | 3) ^ 3);
6482
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6483
0
  overflowed_p = mips_elf_overflow_p (value, 25);
6484
0
      value >>= howto->rightshift;
6485
0
      value &= howto->dst_mask;
6486
0
      break;
6487
6488
0
    case R_MIPS_GOT_HI16:
6489
0
    case R_MIPS_CALL_HI16:
6490
0
    case R_MICROMIPS_GOT_HI16:
6491
0
    case R_MICROMIPS_CALL_HI16:
6492
      /* We're allowed to handle these two relocations identically.
6493
   The dynamic linker is allowed to handle the CALL relocations
6494
   differently by creating a lazy evaluation stub.  */
6495
0
      value = g;
6496
0
      value = mips_elf_high (value);
6497
0
      value &= howto->dst_mask;
6498
0
      break;
6499
6500
0
    case R_MIPS_GOT_LO16:
6501
0
    case R_MIPS_CALL_LO16:
6502
0
    case R_MICROMIPS_GOT_LO16:
6503
0
    case R_MICROMIPS_CALL_LO16:
6504
0
      value = g & howto->dst_mask;
6505
0
      break;
6506
6507
0
    case R_MIPS_GOT_PAGE:
6508
0
    case R_MICROMIPS_GOT_PAGE:
6509
0
      value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
6510
0
      if (value == MINUS_ONE)
6511
0
  return bfd_reloc_outofrange;
6512
0
      value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6513
0
      overflowed_p = mips_elf_overflow_p (value, 16);
6514
0
      break;
6515
6516
0
    case R_MIPS_GOT_OFST:
6517
0
    case R_MICROMIPS_GOT_OFST:
6518
0
      if (local_p)
6519
0
  mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
6520
0
      else
6521
0
  value = addend;
6522
0
      overflowed_p = mips_elf_overflow_p (value, 16);
6523
0
      break;
6524
6525
0
    case R_MIPS_SUB:
6526
0
    case R_MICROMIPS_SUB:
6527
0
      value = symbol - addend;
6528
0
      value &= howto->dst_mask;
6529
0
      break;
6530
6531
0
    case R_MIPS_HIGHER:
6532
0
    case R_MICROMIPS_HIGHER:
6533
0
      value = mips_elf_higher (addend + symbol);
6534
0
      value &= howto->dst_mask;
6535
0
      break;
6536
6537
0
    case R_MIPS_HIGHEST:
6538
0
    case R_MICROMIPS_HIGHEST:
6539
0
      value = mips_elf_highest (addend + symbol);
6540
0
      value &= howto->dst_mask;
6541
0
      break;
6542
6543
0
    case R_MIPS_SCN_DISP:
6544
0
    case R_MICROMIPS_SCN_DISP:
6545
0
      value = symbol + addend - sec->output_offset;
6546
0
      value &= howto->dst_mask;
6547
0
      break;
6548
6549
0
    case R_MIPS_JALR:
6550
0
    case R_MICROMIPS_JALR:
6551
      /* This relocation is only a hint.  In some cases, we optimize
6552
   it into a bal instruction.  But we don't try to optimize
6553
   when the symbol does not resolve locally.  */
6554
0
      if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
6555
0
  return bfd_reloc_continue;
6556
      /* We can't optimize cross-mode jumps either.  */
6557
0
      if (*cross_mode_jump_p)
6558
0
  return bfd_reloc_continue;
6559
0
      value = symbol + addend;
6560
      /* Neither we can non-instruction-aligned targets.  */
6561
0
      if (r_type == R_MIPS_JALR ? (value & 3) != 0 : (value & 1) == 0)
6562
0
  return bfd_reloc_continue;
6563
0
      break;
6564
6565
0
    case R_MIPS_PJUMP:
6566
0
    case R_MIPS_GNU_VTINHERIT:
6567
0
    case R_MIPS_GNU_VTENTRY:
6568
      /* We don't do anything with these at present.  */
6569
0
      return bfd_reloc_continue;
6570
6571
0
    default:
6572
      /* An unrecognized relocation type.  */
6573
0
      return bfd_reloc_notsupported;
6574
0
    }
6575
6576
  /* Store the VALUE for our caller.  */
6577
0
  *valuep = value;
6578
0
  return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6579
0
}
6580
6581
/* It has been determined that the result of the RELOCATION is the
6582
   VALUE.  Use HOWTO to place VALUE into the output file at the
6583
   appropriate position.  The SECTION is the section to which the
6584
   relocation applies.
6585
   CROSS_MODE_JUMP_P is true if the relocation field
6586
   is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
6587
6588
   Returns FALSE if anything goes wrong.  */
6589
6590
static bool
6591
mips_elf_perform_relocation (struct bfd_link_info *info,
6592
           reloc_howto_type *howto,
6593
           const Elf_Internal_Rela *relocation,
6594
           bfd_vma value, bfd *input_bfd,
6595
           asection *input_section, bfd_byte *contents,
6596
           bool cross_mode_jump_p)
6597
0
{
6598
0
  bfd_vma x;
6599
0
  bfd_byte *location;
6600
0
  int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6601
6602
  /* Figure out where the relocation is occurring.  */
6603
0
  location = contents + relocation->r_offset;
6604
6605
0
  _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
6606
6607
  /* Obtain the current value.  */
6608
0
  x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6609
6610
  /* Clear the field we are setting.  */
6611
0
  x &= ~howto->dst_mask;
6612
6613
  /* Set the field.  */
6614
0
  x |= (value & howto->dst_mask);
6615
6616
  /* Detect incorrect JALX usage.  If required, turn JAL or BAL into JALX.  */
6617
0
  if (!cross_mode_jump_p && jal_reloc_p (r_type))
6618
0
    {
6619
0
      bfd_vma opcode = x >> 26;
6620
6621
0
      if (r_type == R_MIPS16_26 ? opcode == 0x7
6622
0
    : r_type == R_MICROMIPS_26_S1 ? opcode == 0x3c
6623
0
    : opcode == 0x1d)
6624
0
  {
6625
0
    info->callbacks->einfo
6626
0
      (_("%X%H: unsupported JALX to the same ISA mode\n"),
6627
0
       input_bfd, input_section, relocation->r_offset);
6628
0
    return true;
6629
0
  }
6630
0
    }
6631
0
  if (cross_mode_jump_p && jal_reloc_p (r_type))
6632
0
    {
6633
0
      bool ok;
6634
0
      bfd_vma opcode = x >> 26;
6635
0
      bfd_vma jalx_opcode;
6636
6637
      /* Check to see if the opcode is already JAL or JALX.  */
6638
0
      if (r_type == R_MIPS16_26)
6639
0
  {
6640
0
    ok = ((opcode == 0x6) || (opcode == 0x7));
6641
0
    jalx_opcode = 0x7;
6642
0
  }
6643
0
      else if (r_type == R_MICROMIPS_26_S1)
6644
0
  {
6645
0
    ok = ((opcode == 0x3d) || (opcode == 0x3c));
6646
0
    jalx_opcode = 0x3c;
6647
0
  }
6648
0
      else
6649
0
  {
6650
0
    ok = ((opcode == 0x3) || (opcode == 0x1d));
6651
0
    jalx_opcode = 0x1d;
6652
0
  }
6653
6654
      /* If the opcode is not JAL or JALX, there's a problem.  We cannot
6655
   convert J or JALS to JALX.  */
6656
0
      if (!ok)
6657
0
  {
6658
0
    info->callbacks->einfo
6659
0
      (_("%X%H: unsupported jump between ISA modes; "
6660
0
         "consider recompiling with interlinking enabled\n"),
6661
0
       input_bfd, input_section, relocation->r_offset);
6662
0
    return true;
6663
0
  }
6664
6665
      /* Make this the JALX opcode.  */
6666
0
      x = (x & ~(0x3fu << 26)) | (jalx_opcode << 26);
6667
0
    }
6668
0
  else if (cross_mode_jump_p && b_reloc_p (r_type))
6669
0
    {
6670
0
      bool ok = false;
6671
0
      bfd_vma opcode = x >> 16;
6672
0
      bfd_vma jalx_opcode = 0;
6673
0
      bfd_vma sign_bit = 0;
6674
0
      bfd_vma addr;
6675
0
      bfd_vma dest;
6676
6677
0
      if (r_type == R_MICROMIPS_PC16_S1)
6678
0
  {
6679
0
    ok = opcode == 0x4060;
6680
0
    jalx_opcode = 0x3c;
6681
0
    sign_bit = 0x10000;
6682
0
    value <<= 1;
6683
0
  }
6684
0
      else if (r_type == R_MIPS_PC16 || r_type == R_MIPS_GNU_REL16_S2)
6685
0
  {
6686
0
    ok = opcode == 0x411;
6687
0
    jalx_opcode = 0x1d;
6688
0
    sign_bit = 0x20000;
6689
0
    value <<= 2;
6690
0
  }
6691
6692
0
      if (ok && !bfd_link_pic (info))
6693
0
  {
6694
0
    addr = (input_section->output_section->vma
6695
0
      + input_section->output_offset
6696
0
      + relocation->r_offset
6697
0
      + 4);
6698
0
    dest = (addr
6699
0
      + (((value & ((sign_bit << 1) - 1)) ^ sign_bit) - sign_bit));
6700
6701
0
    if ((addr >> 28) << 28 != (dest >> 28) << 28)
6702
0
      {
6703
0
        info->callbacks->einfo
6704
0
    (_("%X%H: cannot convert branch between ISA modes "
6705
0
       "to JALX: relocation out of range\n"),
6706
0
     input_bfd, input_section, relocation->r_offset);
6707
0
        return true;
6708
0
      }
6709
6710
    /* Make this the JALX opcode.  */
6711
0
    x = ((dest >> 2) & 0x3ffffff) | jalx_opcode << 26;
6712
0
  }
6713
0
      else if (!mips_elf_hash_table (info)->ignore_branch_isa)
6714
0
  {
6715
0
    info->callbacks->einfo
6716
0
      (_("%X%H: unsupported branch between ISA modes\n"),
6717
0
       input_bfd, input_section, relocation->r_offset);
6718
0
    return true;
6719
0
  }
6720
0
    }
6721
6722
  /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6723
     range.  */
6724
0
  if (!bfd_link_relocatable (info)
6725
0
      && !cross_mode_jump_p
6726
0
      && ((JAL_TO_BAL_P (input_bfd)
6727
0
     && r_type == R_MIPS_26
6728
0
     && (x >> 26) == 0x3)     /* jal addr */
6729
0
    || (JALR_TO_BAL_P (input_bfd)
6730
0
        && r_type == R_MIPS_JALR
6731
0
        && x == 0x0320f809)   /* jalr t9 */
6732
0
    || (JR_TO_B_P (input_bfd)
6733
0
        && r_type == R_MIPS_JALR
6734
0
        && (x & ~1) == 0x03200008))) /* jr t9 / jalr zero, t9 */
6735
0
    {
6736
0
      bfd_vma addr;
6737
0
      bfd_vma dest;
6738
0
      bfd_signed_vma off;
6739
6740
0
      addr = (input_section->output_section->vma
6741
0
        + input_section->output_offset
6742
0
        + relocation->r_offset
6743
0
        + 4);
6744
0
      if (r_type == R_MIPS_26)
6745
0
  dest = (value << 2) | ((addr >> 28) << 28);
6746
0
      else
6747
0
  dest = value;
6748
0
      off = dest - addr;
6749
0
      if (off <= 0x1ffff && off >= -0x20000)
6750
0
  {
6751
0
    if ((x & ~1) == 0x03200008)   /* jr t9 / jalr zero, t9 */
6752
0
      x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff);   /* b addr */
6753
0
    else
6754
0
      x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff);   /* bal addr */
6755
0
  }
6756
0
    }
6757
6758
  /* Put the value into the output.  */
6759
0
  mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
6760
6761
0
  _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !bfd_link_relocatable (info),
6762
0
             location);
6763
6764
0
  return true;
6765
0
}
6766

6767
/* Create a rel.dyn relocation for the dynamic linker to resolve.  REL
6768
   is the original relocation, which is now being transformed into a
6769
   dynamic relocation.  The ADDENDP is adjusted if necessary; the
6770
   caller should store the result in place of the original addend.  */
6771
6772
static bool
6773
mips_elf_create_dynamic_relocation (bfd *output_bfd,
6774
            struct bfd_link_info *info,
6775
            const Elf_Internal_Rela *rel,
6776
            struct mips_elf_link_hash_entry *h,
6777
            asection *sec, bfd_vma symbol,
6778
            bfd_vma *addendp, asection *input_section)
6779
0
{
6780
0
  Elf_Internal_Rela outrel[3];
6781
0
  asection *sreloc;
6782
0
  bfd *dynobj;
6783
0
  int r_type;
6784
0
  long indx;
6785
0
  bool defined_p;
6786
0
  struct mips_elf_link_hash_table *htab;
6787
6788
0
  htab = mips_elf_hash_table (info);
6789
0
  BFD_ASSERT (htab != NULL);
6790
6791
0
  r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6792
0
  dynobj = elf_hash_table (info)->dynobj;
6793
0
  sreloc = mips_elf_rel_dyn_section (info, false);
6794
0
  BFD_ASSERT (sreloc != NULL);
6795
0
  BFD_ASSERT (sreloc->contents != NULL);
6796
0
  BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
6797
0
        < sreloc->size);
6798
6799
0
  outrel[0].r_offset =
6800
0
    _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
6801
0
  if (ABI_64_P (output_bfd))
6802
0
    {
6803
0
      outrel[1].r_offset =
6804
0
  _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6805
0
      outrel[2].r_offset =
6806
0
  _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6807
0
    }
6808
6809
0
  if (outrel[0].r_offset == MINUS_ONE)
6810
    /* The relocation field has been deleted.  */
6811
0
    return true;
6812
6813
0
  if (outrel[0].r_offset == MINUS_TWO)
6814
0
    {
6815
      /* The relocation field has been converted into a relative value of
6816
   some sort.  Functions like _bfd_elf_write_section_eh_frame expect
6817
   the field to be fully relocated, so add in the symbol's value.  */
6818
0
      *addendp += symbol;
6819
0
      return true;
6820
0
    }
6821
6822
  /* We must now calculate the dynamic symbol table index to use
6823
     in the relocation.  */
6824
0
  if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
6825
0
    {
6826
0
      BFD_ASSERT (htab->root.target_os == is_vxworks
6827
0
      || h->global_got_area != GGA_NONE);
6828
0
      indx = h->root.dynindx;
6829
0
      if (SGI_COMPAT (output_bfd))
6830
0
  defined_p = h->root.def_regular;
6831
0
      else
6832
  /* ??? glibc's ld.so just adds the final GOT entry to the
6833
     relocation field.  It therefore treats relocs against
6834
     defined symbols in the same way as relocs against
6835
     undefined symbols.  */
6836
0
  defined_p = false;
6837
0
    }
6838
0
  else
6839
0
    {
6840
0
      if (sec != NULL && bfd_is_abs_section (sec))
6841
0
  indx = 0;
6842
0
      else if (sec == NULL || sec->owner == NULL)
6843
0
  {
6844
0
    BFD_ASSERT (0);
6845
0
    bfd_set_error (bfd_error_bad_value);
6846
0
    return false;
6847
0
  }
6848
0
      else
6849
0
  {
6850
0
    indx = elf_section_data (sec->output_section)->dynindx;
6851
0
    if (indx == 0)
6852
0
      {
6853
0
        asection *osec = htab->root.text_index_section;
6854
0
        indx = elf_section_data (osec)->dynindx;
6855
0
      }
6856
0
    if (indx == 0)
6857
0
      abort ();
6858
0
  }
6859
6860
      /* Instead of generating a relocation using the section
6861
   symbol, we may as well make it a fully relative
6862
   relocation.  We want to avoid generating relocations to
6863
   local symbols because we used to generate them
6864
   incorrectly, without adding the original symbol value,
6865
   which is mandated by the ABI for section symbols.  In
6866
   order to give dynamic loaders and applications time to
6867
   phase out the incorrect use, we refrain from emitting
6868
   section-relative relocations.  It's not like they're
6869
   useful, after all.  This should be a bit more efficient
6870
   as well.  */
6871
      /* ??? Although this behavior is compatible with glibc's ld.so,
6872
   the ABI says that relocations against STN_UNDEF should have
6873
   a symbol value of 0.  Irix rld honors this, so relocations
6874
   against STN_UNDEF have no effect.  */
6875
0
      if (!SGI_COMPAT (output_bfd))
6876
0
  indx = 0;
6877
0
      defined_p = true;
6878
0
    }
6879
6880
  /* If the relocation was previously an absolute relocation and
6881
     this symbol will not be referred to by the relocation, we must
6882
     adjust it by the value we give it in the dynamic symbol table.
6883
     Otherwise leave the job up to the dynamic linker.  */
6884
0
  if (defined_p && r_type != R_MIPS_REL32)
6885
0
    *addendp += symbol;
6886
6887
0
  if (htab->root.target_os == is_vxworks)
6888
    /* VxWorks uses non-relative relocations for this.  */
6889
0
    outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6890
0
  else
6891
    /* The relocation is always an REL32 relocation because we don't
6892
       know where the shared library will wind up at load-time.  */
6893
0
    outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6894
0
           R_MIPS_REL32);
6895
6896
  /* For strict adherence to the ABI specification, we should
6897
     generate a R_MIPS_64 relocation record by itself before the
6898
     _REL32/_64 record as well, such that the addend is read in as
6899
     a 64-bit value (REL32 is a 32-bit relocation, after all).
6900
     However, since none of the existing ELF64 MIPS dynamic
6901
     loaders seems to care, we don't waste space with these
6902
     artificial relocations.  If this turns out to not be true,
6903
     mips_elf_allocate_dynamic_relocation() should be tweaked so
6904
     as to make room for a pair of dynamic relocations per
6905
     invocation if ABI_64_P, and here we should generate an
6906
     additional relocation record with R_MIPS_64 by itself for a
6907
     NULL symbol before this relocation record.  */
6908
0
  outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6909
0
         ABI_64_P (output_bfd)
6910
0
         ? R_MIPS_64
6911
0
         : R_MIPS_NONE);
6912
0
  outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6913
6914
  /* Adjust the output offset of the relocation to reference the
6915
     correct location in the output file.  */
6916
0
  outrel[0].r_offset += (input_section->output_section->vma
6917
0
       + input_section->output_offset);
6918
0
  outrel[1].r_offset += (input_section->output_section->vma
6919
0
       + input_section->output_offset);
6920
0
  outrel[2].r_offset += (input_section->output_section->vma
6921
0
       + input_section->output_offset);
6922
6923
  /* Put the relocation back out.  We have to use the special
6924
     relocation outputter in the 64-bit case since the 64-bit
6925
     relocation format is non-standard.  */
6926
0
  if (ABI_64_P (output_bfd))
6927
0
    {
6928
0
      (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6929
0
  (output_bfd, &outrel[0],
6930
0
   (sreloc->contents
6931
0
    + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6932
0
    }
6933
0
  else if (htab->root.target_os == is_vxworks)
6934
0
    {
6935
      /* VxWorks uses RELA rather than REL dynamic relocations.  */
6936
0
      outrel[0].r_addend = *addendp;
6937
0
      bfd_elf32_swap_reloca_out
6938
0
  (output_bfd, &outrel[0],
6939
0
   (sreloc->contents
6940
0
    + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6941
0
    }
6942
0
  else
6943
0
    bfd_elf32_swap_reloc_out
6944
0
      (output_bfd, &outrel[0],
6945
0
       (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
6946
6947
  /* We've now added another relocation.  */
6948
0
  ++sreloc->reloc_count;
6949
6950
  /* Make sure the output section is writable.  The dynamic linker
6951
     will be writing to it.  */
6952
0
  elf_section_data (input_section->output_section)->this_hdr.sh_flags
6953
0
    |= SHF_WRITE;
6954
6955
  /* On IRIX5, make an entry of compact relocation info.  */
6956
0
  if (IRIX_COMPAT (output_bfd) == ict_irix5)
6957
0
    {
6958
0
      asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
6959
0
      bfd_byte *cr;
6960
6961
0
      if (scpt)
6962
0
  {
6963
0
    Elf32_crinfo cptrel;
6964
6965
0
    mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6966
0
    cptrel.vaddr = (rel->r_offset
6967
0
        + input_section->output_section->vma
6968
0
        + input_section->output_offset);
6969
0
    if (r_type == R_MIPS_REL32)
6970
0
      mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6971
0
    else
6972
0
      mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6973
0
    mips_elf_set_cr_dist2to (cptrel, 0);
6974
0
    cptrel.konst = *addendp;
6975
6976
0
    cr = (scpt->contents
6977
0
    + sizeof (Elf32_External_compact_rel));
6978
0
    mips_elf_set_cr_relvaddr (cptrel, 0);
6979
0
    bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6980
0
             ((Elf32_External_crinfo *) cr
6981
0
              + scpt->reloc_count));
6982
0
    ++scpt->reloc_count;
6983
0
  }
6984
0
    }
6985
6986
  /* If we've written this relocation for a readonly section,
6987
     we need to set DF_TEXTREL again, so that we do not delete the
6988
     DT_TEXTREL tag.  */
6989
0
  if (MIPS_ELF_READONLY_SECTION (input_section))
6990
0
    info->flags |= DF_TEXTREL;
6991
6992
0
  return true;
6993
0
}
6994

6995
/* Return the MACH for a MIPS e_flags value.  */
6996
6997
unsigned long
6998
_bfd_elf_mips_mach (flagword flags)
6999
86.9k
{
7000
86.9k
  switch (flags & EF_MIPS_MACH)
7001
86.9k
    {
7002
16
    case EF_MIPS_MACH_3900:
7003
16
      return bfd_mach_mips3900;
7004
7005
0
    case EF_MIPS_MACH_4010:
7006
0
      return bfd_mach_mips4010;
7007
7008
11
    case EF_MIPS_MACH_ALLEGREX:
7009
11
      return bfd_mach_mips_allegrex;
7010
7011
3
    case EF_MIPS_MACH_4100:
7012
3
      return bfd_mach_mips4100;
7013
7014
7
    case EF_MIPS_MACH_4111:
7015
7
      return bfd_mach_mips4111;
7016
7017
13
    case EF_MIPS_MACH_4120:
7018
13
      return bfd_mach_mips4120;
7019
7020
87
    case EF_MIPS_MACH_4650:
7021
87
      return bfd_mach_mips4650;
7022
7023
0
    case EF_MIPS_MACH_5400:
7024
0
      return bfd_mach_mips5400;
7025
7026
0
    case EF_MIPS_MACH_5500:
7027
0
      return bfd_mach_mips5500;
7028
7029
6
    case EF_MIPS_MACH_5900:
7030
6
      return bfd_mach_mips5900;
7031
7032
4.18k
    case EF_MIPS_MACH_9000:
7033
4.18k
      return bfd_mach_mips9000;
7034
7035
9
    case EF_MIPS_MACH_SB1:
7036
9
      return bfd_mach_mips_sb1;
7037
7038
16
    case EF_MIPS_MACH_LS2E:
7039
16
      return bfd_mach_mips_loongson_2e;
7040
7041
10
    case EF_MIPS_MACH_LS2F:
7042
10
      return bfd_mach_mips_loongson_2f;
7043
7044
4
    case EF_MIPS_MACH_GS464:
7045
4
      return bfd_mach_mips_gs464;
7046
7047
0
    case EF_MIPS_MACH_GS464E:
7048
0
      return bfd_mach_mips_gs464e;
7049
7050
14
    case EF_MIPS_MACH_GS264E:
7051
14
      return bfd_mach_mips_gs264e;
7052
7053
411
    case EF_MIPS_MACH_OCTEON3:
7054
411
      return bfd_mach_mips_octeon3;
7055
7056
7
    case EF_MIPS_MACH_OCTEON2:
7057
7
      return bfd_mach_mips_octeon2;
7058
7059
36
    case EF_MIPS_MACH_OCTEON:
7060
36
      return bfd_mach_mips_octeon;
7061
7062
18
    case EF_MIPS_MACH_XLR:
7063
18
      return bfd_mach_mips_xlr;
7064
7065
27
    case EF_MIPS_MACH_IAMR2:
7066
27
      return bfd_mach_mips_interaptiv_mr2;
7067
7068
82.0k
    default:
7069
82.0k
      switch (flags & EF_MIPS_ARCH)
7070
82.0k
  {
7071
3.06k
  default:
7072
73.6k
  case EF_MIPS_ARCH_1:
7073
73.6k
    return bfd_mach_mips3000;
7074
7075
425
  case EF_MIPS_ARCH_2:
7076
425
    return bfd_mach_mips6000;
7077
7078
844
  case EF_MIPS_ARCH_3:
7079
844
    return bfd_mach_mips4000;
7080
7081
34
  case EF_MIPS_ARCH_4:
7082
34
    return bfd_mach_mips8000;
7083
7084
11
  case EF_MIPS_ARCH_5:
7085
11
    return bfd_mach_mips5;
7086
7087
55
  case EF_MIPS_ARCH_32:
7088
55
    return bfd_mach_mipsisa32;
7089
7090
1.52k
  case EF_MIPS_ARCH_64:
7091
1.52k
    return bfd_mach_mipsisa64;
7092
7093
2.46k
  case EF_MIPS_ARCH_32R2:
7094
2.46k
    return bfd_mach_mipsisa32r2;
7095
7096
2.87k
  case EF_MIPS_ARCH_64R2:
7097
2.87k
    return bfd_mach_mipsisa64r2;
7098
7099
127
  case EF_MIPS_ARCH_32R6:
7100
127
    return bfd_mach_mipsisa32r6;
7101
7102
52
  case EF_MIPS_ARCH_64R6:
7103
52
    return bfd_mach_mipsisa64r6;
7104
82.0k
  }
7105
86.9k
    }
7106
7107
0
  return 0;
7108
86.9k
}
7109
7110
/* Return printable name for ABI.  */
7111
7112
static inline char *
7113
elf_mips_abi_name (bfd *abfd)
7114
0
{
7115
0
  flagword flags;
7116
7117
0
  flags = elf_elfheader (abfd)->e_flags;
7118
0
  switch (flags & EF_MIPS_ABI)
7119
0
    {
7120
0
    case 0:
7121
0
      if (ABI_N32_P (abfd))
7122
0
  return "N32";
7123
0
      else if (ABI_64_P (abfd))
7124
0
  return "64";
7125
0
      else
7126
0
  return "none";
7127
0
    case EF_MIPS_ABI_O32:
7128
0
      return "O32";
7129
0
    case EF_MIPS_ABI_O64:
7130
0
      return "O64";
7131
0
    case EF_MIPS_ABI_EABI32:
7132
0
      return "EABI32";
7133
0
    case EF_MIPS_ABI_EABI64:
7134
0
      return "EABI64";
7135
0
    default:
7136
0
      return "unknown abi";
7137
0
    }
7138
0
}
7139

7140
/* MIPS ELF uses two common sections.  One is the usual one, and the
7141
   other is for small objects.  All the small objects are kept
7142
   together, and then referenced via the gp pointer, which yields
7143
   faster assembler code.  This is what we use for the small common
7144
   section.  This approach is copied from ecoff.c.  */
7145
static asection mips_elf_scom_section;
7146
static const asymbol mips_elf_scom_symbol =
7147
  GLOBAL_SYM_INIT (".scommon", &mips_elf_scom_section);
7148
static asection mips_elf_scom_section =
7149
  BFD_FAKE_SECTION (mips_elf_scom_section, &mips_elf_scom_symbol,
7150
        ".scommon", 0, SEC_IS_COMMON | SEC_SMALL_DATA);
7151
7152
/* MIPS ELF also uses an acommon section, which represents an
7153
   allocated common symbol which may be overridden by a
7154
   definition in a shared library.  */
7155
static asection mips_elf_acom_section;
7156
static const asymbol mips_elf_acom_symbol =
7157
  GLOBAL_SYM_INIT (".acommon", &mips_elf_acom_section);
7158
static asection mips_elf_acom_section =
7159
  BFD_FAKE_SECTION (mips_elf_acom_section, &mips_elf_acom_symbol,
7160
        ".acommon", 0, SEC_ALLOC);
7161
7162
/* This is used for both the 32-bit and the 64-bit ABI.  */
7163
7164
void
7165
_bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
7166
19.9k
{
7167
19.9k
  elf_symbol_type *elfsym;
7168
7169
  /* Handle the special MIPS section numbers that a symbol may use.  */
7170
19.9k
  elfsym = (elf_symbol_type *) asym;
7171
19.9k
  switch (elfsym->internal_elf_sym.st_shndx)
7172
19.9k
    {
7173
96
    case SHN_MIPS_ACOMMON:
7174
      /* This section is used in a dynamically linked executable file.
7175
   It is an allocated common section.  The dynamic linker can
7176
   either resolve these symbols to something in a shared
7177
   library, or it can just leave them here.  For our purposes,
7178
   we can consider these symbols to be in a new section.  */
7179
96
      asym->section = &mips_elf_acom_section;
7180
96
      break;
7181
7182
20
    case SHN_COMMON:
7183
      /* Common symbols less than the GP size are automatically
7184
   treated as SHN_MIPS_SCOMMON symbols, with some exceptions.  */
7185
20
      if (asym->value > elf_gp_size (abfd)
7186
20
    || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
7187
20
    || IRIX_COMPAT (abfd) == ict_irix6
7188
20
    || strcmp (asym->name, "__gnu_lto_slim") == 0)
7189
18
  break;
7190
      /* Fall through.  */
7191
4
    case SHN_MIPS_SCOMMON:
7192
4
      asym->section = &mips_elf_scom_section;
7193
4
      asym->value = elfsym->internal_elf_sym.st_size;
7194
4
      break;
7195
7196
5
    case SHN_MIPS_SUNDEFINED:
7197
5
      asym->section = bfd_und_section_ptr;
7198
5
      break;
7199
7200
6
    case SHN_MIPS_TEXT:
7201
6
      {
7202
6
  asection *section = bfd_get_section_by_name (abfd, ".text");
7203
7204
6
  if (section != NULL)
7205
0
    {
7206
0
      asym->section = section;
7207
      /* MIPS_TEXT is a bit special, the address is not an offset
7208
         to the base of the .text section.  So subtract the section
7209
         base address to make it an offset.  */
7210
0
      asym->value -= section->vma;
7211
0
    }
7212
6
      }
7213
6
      break;
7214
7215
9
    case SHN_MIPS_DATA:
7216
9
      {
7217
9
  asection *section = bfd_get_section_by_name (abfd, ".data");
7218
7219
9
  if (section != NULL)
7220
0
    {
7221
0
      asym->section = section;
7222
      /* MIPS_DATA is a bit special, the address is not an offset
7223
         to the base of the .data section.  So subtract the section
7224
         base address to make it an offset.  */
7225
0
      asym->value -= section->vma;
7226
0
    }
7227
9
      }
7228
9
      break;
7229
19.9k
    }
7230
7231
  /* If this is an odd-valued function symbol, assume it's a MIPS16
7232
     or microMIPS one.  */
7233
19.9k
  if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
7234
19.9k
      && (asym->value & 1) != 0)
7235
135
    {
7236
135
      asym->value--;
7237
135
      if (MICROMIPS_P (abfd))
7238
82
  elfsym->internal_elf_sym.st_other
7239
82
    = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
7240
53
      else
7241
53
  elfsym->internal_elf_sym.st_other
7242
53
    = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
7243
135
    }
7244
19.9k
}
7245

7246
/* Implement elf_backend_eh_frame_address_size.  This differs from
7247
   the default in the way it handles EABI64.
7248
7249
   EABI64 was originally specified as an LP64 ABI, and that is what
7250
   -mabi=eabi normally gives on a 64-bit target.  However, gcc has
7251
   historically accepted the combination of -mabi=eabi and -mlong32,
7252
   and this ILP32 variation has become semi-official over time.
7253
   Both forms use elf32 and have pointer-sized FDE addresses.
7254
7255
   If an EABI object was generated by GCC 4.0 or above, it will have
7256
   an empty .gcc_compiled_longXX section, where XX is the size of longs
7257
   in bits.  Unfortunately, ILP32 objects generated by earlier compilers
7258
   have no special marking to distinguish them from LP64 objects.
7259
7260
   We don't want users of the official LP64 ABI to be punished for the
7261
   existence of the ILP32 variant, but at the same time, we don't want
7262
   to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
7263
   We therefore take the following approach:
7264
7265
      - If ABFD contains a .gcc_compiled_longXX section, use it to
7266
  determine the pointer size.
7267
7268
      - Otherwise check the type of the first relocation.  Assume that
7269
  the LP64 ABI is being used if the relocation is of type R_MIPS_64.
7270
7271
      - Otherwise punt.
7272
7273
   The second check is enough to detect LP64 objects generated by pre-4.0
7274
   compilers because, in the kind of output generated by those compilers,
7275
   the first relocation will be associated with either a CIE personality
7276
   routine or an FDE start address.  Furthermore, the compilers never
7277
   used a special (non-pointer) encoding for this ABI.
7278
7279
   Checking the relocation type should also be safe because there is no
7280
   reason to use R_MIPS_64 in an ILP32 object.  Pre-4.0 compilers never
7281
   did so.  */
7282
7283
unsigned int
7284
_bfd_mips_elf_eh_frame_address_size (bfd *abfd, const asection *sec)
7285
0
{
7286
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
7287
0
    return 8;
7288
0
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI64)
7289
0
    {
7290
0
      bool long32_p, long64_p;
7291
7292
0
      long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
7293
0
      long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
7294
0
      if (long32_p && long64_p)
7295
0
  return 0;
7296
0
      if (long32_p)
7297
0
  return 4;
7298
0
      if (long64_p)
7299
0
  return 8;
7300
7301
0
      if (sec->reloc_count > 0)
7302
0
  {
7303
    /* Load the relocations for this section.  */
7304
0
    Elf_Internal_Rela *internal_relocs =
7305
0
      _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, true);
7306
0
    if (internal_relocs == NULL)
7307
0
      return 0;
7308
7309
0
    unsigned int size = 0;
7310
0
    if (ELF32_R_TYPE (internal_relocs[0].r_info) == R_MIPS_64)
7311
0
      size = 8;
7312
7313
0
    if (elf_section_data (sec)->relocs != internal_relocs)
7314
0
      free (internal_relocs);
7315
7316
0
    return size;
7317
0
  }
7318
7319
0
      return 0;
7320
0
    }
7321
0
  return 4;
7322
0
}
7323

7324
/* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
7325
   relocations against two unnamed section symbols to resolve to the
7326
   same address.  For example, if we have code like:
7327
7328
  lw  $4,%got_disp(.data)($gp)
7329
  lw  $25,%got_disp(.text)($gp)
7330
  jalr  $25
7331
7332
   then the linker will resolve both relocations to .data and the program
7333
   will jump there rather than to .text.
7334
7335
   We can work around this problem by giving names to local section symbols.
7336
   This is also what the MIPSpro tools do.  */
7337
7338
bool
7339
_bfd_mips_elf_name_local_section_symbols (bfd *abfd)
7340
0
{
7341
0
  return elf_elfheader (abfd)->e_type == ET_REL && SGI_COMPAT (abfd);
7342
0
}
7343

7344
/* Work over a section just before writing it out.  This routine is
7345
   used by both the 32-bit and the 64-bit ABI.  FIXME: We recognize
7346
   sections that need the SHF_MIPS_GPREL flag by name; there has to be
7347
   a better way.  */
7348
7349
bool
7350
_bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
7351
29
{
7352
29
  if (hdr->sh_type == SHT_MIPS_REGINFO
7353
29
      && hdr->sh_size > 0)
7354
1
    {
7355
1
      bfd_byte buf[4];
7356
7357
1
      BFD_ASSERT (hdr->contents == NULL);
7358
7359
1
      if (hdr->sh_size != sizeof (Elf32_External_RegInfo))
7360
0
  {
7361
0
    _bfd_error_handler
7362
0
      (_("%pB: incorrect `.reginfo' section size; "
7363
0
         "expected %" PRIu64 ", got %" PRIu64),
7364
0
       abfd, (uint64_t) sizeof (Elf32_External_RegInfo),
7365
0
       (uint64_t) hdr->sh_size);
7366
0
    bfd_set_error (bfd_error_bad_value);
7367
0
    return false;
7368
0
  }
7369
7370
1
      if (bfd_seek (abfd,
7371
1
        hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
7372
1
        SEEK_SET) != 0)
7373
0
  return false;
7374
1
      H_PUT_32 (abfd, elf_gp (abfd), buf);
7375
1
      if (bfd_write (buf, 4, abfd) != 4)
7376
0
  return false;
7377
1
    }
7378
7379
29
  if (hdr->sh_type == SHT_MIPS_OPTIONS
7380
29
      && hdr->bfd_section != NULL
7381
29
      && mips_elf_section_data (hdr->bfd_section) != NULL
7382
29
      && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
7383
0
    {
7384
0
      bfd_byte *contents, *l, *lend;
7385
7386
      /* We stored the section contents in the tdata field in the
7387
   set_section_contents routine.  We save the section contents
7388
   so that we don't have to read them again.
7389
   At this point we know that elf_gp is set, so we can look
7390
   through the section contents to see if there is an
7391
   ODK_REGINFO structure.  */
7392
7393
0
      contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
7394
0
      l = contents;
7395
0
      lend = contents + hdr->sh_size;
7396
0
      while (l + sizeof (Elf_External_Options) <= lend)
7397
0
  {
7398
0
    Elf_Internal_Options intopt;
7399
7400
0
    bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7401
0
          &intopt);
7402
0
    if (intopt.size < sizeof (Elf_External_Options))
7403
0
      {
7404
0
        _bfd_error_handler
7405
    /* xgettext:c-format */
7406
0
    (_("%pB: warning: bad `%s' option size %u smaller than"
7407
0
       " its header"),
7408
0
    abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
7409
0
        break;
7410
0
      }
7411
0
    if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
7412
0
      {
7413
0
        bfd_byte buf[8];
7414
7415
0
        if (bfd_seek (abfd,
7416
0
          (hdr->sh_offset
7417
0
           + (l - contents)
7418
0
           + sizeof (Elf_External_Options)
7419
0
           + (sizeof (Elf64_External_RegInfo) - 8)),
7420
0
           SEEK_SET) != 0)
7421
0
    return false;
7422
0
        H_PUT_64 (abfd, elf_gp (abfd), buf);
7423
0
        if (bfd_write (buf, 8, abfd) != 8)
7424
0
    return false;
7425
0
      }
7426
0
    else if (intopt.kind == ODK_REGINFO)
7427
0
      {
7428
0
        bfd_byte buf[4];
7429
7430
0
        if (bfd_seek (abfd,
7431
0
          (hdr->sh_offset
7432
0
           + (l - contents)
7433
0
           + sizeof (Elf_External_Options)
7434
0
           + (sizeof (Elf32_External_RegInfo) - 4)),
7435
0
          SEEK_SET) != 0)
7436
0
    return false;
7437
0
        H_PUT_32 (abfd, elf_gp (abfd), buf);
7438
0
        if (bfd_write (buf, 4, abfd) != 4)
7439
0
    return false;
7440
0
      }
7441
0
    l += intopt.size;
7442
0
  }
7443
0
    }
7444
7445
29
  if (hdr->bfd_section != NULL)
7446
28
    {
7447
28
      const char *name = bfd_section_name (hdr->bfd_section);
7448
7449
      /* .sbss is not handled specially here because the GNU/Linux
7450
   prelinker can convert .sbss from NOBITS to PROGBITS and
7451
   changing it back to NOBITS breaks the binary.  The entry in
7452
   _bfd_mips_elf_special_sections will ensure the correct flags
7453
   are set on .sbss if BFD creates it without reading it from an
7454
   input file, and without special handling here the flags set
7455
   on it in an input file will be followed.  */
7456
28
      if (strcmp (name, ".sdata") == 0
7457
28
    || strcmp (name, ".lit8") == 0
7458
28
    || strcmp (name, ".lit4") == 0)
7459
0
  hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
7460
28
      else if (strcmp (name, ".srdata") == 0)
7461
0
  hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
7462
28
      else if (strcmp (name, ".compact_rel") == 0)
7463
0
  hdr->sh_flags = 0;
7464
28
      else if (strcmp (name, ".rtproc") == 0)
7465
0
  {
7466
0
    if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
7467
0
      {
7468
0
        unsigned int adjust;
7469
7470
0
        adjust = hdr->sh_size % hdr->sh_addralign;
7471
0
        if (adjust != 0)
7472
0
    hdr->sh_size += hdr->sh_addralign - adjust;
7473
0
      }
7474
0
  }
7475
28
    }
7476
7477
29
  return true;
7478
29
}
7479
7480
/* Handle a MIPS specific section when reading an object file.  This
7481
   is called when elfcode.h finds a section with an unknown type.
7482
   This routine supports both the 32-bit and 64-bit ELF ABI.  */
7483
7484
bool
7485
_bfd_mips_elf_section_from_shdr (bfd *abfd,
7486
         Elf_Internal_Shdr *hdr,
7487
         const char *name,
7488
         int shindex)
7489
183k
{
7490
183k
  flagword flags = 0;
7491
7492
  /* There ought to be a place to keep ELF backend specific flags, but
7493
     at the moment there isn't one.  We just keep track of the
7494
     sections by their name, instead.  Fortunately, the ABI gives
7495
     suggested names for all the MIPS specific sections, so we will
7496
     probably get away with this.  */
7497
183k
  switch (hdr->sh_type)
7498
183k
    {
7499
28
    case SHT_MIPS_LIBLIST:
7500
28
      if (strcmp (name, ".liblist") != 0)
7501
28
  return false;
7502
0
      break;
7503
17
    case SHT_MIPS_MSYM:
7504
17
      if (strcmp (name, ".msym") != 0)
7505
17
  return false;
7506
0
      break;
7507
5
    case SHT_MIPS_CONFLICT:
7508
5
      if (strcmp (name, ".conflict") != 0)
7509
5
  return false;
7510
0
      break;
7511
9
    case SHT_MIPS_GPTAB:
7512
9
      if (! startswith (name, ".gptab."))
7513
9
  return false;
7514
0
      break;
7515
4
    case SHT_MIPS_UCODE:
7516
4
      if (strcmp (name, ".ucode") != 0)
7517
4
  return false;
7518
0
      break;
7519
4
    case SHT_MIPS_DEBUG:
7520
4
      if (strcmp (name, ".mdebug") != 0)
7521
4
  return false;
7522
0
      flags = SEC_DEBUGGING;
7523
0
      break;
7524
57
    case SHT_MIPS_REGINFO:
7525
57
      if (strcmp (name, ".reginfo") != 0
7526
57
    || hdr->sh_size != sizeof (Elf32_External_RegInfo))
7527
21
  return false;
7528
36
      flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7529
36
      break;
7530
5
    case SHT_MIPS_IFACE:
7531
5
      if (strcmp (name, ".MIPS.interfaces") != 0)
7532
5
  return false;
7533
0
      break;
7534
23
    case SHT_MIPS_CONTENT:
7535
23
      if (! startswith (name, ".MIPS.content"))
7536
23
  return false;
7537
0
      break;
7538
1.36k
    case SHT_MIPS_OPTIONS:
7539
1.36k
      if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7540
229
  return false;
7541
1.13k
      break;
7542
1.13k
    case SHT_MIPS_ABIFLAGS:
7543
23
      if (!MIPS_ELF_ABIFLAGS_SECTION_NAME_P (name))
7544
23
  return false;
7545
0
      flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7546
0
      break;
7547
1.17k
    case SHT_MIPS_DWARF:
7548
1.17k
      if (! startswith (name, ".debug_")
7549
1.17k
         && ! startswith (name, ".gnu.debuglto_.debug_")
7550
1.17k
         && ! startswith (name, ".zdebug_")
7551
1.17k
         && ! startswith (name, ".gnu.debuglto_.zdebug_"))
7552
477
  return false;
7553
700
      break;
7554
700
    case SHT_MIPS_SYMBOL_LIB:
7555
7
      if (strcmp (name, ".MIPS.symlib") != 0)
7556
7
  return false;
7557
0
      break;
7558
0
    case SHT_MIPS_EVENTS:
7559
0
      if (! startswith (name, ".MIPS.events")
7560
0
    && ! startswith (name, ".MIPS.post_rel"))
7561
0
  return false;
7562
0
      break;
7563
207
    case SHT_MIPS_XHASH:
7564
207
      if (strcmp (name, ".MIPS.xhash") != 0)
7565
207
  return false;
7566
180k
    default:
7567
180k
      break;
7568
183k
    }
7569
7570
182k
  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
7571
1.58k
    return false;
7572
7573
180k
  if (hdr->sh_flags & SHF_MIPS_GPREL)
7574
36.0k
    flags |= SEC_SMALL_DATA;
7575
7576
180k
  if (flags)
7577
36.0k
    {
7578
36.0k
      if (!bfd_set_section_flags (hdr->bfd_section,
7579
36.0k
          (bfd_section_flags (hdr->bfd_section)
7580
36.0k
           | flags)))
7581
0
  return false;
7582
36.0k
    }
7583
7584
180k
  if (hdr->sh_type == SHT_MIPS_ABIFLAGS)
7585
0
    {
7586
0
      Elf_External_ABIFlags_v0 ext;
7587
7588
0
      if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7589
0
              &ext, 0, sizeof ext))
7590
0
  return false;
7591
0
      bfd_mips_elf_swap_abiflags_v0_in (abfd, &ext,
7592
0
          &mips_elf_tdata (abfd)->abiflags);
7593
0
      if (mips_elf_tdata (abfd)->abiflags.version != 0)
7594
0
  return false;
7595
0
      mips_elf_tdata (abfd)->abiflags_valid = true;
7596
0
    }
7597
7598
  /* FIXME: We should record sh_info for a .gptab section.  */
7599
7600
  /* For a .reginfo section, set the gp value in the tdata information
7601
     from the contents of this section.  We need the gp value while
7602
     processing relocs, so we just get it now.  The .reginfo section
7603
     is not used in the 64-bit MIPS ELF ABI.  */
7604
180k
  if (hdr->sh_type == SHT_MIPS_REGINFO)
7605
36
    {
7606
36
      Elf32_External_RegInfo ext;
7607
36
      Elf32_RegInfo s;
7608
7609
36
      if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7610
36
              &ext, 0, sizeof ext))
7611
0
  return false;
7612
36
      bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
7613
36
      elf_gp (abfd) = s.ri_gp_value;
7614
36
    }
7615
7616
  /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
7617
     set the gp value based on what we find.  We may see both
7618
     SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
7619
     they should agree.  */
7620
180k
  if (hdr->sh_type == SHT_MIPS_OPTIONS)
7621
1.13k
    {
7622
1.13k
      bfd_byte *contents, *l, *lend;
7623
7624
1.13k
      if (!bfd_malloc_and_get_section (abfd, hdr->bfd_section, &contents))
7625
410
  {
7626
410
    free (contents);
7627
410
    return false;
7628
410
  }
7629
722
      l = contents;
7630
722
      lend = contents + hdr->sh_size;
7631
752
      while (l + sizeof (Elf_External_Options) <= lend)
7632
48
  {
7633
48
    Elf_Internal_Options intopt;
7634
7635
48
    bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7636
48
          &intopt);
7637
48
    if (intopt.size < sizeof (Elf_External_Options))
7638
9
      {
7639
18
      bad_opt:
7640
18
        _bfd_error_handler
7641
    /* xgettext:c-format */
7642
18
    (_("%pB: warning: truncated `%s' option"),
7643
18
     abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd));
7644
18
        break;
7645
9
      }
7646
39
    if (intopt.kind == ODK_REGINFO)
7647
9
      {
7648
9
        if (ABI_64_P (abfd))
7649
9
    {
7650
9
      Elf64_Internal_RegInfo intreg;
7651
9
      size_t needed = (sizeof (Elf_External_Options)
7652
9
           + sizeof (Elf64_External_RegInfo));
7653
9
      if (intopt.size < needed || (size_t) (lend - l) < needed)
7654
9
        goto bad_opt;
7655
0
      bfd_mips_elf64_swap_reginfo_in
7656
0
        (abfd,
7657
0
         ((Elf64_External_RegInfo *)
7658
0
          (l + sizeof (Elf_External_Options))),
7659
0
         &intreg);
7660
0
      elf_gp (abfd) = intreg.ri_gp_value;
7661
0
    }
7662
0
        else
7663
0
    {
7664
0
      Elf32_RegInfo intreg;
7665
0
      size_t needed = (sizeof (Elf_External_Options)
7666
0
           + sizeof (Elf32_External_RegInfo));
7667
0
      if (intopt.size < needed || (size_t) (lend - l) < needed)
7668
0
        goto bad_opt;
7669
0
      bfd_mips_elf32_swap_reginfo_in
7670
0
        (abfd,
7671
0
         ((Elf32_External_RegInfo *)
7672
0
          (l + sizeof (Elf_External_Options))),
7673
0
         &intreg);
7674
0
      elf_gp (abfd) = intreg.ri_gp_value;
7675
0
    }
7676
9
      }
7677
30
    l += intopt.size;
7678
30
  }
7679
722
      free (contents);
7680
722
    }
7681
7682
180k
  return true;
7683
180k
}
7684
7685
/* Set the correct type for a MIPS ELF section.  We do this by the
7686
   section name, which is a hack, but ought to work.  This routine is
7687
   used by both the 32-bit and the 64-bit ABI.  */
7688
7689
bool
7690
_bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
7691
28
{
7692
28
  const char *name = bfd_section_name (sec);
7693
7694
28
  if (strcmp (name, ".liblist") == 0)
7695
0
    {
7696
0
      hdr->sh_type = SHT_MIPS_LIBLIST;
7697
0
      hdr->sh_info = sec->size / sizeof (Elf32_Lib);
7698
      /* The sh_link field is set in final_write_processing.  */
7699
0
    }
7700
28
  else if (strcmp (name, ".conflict") == 0)
7701
0
    hdr->sh_type = SHT_MIPS_CONFLICT;
7702
28
  else if (startswith (name, ".gptab."))
7703
0
    {
7704
0
      hdr->sh_type = SHT_MIPS_GPTAB;
7705
0
      hdr->sh_entsize = sizeof (Elf32_External_gptab);
7706
      /* The sh_info field is set in final_write_processing.  */
7707
0
    }
7708
28
  else if (strcmp (name, ".ucode") == 0)
7709
0
    hdr->sh_type = SHT_MIPS_UCODE;
7710
28
  else if (strcmp (name, ".mdebug") == 0)
7711
0
    {
7712
0
      hdr->sh_type = SHT_MIPS_DEBUG;
7713
      /* In a shared object on IRIX 5.3, the .mdebug section has an
7714
   entsize of 0.  FIXME: Does this matter?  */
7715
0
      if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7716
0
  hdr->sh_entsize = 0;
7717
0
      else
7718
0
  hdr->sh_entsize = 1;
7719
0
    }
7720
28
  else if (strcmp (name, ".reginfo") == 0)
7721
1
    {
7722
1
      hdr->sh_type = SHT_MIPS_REGINFO;
7723
      /* In a shared object on IRIX 5.3, the .reginfo section has an
7724
   entsize of 0x18.  FIXME: Does this matter?  */
7725
1
      if (SGI_COMPAT (abfd))
7726
1
  {
7727
1
    if ((abfd->flags & DYNAMIC) != 0)
7728
0
      hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7729
1
    else
7730
1
      hdr->sh_entsize = 1;
7731
1
  }
7732
0
      else
7733
0
  hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7734
1
    }
7735
27
  else if (SGI_COMPAT (abfd)
7736
27
     && (strcmp (name, ".hash") == 0
7737
27
         || strcmp (name, ".dynamic") == 0
7738
27
         || strcmp (name, ".dynstr") == 0))
7739
3
    {
7740
3
      if (SGI_COMPAT (abfd))
7741
3
  hdr->sh_entsize = 0;
7742
#if 0
7743
      /* This isn't how the IRIX6 linker behaves.  */
7744
      hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7745
#endif
7746
3
    }
7747
24
  else if (strcmp (name, ".got") == 0
7748
24
     || strcmp (name, ".srdata") == 0
7749
24
     || strcmp (name, ".sdata") == 0
7750
24
     || strcmp (name, ".sbss") == 0
7751
24
     || strcmp (name, ".lit4") == 0
7752
24
     || strcmp (name, ".lit8") == 0)
7753
2
    hdr->sh_flags |= SHF_MIPS_GPREL;
7754
22
  else if (strcmp (name, ".MIPS.interfaces") == 0)
7755
0
    {
7756
0
      hdr->sh_type = SHT_MIPS_IFACE;
7757
0
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7758
0
    }
7759
22
  else if (startswith (name, ".MIPS.content"))
7760
0
    {
7761
0
      hdr->sh_type = SHT_MIPS_CONTENT;
7762
0
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7763
      /* The sh_info field is set in final_write_processing.  */
7764
0
    }
7765
22
  else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7766
0
    {
7767
0
      hdr->sh_type = SHT_MIPS_OPTIONS;
7768
0
      hdr->sh_entsize = 1;
7769
0
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7770
0
    }
7771
22
  else if (startswith (name, ".MIPS.abiflags"))
7772
0
    {
7773
0
      hdr->sh_type = SHT_MIPS_ABIFLAGS;
7774
0
      hdr->sh_entsize = sizeof (Elf_External_ABIFlags_v0);
7775
0
    }
7776
22
  else if (startswith (name, ".debug_")
7777
22
     || startswith (name, ".gnu.debuglto_.debug_")
7778
22
     || startswith (name, ".zdebug_")
7779
22
     || startswith (name, ".gnu.debuglto_.zdebug_"))
7780
0
    {
7781
0
      hdr->sh_type = SHT_MIPS_DWARF;
7782
7783
      /* Irix facilities such as libexc expect a single .debug_frame
7784
   per executable, the system ones have NOSTRIP set and the linker
7785
   doesn't merge sections with different flags so ...  */
7786
0
      if (SGI_COMPAT (abfd) && startswith (name, ".debug_frame"))
7787
0
  hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7788
0
    }
7789
22
  else if (strcmp (name, ".MIPS.symlib") == 0)
7790
0
    {
7791
0
      hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7792
      /* The sh_link and sh_info fields are set in
7793
   final_write_processing.  */
7794
0
    }
7795
22
  else if (startswith (name, ".MIPS.events")
7796
22
     || startswith (name, ".MIPS.post_rel"))
7797
0
    {
7798
0
      hdr->sh_type = SHT_MIPS_EVENTS;
7799
0
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7800
      /* The sh_link field is set in final_write_processing.  */
7801
0
    }
7802
22
  else if (strcmp (name, ".msym") == 0)
7803
0
    {
7804
0
      hdr->sh_type = SHT_MIPS_MSYM;
7805
0
      hdr->sh_flags |= SHF_ALLOC;
7806
0
      hdr->sh_entsize = 8;
7807
0
    }
7808
22
  else if (strcmp (name, ".MIPS.xhash") == 0)
7809
0
    {
7810
0
      hdr->sh_type = SHT_MIPS_XHASH;
7811
0
      hdr->sh_flags |= SHF_ALLOC;
7812
0
      hdr->sh_entsize = get_elf_backend_data(abfd)->s->arch_size == 64 ? 0 : 4;
7813
0
    }
7814
7815
  /* The generic elf_fake_sections will set up REL_HDR using the default
7816
   kind of relocations.  We used to set up a second header for the
7817
   non-default kind of relocations here, but only NewABI would use
7818
   these, and the IRIX ld doesn't like resulting empty RELA sections.
7819
   Thus we create those header only on demand now.  */
7820
7821
28
  return true;
7822
28
}
7823
7824
/* Given a BFD section, try to locate the corresponding ELF section
7825
   index.  This is used by both the 32-bit and the 64-bit ABI.
7826
   Actually, it's not clear to me that the 64-bit ABI supports these,
7827
   but for non-PIC objects we will certainly want support for at least
7828
   the .scommon section.  */
7829
7830
bool
7831
_bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7832
          asection *sec, int *retval)
7833
0
{
7834
0
  if (strcmp (bfd_section_name (sec), ".scommon") == 0)
7835
0
    {
7836
0
      *retval = SHN_MIPS_SCOMMON;
7837
0
      return true;
7838
0
    }
7839
0
  if (strcmp (bfd_section_name (sec), ".acommon") == 0)
7840
0
    {
7841
0
      *retval = SHN_MIPS_ACOMMON;
7842
0
      return true;
7843
0
    }
7844
0
  return false;
7845
0
}
7846

7847
/* Hook called by the linker routine which adds symbols from an object
7848
   file.  We must handle the special MIPS section numbers here.  */
7849
7850
bool
7851
_bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
7852
             Elf_Internal_Sym *sym, const char **namep,
7853
             flagword *flagsp ATTRIBUTE_UNUSED,
7854
             asection **secp, bfd_vma *valp)
7855
0
{
7856
0
  if (SGI_COMPAT (abfd)
7857
0
      && (abfd->flags & DYNAMIC) != 0
7858
0
      && strcmp (*namep, "_rld_new_interface") == 0)
7859
0
    {
7860
      /* Skip IRIX5 rld entry name.  */
7861
0
      *namep = NULL;
7862
0
      return true;
7863
0
    }
7864
7865
  /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7866
     a SECTION *ABS*.  This causes ld to think it can resolve _gp_disp
7867
     by setting a DT_NEEDED for the shared object.  Since _gp_disp is
7868
     a magic symbol resolved by the linker, we ignore this bogus definition
7869
     of _gp_disp.  New ABI objects do not suffer from this problem so this
7870
     is not done for them. */
7871
0
  if (!NEWABI_P(abfd)
7872
0
      && (sym->st_shndx == SHN_ABS)
7873
0
      && (strcmp (*namep, "_gp_disp") == 0))
7874
0
    {
7875
0
      *namep = NULL;
7876
0
      return true;
7877
0
    }
7878
7879
0
  switch (sym->st_shndx)
7880
0
    {
7881
0
    case SHN_COMMON:
7882
      /* Common symbols less than the GP size are automatically
7883
   treated as SHN_MIPS_SCOMMON symbols, with some exceptions.  */
7884
0
      if (sym->st_size > elf_gp_size (abfd)
7885
0
    || ELF_ST_TYPE (sym->st_info) == STT_TLS
7886
0
    || IRIX_COMPAT (abfd) == ict_irix6
7887
0
    || strcmp (*namep, "__gnu_lto_slim") == 0)
7888
0
  break;
7889
      /* Fall through.  */
7890
0
    case SHN_MIPS_SCOMMON:
7891
0
      *secp = bfd_make_section_old_way (abfd, ".scommon");
7892
0
      (*secp)->flags |= SEC_IS_COMMON | SEC_SMALL_DATA;
7893
0
      *valp = sym->st_size;
7894
0
      break;
7895
7896
0
    case SHN_MIPS_TEXT:
7897
      /* This section is used in a shared object.  */
7898
0
      if (mips_elf_tdata (abfd)->elf_text_section == NULL)
7899
0
  {
7900
0
    asymbol *elf_text_symbol;
7901
0
    asection *elf_text_section;
7902
0
    size_t amt = sizeof (asection);
7903
7904
0
    elf_text_section = bfd_zalloc (abfd, amt);
7905
0
    if (elf_text_section == NULL)
7906
0
      return false;
7907
7908
0
    amt = sizeof (asymbol);
7909
0
    elf_text_symbol = bfd_zalloc (abfd, amt);
7910
0
    if (elf_text_symbol == NULL)
7911
0
      return false;
7912
7913
    /* Initialize the section.  */
7914
7915
0
    mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7916
0
    mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
7917
7918
0
    elf_text_section->symbol = elf_text_symbol;
7919
0
    elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
7920
7921
0
    elf_text_section->name = ".text";
7922
0
    elf_text_section->flags = SEC_NO_FLAGS;
7923
0
    elf_text_section->output_section = NULL;
7924
0
    elf_text_section->owner = abfd;
7925
0
    elf_text_symbol->name = ".text";
7926
0
    elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7927
0
    elf_text_symbol->section = elf_text_section;
7928
0
  }
7929
      /* This code used to do *secp = bfd_und_section_ptr if
7930
   bfd_link_pic (info).  I don't know why, and that doesn't make sense,
7931
   so I took it out.  */
7932
0
      *secp = mips_elf_tdata (abfd)->elf_text_section;
7933
0
      break;
7934
7935
0
    case SHN_MIPS_ACOMMON:
7936
      /* Fall through. XXX Can we treat this as allocated data?  */
7937
0
    case SHN_MIPS_DATA:
7938
      /* This section is used in a shared object.  */
7939
0
      if (mips_elf_tdata (abfd)->elf_data_section == NULL)
7940
0
  {
7941
0
    asymbol *elf_data_symbol;
7942
0
    asection *elf_data_section;
7943
0
    size_t amt = sizeof (asection);
7944
7945
0
    elf_data_section = bfd_zalloc (abfd, amt);
7946
0
    if (elf_data_section == NULL)
7947
0
      return false;
7948
7949
0
    amt = sizeof (asymbol);
7950
0
    elf_data_symbol = bfd_zalloc (abfd, amt);
7951
0
    if (elf_data_symbol == NULL)
7952
0
      return false;
7953
7954
    /* Initialize the section.  */
7955
7956
0
    mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
7957
0
    mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
7958
7959
0
    elf_data_section->symbol = elf_data_symbol;
7960
0
    elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
7961
7962
0
    elf_data_section->name = ".data";
7963
0
    elf_data_section->flags = SEC_NO_FLAGS;
7964
0
    elf_data_section->output_section = NULL;
7965
0
    elf_data_section->owner = abfd;
7966
0
    elf_data_symbol->name = ".data";
7967
0
    elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7968
0
    elf_data_symbol->section = elf_data_section;
7969
0
  }
7970
      /* This code used to do *secp = bfd_und_section_ptr if
7971
   bfd_link_pic (info).  I don't know why, and that doesn't make sense,
7972
   so I took it out.  */
7973
0
      *secp = mips_elf_tdata (abfd)->elf_data_section;
7974
0
      break;
7975
7976
0
    case SHN_MIPS_SUNDEFINED:
7977
0
      *secp = bfd_und_section_ptr;
7978
0
      break;
7979
0
    }
7980
7981
0
  if (SGI_COMPAT (abfd)
7982
0
      && ! bfd_link_pic (info)
7983
0
      && info->output_bfd->xvec == abfd->xvec
7984
0
      && strcmp (*namep, "__rld_obj_head") == 0)
7985
0
    {
7986
0
      struct elf_link_hash_entry *h;
7987
0
      struct bfd_link_hash_entry *bh;
7988
7989
      /* Mark __rld_obj_head as dynamic.  */
7990
0
      bh = NULL;
7991
0
      if (! (_bfd_generic_link_add_one_symbol
7992
0
       (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, false,
7993
0
        get_elf_backend_data (abfd)->collect, &bh)))
7994
0
  return false;
7995
7996
0
      h = (struct elf_link_hash_entry *) bh;
7997
0
      h->non_elf = 0;
7998
0
      h->def_regular = 1;
7999
0
      h->type = STT_OBJECT;
8000
8001
0
      if (! bfd_elf_link_record_dynamic_symbol (info, h))
8002
0
  return false;
8003
8004
0
      mips_elf_hash_table (info)->use_rld_obj_head = true;
8005
0
      mips_elf_hash_table (info)->rld_symbol = h;
8006
0
    }
8007
8008
  /* If this is a mips16 text symbol, add 1 to the value to make it
8009
     odd.  This will cause something like .word SYM to come up with
8010
     the right value when it is loaded into the PC.  */
8011
0
  if (ELF_ST_IS_COMPRESSED (sym->st_other))
8012
0
    ++*valp;
8013
8014
0
  return true;
8015
0
}
8016
8017
/* This hook function is called before the linker writes out a global
8018
   symbol.  We mark symbols as small common if appropriate.  This is
8019
   also where we undo the increment of the value for a mips16 symbol.  */
8020
8021
int
8022
_bfd_mips_elf_link_output_symbol_hook
8023
  (struct bfd_link_info *info ATTRIBUTE_UNUSED,
8024
   const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
8025
   asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
8026
0
{
8027
  /* If we see a common symbol, which implies a relocatable link, then
8028
     if a symbol was small common in an input file, mark it as small
8029
     common in the output file.  */
8030
0
  if (sym->st_shndx == SHN_COMMON
8031
0
      && strcmp (input_sec->name, ".scommon") == 0)
8032
0
    sym->st_shndx = SHN_MIPS_SCOMMON;
8033
8034
0
  if (ELF_ST_IS_COMPRESSED (sym->st_other))
8035
0
    sym->st_value &= ~1;
8036
8037
0
  return 1;
8038
0
}
8039

8040
/* Functions for the dynamic linker.  */
8041
8042
/* Create dynamic sections when linking against a dynamic object.  */
8043
8044
bool
8045
_bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
8046
0
{
8047
0
  struct elf_link_hash_entry *h;
8048
0
  struct bfd_link_hash_entry *bh;
8049
0
  flagword flags;
8050
0
  register asection *s;
8051
0
  const char * const *namep;
8052
0
  struct mips_elf_link_hash_table *htab;
8053
8054
0
  htab = mips_elf_hash_table (info);
8055
0
  BFD_ASSERT (htab != NULL);
8056
8057
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
8058
0
     | SEC_LINKER_CREATED | SEC_READONLY);
8059
8060
  /* The psABI requires a read-only .dynamic section, but the VxWorks
8061
     EABI doesn't.  */
8062
0
  if (htab->root.target_os != is_vxworks)
8063
0
    {
8064
0
      s = bfd_get_linker_section (abfd, ".dynamic");
8065
0
      if (s != NULL)
8066
0
  {
8067
0
    if (!bfd_set_section_flags (s, flags))
8068
0
      return false;
8069
0
  }
8070
0
    }
8071
8072
  /* We need to create .got section.  */
8073
0
  if (!mips_elf_create_got_section (abfd, info))
8074
0
    return false;
8075
8076
0
  if (! mips_elf_rel_dyn_section (info, true))
8077
0
    return false;
8078
8079
  /* Create .stub section.  */
8080
0
  s = bfd_make_section_anyway_with_flags (abfd,
8081
0
            MIPS_ELF_STUB_SECTION_NAME (abfd),
8082
0
            flags | SEC_CODE);
8083
0
  if (s == NULL
8084
0
      || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
8085
0
    return false;
8086
0
  htab->sstubs = s;
8087
8088
0
  if (!mips_elf_hash_table (info)->use_rld_obj_head
8089
0
      && bfd_link_executable (info)
8090
0
      && bfd_get_linker_section (abfd, ".rld_map") == NULL)
8091
0
    {
8092
0
      s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
8093
0
                flags &~ (flagword) SEC_READONLY);
8094
0
      if (s == NULL
8095
0
    || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
8096
0
  return false;
8097
0
    }
8098
8099
  /* Create .MIPS.xhash section.  */
8100
0
  if (info->emit_gnu_hash)
8101
0
    s = bfd_make_section_anyway_with_flags (abfd, ".MIPS.xhash",
8102
0
              flags | SEC_READONLY);
8103
8104
  /* On IRIX5, we adjust add some additional symbols and change the
8105
     alignments of several sections.  There is no ABI documentation
8106
     indicating that this is necessary on IRIX6, nor any evidence that
8107
     the linker takes such action.  */
8108
0
  if (IRIX_COMPAT (abfd) == ict_irix5)
8109
0
    {
8110
0
      for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
8111
0
  {
8112
0
    bh = NULL;
8113
0
    if (! (_bfd_generic_link_add_one_symbol
8114
0
     (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
8115
0
      NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8116
0
      return false;
8117
8118
0
    h = (struct elf_link_hash_entry *) bh;
8119
0
    h->mark = 1;
8120
0
    h->non_elf = 0;
8121
0
    h->def_regular = 1;
8122
0
    h->type = STT_SECTION;
8123
8124
0
    if (! bfd_elf_link_record_dynamic_symbol (info, h))
8125
0
      return false;
8126
0
  }
8127
8128
      /* We need to create a .compact_rel section.  */
8129
0
      if (SGI_COMPAT (abfd))
8130
0
  {
8131
0
    if (!mips_elf_create_compact_rel_section (abfd, info))
8132
0
      return false;
8133
0
  }
8134
8135
      /* Change alignments of some sections.  */
8136
0
      s = bfd_get_linker_section (abfd, ".hash");
8137
0
      if (s != NULL)
8138
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8139
8140
0
      s = bfd_get_linker_section (abfd, ".dynsym");
8141
0
      if (s != NULL)
8142
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8143
8144
0
      s = bfd_get_linker_section (abfd, ".dynstr");
8145
0
      if (s != NULL)
8146
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8147
8148
      /* ??? */
8149
0
      s = bfd_get_section_by_name (abfd, ".reginfo");
8150
0
      if (s != NULL)
8151
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8152
8153
0
      s = bfd_get_linker_section (abfd, ".dynamic");
8154
0
      if (s != NULL)
8155
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8156
0
    }
8157
8158
0
  if (bfd_link_executable (info))
8159
0
    {
8160
0
      const char *name;
8161
8162
0
      name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
8163
0
      bh = NULL;
8164
0
      if (!(_bfd_generic_link_add_one_symbol
8165
0
      (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
8166
0
       NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8167
0
  return false;
8168
8169
0
      h = (struct elf_link_hash_entry *) bh;
8170
0
      h->non_elf = 0;
8171
0
      h->def_regular = 1;
8172
0
      h->type = STT_SECTION;
8173
8174
0
      if (! bfd_elf_link_record_dynamic_symbol (info, h))
8175
0
  return false;
8176
8177
0
      if (! mips_elf_hash_table (info)->use_rld_obj_head)
8178
0
  {
8179
    /* __rld_map is a four byte word located in the .data section
8180
       and is filled in by the rtld to contain a pointer to
8181
       the _r_debug structure. Its symbol value will be set in
8182
       _bfd_mips_elf_finish_dynamic_symbol.  */
8183
0
    s = bfd_get_linker_section (abfd, ".rld_map");
8184
0
    BFD_ASSERT (s != NULL);
8185
8186
0
    name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
8187
0
    bh = NULL;
8188
0
    if (!(_bfd_generic_link_add_one_symbol
8189
0
    (info, abfd, name, BSF_GLOBAL, s, 0, NULL, false,
8190
0
     get_elf_backend_data (abfd)->collect, &bh)))
8191
0
      return false;
8192
8193
0
    h = (struct elf_link_hash_entry *) bh;
8194
0
    h->non_elf = 0;
8195
0
    h->def_regular = 1;
8196
0
    h->type = STT_OBJECT;
8197
8198
0
    if (! bfd_elf_link_record_dynamic_symbol (info, h))
8199
0
      return false;
8200
0
    mips_elf_hash_table (info)->rld_symbol = h;
8201
0
  }
8202
0
    }
8203
8204
  /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
8205
     Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol.  */
8206
0
  if (!_bfd_elf_create_dynamic_sections (abfd, info))
8207
0
    return false;
8208
8209
  /* Do the usual VxWorks handling.  */
8210
0
  if (htab->root.target_os == is_vxworks
8211
0
      && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
8212
0
    return false;
8213
8214
0
  return true;
8215
0
}
8216

8217
/* Return true if relocation REL against section SEC is a REL rather than
8218
   RELA relocation.  RELOCS is the first relocation in the section and
8219
   ABFD is the bfd that contains SEC.  */
8220
8221
static bool
8222
mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
8223
         const Elf_Internal_Rela *relocs,
8224
         const Elf_Internal_Rela *rel)
8225
0
{
8226
0
  Elf_Internal_Shdr *rel_hdr;
8227
0
  const struct elf_backend_data *bed;
8228
8229
  /* To determine which flavor of relocation this is, we depend on the
8230
     fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR.  */
8231
0
  rel_hdr = elf_section_data (sec)->rel.hdr;
8232
0
  if (rel_hdr == NULL)
8233
0
    return false;
8234
0
  bed = get_elf_backend_data (abfd);
8235
0
  return ((size_t) (rel - relocs)
8236
0
    < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
8237
0
}
8238
8239
/* Read the addend for REL relocation REL, which belongs to bfd ABFD.
8240
   HOWTO is the relocation's howto and CONTENTS points to the contents
8241
   of the section that REL is against.  */
8242
8243
static bfd_vma
8244
mips_elf_read_rel_addend (bfd *abfd, asection *sec,
8245
        const Elf_Internal_Rela *rel,
8246
        reloc_howto_type *howto, bfd_byte *contents)
8247
0
{
8248
0
  bfd_byte *location;
8249
0
  unsigned int r_type;
8250
0
  bfd_vma addend;
8251
0
  bfd_vma bytes;
8252
8253
0
  if (!bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8254
0
    return 0;
8255
8256
0
  r_type = ELF_R_TYPE (abfd, rel->r_info);
8257
0
  location = contents + rel->r_offset;
8258
8259
  /* Get the addend, which is stored in the input file.  */
8260
0
  _bfd_mips_elf_reloc_unshuffle (abfd, r_type, false, location);
8261
0
  bytes = mips_elf_obtain_contents (howto, rel, abfd, contents);
8262
0
  _bfd_mips_elf_reloc_shuffle (abfd, r_type, false, location);
8263
8264
0
  addend = bytes & howto->src_mask;
8265
8266
  /* Shift is 2, unusually, for microMIPS JALX.  Adjust the addend
8267
     accordingly.  */
8268
0
  if (r_type == R_MICROMIPS_26_S1 && (bytes >> 26) == 0x3c)
8269
0
    addend <<= 1;
8270
8271
0
  return addend;
8272
0
}
8273
8274
/* REL is a relocation in ABFD that needs a partnering LO16 relocation
8275
   and *ADDEND is the addend for REL itself.  Look for the LO16 relocation
8276
   and update *ADDEND with the final addend.  Return true on success
8277
   or false if the LO16 could not be found.  RELEND is the exclusive
8278
   upper bound on the relocations for REL's section.  */
8279
8280
static bool
8281
mips_elf_add_lo16_rel_addend (bfd *abfd,
8282
            asection *sec,
8283
            const Elf_Internal_Rela *rel,
8284
            const Elf_Internal_Rela *relend,
8285
            bfd_byte *contents, bfd_vma *addend)
8286
0
{
8287
0
  unsigned int r_type, lo16_type;
8288
0
  const Elf_Internal_Rela *lo16_relocation;
8289
0
  reloc_howto_type *lo16_howto;
8290
0
  bfd_vma l;
8291
8292
0
  r_type = ELF_R_TYPE (abfd, rel->r_info);
8293
0
  if (mips16_reloc_p (r_type))
8294
0
    lo16_type = R_MIPS16_LO16;
8295
0
  else if (micromips_reloc_p (r_type))
8296
0
    lo16_type = R_MICROMIPS_LO16;
8297
0
  else if (r_type == R_MIPS_PCHI16)
8298
0
    lo16_type = R_MIPS_PCLO16;
8299
0
  else
8300
0
    lo16_type = R_MIPS_LO16;
8301
8302
  /* The combined value is the sum of the HI16 addend, left-shifted by
8303
     sixteen bits, and the LO16 addend, sign extended.  (Usually, the
8304
     code does a `lui' of the HI16 value, and then an `addiu' of the
8305
     LO16 value.)
8306
8307
     Scan ahead to find a matching LO16 relocation.
8308
8309
     According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
8310
     be immediately following.  However, for the IRIX6 ABI, the next
8311
     relocation may be a composed relocation consisting of several
8312
     relocations for the same address.  In that case, the R_MIPS_LO16
8313
     relocation may occur as one of these.  We permit a similar
8314
     extension in general, as that is useful for GCC.
8315
8316
     In some cases GCC dead code elimination removes the LO16 but keeps
8317
     the corresponding HI16.  This is strictly speaking a violation of
8318
     the ABI but not immediately harmful.  */
8319
0
  lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
8320
0
  if (lo16_relocation == NULL)
8321
0
    return false;
8322
8323
  /* Obtain the addend kept there.  */
8324
0
  lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, false);
8325
0
  l = mips_elf_read_rel_addend (abfd, sec, lo16_relocation, lo16_howto,
8326
0
        contents);
8327
8328
0
  l <<= lo16_howto->rightshift;
8329
0
  l = _bfd_mips_elf_sign_extend (l, 16);
8330
8331
0
  *addend <<= 16;
8332
0
  *addend += l;
8333
0
  return true;
8334
0
}
8335
8336
/* Try to read the contents of section SEC in bfd ABFD.  Return true and
8337
   store the contents in *CONTENTS on success.  Assume that *CONTENTS
8338
   already holds the contents if it is nonull on entry.  */
8339
8340
static bool
8341
mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
8342
0
{
8343
0
  if (*contents)
8344
0
    return true;
8345
8346
  /* Get cached copy if it exists.  */
8347
0
  if (elf_section_data (sec)->this_hdr.contents != NULL)
8348
0
    {
8349
0
      *contents = elf_section_data (sec)->this_hdr.contents;
8350
0
      return true;
8351
0
    }
8352
8353
0
  return bfd_malloc_and_get_section (abfd, sec, contents);
8354
0
}
8355
8356
/* Make a new PLT record to keep internal data.  */
8357
8358
static struct plt_entry *
8359
mips_elf_make_plt_record (bfd *abfd)
8360
0
{
8361
0
  struct plt_entry *entry;
8362
8363
0
  entry = bfd_zalloc (abfd, sizeof (*entry));
8364
0
  if (entry == NULL)
8365
0
    return NULL;
8366
8367
0
  entry->stub_offset = MINUS_ONE;
8368
0
  entry->mips_offset = MINUS_ONE;
8369
0
  entry->comp_offset = MINUS_ONE;
8370
0
  entry->gotplt_index = MINUS_ONE;
8371
0
  return entry;
8372
0
}
8373
8374
/* Define the special `__gnu_absolute_zero' symbol.  We only need this
8375
   for PIC code, as otherwise there is no load-time relocation involved
8376
   and local GOT entries whose value is zero at static link time will
8377
   retain their value at load time.  */
8378
8379
static bool
8380
mips_elf_define_absolute_zero (bfd *abfd, struct bfd_link_info *info,
8381
             struct mips_elf_link_hash_table *htab,
8382
             unsigned int r_type)
8383
0
{
8384
0
  union
8385
0
    {
8386
0
      struct elf_link_hash_entry *eh;
8387
0
      struct bfd_link_hash_entry *bh;
8388
0
    }
8389
0
  hzero;
8390
8391
0
  BFD_ASSERT (!htab->use_absolute_zero);
8392
0
  BFD_ASSERT (bfd_link_pic (info));
8393
8394
0
  hzero.bh = NULL;
8395
0
  if (!_bfd_generic_link_add_one_symbol (info, abfd, "__gnu_absolute_zero",
8396
0
           BSF_GLOBAL, bfd_abs_section_ptr, 0,
8397
0
           NULL, false, false, &hzero.bh))
8398
0
    return false;
8399
8400
0
  BFD_ASSERT (hzero.bh != NULL);
8401
0
  hzero.eh->size = 0;
8402
0
  hzero.eh->type = STT_NOTYPE;
8403
0
  hzero.eh->other = STV_PROTECTED;
8404
0
  hzero.eh->def_regular = 1;
8405
0
  hzero.eh->non_elf = 0;
8406
8407
0
  if (!mips_elf_record_global_got_symbol (hzero.eh, abfd, info, true, r_type))
8408
0
    return false;
8409
8410
0
  htab->use_absolute_zero = true;
8411
8412
0
  return true;
8413
0
}
8414
8415
/* Look through the relocs for a section during the first phase, and
8416
   allocate space in the global offset table and record the need for
8417
   standard MIPS and compressed procedure linkage table entries.  */
8418
8419
bool
8420
_bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
8421
          asection *sec, const Elf_Internal_Rela *relocs)
8422
0
{
8423
0
  const char *name;
8424
0
  bfd *dynobj;
8425
0
  Elf_Internal_Shdr *symtab_hdr;
8426
0
  struct elf_link_hash_entry **sym_hashes;
8427
0
  size_t extsymoff;
8428
0
  const Elf_Internal_Rela *rel;
8429
0
  const Elf_Internal_Rela *rel_end;
8430
0
  asection *sreloc;
8431
0
  const struct elf_backend_data *bed;
8432
0
  struct mips_elf_link_hash_table *htab;
8433
0
  bfd_byte *contents;
8434
0
  bfd_vma addend;
8435
0
  reloc_howto_type *howto;
8436
8437
0
  if (bfd_link_relocatable (info))
8438
0
    return true;
8439
8440
0
  htab = mips_elf_hash_table (info);
8441
0
  BFD_ASSERT (htab != NULL);
8442
8443
0
  dynobj = elf_hash_table (info)->dynobj;
8444
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8445
0
  sym_hashes = elf_sym_hashes (abfd);
8446
0
  extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8447
8448
0
  bed = get_elf_backend_data (abfd);
8449
0
  rel_end = relocs + sec->reloc_count;
8450
8451
  /* Check for the mips16 stub sections.  */
8452
8453
0
  name = bfd_section_name (sec);
8454
0
  if (FN_STUB_P (name))
8455
0
    {
8456
0
      unsigned long r_symndx;
8457
8458
      /* Look at the relocation information to figure out which symbol
8459
   this is for.  */
8460
8461
0
      r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8462
0
      if (r_symndx == 0)
8463
0
  {
8464
0
    _bfd_error_handler
8465
      /* xgettext:c-format */
8466
0
      (_("%pB: warning: cannot determine the target function for"
8467
0
         " stub section `%s'"),
8468
0
       abfd, name);
8469
0
    bfd_set_error (bfd_error_bad_value);
8470
0
    return false;
8471
0
  }
8472
8473
0
      if (r_symndx < extsymoff
8474
0
    || sym_hashes[r_symndx - extsymoff] == NULL)
8475
0
  {
8476
0
    asection *o;
8477
8478
    /* This stub is for a local symbol.  This stub will only be
8479
       needed if there is some relocation in this BFD, other
8480
       than a 16 bit function call, which refers to this symbol.  */
8481
0
    for (o = abfd->sections; o != NULL; o = o->next)
8482
0
      {
8483
0
        Elf_Internal_Rela *sec_relocs;
8484
0
        const Elf_Internal_Rela *r, *rend;
8485
8486
        /* We can ignore stub sections when looking for relocs.  */
8487
0
        if ((o->flags & SEC_RELOC) == 0
8488
0
      || o->reloc_count == 0
8489
0
      || section_allows_mips16_refs_p (o))
8490
0
    continue;
8491
8492
0
        sec_relocs
8493
0
    = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8494
0
               info->keep_memory);
8495
0
        if (sec_relocs == NULL)
8496
0
    return false;
8497
8498
0
        rend = sec_relocs + o->reloc_count;
8499
0
        for (r = sec_relocs; r < rend; r++)
8500
0
    if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8501
0
        && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
8502
0
      break;
8503
8504
0
        if (elf_section_data (o)->relocs != sec_relocs)
8505
0
    free (sec_relocs);
8506
8507
0
        if (r < rend)
8508
0
    break;
8509
0
      }
8510
8511
0
    if (o == NULL)
8512
0
      {
8513
        /* There is no non-call reloc for this stub, so we do
8514
     not need it.  Since this function is called before
8515
     the linker maps input sections to output sections, we
8516
     can easily discard it by setting the SEC_EXCLUDE
8517
     flag.  */
8518
0
        sec->flags |= SEC_EXCLUDE;
8519
0
        return true;
8520
0
      }
8521
8522
    /* Record this stub in an array of local symbol stubs for
8523
       this BFD.  */
8524
0
    if (mips_elf_tdata (abfd)->local_stubs == NULL)
8525
0
      {
8526
0
        unsigned long symcount;
8527
0
        asection **n;
8528
0
        bfd_size_type amt;
8529
8530
0
        if (elf_bad_symtab (abfd))
8531
0
    symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8532
0
        else
8533
0
    symcount = symtab_hdr->sh_info;
8534
0
        amt = symcount * sizeof (asection *);
8535
0
        n = bfd_zalloc (abfd, amt);
8536
0
        if (n == NULL)
8537
0
    return false;
8538
0
        mips_elf_tdata (abfd)->local_stubs = n;
8539
0
      }
8540
8541
0
    sec->flags |= SEC_KEEP;
8542
0
    mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
8543
8544
    /* We don't need to set mips16_stubs_seen in this case.
8545
       That flag is used to see whether we need to look through
8546
       the global symbol table for stubs.  We don't need to set
8547
       it here, because we just have a local stub.  */
8548
0
  }
8549
0
      else
8550
0
  {
8551
0
    struct mips_elf_link_hash_entry *h;
8552
8553
0
    h = ((struct mips_elf_link_hash_entry *)
8554
0
         sym_hashes[r_symndx - extsymoff]);
8555
8556
0
    while (h->root.root.type == bfd_link_hash_indirect
8557
0
     || h->root.root.type == bfd_link_hash_warning)
8558
0
      h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8559
8560
    /* H is the symbol this stub is for.  */
8561
8562
    /* If we already have an appropriate stub for this function, we
8563
       don't need another one, so we can discard this one.  Since
8564
       this function is called before the linker maps input sections
8565
       to output sections, we can easily discard it by setting the
8566
       SEC_EXCLUDE flag.  */
8567
0
    if (h->fn_stub != NULL)
8568
0
      {
8569
0
        sec->flags |= SEC_EXCLUDE;
8570
0
        return true;
8571
0
      }
8572
8573
0
    sec->flags |= SEC_KEEP;
8574
0
    h->fn_stub = sec;
8575
0
    mips_elf_hash_table (info)->mips16_stubs_seen = true;
8576
0
  }
8577
0
    }
8578
0
  else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
8579
0
    {
8580
0
      unsigned long r_symndx;
8581
0
      struct mips_elf_link_hash_entry *h;
8582
0
      asection **loc;
8583
8584
      /* Look at the relocation information to figure out which symbol
8585
   this is for.  */
8586
8587
0
      r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8588
0
      if (r_symndx == 0)
8589
0
  {
8590
0
    _bfd_error_handler
8591
      /* xgettext:c-format */
8592
0
      (_("%pB: warning: cannot determine the target function for"
8593
0
         " stub section `%s'"),
8594
0
       abfd, name);
8595
0
    bfd_set_error (bfd_error_bad_value);
8596
0
    return false;
8597
0
  }
8598
8599
0
      if (r_symndx < extsymoff
8600
0
    || sym_hashes[r_symndx - extsymoff] == NULL)
8601
0
  {
8602
0
    asection *o;
8603
8604
    /* This stub is for a local symbol.  This stub will only be
8605
       needed if there is some relocation (R_MIPS16_26) in this BFD
8606
       that refers to this symbol.  */
8607
0
    for (o = abfd->sections; o != NULL; o = o->next)
8608
0
      {
8609
0
        Elf_Internal_Rela *sec_relocs;
8610
0
        const Elf_Internal_Rela *r, *rend;
8611
8612
        /* We can ignore stub sections when looking for relocs.  */
8613
0
        if ((o->flags & SEC_RELOC) == 0
8614
0
      || o->reloc_count == 0
8615
0
      || section_allows_mips16_refs_p (o))
8616
0
    continue;
8617
8618
0
        sec_relocs
8619
0
    = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8620
0
               info->keep_memory);
8621
0
        if (sec_relocs == NULL)
8622
0
    return false;
8623
8624
0
        rend = sec_relocs + o->reloc_count;
8625
0
        for (r = sec_relocs; r < rend; r++)
8626
0
    if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8627
0
        && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
8628
0
        break;
8629
8630
0
        if (elf_section_data (o)->relocs != sec_relocs)
8631
0
    free (sec_relocs);
8632
8633
0
        if (r < rend)
8634
0
    break;
8635
0
      }
8636
8637
0
    if (o == NULL)
8638
0
      {
8639
        /* There is no non-call reloc for this stub, so we do
8640
     not need it.  Since this function is called before
8641
     the linker maps input sections to output sections, we
8642
     can easily discard it by setting the SEC_EXCLUDE
8643
     flag.  */
8644
0
        sec->flags |= SEC_EXCLUDE;
8645
0
        return true;
8646
0
      }
8647
8648
    /* Record this stub in an array of local symbol call_stubs for
8649
       this BFD.  */
8650
0
    if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
8651
0
      {
8652
0
        unsigned long symcount;
8653
0
        asection **n;
8654
0
        bfd_size_type amt;
8655
8656
0
        if (elf_bad_symtab (abfd))
8657
0
    symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8658
0
        else
8659
0
    symcount = symtab_hdr->sh_info;
8660
0
        amt = symcount * sizeof (asection *);
8661
0
        n = bfd_zalloc (abfd, amt);
8662
0
        if (n == NULL)
8663
0
    return false;
8664
0
        mips_elf_tdata (abfd)->local_call_stubs = n;
8665
0
      }
8666
8667
0
    sec->flags |= SEC_KEEP;
8668
0
    mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
8669
8670
    /* We don't need to set mips16_stubs_seen in this case.
8671
       That flag is used to see whether we need to look through
8672
       the global symbol table for stubs.  We don't need to set
8673
       it here, because we just have a local stub.  */
8674
0
  }
8675
0
      else
8676
0
  {
8677
0
    h = ((struct mips_elf_link_hash_entry *)
8678
0
         sym_hashes[r_symndx - extsymoff]);
8679
8680
    /* H is the symbol this stub is for.  */
8681
8682
0
    if (CALL_FP_STUB_P (name))
8683
0
      loc = &h->call_fp_stub;
8684
0
    else
8685
0
      loc = &h->call_stub;
8686
8687
    /* If we already have an appropriate stub for this function, we
8688
       don't need another one, so we can discard this one.  Since
8689
       this function is called before the linker maps input sections
8690
       to output sections, we can easily discard it by setting the
8691
       SEC_EXCLUDE flag.  */
8692
0
    if (*loc != NULL)
8693
0
      {
8694
0
        sec->flags |= SEC_EXCLUDE;
8695
0
        return true;
8696
0
      }
8697
8698
0
    sec->flags |= SEC_KEEP;
8699
0
    *loc = sec;
8700
0
    mips_elf_hash_table (info)->mips16_stubs_seen = true;
8701
0
  }
8702
0
    }
8703
8704
0
  sreloc = NULL;
8705
0
  contents = NULL;
8706
0
  for (rel = relocs; rel < rel_end; ++rel)
8707
0
    {
8708
0
      unsigned long r_symndx;
8709
0
      unsigned int r_type;
8710
0
      struct elf_link_hash_entry *h;
8711
0
      bool can_make_dynamic_p;
8712
0
      bool call_reloc_p;
8713
0
      bool constrain_symbol_p;
8714
8715
0
      r_symndx = ELF_R_SYM (abfd, rel->r_info);
8716
0
      r_type = ELF_R_TYPE (abfd, rel->r_info);
8717
8718
0
      if (r_symndx < extsymoff)
8719
0
  h = NULL;
8720
0
      else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
8721
0
  {
8722
0
    _bfd_error_handler
8723
      /* xgettext:c-format */
8724
0
      (_("%pB: malformed reloc detected for section %s"),
8725
0
       abfd, name);
8726
0
    bfd_set_error (bfd_error_bad_value);
8727
0
    return false;
8728
0
  }
8729
0
      else
8730
0
  {
8731
0
    h = sym_hashes[r_symndx - extsymoff];
8732
0
    if (h != NULL)
8733
0
      {
8734
0
        while (h->root.type == bfd_link_hash_indirect
8735
0
         || h->root.type == bfd_link_hash_warning)
8736
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
8737
0
      }
8738
0
  }
8739
8740
      /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8741
   relocation into a dynamic one.  */
8742
0
      can_make_dynamic_p = false;
8743
8744
      /* Set CALL_RELOC_P to true if the relocation is for a call,
8745
   and if pointer equality therefore doesn't matter.  */
8746
0
      call_reloc_p = false;
8747
8748
      /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
8749
   into account when deciding how to define the symbol.  */
8750
0
      constrain_symbol_p = true;
8751
8752
0
      switch (r_type)
8753
0
  {
8754
0
  case R_MIPS_CALL16:
8755
0
  case R_MIPS_CALL_HI16:
8756
0
  case R_MIPS_CALL_LO16:
8757
0
  case R_MIPS16_CALL16:
8758
0
  case R_MICROMIPS_CALL16:
8759
0
  case R_MICROMIPS_CALL_HI16:
8760
0
  case R_MICROMIPS_CALL_LO16:
8761
0
    call_reloc_p = true;
8762
    /* Fall through.  */
8763
8764
0
  case R_MIPS_GOT16:
8765
0
  case R_MIPS_GOT_LO16:
8766
0
  case R_MIPS_GOT_PAGE:
8767
0
  case R_MIPS_GOT_DISP:
8768
0
  case R_MIPS16_GOT16:
8769
0
  case R_MICROMIPS_GOT16:
8770
0
  case R_MICROMIPS_GOT_LO16:
8771
0
  case R_MICROMIPS_GOT_PAGE:
8772
0
  case R_MICROMIPS_GOT_DISP:
8773
    /* If we have a symbol that will resolve to zero at static link
8774
       time and it is used by a GOT relocation applied to code we
8775
       cannot relax to an immediate zero load, then we will be using
8776
       the special `__gnu_absolute_zero' symbol whose value is zero
8777
       at dynamic load time.  We ignore HI16-type GOT relocations at
8778
       this stage, because their handling will depend entirely on
8779
       the corresponding LO16-type GOT relocation.  */
8780
0
    if (!call_hi16_reloc_p (r_type)
8781
0
        && h != NULL
8782
0
        && bfd_link_pic (info)
8783
0
        && !htab->use_absolute_zero
8784
0
        && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8785
0
      {
8786
0
        bool rel_reloc;
8787
8788
0
        if (!mips_elf_get_section_contents (abfd, sec, &contents))
8789
0
    return false;
8790
8791
0
        rel_reloc = mips_elf_rel_relocation_p (abfd, sec, relocs, rel);
8792
0
        howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, !rel_reloc);
8793
0
        if (bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8794
0
    if (!mips_elf_nullify_got_load (abfd, contents, rel, howto,
8795
0
            false))
8796
0
      if (!mips_elf_define_absolute_zero (abfd, info, htab,
8797
0
                  r_type))
8798
0
        return false;
8799
0
      }
8800
8801
    /* Fall through.  */
8802
0
  case R_MIPS_GOT_HI16:
8803
0
  case R_MIPS_GOT_OFST:
8804
0
  case R_MIPS_TLS_GOTTPREL:
8805
0
  case R_MIPS_TLS_GD:
8806
0
  case R_MIPS_TLS_LDM:
8807
0
  case R_MIPS16_TLS_GOTTPREL:
8808
0
  case R_MIPS16_TLS_GD:
8809
0
  case R_MIPS16_TLS_LDM:
8810
0
  case R_MICROMIPS_GOT_HI16:
8811
0
  case R_MICROMIPS_GOT_OFST:
8812
0
  case R_MICROMIPS_TLS_GOTTPREL:
8813
0
  case R_MICROMIPS_TLS_GD:
8814
0
  case R_MICROMIPS_TLS_LDM:
8815
0
    if (dynobj == NULL)
8816
0
      elf_hash_table (info)->dynobj = dynobj = abfd;
8817
0
    if (!mips_elf_create_got_section (dynobj, info))
8818
0
      return false;
8819
0
    if (htab->root.target_os == is_vxworks
8820
0
        && !bfd_link_pic (info))
8821
0
      {
8822
0
        _bfd_error_handler
8823
    /* xgettext:c-format */
8824
0
    (_("%pB: GOT reloc at %#" PRIx64 " not expected in executables"),
8825
0
     abfd, (uint64_t) rel->r_offset);
8826
0
        bfd_set_error (bfd_error_bad_value);
8827
0
        return false;
8828
0
      }
8829
0
    can_make_dynamic_p = true;
8830
0
    break;
8831
8832
0
  case R_MIPS_NONE:
8833
0
  case R_MIPS_JALR:
8834
0
  case R_MICROMIPS_JALR:
8835
    /* These relocations have empty fields and are purely there to
8836
       provide link information.  The symbol value doesn't matter.  */
8837
0
    constrain_symbol_p = false;
8838
0
    break;
8839
8840
0
  case R_MIPS_GPREL16:
8841
0
  case R_MIPS_GPREL32:
8842
0
  case R_MIPS16_GPREL:
8843
0
  case R_MICROMIPS_GPREL16:
8844
    /* GP-relative relocations always resolve to a definition in a
8845
       regular input file, ignoring the one-definition rule.  This is
8846
       important for the GP setup sequence in NewABI code, which
8847
       always resolves to a local function even if other relocations
8848
       against the symbol wouldn't.  */
8849
0
    constrain_symbol_p = false;
8850
0
    break;
8851
8852
0
  case R_MIPS_32:
8853
0
  case R_MIPS_REL32:
8854
0
  case R_MIPS_64:
8855
    /* In VxWorks executables, references to external symbols
8856
       must be handled using copy relocs or PLT entries; it is not
8857
       possible to convert this relocation into a dynamic one.
8858
8859
       For executables that use PLTs and copy-relocs, we have a
8860
       choice between converting the relocation into a dynamic
8861
       one or using copy relocations or PLT entries.  It is
8862
       usually better to do the former, unless the relocation is
8863
       against a read-only section.  */
8864
0
    if ((bfd_link_pic (info)
8865
0
         || (h != NULL
8866
0
       && htab->root.target_os != is_vxworks
8867
0
       && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8868
0
       && !(!info->nocopyreloc
8869
0
      && !PIC_OBJECT_P (abfd)
8870
0
      && MIPS_ELF_READONLY_SECTION (sec))))
8871
0
        && (sec->flags & SEC_ALLOC) != 0)
8872
0
      {
8873
0
        can_make_dynamic_p = true;
8874
0
        if (dynobj == NULL)
8875
0
    elf_hash_table (info)->dynobj = dynobj = abfd;
8876
0
      }
8877
0
    break;
8878
8879
0
  case R_MIPS_26:
8880
0
  case R_MIPS_PC16:
8881
0
  case R_MIPS_PC21_S2:
8882
0
  case R_MIPS_PC26_S2:
8883
0
  case R_MIPS16_26:
8884
0
  case R_MIPS16_PC16_S1:
8885
0
  case R_MICROMIPS_26_S1:
8886
0
  case R_MICROMIPS_PC7_S1:
8887
0
  case R_MICROMIPS_PC10_S1:
8888
0
  case R_MICROMIPS_PC16_S1:
8889
0
  case R_MICROMIPS_PC23_S2:
8890
0
    call_reloc_p = true;
8891
0
    break;
8892
0
  }
8893
8894
0
      if (h)
8895
0
  {
8896
0
    if (constrain_symbol_p)
8897
0
      {
8898
0
        if (!can_make_dynamic_p)
8899
0
    ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8900
8901
0
        if (!call_reloc_p)
8902
0
    h->pointer_equality_needed = 1;
8903
8904
        /* We must not create a stub for a symbol that has
8905
     relocations related to taking the function's address.
8906
     This doesn't apply to VxWorks, where CALL relocs refer
8907
     to a .got.plt entry instead of a normal .got entry.  */
8908
0
        if (htab->root.target_os != is_vxworks
8909
0
      && (!can_make_dynamic_p || !call_reloc_p))
8910
0
    ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = true;
8911
0
      }
8912
8913
    /* Relocations against the special VxWorks __GOTT_BASE__ and
8914
       __GOTT_INDEX__ symbols must be left to the loader.  Allocate
8915
       room for them in .rela.dyn.  */
8916
0
    if (is_gott_symbol (info, h))
8917
0
      {
8918
0
        if (sreloc == NULL)
8919
0
    {
8920
0
      sreloc = mips_elf_rel_dyn_section (info, true);
8921
0
      if (sreloc == NULL)
8922
0
        return false;
8923
0
    }
8924
0
        mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8925
0
        if (MIPS_ELF_READONLY_SECTION (sec))
8926
    /* We tell the dynamic linker that there are
8927
       relocations against the text segment.  */
8928
0
    info->flags |= DF_TEXTREL;
8929
0
      }
8930
0
  }
8931
0
      else if (call_lo16_reloc_p (r_type)
8932
0
         || got_lo16_reloc_p (r_type)
8933
0
         || got_disp_reloc_p (r_type)
8934
0
         || (got16_reloc_p (r_type)
8935
0
       && htab->root.target_os == is_vxworks))
8936
0
  {
8937
    /* We may need a local GOT entry for this relocation.  We
8938
       don't count R_MIPS_GOT_PAGE because we can estimate the
8939
       maximum number of pages needed by looking at the size of
8940
       the segment.  Similar comments apply to R_MIPS*_GOT16 and
8941
       R_MIPS*_CALL16, except on VxWorks, where GOT relocations
8942
       always evaluate to "G".  We don't count R_MIPS_GOT_HI16, or
8943
       R_MIPS_CALL_HI16 because these are always followed by an
8944
       R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.  */
8945
0
    if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8946
0
             rel->r_addend, info, r_type))
8947
0
      return false;
8948
0
  }
8949
8950
0
      if (h != NULL
8951
0
    && mips_elf_relocation_needs_la25_stub (abfd, r_type,
8952
0
              ELF_ST_IS_MIPS16 (h->other)))
8953
0
  ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = true;
8954
8955
0
      switch (r_type)
8956
0
  {
8957
0
  case R_MIPS_CALL16:
8958
0
  case R_MIPS16_CALL16:
8959
0
  case R_MICROMIPS_CALL16:
8960
0
    if (h == NULL)
8961
0
      {
8962
0
        _bfd_error_handler
8963
    /* xgettext:c-format */
8964
0
    (_("%pB: CALL16 reloc at %#" PRIx64 " not against global symbol"),
8965
0
     abfd, (uint64_t) rel->r_offset);
8966
0
        bfd_set_error (bfd_error_bad_value);
8967
0
        return false;
8968
0
      }
8969
    /* Fall through.  */
8970
8971
0
  case R_MIPS_CALL_HI16:
8972
0
  case R_MIPS_CALL_LO16:
8973
0
  case R_MICROMIPS_CALL_HI16:
8974
0
  case R_MICROMIPS_CALL_LO16:
8975
0
    if (h != NULL)
8976
0
      {
8977
        /* Make sure there is room in the regular GOT to hold the
8978
     function's address.  We may eliminate it in favour of
8979
     a .got.plt entry later; see mips_elf_count_got_symbols.  */
8980
0
        if (!mips_elf_record_global_got_symbol (h, abfd, info, true,
8981
0
                  r_type))
8982
0
    return false;
8983
8984
        /* We need a stub, not a plt entry for the undefined
8985
     function.  But we record it as if it needs plt.  See
8986
     _bfd_elf_adjust_dynamic_symbol.  */
8987
0
        h->needs_plt = 1;
8988
0
        h->type = STT_FUNC;
8989
0
      }
8990
0
    break;
8991
8992
0
  case R_MIPS_GOT_PAGE:
8993
0
  case R_MICROMIPS_GOT_PAGE:
8994
0
  case R_MIPS16_GOT16:
8995
0
  case R_MIPS_GOT16:
8996
0
  case R_MIPS_GOT_HI16:
8997
0
  case R_MIPS_GOT_LO16:
8998
0
  case R_MICROMIPS_GOT16:
8999
0
  case R_MICROMIPS_GOT_HI16:
9000
0
  case R_MICROMIPS_GOT_LO16:
9001
0
    if (!h || got_page_reloc_p (r_type))
9002
0
      {
9003
        /* This relocation needs (or may need, if h != NULL) a
9004
     page entry in the GOT.  For R_MIPS_GOT_PAGE we do not
9005
     know for sure until we know whether the symbol is
9006
     preemptible.  */
9007
0
        if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
9008
0
    {
9009
0
      if (!mips_elf_get_section_contents (abfd, sec, &contents))
9010
0
        return false;
9011
0
      howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, false);
9012
0
      addend = mips_elf_read_rel_addend (abfd, sec, rel,
9013
0
                 howto, contents);
9014
0
      if (got16_reloc_p (r_type))
9015
0
        mips_elf_add_lo16_rel_addend (abfd, sec, rel, rel_end,
9016
0
              contents, &addend);
9017
0
      else
9018
0
        addend <<= howto->rightshift;
9019
0
    }
9020
0
        else
9021
0
    addend = rel->r_addend;
9022
0
        if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
9023
0
             h, addend))
9024
0
    return false;
9025
9026
0
        if (h)
9027
0
    {
9028
0
      struct mips_elf_link_hash_entry *hmips =
9029
0
        (struct mips_elf_link_hash_entry *) h;
9030
9031
      /* This symbol is definitely not overridable.  */
9032
0
      if (hmips->root.def_regular
9033
0
          && ! (bfd_link_pic (info) && ! info->symbolic
9034
0
          && ! hmips->root.forced_local))
9035
0
        h = NULL;
9036
0
    }
9037
0
      }
9038
    /* If this is a global, overridable symbol, GOT_PAGE will
9039
       decay to GOT_DISP, so we'll need a GOT entry for it.  */
9040
    /* Fall through.  */
9041
9042
0
  case R_MIPS_GOT_DISP:
9043
0
  case R_MICROMIPS_GOT_DISP:
9044
0
    if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
9045
0
                   false, r_type))
9046
0
      return false;
9047
0
    break;
9048
9049
0
  case R_MIPS_TLS_GOTTPREL:
9050
0
  case R_MIPS16_TLS_GOTTPREL:
9051
0
  case R_MICROMIPS_TLS_GOTTPREL:
9052
0
    if (bfd_link_pic (info))
9053
0
      info->flags |= DF_STATIC_TLS;
9054
    /* Fall through */
9055
9056
0
  case R_MIPS_TLS_LDM:
9057
0
  case R_MIPS16_TLS_LDM:
9058
0
  case R_MICROMIPS_TLS_LDM:
9059
0
    if (tls_ldm_reloc_p (r_type))
9060
0
      {
9061
0
        r_symndx = STN_UNDEF;
9062
0
        h = NULL;
9063
0
      }
9064
    /* Fall through */
9065
9066
0
  case R_MIPS_TLS_GD:
9067
0
  case R_MIPS16_TLS_GD:
9068
0
  case R_MICROMIPS_TLS_GD:
9069
    /* This symbol requires a global offset table entry, or two
9070
       for TLS GD relocations.  */
9071
0
    if (h != NULL)
9072
0
      {
9073
0
        if (!mips_elf_record_global_got_symbol (h, abfd, info,
9074
0
                  false, r_type))
9075
0
    return false;
9076
0
      }
9077
0
    else
9078
0
      {
9079
0
        if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
9080
0
                 rel->r_addend,
9081
0
                 info, r_type))
9082
0
    return false;
9083
0
      }
9084
0
    break;
9085
9086
0
  case R_MIPS_32:
9087
0
  case R_MIPS_REL32:
9088
0
  case R_MIPS_64:
9089
    /* In VxWorks executables, references to external symbols
9090
       are handled using copy relocs or PLT stubs, so there's
9091
       no need to add a .rela.dyn entry for this relocation.  */
9092
0
    if (can_make_dynamic_p)
9093
0
      {
9094
0
        if (sreloc == NULL)
9095
0
    {
9096
0
      sreloc = mips_elf_rel_dyn_section (info, true);
9097
0
      if (sreloc == NULL)
9098
0
        return false;
9099
0
    }
9100
0
        if (bfd_link_pic (info) && h == NULL)
9101
0
    {
9102
      /* When creating a shared object, we must copy these
9103
         reloc types into the output file as R_MIPS_REL32
9104
         relocs.  Make room for this reloc in .rel(a).dyn.  */
9105
0
      mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9106
0
      if (MIPS_ELF_READONLY_SECTION (sec))
9107
        /* We tell the dynamic linker that there are
9108
           relocations against the text segment.  */
9109
0
        info->flags |= DF_TEXTREL;
9110
0
    }
9111
0
        else
9112
0
    {
9113
0
      struct mips_elf_link_hash_entry *hmips;
9114
9115
      /* For a shared object, we must copy this relocation
9116
         unless the symbol turns out to be undefined and
9117
         weak with non-default visibility, in which case
9118
         it will be left as zero.
9119
9120
         We could elide R_MIPS_REL32 for locally binding symbols
9121
         in shared libraries, but do not yet do so.
9122
9123
         For an executable, we only need to copy this
9124
         reloc if the symbol is defined in a dynamic
9125
         object.  */
9126
0
      hmips = (struct mips_elf_link_hash_entry *) h;
9127
0
      ++hmips->possibly_dynamic_relocs;
9128
0
      if (MIPS_ELF_READONLY_SECTION (sec))
9129
        /* We need it to tell the dynamic linker if there
9130
           are relocations against the text segment.  */
9131
0
        hmips->readonly_reloc = true;
9132
0
    }
9133
0
      }
9134
9135
0
    if (SGI_COMPAT (abfd))
9136
0
      mips_elf_hash_table (info)->compact_rel_size +=
9137
0
        sizeof (Elf32_External_crinfo);
9138
0
    break;
9139
9140
0
  case R_MIPS_26:
9141
0
  case R_MIPS_GPREL16:
9142
0
  case R_MIPS_LITERAL:
9143
0
  case R_MIPS_GPREL32:
9144
0
  case R_MICROMIPS_26_S1:
9145
0
  case R_MICROMIPS_GPREL16:
9146
0
  case R_MICROMIPS_LITERAL:
9147
0
  case R_MICROMIPS_GPREL7_S2:
9148
0
    if (SGI_COMPAT (abfd))
9149
0
      mips_elf_hash_table (info)->compact_rel_size +=
9150
0
        sizeof (Elf32_External_crinfo);
9151
0
    break;
9152
9153
    /* This relocation describes the C++ object vtable hierarchy.
9154
       Reconstruct it for later use during GC.  */
9155
0
  case R_MIPS_GNU_VTINHERIT:
9156
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
9157
0
      return false;
9158
0
    break;
9159
9160
    /* This relocation describes which C++ vtable entries are actually
9161
       used.  Record for later use during GC.  */
9162
0
  case R_MIPS_GNU_VTENTRY:
9163
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
9164
0
      return false;
9165
0
    break;
9166
9167
0
  default:
9168
0
    break;
9169
0
  }
9170
9171
      /* Record the need for a PLT entry.  At this point we don't know
9172
   yet if we are going to create a PLT in the first place, but
9173
   we only record whether the relocation requires a standard MIPS
9174
   or a compressed code entry anyway.  If we don't make a PLT after
9175
   all, then we'll just ignore these arrangements.  Likewise if
9176
   a PLT entry is not created because the symbol is satisfied
9177
   locally.  */
9178
0
      if (h != NULL
9179
0
    && (branch_reloc_p (r_type)
9180
0
        || mips16_branch_reloc_p (r_type)
9181
0
        || micromips_branch_reloc_p (r_type))
9182
0
    && !SYMBOL_CALLS_LOCAL (info, h))
9183
0
  {
9184
0
    if (h->plt.plist == NULL)
9185
0
      h->plt.plist = mips_elf_make_plt_record (abfd);
9186
0
    if (h->plt.plist == NULL)
9187
0
      return false;
9188
9189
0
    if (branch_reloc_p (r_type))
9190
0
      h->plt.plist->need_mips = true;
9191
0
    else
9192
0
      h->plt.plist->need_comp = true;
9193
0
  }
9194
9195
      /* See if this reloc would need to refer to a MIPS16 hard-float stub,
9196
   if there is one.  We only need to handle global symbols here;
9197
   we decide whether to keep or delete stubs for local symbols
9198
   when processing the stub's relocations.  */
9199
0
      if (h != NULL
9200
0
    && !mips16_call_reloc_p (r_type)
9201
0
    && !section_allows_mips16_refs_p (sec))
9202
0
  {
9203
0
    struct mips_elf_link_hash_entry *mh;
9204
9205
0
    mh = (struct mips_elf_link_hash_entry *) h;
9206
0
    mh->need_fn_stub = true;
9207
0
  }
9208
9209
      /* Refuse some position-dependent relocations when creating a
9210
   shared library.  Do not refuse R_MIPS_32 / R_MIPS_64; they're
9211
   not PIC, but we can create dynamic relocations and the result
9212
   will be fine.  Also do not refuse R_MIPS_LO16, which can be
9213
   combined with R_MIPS_GOT16.  */
9214
0
      if (bfd_link_pic (info))
9215
0
  {
9216
0
    switch (r_type)
9217
0
      {
9218
0
      case R_MIPS_TLS_TPREL_HI16:
9219
0
      case R_MIPS16_TLS_TPREL_HI16:
9220
0
      case R_MICROMIPS_TLS_TPREL_HI16:
9221
0
      case R_MIPS_TLS_TPREL_LO16:
9222
0
      case R_MIPS16_TLS_TPREL_LO16:
9223
0
      case R_MICROMIPS_TLS_TPREL_LO16:
9224
        /* These are okay in PIE, but not in a shared library.  */
9225
0
        if (bfd_link_executable (info))
9226
0
    break;
9227
9228
        /* FALLTHROUGH */
9229
9230
0
      case R_MIPS16_HI16:
9231
0
      case R_MIPS_HI16:
9232
0
      case R_MIPS_HIGHER:
9233
0
      case R_MIPS_HIGHEST:
9234
0
      case R_MICROMIPS_HI16:
9235
0
      case R_MICROMIPS_HIGHER:
9236
0
      case R_MICROMIPS_HIGHEST:
9237
        /* Don't refuse a high part relocation if it's against
9238
     no symbol (e.g. part of a compound relocation).  */
9239
0
        if (r_symndx == STN_UNDEF)
9240
0
    break;
9241
9242
        /* Likewise an absolute symbol.  */
9243
0
        if (h != NULL && bfd_is_abs_symbol (&h->root))
9244
0
    break;
9245
9246
        /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
9247
     and has a special meaning.  */
9248
0
        if (!NEWABI_P (abfd) && h != NULL
9249
0
      && strcmp (h->root.root.string, "_gp_disp") == 0)
9250
0
    break;
9251
9252
        /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks.  */
9253
0
        if (is_gott_symbol (info, h))
9254
0
    break;
9255
9256
        /* FALLTHROUGH */
9257
9258
0
      case R_MIPS16_26:
9259
0
      case R_MIPS_26:
9260
0
      case R_MICROMIPS_26_S1:
9261
0
        howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, NEWABI_P (abfd));
9262
        /* An error for unsupported relocations is raised as part
9263
     of the above search, so we can skip the following.  */
9264
0
        if (howto != NULL)
9265
0
    info->callbacks->einfo
9266
      /* xgettext:c-format */
9267
0
      (_("%X%H: relocation %s against `%s' cannot be used"
9268
0
         " when making a shared object; recompile with -fPIC\n"),
9269
0
       abfd, sec, rel->r_offset, howto->name,
9270
0
       (h) ? h->root.root.string : "a local symbol");
9271
0
        break;
9272
0
      default:
9273
0
        break;
9274
0
      }
9275
0
  }
9276
0
    }
9277
9278
0
  return true;
9279
0
}
9280

9281
/* Allocate space for global sym dynamic relocs.  */
9282
9283
static bool
9284
allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9285
0
{
9286
0
  struct bfd_link_info *info = inf;
9287
0
  bfd *dynobj;
9288
0
  struct mips_elf_link_hash_entry *hmips;
9289
0
  struct mips_elf_link_hash_table *htab;
9290
9291
0
  htab = mips_elf_hash_table (info);
9292
0
  BFD_ASSERT (htab != NULL);
9293
9294
0
  dynobj = elf_hash_table (info)->dynobj;
9295
0
  hmips = (struct mips_elf_link_hash_entry *) h;
9296
9297
  /* VxWorks executables are handled elsewhere; we only need to
9298
     allocate relocations in shared objects.  */
9299
0
  if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9300
0
    return true;
9301
9302
  /* Ignore indirect symbols.  All relocations against such symbols
9303
     will be redirected to the target symbol.  */
9304
0
  if (h->root.type == bfd_link_hash_indirect)
9305
0
    return true;
9306
9307
  /* If this symbol is defined in a dynamic object, or we are creating
9308
     a shared library, we will need to copy any R_MIPS_32 or
9309
     R_MIPS_REL32 relocs against it into the output file.  */
9310
0
  if (! bfd_link_relocatable (info)
9311
0
      && hmips->possibly_dynamic_relocs != 0
9312
0
      && (h->root.type == bfd_link_hash_defweak
9313
0
    || (!h->def_regular && !ELF_COMMON_DEF_P (h))
9314
0
    || bfd_link_pic (info)))
9315
0
    {
9316
0
      bool do_copy = true;
9317
9318
0
      if (h->root.type == bfd_link_hash_undefweak)
9319
0
  {
9320
    /* Do not copy relocations for undefined weak symbols that
9321
       we are not going to export.  */
9322
0
    if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9323
0
      do_copy = false;
9324
9325
    /* Make sure undefined weak symbols are output as a dynamic
9326
       symbol in PIEs.  */
9327
0
    else if (h->dynindx == -1 && !h->forced_local)
9328
0
      {
9329
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
9330
0
    return false;
9331
0
      }
9332
0
  }
9333
9334
0
      if (do_copy)
9335
0
  {
9336
    /* Even though we don't directly need a GOT entry for this symbol,
9337
       the SVR4 psABI requires it to have a dynamic symbol table
9338
       index greater that DT_MIPS_GOTSYM if there are dynamic
9339
       relocations against it.
9340
9341
       VxWorks does not enforce the same mapping between the GOT
9342
       and the symbol table, so the same requirement does not
9343
       apply there.  */
9344
0
    if (htab->root.target_os != is_vxworks)
9345
0
      {
9346
0
        if (hmips->global_got_area > GGA_RELOC_ONLY)
9347
0
    hmips->global_got_area = GGA_RELOC_ONLY;
9348
0
        hmips->got_only_for_calls = false;
9349
0
      }
9350
9351
0
    mips_elf_allocate_dynamic_relocations
9352
0
      (dynobj, info, hmips->possibly_dynamic_relocs);
9353
0
    if (hmips->readonly_reloc)
9354
      /* We tell the dynamic linker that there are relocations
9355
         against the text segment.  */
9356
0
      info->flags |= DF_TEXTREL;
9357
0
  }
9358
0
    }
9359
9360
0
  return true;
9361
0
}
9362
9363
/* Adjust a symbol defined by a dynamic object and referenced by a
9364
   regular object.  The current definition is in some section of the
9365
   dynamic object, but we're not including those sections.  We have to
9366
   change the definition to something the rest of the link can
9367
   understand.  */
9368
9369
bool
9370
_bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
9371
             struct elf_link_hash_entry *h)
9372
0
{
9373
0
  bfd *dynobj;
9374
0
  struct mips_elf_link_hash_entry *hmips;
9375
0
  struct mips_elf_link_hash_table *htab;
9376
0
  asection *s, *srel;
9377
9378
0
  htab = mips_elf_hash_table (info);
9379
0
  BFD_ASSERT (htab != NULL);
9380
9381
0
  dynobj = elf_hash_table (info)->dynobj;
9382
0
  hmips = (struct mips_elf_link_hash_entry *) h;
9383
9384
  /* Make sure we know what is going on here.  */
9385
0
  if (dynobj == NULL
9386
0
      || (! h->needs_plt
9387
0
    && ! h->is_weakalias
9388
0
    && (! h->def_dynamic
9389
0
        || ! h->ref_regular
9390
0
        || h->def_regular)))
9391
0
    {
9392
0
      if (h->type == STT_GNU_IFUNC)
9393
0
  _bfd_error_handler (_("IFUNC symbol %s in dynamic symbol table - IFUNCS are not supported"),
9394
0
          h->root.root.string);
9395
0
      else
9396
0
  _bfd_error_handler (_("non-dynamic symbol %s in dynamic symbol table"),
9397
0
          h->root.root.string);
9398
0
      return true;
9399
0
    }
9400
9401
0
  hmips = (struct mips_elf_link_hash_entry *) h;
9402
9403
  /* If there are call relocations against an externally-defined symbol,
9404
     see whether we can create a MIPS lazy-binding stub for it.  We can
9405
     only do this if all references to the function are through call
9406
     relocations, and in that case, the traditional lazy-binding stubs
9407
     are much more efficient than PLT entries.
9408
9409
     Traditional stubs are only available on SVR4 psABI-based systems;
9410
     VxWorks always uses PLTs instead.  */
9411
0
  if (htab->root.target_os != is_vxworks
9412
0
      && h->needs_plt
9413
0
      && !hmips->no_fn_stub)
9414
0
    {
9415
0
      if (! elf_hash_table (info)->dynamic_sections_created)
9416
0
  return true;
9417
9418
      /* If this symbol is not defined in a regular file, then set
9419
   the symbol to the stub location.  This is required to make
9420
   function pointers compare as equal between the normal
9421
   executable and the shared library.  */
9422
0
      if (!h->def_regular
9423
0
    && !bfd_is_abs_section (htab->sstubs->output_section))
9424
0
  {
9425
0
    hmips->needs_lazy_stub = true;
9426
0
    htab->lazy_stub_count++;
9427
0
    return true;
9428
0
  }
9429
0
    }
9430
  /* As above, VxWorks requires PLT entries for externally-defined
9431
     functions that are only accessed through call relocations.
9432
9433
     Both VxWorks and non-VxWorks targets also need PLT entries if there
9434
     are static-only relocations against an externally-defined function.
9435
     This can technically occur for shared libraries if there are
9436
     branches to the symbol, although it is unlikely that this will be
9437
     used in practice due to the short ranges involved.  It can occur
9438
     for any relative or absolute relocation in executables; in that
9439
     case, the PLT entry becomes the function's canonical address.  */
9440
0
  else if (((h->needs_plt && !hmips->no_fn_stub)
9441
0
      || (h->type == STT_FUNC && hmips->has_static_relocs))
9442
0
     && htab->use_plts_and_copy_relocs
9443
0
     && !SYMBOL_CALLS_LOCAL (info, h)
9444
0
     && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
9445
0
    && h->root.type == bfd_link_hash_undefweak))
9446
0
    {
9447
0
      bool micromips_p = MICROMIPS_P (info->output_bfd);
9448
0
      bool newabi_p = NEWABI_P (info->output_bfd);
9449
9450
      /* If this is the first symbol to need a PLT entry, then make some
9451
   basic setup.  Also work out PLT entry sizes.  We'll need them
9452
   for PLT offset calculations.  */
9453
0
      if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
9454
0
  {
9455
0
    BFD_ASSERT (htab->root.sgotplt->size == 0);
9456
0
    BFD_ASSERT (htab->plt_got_index == 0);
9457
9458
    /* If we're using the PLT additions to the psABI, each PLT
9459
       entry is 16 bytes and the PLT0 entry is 32 bytes.
9460
       Encourage better cache usage by aligning.  We do this
9461
       lazily to avoid pessimizing traditional objects.  */
9462
0
    if (htab->root.target_os != is_vxworks
9463
0
        && !bfd_set_section_alignment (htab->root.splt, 5))
9464
0
      return false;
9465
9466
    /* Make sure that .got.plt is word-aligned.  We do this lazily
9467
       for the same reason as above.  */
9468
0
    if (!bfd_set_section_alignment (htab->root.sgotplt,
9469
0
            MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
9470
0
      return false;
9471
9472
    /* On non-VxWorks targets, the first two entries in .got.plt
9473
       are reserved.  */
9474
0
    if (htab->root.target_os != is_vxworks)
9475
0
      htab->plt_got_index
9476
0
        += (get_elf_backend_data (dynobj)->got_header_size
9477
0
      / MIPS_ELF_GOT_SIZE (dynobj));
9478
9479
    /* On VxWorks, also allocate room for the header's
9480
       .rela.plt.unloaded entries.  */
9481
0
    if (htab->root.target_os == is_vxworks
9482
0
        && !bfd_link_pic (info))
9483
0
      htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
9484
9485
    /* Now work out the sizes of individual PLT entries.  */
9486
0
    if (htab->root.target_os == is_vxworks
9487
0
        && bfd_link_pic (info))
9488
0
      htab->plt_mips_entry_size
9489
0
        = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
9490
0
    else if (htab->root.target_os == is_vxworks)
9491
0
      htab->plt_mips_entry_size
9492
0
        = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
9493
0
    else if (newabi_p)
9494
0
      htab->plt_mips_entry_size
9495
0
        = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9496
0
    else if (!micromips_p)
9497
0
      {
9498
0
        htab->plt_mips_entry_size
9499
0
    = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9500
0
        htab->plt_comp_entry_size
9501
0
    = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
9502
0
      }
9503
0
    else if (htab->insn32)
9504
0
      {
9505
0
        htab->plt_mips_entry_size
9506
0
    = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9507
0
        htab->plt_comp_entry_size
9508
0
    = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
9509
0
      }
9510
0
    else
9511
0
      {
9512
0
        htab->plt_mips_entry_size
9513
0
    = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9514
0
        htab->plt_comp_entry_size
9515
0
    = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
9516
0
      }
9517
0
  }
9518
9519
0
      if (h->plt.plist == NULL)
9520
0
  h->plt.plist = mips_elf_make_plt_record (dynobj);
9521
0
      if (h->plt.plist == NULL)
9522
0
  return false;
9523
9524
      /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
9525
   n32 or n64, so always use a standard entry there.
9526
9527
   If the symbol has a MIPS16 call stub and gets a PLT entry, then
9528
   all MIPS16 calls will go via that stub, and there is no benefit
9529
   to having a MIPS16 entry.  And in the case of call_stub a
9530
   standard entry actually has to be used as the stub ends with a J
9531
   instruction.  */
9532
0
      if (newabi_p
9533
0
    || htab->root.target_os == is_vxworks
9534
0
    || hmips->call_stub
9535
0
    || hmips->call_fp_stub)
9536
0
  {
9537
0
    h->plt.plist->need_mips = true;
9538
0
    h->plt.plist->need_comp = false;
9539
0
  }
9540
9541
      /* Otherwise, if there are no direct calls to the function, we
9542
   have a free choice of whether to use standard or compressed
9543
   entries.  Prefer microMIPS entries if the object is known to
9544
   contain microMIPS code, so that it becomes possible to create
9545
   pure microMIPS binaries.  Prefer standard entries otherwise,
9546
   because MIPS16 ones are no smaller and are usually slower.  */
9547
0
      if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
9548
0
  {
9549
0
    if (micromips_p)
9550
0
      h->plt.plist->need_comp = true;
9551
0
    else
9552
0
      h->plt.plist->need_mips = true;
9553
0
  }
9554
9555
0
      if (h->plt.plist->need_mips)
9556
0
  {
9557
0
    h->plt.plist->mips_offset = htab->plt_mips_offset;
9558
0
    htab->plt_mips_offset += htab->plt_mips_entry_size;
9559
0
  }
9560
0
      if (h->plt.plist->need_comp)
9561
0
  {
9562
0
    h->plt.plist->comp_offset = htab->plt_comp_offset;
9563
0
    htab->plt_comp_offset += htab->plt_comp_entry_size;
9564
0
  }
9565
9566
      /* Reserve the corresponding .got.plt entry now too.  */
9567
0
      h->plt.plist->gotplt_index = htab->plt_got_index++;
9568
9569
      /* If the output file has no definition of the symbol, set the
9570
   symbol's value to the address of the stub.  */
9571
0
      if (!bfd_link_pic (info) && !h->def_regular)
9572
0
  hmips->use_plt_entry = true;
9573
9574
      /* Make room for the R_MIPS_JUMP_SLOT relocation.  */
9575
0
      htab->root.srelplt->size += (htab->root.target_os == is_vxworks
9576
0
           ? MIPS_ELF_RELA_SIZE (dynobj)
9577
0
           : MIPS_ELF_REL_SIZE (dynobj));
9578
9579
      /* Make room for the .rela.plt.unloaded relocations.  */
9580
0
      if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9581
0
  htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
9582
9583
      /* All relocations against this symbol that could have been made
9584
   dynamic will now refer to the PLT entry instead.  */
9585
0
      hmips->possibly_dynamic_relocs = 0;
9586
9587
0
      return true;
9588
0
    }
9589
9590
  /* If this is a weak symbol, and there is a real definition, the
9591
     processor independent code will have arranged for us to see the
9592
     real definition first, and we can just use the same value.  */
9593
0
  if (h->is_weakalias)
9594
0
    {
9595
0
      struct elf_link_hash_entry *def = weakdef (h);
9596
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
9597
0
      h->root.u.def.section = def->root.u.def.section;
9598
0
      h->root.u.def.value = def->root.u.def.value;
9599
0
      return true;
9600
0
    }
9601
9602
  /* Otherwise, there is nothing further to do for symbols defined
9603
     in regular objects.  */
9604
0
  if (h->def_regular)
9605
0
    return true;
9606
9607
  /* There's also nothing more to do if we'll convert all relocations
9608
     against this symbol into dynamic relocations.  */
9609
0
  if (!hmips->has_static_relocs)
9610
0
    return true;
9611
9612
  /* We're now relying on copy relocations.  Complain if we have
9613
     some that we can't convert.  */
9614
0
  if (!htab->use_plts_and_copy_relocs || bfd_link_pic (info))
9615
0
    {
9616
0
      _bfd_error_handler (_("non-dynamic relocations refer to "
9617
0
          "dynamic symbol %s"),
9618
0
        h->root.root.string);
9619
0
      bfd_set_error (bfd_error_bad_value);
9620
0
      return false;
9621
0
    }
9622
9623
  /* We must allocate the symbol in our .dynbss section, which will
9624
     become part of the .bss section of the executable.  There will be
9625
     an entry for this symbol in the .dynsym section.  The dynamic
9626
     object will contain position independent code, so all references
9627
     from the dynamic object to this symbol will go through the global
9628
     offset table.  The dynamic linker will use the .dynsym entry to
9629
     determine the address it must put in the global offset table, so
9630
     both the dynamic object and the regular object will refer to the
9631
     same memory location for the variable.  */
9632
9633
0
  if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
9634
0
    {
9635
0
      s = htab->root.sdynrelro;
9636
0
      srel = htab->root.sreldynrelro;
9637
0
    }
9638
0
  else
9639
0
    {
9640
0
      s = htab->root.sdynbss;
9641
0
      srel = htab->root.srelbss;
9642
0
    }
9643
0
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
9644
0
    {
9645
0
      if (htab->root.target_os == is_vxworks)
9646
0
  srel->size += sizeof (Elf32_External_Rela);
9647
0
      else
9648
0
  mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9649
0
      h->needs_copy = 1;
9650
0
    }
9651
9652
  /* All relocations against this symbol that could have been made
9653
     dynamic will now refer to the local copy instead.  */
9654
0
  hmips->possibly_dynamic_relocs = 0;
9655
9656
0
  return _bfd_elf_adjust_dynamic_copy (info, h, s);
9657
0
}
9658

9659
/* If the link uses a GOT, lay it out and work out its size.  */
9660
9661
static bool
9662
mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9663
0
{
9664
0
  bfd *dynobj;
9665
0
  asection *s;
9666
0
  struct mips_got_info *g;
9667
0
  bfd_size_type loadable_size = 0;
9668
0
  bfd_size_type page_gotno;
9669
0
  bfd *ibfd;
9670
0
  struct mips_elf_traverse_got_arg tga;
9671
0
  struct mips_elf_link_hash_table *htab;
9672
9673
0
  htab = mips_elf_hash_table (info);
9674
0
  BFD_ASSERT (htab != NULL);
9675
9676
0
  s = htab->root.sgot;
9677
0
  if (s == NULL)
9678
0
    return true;
9679
9680
0
  dynobj = elf_hash_table (info)->dynobj;
9681
0
  g = htab->got_info;
9682
9683
  /* Allocate room for the reserved entries.  VxWorks always reserves
9684
     3 entries; other objects only reserve 2 entries.  */
9685
0
  BFD_ASSERT (g->assigned_low_gotno == 0);
9686
0
  if (htab->root.target_os == is_vxworks)
9687
0
    htab->reserved_gotno = 3;
9688
0
  else
9689
0
    htab->reserved_gotno = 2;
9690
0
  g->local_gotno += htab->reserved_gotno;
9691
0
  g->assigned_low_gotno = htab->reserved_gotno;
9692
9693
  /* Decide which symbols need to go in the global part of the GOT and
9694
     count the number of reloc-only GOT symbols.  */
9695
0
  mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
9696
9697
0
  if (!mips_elf_resolve_final_got_entries (info, g))
9698
0
    return false;
9699
9700
  /* Calculate the total loadable size of the output.  That
9701
     will give us the maximum number of GOT_PAGE entries
9702
     required.  */
9703
0
  for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9704
0
    {
9705
0
      asection *subsection;
9706
9707
0
      for (subsection = ibfd->sections;
9708
0
     subsection;
9709
0
     subsection = subsection->next)
9710
0
  {
9711
0
    if ((subsection->flags & SEC_ALLOC) == 0)
9712
0
      continue;
9713
0
    loadable_size += ((subsection->size + 0xf)
9714
0
          &~ (bfd_size_type) 0xf);
9715
0
  }
9716
0
    }
9717
9718
0
  if (htab->root.target_os == is_vxworks)
9719
    /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
9720
       relocations against local symbols evaluate to "G", and the EABI does
9721
       not include R_MIPS_GOT_PAGE.  */
9722
0
    page_gotno = 0;
9723
0
  else
9724
    /* Assume there are two loadable segments consisting of contiguous
9725
       sections.  Is 5 enough?  */
9726
0
    page_gotno = (loadable_size >> 16) + 5;
9727
9728
  /* Choose the smaller of the two page estimates; both are intended to be
9729
     conservative.  */
9730
0
  if (page_gotno > g->page_gotno)
9731
0
    page_gotno = g->page_gotno;
9732
9733
0
  g->local_gotno += page_gotno;
9734
0
  g->assigned_high_gotno = g->local_gotno - 1;
9735
9736
0
  s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9737
0
  s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9738
0
  s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9739
9740
  /* VxWorks does not support multiple GOTs.  It initializes $gp to
9741
     __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9742
     dynamic loader.  */
9743
0
  if (htab->root.target_os != is_vxworks
9744
0
      && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
9745
0
    {
9746
0
      if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
9747
0
  return false;
9748
0
    }
9749
0
  else
9750
0
    {
9751
      /* Record that all bfds use G.  This also has the effect of freeing
9752
   the per-bfd GOTs, which we no longer need.  */
9753
0
      for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9754
0
  if (mips_elf_bfd_got (ibfd, false))
9755
0
    mips_elf_replace_bfd_got (ibfd, g);
9756
0
      mips_elf_replace_bfd_got (output_bfd, g);
9757
9758
      /* Set up TLS entries.  */
9759
0
      g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
9760
0
      tga.info = info;
9761
0
      tga.g = g;
9762
0
      tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9763
0
      htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9764
0
      if (!tga.g)
9765
0
  return false;
9766
0
      BFD_ASSERT (g->tls_assigned_gotno
9767
0
      == g->global_gotno + g->local_gotno + g->tls_gotno);
9768
9769
      /* Each VxWorks GOT entry needs an explicit relocation.  */
9770
0
      if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
9771
0
  g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9772
9773
      /* Allocate room for the TLS relocations.  */
9774
0
      if (g->relocs)
9775
0
  mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
9776
0
    }
9777
9778
0
  return true;
9779
0
}
9780
9781
/* Estimate the size of the .MIPS.stubs section.  */
9782
9783
static void
9784
mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9785
0
{
9786
0
  struct mips_elf_link_hash_table *htab;
9787
0
  bfd_size_type dynsymcount;
9788
9789
0
  htab = mips_elf_hash_table (info);
9790
0
  BFD_ASSERT (htab != NULL);
9791
9792
0
  if (htab->lazy_stub_count == 0)
9793
0
    return;
9794
9795
  /* IRIX rld assumes that a function stub isn't at the end of the .text
9796
     section, so add a dummy entry to the end.  */
9797
0
  htab->lazy_stub_count++;
9798
9799
  /* Get a worst-case estimate of the number of dynamic symbols needed.
9800
     At this point, dynsymcount does not account for section symbols
9801
     and count_section_dynsyms may overestimate the number that will
9802
     be needed.  */
9803
0
  dynsymcount = (elf_hash_table (info)->dynsymcount
9804
0
     + count_section_dynsyms (output_bfd, info));
9805
9806
  /* Determine the size of one stub entry.  There's no disadvantage
9807
     from using microMIPS code here, so for the sake of pure-microMIPS
9808
     binaries we prefer it whenever there's any microMIPS code in
9809
     output produced at all.  This has a benefit of stubs being
9810
     shorter by 4 bytes each too, unless in the insn32 mode.  */
9811
0
  if (!MICROMIPS_P (output_bfd))
9812
0
    htab->function_stub_size = (dynsymcount > 0x10000
9813
0
        ? MIPS_FUNCTION_STUB_BIG_SIZE
9814
0
        : MIPS_FUNCTION_STUB_NORMAL_SIZE);
9815
0
  else if (htab->insn32)
9816
0
    htab->function_stub_size = (dynsymcount > 0x10000
9817
0
        ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9818
0
        : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9819
0
  else
9820
0
    htab->function_stub_size = (dynsymcount > 0x10000
9821
0
        ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9822
0
        : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
9823
9824
0
  htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9825
0
}
9826
9827
/* A mips_elf_link_hash_traverse callback for which DATA points to a
9828
   mips_htab_traverse_info.  If H needs a traditional MIPS lazy-binding
9829
   stub, allocate an entry in the stubs section.  */
9830
9831
static bool
9832
mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
9833
0
{
9834
0
  struct mips_htab_traverse_info *hti = data;
9835
0
  struct mips_elf_link_hash_table *htab;
9836
0
  struct bfd_link_info *info;
9837
0
  bfd *output_bfd;
9838
9839
0
  info = hti->info;
9840
0
  output_bfd = hti->output_bfd;
9841
0
  htab = mips_elf_hash_table (info);
9842
0
  BFD_ASSERT (htab != NULL);
9843
9844
0
  if (h->needs_lazy_stub)
9845
0
    {
9846
0
      bool micromips_p = MICROMIPS_P (output_bfd);
9847
0
      unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9848
0
      bfd_vma isa_bit = micromips_p;
9849
9850
0
      BFD_ASSERT (htab->root.dynobj != NULL);
9851
0
      if (h->root.plt.plist == NULL)
9852
0
  h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9853
0
      if (h->root.plt.plist == NULL)
9854
0
  {
9855
0
    hti->error = true;
9856
0
    return false;
9857
0
  }
9858
0
      h->root.root.u.def.section = htab->sstubs;
9859
0
      h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9860
0
      h->root.plt.plist->stub_offset = htab->sstubs->size;
9861
0
      h->root.other = other;
9862
0
      htab->sstubs->size += htab->function_stub_size;
9863
0
    }
9864
0
  return true;
9865
0
}
9866
9867
/* Allocate offsets in the stubs section to each symbol that needs one.
9868
   Set the final size of the .MIPS.stub section.  */
9869
9870
static bool
9871
mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9872
0
{
9873
0
  bfd *output_bfd = info->output_bfd;
9874
0
  bool micromips_p = MICROMIPS_P (output_bfd);
9875
0
  unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9876
0
  bfd_vma isa_bit = micromips_p;
9877
0
  struct mips_elf_link_hash_table *htab;
9878
0
  struct mips_htab_traverse_info hti;
9879
0
  struct elf_link_hash_entry *h;
9880
0
  bfd *dynobj;
9881
9882
0
  htab = mips_elf_hash_table (info);
9883
0
  BFD_ASSERT (htab != NULL);
9884
9885
0
  if (htab->lazy_stub_count == 0)
9886
0
    return true;
9887
9888
0
  htab->sstubs->size = 0;
9889
0
  hti.info = info;
9890
0
  hti.output_bfd = output_bfd;
9891
0
  hti.error = false;
9892
0
  mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9893
0
  if (hti.error)
9894
0
    return false;
9895
0
  htab->sstubs->size += htab->function_stub_size;
9896
0
  BFD_ASSERT (htab->sstubs->size
9897
0
        == htab->lazy_stub_count * htab->function_stub_size);
9898
9899
0
  dynobj = elf_hash_table (info)->dynobj;
9900
0
  BFD_ASSERT (dynobj != NULL);
9901
0
  h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9902
0
  if (h == NULL)
9903
0
    return false;
9904
0
  h->root.u.def.value = isa_bit;
9905
0
  h->other = other;
9906
0
  h->type = STT_FUNC;
9907
9908
0
  return true;
9909
0
}
9910
9911
/* A mips_elf_link_hash_traverse callback for which DATA points to a
9912
   bfd_link_info.  If H uses the address of a PLT entry as the value
9913
   of the symbol, then set the entry in the symbol table now.  Prefer
9914
   a standard MIPS PLT entry.  */
9915
9916
static bool
9917
mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9918
0
{
9919
0
  struct bfd_link_info *info = data;
9920
0
  bool micromips_p = MICROMIPS_P (info->output_bfd);
9921
0
  struct mips_elf_link_hash_table *htab;
9922
0
  unsigned int other;
9923
0
  bfd_vma isa_bit;
9924
0
  bfd_vma val;
9925
9926
0
  htab = mips_elf_hash_table (info);
9927
0
  BFD_ASSERT (htab != NULL);
9928
9929
0
  if (h->use_plt_entry)
9930
0
    {
9931
0
      BFD_ASSERT (h->root.plt.plist != NULL);
9932
0
      BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
9933
0
      || h->root.plt.plist->comp_offset != MINUS_ONE);
9934
9935
0
      val = htab->plt_header_size;
9936
0
      if (h->root.plt.plist->mips_offset != MINUS_ONE)
9937
0
  {
9938
0
    isa_bit = 0;
9939
0
    val += h->root.plt.plist->mips_offset;
9940
0
    other = 0;
9941
0
  }
9942
0
      else
9943
0
  {
9944
0
    isa_bit = 1;
9945
0
    val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
9946
0
    other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
9947
0
  }
9948
0
      val += isa_bit;
9949
      /* For VxWorks, point at the PLT load stub rather than the lazy
9950
   resolution stub; this stub will become the canonical function
9951
   address.  */
9952
0
      if (htab->root.target_os == is_vxworks)
9953
0
  val += 8;
9954
9955
0
      h->root.root.u.def.section = htab->root.splt;
9956
0
      h->root.root.u.def.value = val;
9957
0
      h->root.other = other;
9958
0
    }
9959
9960
0
  return true;
9961
0
}
9962
9963
/* Set the sizes of the dynamic sections, some mips non-dynamic sections,
9964
   and check for any mips16 stub sections that we can discard.  */
9965
9966
bool
9967
_bfd_mips_elf_late_size_sections (bfd *output_bfd,
9968
          struct bfd_link_info *info)
9969
0
{
9970
0
  bfd *dynobj;
9971
0
  asection *s, *sreldyn;
9972
0
  bool reltext;
9973
0
  struct mips_elf_link_hash_table *htab;
9974
0
  struct mips_htab_traverse_info hti;
9975
9976
0
  htab = mips_elf_hash_table (info);
9977
0
  BFD_ASSERT (htab != NULL);
9978
9979
  /* The .reginfo section has a fixed size.  */
9980
0
  s = bfd_get_section_by_name (output_bfd, ".reginfo");
9981
0
  if (s != NULL)
9982
0
    {
9983
0
      bfd_set_section_size (s, sizeof (Elf32_External_RegInfo));
9984
0
      s->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9985
0
    }
9986
9987
  /* The .MIPS.abiflags section has a fixed size.  */
9988
0
  s = bfd_get_section_by_name (output_bfd, ".MIPS.abiflags");
9989
0
  if (s != NULL)
9990
0
    {
9991
0
      bfd_set_section_size (s, sizeof (Elf_External_ABIFlags_v0));
9992
0
      s->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9993
0
    }
9994
9995
0
  hti.info = info;
9996
0
  hti.output_bfd = output_bfd;
9997
0
  hti.error = false;
9998
0
  mips_elf_link_hash_traverse (htab, mips_elf_check_symbols, &hti);
9999
0
  if (hti.error)
10000
0
    return false;
10001
10002
0
  dynobj = htab->root.dynobj;
10003
0
  if (dynobj == NULL)
10004
0
    return true;
10005
10006
0
  if (htab->root.dynamic_sections_created)
10007
0
    {
10008
      /* Set the contents of the .interp section to the interpreter.  */
10009
0
      if (bfd_link_executable (info) && !info->nointerp)
10010
0
  {
10011
0
    s = bfd_get_linker_section (dynobj, ".interp");
10012
0
    BFD_ASSERT (s != NULL);
10013
0
    s->size
10014
0
      = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
10015
0
    s->contents
10016
0
      = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
10017
0
  }
10018
10019
      /* Figure out the size of the PLT header if we know that we
10020
   are using it.  For the sake of cache alignment always use
10021
   a standard header whenever any standard entries are present
10022
   even if microMIPS entries are present as well.  This also
10023
   lets the microMIPS header rely on the value of $v0 only set
10024
   by microMIPS entries, for a small size reduction.
10025
10026
   Set symbol table entry values for symbols that use the
10027
   address of their PLT entry now that we can calculate it.
10028
10029
   Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
10030
   haven't already in _bfd_elf_create_dynamic_sections.  */
10031
0
      if (htab->root.splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
10032
0
  {
10033
0
    bool micromips_p = (MICROMIPS_P (output_bfd)
10034
0
             && !htab->plt_mips_offset);
10035
0
    unsigned int other = micromips_p ? STO_MICROMIPS : 0;
10036
0
    bfd_vma isa_bit = micromips_p;
10037
0
    struct elf_link_hash_entry *h;
10038
0
    bfd_vma size;
10039
10040
0
    BFD_ASSERT (htab->use_plts_and_copy_relocs);
10041
0
    BFD_ASSERT (htab->root.sgotplt->size == 0);
10042
0
    BFD_ASSERT (htab->root.splt->size == 0);
10043
10044
0
    if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
10045
0
      size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
10046
0
    else if (htab->root.target_os == is_vxworks)
10047
0
      size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
10048
0
    else if (ABI_64_P (output_bfd))
10049
0
      size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
10050
0
    else if (ABI_N32_P (output_bfd))
10051
0
      size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
10052
0
    else if (!micromips_p)
10053
0
      size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
10054
0
    else if (htab->insn32)
10055
0
      size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
10056
0
    else
10057
0
      size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
10058
10059
0
    htab->plt_header_is_comp = micromips_p;
10060
0
    htab->plt_header_size = size;
10061
0
    htab->root.splt->size = (size
10062
0
           + htab->plt_mips_offset
10063
0
           + htab->plt_comp_offset);
10064
0
    htab->root.sgotplt->size = (htab->plt_got_index
10065
0
              * MIPS_ELF_GOT_SIZE (dynobj));
10066
10067
0
    mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
10068
10069
0
    if (htab->root.hplt == NULL)
10070
0
      {
10071
0
        h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.splt,
10072
0
                 "_PROCEDURE_LINKAGE_TABLE_");
10073
0
        htab->root.hplt = h;
10074
0
        if (h == NULL)
10075
0
    return false;
10076
0
      }
10077
10078
0
    h = htab->root.hplt;
10079
0
    h->root.u.def.value = isa_bit;
10080
0
    h->other = other;
10081
0
    h->type = STT_FUNC;
10082
0
  }
10083
0
    }
10084
10085
  /* Allocate space for global sym dynamic relocs.  */
10086
0
  elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
10087
10088
0
  mips_elf_estimate_stub_size (output_bfd, info);
10089
10090
0
  if (!mips_elf_lay_out_got (output_bfd, info))
10091
0
    return false;
10092
10093
0
  mips_elf_lay_out_lazy_stubs (info);
10094
10095
  /* The check_relocs and adjust_dynamic_symbol entry points have
10096
     determined the sizes of the various dynamic sections.  Allocate
10097
     memory for them.  */
10098
0
  reltext = false;
10099
0
  for (s = dynobj->sections; s != NULL; s = s->next)
10100
0
    {
10101
0
      const char *name;
10102
10103
      /* It's OK to base decisions on the section name, because none
10104
   of the dynobj section names depend upon the input files.  */
10105
0
      name = bfd_section_name (s);
10106
10107
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
10108
0
  continue;
10109
10110
0
      if (startswith (name, ".rel"))
10111
0
  {
10112
0
    if (s->size != 0)
10113
0
      {
10114
0
        const char *outname;
10115
0
        asection *target;
10116
10117
        /* If this relocation section applies to a read only
10118
     section, then we probably need a DT_TEXTREL entry.
10119
     If the relocation section is .rel(a).dyn, we always
10120
     assert a DT_TEXTREL entry rather than testing whether
10121
     there exists a relocation to a read only section or
10122
     not.  */
10123
0
        outname = bfd_section_name (s->output_section);
10124
0
        target = bfd_get_section_by_name (output_bfd, outname + 4);
10125
0
        if ((target != NULL
10126
0
       && (target->flags & SEC_READONLY) != 0
10127
0
       && (target->flags & SEC_ALLOC) != 0)
10128
0
      || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
10129
0
    reltext = true;
10130
10131
        /* We use the reloc_count field as a counter if we need
10132
     to copy relocs into the output file.  */
10133
0
        if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
10134
0
    s->reloc_count = 0;
10135
10136
        /* If combreloc is enabled, elf_link_sort_relocs() will
10137
     sort relocations, but in a different way than we do,
10138
     and before we're done creating relocations.  Also, it
10139
     will move them around between input sections'
10140
     relocation's contents, so our sorting would be
10141
     broken, so don't let it run.  */
10142
0
        info->combreloc = 0;
10143
0
      }
10144
0
  }
10145
0
      else if (bfd_link_executable (info)
10146
0
         && !htab->use_rld_obj_head
10147
0
         && startswith (name, ".rld_map"))
10148
0
  {
10149
    /* We add a room for __rld_map.  It will be filled in by the
10150
       rtld to contain a pointer to the _r_debug structure.  */
10151
0
    s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
10152
0
  }
10153
0
      else if (SGI_COMPAT (output_bfd)
10154
0
         && startswith (name, ".compact_rel"))
10155
0
  s->size += htab->compact_rel_size;
10156
0
      else if (s == htab->root.splt)
10157
0
  {
10158
    /* If the last PLT entry has a branch delay slot, allocate
10159
       room for an extra nop to fill the delay slot.  This is
10160
       for CPUs without load interlocking.  */
10161
0
    if (! LOAD_INTERLOCKS_P (output_bfd)
10162
0
        && htab->root.target_os != is_vxworks
10163
0
        && s->size > 0)
10164
0
      s->size += 4;
10165
0
  }
10166
0
      else if (! startswith (name, ".init")
10167
0
         && s != htab->root.sgot
10168
0
         && s != htab->root.sgotplt
10169
0
         && s != htab->sstubs
10170
0
         && s != htab->root.sdynbss
10171
0
         && s != htab->root.sdynrelro)
10172
0
  {
10173
    /* It's not one of our sections, so don't allocate space.  */
10174
0
    continue;
10175
0
  }
10176
10177
0
      if (s->size == 0)
10178
0
  {
10179
0
    s->flags |= SEC_EXCLUDE;
10180
0
    continue;
10181
0
  }
10182
10183
0
      if ((s->flags & SEC_HAS_CONTENTS) == 0)
10184
0
  continue;
10185
10186
      /* Allocate memory for the section contents.  */
10187
0
      s->contents = bfd_zalloc (dynobj, s->size);
10188
0
      if (s->contents == NULL)
10189
0
  {
10190
0
    bfd_set_error (bfd_error_no_memory);
10191
0
    return false;
10192
0
  }
10193
0
    }
10194
10195
0
  if (htab->root.dynamic_sections_created)
10196
0
    {
10197
      /* Add some entries to the .dynamic section.  We fill in the
10198
   values later, in _bfd_mips_elf_finish_dynamic_sections, but we
10199
   must add the entries now so that we get the correct size for
10200
   the .dynamic section.  */
10201
10202
      /* SGI object has the equivalence of DT_DEBUG in the
10203
   DT_MIPS_RLD_MAP entry.  This must come first because glibc
10204
   only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
10205
   may only look at the first one they see.  */
10206
0
      if (!bfd_link_pic (info)
10207
0
    && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
10208
0
  return false;
10209
10210
0
      if (bfd_link_executable (info)
10211
0
    && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP_REL, 0))
10212
0
  return false;
10213
10214
      /* The DT_DEBUG entry may be filled in by the dynamic linker and
10215
   used by the debugger.  */
10216
0
      if (bfd_link_executable (info)
10217
0
    && !SGI_COMPAT (output_bfd)
10218
0
    && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
10219
0
  return false;
10220
10221
0
      if (reltext
10222
0
    && (SGI_COMPAT (output_bfd)
10223
0
        || htab->root.target_os == is_vxworks))
10224
0
  info->flags |= DF_TEXTREL;
10225
10226
0
      if ((info->flags & DF_TEXTREL) != 0)
10227
0
  {
10228
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
10229
0
      return false;
10230
10231
    /* Clear the DF_TEXTREL flag.  It will be set again if we
10232
       write out an actual text relocation; we may not, because
10233
       at this point we do not know whether e.g. any .eh_frame
10234
       absolute relocations have been converted to PC-relative.  */
10235
0
    info->flags &= ~DF_TEXTREL;
10236
0
  }
10237
10238
0
      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
10239
0
  return false;
10240
10241
0
      sreldyn = mips_elf_rel_dyn_section (info, false);
10242
0
      if (htab->root.target_os == is_vxworks)
10243
0
  {
10244
    /* VxWorks uses .rela.dyn instead of .rel.dyn.  It does not
10245
       use any of the DT_MIPS_* tags.  */
10246
0
    if (sreldyn && sreldyn->size > 0)
10247
0
      {
10248
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
10249
0
    return false;
10250
10251
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
10252
0
    return false;
10253
10254
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
10255
0
    return false;
10256
0
      }
10257
0
  }
10258
0
      else
10259
0
  {
10260
0
    if (sreldyn && sreldyn->size > 0
10261
0
        && !bfd_is_abs_section (sreldyn->output_section))
10262
0
      {
10263
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
10264
0
    return false;
10265
10266
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
10267
0
    return false;
10268
10269
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
10270
0
    return false;
10271
0
      }
10272
10273
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
10274
0
      return false;
10275
10276
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
10277
0
      return false;
10278
10279
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
10280
0
      return false;
10281
10282
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
10283
0
      return false;
10284
10285
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
10286
0
      return false;
10287
10288
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
10289
0
      return false;
10290
10291
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
10292
0
      return false;
10293
10294
0
    if (info->emit_gnu_hash
10295
0
        && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_XHASH, 0))
10296
0
      return false;
10297
10298
0
    if (IRIX_COMPAT (dynobj) == ict_irix5
10299
0
        && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
10300
0
      return false;
10301
10302
0
    if (IRIX_COMPAT (dynobj) == ict_irix6
10303
0
        && (bfd_get_section_by_name
10304
0
      (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
10305
0
        && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
10306
0
      return false;
10307
0
  }
10308
0
      if (htab->root.splt->size > 0)
10309
0
  {
10310
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
10311
0
      return false;
10312
10313
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
10314
0
      return false;
10315
10316
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
10317
0
      return false;
10318
10319
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
10320
0
      return false;
10321
0
  }
10322
0
      if (htab->root.target_os == is_vxworks
10323
0
    && !elf_vxworks_add_dynamic_entries (output_bfd, info))
10324
0
  return false;
10325
0
    }
10326
10327
0
  return true;
10328
0
}
10329

10330
/* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
10331
   Adjust its R_ADDEND field so that it is correct for the output file.
10332
   LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
10333
   and sections respectively; both use symbol indexes.  */
10334
10335
static void
10336
mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
10337
      bfd *input_bfd, Elf_Internal_Sym *local_syms,
10338
      asection **local_sections, Elf_Internal_Rela *rel)
10339
0
{
10340
0
  unsigned int r_type, r_symndx;
10341
0
  Elf_Internal_Sym *sym;
10342
0
  asection *sec;
10343
10344
0
  if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10345
0
    {
10346
0
      r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10347
0
      if (gprel16_reloc_p (r_type)
10348
0
    || r_type == R_MIPS_GPREL32
10349
0
    || literal_reloc_p (r_type))
10350
0
  {
10351
0
    rel->r_addend += _bfd_get_gp_value (input_bfd);
10352
0
    rel->r_addend -= _bfd_get_gp_value (output_bfd);
10353
0
  }
10354
10355
0
      r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
10356
0
      sym = local_syms + r_symndx;
10357
10358
      /* Adjust REL's addend to account for section merging.  */
10359
0
      if (!bfd_link_relocatable (info))
10360
0
  {
10361
0
    sec = local_sections[r_symndx];
10362
0
    _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
10363
0
  }
10364
10365
      /* This would normally be done by the rela_normal code in elflink.c.  */
10366
0
      if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
10367
0
  rel->r_addend += local_sections[r_symndx]->output_offset;
10368
0
    }
10369
0
}
10370
10371
/* Handle relocations against symbols from removed linkonce sections,
10372
   or sections discarded by a linker script.  We use this wrapper around
10373
   RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
10374
   on 64-bit ELF targets.  In this case for any relocation handled, which
10375
   always be the first in a triplet, the remaining two have to be processed
10376
   together with the first, even if they are R_MIPS_NONE.  It is the symbol
10377
   index referred by the first reloc that applies to all the three and the
10378
   remaining two never refer to an object symbol.  And it is the final
10379
   relocation (the last non-null one) that determines the output field of
10380
   the whole relocation so retrieve the corresponding howto structure for
10381
   the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
10382
10383
   Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
10384
   and therefore requires to be pasted in a loop.  It also defines a block
10385
   and does not protect any of its arguments, hence the extra brackets.  */
10386
10387
static void
10388
mips_reloc_against_discarded_section (bfd *output_bfd,
10389
              struct bfd_link_info *info,
10390
              bfd *input_bfd, asection *input_section,
10391
              Elf_Internal_Rela **rel,
10392
              const Elf_Internal_Rela **relend,
10393
              bool rel_reloc,
10394
              reloc_howto_type *howto,
10395
              bfd_byte *contents)
10396
0
{
10397
0
  const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10398
0
  int count = bed->s->int_rels_per_ext_rel;
10399
0
  unsigned int r_type;
10400
0
  int i;
10401
10402
0
  for (i = count - 1; i > 0; i--)
10403
0
    {
10404
0
      r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
10405
0
      if (r_type != R_MIPS_NONE)
10406
0
  {
10407
0
    howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10408
0
    break;
10409
0
  }
10410
0
    }
10411
0
  do
10412
0
    {
10413
0
       RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
10414
0
          (*rel), count, (*relend),
10415
0
          howto, i, contents);
10416
0
    }
10417
0
  while (0);
10418
0
}
10419
10420
/* Relocate a MIPS ELF section.  */
10421
10422
int
10423
_bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
10424
        bfd *input_bfd, asection *input_section,
10425
        bfd_byte *contents, Elf_Internal_Rela *relocs,
10426
        Elf_Internal_Sym *local_syms,
10427
        asection **local_sections)
10428
0
{
10429
0
  Elf_Internal_Rela *rel;
10430
0
  const Elf_Internal_Rela *relend;
10431
0
  bfd_vma addend = 0;
10432
0
  bool use_saved_addend_p = false;
10433
10434
0
  relend = relocs + input_section->reloc_count;
10435
0
  for (rel = relocs; rel < relend; ++rel)
10436
0
    {
10437
0
      const char *name;
10438
0
      bfd_vma value = 0;
10439
0
      reloc_howto_type *howto;
10440
0
      bool cross_mode_jump_p = false;
10441
      /* TRUE if the relocation is a RELA relocation, rather than a
10442
   REL relocation.  */
10443
0
      bool rela_relocation_p = true;
10444
0
      unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10445
0
      const char *msg;
10446
0
      unsigned long r_symndx;
10447
0
      asection *sec;
10448
0
      Elf_Internal_Shdr *symtab_hdr;
10449
0
      struct elf_link_hash_entry *h;
10450
0
      bool rel_reloc;
10451
10452
0
      rel_reloc = (NEWABI_P (input_bfd)
10453
0
       && mips_elf_rel_relocation_p (input_bfd, input_section,
10454
0
             relocs, rel));
10455
      /* Find the relocation howto for this relocation.  */
10456
0
      howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10457
10458
0
      r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
10459
0
      symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10460
0
      if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10461
0
  {
10462
0
    sec = local_sections[r_symndx];
10463
0
    h = NULL;
10464
0
  }
10465
0
      else
10466
0
  {
10467
0
    unsigned long extsymoff;
10468
10469
0
    extsymoff = 0;
10470
0
    if (!elf_bad_symtab (input_bfd))
10471
0
      extsymoff = symtab_hdr->sh_info;
10472
0
    h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
10473
0
    while (h->root.type == bfd_link_hash_indirect
10474
0
     || h->root.type == bfd_link_hash_warning)
10475
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
10476
10477
0
    sec = NULL;
10478
0
    if (h->root.type == bfd_link_hash_defined
10479
0
        || h->root.type == bfd_link_hash_defweak)
10480
0
      sec = h->root.u.def.section;
10481
0
  }
10482
10483
0
      if (sec != NULL && discarded_section (sec))
10484
0
  {
10485
0
    mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
10486
0
            input_section, &rel, &relend,
10487
0
            rel_reloc, howto, contents);
10488
0
    continue;
10489
0
  }
10490
10491
0
      if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
10492
0
  {
10493
    /* Some 32-bit code uses R_MIPS_64.  In particular, people use
10494
       64-bit code, but make sure all their addresses are in the
10495
       lowermost or uppermost 32-bit section of the 64-bit address
10496
       space.  Thus, when they use an R_MIPS_64 they mean what is
10497
       usually meant by R_MIPS_32, with the exception that the
10498
       stored value is sign-extended to 64 bits.  */
10499
0
    howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, false);
10500
10501
    /* On big-endian systems, we need to lie about the position
10502
       of the reloc.  */
10503
0
    if (bfd_big_endian (input_bfd))
10504
0
      rel->r_offset += 4;
10505
0
  }
10506
10507
0
      if (!use_saved_addend_p)
10508
0
  {
10509
    /* If these relocations were originally of the REL variety,
10510
       we must pull the addend out of the field that will be
10511
       relocated.  Otherwise, we simply use the contents of the
10512
       RELA relocation.  */
10513
0
    if (mips_elf_rel_relocation_p (input_bfd, input_section,
10514
0
           relocs, rel))
10515
0
      {
10516
0
        rela_relocation_p = false;
10517
0
        addend = mips_elf_read_rel_addend (input_bfd, input_section,
10518
0
             rel, howto, contents);
10519
0
        if (hi16_reloc_p (r_type)
10520
0
      || (got16_reloc_p (r_type)
10521
0
          && mips_elf_local_relocation_p (input_bfd, rel,
10522
0
                  local_sections)))
10523
0
    {
10524
0
      if (!mips_elf_add_lo16_rel_addend (input_bfd, input_section,
10525
0
                 rel, relend,
10526
0
                 contents, &addend))
10527
0
        {
10528
0
          if (h)
10529
0
      name = h->root.root.string;
10530
0
          else
10531
0
      name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10532
0
             local_syms + r_symndx,
10533
0
             sec);
10534
0
          _bfd_error_handler
10535
      /* xgettext:c-format */
10536
0
      (_("%pB: can't find matching LO16 reloc against `%s'"
10537
0
         " for %s at %#" PRIx64 " in section `%pA'"),
10538
0
       input_bfd, name,
10539
0
       howto->name, (uint64_t) rel->r_offset, input_section);
10540
0
        }
10541
0
    }
10542
0
        else
10543
0
    addend <<= howto->rightshift;
10544
0
      }
10545
0
    else
10546
0
      addend = rel->r_addend;
10547
0
    mips_elf_adjust_addend (output_bfd, info, input_bfd,
10548
0
          local_syms, local_sections, rel);
10549
0
  }
10550
10551
0
      if (bfd_link_relocatable (info))
10552
0
  {
10553
0
    if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
10554
0
        && bfd_big_endian (input_bfd))
10555
0
      rel->r_offset -= 4;
10556
10557
0
    if (!rela_relocation_p && rel->r_addend)
10558
0
      {
10559
0
        addend += rel->r_addend;
10560
0
        if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
10561
0
    addend = mips_elf_high (addend);
10562
0
        else if (r_type == R_MIPS_HIGHER)
10563
0
    addend = mips_elf_higher (addend);
10564
0
        else if (r_type == R_MIPS_HIGHEST)
10565
0
    addend = mips_elf_highest (addend);
10566
0
        else
10567
0
    addend >>= howto->rightshift;
10568
10569
        /* We use the source mask, rather than the destination
10570
     mask because the place to which we are writing will be
10571
     source of the addend in the final link.  */
10572
0
        addend &= howto->src_mask;
10573
10574
0
        if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10575
    /* See the comment above about using R_MIPS_64 in the 32-bit
10576
       ABI.  Here, we need to update the addend.  It would be
10577
       possible to get away with just using the R_MIPS_32 reloc
10578
       but for endianness.  */
10579
0
    {
10580
0
      bfd_vma sign_bits;
10581
0
      bfd_vma low_bits;
10582
0
      bfd_vma high_bits;
10583
10584
0
      if (addend & ((bfd_vma) 1 << 31))
10585
0
#ifdef BFD64
10586
0
        sign_bits = ((bfd_vma) 1 << 32) - 1;
10587
#else
10588
        sign_bits = -1;
10589
#endif
10590
0
      else
10591
0
        sign_bits = 0;
10592
10593
      /* If we don't know that we have a 64-bit type,
10594
         do two separate stores.  */
10595
0
      if (bfd_big_endian (input_bfd))
10596
0
        {
10597
          /* Store the sign-bits (which are most significant)
10598
       first.  */
10599
0
          low_bits = sign_bits;
10600
0
          high_bits = addend;
10601
0
        }
10602
0
      else
10603
0
        {
10604
0
          low_bits = addend;
10605
0
          high_bits = sign_bits;
10606
0
        }
10607
0
      bfd_put_32 (input_bfd, low_bits,
10608
0
            contents + rel->r_offset);
10609
0
      bfd_put_32 (input_bfd, high_bits,
10610
0
            contents + rel->r_offset + 4);
10611
0
      continue;
10612
0
    }
10613
10614
0
        if (! mips_elf_perform_relocation (info, howto, rel, addend,
10615
0
             input_bfd, input_section,
10616
0
             contents, false))
10617
0
    return false;
10618
0
      }
10619
10620
    /* Go on to the next relocation.  */
10621
0
    continue;
10622
0
  }
10623
10624
      /* In the N32 and 64-bit ABIs there may be multiple consecutive
10625
   relocations for the same offset.  In that case we are
10626
   supposed to treat the output of each relocation as the addend
10627
   for the next.  */
10628
0
      if (rel + 1 < relend
10629
0
    && rel->r_offset == rel[1].r_offset
10630
0
    && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
10631
0
  use_saved_addend_p = true;
10632
0
      else
10633
0
  use_saved_addend_p = false;
10634
10635
      /* Figure out what value we are supposed to relocate.  */
10636
0
      switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
10637
0
               input_section, contents,
10638
0
               info, rel, addend, howto,
10639
0
               local_syms, local_sections,
10640
0
               &value, &name, &cross_mode_jump_p,
10641
0
               use_saved_addend_p))
10642
0
  {
10643
0
  case bfd_reloc_continue:
10644
    /* There's nothing to do.  */
10645
0
    continue;
10646
10647
0
  case bfd_reloc_undefined:
10648
    /* mips_elf_calculate_relocation already called the
10649
       undefined_symbol callback.  There's no real point in
10650
       trying to perform the relocation at this point, so we
10651
       just skip ahead to the next relocation.  */
10652
0
    continue;
10653
10654
0
  case bfd_reloc_notsupported:
10655
0
    msg = _("internal error: unsupported relocation error");
10656
0
    info->callbacks->warning
10657
0
      (info, msg, name, input_bfd, input_section, rel->r_offset);
10658
0
    return false;
10659
10660
0
  case bfd_reloc_overflow:
10661
0
    if (use_saved_addend_p)
10662
      /* Ignore overflow until we reach the last relocation for
10663
         a given location.  */
10664
0
      ;
10665
0
    else
10666
0
      {
10667
0
        struct mips_elf_link_hash_table *htab;
10668
10669
0
        htab = mips_elf_hash_table (info);
10670
0
        BFD_ASSERT (htab != NULL);
10671
0
        BFD_ASSERT (name != NULL);
10672
0
        if (!htab->small_data_overflow_reported
10673
0
      && (gprel16_reloc_p (howto->type)
10674
0
          || literal_reloc_p (howto->type)))
10675
0
    {
10676
0
      msg = _("small-data section too large;"
10677
0
        " lower small-data size limit (see option -G)");
10678
10679
0
      htab->small_data_overflow_reported = true;
10680
0
      (*info->callbacks->einfo) ("%P: %s\n", msg);
10681
0
    }
10682
0
        (*info->callbacks->reloc_overflow)
10683
0
    (info, NULL, name, howto->name, (bfd_vma) 0,
10684
0
     input_bfd, input_section, rel->r_offset);
10685
0
      }
10686
0
    break;
10687
10688
0
  case bfd_reloc_ok:
10689
0
    break;
10690
10691
0
  case bfd_reloc_outofrange:
10692
0
    msg = NULL;
10693
0
    if (jal_reloc_p (howto->type))
10694
0
      msg = (cross_mode_jump_p
10695
0
       ? _("cannot convert a jump to JALX "
10696
0
           "for a non-word-aligned address")
10697
0
       : (howto->type == R_MIPS16_26
10698
0
          ? _("jump to a non-word-aligned address")
10699
0
          : _("jump to a non-instruction-aligned address")));
10700
0
    else if (b_reloc_p (howto->type))
10701
0
      msg = (cross_mode_jump_p
10702
0
       ? _("cannot convert a branch to JALX "
10703
0
           "for a non-word-aligned address")
10704
0
       : _("branch to a non-instruction-aligned address"));
10705
0
    else if (aligned_pcrel_reloc_p (howto->type))
10706
0
      msg = _("PC-relative load from unaligned address");
10707
0
    if (msg)
10708
0
      {
10709
0
        info->callbacks->einfo
10710
0
    ("%X%H: %s\n", input_bfd, input_section, rel->r_offset, msg);
10711
0
        break;
10712
0
      }
10713
    /* Fall through.  */
10714
10715
0
  default:
10716
0
    abort ();
10717
0
    break;
10718
0
  }
10719
10720
      /* If we've got another relocation for the address, keep going
10721
   until we reach the last one.  */
10722
0
      if (use_saved_addend_p)
10723
0
  {
10724
0
    addend = value;
10725
0
    continue;
10726
0
  }
10727
10728
0
      if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10729
  /* See the comment above about using R_MIPS_64 in the 32-bit
10730
     ABI.  Until now, we've been using the HOWTO for R_MIPS_32;
10731
     that calculated the right value.  Now, however, we
10732
     sign-extend the 32-bit result to 64-bits, and store it as a
10733
     64-bit value.  We are especially generous here in that we
10734
     go to extreme lengths to support this usage on systems with
10735
     only a 32-bit VMA.  */
10736
0
  {
10737
0
    bfd_vma sign_bits;
10738
0
    bfd_vma low_bits;
10739
0
    bfd_vma high_bits;
10740
10741
0
    if (value & ((bfd_vma) 1 << 31))
10742
0
#ifdef BFD64
10743
0
      sign_bits = ((bfd_vma) 1 << 32) - 1;
10744
#else
10745
      sign_bits = -1;
10746
#endif
10747
0
    else
10748
0
      sign_bits = 0;
10749
10750
    /* If we don't know that we have a 64-bit type,
10751
       do two separate stores.  */
10752
0
    if (bfd_big_endian (input_bfd))
10753
0
      {
10754
        /* Undo what we did above.  */
10755
0
        rel->r_offset -= 4;
10756
        /* Store the sign-bits (which are most significant)
10757
     first.  */
10758
0
        low_bits = sign_bits;
10759
0
        high_bits = value;
10760
0
      }
10761
0
    else
10762
0
      {
10763
0
        low_bits = value;
10764
0
        high_bits = sign_bits;
10765
0
      }
10766
0
    bfd_put_32 (input_bfd, low_bits,
10767
0
          contents + rel->r_offset);
10768
0
    bfd_put_32 (input_bfd, high_bits,
10769
0
          contents + rel->r_offset + 4);
10770
0
    continue;
10771
0
  }
10772
10773
      /* Actually perform the relocation.  */
10774
0
      if (! mips_elf_perform_relocation (info, howto, rel, value,
10775
0
           input_bfd, input_section,
10776
0
           contents, cross_mode_jump_p))
10777
0
  return false;
10778
0
    }
10779
10780
0
  return true;
10781
0
}
10782

10783
/* A function that iterates over each entry in la25_stubs and fills
10784
   in the code for each one.  DATA points to a mips_htab_traverse_info.  */
10785
10786
static int
10787
mips_elf_create_la25_stub (void **slot, void *data)
10788
0
{
10789
0
  struct mips_htab_traverse_info *hti;
10790
0
  struct mips_elf_link_hash_table *htab;
10791
0
  struct mips_elf_la25_stub *stub;
10792
0
  asection *s;
10793
0
  bfd_byte *loc;
10794
0
  bfd_vma offset, target, target_high, target_low;
10795
0
  bfd_vma branch_pc;
10796
0
  bfd_signed_vma pcrel_offset = 0;
10797
10798
0
  stub = (struct mips_elf_la25_stub *) *slot;
10799
0
  hti = (struct mips_htab_traverse_info *) data;
10800
0
  htab = mips_elf_hash_table (hti->info);
10801
0
  BFD_ASSERT (htab != NULL);
10802
10803
  /* Create the section contents, if we haven't already.  */
10804
0
  s = stub->stub_section;
10805
0
  loc = s->contents;
10806
0
  if (loc == NULL)
10807
0
    {
10808
0
      loc = bfd_malloc (s->size);
10809
0
      if (loc == NULL)
10810
0
  {
10811
0
    hti->error = true;
10812
0
    return false;
10813
0
  }
10814
0
      s->contents = loc;
10815
0
    }
10816
10817
  /* Work out where in the section this stub should go.  */
10818
0
  offset = stub->offset;
10819
10820
  /* We add 8 here to account for the LUI/ADDIU instructions
10821
     before the branch instruction.  This cannot be moved down to
10822
     where pcrel_offset is calculated as 's' is updated in
10823
     mips_elf_get_la25_target.  */
10824
0
  branch_pc = s->output_section->vma + s->output_offset + offset + 8;
10825
10826
  /* Work out the target address.  */
10827
0
  target = mips_elf_get_la25_target (stub, &s);
10828
0
  target += s->output_section->vma + s->output_offset;
10829
10830
0
  target_high = ((target + 0x8000) >> 16) & 0xffff;
10831
0
  target_low = (target & 0xffff);
10832
10833
  /* Calculate the PC of the compact branch instruction (for the case where
10834
     compact branches are used for either microMIPSR6 or MIPSR6 with
10835
     compact branches.  Add 4-bytes to account for BC using the PC of the
10836
     next instruction as the base.  */
10837
0
  pcrel_offset = target - (branch_pc + 4);
10838
10839
0
  if (stub->stub_section != htab->strampoline)
10840
0
    {
10841
      /* This is a simple LUI/ADDIU stub.  Zero out the beginning
10842
   of the section and write the two instructions at the end.  */
10843
0
      memset (loc, 0, offset);
10844
0
      loc += offset;
10845
0
      if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10846
0
  {
10847
0
    bfd_put_micromips_32 (hti->output_bfd,
10848
0
        LA25_LUI_MICROMIPS (target_high),
10849
0
        loc);
10850
0
    bfd_put_micromips_32 (hti->output_bfd,
10851
0
        LA25_ADDIU_MICROMIPS (target_low),
10852
0
        loc + 4);
10853
0
  }
10854
0
      else
10855
0
  {
10856
0
    bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10857
0
    bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10858
0
  }
10859
0
    }
10860
0
  else
10861
0
    {
10862
      /* This is trampoline.  */
10863
0
      loc += offset;
10864
0
      if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10865
0
  {
10866
0
    bfd_put_micromips_32 (hti->output_bfd,
10867
0
        LA25_LUI_MICROMIPS (target_high), loc);
10868
0
    bfd_put_micromips_32 (hti->output_bfd,
10869
0
        LA25_J_MICROMIPS (target), loc + 4);
10870
0
    bfd_put_micromips_32 (hti->output_bfd,
10871
0
        LA25_ADDIU_MICROMIPS (target_low), loc + 8);
10872
0
    bfd_put_32 (hti->output_bfd, 0, loc + 12);
10873
0
  }
10874
0
      else
10875
0
  {
10876
0
    bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10877
0
    if (MIPSR6_P (hti->output_bfd) && htab->compact_branches)
10878
0
      {
10879
0
        bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10880
0
        bfd_put_32 (hti->output_bfd, LA25_BC (pcrel_offset), loc + 8);
10881
0
      }
10882
0
    else
10883
0
      {
10884
0
        bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10885
0
        bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10886
0
      }
10887
0
    bfd_put_32 (hti->output_bfd, 0, loc + 12);
10888
0
  }
10889
0
    }
10890
0
  return true;
10891
0
}
10892
10893
/* If NAME is one of the special IRIX6 symbols defined by the linker,
10894
   adjust it appropriately now.  */
10895
10896
static void
10897
mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
10898
              const char *name, Elf_Internal_Sym *sym)
10899
0
{
10900
  /* The linker script takes care of providing names and values for
10901
     these, but we must place them into the right sections.  */
10902
0
  static const char* const text_section_symbols[] = {
10903
0
    "_ftext",
10904
0
    "_etext",
10905
0
    "__dso_displacement",
10906
0
    "__elf_header",
10907
0
    "__program_header_table",
10908
0
    NULL
10909
0
  };
10910
10911
0
  static const char* const data_section_symbols[] = {
10912
0
    "_fdata",
10913
0
    "_edata",
10914
0
    "_end",
10915
0
    "_fbss",
10916
0
    NULL
10917
0
  };
10918
10919
0
  const char* const *p;
10920
0
  int i;
10921
10922
0
  for (i = 0; i < 2; ++i)
10923
0
    for (p = (i == 0) ? text_section_symbols : data_section_symbols;
10924
0
   *p;
10925
0
   ++p)
10926
0
      if (strcmp (*p, name) == 0)
10927
0
  {
10928
    /* All of these symbols are given type STT_SECTION by the
10929
       IRIX6 linker.  */
10930
0
    sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10931
0
    sym->st_other = STO_PROTECTED;
10932
10933
    /* The IRIX linker puts these symbols in special sections.  */
10934
0
    if (i == 0)
10935
0
      sym->st_shndx = SHN_MIPS_TEXT;
10936
0
    else
10937
0
      sym->st_shndx = SHN_MIPS_DATA;
10938
10939
0
    break;
10940
0
  }
10941
0
}
10942
10943
/* Finish up dynamic symbol handling.  We set the contents of various
10944
   dynamic sections here.  */
10945
10946
bool
10947
_bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
10948
             struct bfd_link_info *info,
10949
             struct elf_link_hash_entry *h,
10950
             Elf_Internal_Sym *sym)
10951
0
{
10952
0
  bfd *dynobj;
10953
0
  asection *sgot;
10954
0
  struct mips_got_info *g, *gg;
10955
0
  const char *name;
10956
0
  int idx;
10957
0
  struct mips_elf_link_hash_table *htab;
10958
0
  struct mips_elf_link_hash_entry *hmips;
10959
10960
0
  htab = mips_elf_hash_table (info);
10961
0
  BFD_ASSERT (htab != NULL);
10962
0
  dynobj = elf_hash_table (info)->dynobj;
10963
0
  hmips = (struct mips_elf_link_hash_entry *) h;
10964
10965
0
  BFD_ASSERT (htab->root.target_os != is_vxworks);
10966
10967
0
  if (h->plt.plist != NULL
10968
0
      && (h->plt.plist->mips_offset != MINUS_ONE
10969
0
    || h->plt.plist->comp_offset != MINUS_ONE))
10970
0
    {
10971
      /* We've decided to create a PLT entry for this symbol.  */
10972
0
      bfd_byte *loc;
10973
0
      bfd_vma header_address, got_address;
10974
0
      bfd_vma got_address_high, got_address_low, load;
10975
0
      bfd_vma got_index;
10976
0
      bfd_vma isa_bit;
10977
10978
0
      got_index = h->plt.plist->gotplt_index;
10979
10980
0
      BFD_ASSERT (htab->use_plts_and_copy_relocs);
10981
0
      BFD_ASSERT (h->dynindx != -1);
10982
0
      BFD_ASSERT (htab->root.splt != NULL);
10983
0
      BFD_ASSERT (got_index != MINUS_ONE);
10984
0
      BFD_ASSERT (!h->def_regular);
10985
10986
      /* Calculate the address of the PLT header.  */
10987
0
      isa_bit = htab->plt_header_is_comp;
10988
0
      header_address = (htab->root.splt->output_section->vma
10989
0
      + htab->root.splt->output_offset + isa_bit);
10990
10991
      /* Calculate the address of the .got.plt entry.  */
10992
0
      got_address = (htab->root.sgotplt->output_section->vma
10993
0
         + htab->root.sgotplt->output_offset
10994
0
         + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10995
10996
0
      got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10997
0
      got_address_low = got_address & 0xffff;
10998
10999
      /* The PLT sequence is not safe for N64 if .got.plt entry's address
11000
   cannot be loaded in two instructions.  */
11001
0
      if (ABI_64_P (output_bfd)
11002
0
    && ((got_address + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
11003
0
  {
11004
0
    _bfd_error_handler
11005
      /* xgettext:c-format */
11006
0
      (_("%pB: `%pA' entry VMA of %#" PRIx64 " outside the 32-bit range "
11007
0
         "supported; consider using `-Ttext-segment=...'"),
11008
0
       output_bfd,
11009
0
       htab->root.sgotplt->output_section,
11010
0
       (int64_t) got_address);
11011
0
    bfd_set_error (bfd_error_no_error);
11012
0
    return false;
11013
0
  }
11014
11015
      /* Initially point the .got.plt entry at the PLT header.  */
11016
0
      loc = (htab->root.sgotplt->contents
11017
0
       + got_index * MIPS_ELF_GOT_SIZE (dynobj));
11018
0
      if (ABI_64_P (output_bfd))
11019
0
  bfd_put_64 (output_bfd, header_address, loc);
11020
0
      else
11021
0
  bfd_put_32 (output_bfd, header_address, loc);
11022
11023
      /* Now handle the PLT itself.  First the standard entry (the order
11024
   does not matter, we just have to pick one).  */
11025
0
      if (h->plt.plist->mips_offset != MINUS_ONE)
11026
0
  {
11027
0
    const bfd_vma *plt_entry;
11028
0
    bfd_vma plt_offset;
11029
11030
0
    plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11031
11032
0
    BFD_ASSERT (plt_offset <= htab->root.splt->size);
11033
11034
    /* Find out where the .plt entry should go.  */
11035
0
    loc = htab->root.splt->contents + plt_offset;
11036
11037
    /* Pick the load opcode.  */
11038
0
    load = MIPS_ELF_LOAD_WORD (output_bfd);
11039
11040
    /* Fill in the PLT entry itself.  */
11041
11042
0
    if (MIPSR6_P (output_bfd))
11043
0
      plt_entry = htab->compact_branches ? mipsr6_exec_plt_entry_compact
11044
0
                 : mipsr6_exec_plt_entry;
11045
0
    else
11046
0
      plt_entry = mips_exec_plt_entry;
11047
0
    bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
11048
0
    bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
11049
0
          loc + 4);
11050
11051
0
    if (! LOAD_INTERLOCKS_P (output_bfd)
11052
0
        || (MIPSR6_P (output_bfd) && htab->compact_branches))
11053
0
      {
11054
0
        bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
11055
0
        bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11056
0
      }
11057
0
    else
11058
0
      {
11059
0
        bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
11060
0
        bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
11061
0
        loc + 12);
11062
0
      }
11063
0
  }
11064
11065
      /* Now the compressed entry.  They come after any standard ones.  */
11066
0
      if (h->plt.plist->comp_offset != MINUS_ONE)
11067
0
  {
11068
0
    bfd_vma plt_offset;
11069
11070
0
    plt_offset = (htab->plt_header_size + htab->plt_mips_offset
11071
0
      + h->plt.plist->comp_offset);
11072
11073
0
    BFD_ASSERT (plt_offset <= htab->root.splt->size);
11074
11075
    /* Find out where the .plt entry should go.  */
11076
0
    loc = htab->root.splt->contents + plt_offset;
11077
11078
    /* Fill in the PLT entry itself.  */
11079
0
    if (!MICROMIPS_P (output_bfd))
11080
0
      {
11081
0
        const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
11082
11083
0
        bfd_put_16 (output_bfd, plt_entry[0], loc);
11084
0
        bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
11085
0
        bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11086
0
        bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11087
0
        bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11088
0
        bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11089
0
        bfd_put_32 (output_bfd, got_address, loc + 12);
11090
0
      }
11091
0
    else if (htab->insn32)
11092
0
      {
11093
0
        const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
11094
11095
0
        bfd_put_16 (output_bfd, plt_entry[0], loc);
11096
0
        bfd_put_16 (output_bfd, got_address_high, loc + 2);
11097
0
        bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11098
0
        bfd_put_16 (output_bfd, got_address_low, loc + 6);
11099
0
        bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11100
0
        bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11101
0
        bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
11102
0
        bfd_put_16 (output_bfd, got_address_low, loc + 14);
11103
0
      }
11104
0
    else
11105
0
      {
11106
0
        const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
11107
0
        bfd_signed_vma gotpc_offset;
11108
0
        bfd_vma loc_address;
11109
11110
0
        BFD_ASSERT (got_address % 4 == 0);
11111
11112
0
        loc_address = (htab->root.splt->output_section->vma
11113
0
           + htab->root.splt->output_offset + plt_offset);
11114
0
        gotpc_offset = got_address - ((loc_address | 3) ^ 3);
11115
11116
        /* ADDIUPC has a span of +/-16MB, check we're in range.  */
11117
0
        if (gotpc_offset + 0x1000000 >= 0x2000000)
11118
0
    {
11119
0
      _bfd_error_handler
11120
        /* xgettext:c-format */
11121
0
        (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11122
0
           "beyond the range of ADDIUPC"),
11123
0
         output_bfd,
11124
0
         htab->root.sgotplt->output_section,
11125
0
         (int64_t) gotpc_offset,
11126
0
         htab->root.splt->output_section);
11127
0
      bfd_set_error (bfd_error_no_error);
11128
0
      return false;
11129
0
    }
11130
0
        bfd_put_16 (output_bfd,
11131
0
        plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11132
0
        bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11133
0
        bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11134
0
        bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11135
0
        bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11136
0
        bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11137
0
      }
11138
0
  }
11139
11140
      /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
11141
0
      mips_elf_output_dynamic_relocation (output_bfd, htab->root.srelplt,
11142
0
            got_index - 2, h->dynindx,
11143
0
            R_MIPS_JUMP_SLOT, got_address);
11144
11145
      /* We distinguish between PLT entries and lazy-binding stubs by
11146
   giving the former an st_other value of STO_MIPS_PLT.  Set the
11147
   flag and leave the value if there are any relocations in the
11148
   binary where pointer equality matters.  */
11149
0
      sym->st_shndx = SHN_UNDEF;
11150
0
      if (h->pointer_equality_needed)
11151
0
  sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
11152
0
      else
11153
0
  {
11154
0
    sym->st_value = 0;
11155
0
    sym->st_other = 0;
11156
0
  }
11157
0
    }
11158
11159
0
  if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
11160
0
    {
11161
      /* We've decided to create a lazy-binding stub.  */
11162
0
      bool micromips_p = MICROMIPS_P (output_bfd);
11163
0
      unsigned int other = micromips_p ? STO_MICROMIPS : 0;
11164
0
      bfd_vma stub_size = htab->function_stub_size;
11165
0
      bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
11166
0
      bfd_vma isa_bit = micromips_p;
11167
0
      bfd_vma stub_big_size;
11168
11169
0
      if (!micromips_p)
11170
0
  stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
11171
0
      else if (htab->insn32)
11172
0
  stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
11173
0
      else
11174
0
  stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
11175
11176
      /* This symbol has a stub.  Set it up.  */
11177
11178
0
      BFD_ASSERT (h->dynindx != -1);
11179
11180
0
      BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
11181
11182
      /* Values up to 2^31 - 1 are allowed.  Larger values would cause
11183
   sign extension at runtime in the stub, resulting in a negative
11184
   index value.  */
11185
0
      if (h->dynindx & ~0x7fffffff)
11186
0
  {
11187
0
    _bfd_error_handler
11188
0
      (_("%pB: cannot handle more than %d dynamic symbols"),
11189
0
       output_bfd, 0x7fffffff);
11190
0
    bfd_set_error (bfd_error_bad_value);
11191
0
    return false;
11192
0
  }
11193
11194
      /* Fill the stub.  */
11195
0
      if (micromips_p)
11196
0
  {
11197
0
    idx = 0;
11198
0
    bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
11199
0
        stub + idx);
11200
0
    idx += 4;
11201
0
    if (htab->insn32)
11202
0
      {
11203
0
        bfd_put_micromips_32 (output_bfd,
11204
0
            STUB_MOVE32_MICROMIPS, stub + idx);
11205
0
        idx += 4;
11206
0
      }
11207
0
    else
11208
0
      {
11209
0
        bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
11210
0
        idx += 2;
11211
0
      }
11212
0
    if (stub_size == stub_big_size)
11213
0
      {
11214
0
        long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
11215
11216
0
        bfd_put_micromips_32 (output_bfd,
11217
0
            STUB_LUI_MICROMIPS (dynindx_hi),
11218
0
            stub + idx);
11219
0
        idx += 4;
11220
0
      }
11221
0
    if (htab->insn32)
11222
0
      {
11223
0
        bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
11224
0
            stub + idx);
11225
0
        idx += 4;
11226
0
      }
11227
0
    else
11228
0
      {
11229
0
        bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
11230
0
        idx += 2;
11231
0
      }
11232
11233
    /* If a large stub is not required and sign extension is not a
11234
       problem, then use legacy code in the stub.  */
11235
0
    if (stub_size == stub_big_size)
11236
0
      bfd_put_micromips_32 (output_bfd,
11237
0
          STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
11238
0
          stub + idx);
11239
0
    else if (h->dynindx & ~0x7fff)
11240
0
      bfd_put_micromips_32 (output_bfd,
11241
0
          STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
11242
0
          stub + idx);
11243
0
    else
11244
0
      bfd_put_micromips_32 (output_bfd,
11245
0
          STUB_LI16S_MICROMIPS (output_bfd,
11246
0
              h->dynindx),
11247
0
          stub + idx);
11248
0
  }
11249
0
      else
11250
0
  {
11251
0
    idx = 0;
11252
0
    bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
11253
0
    idx += 4;
11254
0
    bfd_put_32 (output_bfd, STUB_MOVE, stub + idx);
11255
0
    idx += 4;
11256
0
    if (stub_size == stub_big_size)
11257
0
      {
11258
0
        bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
11259
0
        stub + idx);
11260
0
        idx += 4;
11261
0
      }
11262
11263
0
    if (!(MIPSR6_P (output_bfd) && htab->compact_branches))
11264
0
      {
11265
0
        bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
11266
0
        idx += 4;
11267
0
      }
11268
11269
    /* If a large stub is not required and sign extension is not a
11270
       problem, then use legacy code in the stub.  */
11271
0
    if (stub_size == stub_big_size)
11272
0
      bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
11273
0
      stub + idx);
11274
0
    else if (h->dynindx & ~0x7fff)
11275
0
      bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
11276
0
      stub + idx);
11277
0
    else
11278
0
      bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
11279
0
      stub + idx);
11280
0
    idx += 4;
11281
11282
0
    if (MIPSR6_P (output_bfd) && htab->compact_branches)
11283
0
      bfd_put_32 (output_bfd, STUB_JALRC, stub + idx);
11284
0
  }
11285
11286
0
      BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
11287
0
      memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
11288
0
        stub, stub_size);
11289
11290
      /* Mark the symbol as undefined.  stub_offset != -1 occurs
11291
   only for the referenced symbol.  */
11292
0
      sym->st_shndx = SHN_UNDEF;
11293
11294
      /* The run-time linker uses the st_value field of the symbol
11295
   to reset the global offset table entry for this external
11296
   to its stub address when unlinking a shared object.  */
11297
0
      sym->st_value = (htab->sstubs->output_section->vma
11298
0
           + htab->sstubs->output_offset
11299
0
           + h->plt.plist->stub_offset
11300
0
           + isa_bit);
11301
0
      sym->st_other = other;
11302
0
    }
11303
11304
  /* If we have a MIPS16 function with a stub, the dynamic symbol must
11305
     refer to the stub, since only the stub uses the standard calling
11306
     conventions.  */
11307
0
  if (h->dynindx != -1 && hmips->fn_stub != NULL)
11308
0
    {
11309
0
      BFD_ASSERT (hmips->need_fn_stub);
11310
0
      sym->st_value = (hmips->fn_stub->output_section->vma
11311
0
           + hmips->fn_stub->output_offset);
11312
0
      sym->st_size = hmips->fn_stub->size;
11313
0
      sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
11314
0
    }
11315
11316
0
  BFD_ASSERT (h->dynindx != -1
11317
0
        || h->forced_local);
11318
11319
0
  sgot = htab->root.sgot;
11320
0
  g = htab->got_info;
11321
0
  BFD_ASSERT (g != NULL);
11322
11323
  /* Run through the global symbol table, creating GOT entries for all
11324
     the symbols that need them.  */
11325
0
  if (hmips->global_got_area != GGA_NONE)
11326
0
    {
11327
0
      bfd_vma offset;
11328
0
      bfd_vma value;
11329
11330
0
      value = sym->st_value;
11331
0
      offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11332
0
      MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
11333
0
    }
11334
11335
0
  if (hmips->global_got_area != GGA_NONE && g->next)
11336
0
    {
11337
0
      struct mips_got_entry e, *p;
11338
0
      bfd_vma entry;
11339
0
      bfd_vma offset;
11340
11341
0
      gg = g;
11342
11343
0
      e.abfd = output_bfd;
11344
0
      e.symndx = -1;
11345
0
      e.d.h = hmips;
11346
0
      e.tls_type = GOT_TLS_NONE;
11347
11348
0
      for (g = g->next; g->next != gg; g = g->next)
11349
0
  {
11350
0
    if (g->got_entries
11351
0
        && (p = (struct mips_got_entry *) htab_find (g->got_entries,
11352
0
                 &e)))
11353
0
      {
11354
0
        offset = p->gotidx;
11355
0
        BFD_ASSERT (offset > 0 && offset < htab->root.sgot->size);
11356
0
        if (bfd_link_pic (info)
11357
0
      || (elf_hash_table (info)->dynamic_sections_created
11358
0
          && p->d.h != NULL
11359
0
          && p->d.h->root.def_dynamic
11360
0
          && !p->d.h->root.def_regular))
11361
0
    {
11362
      /* Create an R_MIPS_REL32 relocation for this entry.  Due to
11363
         the various compatibility problems, it's easier to mock
11364
         up an R_MIPS_32 or R_MIPS_64 relocation and leave
11365
         mips_elf_create_dynamic_relocation to calculate the
11366
         appropriate addend.  */
11367
0
      Elf_Internal_Rela rel[3];
11368
11369
0
      memset (rel, 0, sizeof (rel));
11370
0
      if (ABI_64_P (output_bfd))
11371
0
        rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
11372
0
      else
11373
0
        rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
11374
0
      rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
11375
11376
0
      entry = 0;
11377
0
      if (! (mips_elf_create_dynamic_relocation
11378
0
       (output_bfd, info, rel,
11379
0
        e.d.h, NULL, sym->st_value, &entry, sgot)))
11380
0
        return false;
11381
0
    }
11382
0
        else
11383
0
    entry = sym->st_value;
11384
0
        MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
11385
0
      }
11386
0
  }
11387
0
    }
11388
11389
  /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
11390
0
  name = h->root.root.string;
11391
0
  if (h == elf_hash_table (info)->hdynamic
11392
0
      || h == elf_hash_table (info)->hgot)
11393
0
    sym->st_shndx = SHN_ABS;
11394
0
  else if (strcmp (name, "_DYNAMIC_LINK") == 0
11395
0
     || strcmp (name, "_DYNAMIC_LINKING") == 0)
11396
0
    {
11397
0
      sym->st_shndx = SHN_ABS;
11398
0
      sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11399
0
      sym->st_value = 1;
11400
0
    }
11401
0
  else if (SGI_COMPAT (output_bfd))
11402
0
    {
11403
0
      if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
11404
0
    || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
11405
0
  {
11406
0
    sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11407
0
    sym->st_other = STO_PROTECTED;
11408
0
    sym->st_value = 0;
11409
0
    sym->st_shndx = SHN_MIPS_DATA;
11410
0
  }
11411
0
      else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
11412
0
  {
11413
0
    sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11414
0
    sym->st_other = STO_PROTECTED;
11415
0
    sym->st_value = mips_elf_hash_table (info)->procedure_count;
11416
0
    sym->st_shndx = SHN_ABS;
11417
0
  }
11418
0
      else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
11419
0
  {
11420
0
    if (h->type == STT_FUNC)
11421
0
      sym->st_shndx = SHN_MIPS_TEXT;
11422
0
    else if (h->type == STT_OBJECT)
11423
0
      sym->st_shndx = SHN_MIPS_DATA;
11424
0
  }
11425
0
    }
11426
11427
  /* Emit a copy reloc, if needed.  */
11428
0
  if (h->needs_copy)
11429
0
    {
11430
0
      asection *s;
11431
0
      bfd_vma symval;
11432
11433
0
      BFD_ASSERT (h->dynindx != -1);
11434
0
      BFD_ASSERT (htab->use_plts_and_copy_relocs);
11435
11436
0
      s = mips_elf_rel_dyn_section (info, false);
11437
0
      symval = (h->root.u.def.section->output_section->vma
11438
0
    + h->root.u.def.section->output_offset
11439
0
    + h->root.u.def.value);
11440
0
      mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
11441
0
            h->dynindx, R_MIPS_COPY, symval);
11442
0
    }
11443
11444
  /* Handle the IRIX6-specific symbols.  */
11445
0
  if (IRIX_COMPAT (output_bfd) == ict_irix6)
11446
0
    mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
11447
11448
  /* Keep dynamic compressed symbols odd.  This allows the dynamic linker
11449
     to treat compressed symbols like any other.  */
11450
0
  if (ELF_ST_IS_MIPS16 (sym->st_other))
11451
0
    {
11452
0
      BFD_ASSERT (sym->st_value & 1);
11453
0
      sym->st_other -= STO_MIPS16;
11454
0
    }
11455
0
  else if (ELF_ST_IS_MICROMIPS (sym->st_other))
11456
0
    {
11457
0
      BFD_ASSERT (sym->st_value & 1);
11458
0
      sym->st_other -= STO_MICROMIPS;
11459
0
    }
11460
11461
0
  return true;
11462
0
}
11463
11464
/* Likewise, for VxWorks.  */
11465
11466
bool
11467
_bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
11468
           struct bfd_link_info *info,
11469
           struct elf_link_hash_entry *h,
11470
           Elf_Internal_Sym *sym)
11471
0
{
11472
0
  bfd *dynobj;
11473
0
  asection *sgot;
11474
0
  struct mips_got_info *g;
11475
0
  struct mips_elf_link_hash_table *htab;
11476
0
  struct mips_elf_link_hash_entry *hmips;
11477
11478
0
  htab = mips_elf_hash_table (info);
11479
0
  BFD_ASSERT (htab != NULL);
11480
0
  dynobj = elf_hash_table (info)->dynobj;
11481
0
  hmips = (struct mips_elf_link_hash_entry *) h;
11482
11483
0
  if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
11484
0
    {
11485
0
      bfd_byte *loc;
11486
0
      bfd_vma plt_address, got_address, got_offset, branch_offset;
11487
0
      Elf_Internal_Rela rel;
11488
0
      static const bfd_vma *plt_entry;
11489
0
      bfd_vma gotplt_index;
11490
0
      bfd_vma plt_offset;
11491
11492
0
      plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11493
0
      gotplt_index = h->plt.plist->gotplt_index;
11494
11495
0
      BFD_ASSERT (h->dynindx != -1);
11496
0
      BFD_ASSERT (htab->root.splt != NULL);
11497
0
      BFD_ASSERT (gotplt_index != MINUS_ONE);
11498
0
      BFD_ASSERT (plt_offset <= htab->root.splt->size);
11499
11500
      /* Calculate the address of the .plt entry.  */
11501
0
      plt_address = (htab->root.splt->output_section->vma
11502
0
         + htab->root.splt->output_offset
11503
0
         + plt_offset);
11504
11505
      /* Calculate the address of the .got.plt entry.  */
11506
0
      got_address = (htab->root.sgotplt->output_section->vma
11507
0
         + htab->root.sgotplt->output_offset
11508
0
         + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
11509
11510
      /* Calculate the offset of the .got.plt entry from
11511
   _GLOBAL_OFFSET_TABLE_.  */
11512
0
      got_offset = mips_elf_gotplt_index (info, h);
11513
11514
      /* Calculate the offset for the branch at the start of the PLT
11515
   entry.  The branch jumps to the beginning of .plt.  */
11516
0
      branch_offset = -(plt_offset / 4 + 1) & 0xffff;
11517
11518
      /* Fill in the initial value of the .got.plt entry.  */
11519
0
      bfd_put_32 (output_bfd, plt_address,
11520
0
      (htab->root.sgotplt->contents
11521
0
       + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
11522
11523
      /* Find out where the .plt entry should go.  */
11524
0
      loc = htab->root.splt->contents + plt_offset;
11525
11526
0
      if (bfd_link_pic (info))
11527
0
  {
11528
0
    plt_entry = mips_vxworks_shared_plt_entry;
11529
0
    bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11530
0
    bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11531
0
  }
11532
0
      else
11533
0
  {
11534
0
    bfd_vma got_address_high, got_address_low;
11535
11536
0
    plt_entry = mips_vxworks_exec_plt_entry;
11537
0
    got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
11538
0
    got_address_low = got_address & 0xffff;
11539
11540
0
    bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11541
0
    bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11542
0
    bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
11543
0
    bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
11544
0
    bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11545
0
    bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11546
0
    bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11547
0
    bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11548
11549
0
    loc = (htab->srelplt2->contents
11550
0
     + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
11551
11552
    /* Emit a relocation for the .got.plt entry.  */
11553
0
    rel.r_offset = got_address;
11554
0
    rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11555
0
    rel.r_addend = plt_offset;
11556
0
    bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11557
11558
    /* Emit a relocation for the lui of %hi(<.got.plt slot>).  */
11559
0
    loc += sizeof (Elf32_External_Rela);
11560
0
    rel.r_offset = plt_address + 8;
11561
0
    rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11562
0
    rel.r_addend = got_offset;
11563
0
    bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11564
11565
    /* Emit a relocation for the addiu of %lo(<.got.plt slot>).  */
11566
0
    loc += sizeof (Elf32_External_Rela);
11567
0
    rel.r_offset += 4;
11568
0
    rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11569
0
    bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11570
0
  }
11571
11572
      /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
11573
0
      loc = (htab->root.srelplt->contents
11574
0
       + gotplt_index * sizeof (Elf32_External_Rela));
11575
0
      rel.r_offset = got_address;
11576
0
      rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
11577
0
      rel.r_addend = 0;
11578
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11579
11580
0
      if (!h->def_regular)
11581
0
  sym->st_shndx = SHN_UNDEF;
11582
0
    }
11583
11584
0
  BFD_ASSERT (h->dynindx != -1 || h->forced_local);
11585
11586
0
  sgot = htab->root.sgot;
11587
0
  g = htab->got_info;
11588
0
  BFD_ASSERT (g != NULL);
11589
11590
  /* See if this symbol has an entry in the GOT.  */
11591
0
  if (hmips->global_got_area != GGA_NONE)
11592
0
    {
11593
0
      bfd_vma offset;
11594
0
      Elf_Internal_Rela outrel;
11595
0
      bfd_byte *loc;
11596
0
      asection *s;
11597
11598
      /* Install the symbol value in the GOT.   */
11599
0
      offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11600
0
      MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
11601
11602
      /* Add a dynamic relocation for it.  */
11603
0
      s = mips_elf_rel_dyn_section (info, false);
11604
0
      loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
11605
0
      outrel.r_offset = (sgot->output_section->vma
11606
0
       + sgot->output_offset
11607
0
       + offset);
11608
0
      outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
11609
0
      outrel.r_addend = 0;
11610
0
      bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
11611
0
    }
11612
11613
  /* Emit a copy reloc, if needed.  */
11614
0
  if (h->needs_copy)
11615
0
    {
11616
0
      Elf_Internal_Rela rel;
11617
0
      asection *srel;
11618
0
      bfd_byte *loc;
11619
11620
0
      BFD_ASSERT (h->dynindx != -1);
11621
11622
0
      rel.r_offset = (h->root.u.def.section->output_section->vma
11623
0
          + h->root.u.def.section->output_offset
11624
0
          + h->root.u.def.value);
11625
0
      rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
11626
0
      rel.r_addend = 0;
11627
0
      if (h->root.u.def.section == htab->root.sdynrelro)
11628
0
  srel = htab->root.sreldynrelro;
11629
0
      else
11630
0
  srel = htab->root.srelbss;
11631
0
      loc = srel->contents + srel->reloc_count * sizeof (Elf32_External_Rela);
11632
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11633
0
      ++srel->reloc_count;
11634
0
    }
11635
11636
  /* If this is a mips16/microMIPS symbol, force the value to be even.  */
11637
0
  if (ELF_ST_IS_COMPRESSED (sym->st_other))
11638
0
    sym->st_value &= ~1;
11639
11640
0
  return true;
11641
0
}
11642
11643
/* Write out a plt0 entry to the beginning of .plt.  */
11644
11645
static bool
11646
mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11647
0
{
11648
0
  bfd_byte *loc;
11649
0
  bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
11650
0
  static const bfd_vma *plt_entry;
11651
0
  struct mips_elf_link_hash_table *htab;
11652
11653
0
  htab = mips_elf_hash_table (info);
11654
0
  BFD_ASSERT (htab != NULL);
11655
11656
0
  if (ABI_64_P (output_bfd))
11657
0
    plt_entry = (htab->compact_branches
11658
0
     ? mipsr6_n64_exec_plt0_entry_compact
11659
0
     : mips_n64_exec_plt0_entry);
11660
0
  else if (ABI_N32_P (output_bfd))
11661
0
    plt_entry = (htab->compact_branches
11662
0
     ? mipsr6_n32_exec_plt0_entry_compact
11663
0
     : mips_n32_exec_plt0_entry);
11664
0
  else if (!htab->plt_header_is_comp)
11665
0
    plt_entry = (htab->compact_branches
11666
0
     ? mipsr6_o32_exec_plt0_entry_compact
11667
0
     : mips_o32_exec_plt0_entry);
11668
0
  else if (htab->insn32)
11669
0
    plt_entry = micromips_insn32_o32_exec_plt0_entry;
11670
0
  else
11671
0
    plt_entry = micromips_o32_exec_plt0_entry;
11672
11673
  /* Calculate the value of .got.plt.  */
11674
0
  gotplt_value = (htab->root.sgotplt->output_section->vma
11675
0
      + htab->root.sgotplt->output_offset);
11676
0
  gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
11677
0
  gotplt_value_low = gotplt_value & 0xffff;
11678
11679
  /* The PLT sequence is not safe for N64 if .got.plt's address can
11680
     not be loaded in two instructions.  */
11681
0
  if (ABI_64_P (output_bfd)
11682
0
      && ((gotplt_value + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
11683
0
    {
11684
0
      _bfd_error_handler
11685
  /* xgettext:c-format */
11686
0
  (_("%pB: `%pA' start VMA of %#" PRIx64 " outside the 32-bit range "
11687
0
     "supported; consider using `-Ttext-segment=...'"),
11688
0
   output_bfd,
11689
0
   htab->root.sgotplt->output_section,
11690
0
   (int64_t) gotplt_value);
11691
0
      bfd_set_error (bfd_error_no_error);
11692
0
      return false;
11693
0
    }
11694
11695
  /* Install the PLT header.  */
11696
0
  loc = htab->root.splt->contents;
11697
0
  if (plt_entry == micromips_o32_exec_plt0_entry)
11698
0
    {
11699
0
      bfd_vma gotpc_offset;
11700
0
      bfd_vma loc_address;
11701
0
      size_t i;
11702
11703
0
      BFD_ASSERT (gotplt_value % 4 == 0);
11704
11705
0
      loc_address = (htab->root.splt->output_section->vma
11706
0
         + htab->root.splt->output_offset);
11707
0
      gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
11708
11709
      /* ADDIUPC has a span of +/-16MB, check we're in range.  */
11710
0
      if (gotpc_offset + 0x1000000 >= 0x2000000)
11711
0
  {
11712
0
    _bfd_error_handler
11713
      /* xgettext:c-format */
11714
0
      (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11715
0
         "beyond the range of ADDIUPC"),
11716
0
       output_bfd,
11717
0
       htab->root.sgotplt->output_section,
11718
0
       (int64_t) gotpc_offset,
11719
0
       htab->root.splt->output_section);
11720
0
    bfd_set_error (bfd_error_no_error);
11721
0
    return false;
11722
0
  }
11723
0
      bfd_put_16 (output_bfd,
11724
0
      plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11725
0
      bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11726
0
      for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
11727
0
  bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11728
0
    }
11729
0
  else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
11730
0
    {
11731
0
      size_t i;
11732
11733
0
      bfd_put_16 (output_bfd, plt_entry[0], loc);
11734
0
      bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
11735
0
      bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11736
0
      bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
11737
0
      bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11738
0
      bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
11739
0
      for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
11740
0
  bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11741
0
    }
11742
0
  else
11743
0
    {
11744
0
      bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
11745
0
      bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
11746
0
      bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
11747
0
      bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11748
0
      bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11749
0
      bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11750
0
      bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11751
0
      bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11752
0
    }
11753
11754
0
  return true;
11755
0
}
11756
11757
/* Install the PLT header for a VxWorks executable and finalize the
11758
   contents of .rela.plt.unloaded.  */
11759
11760
static void
11761
mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11762
0
{
11763
0
  Elf_Internal_Rela rela;
11764
0
  bfd_byte *loc;
11765
0
  bfd_vma got_value, got_value_high, got_value_low, plt_address;
11766
0
  static const bfd_vma *plt_entry;
11767
0
  struct mips_elf_link_hash_table *htab;
11768
11769
0
  htab = mips_elf_hash_table (info);
11770
0
  BFD_ASSERT (htab != NULL);
11771
11772
0
  plt_entry = mips_vxworks_exec_plt0_entry;
11773
11774
  /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
11775
0
  got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11776
0
         + htab->root.hgot->root.u.def.section->output_offset
11777
0
         + htab->root.hgot->root.u.def.value);
11778
11779
0
  got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11780
0
  got_value_low = got_value & 0xffff;
11781
11782
  /* Calculate the address of the PLT header.  */
11783
0
  plt_address = (htab->root.splt->output_section->vma
11784
0
     + htab->root.splt->output_offset);
11785
11786
  /* Install the PLT header.  */
11787
0
  loc = htab->root.splt->contents;
11788
0
  bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11789
0
  bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11790
0
  bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11791
0
  bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11792
0
  bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11793
0
  bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11794
11795
  /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_).  */
11796
0
  loc = htab->srelplt2->contents;
11797
0
  rela.r_offset = plt_address;
11798
0
  rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11799
0
  rela.r_addend = 0;
11800
0
  bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11801
0
  loc += sizeof (Elf32_External_Rela);
11802
11803
  /* Output the relocation for the following addiu of
11804
     %lo(_GLOBAL_OFFSET_TABLE_).  */
11805
0
  rela.r_offset += 4;
11806
0
  rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11807
0
  bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11808
0
  loc += sizeof (Elf32_External_Rela);
11809
11810
  /* Fix up the remaining relocations.  They may have the wrong
11811
     symbol index for _G_O_T_ or _P_L_T_ depending on the order
11812
     in which symbols were output.  */
11813
0
  while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11814
0
    {
11815
0
      Elf_Internal_Rela rel;
11816
11817
0
      bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11818
0
      rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11819
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11820
0
      loc += sizeof (Elf32_External_Rela);
11821
11822
0
      bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11823
0
      rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11824
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11825
0
      loc += sizeof (Elf32_External_Rela);
11826
11827
0
      bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11828
0
      rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11829
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11830
0
      loc += sizeof (Elf32_External_Rela);
11831
0
    }
11832
0
}
11833
11834
/* Install the PLT header for a VxWorks shared library.  */
11835
11836
static void
11837
mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11838
0
{
11839
0
  unsigned int i;
11840
0
  struct mips_elf_link_hash_table *htab;
11841
11842
0
  htab = mips_elf_hash_table (info);
11843
0
  BFD_ASSERT (htab != NULL);
11844
11845
  /* We just need to copy the entry byte-by-byte.  */
11846
0
  for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11847
0
    bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
11848
0
    htab->root.splt->contents + i * 4);
11849
0
}
11850
11851
/* Finish up the dynamic sections.  */
11852
11853
bool
11854
_bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
11855
               struct bfd_link_info *info)
11856
0
{
11857
0
  bfd *dynobj;
11858
0
  asection *sdyn;
11859
0
  asection *sgot;
11860
0
  struct mips_got_info *gg, *g;
11861
0
  struct mips_elf_link_hash_table *htab;
11862
11863
0
  htab = mips_elf_hash_table (info);
11864
0
  BFD_ASSERT (htab != NULL);
11865
11866
0
  dynobj = elf_hash_table (info)->dynobj;
11867
11868
0
  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
11869
11870
0
  sgot = htab->root.sgot;
11871
0
  gg = htab->got_info;
11872
11873
0
  if (elf_hash_table (info)->dynamic_sections_created)
11874
0
    {
11875
0
      bfd_byte *b;
11876
0
      int dyn_to_skip = 0, dyn_skipped = 0;
11877
11878
0
      BFD_ASSERT (sdyn != NULL);
11879
0
      BFD_ASSERT (gg != NULL);
11880
11881
0
      g = mips_elf_bfd_got (output_bfd, false);
11882
0
      BFD_ASSERT (g != NULL);
11883
11884
0
      for (b = sdyn->contents;
11885
0
     b < sdyn->contents + sdyn->size;
11886
0
     b += MIPS_ELF_DYN_SIZE (dynobj))
11887
0
  {
11888
0
    Elf_Internal_Dyn dyn;
11889
0
    const char *name;
11890
0
    size_t elemsize;
11891
0
    asection *s;
11892
0
    bool swap_out_p;
11893
11894
    /* Read in the current dynamic entry.  */
11895
0
    (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11896
11897
    /* Assume that we're going to modify it and write it out.  */
11898
0
    swap_out_p = true;
11899
11900
0
    switch (dyn.d_tag)
11901
0
      {
11902
0
      case DT_RELENT:
11903
0
        dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11904
0
        break;
11905
11906
0
      case DT_RELAENT:
11907
0
        BFD_ASSERT (htab->root.target_os == is_vxworks);
11908
0
        dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
11909
0
        break;
11910
11911
0
      case DT_STRSZ:
11912
        /* Rewrite DT_STRSZ.  */
11913
0
        dyn.d_un.d_val =
11914
0
    _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
11915
0
        break;
11916
11917
0
      case DT_PLTGOT:
11918
0
        s = htab->root.sgot;
11919
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11920
0
        break;
11921
11922
0
      case DT_MIPS_PLTGOT:
11923
0
        s = htab->root.sgotplt;
11924
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11925
0
        break;
11926
11927
0
      case DT_MIPS_RLD_VERSION:
11928
0
        dyn.d_un.d_val = 1; /* XXX */
11929
0
        break;
11930
11931
0
      case DT_MIPS_FLAGS:
11932
0
        dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
11933
0
        break;
11934
11935
0
      case DT_MIPS_TIME_STAMP:
11936
0
        {
11937
0
    time_t t;
11938
0
    time (&t);
11939
0
    dyn.d_un.d_val = t;
11940
0
        }
11941
0
        break;
11942
11943
0
      case DT_MIPS_ICHECKSUM:
11944
        /* XXX FIXME: */
11945
0
        swap_out_p = false;
11946
0
        break;
11947
11948
0
      case DT_MIPS_IVERSION:
11949
        /* XXX FIXME: */
11950
0
        swap_out_p = false;
11951
0
        break;
11952
11953
0
      case DT_MIPS_BASE_ADDRESS:
11954
0
        s = output_bfd->sections;
11955
0
        BFD_ASSERT (s != NULL);
11956
0
        dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
11957
0
        break;
11958
11959
0
      case DT_MIPS_LOCAL_GOTNO:
11960
0
        dyn.d_un.d_val = g->local_gotno;
11961
0
        break;
11962
11963
0
      case DT_MIPS_UNREFEXTNO:
11964
        /* The index into the dynamic symbol table which is the
11965
     entry of the first external symbol that is not
11966
     referenced within the same object.  */
11967
0
        dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
11968
0
        break;
11969
11970
0
      case DT_MIPS_GOTSYM:
11971
0
        if (htab->global_gotsym)
11972
0
    {
11973
0
      dyn.d_un.d_val = htab->global_gotsym->dynindx;
11974
0
      break;
11975
0
    }
11976
        /* In case if we don't have global got symbols we default
11977
     to setting DT_MIPS_GOTSYM to the same value as
11978
     DT_MIPS_SYMTABNO.  */
11979
        /* Fall through.  */
11980
11981
0
      case DT_MIPS_SYMTABNO:
11982
0
        name = ".dynsym";
11983
0
        elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
11984
0
        s = bfd_get_linker_section (dynobj, name);
11985
11986
0
        if (s != NULL)
11987
0
    dyn.d_un.d_val = s->size / elemsize;
11988
0
        else
11989
0
    dyn.d_un.d_val = 0;
11990
0
        break;
11991
11992
0
      case DT_MIPS_HIPAGENO:
11993
0
        dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
11994
0
        break;
11995
11996
0
      case DT_MIPS_RLD_MAP:
11997
0
        {
11998
0
    struct elf_link_hash_entry *h;
11999
0
    h = mips_elf_hash_table (info)->rld_symbol;
12000
0
    if (!h)
12001
0
      {
12002
0
        dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12003
0
        swap_out_p = false;
12004
0
        break;
12005
0
      }
12006
0
    s = h->root.u.def.section;
12007
12008
    /* The MIPS_RLD_MAP tag stores the absolute address of the
12009
       debug pointer.  */
12010
0
    dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
12011
0
          + h->root.u.def.value);
12012
0
        }
12013
0
        break;
12014
12015
0
      case DT_MIPS_RLD_MAP_REL:
12016
0
        {
12017
0
    struct elf_link_hash_entry *h;
12018
0
    bfd_vma dt_addr, rld_addr;
12019
0
    h = mips_elf_hash_table (info)->rld_symbol;
12020
0
    if (!h)
12021
0
      {
12022
0
        dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12023
0
        swap_out_p = false;
12024
0
        break;
12025
0
      }
12026
0
    s = h->root.u.def.section;
12027
12028
    /* The MIPS_RLD_MAP_REL tag stores the offset to the debug
12029
       pointer, relative to the address of the tag.  */
12030
0
    dt_addr = (sdyn->output_section->vma + sdyn->output_offset
12031
0
         + (b - sdyn->contents));
12032
0
    rld_addr = (s->output_section->vma + s->output_offset
12033
0
          + h->root.u.def.value);
12034
0
    dyn.d_un.d_ptr = rld_addr - dt_addr;
12035
0
        }
12036
0
        break;
12037
12038
0
      case DT_MIPS_OPTIONS:
12039
0
        s = (bfd_get_section_by_name
12040
0
       (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
12041
0
        dyn.d_un.d_ptr = s->vma;
12042
0
        break;
12043
12044
0
      case DT_PLTREL:
12045
0
        BFD_ASSERT (htab->use_plts_and_copy_relocs);
12046
0
        if (htab->root.target_os == is_vxworks)
12047
0
    dyn.d_un.d_val = DT_RELA;
12048
0
        else
12049
0
    dyn.d_un.d_val = DT_REL;
12050
0
        break;
12051
12052
0
      case DT_PLTRELSZ:
12053
0
        BFD_ASSERT (htab->use_plts_and_copy_relocs);
12054
0
        dyn.d_un.d_val = htab->root.srelplt->size;
12055
0
        break;
12056
12057
0
      case DT_JMPREL:
12058
0
        BFD_ASSERT (htab->use_plts_and_copy_relocs);
12059
0
        dyn.d_un.d_ptr = (htab->root.srelplt->output_section->vma
12060
0
        + htab->root.srelplt->output_offset);
12061
0
        break;
12062
12063
0
      case DT_TEXTREL:
12064
        /* If we didn't need any text relocations after all, delete
12065
     the dynamic tag.  */
12066
0
        if (!(info->flags & DF_TEXTREL))
12067
0
    {
12068
0
      dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12069
0
      swap_out_p = false;
12070
0
    }
12071
0
        break;
12072
12073
0
      case DT_FLAGS:
12074
        /* If we didn't need any text relocations after all, clear
12075
     DF_TEXTREL from DT_FLAGS.  */
12076
0
        if (!(info->flags & DF_TEXTREL))
12077
0
    dyn.d_un.d_val &= ~DF_TEXTREL;
12078
0
        else
12079
0
    swap_out_p = false;
12080
0
        break;
12081
12082
0
      case DT_MIPS_XHASH:
12083
0
        name = ".MIPS.xhash";
12084
0
        s = bfd_get_linker_section (dynobj, name);
12085
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
12086
0
        break;
12087
12088
0
      default:
12089
0
        swap_out_p = false;
12090
0
        if (htab->root.target_os == is_vxworks
12091
0
      && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
12092
0
    swap_out_p = true;
12093
0
        break;
12094
0
      }
12095
12096
0
    if (swap_out_p || dyn_skipped)
12097
0
      (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12098
0
        (dynobj, &dyn, b - dyn_skipped);
12099
12100
0
    if (dyn_to_skip)
12101
0
      {
12102
0
        dyn_skipped += dyn_to_skip;
12103
0
        dyn_to_skip = 0;
12104
0
      }
12105
0
  }
12106
12107
      /* Wipe out any trailing entries if we shifted down a dynamic tag.  */
12108
0
      if (dyn_skipped > 0)
12109
0
  memset (b - dyn_skipped, 0, dyn_skipped);
12110
0
    }
12111
12112
0
  if (sgot != NULL && sgot->size > 0
12113
0
      && !bfd_is_abs_section (sgot->output_section))
12114
0
    {
12115
0
      if (htab->root.target_os == is_vxworks)
12116
0
  {
12117
    /* The first entry of the global offset table points to the
12118
       ".dynamic" section.  The second is initialized by the
12119
       loader and contains the shared library identifier.
12120
       The third is also initialized by the loader and points
12121
       to the lazy resolution stub.  */
12122
0
    MIPS_ELF_PUT_WORD (output_bfd,
12123
0
           sdyn->output_offset + sdyn->output_section->vma,
12124
0
           sgot->contents);
12125
0
    MIPS_ELF_PUT_WORD (output_bfd, 0,
12126
0
           sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12127
0
    MIPS_ELF_PUT_WORD (output_bfd, 0,
12128
0
           sgot->contents
12129
0
           + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
12130
0
  }
12131
0
      else
12132
0
  {
12133
    /* The first entry of the global offset table will be filled at
12134
       runtime. The second entry will be used by some runtime loaders.
12135
       This isn't the case of IRIX rld.  */
12136
0
    MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
12137
0
    MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12138
0
           sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12139
0
  }
12140
12141
0
      elf_section_data (sgot->output_section)->this_hdr.sh_entsize
12142
0
   = MIPS_ELF_GOT_SIZE (output_bfd);
12143
0
    }
12144
12145
  /* Generate dynamic relocations for the non-primary gots.  */
12146
0
  if (gg != NULL && gg->next)
12147
0
    {
12148
0
      Elf_Internal_Rela rel[3];
12149
0
      bfd_vma addend = 0;
12150
12151
0
      memset (rel, 0, sizeof (rel));
12152
0
      rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
12153
12154
0
      for (g = gg->next; g->next != gg; g = g->next)
12155
0
  {
12156
0
    bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
12157
0
      + g->next->tls_gotno;
12158
12159
0
    MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
12160
0
           + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
12161
0
    MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12162
0
           sgot->contents
12163
0
           + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
12164
12165
0
    if (! bfd_link_pic (info))
12166
0
      continue;
12167
12168
0
    for (; got_index < g->local_gotno; got_index++)
12169
0
      {
12170
0
        if (got_index >= g->assigned_low_gotno
12171
0
      && got_index <= g->assigned_high_gotno)
12172
0
    continue;
12173
12174
0
        rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
12175
0
    = got_index * MIPS_ELF_GOT_SIZE (output_bfd);
12176
0
        if (!(mips_elf_create_dynamic_relocation
12177
0
        (output_bfd, info, rel, NULL,
12178
0
         bfd_abs_section_ptr,
12179
0
         0, &addend, sgot)))
12180
0
    return false;
12181
0
        BFD_ASSERT (addend == 0);
12182
0
      }
12183
0
  }
12184
0
    }
12185
12186
  /* The generation of dynamic relocations for the non-primary gots
12187
     adds more dynamic relocations.  We cannot count them until
12188
     here.  */
12189
12190
0
  if (elf_hash_table (info)->dynamic_sections_created)
12191
0
    {
12192
0
      bfd_byte *b;
12193
0
      bool swap_out_p;
12194
12195
0
      BFD_ASSERT (sdyn != NULL);
12196
12197
0
      for (b = sdyn->contents;
12198
0
     b < sdyn->contents + sdyn->size;
12199
0
     b += MIPS_ELF_DYN_SIZE (dynobj))
12200
0
  {
12201
0
    Elf_Internal_Dyn dyn;
12202
0
    asection *s;
12203
12204
    /* Read in the current dynamic entry.  */
12205
0
    (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
12206
12207
    /* Assume that we're going to modify it and write it out.  */
12208
0
    swap_out_p = true;
12209
12210
0
    switch (dyn.d_tag)
12211
0
      {
12212
0
      case DT_RELSZ:
12213
        /* Reduce DT_RELSZ to account for any relocations we
12214
     decided not to make.  This is for the n64 irix rld,
12215
     which doesn't seem to apply any relocations if there
12216
     are trailing null entries.  */
12217
0
        s = mips_elf_rel_dyn_section (info, false);
12218
0
        dyn.d_un.d_val = (s->reloc_count
12219
0
        * (ABI_64_P (output_bfd)
12220
0
           ? sizeof (Elf64_Mips_External_Rel)
12221
0
           : sizeof (Elf32_External_Rel)));
12222
        /* Adjust the section size too.  Tools like the prelinker
12223
     can reasonably expect the values to the same.  */
12224
0
        BFD_ASSERT (!bfd_is_abs_section (s->output_section));
12225
0
        elf_section_data (s->output_section)->this_hdr.sh_size
12226
0
    = dyn.d_un.d_val;
12227
0
        break;
12228
12229
0
      default:
12230
0
        swap_out_p = false;
12231
0
        break;
12232
0
      }
12233
12234
0
    if (swap_out_p)
12235
0
      (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12236
0
        (dynobj, &dyn, b);
12237
0
  }
12238
0
    }
12239
12240
0
  {
12241
0
    asection *s;
12242
0
    Elf32_compact_rel cpt;
12243
12244
0
    if (SGI_COMPAT (output_bfd))
12245
0
      {
12246
  /* Write .compact_rel section out.  */
12247
0
  s = bfd_get_linker_section (dynobj, ".compact_rel");
12248
0
  if (s != NULL)
12249
0
    {
12250
0
      cpt.id1 = 1;
12251
0
      cpt.num = s->reloc_count;
12252
0
      cpt.id2 = 2;
12253
0
      cpt.offset = (s->output_section->filepos
12254
0
        + sizeof (Elf32_External_compact_rel));
12255
0
      cpt.reserved0 = 0;
12256
0
      cpt.reserved1 = 0;
12257
0
      bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
12258
0
              ((Elf32_External_compact_rel *)
12259
0
               s->contents));
12260
12261
      /* Clean up a dummy stub function entry in .text.  */
12262
0
      if (htab->sstubs != NULL
12263
0
    && htab->sstubs->contents != NULL)
12264
0
        {
12265
0
    file_ptr dummy_offset;
12266
12267
0
    BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
12268
0
    dummy_offset = htab->sstubs->size - htab->function_stub_size;
12269
0
    memset (htab->sstubs->contents + dummy_offset, 0,
12270
0
      htab->function_stub_size);
12271
0
        }
12272
0
    }
12273
0
      }
12274
12275
    /* The psABI says that the dynamic relocations must be sorted in
12276
       increasing order of r_symndx.  The VxWorks EABI doesn't require
12277
       this, and because the code below handles REL rather than RELA
12278
       relocations, using it for VxWorks would be outright harmful.  */
12279
0
    if (htab->root.target_os != is_vxworks)
12280
0
      {
12281
0
  s = mips_elf_rel_dyn_section (info, false);
12282
0
  if (s != NULL
12283
0
      && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
12284
0
    {
12285
0
      reldyn_sorting_bfd = output_bfd;
12286
12287
0
      if (ABI_64_P (output_bfd))
12288
0
        qsort ((Elf64_External_Rel *) s->contents + 1,
12289
0
         s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
12290
0
         sort_dynamic_relocs_64);
12291
0
      else
12292
0
        qsort ((Elf32_External_Rel *) s->contents + 1,
12293
0
         s->reloc_count - 1, sizeof (Elf32_External_Rel),
12294
0
         sort_dynamic_relocs);
12295
0
    }
12296
0
      }
12297
0
  }
12298
12299
0
  if (htab->root.splt && htab->root.splt->size > 0)
12300
0
    {
12301
0
      if (htab->root.target_os == is_vxworks)
12302
0
  {
12303
0
    if (bfd_link_pic (info))
12304
0
      mips_vxworks_finish_shared_plt (output_bfd, info);
12305
0
    else
12306
0
      mips_vxworks_finish_exec_plt (output_bfd, info);
12307
0
  }
12308
0
      else
12309
0
  {
12310
0
    BFD_ASSERT (!bfd_link_pic (info));
12311
0
    if (!mips_finish_exec_plt (output_bfd, info))
12312
0
      return false;
12313
0
  }
12314
0
    }
12315
0
  return true;
12316
0
}
12317
12318
12319
/* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags.  */
12320
12321
static void
12322
mips_set_isa_flags (bfd *abfd)
12323
1
{
12324
1
  flagword val;
12325
12326
1
  switch (bfd_get_mach (abfd))
12327
1
    {
12328
0
    default:
12329
0
      if (ABI_N32_P (abfd) || ABI_64_P (abfd))
12330
0
        val = MIPS_DEFAULT_R6 ? EF_MIPS_ARCH_64R6 : EF_MIPS_ARCH_3;
12331
0
      else
12332
0
        val = MIPS_DEFAULT_R6 ? EF_MIPS_ARCH_32R6 : EF_MIPS_ARCH_1;
12333
0
      break;
12334
12335
0
    case bfd_mach_mips3000:
12336
0
      val = EF_MIPS_ARCH_1;
12337
0
      break;
12338
12339
0
    case bfd_mach_mips3900:
12340
0
      val = EF_MIPS_ARCH_1 | EF_MIPS_MACH_3900;
12341
0
      break;
12342
12343
0
    case bfd_mach_mips6000:
12344
0
      val = EF_MIPS_ARCH_2;
12345
0
      break;
12346
12347
0
    case bfd_mach_mips4010:
12348
0
      val = EF_MIPS_ARCH_2 | EF_MIPS_MACH_4010;
12349
0
      break;
12350
12351
0
    case bfd_mach_mips_allegrex:
12352
0
      val = EF_MIPS_ARCH_2 | EF_MIPS_MACH_ALLEGREX;
12353
0
      break;
12354
12355
0
    case bfd_mach_mips4000:
12356
0
    case bfd_mach_mips4300:
12357
0
    case bfd_mach_mips4400:
12358
0
    case bfd_mach_mips4600:
12359
0
      val = EF_MIPS_ARCH_3;
12360
0
      break;
12361
12362
0
    case bfd_mach_mips4100:
12363
0
      val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4100;
12364
0
      break;
12365
12366
0
    case bfd_mach_mips4111:
12367
0
      val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4111;
12368
0
      break;
12369
12370
0
    case bfd_mach_mips4120:
12371
0
      val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4120;
12372
0
      break;
12373
12374
0
    case bfd_mach_mips4650:
12375
0
      val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4650;
12376
0
      break;
12377
12378
0
    case bfd_mach_mips5400:
12379
0
      val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_5400;
12380
0
      break;
12381
12382
0
    case bfd_mach_mips5500:
12383
0
      val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_5500;
12384
0
      break;
12385
12386
0
    case bfd_mach_mips5900:
12387
0
      val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_5900;
12388
0
      break;
12389
12390
0
    case bfd_mach_mips9000:
12391
0
      val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_9000;
12392
0
      break;
12393
12394
0
    case bfd_mach_mips5000:
12395
0
    case bfd_mach_mips7000:
12396
1
    case bfd_mach_mips8000:
12397
1
    case bfd_mach_mips10000:
12398
1
    case bfd_mach_mips12000:
12399
1
    case bfd_mach_mips14000:
12400
1
    case bfd_mach_mips16000:
12401
1
      val = EF_MIPS_ARCH_4;
12402
1
      break;
12403
12404
0
    case bfd_mach_mips5:
12405
0
      val = EF_MIPS_ARCH_5;
12406
0
      break;
12407
12408
0
    case bfd_mach_mips_loongson_2e:
12409
0
      val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_LS2E;
12410
0
      break;
12411
12412
0
    case bfd_mach_mips_loongson_2f:
12413
0
      val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_LS2F;
12414
0
      break;
12415
12416
0
    case bfd_mach_mips_sb1:
12417
0
      val = EF_MIPS_ARCH_64 | EF_MIPS_MACH_SB1;
12418
0
      break;
12419
12420
0
    case bfd_mach_mips_gs464:
12421
0
      val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS464;
12422
0
      break;
12423
12424
0
    case bfd_mach_mips_gs464e:
12425
0
      val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS464E;
12426
0
      break;
12427
12428
0
    case bfd_mach_mips_gs264e:
12429
0
      val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS264E;
12430
0
      break;
12431
12432
0
    case bfd_mach_mips_octeon:
12433
0
    case bfd_mach_mips_octeonp:
12434
0
      val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON;
12435
0
      break;
12436
12437
0
    case bfd_mach_mips_octeon3:
12438
0
      val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON3;
12439
0
      break;
12440
12441
0
    case bfd_mach_mips_xlr:
12442
0
      val = EF_MIPS_ARCH_64 | EF_MIPS_MACH_XLR;
12443
0
      break;
12444
12445
0
    case bfd_mach_mips_octeon2:
12446
0
      val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON2;
12447
0
      break;
12448
12449
0
    case bfd_mach_mipsisa32:
12450
0
      val = EF_MIPS_ARCH_32;
12451
0
      break;
12452
12453
0
    case bfd_mach_mipsisa64:
12454
0
      val = EF_MIPS_ARCH_64;
12455
0
      break;
12456
12457
0
    case bfd_mach_mipsisa32r2:
12458
0
    case bfd_mach_mipsisa32r3:
12459
0
    case bfd_mach_mipsisa32r5:
12460
0
      val = EF_MIPS_ARCH_32R2;
12461
0
      break;
12462
12463
0
    case bfd_mach_mips_interaptiv_mr2:
12464
0
      val = EF_MIPS_ARCH_32R2 | EF_MIPS_MACH_IAMR2;
12465
0
      break;
12466
12467
0
    case bfd_mach_mipsisa64r2:
12468
0
    case bfd_mach_mipsisa64r3:
12469
0
    case bfd_mach_mipsisa64r5:
12470
0
      val = EF_MIPS_ARCH_64R2;
12471
0
      break;
12472
12473
0
    case bfd_mach_mipsisa32r6:
12474
0
      val = EF_MIPS_ARCH_32R6;
12475
0
      break;
12476
12477
0
    case bfd_mach_mipsisa64r6:
12478
0
      val = EF_MIPS_ARCH_64R6;
12479
0
      break;
12480
1
    }
12481
1
  elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12482
1
  elf_elfheader (abfd)->e_flags |= val;
12483
12484
1
}
12485
12486
12487
/* Whether to sort relocs output by ld -r or ld --emit-relocs, by r_offset.
12488
   Don't do so for code sections.  We want to keep ordering of HI16/LO16
12489
   as is.  On the other hand, elf-eh-frame.c processing requires .eh_frame
12490
   relocs to be sorted.  */
12491
12492
bool
12493
_bfd_mips_elf_sort_relocs_p (asection *sec)
12494
0
{
12495
0
  return (sec->flags & SEC_CODE) == 0;
12496
0
}
12497
12498
12499
/* The final processing done just before writing out a MIPS ELF object
12500
   file.  This gets the MIPS architecture right based on the machine
12501
   number.  This is used by both the 32-bit and the 64-bit ABI.  */
12502
12503
void
12504
_bfd_mips_final_write_processing (bfd *abfd)
12505
1
{
12506
1
  unsigned int i;
12507
1
  Elf_Internal_Shdr **hdrpp;
12508
1
  const char *name;
12509
1
  asection *sec;
12510
12511
  /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
12512
     is nonzero.  This is for compatibility with old objects, which used
12513
     a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH.  */
12514
1
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
12515
1
    mips_set_isa_flags (abfd);
12516
12517
  /* Set the sh_info field for .gptab sections and other appropriate
12518
     info for each special section.  */
12519
1
  for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
12520
30
       i < elf_numsections (abfd);
12521
29
       i++, hdrpp++)
12522
29
    {
12523
29
      switch ((*hdrpp)->sh_type)
12524
29
  {
12525
0
  case SHT_MIPS_MSYM:
12526
0
  case SHT_MIPS_LIBLIST:
12527
0
    sec = bfd_get_section_by_name (abfd, ".dynstr");
12528
0
    if (sec != NULL)
12529
0
      (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12530
0
    break;
12531
12532
0
  case SHT_MIPS_GPTAB:
12533
0
    BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12534
0
    name = bfd_section_name ((*hdrpp)->bfd_section);
12535
0
    if (startswith (name, ".gptab."))
12536
0
      {
12537
0
        sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
12538
0
        if (sec != NULL)
12539
0
    (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12540
0
      }
12541
0
    break;
12542
12543
0
  case SHT_MIPS_CONTENT:
12544
0
    BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12545
0
    name = bfd_section_name ((*hdrpp)->bfd_section);
12546
0
    if (startswith (name, ".MIPS.content"))
12547
0
      {
12548
0
        sec = bfd_get_section_by_name (abfd,
12549
0
               name + sizeof ".MIPS.content" - 1);
12550
0
        if (sec != NULL)
12551
0
    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12552
0
      }
12553
0
    break;
12554
12555
0
  case SHT_MIPS_SYMBOL_LIB:
12556
0
    sec = bfd_get_section_by_name (abfd, ".dynsym");
12557
0
    if (sec != NULL)
12558
0
      (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12559
0
    sec = bfd_get_section_by_name (abfd, ".liblist");
12560
0
    if (sec != NULL)
12561
0
      (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12562
0
    break;
12563
12564
0
  case SHT_MIPS_EVENTS:
12565
0
    BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12566
0
    name = bfd_section_name ((*hdrpp)->bfd_section);
12567
0
    if (startswith (name, ".MIPS.events"))
12568
0
      sec = bfd_get_section_by_name (abfd,
12569
0
             name + sizeof ".MIPS.events" - 1);
12570
0
    else if (startswith (name, ".MIPS.post_rel"))
12571
0
      sec = bfd_get_section_by_name (abfd,
12572
0
             name + sizeof ".MIPS.post_rel" - 1);
12573
0
    else
12574
0
      sec = NULL;
12575
0
    if (sec != NULL)
12576
0
      (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12577
0
    break;
12578
12579
0
  case SHT_MIPS_XHASH:
12580
0
    sec = bfd_get_section_by_name (abfd, ".dynsym");
12581
0
    if (sec != NULL)
12582
0
      (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12583
29
  }
12584
29
    }
12585
1
}
12586
12587
bool
12588
_bfd_mips_elf_final_write_processing (bfd *abfd)
12589
1
{
12590
1
  _bfd_mips_final_write_processing (abfd);
12591
1
  return _bfd_elf_final_write_processing (abfd);
12592
1
}
12593

12594
/* When creating an IRIX5 executable, we need REGINFO and RTPROC
12595
   segments.  */
12596
12597
int
12598
_bfd_mips_elf_additional_program_headers (bfd *abfd,
12599
            struct bfd_link_info *info ATTRIBUTE_UNUSED)
12600
0
{
12601
0
  asection *s;
12602
0
  int ret = 0;
12603
12604
  /* See if we need a PT_MIPS_REGINFO segment.  */
12605
0
  s = bfd_get_section_by_name (abfd, ".reginfo");
12606
0
  if (s && (s->flags & SEC_LOAD))
12607
0
    ++ret;
12608
12609
  /* See if we need a PT_MIPS_ABIFLAGS segment.  */
12610
0
  if (bfd_get_section_by_name (abfd, ".MIPS.abiflags"))
12611
0
    ++ret;
12612
12613
  /* See if we need a PT_MIPS_OPTIONS segment.  */
12614
0
  if (IRIX_COMPAT (abfd) == ict_irix6
12615
0
      && bfd_get_section_by_name (abfd,
12616
0
          MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
12617
0
    ++ret;
12618
12619
  /* See if we need a PT_MIPS_RTPROC segment.  */
12620
0
  if (IRIX_COMPAT (abfd) == ict_irix5
12621
0
      && bfd_get_section_by_name (abfd, ".dynamic")
12622
0
      && bfd_get_section_by_name (abfd, ".mdebug"))
12623
0
    ++ret;
12624
12625
  /* Allocate a PT_NULL header in dynamic objects.  See
12626
     _bfd_mips_elf_modify_segment_map for details.  */
12627
0
  if (!SGI_COMPAT (abfd)
12628
0
      && bfd_get_section_by_name (abfd, ".dynamic"))
12629
0
    ++ret;
12630
12631
0
  return ret;
12632
0
}
12633
12634
/* Modify the segment map for an IRIX5 executable.  */
12635
12636
bool
12637
_bfd_mips_elf_modify_segment_map (bfd *abfd,
12638
          struct bfd_link_info *info)
12639
1
{
12640
1
  asection *s;
12641
1
  struct elf_segment_map *m, **pm;
12642
1
  size_t amt;
12643
12644
  /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
12645
     segment.  */
12646
1
  s = bfd_get_section_by_name (abfd, ".reginfo");
12647
1
  if (s != NULL && (s->flags & SEC_LOAD) != 0)
12648
1
    {
12649
3
      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12650
3
  if (m->p_type == PT_MIPS_REGINFO)
12651
1
    break;
12652
1
      if (m == NULL)
12653
0
  {
12654
0
    amt = sizeof *m;
12655
0
    m = bfd_zalloc (abfd, amt);
12656
0
    if (m == NULL)
12657
0
      return false;
12658
12659
0
    m->p_type = PT_MIPS_REGINFO;
12660
0
    m->count = 1;
12661
0
    m->sections[0] = s;
12662
12663
    /* We want to put it after the PHDR and INTERP segments.  */
12664
0
    pm = &elf_seg_map (abfd);
12665
0
    while (*pm != NULL
12666
0
     && ((*pm)->p_type == PT_PHDR
12667
0
         || (*pm)->p_type == PT_INTERP))
12668
0
      pm = &(*pm)->next;
12669
12670
0
    m->next = *pm;
12671
0
    *pm = m;
12672
0
  }
12673
1
    }
12674
12675
  /* If there is a .MIPS.abiflags section, we need a PT_MIPS_ABIFLAGS
12676
     segment.  */
12677
1
  s = bfd_get_section_by_name (abfd, ".MIPS.abiflags");
12678
1
  if (s != NULL && (s->flags & SEC_LOAD) != 0)
12679
0
    {
12680
0
      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12681
0
  if (m->p_type == PT_MIPS_ABIFLAGS)
12682
0
    break;
12683
0
      if (m == NULL)
12684
0
  {
12685
0
    amt = sizeof *m;
12686
0
    m = bfd_zalloc (abfd, amt);
12687
0
    if (m == NULL)
12688
0
      return false;
12689
12690
0
    m->p_type = PT_MIPS_ABIFLAGS;
12691
0
    m->count = 1;
12692
0
    m->sections[0] = s;
12693
12694
    /* We want to put it after the PHDR and INTERP segments.  */
12695
0
    pm = &elf_seg_map (abfd);
12696
0
    while (*pm != NULL
12697
0
     && ((*pm)->p_type == PT_PHDR
12698
0
         || (*pm)->p_type == PT_INTERP))
12699
0
      pm = &(*pm)->next;
12700
12701
0
    m->next = *pm;
12702
0
    *pm = m;
12703
0
  }
12704
0
    }
12705
12706
  /* For IRIX 6, we don't have .mdebug sections, nor does anything but
12707
     .dynamic end up in PT_DYNAMIC.  However, we do have to insert a
12708
     PT_MIPS_OPTIONS segment immediately following the program header
12709
     table.  */
12710
1
  if (NEWABI_P (abfd)
12711
      /* On non-IRIX6 new abi, we'll have already created a segment
12712
   for this section, so don't create another.  I'm not sure this
12713
   is not also the case for IRIX 6, but I can't test it right
12714
   now.  */
12715
1
      && IRIX_COMPAT (abfd) == ict_irix6)
12716
0
    {
12717
0
      for (s = abfd->sections; s; s = s->next)
12718
0
  if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
12719
0
    break;
12720
12721
0
      if (s)
12722
0
  {
12723
0
    struct elf_segment_map *options_segment;
12724
12725
0
    pm = &elf_seg_map (abfd);
12726
0
    while (*pm != NULL
12727
0
     && ((*pm)->p_type == PT_PHDR
12728
0
         || (*pm)->p_type == PT_INTERP))
12729
0
      pm = &(*pm)->next;
12730
12731
0
    if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
12732
0
      {
12733
0
        amt = sizeof (struct elf_segment_map);
12734
0
        options_segment = bfd_zalloc (abfd, amt);
12735
0
        options_segment->next = *pm;
12736
0
        options_segment->p_type = PT_MIPS_OPTIONS;
12737
0
        options_segment->p_flags = PF_R;
12738
0
        options_segment->p_flags_valid = true;
12739
0
        options_segment->count = 1;
12740
0
        options_segment->sections[0] = s;
12741
0
        *pm = options_segment;
12742
0
      }
12743
0
  }
12744
0
    }
12745
1
  else
12746
1
    {
12747
1
      if (IRIX_COMPAT (abfd) == ict_irix5)
12748
1
  {
12749
    /* If there are .dynamic and .mdebug sections, we make a room
12750
       for the RTPROC header.  FIXME: Rewrite without section names.  */
12751
1
    if (bfd_get_section_by_name (abfd, ".interp") == NULL
12752
1
        && bfd_get_section_by_name (abfd, ".dynamic") != NULL
12753
1
        && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
12754
0
      {
12755
0
        for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12756
0
    if (m->p_type == PT_MIPS_RTPROC)
12757
0
      break;
12758
0
        if (m == NULL)
12759
0
    {
12760
0
      amt = sizeof *m;
12761
0
      m = bfd_zalloc (abfd, amt);
12762
0
      if (m == NULL)
12763
0
        return false;
12764
12765
0
      m->p_type = PT_MIPS_RTPROC;
12766
12767
0
      s = bfd_get_section_by_name (abfd, ".rtproc");
12768
0
      if (s == NULL)
12769
0
        {
12770
0
          m->count = 0;
12771
0
          m->p_flags = 0;
12772
0
          m->p_flags_valid = 1;
12773
0
        }
12774
0
      else
12775
0
        {
12776
0
          m->count = 1;
12777
0
          m->sections[0] = s;
12778
0
        }
12779
12780
      /* We want to put it after the DYNAMIC segment.  */
12781
0
      pm = &elf_seg_map (abfd);
12782
0
      while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
12783
0
        pm = &(*pm)->next;
12784
0
      if (*pm != NULL)
12785
0
        pm = &(*pm)->next;
12786
12787
0
      m->next = *pm;
12788
0
      *pm = m;
12789
0
    }
12790
0
      }
12791
1
  }
12792
      /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
12793
   .dynstr, .dynsym, and .hash sections, and everything in
12794
   between.  */
12795
6
      for (pm = &elf_seg_map (abfd); *pm != NULL;
12796
5
     pm = &(*pm)->next)
12797
6
  if ((*pm)->p_type == PT_DYNAMIC)
12798
1
    break;
12799
1
      m = *pm;
12800
      /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
12801
   glibc's dynamic linker has traditionally derived the number of
12802
   tags from the p_filesz field, and sometimes allocates stack
12803
   arrays of that size.  An overly-big PT_DYNAMIC segment can
12804
   be actively harmful in such cases.  Making PT_DYNAMIC contain
12805
   other sections can also make life hard for the prelinker,
12806
   which might move one of the other sections to a different
12807
   PT_LOAD segment.  */
12808
1
      if (SGI_COMPAT (abfd)
12809
1
    && m != NULL
12810
1
    && m->count == 1
12811
1
    && strcmp (m->sections[0]->name, ".dynamic") == 0)
12812
0
  {
12813
0
    static const char *sec_names[] =
12814
0
    {
12815
0
      ".dynamic", ".dynstr", ".dynsym", ".hash"
12816
0
    };
12817
0
    bfd_vma low, high;
12818
0
    unsigned int i, c;
12819
0
    struct elf_segment_map *n;
12820
12821
0
    low = ~(bfd_vma) 0;
12822
0
    high = 0;
12823
0
    for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
12824
0
      {
12825
0
        s = bfd_get_section_by_name (abfd, sec_names[i]);
12826
0
        if (s != NULL && (s->flags & SEC_LOAD) != 0)
12827
0
    {
12828
0
      bfd_size_type sz;
12829
12830
0
      if (low > s->vma)
12831
0
        low = s->vma;
12832
0
      sz = s->size;
12833
0
      if (high < s->vma + sz)
12834
0
        high = s->vma + sz;
12835
0
    }
12836
0
      }
12837
12838
0
    c = 0;
12839
0
    for (s = abfd->sections; s != NULL; s = s->next)
12840
0
      if ((s->flags & SEC_LOAD) != 0
12841
0
    && s->vma >= low
12842
0
    && s->vma + s->size <= high)
12843
0
        ++c;
12844
12845
0
    amt = sizeof *n - sizeof (asection *) + c * sizeof (asection *);
12846
0
    n = bfd_zalloc (abfd, amt);
12847
0
    if (n == NULL)
12848
0
      return false;
12849
0
    *n = *m;
12850
0
    n->count = c;
12851
12852
0
    i = 0;
12853
0
    for (s = abfd->sections; s != NULL; s = s->next)
12854
0
      {
12855
0
        if ((s->flags & SEC_LOAD) != 0
12856
0
      && s->vma >= low
12857
0
      && s->vma + s->size <= high)
12858
0
    {
12859
0
      n->sections[i] = s;
12860
0
      ++i;
12861
0
    }
12862
0
      }
12863
12864
0
    *pm = n;
12865
0
  }
12866
1
    }
12867
12868
  /* Allocate a spare program header in dynamic objects so that tools
12869
     like the prelinker can add an extra PT_LOAD entry.
12870
12871
     If the prelinker needs to make room for a new PT_LOAD entry, its
12872
     standard procedure is to move the first (read-only) sections into
12873
     the new (writable) segment.  However, the MIPS ABI requires
12874
     .dynamic to be in a read-only segment, and the section will often
12875
     start within sizeof (ElfNN_Phdr) bytes of the last program header.
12876
12877
     Although the prelinker could in principle move .dynamic to a
12878
     writable segment, it seems better to allocate a spare program
12879
     header instead, and avoid the need to move any sections.
12880
     There is a long tradition of allocating spare dynamic tags,
12881
     so allocating a spare program header seems like a natural
12882
     extension.
12883
12884
     If INFO is NULL, we may be copying an already prelinked binary
12885
     with objcopy or strip, so do not add this header.  */
12886
1
  if (info != NULL
12887
1
      && !SGI_COMPAT (abfd)
12888
1
      && bfd_get_section_by_name (abfd, ".dynamic"))
12889
0
    {
12890
0
      for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
12891
0
  if ((*pm)->p_type == PT_NULL)
12892
0
    break;
12893
0
      if (*pm == NULL)
12894
0
  {
12895
0
    m = bfd_zalloc (abfd, sizeof (*m));
12896
0
    if (m == NULL)
12897
0
      return false;
12898
12899
0
    m->p_type = PT_NULL;
12900
0
    *pm = m;
12901
0
  }
12902
0
    }
12903
12904
1
  return true;
12905
1
}
12906

12907
/* Return the section that should be marked against GC for a given
12908
   relocation.  */
12909
12910
asection *
12911
_bfd_mips_elf_gc_mark_hook (asection *sec,
12912
          struct bfd_link_info *info,
12913
          Elf_Internal_Rela *rel,
12914
          struct elf_link_hash_entry *h,
12915
          Elf_Internal_Sym *sym)
12916
0
{
12917
  /* ??? Do mips16 stub sections need to be handled special?  */
12918
12919
0
  if (h != NULL)
12920
0
    switch (ELF_R_TYPE (sec->owner, rel->r_info))
12921
0
      {
12922
0
      case R_MIPS_GNU_VTINHERIT:
12923
0
      case R_MIPS_GNU_VTENTRY:
12924
0
  return NULL;
12925
0
      }
12926
12927
0
  return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
12928
0
}
12929
12930
/* Prevent .MIPS.abiflags from being discarded with --gc-sections.  */
12931
12932
bool
12933
_bfd_mips_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12934
              elf_gc_mark_hook_fn gc_mark_hook)
12935
0
{
12936
0
  bfd *sub;
12937
12938
0
  _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
12939
12940
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12941
0
    {
12942
0
      asection *o;
12943
12944
0
      if (! is_mips_elf (sub))
12945
0
  continue;
12946
12947
0
      for (o = sub->sections; o != NULL; o = o->next)
12948
0
  if (!o->gc_mark
12949
0
      && MIPS_ELF_ABIFLAGS_SECTION_NAME_P (bfd_section_name (o)))
12950
0
    {
12951
0
      if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12952
0
        return false;
12953
0
    }
12954
0
    }
12955
12956
0
  return true;
12957
0
}
12958

12959
/* Copy data from a MIPS ELF indirect symbol to its direct symbol,
12960
   hiding the old indirect symbol.  Process additional relocation
12961
   information.  Also called for weakdefs, in which case we just let
12962
   _bfd_elf_link_hash_copy_indirect copy the flags for us.  */
12963
12964
void
12965
_bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
12966
            struct elf_link_hash_entry *dir,
12967
            struct elf_link_hash_entry *ind)
12968
0
{
12969
0
  struct mips_elf_link_hash_entry *dirmips, *indmips;
12970
12971
0
  _bfd_elf_link_hash_copy_indirect (info, dir, ind);
12972
12973
0
  dirmips = (struct mips_elf_link_hash_entry *) dir;
12974
0
  indmips = (struct mips_elf_link_hash_entry *) ind;
12975
  /* Any absolute non-dynamic relocations against an indirect or weak
12976
     definition will be against the target symbol.  */
12977
0
  if (indmips->has_static_relocs)
12978
0
    dirmips->has_static_relocs = true;
12979
12980
0
  if (ind->root.type != bfd_link_hash_indirect)
12981
0
    return;
12982
12983
0
  dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
12984
0
  if (indmips->readonly_reloc)
12985
0
    dirmips->readonly_reloc = true;
12986
0
  if (indmips->no_fn_stub)
12987
0
    dirmips->no_fn_stub = true;
12988
0
  if (indmips->fn_stub)
12989
0
    {
12990
0
      dirmips->fn_stub = indmips->fn_stub;
12991
0
      indmips->fn_stub = NULL;
12992
0
    }
12993
0
  if (indmips->need_fn_stub)
12994
0
    {
12995
0
      dirmips->need_fn_stub = true;
12996
0
      indmips->need_fn_stub = false;
12997
0
    }
12998
0
  if (indmips->call_stub)
12999
0
    {
13000
0
      dirmips->call_stub = indmips->call_stub;
13001
0
      indmips->call_stub = NULL;
13002
0
    }
13003
0
  if (indmips->call_fp_stub)
13004
0
    {
13005
0
      dirmips->call_fp_stub = indmips->call_fp_stub;
13006
0
      indmips->call_fp_stub = NULL;
13007
0
    }
13008
0
  if (indmips->global_got_area < dirmips->global_got_area)
13009
0
    dirmips->global_got_area = indmips->global_got_area;
13010
0
  if (indmips->global_got_area < GGA_NONE)
13011
0
    indmips->global_got_area = GGA_NONE;
13012
0
  if (indmips->has_nonpic_branches)
13013
0
    dirmips->has_nonpic_branches = true;
13014
0
}
13015
13016
/* Take care of the special `__gnu_absolute_zero' symbol and ignore attempts
13017
   to hide it.  It has to remain global (it will also be protected) so as to
13018
   be assigned a global GOT entry, which will then remain unchanged at load
13019
   time.  */
13020
13021
void
13022
_bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
13023
         struct elf_link_hash_entry *entry,
13024
         bool force_local)
13025
0
{
13026
0
  struct mips_elf_link_hash_table *htab;
13027
13028
0
  htab = mips_elf_hash_table (info);
13029
0
  BFD_ASSERT (htab != NULL);
13030
0
  if (htab->use_absolute_zero
13031
0
      && strcmp (entry->root.root.string, "__gnu_absolute_zero") == 0)
13032
0
    return;
13033
13034
0
  _bfd_elf_link_hash_hide_symbol (info, entry, force_local);
13035
0
}
13036

13037
0
#define PDR_SIZE 32
13038
13039
bool
13040
_bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
13041
          struct bfd_link_info *info)
13042
0
{
13043
0
  asection *o;
13044
0
  bool ret = false;
13045
0
  unsigned char *tdata;
13046
0
  size_t i, skip;
13047
13048
0
  o = bfd_get_section_by_name (abfd, ".pdr");
13049
0
  if (! o)
13050
0
    return false;
13051
0
  if (o->size == 0)
13052
0
    return false;
13053
0
  if (o->size % PDR_SIZE != 0)
13054
0
    return false;
13055
0
  if (o->output_section != NULL
13056
0
      && bfd_is_abs_section (o->output_section))
13057
0
    return false;
13058
13059
0
  tdata = bfd_zmalloc (o->size / PDR_SIZE);
13060
0
  if (! tdata)
13061
0
    return false;
13062
13063
0
  cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
13064
0
              info->keep_memory);
13065
0
  if (!cookie->rels)
13066
0
    {
13067
0
      free (tdata);
13068
0
      return false;
13069
0
    }
13070
13071
0
  cookie->rel = cookie->rels;
13072
0
  cookie->relend = cookie->rels + o->reloc_count;
13073
13074
0
  for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
13075
0
    {
13076
0
      if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
13077
0
  {
13078
0
    tdata[i] = 1;
13079
0
    skip ++;
13080
0
  }
13081
0
    }
13082
13083
0
  if (skip != 0)
13084
0
    {
13085
0
      mips_elf_section_data (o)->u.tdata = tdata;
13086
0
      if (o->rawsize == 0)
13087
0
  o->rawsize = o->size;
13088
0
      o->size -= skip * PDR_SIZE;
13089
0
      ret = true;
13090
0
    }
13091
0
  else
13092
0
    free (tdata);
13093
13094
0
  if (! info->keep_memory)
13095
0
    free (cookie->rels);
13096
13097
0
  return ret;
13098
0
}
13099
13100
bool
13101
_bfd_mips_elf_ignore_discarded_relocs (asection *sec)
13102
0
{
13103
0
  if (strcmp (sec->name, ".pdr") == 0)
13104
0
    return true;
13105
0
  return false;
13106
0
}
13107
13108
bool
13109
_bfd_mips_elf_write_section (bfd *output_bfd,
13110
           struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
13111
           asection *sec, bfd_byte *contents)
13112
0
{
13113
0
  bfd_byte *to, *from, *end;
13114
0
  int i;
13115
13116
0
  if (strcmp (sec->name, ".pdr") != 0)
13117
0
    return false;
13118
13119
0
  if (mips_elf_section_data (sec)->u.tdata == NULL)
13120
0
    return false;
13121
13122
0
  to = contents;
13123
0
  end = contents + sec->size;
13124
0
  for (from = contents, i = 0;
13125
0
       from < end;
13126
0
       from += PDR_SIZE, i++)
13127
0
    {
13128
0
      if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
13129
0
  continue;
13130
0
      if (to != from)
13131
0
  memcpy (to, from, PDR_SIZE);
13132
0
      to += PDR_SIZE;
13133
0
    }
13134
0
  bfd_set_section_contents (output_bfd, sec->output_section, contents,
13135
0
          sec->output_offset, sec->size);
13136
0
  return true;
13137
0
}
13138

13139
/* microMIPS code retains local labels for linker relaxation.  Omit them
13140
   from output by default for clarity.  */
13141
13142
bool
13143
_bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
13144
4.81k
{
13145
4.81k
  return _bfd_elf_is_local_label_name (abfd, sym->name);
13146
4.81k
}
13147
13148
bool
13149
_bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
13150
         asection *section, bfd_vma offset,
13151
         const char **filename_ptr,
13152
         const char **functionname_ptr,
13153
         unsigned int *line_ptr,
13154
         unsigned int *discriminator_ptr)
13155
3.41k
{
13156
3.41k
  asection *msec;
13157
13158
3.41k
  if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
13159
3.41k
             filename_ptr, functionname_ptr,
13160
3.41k
             line_ptr, discriminator_ptr,
13161
3.41k
             dwarf_debug_sections,
13162
3.41k
             &elf_tdata (abfd)->dwarf2_find_line_info)
13163
3.41k
      == 1)
13164
37
    return true;
13165
13166
3.37k
  if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
13167
3.37k
             filename_ptr, functionname_ptr,
13168
3.37k
             line_ptr))
13169
0
    {
13170
0
      if (!*functionname_ptr)
13171
0
  _bfd_elf_find_function (abfd, symbols, section, offset,
13172
0
        *filename_ptr ? NULL : filename_ptr,
13173
0
        functionname_ptr);
13174
0
      return true;
13175
0
    }
13176
13177
3.37k
  msec = bfd_get_section_by_name (abfd, ".mdebug");
13178
3.37k
  if (msec != NULL)
13179
1.95k
    {
13180
1.95k
      flagword origflags;
13181
1.95k
      struct mips_elf_find_line *fi;
13182
1.95k
      const struct ecoff_debug_swap * const swap =
13183
1.95k
  get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
13184
13185
      /* If we are called during a link, mips_elf_final_link may have
13186
   cleared the SEC_HAS_CONTENTS field.  We force it back on here
13187
   if appropriate (which it normally will be).  */
13188
1.95k
      origflags = msec->flags;
13189
1.95k
      if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
13190
1.95k
  msec->flags |= SEC_HAS_CONTENTS;
13191
13192
1.95k
      fi = mips_elf_tdata (abfd)->find_line_info;
13193
1.95k
      if (fi == NULL)
13194
1.34k
  {
13195
1.34k
    bfd_size_type external_fdr_size;
13196
1.34k
    char *fraw_src;
13197
1.34k
    char *fraw_end;
13198
1.34k
    struct fdr *fdr_ptr;
13199
1.34k
    bfd_size_type amt = sizeof (struct mips_elf_find_line);
13200
13201
1.34k
    fi = bfd_zalloc (abfd, amt);
13202
1.34k
    if (fi == NULL)
13203
0
      {
13204
0
        msec->flags = origflags;
13205
0
        return false;
13206
0
      }
13207
13208
1.34k
    if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
13209
1.22k
      {
13210
1.22k
        msec->flags = origflags;
13211
1.22k
        return false;
13212
1.22k
      }
13213
13214
    /* Swap in the FDR information.  */
13215
124
    amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
13216
124
    fi->d.fdr = bfd_alloc (abfd, amt);
13217
124
    if (fi->d.fdr == NULL)
13218
0
      {
13219
0
        _bfd_ecoff_free_ecoff_debug_info (&fi->d);
13220
0
        msec->flags = origflags;
13221
0
        return false;
13222
0
      }
13223
124
    external_fdr_size = swap->external_fdr_size;
13224
124
    fdr_ptr = fi->d.fdr;
13225
124
    fraw_src = (char *) fi->d.external_fdr;
13226
124
    fraw_end = (fraw_src
13227
124
          + fi->d.symbolic_header.ifdMax * external_fdr_size);
13228
14.6k
    for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
13229
14.5k
      (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
13230
13231
124
    mips_elf_tdata (abfd)->find_line_info = fi;
13232
124
  }
13233
13234
733
      if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
13235
733
          &fi->i, filename_ptr, functionname_ptr,
13236
733
          line_ptr))
13237
443
  {
13238
443
    msec->flags = origflags;
13239
443
    return true;
13240
443
  }
13241
13242
290
      msec->flags = origflags;
13243
290
    }
13244
13245
  /* Fall back on the generic ELF find_nearest_line routine.  */
13246
13247
1.70k
  return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
13248
1.70k
             filename_ptr, functionname_ptr,
13249
1.70k
             line_ptr, discriminator_ptr);
13250
3.37k
}
13251
13252
bool
13253
_bfd_mips_elf_find_inliner_info (bfd *abfd,
13254
         const char **filename_ptr,
13255
         const char **functionname_ptr,
13256
         unsigned int *line_ptr)
13257
0
{
13258
0
  bool found;
13259
0
  found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
13260
0
           functionname_ptr, line_ptr,
13261
0
           & elf_tdata (abfd)->dwarf2_find_line_info);
13262
0
  return found;
13263
0
}
13264
13265

13266
/* When are writing out the .options or .MIPS.options section,
13267
   remember the bytes we are writing out, so that we can install the
13268
   GP value in the section_processing routine.  */
13269
13270
bool
13271
_bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
13272
            const void *location,
13273
            file_ptr offset, bfd_size_type count)
13274
25
{
13275
25
  if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
13276
0
    {
13277
0
      bfd_byte *c;
13278
13279
0
      if (elf_section_data (section) == NULL)
13280
0
  {
13281
0
    size_t amt = sizeof (struct bfd_elf_section_data);
13282
0
    section->used_by_bfd = bfd_zalloc (abfd, amt);
13283
0
    if (elf_section_data (section) == NULL)
13284
0
      return false;
13285
0
  }
13286
0
      c = mips_elf_section_data (section)->u.tdata;
13287
0
      if (c == NULL)
13288
0
  {
13289
0
    c = bfd_zalloc (abfd, section->size);
13290
0
    if (c == NULL)
13291
0
      return false;
13292
0
    mips_elf_section_data (section)->u.tdata = c;
13293
0
  }
13294
13295
0
      memcpy (c + offset, location, count);
13296
0
    }
13297
13298
25
  return _bfd_elf_set_section_contents (abfd, section, location, offset,
13299
25
          count);
13300
25
}
13301
13302
/* This is almost identical to bfd_generic_get_... except that some
13303
   MIPS relocations need to be handled specially.  Sigh.  */
13304
13305
bfd_byte *
13306
_bfd_elf_mips_get_relocated_section_contents
13307
  (bfd *abfd,
13308
   struct bfd_link_info *link_info,
13309
   struct bfd_link_order *link_order,
13310
   bfd_byte *data,
13311
   bool relocatable,
13312
   asymbol **symbols)
13313
433
{
13314
433
  bfd *input_bfd = link_order->u.indirect.section->owner;
13315
433
  asection *input_section = link_order->u.indirect.section;
13316
433
  long reloc_size;
13317
433
  arelent **reloc_vector;
13318
433
  long reloc_count;
13319
13320
433
  reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13321
433
  if (reloc_size < 0)
13322
15
    return NULL;
13323
13324
  /* Read in the section.  */
13325
418
  bfd_byte *orig_data = data;
13326
418
  if (!bfd_get_full_section_contents (input_bfd, input_section, &data))
13327
2
    return NULL;
13328
13329
416
  if (data == NULL)
13330
27
    return NULL;
13331
13332
389
  if (reloc_size == 0)
13333
0
    return data;
13334
13335
389
  reloc_vector = (arelent **) bfd_malloc (reloc_size);
13336
389
  if (reloc_vector == NULL)
13337
0
    {
13338
0
      struct mips_elf_obj_tdata *tdata;
13339
0
      struct mips_hi16 **hip, *hi;
13340
334
    error_return:
13341
      /* If we are going to return an error, remove entries on
13342
   mips_hi16_list that point into this section's data.  Data
13343
   will typically be freed on return from this function.  */
13344
334
      tdata = mips_elf_tdata (abfd);
13345
334
      hip = &tdata->mips_hi16_list;
13346
361
      while ((hi = *hip) != NULL)
13347
27
  {
13348
27
    if (hi->input_section == input_section)
13349
4
      {
13350
4
        *hip = hi->next;
13351
4
        free (hi);
13352
4
      }
13353
23
    else
13354
23
      hip = &hi->next;
13355
27
  }
13356
334
      if (orig_data == NULL)
13357
10
  free (data);
13358
334
      data = NULL;
13359
334
      goto out;
13360
0
    }
13361
13362
389
  reloc_count = bfd_canonicalize_reloc (input_bfd,
13363
389
          input_section,
13364
389
          reloc_vector,
13365
389
          symbols);
13366
389
  if (reloc_count < 0)
13367
274
    goto error_return;
13368
13369
115
  if (reloc_count > 0)
13370
111
    {
13371
111
      arelent **parent;
13372
      /* for mips */
13373
111
      int gp_found;
13374
111
      bfd_vma gp = 0x12345678;  /* initialize just to shut gcc up */
13375
13376
111
      {
13377
111
  struct bfd_hash_entry *h;
13378
111
  struct bfd_link_hash_entry *lh;
13379
  /* Skip all this stuff if we aren't mixing formats.  */
13380
111
  if (abfd && input_bfd
13381
111
      && abfd->xvec == input_bfd->xvec)
13382
111
    lh = 0;
13383
0
  else
13384
0
    {
13385
0
      h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
13386
0
      lh = (struct bfd_link_hash_entry *) h;
13387
0
    }
13388
111
      lookup:
13389
111
  if (lh)
13390
0
    {
13391
0
      switch (lh->type)
13392
0
        {
13393
0
        case bfd_link_hash_undefined:
13394
0
        case bfd_link_hash_undefweak:
13395
0
        case bfd_link_hash_common:
13396
0
    gp_found = 0;
13397
0
    break;
13398
0
        case bfd_link_hash_defined:
13399
0
        case bfd_link_hash_defweak:
13400
0
    gp_found = 1;
13401
0
    gp = lh->u.def.value;
13402
0
    break;
13403
0
        case bfd_link_hash_indirect:
13404
0
        case bfd_link_hash_warning:
13405
0
    lh = lh->u.i.link;
13406
    /* @@FIXME  ignoring warning for now */
13407
0
    goto lookup;
13408
0
        case bfd_link_hash_new:
13409
0
        default:
13410
0
    abort ();
13411
0
        }
13412
0
    }
13413
111
  else
13414
111
    gp_found = 0;
13415
111
      }
13416
      /* end mips */
13417
13418
3.27k
      for (parent = reloc_vector; *parent != NULL; parent++)
13419
3.22k
  {
13420
3.22k
    char *error_message = NULL;
13421
3.22k
    asymbol *symbol;
13422
3.22k
    bfd_reloc_status_type r;
13423
13424
3.22k
    symbol = *(*parent)->sym_ptr_ptr;
13425
    /* PR ld/19628: A specially crafted input file
13426
       can result in a NULL symbol pointer here.  */
13427
3.22k
    if (symbol == NULL)
13428
0
      {
13429
0
        link_info->callbacks->einfo
13430
    /* xgettext:c-format */
13431
0
    (_("%X%P: %pB(%pA): error: relocation for offset %V has no value\n"),
13432
0
     abfd, input_section, (* parent)->address);
13433
0
        goto error_return;
13434
0
      }
13435
13436
    /* Zap reloc field when the symbol is from a discarded
13437
       section, ignoring any addend.  Do the same when called
13438
       from bfd_simple_get_relocated_section_contents for
13439
       undefined symbols in debug sections.  This is to keep
13440
       debug info reasonably sane, in particular so that
13441
       DW_FORM_ref_addr to another file's .debug_info isn't
13442
       confused with an offset into the current file's
13443
       .debug_info.  */
13444
3.22k
    if ((symbol->section != NULL && discarded_section (symbol->section))
13445
3.22k
        || (symbol->section == bfd_und_section_ptr
13446
3.22k
      && (input_section->flags & SEC_DEBUGGING) != 0
13447
3.22k
      && link_info->input_bfds == link_info->output_bfd))
13448
15
      {
13449
15
        bfd_vma off;
13450
15
        static reloc_howto_type none_howto
13451
15
    = HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont, NULL,
13452
15
       "unused", false, 0, 0, false);
13453
13454
15
        off = ((*parent)->address
13455
15
         * bfd_octets_per_byte (input_bfd, input_section));
13456
15
        _bfd_clear_contents ((*parent)->howto, input_bfd,
13457
15
           input_section, data, off);
13458
15
        (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
13459
15
        (*parent)->addend = 0;
13460
15
        (*parent)->howto = &none_howto;
13461
15
        r = bfd_reloc_ok;
13462
15
      }
13463
13464
    /* Specific to MIPS: Deal with relocation types that require
13465
       knowing the gp of the output bfd.  */
13466
13467
    /* If we've managed to find the gp and have a special
13468
       function for the relocation then go ahead, else default
13469
       to the generic handling.  */
13470
3.20k
    else if (gp_found
13471
3.20k
       && ((*parent)->howto->special_function
13472
0
           == _bfd_mips_elf32_gprel16_reloc))
13473
0
      r = _bfd_mips_elf_gprel16_with_gp (input_bfd, symbol, *parent,
13474
0
                 input_section, relocatable,
13475
0
                 data, gp);
13476
3.20k
    else
13477
3.20k
      r = bfd_perform_relocation (input_bfd,
13478
3.20k
          *parent,
13479
3.20k
          data,
13480
3.20k
          input_section,
13481
3.20k
          relocatable ? abfd : NULL,
13482
3.20k
          &error_message);
13483
13484
3.22k
    if (relocatable)
13485
0
      {
13486
0
        asection *os = input_section->output_section;
13487
13488
        /* A partial link, so keep the relocs.  */
13489
0
        os->orelocation[os->reloc_count] = *parent;
13490
0
        os->reloc_count++;
13491
0
      }
13492
13493
3.22k
    if (r != bfd_reloc_ok)
13494
122
      {
13495
122
        switch (r)
13496
122
    {
13497
0
    case bfd_reloc_undefined:
13498
0
      (*link_info->callbacks->undefined_symbol)
13499
0
        (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13500
0
         input_bfd, input_section, (*parent)->address, true);
13501
0
      break;
13502
15
    case bfd_reloc_dangerous:
13503
15
      BFD_ASSERT (error_message != NULL);
13504
15
      (*link_info->callbacks->reloc_dangerous)
13505
15
        (link_info, error_message,
13506
15
         input_bfd, input_section, (*parent)->address);
13507
15
      break;
13508
47
    case bfd_reloc_overflow:
13509
47
      (*link_info->callbacks->reloc_overflow)
13510
47
        (link_info, NULL,
13511
47
         bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13512
47
         (*parent)->howto->name, (*parent)->addend,
13513
47
         input_bfd, input_section, (*parent)->address);
13514
47
      break;
13515
60
    case bfd_reloc_outofrange:
13516
      /* PR ld/13730:
13517
         This error can result when processing some partially
13518
         complete binaries.  Do not abort, but issue an error
13519
         message instead.  */
13520
60
      link_info->callbacks->einfo
13521
        /* xgettext:c-format */
13522
60
        (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
13523
60
         abfd, input_section, * parent);
13524
60
      goto error_return;
13525
13526
0
    case bfd_reloc_notsupported:
13527
      /* PR ld/17512
13528
         This error can result when processing a corrupt binary.
13529
         Do not abort.  Issue an error message instead.  */
13530
0
      link_info->callbacks->einfo
13531
        /* xgettext:c-format */
13532
0
        (_("%X%P: %pB(%pA): relocation \"%pR\" is not supported\n"),
13533
0
         abfd, input_section, * parent);
13534
0
      goto error_return;
13535
13536
0
    default:
13537
      /* PR 17512; file: 90c2a92e.
13538
         Report unexpected results, without aborting.  */
13539
0
      link_info->callbacks->einfo
13540
        /* xgettext:c-format */
13541
0
        (_("%X%P: %pB(%pA): relocation \"%pR\" returns an unrecognized value %x\n"),
13542
0
         abfd, input_section, * parent, r);
13543
0
      break;
13544
122
    }
13545
13546
122
      }
13547
3.22k
  }
13548
111
    }
13549
13550
389
 out:
13551
389
  free (reloc_vector);
13552
389
  return data;
13553
115
}
13554

13555
static bool
13556
mips_elf_relax_delete_bytes (bfd *abfd,
13557
           asection *sec, bfd_vma addr, int count)
13558
0
{
13559
0
  Elf_Internal_Shdr *symtab_hdr;
13560
0
  unsigned int sec_shndx;
13561
0
  bfd_byte *contents;
13562
0
  Elf_Internal_Rela *irel, *irelend;
13563
0
  Elf_Internal_Sym *isym;
13564
0
  Elf_Internal_Sym *isymend;
13565
0
  struct elf_link_hash_entry **sym_hashes;
13566
0
  struct elf_link_hash_entry **end_hashes;
13567
0
  struct elf_link_hash_entry **start_hashes;
13568
0
  unsigned int symcount;
13569
13570
0
  sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
13571
0
  contents = elf_section_data (sec)->this_hdr.contents;
13572
13573
0
  irel = elf_section_data (sec)->relocs;
13574
0
  irelend = irel + sec->reloc_count;
13575
13576
  /* Actually delete the bytes.  */
13577
0
  memmove (contents + addr, contents + addr + count,
13578
0
     (size_t) (sec->size - addr - count));
13579
0
  sec->size -= count;
13580
13581
  /* Adjust all the relocs.  */
13582
0
  for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
13583
0
    {
13584
      /* Get the new reloc address.  */
13585
0
      if (irel->r_offset > addr)
13586
0
  irel->r_offset -= count;
13587
0
    }
13588
13589
0
  BFD_ASSERT (addr % 2 == 0);
13590
0
  BFD_ASSERT (count % 2 == 0);
13591
13592
  /* Adjust the local symbols defined in this section.  */
13593
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13594
0
  isym = (Elf_Internal_Sym *) symtab_hdr->contents;
13595
0
  for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
13596
0
    if (isym->st_shndx == sec_shndx && isym->st_value > addr)
13597
0
      isym->st_value -= count;
13598
13599
  /* Now adjust the global symbols defined in this section.  */
13600
0
  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
13601
0
        - symtab_hdr->sh_info);
13602
0
  sym_hashes = start_hashes = elf_sym_hashes (abfd);
13603
0
  end_hashes = sym_hashes + symcount;
13604
13605
0
  for (; sym_hashes < end_hashes; sym_hashes++)
13606
0
    {
13607
0
      struct elf_link_hash_entry *sym_hash = *sym_hashes;
13608
13609
0
      if ((sym_hash->root.type == bfd_link_hash_defined
13610
0
     || sym_hash->root.type == bfd_link_hash_defweak)
13611
0
    && sym_hash->root.u.def.section == sec)
13612
0
  {
13613
0
    bfd_vma value = sym_hash->root.u.def.value;
13614
13615
0
    if (ELF_ST_IS_MICROMIPS (sym_hash->other))
13616
0
      value &= MINUS_TWO;
13617
0
    if (value > addr)
13618
0
      sym_hash->root.u.def.value -= count;
13619
0
  }
13620
0
    }
13621
13622
0
  return true;
13623
0
}
13624
13625
13626
/* Opcodes needed for microMIPS relaxation as found in
13627
   opcodes/micromips-opc.c.  */
13628
13629
struct opcode_descriptor {
13630
  unsigned long match;
13631
  unsigned long mask;
13632
};
13633
13634
/* The $ra register aka $31.  */
13635
13636
0
#define RA 31
13637
13638
/* 32-bit instruction format register fields.  */
13639
13640
0
#define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
13641
0
#define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
13642
13643
/* Check if a 5-bit register index can be abbreviated to 3 bits.  */
13644
13645
#define OP16_VALID_REG(r) \
13646
0
  ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
13647
13648
13649
/* 32-bit and 16-bit branches.  */
13650
13651
static const struct opcode_descriptor b_insns_32[] = {
13652
  { /* "b", "p",    */ 0x40400000, 0xffff0000 }, /* bgez 0 */
13653
  { /* "b", "p",    */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
13654
  { 0, 0 }  /* End marker for find_match().  */
13655
};
13656
13657
static const struct opcode_descriptor bc_insn_32 =
13658
  { /* "bc(1|2)(ft)", "N,p",  */ 0x42800000, 0xfec30000 };
13659
13660
static const struct opcode_descriptor bz_insn_32 =
13661
  { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
13662
13663
static const struct opcode_descriptor bzal_insn_32 =
13664
  { /* "b(ge|lt)zal", "s,p",  */ 0x40200000, 0xffa00000 };
13665
13666
static const struct opcode_descriptor beq_insn_32 =
13667
  { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
13668
13669
static const struct opcode_descriptor b_insn_16 =
13670
  { /* "b", "mD",   */ 0xcc00,     0xfc00 };
13671
13672
static const struct opcode_descriptor bz_insn_16 =
13673
  { /* "b(eq|ne)z", "md,mE",  */ 0x8c00,     0xdc00 };
13674
13675
13676
/* 32-bit and 16-bit branch EQ and NE zero.  */
13677
13678
/* NOTE: All opcode tables have BEQ/BNE in the same order: first the
13679
   eq and second the ne.  This convention is used when replacing a
13680
   32-bit BEQ/BNE with the 16-bit version.  */
13681
13682
0
#define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
13683
13684
static const struct opcode_descriptor bz_rs_insns_32[] = {
13685
  { /* "beqz",  "s,p",    */ 0x94000000, 0xffe00000 },
13686
  { /* "bnez",  "s,p",    */ 0xb4000000, 0xffe00000 },
13687
  { 0, 0 }  /* End marker for find_match().  */
13688
};
13689
13690
static const struct opcode_descriptor bz_rt_insns_32[] = {
13691
  { /* "beqz",  "t,p",    */ 0x94000000, 0xfc01f000 },
13692
  { /* "bnez",  "t,p",    */ 0xb4000000, 0xfc01f000 },
13693
  { 0, 0 }  /* End marker for find_match().  */
13694
};
13695
13696
static const struct opcode_descriptor bzc_insns_32[] = {
13697
  { /* "beqzc", "s,p",    */ 0x40e00000, 0xffe00000 },
13698
  { /* "bnezc", "s,p",    */ 0x40a00000, 0xffe00000 },
13699
  { 0, 0 }  /* End marker for find_match().  */
13700
};
13701
13702
static const struct opcode_descriptor bz_insns_16[] = {
13703
  { /* "beqz",  "md,mE",  */ 0x8c00,     0xfc00 },
13704
  { /* "bnez",  "md,mE",  */ 0xac00,     0xfc00 },
13705
  { 0, 0 }  /* End marker for find_match().  */
13706
};
13707
13708
/* Switch between a 5-bit register index and its 3-bit shorthand.  */
13709
13710
0
#define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0xf) + 2)
13711
#define BZ16_REG_FIELD(r) (((r) & 7) << 7)
13712
13713
13714
/* 32-bit instructions with a delay slot.  */
13715
13716
static const struct opcode_descriptor jal_insn_32_bd16 =
13717
  { /* "jals",  "a",    */ 0x74000000, 0xfc000000 };
13718
13719
static const struct opcode_descriptor jal_insn_32_bd32 =
13720
  { /* "jal", "a",    */ 0xf4000000, 0xfc000000 };
13721
13722
static const struct opcode_descriptor jal_x_insn_32_bd32 =
13723
  { /* "jal[x]", "a",   */ 0xf0000000, 0xf8000000 };
13724
13725
static const struct opcode_descriptor j_insn_32 =
13726
  { /* "j", "a",    */ 0xd4000000, 0xfc000000 };
13727
13728
static const struct opcode_descriptor jalr_insn_32 =
13729
  { /* "jalr[.hb]", "t,s",  */ 0x00000f3c, 0xfc00efff };
13730
13731
/* This table can be compacted, because no opcode replacement is made.  */
13732
13733
static const struct opcode_descriptor ds_insns_32_bd16[] = {
13734
  { /* "jals",  "a",    */ 0x74000000, 0xfc000000 },
13735
13736
  { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
13737
  { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
13738
13739
  { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
13740
  { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
13741
  { /* "j", "a",    */ 0xd4000000, 0xfc000000 },
13742
  { 0, 0 }  /* End marker for find_match().  */
13743
};
13744
13745
/* This table can be compacted, because no opcode replacement is made.  */
13746
13747
static const struct opcode_descriptor ds_insns_32_bd32[] = {
13748
  { /* "jal[x]", "a",   */ 0xf0000000, 0xf8000000 },
13749
13750
  { /* "jalr[.hb]", "t,s",  */ 0x00000f3c, 0xfc00efff },
13751
  { /* "b(ge|lt)zal", "s,p",  */ 0x40200000, 0xffa00000 },
13752
  { 0, 0 }  /* End marker for find_match().  */
13753
};
13754
13755
13756
/* 16-bit instructions with a delay slot.  */
13757
13758
static const struct opcode_descriptor jalr_insn_16_bd16 =
13759
  { /* "jalrs", "my,mj",  */ 0x45e0,     0xffe0 };
13760
13761
static const struct opcode_descriptor jalr_insn_16_bd32 =
13762
  { /* "jalr",  "my,mj",  */ 0x45c0,     0xffe0 };
13763
13764
static const struct opcode_descriptor jr_insn_16 =
13765
  { /* "jr",  "mj",   */ 0x4580,     0xffe0 };
13766
13767
0
#define JR16_REG(opcode) ((opcode) & 0x1f)
13768
13769
/* This table can be compacted, because no opcode replacement is made.  */
13770
13771
static const struct opcode_descriptor ds_insns_16_bd16[] = {
13772
  { /* "jalrs", "my,mj",  */ 0x45e0,     0xffe0 },
13773
13774
  { /* "b", "mD",   */ 0xcc00,     0xfc00 },
13775
  { /* "b(eq|ne)z", "md,mE",  */ 0x8c00,     0xdc00 },
13776
  { /* "jr",  "mj",   */ 0x4580,     0xffe0 },
13777
  { 0, 0 }  /* End marker for find_match().  */
13778
};
13779
13780
13781
/* LUI instruction.  */
13782
13783
static const struct opcode_descriptor lui_insn =
13784
 { /* "lui",  "s,u",    */ 0x41a00000, 0xffe00000 };
13785
13786
13787
/* ADDIU instruction.  */
13788
13789
static const struct opcode_descriptor addiu_insn =
13790
  { /* "addiu", "t,r,j",  */ 0x30000000, 0xfc000000 };
13791
13792
static const struct opcode_descriptor addiupc_insn =
13793
  { /* "addiu", "mb,$pc,mQ",  */ 0x78000000, 0xfc000000 };
13794
13795
#define ADDIUPC_REG_FIELD(r) \
13796
0
  (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
13797
13798
13799
/* Relaxable instructions in a JAL delay slot: MOVE.  */
13800
13801
/* The 16-bit move has rd in 9:5 and rs in 4:0.  The 32-bit moves
13802
   (ADDU, OR) have rd in 15:11 and rs in 10:16.  */
13803
#define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
13804
#define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
13805
13806
#define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
13807
#define MOVE16_RS_FIELD(r) (((r) & 0x1f)     )
13808
13809
static const struct opcode_descriptor move_insns_32[] = {
13810
  { /* "move",  "d,s",    */ 0x00000290, 0xffe007ff }, /* or   d,s,$0 */
13811
  { /* "move",  "d,s",    */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
13812
  { 0, 0 }  /* End marker for find_match().  */
13813
};
13814
13815
static const struct opcode_descriptor move_insn_16 =
13816
  { /* "move",  "mp,mj",  */ 0x0c00,     0xfc00 };
13817
13818
13819
/* NOP instructions.  */
13820
13821
static const struct opcode_descriptor nop_insn_32 =
13822
  { /* "nop", "",   */ 0x00000000, 0xffffffff };
13823
13824
static const struct opcode_descriptor nop_insn_16 =
13825
  { /* "nop", "",   */ 0x0c00,     0xffff };
13826
13827
13828
/* Instruction match support.  */
13829
13830
0
#define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
13831
13832
static int
13833
find_match (unsigned long opcode, const struct opcode_descriptor insn[])
13834
0
{
13835
0
  unsigned long indx;
13836
13837
0
  for (indx = 0; insn[indx].mask != 0; indx++)
13838
0
    if (MATCH (opcode, insn[indx]))
13839
0
      return indx;
13840
13841
0
  return -1;
13842
0
}
13843
13844
13845
/* Branch and delay slot decoding support.  */
13846
13847
/* If PTR points to what *might* be a 16-bit branch or jump, then
13848
   return the minimum length of its delay slot, otherwise return 0.
13849
   Non-zero results are not definitive as we might be checking against
13850
   the second half of another instruction.  */
13851
13852
static int
13853
check_br16_dslot (bfd *abfd, bfd_byte *ptr)
13854
0
{
13855
0
  unsigned long opcode;
13856
0
  int bdsize;
13857
13858
0
  opcode = bfd_get_16 (abfd, ptr);
13859
0
  if (MATCH (opcode, jalr_insn_16_bd32) != 0)
13860
    /* 16-bit branch/jump with a 32-bit delay slot.  */
13861
0
    bdsize = 4;
13862
0
  else if (MATCH (opcode, jalr_insn_16_bd16) != 0
13863
0
     || find_match (opcode, ds_insns_16_bd16) >= 0)
13864
    /* 16-bit branch/jump with a 16-bit delay slot.  */
13865
0
    bdsize = 2;
13866
0
  else
13867
    /* No delay slot.  */
13868
0
    bdsize = 0;
13869
13870
0
  return bdsize;
13871
0
}
13872
13873
/* If PTR points to what *might* be a 32-bit branch or jump, then
13874
   return the minimum length of its delay slot, otherwise return 0.
13875
   Non-zero results are not definitive as we might be checking against
13876
   the second half of another instruction.  */
13877
13878
static int
13879
check_br32_dslot (bfd *abfd, bfd_byte *ptr)
13880
0
{
13881
0
  unsigned long opcode;
13882
0
  int bdsize;
13883
13884
0
  opcode = bfd_get_micromips_32 (abfd, ptr);
13885
0
  if (find_match (opcode, ds_insns_32_bd32) >= 0)
13886
    /* 32-bit branch/jump with a 32-bit delay slot.  */
13887
0
    bdsize = 4;
13888
0
  else if (find_match (opcode, ds_insns_32_bd16) >= 0)
13889
    /* 32-bit branch/jump with a 16-bit delay slot.  */
13890
0
    bdsize = 2;
13891
0
  else
13892
    /* No delay slot.  */
13893
0
    bdsize = 0;
13894
13895
0
  return bdsize;
13896
0
}
13897
13898
/* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
13899
   that doesn't fiddle with REG, then return TRUE, otherwise FALSE.  */
13900
13901
static bool
13902
check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13903
0
{
13904
0
  unsigned long opcode;
13905
13906
0
  opcode = bfd_get_16 (abfd, ptr);
13907
0
  if (MATCH (opcode, b_insn_16)
13908
            /* B16  */
13909
0
      || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
13910
            /* JR16  */
13911
0
      || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
13912
            /* BEQZ16, BNEZ16  */
13913
0
      || (MATCH (opcode, jalr_insn_16_bd32)
13914
            /* JALR16  */
13915
0
    && reg != JR16_REG (opcode) && reg != RA))
13916
0
    return true;
13917
13918
0
  return false;
13919
0
}
13920
13921
/* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
13922
   then return TRUE, otherwise FALSE.  */
13923
13924
static bool
13925
check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13926
0
{
13927
0
  unsigned long opcode;
13928
13929
0
  opcode = bfd_get_micromips_32 (abfd, ptr);
13930
0
  if (MATCH (opcode, j_insn_32)
13931
            /* J  */
13932
0
      || MATCH (opcode, bc_insn_32)
13933
            /* BC1F, BC1T, BC2F, BC2T  */
13934
0
      || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
13935
            /* JAL, JALX  */
13936
0
      || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
13937
            /* BGEZ, BGTZ, BLEZ, BLTZ  */
13938
0
      || (MATCH (opcode, bzal_insn_32)
13939
            /* BGEZAL, BLTZAL  */
13940
0
    && reg != OP32_SREG (opcode) && reg != RA)
13941
0
      || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
13942
            /* JALR, JALR.HB, BEQ, BNE  */
13943
0
    && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
13944
0
    return true;
13945
13946
0
  return false;
13947
0
}
13948
13949
/* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
13950
   IRELEND) at OFFSET indicate that there must be a compact branch there,
13951
   then return TRUE, otherwise FALSE.  */
13952
13953
static bool
13954
check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
13955
         const Elf_Internal_Rela *internal_relocs,
13956
         const Elf_Internal_Rela *irelend)
13957
0
{
13958
0
  const Elf_Internal_Rela *irel;
13959
0
  unsigned long opcode;
13960
13961
0
  opcode = bfd_get_micromips_32 (abfd, ptr);
13962
0
  if (find_match (opcode, bzc_insns_32) < 0)
13963
0
    return false;
13964
13965
0
  for (irel = internal_relocs; irel < irelend; irel++)
13966
0
    if (irel->r_offset == offset
13967
0
  && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
13968
0
      return true;
13969
13970
0
  return false;
13971
0
}
13972
13973
/* Bitsize checking.  */
13974
#define IS_BITSIZE(val, N)            \
13975
0
  (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1)))   \
13976
0
    - (1ULL << ((N) - 1))) == (val))
13977
13978

13979
bool
13980
_bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
13981
           struct bfd_link_info *link_info,
13982
           bool *again)
13983
0
{
13984
0
  bool insn32 = mips_elf_hash_table (link_info)->insn32;
13985
0
  Elf_Internal_Shdr *symtab_hdr;
13986
0
  Elf_Internal_Rela *internal_relocs;
13987
0
  Elf_Internal_Rela *irel, *irelend;
13988
0
  bfd_byte *contents = NULL;
13989
0
  Elf_Internal_Sym *isymbuf = NULL;
13990
13991
  /* Assume nothing changes.  */
13992
0
  *again = false;
13993
13994
  /* We don't have to do anything for a relocatable link, if
13995
     this section does not have relocs, or if this is not a
13996
     code section.  */
13997
13998
0
  if (bfd_link_relocatable (link_info)
13999
0
      || sec->reloc_count == 0
14000
0
      || (sec->flags & SEC_RELOC) == 0
14001
0
      || (sec->flags & SEC_HAS_CONTENTS) == 0
14002
0
      || (sec->flags & SEC_CODE) == 0)
14003
0
    return true;
14004
14005
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
14006
14007
  /* Get a copy of the native relocations.  */
14008
0
  internal_relocs = (_bfd_elf_link_read_relocs
14009
0
         (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
14010
0
          link_info->keep_memory));
14011
0
  if (internal_relocs == NULL)
14012
0
    goto error_return;
14013
14014
  /* Walk through them looking for relaxing opportunities.  */
14015
0
  irelend = internal_relocs + sec->reloc_count;
14016
0
  for (irel = internal_relocs; irel < irelend; irel++)
14017
0
    {
14018
0
      unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
14019
0
      unsigned int r_type = ELF32_R_TYPE (irel->r_info);
14020
0
      bool target_is_micromips_code_p;
14021
0
      unsigned long opcode;
14022
0
      bfd_vma symval;
14023
0
      bfd_vma pcrval;
14024
0
      bfd_byte *ptr;
14025
0
      int fndopc;
14026
14027
      /* The number of bytes to delete for relaxation and from where
14028
   to delete these bytes starting at irel->r_offset.  */
14029
0
      int delcnt = 0;
14030
0
      int deloff = 0;
14031
14032
      /* If this isn't something that can be relaxed, then ignore
14033
   this reloc.  */
14034
0
      if (r_type != R_MICROMIPS_HI16
14035
0
    && r_type != R_MICROMIPS_PC16_S1
14036
0
    && r_type != R_MICROMIPS_26_S1)
14037
0
  continue;
14038
14039
      /* Get the section contents if we haven't done so already.  */
14040
0
      if (contents == NULL)
14041
0
  {
14042
    /* Get cached copy if it exists.  */
14043
0
    if (elf_section_data (sec)->this_hdr.contents != NULL)
14044
0
      contents = elf_section_data (sec)->this_hdr.contents;
14045
    /* Go get them off disk.  */
14046
0
    else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
14047
0
      goto error_return;
14048
0
  }
14049
0
      ptr = contents + irel->r_offset;
14050
14051
      /* Read this BFD's local symbols if we haven't done so already.  */
14052
0
      if (isymbuf == NULL && symtab_hdr->sh_info != 0)
14053
0
  {
14054
0
    isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
14055
0
    if (isymbuf == NULL)
14056
0
      isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
14057
0
              symtab_hdr->sh_info, 0,
14058
0
              NULL, NULL, NULL);
14059
0
    if (isymbuf == NULL)
14060
0
      goto error_return;
14061
0
  }
14062
14063
      /* Get the value of the symbol referred to by the reloc.  */
14064
0
      if (r_symndx < symtab_hdr->sh_info)
14065
0
  {
14066
    /* A local symbol.  */
14067
0
    Elf_Internal_Sym *isym;
14068
0
    asection *sym_sec;
14069
14070
0
    isym = isymbuf + r_symndx;
14071
0
    if (isym->st_shndx == SHN_UNDEF)
14072
0
      sym_sec = bfd_und_section_ptr;
14073
0
    else if (isym->st_shndx == SHN_ABS)
14074
0
      sym_sec = bfd_abs_section_ptr;
14075
0
    else if (isym->st_shndx == SHN_COMMON)
14076
0
      sym_sec = bfd_com_section_ptr;
14077
0
    else
14078
0
      sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
14079
0
    symval = (isym->st_value
14080
0
        + sym_sec->output_section->vma
14081
0
        + sym_sec->output_offset);
14082
0
    target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
14083
0
  }
14084
0
      else
14085
0
  {
14086
0
    unsigned long indx;
14087
0
    struct elf_link_hash_entry *h;
14088
14089
    /* An external symbol.  */
14090
0
    indx = r_symndx - symtab_hdr->sh_info;
14091
0
    h = elf_sym_hashes (abfd)[indx];
14092
0
    BFD_ASSERT (h != NULL);
14093
14094
0
    if (h->root.type != bfd_link_hash_defined
14095
0
        && h->root.type != bfd_link_hash_defweak)
14096
      /* This appears to be a reference to an undefined
14097
         symbol.  Just ignore it -- it will be caught by the
14098
         regular reloc processing.  */
14099
0
      continue;
14100
14101
0
    symval = (h->root.u.def.value
14102
0
        + h->root.u.def.section->output_section->vma
14103
0
        + h->root.u.def.section->output_offset);
14104
0
    target_is_micromips_code_p = (!h->needs_plt
14105
0
          && ELF_ST_IS_MICROMIPS (h->other));
14106
0
  }
14107
14108
14109
      /* For simplicity of coding, we are going to modify the
14110
   section contents, the section relocs, and the BFD symbol
14111
   table.  We must tell the rest of the code not to free up this
14112
   information.  It would be possible to instead create a table
14113
   of changes which have to be made, as is done in coff-mips.c;
14114
   that would be more work, but would require less memory when
14115
   the linker is run.  */
14116
14117
      /* Only 32-bit instructions relaxed.  */
14118
0
      if (irel->r_offset + 4 > sec->size)
14119
0
  continue;
14120
14121
0
      opcode = bfd_get_micromips_32 (abfd, ptr);
14122
14123
      /* This is the pc-relative distance from the instruction the
14124
   relocation is applied to, to the symbol referred.  */
14125
0
      pcrval = (symval
14126
0
    - (sec->output_section->vma + sec->output_offset)
14127
0
    - irel->r_offset);
14128
14129
      /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
14130
   of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
14131
   R_MICROMIPS_PC23_S2.  The R_MICROMIPS_PC23_S2 condition is
14132
14133
     (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
14134
14135
   where pcrval has first to be adjusted to apply against the LO16
14136
   location (we make the adjustment later on, when we have figured
14137
   out the offset).  */
14138
0
      if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
14139
0
  {
14140
0
    bool bzc = false;
14141
0
    unsigned long nextopc;
14142
0
    unsigned long reg;
14143
0
    bfd_vma offset;
14144
14145
    /* Give up if the previous reloc was a HI16 against this symbol
14146
       too.  */
14147
0
    if (irel > internal_relocs
14148
0
        && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
14149
0
        && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
14150
0
      continue;
14151
14152
    /* Or if the next reloc is not a LO16 against this symbol.  */
14153
0
    if (irel + 1 >= irelend
14154
0
        || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
14155
0
        || ELF32_R_SYM (irel[1].r_info) != r_symndx)
14156
0
      continue;
14157
14158
    /* Or if the second next reloc is a LO16 against this symbol too.  */
14159
0
    if (irel + 2 >= irelend
14160
0
        && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
14161
0
        && ELF32_R_SYM (irel[2].r_info) == r_symndx)
14162
0
      continue;
14163
14164
    /* See if the LUI instruction *might* be in a branch delay slot.
14165
       We check whether what looks like a 16-bit branch or jump is
14166
       actually an immediate argument to a compact branch, and let
14167
       it through if so.  */
14168
0
    if (irel->r_offset >= 2
14169
0
        && check_br16_dslot (abfd, ptr - 2)
14170
0
        && !(irel->r_offset >= 4
14171
0
       && (bzc = check_relocated_bzc (abfd,
14172
0
              ptr - 4, irel->r_offset - 4,
14173
0
              internal_relocs, irelend))))
14174
0
      continue;
14175
0
    if (irel->r_offset >= 4
14176
0
        && !bzc
14177
0
        && check_br32_dslot (abfd, ptr - 4))
14178
0
      continue;
14179
14180
0
    reg = OP32_SREG (opcode);
14181
14182
    /* We only relax adjacent instructions or ones separated with
14183
       a branch or jump that has a delay slot.  The branch or jump
14184
       must not fiddle with the register used to hold the address.
14185
       Subtract 4 for the LUI itself.  */
14186
0
    offset = irel[1].r_offset - irel[0].r_offset;
14187
0
    switch (offset - 4)
14188
0
      {
14189
0
      case 0:
14190
0
        break;
14191
0
      case 2:
14192
0
        if (check_br16 (abfd, ptr + 4, reg))
14193
0
    break;
14194
0
        continue;
14195
0
      case 4:
14196
0
        if (check_br32 (abfd, ptr + 4, reg))
14197
0
    break;
14198
0
        continue;
14199
0
      default:
14200
0
        continue;
14201
0
      }
14202
14203
0
    nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
14204
14205
    /* Give up unless the same register is used with both
14206
       relocations.  */
14207
0
    if (OP32_SREG (nextopc) != reg)
14208
0
      continue;
14209
14210
    /* Now adjust pcrval, subtracting the offset to the LO16 reloc
14211
       and rounding up to take masking of the two LSBs into account.  */
14212
0
    pcrval = ((pcrval - offset + 3) | 3) ^ 3;
14213
14214
    /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16.  */
14215
0
    if (IS_BITSIZE (symval, 16))
14216
0
      {
14217
        /* Fix the relocation's type.  */
14218
0
        irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
14219
14220
        /* Instructions using R_MICROMIPS_LO16 have the base or
14221
     source register in bits 20:16.  This register becomes $0
14222
     (zero) as the result of the R_MICROMIPS_HI16 being 0.  */
14223
0
        nextopc &= ~0x001f0000;
14224
0
        bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
14225
0
        contents + irel[1].r_offset);
14226
0
      }
14227
14228
    /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
14229
       We add 4 to take LUI deletion into account while checking
14230
       the PC-relative distance.  */
14231
0
    else if (symval % 4 == 0
14232
0
       && IS_BITSIZE (pcrval + 4, 25)
14233
0
       && MATCH (nextopc, addiu_insn)
14234
0
       && OP32_TREG (nextopc) == OP32_SREG (nextopc)
14235
0
       && OP16_VALID_REG (OP32_TREG (nextopc)))
14236
0
      {
14237
        /* Fix the relocation's type.  */
14238
0
        irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
14239
14240
        /* Replace ADDIU with the ADDIUPC version.  */
14241
0
        nextopc = (addiupc_insn.match
14242
0
       | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
14243
14244
0
        bfd_put_micromips_32 (abfd, nextopc,
14245
0
            contents + irel[1].r_offset);
14246
0
      }
14247
14248
    /* Can't do anything, give up, sigh...  */
14249
0
    else
14250
0
      continue;
14251
14252
    /* Fix the relocation's type.  */
14253
0
    irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
14254
14255
    /* Delete the LUI instruction: 4 bytes at irel->r_offset.  */
14256
0
    delcnt = 4;
14257
0
    deloff = 0;
14258
0
  }
14259
14260
      /* Compact branch relaxation -- due to the multitude of macros
14261
   employed by the compiler/assembler, compact branches are not
14262
   always generated.  Obviously, this can/will be fixed elsewhere,
14263
   but there is no drawback in double checking it here.  */
14264
0
      else if (r_type == R_MICROMIPS_PC16_S1
14265
0
         && irel->r_offset + 5 < sec->size
14266
0
         && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14267
0
       || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
14268
0
         && ((!insn32
14269
0
        && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
14270
0
          nop_insn_16) ? 2 : 0))
14271
0
       || (irel->r_offset + 7 < sec->size
14272
0
           && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
14273
0
                 ptr + 4),
14274
0
             nop_insn_32) ? 4 : 0))))
14275
0
  {
14276
0
    unsigned long reg;
14277
14278
0
    reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14279
14280
    /* Replace BEQZ/BNEZ with the compact version.  */
14281
0
    opcode = (bzc_insns_32[fndopc].match
14282
0
        | BZC32_REG_FIELD (reg)
14283
0
        | (opcode & 0xffff));   /* Addend value.  */
14284
14285
0
    bfd_put_micromips_32 (abfd, opcode, ptr);
14286
14287
    /* Delete the delay slot NOP: two or four bytes from
14288
       irel->offset + 4; delcnt has already been set above.  */
14289
0
    deloff = 4;
14290
0
  }
14291
14292
      /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1.  We need
14293
   to check the distance from the next instruction, so subtract 2.  */
14294
0
      else if (!insn32
14295
0
         && r_type == R_MICROMIPS_PC16_S1
14296
0
         && IS_BITSIZE (pcrval - 2, 11)
14297
0
         && find_match (opcode, b_insns_32) >= 0)
14298
0
  {
14299
    /* Fix the relocation's type.  */
14300
0
    irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
14301
14302
    /* Replace the 32-bit opcode with a 16-bit opcode.  */
14303
0
    bfd_put_16 (abfd,
14304
0
          (b_insn_16.match
14305
0
           | (opcode & 0x3ff)),   /* Addend value.  */
14306
0
          ptr);
14307
14308
    /* Delete 2 bytes from irel->r_offset + 2.  */
14309
0
    delcnt = 2;
14310
0
    deloff = 2;
14311
0
  }
14312
14313
      /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1.  We need
14314
   to check the distance from the next instruction, so subtract 2.  */
14315
0
      else if (!insn32
14316
0
         && r_type == R_MICROMIPS_PC16_S1
14317
0
         && IS_BITSIZE (pcrval - 2, 8)
14318
0
         && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14319
0
        && OP16_VALID_REG (OP32_SREG (opcode)))
14320
0
       || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
14321
0
           && OP16_VALID_REG (OP32_TREG (opcode)))))
14322
0
  {
14323
0
    unsigned long reg;
14324
14325
0
    reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14326
14327
    /* Fix the relocation's type.  */
14328
0
    irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
14329
14330
    /* Replace the 32-bit opcode with a 16-bit opcode.  */
14331
0
    bfd_put_16 (abfd,
14332
0
          (bz_insns_16[fndopc].match
14333
0
           | BZ16_REG_FIELD (reg)
14334
0
           | (opcode & 0x7f)),    /* Addend value.  */
14335
0
          ptr);
14336
14337
    /* Delete 2 bytes from irel->r_offset + 2.  */
14338
0
    delcnt = 2;
14339
0
    deloff = 2;
14340
0
  }
14341
14342
      /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets.  */
14343
0
      else if (!insn32
14344
0
         && r_type == R_MICROMIPS_26_S1
14345
0
         && target_is_micromips_code_p
14346
0
         && irel->r_offset + 7 < sec->size
14347
0
         && MATCH (opcode, jal_insn_32_bd32))
14348
0
  {
14349
0
    unsigned long n32opc;
14350
0
    bool relaxed = false;
14351
14352
0
    n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
14353
14354
0
    if (MATCH (n32opc, nop_insn_32))
14355
0
      {
14356
        /* Replace delay slot 32-bit NOP with a 16-bit NOP.  */
14357
0
        bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
14358
14359
0
        relaxed = true;
14360
0
      }
14361
0
    else if (find_match (n32opc, move_insns_32) >= 0)
14362
0
      {
14363
        /* Replace delay slot 32-bit MOVE with 16-bit MOVE.  */
14364
0
        bfd_put_16 (abfd,
14365
0
        (move_insn_16.match
14366
0
         | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
14367
0
         | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
14368
0
        ptr + 4);
14369
14370
0
        relaxed = true;
14371
0
      }
14372
    /* Other 32-bit instructions relaxable to 16-bit
14373
       instructions will be handled here later.  */
14374
14375
0
    if (relaxed)
14376
0
      {
14377
        /* JAL with 32-bit delay slot that is changed to a JALS
14378
     with 16-bit delay slot.  */
14379
0
        bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
14380
14381
        /* Delete 2 bytes from irel->r_offset + 6.  */
14382
0
        delcnt = 2;
14383
0
        deloff = 6;
14384
0
      }
14385
0
  }
14386
14387
0
      if (delcnt != 0)
14388
0
  {
14389
    /* Note that we've changed the relocs, section contents, etc.  */
14390
0
    elf_section_data (sec)->relocs = internal_relocs;
14391
0
    elf_section_data (sec)->this_hdr.contents = contents;
14392
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
14393
14394
    /* Delete bytes depending on the delcnt and deloff.  */
14395
0
    if (!mips_elf_relax_delete_bytes (abfd, sec,
14396
0
              irel->r_offset + deloff, delcnt))
14397
0
      goto error_return;
14398
14399
    /* That will change things, so we should relax again.
14400
       Note that this is not required, and it may be slow.  */
14401
0
    *again = true;
14402
0
  }
14403
0
    }
14404
14405
0
  if (isymbuf != NULL
14406
0
      && symtab_hdr->contents != (unsigned char *) isymbuf)
14407
0
    {
14408
0
      if (! link_info->keep_memory)
14409
0
  free (isymbuf);
14410
0
      else
14411
0
  {
14412
    /* Cache the symbols for elf_link_input_bfd.  */
14413
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
14414
0
  }
14415
0
    }
14416
14417
0
  if (contents != NULL
14418
0
      && elf_section_data (sec)->this_hdr.contents != contents)
14419
0
    {
14420
0
      if (! link_info->keep_memory)
14421
0
  free (contents);
14422
0
      else
14423
0
  {
14424
    /* Cache the section contents for elf_link_input_bfd.  */
14425
0
    elf_section_data (sec)->this_hdr.contents = contents;
14426
0
  }
14427
0
    }
14428
14429
0
  if (elf_section_data (sec)->relocs != internal_relocs)
14430
0
    free (internal_relocs);
14431
14432
0
  return true;
14433
14434
0
 error_return:
14435
0
  if (symtab_hdr->contents != (unsigned char *) isymbuf)
14436
0
    free (isymbuf);
14437
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
14438
0
    free (contents);
14439
0
  if (elf_section_data (sec)->relocs != internal_relocs)
14440
0
    free (internal_relocs);
14441
14442
0
  return false;
14443
0
}
14444

14445
/* Create a MIPS ELF linker hash table.  */
14446
14447
struct bfd_link_hash_table *
14448
_bfd_mips_elf_link_hash_table_create (bfd *abfd)
14449
0
{
14450
0
  struct mips_elf_link_hash_table *ret;
14451
0
  size_t amt = sizeof (struct mips_elf_link_hash_table);
14452
14453
0
  ret = bfd_zmalloc (amt);
14454
0
  if (ret == NULL)
14455
0
    return NULL;
14456
14457
0
  if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
14458
0
              mips_elf_link_hash_newfunc,
14459
0
              sizeof (struct mips_elf_link_hash_entry),
14460
0
              MIPS_ELF_DATA))
14461
0
    {
14462
0
      free (ret);
14463
0
      return NULL;
14464
0
    }
14465
0
  ret->root.init_plt_refcount.plist = NULL;
14466
0
  ret->root.init_plt_offset.plist = NULL;
14467
14468
0
  return &ret->root.root;
14469
0
}
14470
14471
/* Likewise, but indicate that the target is VxWorks.  */
14472
14473
struct bfd_link_hash_table *
14474
_bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
14475
0
{
14476
0
  struct bfd_link_hash_table *ret;
14477
14478
0
  ret = _bfd_mips_elf_link_hash_table_create (abfd);
14479
0
  if (ret)
14480
0
    {
14481
0
      struct mips_elf_link_hash_table *htab;
14482
14483
0
      htab = (struct mips_elf_link_hash_table *) ret;
14484
0
      htab->use_plts_and_copy_relocs = true;
14485
0
    }
14486
0
  return ret;
14487
0
}
14488
14489
/* A function that the linker calls if we are allowed to use PLTs
14490
   and copy relocs.  */
14491
14492
void
14493
_bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
14494
0
{
14495
0
  mips_elf_hash_table (info)->use_plts_and_copy_relocs = true;
14496
0
}
14497
14498
/* A function that the linker calls to select between all or only
14499
   32-bit microMIPS instructions, and between making or ignoring
14500
   branch relocation checks for invalid transitions between ISA modes.
14501
   Also record whether we have been configured for a GNU target.  */
14502
14503
void
14504
_bfd_mips_elf_linker_flags (struct bfd_link_info *info, bool insn32,
14505
          bool ignore_branch_isa,
14506
          bool gnu_target)
14507
0
{
14508
0
  mips_elf_hash_table (info)->insn32 = insn32;
14509
0
  mips_elf_hash_table (info)->ignore_branch_isa = ignore_branch_isa;
14510
0
  mips_elf_hash_table (info)->gnu_target = gnu_target;
14511
0
}
14512
14513
/* A function that the linker calls to enable use of compact branches in
14514
   linker generated code for MIPSR6.  */
14515
14516
void
14517
_bfd_mips_elf_compact_branches (struct bfd_link_info *info, bool on)
14518
0
{
14519
0
  mips_elf_hash_table (info)->compact_branches = on;
14520
0
}
14521
14522

14523
/* Structure for saying that BFD machine EXTENSION extends BASE.  */
14524
14525
struct mips_mach_extension
14526
{
14527
  unsigned long extension, base;
14528
};
14529
14530
/* An array that maps 64-bit architectures to the corresponding 32-bit
14531
   architectures.  */
14532
static const struct mips_mach_extension mips_mach_32_64[] =
14533
{
14534
  { bfd_mach_mipsisa64r6, bfd_mach_mipsisa32r6 },
14535
  { bfd_mach_mipsisa64r5, bfd_mach_mipsisa32r5 },
14536
  { bfd_mach_mipsisa64r3, bfd_mach_mipsisa32r3 },
14537
  { bfd_mach_mipsisa64r2, bfd_mach_mipsisa32r2 },
14538
  { bfd_mach_mipsisa64,   bfd_mach_mipsisa32 }
14539
};
14540
14541
/* An array describing how BFD machines relate to one another.  The entries
14542
   are ordered topologically with MIPS I extensions listed last.  */
14543
14544
static const struct mips_mach_extension mips_mach_extensions[] =
14545
{
14546
  /* MIPS64r2 extensions.  */
14547
  { bfd_mach_mips_octeon3, bfd_mach_mips_octeon2 },
14548
  { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
14549
  { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
14550
  { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
14551
  { bfd_mach_mips_gs264e, bfd_mach_mips_gs464e },
14552
  { bfd_mach_mips_gs464e, bfd_mach_mips_gs464 },
14553
  { bfd_mach_mips_gs464, bfd_mach_mipsisa64r2 },
14554
14555
  /* MIPS64 extensions.  */
14556
  { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
14557
  { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
14558
  { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
14559
14560
  /* MIPS V extensions.  */
14561
  { bfd_mach_mipsisa64, bfd_mach_mips5 },
14562
14563
  /* R10000 extensions.  */
14564
  { bfd_mach_mips12000, bfd_mach_mips10000 },
14565
  { bfd_mach_mips14000, bfd_mach_mips10000 },
14566
  { bfd_mach_mips16000, bfd_mach_mips10000 },
14567
14568
  /* R5000 extensions.  Note: the vr5500 ISA is an extension of the core
14569
     vr5400 ISA, but doesn't include the multimedia stuff.  It seems
14570
     better to allow vr5400 and vr5500 code to be merged anyway, since
14571
     many libraries will just use the core ISA.  Perhaps we could add
14572
     some sort of ASE flag if this ever proves a problem.  */
14573
  { bfd_mach_mips5500, bfd_mach_mips5400 },
14574
  { bfd_mach_mips5400, bfd_mach_mips5000 },
14575
14576
  /* MIPS IV extensions.  */
14577
  { bfd_mach_mips5, bfd_mach_mips8000 },
14578
  { bfd_mach_mips10000, bfd_mach_mips8000 },
14579
  { bfd_mach_mips5000, bfd_mach_mips8000 },
14580
  { bfd_mach_mips7000, bfd_mach_mips8000 },
14581
  { bfd_mach_mips9000, bfd_mach_mips8000 },
14582
14583
  /* VR4100 extensions.  */
14584
  { bfd_mach_mips4120, bfd_mach_mips4100 },
14585
  { bfd_mach_mips4111, bfd_mach_mips4100 },
14586
14587
  /* MIPS III extensions.  */
14588
  { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14589
  { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
14590
  { bfd_mach_mips8000, bfd_mach_mips4000 },
14591
  { bfd_mach_mips4650, bfd_mach_mips4000 },
14592
  { bfd_mach_mips4600, bfd_mach_mips4000 },
14593
  { bfd_mach_mips4400, bfd_mach_mips4000 },
14594
  { bfd_mach_mips4300, bfd_mach_mips4000 },
14595
  { bfd_mach_mips4100, bfd_mach_mips4000 },
14596
  { bfd_mach_mips5900, bfd_mach_mips4000 },
14597
14598
  /* MIPS32r3 extensions.  */
14599
  { bfd_mach_mips_interaptiv_mr2, bfd_mach_mipsisa32r3 },
14600
14601
  /* MIPS32r2 extensions.  */
14602
  { bfd_mach_mipsisa32r3, bfd_mach_mipsisa32r2 },
14603
14604
  /* MIPS32 extensions.  */
14605
  { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14606
14607
  /* MIPS II extensions.  */
14608
  { bfd_mach_mips4000, bfd_mach_mips6000 },
14609
  { bfd_mach_mipsisa32, bfd_mach_mips6000 },
14610
  { bfd_mach_mips4010, bfd_mach_mips6000 },
14611
  { bfd_mach_mips_allegrex, bfd_mach_mips6000 },
14612
14613
  /* MIPS I extensions.  */
14614
  { bfd_mach_mips6000, bfd_mach_mips3000 },
14615
  { bfd_mach_mips3900, bfd_mach_mips3000 }
14616
};
14617
14618
/* Return true if bfd machine EXTENSION is the same as BASE, or if
14619
   EXTENSION is the 64-bit equivalent of a 32-bit BASE.  */
14620
14621
static bool
14622
mips_mach_extends_32_64 (unsigned long base, unsigned long extension)
14623
0
{
14624
0
  size_t i;
14625
14626
0
  if (extension == base)
14627
0
    return true;
14628
14629
0
  for (i = 0; i < ARRAY_SIZE (mips_mach_32_64); i++)
14630
0
    if (extension == mips_mach_32_64[i].extension)
14631
0
      return base == mips_mach_32_64[i].base;
14632
14633
0
  return false;
14634
0
}
14635
14636
static bool
14637
mips_mach_extends_p (unsigned long base, unsigned long extension)
14638
0
{
14639
0
  size_t i;
14640
14641
0
  if (mips_mach_extends_32_64 (base, extension))
14642
0
    return true;
14643
14644
0
  for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
14645
0
    if (extension == mips_mach_extensions[i].extension)
14646
0
      {
14647
0
  extension = mips_mach_extensions[i].base;
14648
0
  if (mips_mach_extends_32_64 (base, extension))
14649
0
    return true;
14650
0
      }
14651
14652
0
  return false;
14653
0
}
14654
14655
/* Return the BFD mach for each .MIPS.abiflags ISA Extension.  */
14656
14657
static unsigned long
14658
bfd_mips_isa_ext_mach (unsigned int isa_ext)
14659
0
{
14660
0
  switch (isa_ext)
14661
0
    {
14662
0
    case AFL_EXT_3900:       return bfd_mach_mips3900;
14663
0
    case AFL_EXT_4010:       return bfd_mach_mips4010;
14664
0
    case AFL_EXT_4100:       return bfd_mach_mips4100;
14665
0
    case AFL_EXT_4111:       return bfd_mach_mips4111;
14666
0
    case AFL_EXT_4120:       return bfd_mach_mips4120;
14667
0
    case AFL_EXT_4650:       return bfd_mach_mips4650;
14668
0
    case AFL_EXT_5400:       return bfd_mach_mips5400;
14669
0
    case AFL_EXT_5500:       return bfd_mach_mips5500;
14670
0
    case AFL_EXT_5900:       return bfd_mach_mips5900;
14671
0
    case AFL_EXT_10000:       return bfd_mach_mips10000;
14672
0
    case AFL_EXT_LOONGSON_2E: return bfd_mach_mips_loongson_2e;
14673
0
    case AFL_EXT_LOONGSON_2F: return bfd_mach_mips_loongson_2f;
14674
0
    case AFL_EXT_SB1:       return bfd_mach_mips_sb1;
14675
0
    case AFL_EXT_OCTEON:      return bfd_mach_mips_octeon;
14676
0
    case AFL_EXT_OCTEONP:     return bfd_mach_mips_octeonp;
14677
0
    case AFL_EXT_OCTEON2:     return bfd_mach_mips_octeon2;
14678
0
    case AFL_EXT_XLR:       return bfd_mach_mips_xlr;
14679
0
    default:          return bfd_mach_mips3000;
14680
0
    }
14681
0
}
14682
14683
/* Return the .MIPS.abiflags value representing each ISA Extension.  */
14684
14685
unsigned int
14686
bfd_mips_isa_ext (bfd *abfd)
14687
0
{
14688
0
  switch (bfd_get_mach (abfd))
14689
0
    {
14690
0
    case bfd_mach_mips3900:     return AFL_EXT_3900;
14691
0
    case bfd_mach_mips4010:     return AFL_EXT_4010;
14692
0
    case bfd_mach_mips4100:     return AFL_EXT_4100;
14693
0
    case bfd_mach_mips4111:     return AFL_EXT_4111;
14694
0
    case bfd_mach_mips4120:     return AFL_EXT_4120;
14695
0
    case bfd_mach_mips4650:     return AFL_EXT_4650;
14696
0
    case bfd_mach_mips5400:     return AFL_EXT_5400;
14697
0
    case bfd_mach_mips5500:     return AFL_EXT_5500;
14698
0
    case bfd_mach_mips5900:     return AFL_EXT_5900;
14699
0
    case bfd_mach_mips10000:     return AFL_EXT_10000;
14700
0
    case bfd_mach_mips_loongson_2e: return AFL_EXT_LOONGSON_2E;
14701
0
    case bfd_mach_mips_loongson_2f: return AFL_EXT_LOONGSON_2F;
14702
0
    case bfd_mach_mips_sb1:     return AFL_EXT_SB1;
14703
0
    case bfd_mach_mips_octeon:     return AFL_EXT_OCTEON;
14704
0
    case bfd_mach_mips_octeonp:     return AFL_EXT_OCTEONP;
14705
0
    case bfd_mach_mips_octeon3:     return AFL_EXT_OCTEON3;
14706
0
    case bfd_mach_mips_octeon2:     return AFL_EXT_OCTEON2;
14707
0
    case bfd_mach_mips_xlr:     return AFL_EXT_XLR;
14708
0
    case bfd_mach_mips_interaptiv_mr2:
14709
0
      return AFL_EXT_INTERAPTIV_MR2;
14710
0
    default:          return 0;
14711
0
    }
14712
0
}
14713
14714
/* Encode ISA level and revision as a single value.  */
14715
0
#define LEVEL_REV(LEV,REV) ((LEV) << 3 | (REV))
14716
14717
/* Decode a single value into level and revision.  */
14718
0
#define ISA_LEVEL(LEVREV)  ((LEVREV) >> 3)
14719
0
#define ISA_REV(LEVREV)    ((LEVREV) & 0x7)
14720
14721
/* Update the isa_level, isa_rev, isa_ext fields of abiflags.  */
14722
14723
static void
14724
update_mips_abiflags_isa (bfd *abfd, Elf_Internal_ABIFlags_v0 *abiflags)
14725
0
{
14726
0
  int new_isa = 0;
14727
0
  switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
14728
0
    {
14729
0
    case EF_MIPS_ARCH_1:    new_isa = LEVEL_REV (1, 0); break;
14730
0
    case EF_MIPS_ARCH_2:    new_isa = LEVEL_REV (2, 0); break;
14731
0
    case EF_MIPS_ARCH_3:    new_isa = LEVEL_REV (3, 0); break;
14732
0
    case EF_MIPS_ARCH_4:    new_isa = LEVEL_REV (4, 0); break;
14733
0
    case EF_MIPS_ARCH_5:    new_isa = LEVEL_REV (5, 0); break;
14734
0
    case EF_MIPS_ARCH_32:   new_isa = LEVEL_REV (32, 1); break;
14735
0
    case EF_MIPS_ARCH_32R2: new_isa = LEVEL_REV (32, 2); break;
14736
0
    case EF_MIPS_ARCH_32R6: new_isa = LEVEL_REV (32, 6); break;
14737
0
    case EF_MIPS_ARCH_64:   new_isa = LEVEL_REV (64, 1); break;
14738
0
    case EF_MIPS_ARCH_64R2: new_isa = LEVEL_REV (64, 2); break;
14739
0
    case EF_MIPS_ARCH_64R6: new_isa = LEVEL_REV (64, 6); break;
14740
0
    default:
14741
0
      _bfd_error_handler
14742
  /* xgettext:c-format */
14743
0
  (_("%pB: unknown architecture %s"),
14744
0
   abfd, bfd_printable_name (abfd));
14745
0
    }
14746
14747
0
  if (new_isa > LEVEL_REV (abiflags->isa_level, abiflags->isa_rev))
14748
0
    {
14749
0
      abiflags->isa_level = ISA_LEVEL (new_isa);
14750
0
      abiflags->isa_rev = ISA_REV (new_isa);
14751
0
    }
14752
14753
  /* Update the isa_ext if ABFD describes a further extension.  */
14754
0
  if (mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags->isa_ext),
14755
0
         bfd_get_mach (abfd)))
14756
0
    abiflags->isa_ext = bfd_mips_isa_ext (abfd);
14757
0
}
14758
14759
/* Return true if the given ELF header flags describe a 32-bit binary.  */
14760
14761
static bool
14762
mips_32bit_flags_p (flagword flags)
14763
0
{
14764
0
  return ((flags & EF_MIPS_32BITMODE) != 0
14765
0
    || (flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32
14766
0
    || (flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI32
14767
0
    || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_1
14768
0
    || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_2
14769
0
    || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32
14770
0
    || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R2
14771
0
    || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6);
14772
0
}
14773
14774
/* Infer the content of the ABI flags based on the elf header.  */
14775
14776
static void
14777
infer_mips_abiflags (bfd *abfd, Elf_Internal_ABIFlags_v0* abiflags)
14778
0
{
14779
0
  obj_attribute *in_attr;
14780
14781
0
  memset (abiflags, 0, sizeof (Elf_Internal_ABIFlags_v0));
14782
0
  update_mips_abiflags_isa (abfd, abiflags);
14783
14784
0
  if (mips_32bit_flags_p (elf_elfheader (abfd)->e_flags))
14785
0
    abiflags->gpr_size = AFL_REG_32;
14786
0
  else
14787
0
    abiflags->gpr_size = AFL_REG_64;
14788
14789
0
  abiflags->cpr1_size = AFL_REG_NONE;
14790
14791
0
  in_attr = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
14792
0
  abiflags->fp_abi = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14793
14794
0
  if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_SINGLE
14795
0
      || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_XX
14796
0
      || (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14797
0
    && abiflags->gpr_size == AFL_REG_32))
14798
0
    abiflags->cpr1_size = AFL_REG_32;
14799
0
  else if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14800
0
     || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64
14801
0
     || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64A)
14802
0
    abiflags->cpr1_size = AFL_REG_64;
14803
14804
0
  abiflags->cpr2_size = AFL_REG_NONE;
14805
14806
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
14807
0
    abiflags->ases |= AFL_ASE_MDMX;
14808
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
14809
0
    abiflags->ases |= AFL_ASE_MIPS16;
14810
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14811
0
    abiflags->ases |= AFL_ASE_MICROMIPS;
14812
14813
0
  if (abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_ANY
14814
0
      && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_SOFT
14815
0
      && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_64A
14816
0
      && abiflags->isa_level >= 32
14817
0
      && abiflags->ases != AFL_ASE_LOONGSON_EXT)
14818
0
    abiflags->flags1 |= AFL_FLAGS1_ODDSPREG;
14819
0
}
14820
14821
/* We need to use a special link routine to handle the .reginfo and
14822
   the .mdebug sections.  We need to merge all instances of these
14823
   sections together, not write them all out sequentially.  */
14824
14825
bool
14826
_bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
14827
0
{
14828
0
  asection *o;
14829
0
  struct bfd_link_order *p;
14830
0
  asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
14831
0
  asection *rtproc_sec, *abiflags_sec;
14832
0
  Elf32_RegInfo reginfo;
14833
0
  struct ecoff_debug_info debug;
14834
0
  struct mips_htab_traverse_info hti;
14835
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14836
0
  const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
14837
0
  HDRR *symhdr = &debug.symbolic_header;
14838
0
  void *mdebug_handle = NULL;
14839
0
  asection *s;
14840
0
  EXTR esym;
14841
0
  unsigned int i;
14842
0
  bfd_size_type amt;
14843
0
  struct mips_elf_link_hash_table *htab;
14844
14845
0
  static const char * const secname[] =
14846
0
  {
14847
0
    ".text", ".init", ".fini", ".data",
14848
0
    ".rodata", ".sdata", ".sbss", ".bss"
14849
0
  };
14850
0
  static const int sc[] =
14851
0
  {
14852
0
    scText, scInit, scFini, scData,
14853
0
    scRData, scSData, scSBss, scBss
14854
0
  };
14855
14856
0
  htab = mips_elf_hash_table (info);
14857
0
  BFD_ASSERT (htab != NULL);
14858
14859
  /* Sort the dynamic symbols so that those with GOT entries come after
14860
     those without.  */
14861
0
  if (!mips_elf_sort_hash_table (abfd, info))
14862
0
    return false;
14863
14864
  /* Create any scheduled LA25 stubs.  */
14865
0
  hti.info = info;
14866
0
  hti.output_bfd = abfd;
14867
0
  hti.error = false;
14868
0
  htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
14869
0
  if (hti.error)
14870
0
    return false;
14871
14872
  /* Get a value for the GP register.  */
14873
0
  if (elf_gp (abfd) == 0)
14874
0
    {
14875
0
      struct bfd_link_hash_entry *h;
14876
14877
0
      h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
14878
0
      if (h != NULL && h->type == bfd_link_hash_defined)
14879
0
  elf_gp (abfd) = (h->u.def.value
14880
0
       + h->u.def.section->output_section->vma
14881
0
       + h->u.def.section->output_offset);
14882
0
      else if (htab->root.target_os == is_vxworks
14883
0
         && (h = bfd_link_hash_lookup (info->hash,
14884
0
               "_GLOBAL_OFFSET_TABLE_",
14885
0
               false, false, true))
14886
0
         && h->type == bfd_link_hash_defined)
14887
0
  elf_gp (abfd) = (h->u.def.section->output_section->vma
14888
0
       + h->u.def.section->output_offset
14889
0
       + h->u.def.value);
14890
0
      else if (bfd_link_relocatable (info))
14891
0
  {
14892
0
    bfd_vma lo = MINUS_ONE;
14893
14894
    /* Find the GP-relative section with the lowest offset.  */
14895
0
    for (o = abfd->sections; o != NULL; o = o->next)
14896
0
      if (o->vma < lo
14897
0
    && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
14898
0
        lo = o->vma;
14899
14900
    /* And calculate GP relative to that.  */
14901
0
    elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
14902
0
  }
14903
0
      else
14904
0
  {
14905
    /* If the relocate_section function needs to do a reloc
14906
       involving the GP value, it should make a reloc_dangerous
14907
       callback to warn that GP is not defined.  */
14908
0
  }
14909
0
    }
14910
14911
  /* Go through the sections and collect the .reginfo and .mdebug
14912
     information.  */
14913
0
  abiflags_sec = NULL;
14914
0
  reginfo_sec = NULL;
14915
0
  mdebug_sec = NULL;
14916
0
  gptab_data_sec = NULL;
14917
0
  gptab_bss_sec = NULL;
14918
0
  for (o = abfd->sections; o != NULL; o = o->next)
14919
0
    {
14920
0
      if (strcmp (o->name, ".MIPS.abiflags") == 0)
14921
0
  {
14922
    /* We have found the .MIPS.abiflags section in the output file.
14923
       Look through all the link_orders comprising it and remove them.
14924
       The data is merged in _bfd_mips_elf_merge_private_bfd_data.  */
14925
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
14926
0
      {
14927
0
        asection *input_section;
14928
14929
0
        if (p->type != bfd_indirect_link_order)
14930
0
    {
14931
0
      if (p->type == bfd_data_link_order)
14932
0
        continue;
14933
0
      abort ();
14934
0
    }
14935
14936
0
        input_section = p->u.indirect.section;
14937
14938
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
14939
     elf_link_input_bfd ignores this section.  */
14940
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
14941
0
      }
14942
14943
    /* Size has been set in _bfd_mips_elf_late_size_sections.  */
14944
0
    BFD_ASSERT(o->size == sizeof (Elf_External_ABIFlags_v0));
14945
14946
    /* Skip this section later on (I don't think this currently
14947
       matters, but someday it might).  */
14948
0
    o->map_head.link_order = NULL;
14949
14950
0
    abiflags_sec = o;
14951
0
  }
14952
14953
0
      if (strcmp (o->name, ".reginfo") == 0)
14954
0
  {
14955
0
    memset (&reginfo, 0, sizeof reginfo);
14956
14957
    /* We have found the .reginfo section in the output file.
14958
       Look through all the link_orders comprising it and merge
14959
       the information together.  */
14960
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
14961
0
      {
14962
0
        asection *input_section;
14963
0
        bfd *input_bfd;
14964
0
        Elf32_External_RegInfo ext;
14965
0
        Elf32_RegInfo sub;
14966
0
        bfd_size_type sz;
14967
14968
0
        if (p->type != bfd_indirect_link_order)
14969
0
    {
14970
0
      if (p->type == bfd_data_link_order)
14971
0
        continue;
14972
0
      abort ();
14973
0
    }
14974
14975
0
        input_section = p->u.indirect.section;
14976
0
        input_bfd = input_section->owner;
14977
14978
0
        sz = (input_section->size < sizeof (ext)
14979
0
        ? input_section->size : sizeof (ext));
14980
0
        memset (&ext, 0, sizeof (ext));
14981
0
        if (! bfd_get_section_contents (input_bfd, input_section,
14982
0
                &ext, 0, sz))
14983
0
    return false;
14984
14985
0
        bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
14986
14987
0
        reginfo.ri_gprmask |= sub.ri_gprmask;
14988
0
        reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
14989
0
        reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
14990
0
        reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
14991
0
        reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
14992
14993
        /* ri_gp_value is set by the function
14994
     `_bfd_mips_elf_section_processing' when the section is
14995
     finally written out.  */
14996
14997
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
14998
     elf_link_input_bfd ignores this section.  */
14999
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
15000
0
      }
15001
15002
    /* Size has been set in _bfd_mips_elf_late_size_sections.  */
15003
0
    BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
15004
15005
    /* Skip this section later on (I don't think this currently
15006
       matters, but someday it might).  */
15007
0
    o->map_head.link_order = NULL;
15008
15009
0
    reginfo_sec = o;
15010
0
  }
15011
15012
0
      if (strcmp (o->name, ".mdebug") == 0)
15013
0
  {
15014
0
    struct extsym_info einfo;
15015
0
    bfd_vma last;
15016
15017
    /* We have found the .mdebug section in the output file.
15018
       Look through all the link_orders comprising it and merge
15019
       the information together.  */
15020
0
    symhdr->magic = swap->sym_magic;
15021
    /* FIXME: What should the version stamp be?  */
15022
0
    symhdr->vstamp = 0;
15023
0
    symhdr->ilineMax = 0;
15024
0
    symhdr->cbLine = 0;
15025
0
    symhdr->idnMax = 0;
15026
0
    symhdr->ipdMax = 0;
15027
0
    symhdr->isymMax = 0;
15028
0
    symhdr->ioptMax = 0;
15029
0
    symhdr->iauxMax = 0;
15030
0
    symhdr->issMax = 0;
15031
0
    symhdr->issExtMax = 0;
15032
0
    symhdr->ifdMax = 0;
15033
0
    symhdr->crfd = 0;
15034
0
    symhdr->iextMax = 0;
15035
15036
    /* We accumulate the debugging information itself in the
15037
       debug_info structure.  */
15038
0
    debug.alloc_syments = false;
15039
0
    debug.line = NULL;
15040
0
    debug.external_dnr = NULL;
15041
0
    debug.external_pdr = NULL;
15042
0
    debug.external_sym = NULL;
15043
0
    debug.external_opt = NULL;
15044
0
    debug.external_aux = NULL;
15045
0
    debug.ss = NULL;
15046
0
    debug.ssext = debug.ssext_end = NULL;
15047
0
    debug.external_fdr = NULL;
15048
0
    debug.external_rfd = NULL;
15049
0
    debug.external_ext = debug.external_ext_end = NULL;
15050
15051
0
    mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
15052
0
    if (mdebug_handle == NULL)
15053
0
      return false;
15054
15055
0
    esym.jmptbl = 0;
15056
0
    esym.cobol_main = 0;
15057
0
    esym.weakext = 0;
15058
0
    esym.reserved = 0;
15059
0
    esym.ifd = ifdNil;
15060
0
    esym.asym.iss = issNil;
15061
0
    esym.asym.st = stLocal;
15062
0
    esym.asym.reserved = 0;
15063
0
    esym.asym.index = indexNil;
15064
0
    last = 0;
15065
0
    for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
15066
0
      {
15067
0
        esym.asym.sc = sc[i];
15068
0
        s = bfd_get_section_by_name (abfd, secname[i]);
15069
0
        if (s != NULL)
15070
0
    {
15071
0
      esym.asym.value = s->vma;
15072
0
      last = s->vma + s->size;
15073
0
    }
15074
0
        else
15075
0
    esym.asym.value = last;
15076
0
        if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
15077
0
             secname[i], &esym))
15078
0
    return false;
15079
0
      }
15080
15081
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
15082
0
      {
15083
0
        asection *input_section;
15084
0
        bfd *input_bfd;
15085
0
        const struct ecoff_debug_swap *input_swap;
15086
0
        struct ecoff_debug_info input_debug;
15087
0
        char *eraw_src;
15088
0
        char *eraw_end;
15089
15090
0
        if (p->type != bfd_indirect_link_order)
15091
0
    {
15092
0
      if (p->type == bfd_data_link_order)
15093
0
        continue;
15094
0
      abort ();
15095
0
    }
15096
15097
0
        input_section = p->u.indirect.section;
15098
0
        input_bfd = input_section->owner;
15099
15100
0
        if (!is_mips_elf (input_bfd))
15101
0
    {
15102
      /* I don't know what a non MIPS ELF bfd would be
15103
         doing with a .mdebug section, but I don't really
15104
         want to deal with it.  */
15105
0
      continue;
15106
0
    }
15107
15108
0
        input_swap = (get_elf_backend_data (input_bfd)
15109
0
          ->elf_backend_ecoff_debug_swap);
15110
15111
0
        BFD_ASSERT (p->size == input_section->size);
15112
15113
        /* The ECOFF linking code expects that we have already
15114
     read in the debugging information and set up an
15115
     ecoff_debug_info structure, so we do that now.  */
15116
0
        if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
15117
0
               &input_debug))
15118
0
    return false;
15119
15120
0
        if (! (bfd_ecoff_debug_accumulate
15121
0
         (mdebug_handle, abfd, &debug, swap, input_bfd,
15122
0
          &input_debug, input_swap, info)))
15123
0
    {
15124
0
      _bfd_ecoff_free_ecoff_debug_info (&input_debug);
15125
0
      return false;
15126
0
    }
15127
15128
        /* Loop through the external symbols.  For each one with
15129
     interesting information, try to find the symbol in
15130
     the linker global hash table and save the information
15131
     for the output external symbols.  */
15132
0
        eraw_src = input_debug.external_ext;
15133
0
        eraw_end = (eraw_src
15134
0
        + (input_debug.symbolic_header.iextMax
15135
0
           * input_swap->external_ext_size));
15136
0
        for (;
15137
0
       eraw_src < eraw_end;
15138
0
       eraw_src += input_swap->external_ext_size)
15139
0
    {
15140
0
      EXTR ext;
15141
0
      const char *name;
15142
0
      struct mips_elf_link_hash_entry *h;
15143
15144
0
      (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
15145
0
      if (ext.asym.sc == scNil
15146
0
          || ext.asym.sc == scUndefined
15147
0
          || ext.asym.sc == scSUndefined)
15148
0
        continue;
15149
15150
0
      name = input_debug.ssext + ext.asym.iss;
15151
0
      h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
15152
0
             name, false, false, true);
15153
0
      if (h == NULL || h->esym.ifd != -2)
15154
0
        continue;
15155
15156
0
      if (ext.ifd != -1)
15157
0
        {
15158
0
          BFD_ASSERT (ext.ifd
15159
0
          < input_debug.symbolic_header.ifdMax);
15160
0
          ext.ifd = input_debug.ifdmap[ext.ifd];
15161
0
        }
15162
15163
0
      h->esym = ext;
15164
0
    }
15165
15166
        /* Free up the information we just read.  */
15167
0
        _bfd_ecoff_free_ecoff_debug_info (&input_debug);
15168
15169
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
15170
     elf_link_input_bfd ignores this section.  */
15171
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
15172
0
      }
15173
15174
0
    if (SGI_COMPAT (abfd) && bfd_link_pic (info))
15175
0
      {
15176
        /* Create .rtproc section.  */
15177
0
        rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
15178
0
        if (rtproc_sec == NULL)
15179
0
    {
15180
0
      flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
15181
0
            | SEC_LINKER_CREATED | SEC_READONLY);
15182
15183
0
      rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
15184
0
                   ".rtproc",
15185
0
                   flags);
15186
0
      if (rtproc_sec == NULL
15187
0
          || !bfd_set_section_alignment (rtproc_sec, 4))
15188
0
        return false;
15189
0
    }
15190
15191
0
        if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
15192
0
                 info, rtproc_sec,
15193
0
                 &debug))
15194
0
    return false;
15195
0
      }
15196
15197
    /* Build the external symbol information.  */
15198
0
    einfo.abfd = abfd;
15199
0
    einfo.info = info;
15200
0
    einfo.debug = &debug;
15201
0
    einfo.swap = swap;
15202
0
    einfo.failed = false;
15203
0
    mips_elf_link_hash_traverse (mips_elf_hash_table (info),
15204
0
               mips_elf_output_extsym, &einfo);
15205
0
    if (einfo.failed)
15206
0
      return false;
15207
15208
    /* Set the size of the .mdebug section.  */
15209
0
    o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
15210
15211
    /* Skip this section later on (I don't think this currently
15212
       matters, but someday it might).  */
15213
0
    o->map_head.link_order = NULL;
15214
15215
0
    mdebug_sec = o;
15216
0
  }
15217
15218
0
      if (startswith (o->name, ".gptab."))
15219
0
  {
15220
0
    const char *subname;
15221
0
    unsigned int c;
15222
0
    Elf32_gptab *tab;
15223
0
    Elf32_External_gptab *ext_tab;
15224
0
    unsigned int j;
15225
15226
    /* The .gptab.sdata and .gptab.sbss sections hold
15227
       information describing how the small data area would
15228
       change depending upon the -G switch.  These sections
15229
       not used in executables files.  */
15230
0
    if (! bfd_link_relocatable (info))
15231
0
      {
15232
0
        for (p = o->map_head.link_order; p != NULL; p = p->next)
15233
0
    {
15234
0
      asection *input_section;
15235
15236
0
      if (p->type != bfd_indirect_link_order)
15237
0
        {
15238
0
          if (p->type == bfd_data_link_order)
15239
0
      continue;
15240
0
          abort ();
15241
0
        }
15242
15243
0
      input_section = p->u.indirect.section;
15244
15245
      /* Hack: reset the SEC_HAS_CONTENTS flag so that
15246
         elf_link_input_bfd ignores this section.  */
15247
0
      input_section->flags &= ~SEC_HAS_CONTENTS;
15248
0
    }
15249
15250
        /* Skip this section later on (I don't think this
15251
     currently matters, but someday it might).  */
15252
0
        o->map_head.link_order = NULL;
15253
15254
        /* Really remove the section.  */
15255
0
        bfd_section_list_remove (abfd, o);
15256
0
        --abfd->section_count;
15257
15258
0
        continue;
15259
0
      }
15260
15261
    /* There is one gptab for initialized data, and one for
15262
       uninitialized data.  */
15263
0
    if (strcmp (o->name, ".gptab.sdata") == 0)
15264
0
      gptab_data_sec = o;
15265
0
    else if (strcmp (o->name, ".gptab.sbss") == 0)
15266
0
      gptab_bss_sec = o;
15267
0
    else
15268
0
      {
15269
0
        _bfd_error_handler
15270
    /* xgettext:c-format */
15271
0
    (_("%pB: illegal section name `%pA'"), abfd, o);
15272
0
        bfd_set_error (bfd_error_nonrepresentable_section);
15273
0
        return false;
15274
0
      }
15275
15276
    /* The linker script always combines .gptab.data and
15277
       .gptab.sdata into .gptab.sdata, and likewise for
15278
       .gptab.bss and .gptab.sbss.  It is possible that there is
15279
       no .sdata or .sbss section in the output file, in which
15280
       case we must change the name of the output section.  */
15281
0
    subname = o->name + sizeof ".gptab" - 1;
15282
0
    if (bfd_get_section_by_name (abfd, subname) == NULL)
15283
0
      {
15284
0
        if (o == gptab_data_sec)
15285
0
    o->name = ".gptab.data";
15286
0
        else
15287
0
    o->name = ".gptab.bss";
15288
0
        subname = o->name + sizeof ".gptab" - 1;
15289
0
        BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
15290
0
      }
15291
15292
    /* Set up the first entry.  */
15293
0
    c = 1;
15294
0
    amt = c * sizeof (Elf32_gptab);
15295
0
    tab = bfd_malloc (amt);
15296
0
    if (tab == NULL)
15297
0
      return false;
15298
0
    tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
15299
0
    tab[0].gt_header.gt_unused = 0;
15300
15301
    /* Combine the input sections.  */
15302
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
15303
0
      {
15304
0
        asection *input_section;
15305
0
        bfd *input_bfd;
15306
0
        bfd_size_type size;
15307
0
        unsigned long last;
15308
0
        bfd_size_type gpentry;
15309
15310
0
        if (p->type != bfd_indirect_link_order)
15311
0
    {
15312
0
      if (p->type == bfd_data_link_order)
15313
0
        continue;
15314
0
      abort ();
15315
0
    }
15316
15317
0
        input_section = p->u.indirect.section;
15318
0
        input_bfd = input_section->owner;
15319
15320
        /* Combine the gptab entries for this input section one
15321
     by one.  We know that the input gptab entries are
15322
     sorted by ascending -G value.  */
15323
0
        size = input_section->size;
15324
0
        last = 0;
15325
0
        for (gpentry = sizeof (Elf32_External_gptab);
15326
0
       gpentry < size;
15327
0
       gpentry += sizeof (Elf32_External_gptab))
15328
0
    {
15329
0
      Elf32_External_gptab ext_gptab;
15330
0
      Elf32_gptab int_gptab;
15331
0
      unsigned long val;
15332
0
      unsigned long add;
15333
0
      bool exact;
15334
0
      unsigned int look;
15335
15336
0
      if (! (bfd_get_section_contents
15337
0
       (input_bfd, input_section, &ext_gptab, gpentry,
15338
0
        sizeof (Elf32_External_gptab))))
15339
0
        {
15340
0
          free (tab);
15341
0
          return false;
15342
0
        }
15343
15344
0
      bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
15345
0
            &int_gptab);
15346
0
      val = int_gptab.gt_entry.gt_g_value;
15347
0
      add = int_gptab.gt_entry.gt_bytes - last;
15348
15349
0
      exact = false;
15350
0
      for (look = 1; look < c; look++)
15351
0
        {
15352
0
          if (tab[look].gt_entry.gt_g_value >= val)
15353
0
      tab[look].gt_entry.gt_bytes += add;
15354
15355
0
          if (tab[look].gt_entry.gt_g_value == val)
15356
0
      exact = true;
15357
0
        }
15358
15359
0
      if (! exact)
15360
0
        {
15361
0
          Elf32_gptab *new_tab;
15362
0
          unsigned int max;
15363
15364
          /* We need a new table entry.  */
15365
0
          amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
15366
0
          new_tab = bfd_realloc (tab, amt);
15367
0
          if (new_tab == NULL)
15368
0
      {
15369
0
        free (tab);
15370
0
        return false;
15371
0
      }
15372
0
          tab = new_tab;
15373
0
          tab[c].gt_entry.gt_g_value = val;
15374
0
          tab[c].gt_entry.gt_bytes = add;
15375
15376
          /* Merge in the size for the next smallest -G
15377
       value, since that will be implied by this new
15378
       value.  */
15379
0
          max = 0;
15380
0
          for (look = 1; look < c; look++)
15381
0
      {
15382
0
        if (tab[look].gt_entry.gt_g_value < val
15383
0
            && (max == 0
15384
0
          || (tab[look].gt_entry.gt_g_value
15385
0
              > tab[max].gt_entry.gt_g_value)))
15386
0
          max = look;
15387
0
      }
15388
0
          if (max != 0)
15389
0
      tab[c].gt_entry.gt_bytes +=
15390
0
        tab[max].gt_entry.gt_bytes;
15391
15392
0
          ++c;
15393
0
        }
15394
15395
0
      last = int_gptab.gt_entry.gt_bytes;
15396
0
    }
15397
15398
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
15399
     elf_link_input_bfd ignores this section.  */
15400
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
15401
0
      }
15402
15403
    /* The table must be sorted by -G value.  */
15404
0
    if (c > 2)
15405
0
      qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
15406
15407
    /* Swap out the table.  */
15408
0
    amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
15409
0
    ext_tab = bfd_alloc (abfd, amt);
15410
0
    if (ext_tab == NULL)
15411
0
      {
15412
0
        free (tab);
15413
0
        return false;
15414
0
      }
15415
15416
0
    for (j = 0; j < c; j++)
15417
0
      bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
15418
0
    free (tab);
15419
15420
0
    o->size = c * sizeof (Elf32_External_gptab);
15421
0
    o->contents = (bfd_byte *) ext_tab;
15422
15423
    /* Skip this section later on (I don't think this currently
15424
       matters, but someday it might).  */
15425
0
    o->map_head.link_order = NULL;
15426
0
  }
15427
0
    }
15428
15429
  /* Invoke the regular ELF backend linker to do all the work.  */
15430
0
  if (!bfd_elf_final_link (abfd, info))
15431
0
    return false;
15432
15433
  /* Now write out the computed sections.  */
15434
15435
0
  if (abiflags_sec != NULL)
15436
0
    {
15437
0
      Elf_External_ABIFlags_v0 ext;
15438
0
      Elf_Internal_ABIFlags_v0 *abiflags;
15439
15440
0
      abiflags = &mips_elf_tdata (abfd)->abiflags;
15441
15442
      /* Set up the abiflags if no valid input sections were found.  */
15443
0
      if (!mips_elf_tdata (abfd)->abiflags_valid)
15444
0
  {
15445
0
    infer_mips_abiflags (abfd, abiflags);
15446
0
    mips_elf_tdata (abfd)->abiflags_valid = true;
15447
0
  }
15448
0
      bfd_mips_elf_swap_abiflags_v0_out (abfd, abiflags, &ext);
15449
0
      if (! bfd_set_section_contents (abfd, abiflags_sec, &ext, 0, sizeof ext))
15450
0
  return false;
15451
0
    }
15452
15453
0
  if (reginfo_sec != NULL)
15454
0
    {
15455
0
      Elf32_External_RegInfo ext;
15456
15457
0
      bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
15458
0
      if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
15459
0
  return false;
15460
0
    }
15461
15462
0
  if (mdebug_sec != NULL)
15463
0
    {
15464
0
      BFD_ASSERT (abfd->output_has_begun);
15465
0
      if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
15466
0
                 swap, info,
15467
0
                 mdebug_sec->filepos))
15468
0
  return false;
15469
15470
0
      bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
15471
0
    }
15472
15473
0
  if (gptab_data_sec != NULL)
15474
0
    {
15475
0
      if (! bfd_set_section_contents (abfd, gptab_data_sec,
15476
0
              gptab_data_sec->contents,
15477
0
              0, gptab_data_sec->size))
15478
0
  return false;
15479
0
    }
15480
15481
0
  if (gptab_bss_sec != NULL)
15482
0
    {
15483
0
      if (! bfd_set_section_contents (abfd, gptab_bss_sec,
15484
0
              gptab_bss_sec->contents,
15485
0
              0, gptab_bss_sec->size))
15486
0
  return false;
15487
0
    }
15488
15489
0
  if (SGI_COMPAT (abfd))
15490
0
    {
15491
0
      rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
15492
0
      if (rtproc_sec != NULL)
15493
0
  {
15494
0
    if (! bfd_set_section_contents (abfd, rtproc_sec,
15495
0
            rtproc_sec->contents,
15496
0
            0, rtproc_sec->size))
15497
0
      return false;
15498
0
  }
15499
0
    }
15500
15501
0
  return true;
15502
0
}
15503

15504
/* Merge object file header flags from IBFD into OBFD.  Raise an error
15505
   if there are conflicting settings.  */
15506
15507
static bool
15508
mips_elf_merge_obj_e_flags (bfd *ibfd, struct bfd_link_info *info)
15509
0
{
15510
0
  bfd *obfd = info->output_bfd;
15511
0
  struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15512
0
  flagword old_flags;
15513
0
  flagword new_flags;
15514
0
  bool ok;
15515
15516
0
  new_flags = elf_elfheader (ibfd)->e_flags;
15517
0
  elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
15518
0
  old_flags = elf_elfheader (obfd)->e_flags;
15519
15520
  /* Check flag compatibility.  */
15521
15522
0
  new_flags &= ~EF_MIPS_NOREORDER;
15523
0
  old_flags &= ~EF_MIPS_NOREORDER;
15524
15525
  /* Some IRIX 6 BSD-compatibility objects have this bit set.  It
15526
     doesn't seem to matter.  */
15527
0
  new_flags &= ~EF_MIPS_XGOT;
15528
0
  old_flags &= ~EF_MIPS_XGOT;
15529
15530
  /* MIPSpro generates ucode info in n64 objects.  Again, we should
15531
     just be able to ignore this.  */
15532
0
  new_flags &= ~EF_MIPS_UCODE;
15533
0
  old_flags &= ~EF_MIPS_UCODE;
15534
15535
  /* DSOs should only be linked with CPIC code.  */
15536
0
  if ((ibfd->flags & DYNAMIC) != 0)
15537
0
    new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
15538
15539
0
  if (new_flags == old_flags)
15540
0
    return true;
15541
15542
0
  ok = true;
15543
15544
0
  if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
15545
0
      != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
15546
0
    {
15547
0
      _bfd_error_handler
15548
0
  (_("%pB: warning: linking abicalls files with non-abicalls files"),
15549
0
   ibfd);
15550
0
      ok = true;
15551
0
    }
15552
15553
0
  if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
15554
0
    elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
15555
0
  if (! (new_flags & EF_MIPS_PIC))
15556
0
    elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
15557
15558
0
  new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15559
0
  old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15560
15561
  /* Compare the ISAs.  */
15562
0
  if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
15563
0
    {
15564
0
      _bfd_error_handler
15565
0
  (_("%pB: linking 32-bit code with 64-bit code"),
15566
0
   ibfd);
15567
0
      ok = false;
15568
0
    }
15569
0
  else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
15570
0
    {
15571
      /* OBFD's ISA isn't the same as, or an extension of, IBFD's.  */
15572
0
      if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
15573
0
  {
15574
    /* Copy the architecture info from IBFD to OBFD.  Also copy
15575
       the 32-bit flag (if set) so that we continue to recognise
15576
       OBFD as a 32-bit binary.  */
15577
0
    bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
15578
0
    elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
15579
0
    elf_elfheader (obfd)->e_flags
15580
0
      |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15581
15582
    /* Update the ABI flags isa_level, isa_rev, isa_ext fields.  */
15583
0
    update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
15584
15585
    /* Copy across the ABI flags if OBFD doesn't use them
15586
       and if that was what caused us to treat IBFD as 32-bit.  */
15587
0
    if ((old_flags & EF_MIPS_ABI) == 0
15588
0
        && mips_32bit_flags_p (new_flags)
15589
0
        && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
15590
0
      elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
15591
0
  }
15592
0
      else
15593
0
  {
15594
    /* The ISAs aren't compatible.  */
15595
0
    _bfd_error_handler
15596
      /* xgettext:c-format */
15597
0
      (_("%pB: linking %s module with previous %s modules"),
15598
0
       ibfd,
15599
0
       bfd_printable_name (ibfd),
15600
0
       bfd_printable_name (obfd));
15601
0
    ok = false;
15602
0
  }
15603
0
    }
15604
15605
0
  new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15606
0
  old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15607
15608
  /* Compare ABIs.  The 64-bit ABI does not use EF_MIPS_ABI.  But, it
15609
     does set EI_CLASS differently from any 32-bit ABI.  */
15610
0
  if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
15611
0
      || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15612
0
    != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15613
0
    {
15614
      /* Only error if both are set (to different values).  */
15615
0
      if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
15616
0
    || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15617
0
        != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15618
0
  {
15619
0
    _bfd_error_handler
15620
      /* xgettext:c-format */
15621
0
      (_("%pB: ABI mismatch: linking %s module with previous %s modules"),
15622
0
       ibfd,
15623
0
       elf_mips_abi_name (ibfd),
15624
0
       elf_mips_abi_name (obfd));
15625
0
    ok = false;
15626
0
  }
15627
0
      new_flags &= ~EF_MIPS_ABI;
15628
0
      old_flags &= ~EF_MIPS_ABI;
15629
0
    }
15630
15631
  /* Compare ASEs.  Forbid linking MIPS16 and microMIPS ASE modules together
15632
     and allow arbitrary mixing of the remaining ASEs (retain the union).  */
15633
0
  if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
15634
0
    {
15635
0
      int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15636
0
      int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15637
0
      int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
15638
0
      int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
15639
0
      int micro_mis = old_m16 && new_micro;
15640
0
      int m16_mis = old_micro && new_m16;
15641
15642
0
      if (m16_mis || micro_mis)
15643
0
  {
15644
0
    _bfd_error_handler
15645
      /* xgettext:c-format */
15646
0
      (_("%pB: ASE mismatch: linking %s module with previous %s modules"),
15647
0
       ibfd,
15648
0
       m16_mis ? "MIPS16" : "microMIPS",
15649
0
       m16_mis ? "microMIPS" : "MIPS16");
15650
0
    ok = false;
15651
0
  }
15652
15653
0
      elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
15654
15655
0
      new_flags &= ~ EF_MIPS_ARCH_ASE;
15656
0
      old_flags &= ~ EF_MIPS_ARCH_ASE;
15657
0
    }
15658
15659
  /* Compare NaN encodings.  */
15660
0
  if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
15661
0
    {
15662
      /* xgettext:c-format */
15663
0
      _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15664
0
        ibfd,
15665
0
        (new_flags & EF_MIPS_NAN2008
15666
0
         ? "-mnan=2008" : "-mnan=legacy"),
15667
0
        (old_flags & EF_MIPS_NAN2008
15668
0
         ? "-mnan=2008" : "-mnan=legacy"));
15669
0
      ok = false;
15670
0
      new_flags &= ~EF_MIPS_NAN2008;
15671
0
      old_flags &= ~EF_MIPS_NAN2008;
15672
0
    }
15673
15674
  /* Compare FP64 state.  */
15675
0
  if ((new_flags & EF_MIPS_FP64) != (old_flags & EF_MIPS_FP64))
15676
0
    {
15677
      /* xgettext:c-format */
15678
0
      _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15679
0
        ibfd,
15680
0
        (new_flags & EF_MIPS_FP64
15681
0
         ? "-mfp64" : "-mfp32"),
15682
0
        (old_flags & EF_MIPS_FP64
15683
0
         ? "-mfp64" : "-mfp32"));
15684
0
      ok = false;
15685
0
      new_flags &= ~EF_MIPS_FP64;
15686
0
      old_flags &= ~EF_MIPS_FP64;
15687
0
    }
15688
15689
  /* Warn about any other mismatches */
15690
0
  if (new_flags != old_flags)
15691
0
    {
15692
      /* xgettext:c-format */
15693
0
      _bfd_error_handler
15694
0
  (_("%pB: uses different e_flags (%#x) fields than previous modules "
15695
0
     "(%#x)"),
15696
0
   ibfd, new_flags, old_flags);
15697
0
      ok = false;
15698
0
    }
15699
15700
0
  return ok;
15701
0
}
15702
15703
/* Merge object attributes from IBFD into OBFD.  Raise an error if
15704
   there are conflicting attributes.  */
15705
static bool
15706
mips_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
15707
0
{
15708
0
  bfd *obfd = info->output_bfd;
15709
0
  obj_attribute *in_attr;
15710
0
  obj_attribute *out_attr;
15711
0
  bfd *abi_fp_bfd;
15712
0
  bfd *abi_msa_bfd;
15713
15714
0
  abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
15715
0
  in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15716
0
  if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
15717
0
    mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15718
15719
0
  abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
15720
0
  if (!abi_msa_bfd
15721
0
      && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15722
0
    mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
15723
15724
0
  if (!elf_known_obj_attributes_proc (obfd)[0].i)
15725
0
    {
15726
      /* This is the first object.  Copy the attributes.  */
15727
0
      _bfd_elf_copy_obj_attributes (ibfd, obfd);
15728
15729
      /* Use the Tag_null value to indicate the attributes have been
15730
   initialized.  */
15731
0
      elf_known_obj_attributes_proc (obfd)[0].i = 1;
15732
15733
0
      return true;
15734
0
    }
15735
15736
  /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
15737
     non-conflicting ones.  */
15738
0
  out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15739
0
  if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
15740
0
    {
15741
0
      int out_fp, in_fp;
15742
15743
0
      out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15744
0
      in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15745
0
      out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
15746
0
      if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
15747
0
  out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
15748
0
      else if (out_fp == Val_GNU_MIPS_ABI_FP_XX
15749
0
         && (in_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15750
0
       || in_fp == Val_GNU_MIPS_ABI_FP_64
15751
0
       || in_fp == Val_GNU_MIPS_ABI_FP_64A))
15752
0
  {
15753
0
    mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15754
0
    out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15755
0
  }
15756
0
      else if (in_fp == Val_GNU_MIPS_ABI_FP_XX
15757
0
         && (out_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15758
0
       || out_fp == Val_GNU_MIPS_ABI_FP_64
15759
0
       || out_fp == Val_GNU_MIPS_ABI_FP_64A))
15760
0
  /* Keep the current setting.  */;
15761
0
      else if (out_fp == Val_GNU_MIPS_ABI_FP_64A
15762
0
         && in_fp == Val_GNU_MIPS_ABI_FP_64)
15763
0
  {
15764
0
    mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15765
0
    out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15766
0
  }
15767
0
      else if (in_fp == Val_GNU_MIPS_ABI_FP_64A
15768
0
         && out_fp == Val_GNU_MIPS_ABI_FP_64)
15769
0
  /* Keep the current setting.  */;
15770
0
      else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
15771
0
  {
15772
0
    const char *out_string, *in_string;
15773
15774
0
    out_string = _bfd_mips_fp_abi_string (out_fp);
15775
0
    in_string = _bfd_mips_fp_abi_string (in_fp);
15776
    /* First warn about cases involving unrecognised ABIs.  */
15777
0
    if (!out_string && !in_string)
15778
      /* xgettext:c-format */
15779
0
      _bfd_error_handler
15780
0
        (_("warning: %pB uses unknown floating point ABI %d "
15781
0
     "(set by %pB), %pB uses unknown floating point ABI %d"),
15782
0
         obfd, out_fp, abi_fp_bfd, ibfd, in_fp);
15783
0
    else if (!out_string)
15784
0
      _bfd_error_handler
15785
        /* xgettext:c-format */
15786
0
        (_("warning: %pB uses unknown floating point ABI %d "
15787
0
     "(set by %pB), %pB uses %s"),
15788
0
         obfd, out_fp, abi_fp_bfd, ibfd, in_string);
15789
0
    else if (!in_string)
15790
0
      _bfd_error_handler
15791
        /* xgettext:c-format */
15792
0
        (_("warning: %pB uses %s (set by %pB), "
15793
0
     "%pB uses unknown floating point ABI %d"),
15794
0
         obfd, out_string, abi_fp_bfd, ibfd, in_fp);
15795
0
    else
15796
0
      {
15797
        /* If one of the bfds is soft-float, the other must be
15798
     hard-float.  The exact choice of hard-float ABI isn't
15799
     really relevant to the error message.  */
15800
0
        if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15801
0
    out_string = "-mhard-float";
15802
0
        else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15803
0
    in_string = "-mhard-float";
15804
0
        _bfd_error_handler
15805
    /* xgettext:c-format */
15806
0
    (_("warning: %pB uses %s (set by %pB), %pB uses %s"),
15807
0
     obfd, out_string, abi_fp_bfd, ibfd, in_string);
15808
0
      }
15809
0
  }
15810
0
    }
15811
15812
  /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
15813
     non-conflicting ones.  */
15814
0
  if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15815
0
    {
15816
0
      out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
15817
0
      if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
15818
0
  out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
15819
0
      else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15820
0
  switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15821
0
    {
15822
0
    case Val_GNU_MIPS_ABI_MSA_128:
15823
0
      _bfd_error_handler
15824
        /* xgettext:c-format */
15825
0
        (_("warning: %pB uses %s (set by %pB), "
15826
0
     "%pB uses unknown MSA ABI %d"),
15827
0
         obfd, "-mmsa", abi_msa_bfd,
15828
0
         ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15829
0
      break;
15830
15831
0
    default:
15832
0
      switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
15833
0
        {
15834
0
        case Val_GNU_MIPS_ABI_MSA_128:
15835
0
    _bfd_error_handler
15836
      /* xgettext:c-format */
15837
0
      (_("warning: %pB uses unknown MSA ABI %d "
15838
0
         "(set by %pB), %pB uses %s"),
15839
0
         obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15840
0
       abi_msa_bfd, ibfd, "-mmsa");
15841
0
      break;
15842
15843
0
        default:
15844
0
    _bfd_error_handler
15845
      /* xgettext:c-format */
15846
0
      (_("warning: %pB uses unknown MSA ABI %d "
15847
0
         "(set by %pB), %pB uses unknown MSA ABI %d"),
15848
0
       obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15849
0
       abi_msa_bfd, ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15850
0
    break;
15851
0
        }
15852
0
    }
15853
0
    }
15854
15855
  /* Merge Tag_compatibility attributes and any common GNU ones.  */
15856
0
  return _bfd_elf_merge_object_attributes (ibfd, info);
15857
0
}
15858
15859
/* Merge object ABI flags from IBFD into OBFD.  Raise an error if
15860
   there are conflicting settings.  */
15861
15862
static bool
15863
mips_elf_merge_obj_abiflags (bfd *ibfd, bfd *obfd)
15864
0
{
15865
0
  obj_attribute *out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15866
0
  struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15867
0
  struct mips_elf_obj_tdata *in_tdata = mips_elf_tdata (ibfd);
15868
15869
  /* Update the output abiflags fp_abi using the computed fp_abi.  */
15870
0
  out_tdata->abiflags.fp_abi = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15871
15872
0
#define max(a, b) ((a) > (b) ? (a) : (b))
15873
  /* Merge abiflags.  */
15874
0
  out_tdata->abiflags.isa_level = max (out_tdata->abiflags.isa_level,
15875
0
               in_tdata->abiflags.isa_level);
15876
0
  out_tdata->abiflags.isa_rev = max (out_tdata->abiflags.isa_rev,
15877
0
             in_tdata->abiflags.isa_rev);
15878
0
  out_tdata->abiflags.gpr_size = max (out_tdata->abiflags.gpr_size,
15879
0
              in_tdata->abiflags.gpr_size);
15880
0
  out_tdata->abiflags.cpr1_size = max (out_tdata->abiflags.cpr1_size,
15881
0
               in_tdata->abiflags.cpr1_size);
15882
0
  out_tdata->abiflags.cpr2_size = max (out_tdata->abiflags.cpr2_size,
15883
0
               in_tdata->abiflags.cpr2_size);
15884
0
#undef max
15885
0
  out_tdata->abiflags.ases |= in_tdata->abiflags.ases;
15886
0
  out_tdata->abiflags.flags1 |= in_tdata->abiflags.flags1;
15887
15888
0
  return true;
15889
0
}
15890
15891
/* Merge backend specific data from an object file to the output
15892
   object file when linking.  */
15893
15894
bool
15895
_bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
15896
0
{
15897
0
  bfd *obfd = info->output_bfd;
15898
0
  struct mips_elf_obj_tdata *out_tdata;
15899
0
  struct mips_elf_obj_tdata *in_tdata;
15900
0
  bool null_input_bfd = true;
15901
0
  asection *sec;
15902
0
  bool ok;
15903
15904
  /* Check if we have the same endianness.  */
15905
0
  if (! _bfd_generic_verify_endian_match (ibfd, info))
15906
0
    {
15907
0
      _bfd_error_handler
15908
0
  (_("%pB: endianness incompatible with that of the selected emulation"),
15909
0
   ibfd);
15910
0
      return false;
15911
0
    }
15912
15913
0
  if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
15914
0
    return true;
15915
15916
0
  in_tdata = mips_elf_tdata (ibfd);
15917
0
  out_tdata = mips_elf_tdata (obfd);
15918
15919
0
  if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
15920
0
    {
15921
0
      _bfd_error_handler
15922
0
  (_("%pB: ABI is incompatible with that of the selected emulation"),
15923
0
   ibfd);
15924
0
      return false;
15925
0
    }
15926
15927
  /* Check to see if the input BFD actually contains any sections.  If not,
15928
     then it has no attributes, and its flags may not have been initialized
15929
     either, but it cannot actually cause any incompatibility.  */
15930
  /* FIXME: This excludes any input shared library from consideration.  */
15931
0
  for (sec = ibfd->sections; sec != NULL; sec = sec->next)
15932
0
    {
15933
      /* Ignore synthetic sections and empty .text, .data and .bss sections
15934
   which are automatically generated by gas.  Also ignore fake
15935
   (s)common sections, since merely defining a common symbol does
15936
   not affect compatibility.  */
15937
0
      if ((sec->flags & SEC_IS_COMMON) == 0
15938
0
    && strcmp (sec->name, ".reginfo")
15939
0
    && strcmp (sec->name, ".mdebug")
15940
0
    && (sec->size != 0
15941
0
        || (strcmp (sec->name, ".text")
15942
0
      && strcmp (sec->name, ".data")
15943
0
      && strcmp (sec->name, ".bss"))))
15944
0
  {
15945
0
    null_input_bfd = false;
15946
0
    break;
15947
0
  }
15948
0
    }
15949
0
  if (null_input_bfd)
15950
0
    return true;
15951
15952
  /* Populate abiflags using existing information.  */
15953
0
  if (in_tdata->abiflags_valid)
15954
0
    {
15955
0
      obj_attribute *in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15956
0
      Elf_Internal_ABIFlags_v0 in_abiflags;
15957
0
      Elf_Internal_ABIFlags_v0 abiflags;
15958
15959
      /* Set up the FP ABI attribute from the abiflags if it is not already
15960
   set.  */
15961
0
      if (in_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
15962
0
  in_attr[Tag_GNU_MIPS_ABI_FP].i = in_tdata->abiflags.fp_abi;
15963
15964
0
      infer_mips_abiflags (ibfd, &abiflags);
15965
0
      in_abiflags = in_tdata->abiflags;
15966
15967
      /* It is not possible to infer the correct ISA revision
15968
   for R3 or R5 so drop down to R2 for the checks.  */
15969
0
      if (in_abiflags.isa_rev == 3 || in_abiflags.isa_rev == 5)
15970
0
  in_abiflags.isa_rev = 2;
15971
15972
0
      if (LEVEL_REV (in_abiflags.isa_level, in_abiflags.isa_rev)
15973
0
    < LEVEL_REV (abiflags.isa_level, abiflags.isa_rev))
15974
0
  _bfd_error_handler
15975
0
    (_("%pB: warning: inconsistent ISA between e_flags and "
15976
0
       ".MIPS.abiflags"), ibfd);
15977
0
      if (abiflags.fp_abi != Val_GNU_MIPS_ABI_FP_ANY
15978
0
    && in_abiflags.fp_abi != abiflags.fp_abi)
15979
0
  _bfd_error_handler
15980
0
    (_("%pB: warning: inconsistent FP ABI between .gnu.attributes and "
15981
0
       ".MIPS.abiflags"), ibfd);
15982
0
      if ((in_abiflags.ases & abiflags.ases) != abiflags.ases)
15983
0
  _bfd_error_handler
15984
0
    (_("%pB: warning: inconsistent ASEs between e_flags and "
15985
0
       ".MIPS.abiflags"), ibfd);
15986
      /* The isa_ext is allowed to be an extension of what can be inferred
15987
   from e_flags.  */
15988
0
      if (!mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags.isa_ext),
15989
0
        bfd_mips_isa_ext_mach (in_abiflags.isa_ext)))
15990
0
  _bfd_error_handler
15991
0
    (_("%pB: warning: inconsistent ISA extensions between e_flags and "
15992
0
       ".MIPS.abiflags"), ibfd);
15993
0
      if (in_abiflags.flags2 != 0)
15994
0
  _bfd_error_handler
15995
0
    (_("%pB: warning: unexpected flag in the flags2 field of "
15996
0
       ".MIPS.abiflags (0x%lx)"), ibfd,
15997
0
     in_abiflags.flags2);
15998
0
    }
15999
0
  else
16000
0
    {
16001
0
      infer_mips_abiflags (ibfd, &in_tdata->abiflags);
16002
0
      in_tdata->abiflags_valid = true;
16003
0
    }
16004
16005
0
  if (!out_tdata->abiflags_valid)
16006
0
    {
16007
      /* Copy input abiflags if output abiflags are not already valid.  */
16008
0
      out_tdata->abiflags = in_tdata->abiflags;
16009
0
      out_tdata->abiflags_valid = true;
16010
0
    }
16011
16012
0
  if (! elf_flags_init (obfd))
16013
0
    {
16014
0
      elf_flags_init (obfd) = true;
16015
0
      elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
16016
0
      elf_elfheader (obfd)->e_ident[EI_CLASS]
16017
0
  = elf_elfheader (ibfd)->e_ident[EI_CLASS];
16018
16019
0
      if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
16020
0
    && (bfd_get_arch_info (obfd)->the_default
16021
0
        || mips_mach_extends_p (bfd_get_mach (obfd),
16022
0
              bfd_get_mach (ibfd))))
16023
0
  {
16024
0
    if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
16025
0
           bfd_get_mach (ibfd)))
16026
0
      return false;
16027
16028
    /* Update the ABI flags isa_level, isa_rev and isa_ext fields.  */
16029
0
    update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
16030
0
  }
16031
16032
0
      ok = true;
16033
0
    }
16034
0
  else
16035
0
    ok = mips_elf_merge_obj_e_flags (ibfd, info);
16036
16037
0
  ok = mips_elf_merge_obj_attributes (ibfd, info) && ok;
16038
16039
0
  ok = mips_elf_merge_obj_abiflags (ibfd, obfd) && ok;
16040
16041
0
  if (!ok)
16042
0
    {
16043
0
      bfd_set_error (bfd_error_bad_value);
16044
0
      return false;
16045
0
    }
16046
16047
0
  return true;
16048
0
}
16049
16050
/* Function to keep MIPS specific file flags like as EF_MIPS_PIC.  */
16051
16052
bool
16053
_bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
16054
0
{
16055
0
  BFD_ASSERT (!elf_flags_init (abfd)
16056
0
        || elf_elfheader (abfd)->e_flags == flags);
16057
16058
0
  elf_elfheader (abfd)->e_flags = flags;
16059
0
  elf_flags_init (abfd) = true;
16060
0
  return true;
16061
0
}
16062
16063
char *
16064
_bfd_mips_elf_get_target_dtag (bfd_vma dtag)
16065
16
{
16066
16
  switch (dtag)
16067
16
    {
16068
0
    default: return "";
16069
2
    case DT_MIPS_RLD_VERSION:
16070
2
      return "MIPS_RLD_VERSION";
16071
0
    case DT_MIPS_TIME_STAMP:
16072
0
      return "MIPS_TIME_STAMP";
16073
0
    case DT_MIPS_ICHECKSUM:
16074
0
      return "MIPS_ICHECKSUM";
16075
0
    case DT_MIPS_IVERSION:
16076
0
      return "MIPS_IVERSION";
16077
2
    case DT_MIPS_FLAGS:
16078
2
      return "MIPS_FLAGS";
16079
2
    case DT_MIPS_BASE_ADDRESS:
16080
2
      return "MIPS_BASE_ADDRESS";
16081
0
    case DT_MIPS_MSYM:
16082
0
      return "MIPS_MSYM";
16083
0
    case DT_MIPS_CONFLICT:
16084
0
      return "MIPS_CONFLICT";
16085
0
    case DT_MIPS_LIBLIST:
16086
0
      return "MIPS_LIBLIST";
16087
2
    case DT_MIPS_LOCAL_GOTNO:
16088
2
      return "MIPS_LOCAL_GOTNO";
16089
0
    case DT_MIPS_CONFLICTNO:
16090
0
      return "MIPS_CONFLICTNO";
16091
0
    case DT_MIPS_LIBLISTNO:
16092
0
      return "MIPS_LIBLISTNO";
16093
2
    case DT_MIPS_SYMTABNO:
16094
2
      return "MIPS_SYMTABNO";
16095
2
    case DT_MIPS_UNREFEXTNO:
16096
2
      return "MIPS_UNREFEXTNO";
16097
2
    case DT_MIPS_GOTSYM:
16098
2
      return "MIPS_GOTSYM";
16099
0
    case DT_MIPS_HIPAGENO:
16100
0
      return "MIPS_HIPAGENO";
16101
2
    case DT_MIPS_RLD_MAP:
16102
2
      return "MIPS_RLD_MAP";
16103
0
    case DT_MIPS_RLD_MAP_REL:
16104
0
      return "MIPS_RLD_MAP_REL";
16105
0
    case DT_MIPS_DELTA_CLASS:
16106
0
      return "MIPS_DELTA_CLASS";
16107
0
    case DT_MIPS_DELTA_CLASS_NO:
16108
0
      return "MIPS_DELTA_CLASS_NO";
16109
0
    case DT_MIPS_DELTA_INSTANCE:
16110
0
      return "MIPS_DELTA_INSTANCE";
16111
0
    case DT_MIPS_DELTA_INSTANCE_NO:
16112
0
      return "MIPS_DELTA_INSTANCE_NO";
16113
0
    case DT_MIPS_DELTA_RELOC:
16114
0
      return "MIPS_DELTA_RELOC";
16115
0
    case DT_MIPS_DELTA_RELOC_NO:
16116
0
      return "MIPS_DELTA_RELOC_NO";
16117
0
    case DT_MIPS_DELTA_SYM:
16118
0
      return "MIPS_DELTA_SYM";
16119
0
    case DT_MIPS_DELTA_SYM_NO:
16120
0
      return "MIPS_DELTA_SYM_NO";
16121
0
    case DT_MIPS_DELTA_CLASSSYM:
16122
0
      return "MIPS_DELTA_CLASSSYM";
16123
0
    case DT_MIPS_DELTA_CLASSSYM_NO:
16124
0
      return "MIPS_DELTA_CLASSSYM_NO";
16125
0
    case DT_MIPS_CXX_FLAGS:
16126
0
      return "MIPS_CXX_FLAGS";
16127
0
    case DT_MIPS_PIXIE_INIT:
16128
0
      return "MIPS_PIXIE_INIT";
16129
0
    case DT_MIPS_SYMBOL_LIB:
16130
0
      return "MIPS_SYMBOL_LIB";
16131
0
    case DT_MIPS_LOCALPAGE_GOTIDX:
16132
0
      return "MIPS_LOCALPAGE_GOTIDX";
16133
0
    case DT_MIPS_LOCAL_GOTIDX:
16134
0
      return "MIPS_LOCAL_GOTIDX";
16135
0
    case DT_MIPS_HIDDEN_GOTIDX:
16136
0
      return "MIPS_HIDDEN_GOTIDX";
16137
0
    case DT_MIPS_PROTECTED_GOTIDX:
16138
0
      return "MIPS_PROTECTED_GOT_IDX";
16139
0
    case DT_MIPS_OPTIONS:
16140
0
      return "MIPS_OPTIONS";
16141
0
    case DT_MIPS_INTERFACE:
16142
0
      return "MIPS_INTERFACE";
16143
0
    case DT_MIPS_DYNSTR_ALIGN:
16144
0
      return "DT_MIPS_DYNSTR_ALIGN";
16145
0
    case DT_MIPS_INTERFACE_SIZE:
16146
0
      return "DT_MIPS_INTERFACE_SIZE";
16147
0
    case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
16148
0
      return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
16149
0
    case DT_MIPS_PERF_SUFFIX:
16150
0
      return "DT_MIPS_PERF_SUFFIX";
16151
0
    case DT_MIPS_COMPACT_SIZE:
16152
0
      return "DT_MIPS_COMPACT_SIZE";
16153
0
    case DT_MIPS_GP_VALUE:
16154
0
      return "DT_MIPS_GP_VALUE";
16155
0
    case DT_MIPS_AUX_DYNAMIC:
16156
0
      return "DT_MIPS_AUX_DYNAMIC";
16157
0
    case DT_MIPS_PLTGOT:
16158
0
      return "DT_MIPS_PLTGOT";
16159
0
    case DT_MIPS_RWPLT:
16160
0
      return "DT_MIPS_RWPLT";
16161
0
    case DT_MIPS_XHASH:
16162
0
      return "DT_MIPS_XHASH";
16163
16
    }
16164
16
}
16165
16166
/* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
16167
   not known.  */
16168
16169
const char *
16170
_bfd_mips_fp_abi_string (int fp)
16171
0
{
16172
0
  switch (fp)
16173
0
    {
16174
      /* These strings aren't translated because they're simply
16175
   option lists.  */
16176
0
    case Val_GNU_MIPS_ABI_FP_DOUBLE:
16177
0
      return "-mdouble-float";
16178
16179
0
    case Val_GNU_MIPS_ABI_FP_SINGLE:
16180
0
      return "-msingle-float";
16181
16182
0
    case Val_GNU_MIPS_ABI_FP_SOFT:
16183
0
      return "-msoft-float";
16184
16185
0
    case Val_GNU_MIPS_ABI_FP_OLD_64:
16186
0
      return _("-mips32r2 -mfp64 (12 callee-saved)");
16187
16188
0
    case Val_GNU_MIPS_ABI_FP_XX:
16189
0
      return "-mfpxx";
16190
16191
0
    case Val_GNU_MIPS_ABI_FP_64:
16192
0
      return "-mgp32 -mfp64";
16193
16194
0
    case Val_GNU_MIPS_ABI_FP_64A:
16195
0
      return "-mgp32 -mfp64 -mno-odd-spreg";
16196
16197
0
    default:
16198
0
      return 0;
16199
0
    }
16200
0
}
16201
16202
static void
16203
print_mips_ases (FILE *file, unsigned int mask)
16204
0
{
16205
0
  if (mask & AFL_ASE_DSP)
16206
0
    fputs ("\n\tDSP ASE", file);
16207
0
  if (mask & AFL_ASE_DSPR2)
16208
0
    fputs ("\n\tDSP R2 ASE", file);
16209
0
  if (mask & AFL_ASE_DSPR3)
16210
0
    fputs ("\n\tDSP R3 ASE", file);
16211
0
  if (mask & AFL_ASE_EVA)
16212
0
    fputs ("\n\tEnhanced VA Scheme", file);
16213
0
  if (mask & AFL_ASE_MCU)
16214
0
    fputs ("\n\tMCU (MicroController) ASE", file);
16215
0
  if (mask & AFL_ASE_MDMX)
16216
0
    fputs ("\n\tMDMX ASE", file);
16217
0
  if (mask & AFL_ASE_MIPS3D)
16218
0
    fputs ("\n\tMIPS-3D ASE", file);
16219
0
  if (mask & AFL_ASE_MT)
16220
0
    fputs ("\n\tMT ASE", file);
16221
0
  if (mask & AFL_ASE_SMARTMIPS)
16222
0
    fputs ("\n\tSmartMIPS ASE", file);
16223
0
  if (mask & AFL_ASE_VIRT)
16224
0
    fputs ("\n\tVZ ASE", file);
16225
0
  if (mask & AFL_ASE_MSA)
16226
0
    fputs ("\n\tMSA ASE", file);
16227
0
  if (mask & AFL_ASE_MIPS16)
16228
0
    fputs ("\n\tMIPS16 ASE", file);
16229
0
  if (mask & AFL_ASE_MICROMIPS)
16230
0
    fputs ("\n\tMICROMIPS ASE", file);
16231
0
  if (mask & AFL_ASE_XPA)
16232
0
    fputs ("\n\tXPA ASE", file);
16233
0
  if (mask & AFL_ASE_MIPS16E2)
16234
0
    fputs ("\n\tMIPS16e2 ASE", file);
16235
0
  if (mask & AFL_ASE_CRC)
16236
0
    fputs ("\n\tCRC ASE", file);
16237
0
  if (mask & AFL_ASE_GINV)
16238
0
    fputs ("\n\tGINV ASE", file);
16239
0
  if (mask & AFL_ASE_LOONGSON_MMI)
16240
0
    fputs ("\n\tLoongson MMI ASE", file);
16241
0
  if (mask & AFL_ASE_LOONGSON_CAM)
16242
0
    fputs ("\n\tLoongson CAM ASE", file);
16243
0
  if (mask & AFL_ASE_LOONGSON_EXT)
16244
0
    fputs ("\n\tLoongson EXT ASE", file);
16245
0
  if (mask & AFL_ASE_LOONGSON_EXT2)
16246
0
    fputs ("\n\tLoongson EXT2 ASE", file);
16247
0
  if (mask == 0)
16248
0
    fprintf (file, "\n\t%s", _("None"));
16249
0
  else if ((mask & ~AFL_ASE_MASK) != 0)
16250
0
    fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
16251
0
}
16252
16253
static void
16254
print_mips_isa_ext (FILE *file, unsigned int isa_ext)
16255
0
{
16256
0
  switch (isa_ext)
16257
0
    {
16258
0
    case 0:
16259
0
      fputs (_("None"), file);
16260
0
      break;
16261
0
    case AFL_EXT_XLR:
16262
0
      fputs ("RMI XLR", file);
16263
0
      break;
16264
0
    case AFL_EXT_OCTEON3:
16265
0
      fputs ("Cavium Networks Octeon3", file);
16266
0
      break;
16267
0
    case AFL_EXT_OCTEON2:
16268
0
      fputs ("Cavium Networks Octeon2", file);
16269
0
      break;
16270
0
    case AFL_EXT_OCTEONP:
16271
0
      fputs ("Cavium Networks OcteonP", file);
16272
0
      break;
16273
0
    case AFL_EXT_OCTEON:
16274
0
      fputs ("Cavium Networks Octeon", file);
16275
0
      break;
16276
0
    case AFL_EXT_5900:
16277
0
      fputs ("Toshiba R5900", file);
16278
0
      break;
16279
0
    case AFL_EXT_4650:
16280
0
      fputs ("MIPS R4650", file);
16281
0
      break;
16282
0
    case AFL_EXT_4010:
16283
0
      fputs ("LSI R4010", file);
16284
0
      break;
16285
0
    case AFL_EXT_4100:
16286
0
      fputs ("NEC VR4100", file);
16287
0
      break;
16288
0
    case AFL_EXT_3900:
16289
0
      fputs ("Toshiba R3900", file);
16290
0
      break;
16291
0
    case AFL_EXT_10000:
16292
0
      fputs ("MIPS R10000", file);
16293
0
      break;
16294
0
    case AFL_EXT_SB1:
16295
0
      fputs ("Broadcom SB-1", file);
16296
0
      break;
16297
0
    case AFL_EXT_4111:
16298
0
      fputs ("NEC VR4111/VR4181", file);
16299
0
      break;
16300
0
    case AFL_EXT_4120:
16301
0
      fputs ("NEC VR4120", file);
16302
0
      break;
16303
0
    case AFL_EXT_5400:
16304
0
      fputs ("NEC VR5400", file);
16305
0
      break;
16306
0
    case AFL_EXT_5500:
16307
0
      fputs ("NEC VR5500", file);
16308
0
      break;
16309
0
    case AFL_EXT_LOONGSON_2E:
16310
0
      fputs ("ST Microelectronics Loongson 2E", file);
16311
0
      break;
16312
0
    case AFL_EXT_LOONGSON_2F:
16313
0
      fputs ("ST Microelectronics Loongson 2F", file);
16314
0
      break;
16315
0
    case AFL_EXT_INTERAPTIV_MR2:
16316
0
      fputs ("Imagination interAptiv MR2", file);
16317
0
      break;
16318
0
    default:
16319
0
      fprintf (file, "%s (%d)", _("Unknown"), isa_ext);
16320
0
      break;
16321
0
    }
16322
0
}
16323
16324
static void
16325
print_mips_fp_abi_value (FILE *file, int val)
16326
0
{
16327
0
  switch (val)
16328
0
    {
16329
0
    case Val_GNU_MIPS_ABI_FP_ANY:
16330
0
      fprintf (file, _("Hard or soft float\n"));
16331
0
      break;
16332
0
    case Val_GNU_MIPS_ABI_FP_DOUBLE:
16333
0
      fprintf (file, _("Hard float (double precision)\n"));
16334
0
      break;
16335
0
    case Val_GNU_MIPS_ABI_FP_SINGLE:
16336
0
      fprintf (file, _("Hard float (single precision)\n"));
16337
0
      break;
16338
0
    case Val_GNU_MIPS_ABI_FP_SOFT:
16339
0
      fprintf (file, _("Soft float\n"));
16340
0
      break;
16341
0
    case Val_GNU_MIPS_ABI_FP_OLD_64:
16342
0
      fprintf (file, _("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
16343
0
      break;
16344
0
    case Val_GNU_MIPS_ABI_FP_XX:
16345
0
      fprintf (file, _("Hard float (32-bit CPU, Any FPU)\n"));
16346
0
      break;
16347
0
    case Val_GNU_MIPS_ABI_FP_64:
16348
0
      fprintf (file, _("Hard float (32-bit CPU, 64-bit FPU)\n"));
16349
0
      break;
16350
0
    case Val_GNU_MIPS_ABI_FP_64A:
16351
0
      fprintf (file, _("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16352
0
      break;
16353
0
    default:
16354
0
      fprintf (file, "??? (%d)\n", val);
16355
0
      break;
16356
0
    }
16357
0
}
16358
16359
static int
16360
get_mips_reg_size (int reg_size)
16361
0
{
16362
0
  return (reg_size == AFL_REG_NONE) ? 0
16363
0
   : (reg_size == AFL_REG_32) ? 32
16364
0
   : (reg_size == AFL_REG_64) ? 64
16365
0
   : (reg_size == AFL_REG_128) ? 128
16366
0
   : -1;
16367
0
}
16368
16369
bool
16370
_bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
16371
277
{
16372
277
  FILE *file = ptr;
16373
16374
277
  BFD_ASSERT (abfd != NULL && ptr != NULL);
16375
16376
  /* Print normal ELF private data.  */
16377
277
  _bfd_elf_print_private_bfd_data (abfd, ptr);
16378
16379
  /* xgettext:c-format */
16380
277
  fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
16381
16382
277
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32)
16383
11
    fprintf (file, _(" [abi=O32]"));
16384
266
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O64)
16385
6
    fprintf (file, _(" [abi=O64]"));
16386
260
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI32)
16387
1
    fprintf (file, _(" [abi=EABI32]"));
16388
259
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI64)
16389
1
    fprintf (file, _(" [abi=EABI64]"));
16390
258
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
16391
140
    fprintf (file, _(" [abi unknown]"));
16392
118
  else if (ABI_N32_P (abfd))
16393
6
    fprintf (file, _(" [abi=N32]"));
16394
112
  else if (ABI_64_P (abfd))
16395
82
    fprintf (file, _(" [abi=64]"));
16396
30
  else
16397
30
    fprintf (file, _(" [no abi set]"));
16398
16399
277
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_1)
16400
127
    fprintf (file, " [mips1]");
16401
150
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_2)
16402
1
    fprintf (file, " [mips2]");
16403
149
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_3)
16404
6
    fprintf (file, " [mips3]");
16405
143
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_4)
16406
2
    fprintf (file, " [mips4]");
16407
141
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_5)
16408
4
    fprintf (file, " [mips5]");
16409
137
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32)
16410
16
    fprintf (file, " [mips32]");
16411
121
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64)
16412
9
    fprintf (file, " [mips64]");
16413
112
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R2)
16414
38
    fprintf (file, " [mips32r2]");
16415
74
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R2)
16416
24
    fprintf (file, " [mips64r2]");
16417
50
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6)
16418
34
    fprintf (file, " [mips32r6]");
16419
16
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R6)
16420
12
    fprintf (file, " [mips64r6]");
16421
4
  else
16422
4
    fprintf (file, _(" [unknown ISA]"));
16423
16424
277
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
16425
60
    fprintf (file, " [mdmx]");
16426
16427
277
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
16428
71
    fprintf (file, " [mips16]");
16429
16430
277
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
16431
103
    fprintf (file, " [micromips]");
16432
16433
277
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
16434
123
    fprintf (file, " [nan2008]");
16435
16436
277
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
16437
131
    fprintf (file, " [old fp64]");
16438
16439
277
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
16440
65
    fprintf (file, " [32bitmode]");
16441
212
  else
16442
212
    fprintf (file, _(" [not 32bitmode]"));
16443
16444
277
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
16445
131
    fprintf (file, " [noreorder]");
16446
16447
277
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
16448
22
    fprintf (file, " [PIC]");
16449
16450
277
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
16451
107
    fprintf (file, " [CPIC]");
16452
16453
277
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
16454
26
    fprintf (file, " [XGOT]");
16455
16456
277
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
16457
103
    fprintf (file, " [UCODE]");
16458
16459
277
  fputc ('\n', file);
16460
16461
277
  if (mips_elf_tdata (abfd)->abiflags_valid)
16462
0
    {
16463
0
      Elf_Internal_ABIFlags_v0 *abiflags = &mips_elf_tdata (abfd)->abiflags;
16464
0
      fprintf (file, "\nMIPS ABI Flags Version: %d\n", abiflags->version);
16465
0
      fprintf (file, "\nISA: MIPS%d", abiflags->isa_level);
16466
0
      if (abiflags->isa_rev > 1)
16467
0
  fprintf (file, "r%d", abiflags->isa_rev);
16468
0
      fprintf (file, "\nGPR size: %d",
16469
0
         get_mips_reg_size (abiflags->gpr_size));
16470
0
      fprintf (file, "\nCPR1 size: %d",
16471
0
         get_mips_reg_size (abiflags->cpr1_size));
16472
0
      fprintf (file, "\nCPR2 size: %d",
16473
0
         get_mips_reg_size (abiflags->cpr2_size));
16474
0
      fputs ("\nFP ABI: ", file);
16475
0
      print_mips_fp_abi_value (file, abiflags->fp_abi);
16476
0
      fputs ("ISA Extension: ", file);
16477
0
      print_mips_isa_ext (file, abiflags->isa_ext);
16478
0
      fputs ("\nASEs:", file);
16479
0
      print_mips_ases (file, abiflags->ases);
16480
0
      fprintf (file, "\nFLAGS 1: %8.8lx", abiflags->flags1);
16481
0
      fprintf (file, "\nFLAGS 2: %8.8lx", abiflags->flags2);
16482
0
      fputc ('\n', file);
16483
0
    }
16484
16485
277
  return true;
16486
277
}
16487
16488
const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
16489
{
16490
  { STRING_COMMA_LEN (".lit4"),   0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16491
  { STRING_COMMA_LEN (".lit8"),   0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16492
  { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
16493
  { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,     SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16494
  { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16495
  { STRING_COMMA_LEN (".ucode"),  0, SHT_MIPS_UCODE, 0 },
16496
  { STRING_COMMA_LEN (".MIPS.xhash"),  0, SHT_MIPS_XHASH,   SHF_ALLOC },
16497
  { NULL,         0,  0, 0,        0 }
16498
};
16499
16500
/* Merge non visibility st_other attributes.  Ensure that the
16501
   STO_OPTIONAL flag is copied into h->other, even if this is not a
16502
   definiton of the symbol.  */
16503
void
16504
_bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
16505
              unsigned int st_other,
16506
              bool definition,
16507
              bool dynamic ATTRIBUTE_UNUSED)
16508
0
{
16509
0
  if ((st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
16510
0
    {
16511
0
      unsigned char other;
16512
16513
0
      other = (definition ? st_other : h->other);
16514
0
      other &= ~ELF_ST_VISIBILITY (-1);
16515
0
      h->other = other | ELF_ST_VISIBILITY (h->other);
16516
0
    }
16517
16518
0
  if (!definition
16519
0
      && ELF_MIPS_IS_OPTIONAL (st_other))
16520
0
    h->other |= STO_OPTIONAL;
16521
0
}
16522
16523
/* Decide whether an undefined symbol is special and can be ignored.
16524
   This is the case for OPTIONAL symbols on IRIX.  */
16525
bool
16526
_bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
16527
0
{
16528
0
  return ELF_MIPS_IS_OPTIONAL (h->other) != 0;
16529
0
}
16530
16531
bool
16532
_bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
16533
0
{
16534
0
  return (sym->st_shndx == SHN_COMMON
16535
0
    || sym->st_shndx == SHN_MIPS_ACOMMON
16536
0
    || sym->st_shndx == SHN_MIPS_SCOMMON);
16537
0
}
16538
16539
/* Return address for Ith PLT stub in section PLT, for relocation REL
16540
   or (bfd_vma) -1 if it should not be included.  */
16541
16542
bfd_vma
16543
_bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
16544
         const arelent *rel ATTRIBUTE_UNUSED)
16545
0
{
16546
0
  return (plt->vma
16547
0
    + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
16548
0
    + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
16549
0
}
16550
16551
/* Build a table of synthetic symbols to represent the PLT.  As with MIPS16
16552
   and microMIPS PLT slots we may have a many-to-one mapping between .plt
16553
   and .got.plt and also the slots may be of a different size each we walk
16554
   the PLT manually fetching instructions and matching them against known
16555
   patterns.  To make things easier standard MIPS slots, if any, always come
16556
   first.  As we don't create proper ELF symbols we use the UDATA.I member
16557
   of ASYMBOL to carry ISA annotation.  The encoding used is the same as
16558
   with the ST_OTHER member of the ELF symbol.  */
16559
16560
long
16561
_bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
16562
            long symcount ATTRIBUTE_UNUSED,
16563
            asymbol **syms ATTRIBUTE_UNUSED,
16564
            long dynsymcount, asymbol **dynsyms,
16565
            asymbol **ret)
16566
36
{
16567
36
  static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
16568
36
  static const char microsuffix[] = "@micromipsplt";
16569
36
  static const char m16suffix[] = "@mips16plt";
16570
36
  static const char mipssuffix[] = "@plt";
16571
16572
36
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
16573
36
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
16574
36
  bool micromips_p = MICROMIPS_P (abfd);
16575
36
  Elf_Internal_Shdr *hdr;
16576
36
  bfd_byte *plt_data;
16577
36
  bfd_vma plt_offset;
16578
36
  unsigned int other;
16579
36
  bfd_vma entry_size;
16580
36
  bfd_vma plt0_size;
16581
36
  asection *relplt;
16582
36
  bfd_vma opcode;
16583
36
  asection *plt;
16584
36
  asymbol *send;
16585
36
  size_t size;
16586
36
  char *names;
16587
36
  long counti;
16588
36
  arelent *p;
16589
36
  asymbol *s;
16590
36
  char *nend;
16591
36
  long count;
16592
36
  long pi;
16593
36
  long i;
16594
36
  long n;
16595
16596
36
  *ret = NULL;
16597
16598
36
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
16599
34
    return 0;
16600
16601
2
  relplt = bfd_get_section_by_name (abfd, ".rel.plt");
16602
2
  if (relplt == NULL)
16603
1
    return 0;
16604
16605
1
  hdr = &elf_section_data (relplt)->this_hdr;
16606
1
  if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
16607
0
    return 0;
16608
16609
1
  plt = bfd_get_section_by_name (abfd, ".plt");
16610
1
  if (plt == NULL || (plt->flags & SEC_HAS_CONTENTS) == 0)
16611
0
    return 0;
16612
16613
1
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
16614
1
  if (!(*slurp_relocs) (abfd, relplt, dynsyms, true))
16615
0
    return -1;
16616
1
  p = relplt->relocation;
16617
16618
  /* Calculating the exact amount of space required for symbols would
16619
     require two passes over the PLT, so just pessimise assuming two
16620
     PLT slots per relocation.  */
16621
1
  count = NUM_SHDR_ENTRIES (hdr);
16622
1
  counti = count * bed->s->int_rels_per_ext_rel;
16623
1
  size = 2 * count * sizeof (asymbol);
16624
1
  size += count * (sizeof (mipssuffix) +
16625
1
       (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
16626
96
  for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
16627
95
    size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
16628
16629
  /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too.  */
16630
1
  size += sizeof (asymbol) + sizeof (pltname);
16631
16632
1
  if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
16633
0
    return -1;
16634
16635
1
  if (plt->size < 16)
16636
0
    return -1;
16637
16638
1
  s = *ret = bfd_malloc (size);
16639
1
  if (s == NULL)
16640
0
    return -1;
16641
1
  send = s + 2 * count + 1;
16642
16643
1
  names = (char *) send;
16644
1
  nend = (char *) s + size;
16645
1
  n = 0;
16646
16647
1
  opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
16648
1
  if (opcode == 0x3302fffe)
16649
0
    {
16650
0
      if (!micromips_p)
16651
0
  return -1;
16652
0
      plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
16653
0
      other = STO_MICROMIPS;
16654
0
    }
16655
1
  else if (opcode == 0x0398c1d0)
16656
0
    {
16657
0
      if (!micromips_p)
16658
0
  return -1;
16659
0
      plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
16660
0
      other = STO_MICROMIPS;
16661
0
    }
16662
1
  else
16663
1
    {
16664
1
      plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
16665
1
      other = 0;
16666
1
    }
16667
16668
1
  s->the_bfd = abfd;
16669
1
  s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
16670
1
  s->section = plt;
16671
1
  s->value = 0;
16672
1
  s->name = names;
16673
1
  s->udata.i = other;
16674
1
  memcpy (names, pltname, sizeof (pltname));
16675
1
  names += sizeof (pltname);
16676
1
  ++s, ++n;
16677
16678
1
  pi = 0;
16679
1
  for (plt_offset = plt0_size;
16680
95
       plt_offset + 8 <= plt->size && s < send;
16681
94
       plt_offset += entry_size)
16682
94
    {
16683
94
      bfd_vma gotplt_addr;
16684
94
      const char *suffix;
16685
94
      bfd_vma gotplt_hi;
16686
94
      bfd_vma gotplt_lo;
16687
94
      size_t suffixlen;
16688
16689
94
      opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
16690
16691
      /* Check if the second word matches the expected MIPS16 instruction.  */
16692
94
      if (opcode == 0x651aeb00)
16693
0
  {
16694
0
    if (micromips_p)
16695
0
      return -1;
16696
    /* Truncated table???  */
16697
0
    if (plt_offset + 16 > plt->size)
16698
0
      break;
16699
0
    gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
16700
0
    entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
16701
0
    suffixlen = sizeof (m16suffix);
16702
0
    suffix = m16suffix;
16703
0
    other = STO_MIPS16;
16704
0
  }
16705
      /* Likewise the expected microMIPS instruction (no insn32 mode).  */
16706
94
      else if (opcode == 0xff220000)
16707
0
  {
16708
0
    if (!micromips_p)
16709
0
      return -1;
16710
0
    gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
16711
0
    gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16712
0
    gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
16713
0
    gotplt_lo <<= 2;
16714
0
    gotplt_addr = gotplt_hi + gotplt_lo;
16715
0
    gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
16716
0
    entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
16717
0
    suffixlen = sizeof (microsuffix);
16718
0
    suffix = microsuffix;
16719
0
    other = STO_MICROMIPS;
16720
0
  }
16721
      /* Likewise the expected microMIPS instruction (insn32 mode).  */
16722
94
      else if ((opcode & 0xffff0000) == 0xff2f0000)
16723
0
  {
16724
0
    gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16725
0
    gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
16726
0
    gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16727
0
    gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16728
0
    gotplt_addr = gotplt_hi + gotplt_lo;
16729
0
    entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
16730
0
    suffixlen = sizeof (microsuffix);
16731
0
    suffix = microsuffix;
16732
0
    other = STO_MICROMIPS;
16733
0
  }
16734
      /* Otherwise assume standard MIPS code.  */
16735
94
      else
16736
94
  {
16737
94
    gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
16738
94
    gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
16739
94
    gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16740
94
    gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16741
94
    gotplt_addr = gotplt_hi + gotplt_lo;
16742
94
    entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
16743
94
    suffixlen = sizeof (mipssuffix);
16744
94
    suffix = mipssuffix;
16745
94
    other = 0;
16746
94
  }
16747
      /* Truncated table???  */
16748
94
      if (plt_offset + entry_size > plt->size)
16749
0
  break;
16750
16751
94
      for (i = 0;
16752
9.02k
     i < count && p[pi].address != gotplt_addr;
16753
8.93k
     i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
16754
16755
94
      if (i < count)
16756
0
  {
16757
0
    size_t namelen;
16758
0
    size_t len;
16759
16760
0
    *s = **p[pi].sym_ptr_ptr;
16761
    /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
16762
       we are defining a symbol, ensure one of them is set.  */
16763
0
    if ((s->flags & BSF_LOCAL) == 0)
16764
0
      s->flags |= BSF_GLOBAL;
16765
0
    s->flags |= BSF_SYNTHETIC;
16766
0
    s->section = plt;
16767
0
    s->value = plt_offset;
16768
0
    s->name = names;
16769
0
    s->udata.i = other;
16770
16771
0
    len = strlen ((*p[pi].sym_ptr_ptr)->name);
16772
0
    namelen = len + suffixlen;
16773
0
    if (names + namelen > nend)
16774
0
      break;
16775
16776
0
    memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
16777
0
    names += len;
16778
0
    memcpy (names, suffix, suffixlen);
16779
0
    names += suffixlen;
16780
16781
0
    ++s, ++n;
16782
0
    pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
16783
0
  }
16784
94
    }
16785
16786
1
  free (plt_data);
16787
16788
1
  return n;
16789
1
}
16790
16791
/* Return the ABI flags associated with ABFD if available.  */
16792
16793
Elf_Internal_ABIFlags_v0 *
16794
bfd_mips_elf_get_abiflags (bfd *abfd)
16795
604k
{
16796
604k
  struct mips_elf_obj_tdata *tdata = mips_elf_tdata (abfd);
16797
16798
604k
  return tdata->abiflags_valid ? &tdata->abiflags : NULL;
16799
604k
}
16800
16801
/* MIPS libc ABI versions, used with the EI_ABIVERSION ELF file header
16802
   field.  Taken from `libc-abis.h' generated at GNU libc build time.
16803
   Using a MIPS_ prefix as other libc targets use different values.  */
16804
enum
16805
{
16806
  MIPS_LIBC_ABI_DEFAULT = 0,
16807
  MIPS_LIBC_ABI_MIPS_PLT,
16808
  MIPS_LIBC_ABI_UNIQUE,
16809
  MIPS_LIBC_ABI_MIPS_O32_FP64,
16810
  MIPS_LIBC_ABI_ABSOLUTE,
16811
  MIPS_LIBC_ABI_XHASH,
16812
  MIPS_LIBC_ABI_MAX
16813
};
16814
16815
bool
16816
_bfd_mips_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
16817
1
{
16818
1
  struct mips_elf_link_hash_table *htab = NULL;
16819
1
  Elf_Internal_Ehdr *i_ehdrp;
16820
16821
1
  if (!_bfd_elf_init_file_header (abfd, link_info))
16822
0
    return false;
16823
16824
1
  i_ehdrp = elf_elfheader (abfd);
16825
1
  if (link_info)
16826
0
    {
16827
0
      htab = mips_elf_hash_table (link_info);
16828
0
      BFD_ASSERT (htab != NULL);
16829
0
    }
16830
16831
1
  if (htab != NULL
16832
1
      && htab->use_plts_and_copy_relocs
16833
1
      && htab->root.target_os != is_vxworks)
16834
0
    i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_PLT;
16835
16836
1
  if (mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64
16837
1
      || mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64A)
16838
0
    i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_O32_FP64;
16839
16840
  /* Mark that we need support for absolute symbols in the dynamic loader.  */
16841
1
  if (htab != NULL && htab->use_absolute_zero && htab->gnu_target)
16842
0
    i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_ABSOLUTE;
16843
16844
  /* Mark that we need support for .MIPS.xhash in the dynamic linker,
16845
     if it is the only hash section that will be created.  */
16846
1
  if (link_info && link_info->emit_gnu_hash && !link_info->emit_hash)
16847
0
    i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_XHASH;
16848
1
  return true;
16849
1
}
16850
16851
int
16852
_bfd_mips_elf_compact_eh_encoding
16853
  (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16854
0
{
16855
0
  return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
16856
0
}
16857
16858
/* Return the opcode for can't unwind.  */
16859
16860
int
16861
_bfd_mips_elf_cant_unwind_opcode
16862
  (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16863
0
{
16864
0
  return COMPACT_EH_CANT_UNWIND_OPCODE;
16865
0
}
16866
16867
/* Record a position XLAT_LOC in the xlat translation table, associated with
16868
   the hash entry H.  The entry in the translation table will later be
16869
   populated with the real symbol dynindx.  */
16870
16871
void
16872
_bfd_mips_elf_record_xhash_symbol (struct elf_link_hash_entry *h,
16873
           bfd_vma xlat_loc)
16874
0
{
16875
0
  struct mips_elf_link_hash_entry *hmips;
16876
16877
0
  hmips = (struct mips_elf_link_hash_entry *) h;
16878
0
  hmips->mipsxhash_loc = xlat_loc;
16879
0
}