* configure.tgt (mips*el-*-elf*): Use elf32elmip.
[deliverable/binutils-gdb.git] / bfd / elf32-mips.c
CommitLineData
b3c0fc57 1/* MIPS-specific support for 32-bit ELF
5b3b9ff6 2 Copyright 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
6b4b4d17 3
b3c0fc57
ILT
4 Most of the information added by Ian Lance Taylor, Cygnus Support,
5 <ian@cygnus.com>.
6
6b4b4d17
JK
7This file is part of BFD, the Binary File Descriptor library.
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
3fe22b98 21Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
6b4b4d17 22
5b3b9ff6
ILT
23/* This file handles MIPS ELF targets. SGI Irix 5 uses a slightly
24 different MIPS ELF from other targets. This matters when linking.
25 This file supports both, switching at runtime. */
26
6b4b4d17
JK
27#include "bfd.h"
28#include "sysdep.h"
29#include "libbfd.h"
6e07e54f
ILT
30#include "bfdlink.h"
31#include "genlink.h"
6ab826bd 32#include "elf-bfd.h"
b3c0fc57
ILT
33#include "elf/mips.h"
34
6e07e54f
ILT
35/* Get the ECOFF swapping routines. */
36#include "coff/sym.h"
37#include "coff/symconst.h"
38#include "coff/internal.h"
39#include "coff/ecoff.h"
40#include "coff/mips.h"
41#define ECOFF_32
42#include "ecoffswap.h"
43
16fe8950 44static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
aac6b32f
ILT
45 PARAMS ((bfd *, bfd_reloc_code_real_type));
46static void mips_info_to_howto_rel
47 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
53787b23
ILT
48static void bfd_mips_elf32_swap_gptab_in
49 PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *));
50static void bfd_mips_elf32_swap_gptab_out
51 PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *));
aac6b32f 52static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
00176555 53static boolean mips_elf32_object_p PARAMS ((bfd *));
5b3b9ff6
ILT
54static boolean mips_elf_create_procedure_table
55 PARAMS ((PTR, bfd *, struct bfd_link_info *, asection *,
56 struct ecoff_debug_info *));
57static int mips_elf_additional_program_headers PARAMS ((bfd *));
58static boolean mips_elf_modify_segment_map PARAMS ((bfd *));
d1bf45aa 59static boolean mips_elf32_section_from_shdr
aac6b32f 60 PARAMS ((bfd *, Elf32_Internal_Shdr *, char *));
00176555 61static boolean mips_elf32_section_processing
aac6b32f 62 PARAMS ((bfd *, Elf32_Internal_Shdr *));
a2546fce
ILT
63static boolean mips_elf_is_local_label
64 PARAMS ((bfd *, asymbol *));
aac6b32f
ILT
65static struct bfd_hash_entry *mips_elf_link_hash_newfunc
66 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
67static struct bfd_link_hash_table *mips_elf_link_hash_table_create
68 PARAMS ((bfd *));
69static int gptab_compare PARAMS ((const void *, const void *));
70static boolean mips_elf_final_link
71 PARAMS ((bfd *, struct bfd_link_info *));
72static void mips_elf_relocate_hi16
73 PARAMS ((bfd *, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *,
74 bfd_vma));
5b3b9ff6
ILT
75static void mips_elf_relocate_got_local
76 PARAMS ((bfd *, bfd *, asection *, Elf_Internal_Rela *,
77 Elf_Internal_Rela *, bfd_byte *, bfd_vma));
78static void mips_elf_relocate_global_got
79 PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
80static boolean mips_elf_adjust_dynindx
81 PARAMS ((struct elf_link_hash_entry *, PTR));
aac6b32f
ILT
82static boolean mips_elf_relocate_section
83 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
82b1edf7 84 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
53787b23
ILT
85static boolean mips_elf_create_dynamic_sections
86 PARAMS ((bfd *, struct bfd_link_info *));
87static boolean mips_elf_create_compact_rel_section
88 PARAMS ((bfd *, struct bfd_link_info *));
89static boolean mips_elf_create_got_section
90 PARAMS ((bfd *, struct bfd_link_info *));
91static boolean mips_elf_check_relocs
92 PARAMS ((bfd *, struct bfd_link_info *, asection *,
93 const Elf_Internal_Rela *));
94static boolean mips_elf_adjust_dynamic_symbol
95 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
96static boolean mips_elf_size_dynamic_sections
97 PARAMS ((bfd *, struct bfd_link_info *));
98static boolean mips_elf_finish_dynamic_symbol
99 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
100 Elf_Internal_Sym *));
101static boolean mips_elf_finish_dynamic_sections
102 PARAMS ((bfd *, struct bfd_link_info *));
aac6b32f
ILT
103static boolean mips_elf_add_symbol_hook
104 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
105 const char **, flagword *, asection **, bfd_vma *));
5b3b9ff6 106static bfd_reloc_status_type mips_elf_final_gp
abec70b9 107 PARAMS ((bfd *, asymbol *, boolean, char **, bfd_vma *));
53787b23
ILT
108static bfd_byte *elf32_mips_get_relocated_section_contents
109 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
110 bfd_byte *, boolean, asymbol **));
5b3b9ff6
ILT
111
112/* This is true for Irix 5 executables, false for normal MIPS ELF ABI
113 executables. FIXME: At the moment, we default to always generating
114 Irix 5 executables. */
115
116#define SGI_COMPAT(abfd) (1)
117
118/* This structure is used to hold .got information when linking. It
119 is stored in the tdata field of the bfd_elf_section_data structure. */
120
121struct mips_got_info
122{
123 /* The symbol index of the first global .got symbol. */
124 unsigned long global_gotsym;
125 /* The number of local .got entries. */
126 unsigned int local_gotno;
127};
128
129/* The number of local .got entries we reserve. */
130#define MIPS_RESERVED_GOTNO (2)
131
132/* Instructions which appear in a stub. For some reason the stub is
133 slightly different on an SGI system. */
134#define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000)
135#define STUB_LW(abfd) \
136 (SGI_COMPAT (abfd) \
137 ? 0x8f998010 /* lw t9,0x8010(gp) */ \
138 : 0x8f998000) /* lw t9,0x8000(gp) */
139#define STUB_MOVE 0x03e07825 /* move t7,ra */
140#define STUB_JALR 0x0320f809 /* jal t9 */
141#define STUB_LI16 0x34180000 /* ori t8,zero,0 */
142#define MIPS_FUNCTION_STUB_SIZE (16)
143
144/* Names of sections which appear in the .dynsym section in an Irix 5
145 executable. */
146
147static const char * const mips_elf_dynsym_sec_names[] =
148{
149 ".text",
150 ".init",
151 ".fini",
152 ".data",
153 ".rodata",
154 ".sdata",
155 ".sbss",
156 ".bss",
157 NULL
158};
159
160#define SIZEOF_MIPS_DYNSYM_SECNAMES \
161 (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0])
162
163/* The number of entries in mips_elf_dynsym_sec_names which go in the
164 text segment. */
165
166#define MIPS_TEXT_DYNSYM_SECNO (3)
167
168/* The names of the runtime procedure table symbols used on Irix 5. */
169
170static const char * const mips_elf_dynsym_rtproc_names[] =
171{
172 "_procedure_table",
173 "_procedure_string_table",
174 "_procedure_table_size",
175 NULL
176};
177
178/* These structures are used to generate the .compact_rel section on
179 Irix 5. */
180
181typedef struct
182{
183 unsigned long id1; /* Always one? */
184 unsigned long num; /* Number of compact relocation entries. */
185 unsigned long id2; /* Always two? */
186 unsigned long offset; /* The file offset of the first relocation. */
187 unsigned long reserved0; /* Zero? */
188 unsigned long reserved1; /* Zero? */
189} Elf32_compact_rel;
190
191typedef struct
192{
193 bfd_byte id1[4];
194 bfd_byte num[4];
195 bfd_byte id2[4];
196 bfd_byte offset[4];
197 bfd_byte reserved0[4];
198 bfd_byte reserved1[4];
199} Elf32_External_compact_rel;
200
201typedef struct
202{
053baeb1
ILT
203 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
204 unsigned int rtype : 4; /* Relocation types. See below. */
205 unsigned int dist2to : 8;
206 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
5b3b9ff6
ILT
207 unsigned long konst; /* KONST field. See below. */
208 unsigned long vaddr; /* VADDR to be relocated. */
209} Elf32_crinfo;
210
211typedef struct
212{
053baeb1
ILT
213 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
214 unsigned int rtype : 4; /* Relocation types. See below. */
215 unsigned int dist2to : 8;
216 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
5b3b9ff6
ILT
217 unsigned long konst; /* KONST field. See below. */
218} Elf32_crinfo2;
219
220typedef struct
221{
222 bfd_byte info[4];
223 bfd_byte konst[4];
224 bfd_byte vaddr[4];
225} Elf32_External_crinfo;
226
227typedef struct
228{
229 bfd_byte info[4];
230 bfd_byte konst[4];
231} Elf32_External_crinfo2;
232
053baeb1
ILT
233/* These are the constants used to swap the bitfields in a crinfo. */
234
235#define CRINFO_CTYPE (0x1)
236#define CRINFO_CTYPE_SH (31)
237#define CRINFO_RTYPE (0xf)
238#define CRINFO_RTYPE_SH (27)
239#define CRINFO_DIST2TO (0xff)
240#define CRINFO_DIST2TO_SH (19)
241#define CRINFO_RELVADDR (0x7ffff)
242#define CRINFO_RELVADDR_SH (0)
243
5b3b9ff6
ILT
244/* A compact relocation info has long (3 words) or short (2 words)
245 formats. A short format doesn't have VADDR field and relvaddr
246 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
247#define CRF_MIPS_LONG 1
248#define CRF_MIPS_SHORT 0
249
250/* There are 4 types of compact relocation at least. The value KONST
251 has different meaning for each type:
252
253 (type) (konst)
254 CT_MIPS_REL32 Address in data
255 CT_MIPS_WORD Address in word (XXX)
256 CT_MIPS_GPHI_LO GP - vaddr
257 CT_MIPS_JMPAD Address to jump
258 */
259
260#define CRT_MIPS_REL32 0xa
261#define CRT_MIPS_WORD 0xb
262#define CRT_MIPS_GPHI_LO 0xc
263#define CRT_MIPS_JMPAD 0xd
264
053baeb1
ILT
265#define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
266#define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
267#define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
268#define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
b3c0fc57 269
53787b23
ILT
270static void bfd_elf32_swap_compact_rel_out
271 PARAMS ((bfd *, const Elf32_compact_rel *, Elf32_External_compact_rel *));
272static void bfd_elf32_swap_crinfo_out
273 PARAMS ((bfd *, const Elf32_crinfo *, Elf32_External_crinfo *));
274
b3c0fc57
ILT
275#define USE_REL 1 /* MIPS uses REL relocations instead of RELA */
276
277enum reloc_type
278{
279 R_MIPS_NONE = 0,
280 R_MIPS_16, R_MIPS_32,
281 R_MIPS_REL32, R_MIPS_26,
282 R_MIPS_HI16, R_MIPS_LO16,
283 R_MIPS_GPREL16, R_MIPS_LITERAL,
284 R_MIPS_GOT16, R_MIPS_PC16,
285 R_MIPS_CALL16, R_MIPS_GPREL32,
6a5848cd
ILT
286 /* The remaining relocs are defined on Irix, although they are not
287 in the MIPS ELF ABI. */
288 R_MIPS_UNUSED1, R_MIPS_UNUSED2,
289 R_MIPS_UNUSED3,
290 R_MIPS_SHIFT5, R_MIPS_SHIFT6,
291 R_MIPS_64, R_MIPS_GOT_DISP,
292 R_MIPS_GOT_PAGE, R_MIPS_GOT_OFST,
293 R_MIPS_GOT_HI16, R_MIPS_GOT_LO16,
294 R_MIPS_SUB, R_MIPS_INSERT_A,
295 R_MIPS_INSERT_B, R_MIPS_DELETE,
296 R_MIPS_HIGHER, R_MIPS_HIGHEST,
297 R_MIPS_CALL_HI16, R_MIPS_CALL_LO16,
b3c0fc57
ILT
298 R_MIPS_max
299};
300
301static reloc_howto_type elf_mips_howto_table[] =
302{
303 /* No relocation. */
304 HOWTO (R_MIPS_NONE, /* type */
305 0, /* rightshift */
306 0, /* size (0 = byte, 1 = short, 2 = long) */
307 0, /* bitsize */
308 false, /* pc_relative */
309 0, /* bitpos */
310 complain_overflow_dont, /* complain_on_overflow */
311 bfd_elf_generic_reloc, /* special_function */
312 "R_MIPS_NONE", /* name */
313 false, /* partial_inplace */
314 0, /* src_mask */
315 0, /* dst_mask */
316 false), /* pcrel_offset */
317
318 /* 16 bit relocation. */
319 HOWTO (R_MIPS_16, /* type */
320 0, /* rightshift */
321 1, /* size (0 = byte, 1 = short, 2 = long) */
322 16, /* bitsize */
323 false, /* pc_relative */
324 0, /* bitpos */
325 complain_overflow_bitfield, /* complain_on_overflow */
326 bfd_elf_generic_reloc, /* special_function */
327 "R_MIPS_16", /* name */
328 true, /* partial_inplace */
329 0xffff, /* src_mask */
330 0xffff, /* dst_mask */
331 false), /* pcrel_offset */
332
333 /* 32 bit relocation. */
334 HOWTO (R_MIPS_32, /* type */
335 0, /* rightshift */
336 2, /* size (0 = byte, 1 = short, 2 = long) */
337 32, /* bitsize */
338 false, /* pc_relative */
339 0, /* bitpos */
340 complain_overflow_bitfield, /* complain_on_overflow */
341 bfd_elf_generic_reloc, /* special_function */
342 "R_MIPS_32", /* name */
343 true, /* partial_inplace */
344 0xffffffff, /* src_mask */
345 0xffffffff, /* dst_mask */
346 false), /* pcrel_offset */
347
348 /* 32 bit symbol relative relocation. */
349 HOWTO (R_MIPS_REL32, /* type */
350 0, /* rightshift */
351 2, /* size (0 = byte, 1 = short, 2 = long) */
352 32, /* bitsize */
353 false, /* pc_relative */
354 0, /* bitpos */
355 complain_overflow_bitfield, /* complain_on_overflow */
356 bfd_elf_generic_reloc, /* special_function */
357 "R_MIPS_REL32", /* name */
358 true, /* partial_inplace */
359 0xffffffff, /* src_mask */
360 0xffffffff, /* dst_mask */
361 false), /* pcrel_offset */
362
363 /* 26 bit branch address. */
364 HOWTO (R_MIPS_26, /* type */
365 2, /* rightshift */
366 2, /* size (0 = byte, 1 = short, 2 = long) */
367 26, /* bitsize */
368 false, /* pc_relative */
369 0, /* bitpos */
6e07e54f
ILT
370 complain_overflow_dont, /* complain_on_overflow */
371 /* This needs complex overflow
372 detection, because the upper four
373 bits must match the PC. */
b3c0fc57
ILT
374 bfd_elf_generic_reloc, /* special_function */
375 "R_MIPS_26", /* name */
376 true, /* partial_inplace */
377 0x3ffffff, /* src_mask */
378 0x3ffffff, /* dst_mask */
379 false), /* pcrel_offset */
380
381 /* High 16 bits of symbol value. */
382 HOWTO (R_MIPS_HI16, /* type */
383 0, /* rightshift */
384 2, /* size (0 = byte, 1 = short, 2 = long) */
385 16, /* bitsize */
386 false, /* pc_relative */
387 0, /* bitpos */
388 complain_overflow_dont, /* complain_on_overflow */
bc05732b 389 _bfd_mips_elf_hi16_reloc, /* special_function */
b3c0fc57
ILT
390 "R_MIPS_HI16", /* name */
391 true, /* partial_inplace */
392 0xffff, /* src_mask */
393 0xffff, /* dst_mask */
394 false), /* pcrel_offset */
395
396 /* Low 16 bits of symbol value. */
397 HOWTO (R_MIPS_LO16, /* type */
398 0, /* rightshift */
399 2, /* size (0 = byte, 1 = short, 2 = long) */
400 16, /* bitsize */
401 false, /* pc_relative */
402 0, /* bitpos */
403 complain_overflow_dont, /* complain_on_overflow */
bc05732b 404 _bfd_mips_elf_lo16_reloc, /* special_function */
b3c0fc57
ILT
405 "R_MIPS_LO16", /* name */
406 true, /* partial_inplace */
407 0xffff, /* src_mask */
408 0xffff, /* dst_mask */
409 false), /* pcrel_offset */
410
411 /* GP relative reference. */
412 HOWTO (R_MIPS_GPREL16, /* type */
413 0, /* rightshift */
414 2, /* size (0 = byte, 1 = short, 2 = long) */
415 16, /* bitsize */
416 false, /* pc_relative */
417 0, /* bitpos */
418 complain_overflow_signed, /* complain_on_overflow */
bc05732b 419 _bfd_mips_elf_gprel16_reloc, /* special_function */
b3c0fc57
ILT
420 "R_MIPS_GPREL16", /* name */
421 true, /* partial_inplace */
422 0xffff, /* src_mask */
423 0xffff, /* dst_mask */
424 false), /* pcrel_offset */
425
426 /* Reference to literal section. */
427 HOWTO (R_MIPS_LITERAL, /* type */
428 0, /* rightshift */
429 2, /* size (0 = byte, 1 = short, 2 = long) */
430 16, /* bitsize */
431 false, /* pc_relative */
432 0, /* bitpos */
433 complain_overflow_signed, /* complain_on_overflow */
bc05732b 434 _bfd_mips_elf_gprel16_reloc, /* special_function */
b3c0fc57
ILT
435 "R_MIPS_LITERAL", /* name */
436 true, /* partial_inplace */
437 0xffff, /* src_mask */
438 0xffff, /* dst_mask */
439 false), /* pcrel_offset */
440
441 /* Reference to global offset table. */
b3c0fc57
ILT
442 HOWTO (R_MIPS_GOT16, /* type */
443 0, /* rightshift */
444 2, /* size (0 = byte, 1 = short, 2 = long) */
445 16, /* bitsize */
446 false, /* pc_relative */
447 0, /* bitpos */
448 complain_overflow_signed, /* complain_on_overflow */
bc05732b 449 _bfd_mips_elf_got16_reloc, /* special_function */
b3c0fc57
ILT
450 "R_MIPS_GOT16", /* name */
451 false, /* partial_inplace */
452 0, /* src_mask */
453 0xffff, /* dst_mask */
454 false), /* pcrel_offset */
455
456 /* 16 bit PC relative reference. */
457 HOWTO (R_MIPS_PC16, /* type */
458 0, /* rightshift */
459 2, /* size (0 = byte, 1 = short, 2 = long) */
460 16, /* bitsize */
461 true, /* pc_relative */
462 0, /* bitpos */
463 complain_overflow_signed, /* complain_on_overflow */
464 bfd_elf_generic_reloc, /* special_function */
465 "R_MIPS_PC16", /* name */
466 true, /* partial_inplace */
467 0xffff, /* src_mask */
468 0xffff, /* dst_mask */
469 false), /* pcrel_offset */
470
471 /* 16 bit call through global offset table. */
472 /* FIXME: This is not handled correctly. */
473 HOWTO (R_MIPS_CALL16, /* type */
474 0, /* rightshift */
475 2, /* size (0 = byte, 1 = short, 2 = long) */
476 16, /* bitsize */
477 false, /* pc_relative */
478 0, /* bitpos */
479 complain_overflow_signed, /* complain_on_overflow */
480 bfd_elf_generic_reloc, /* special_function */
481 "R_MIPS_CALL16", /* name */
482 false, /* partial_inplace */
483 0, /* src_mask */
484 0xffff, /* dst_mask */
485 false), /* pcrel_offset */
486
487 /* 32 bit GP relative reference. */
b3c0fc57
ILT
488 HOWTO (R_MIPS_GPREL32, /* type */
489 0, /* rightshift */
490 2, /* size (0 = byte, 1 = short, 2 = long) */
491 32, /* bitsize */
492 false, /* pc_relative */
493 0, /* bitpos */
494 complain_overflow_bitfield, /* complain_on_overflow */
bc05732b 495 _bfd_mips_elf_gprel32_reloc, /* special_function */
b3c0fc57
ILT
496 "R_MIPS_GPREL32", /* name */
497 true, /* partial_inplace */
498 0xffffffff, /* src_mask */
499 0xffffffff, /* dst_mask */
6a5848cd
ILT
500 false), /* pcrel_offset */
501
502 /* The remaining relocs are defined on Irix 5, although they are
503 not defined by the ABI. */
504 { 13 },
505 { 14 },
506 { 15 },
507
508 /* A 5 bit shift field. */
509 HOWTO (R_MIPS_SHIFT5, /* type */
510 0, /* rightshift */
511 2, /* size (0 = byte, 1 = short, 2 = long) */
512 5, /* bitsize */
513 false, /* pc_relative */
514 6, /* bitpos */
515 complain_overflow_bitfield, /* complain_on_overflow */
516 bfd_elf_generic_reloc, /* special_function */
517 "R_MIPS_SHIFT5", /* name */
518 true, /* partial_inplace */
519 0x000007c0, /* src_mask */
520 0x000007c0, /* dst_mask */
521 false), /* pcrel_offset */
522
523 /* A 6 bit shift field. */
524 /* FIXME: This is not handled correctly; a special function is
525 needed to put the most significant bit in the right place. */
526 HOWTO (R_MIPS_SHIFT6, /* type */
527 0, /* rightshift */
528 2, /* size (0 = byte, 1 = short, 2 = long) */
529 6, /* bitsize */
530 false, /* pc_relative */
531 6, /* bitpos */
532 complain_overflow_bitfield, /* complain_on_overflow */
533 bfd_elf_generic_reloc, /* special_function */
534 "R_MIPS_SHIFT6", /* name */
535 true, /* partial_inplace */
536 0x000007c4, /* src_mask */
537 0x000007c4, /* dst_mask */
538 false), /* pcrel_offset */
539
540 /* A 64 bit relocation. Presumably not used in 32 bit ELF. */
541 { R_MIPS_64 },
542
543 /* Displacement in the global offset table. */
544 /* FIXME: Not handled correctly. */
545 HOWTO (R_MIPS_GOT_DISP, /* type */
546 0, /* rightshift */
547 2, /* size (0 = byte, 1 = short, 2 = long) */
548 16, /* bitsize */
549 false, /* pc_relative */
550 0, /* bitpos */
551 complain_overflow_bitfield, /* complain_on_overflow */
552 bfd_elf_generic_reloc, /* special_function */
553 "R_MIPS_GOT_DISP", /* name */
554 true, /* partial_inplace */
555 0x0000ffff, /* src_mask */
556 0x0000ffff, /* dst_mask */
557 false), /* pcrel_offset */
558
559 /* Displacement to page pointer in the global offset table. */
560 /* FIXME: Not handled correctly. */
561 HOWTO (R_MIPS_GOT_PAGE, /* type */
562 0, /* rightshift */
563 2, /* size (0 = byte, 1 = short, 2 = long) */
564 16, /* bitsize */
565 false, /* pc_relative */
566 0, /* bitpos */
567 complain_overflow_bitfield, /* complain_on_overflow */
568 bfd_elf_generic_reloc, /* special_function */
569 "R_MIPS_GOT_PAGE", /* name */
570 true, /* partial_inplace */
571 0x0000ffff, /* src_mask */
572 0x0000ffff, /* dst_mask */
573 false), /* pcrel_offset */
574
575 /* Offset from page pointer in the global offset table. */
576 /* FIXME: Not handled correctly. */
577 HOWTO (R_MIPS_GOT_OFST, /* type */
578 0, /* rightshift */
579 2, /* size (0 = byte, 1 = short, 2 = long) */
580 16, /* bitsize */
581 false, /* pc_relative */
582 0, /* bitpos */
583 complain_overflow_bitfield, /* complain_on_overflow */
584 bfd_elf_generic_reloc, /* special_function */
585 "R_MIPS_GOT_OFST", /* name */
586 true, /* partial_inplace */
587 0x0000ffff, /* src_mask */
588 0x0000ffff, /* dst_mask */
589 false), /* pcrel_offset */
590
591 /* High 16 bits of displacement in global offset table. */
592 /* FIXME: Not handled correctly. */
593 HOWTO (R_MIPS_GOT_HI16, /* type */
594 0, /* rightshift */
595 2, /* size (0 = byte, 1 = short, 2 = long) */
596 16, /* bitsize */
597 false, /* pc_relative */
598 0, /* bitpos */
599 complain_overflow_dont, /* complain_on_overflow */
600 bfd_elf_generic_reloc, /* special_function */
601 "R_MIPS_GOT_HI16", /* name */
602 true, /* partial_inplace */
603 0x0000ffff, /* src_mask */
604 0x0000ffff, /* dst_mask */
605 false), /* pcrel_offset */
606
607 /* Low 16 bits of displacement in global offset table. */
608 /* FIXME: Not handled correctly. */
609 HOWTO (R_MIPS_GOT_LO16, /* type */
610 0, /* rightshift */
611 2, /* size (0 = byte, 1 = short, 2 = long) */
612 16, /* bitsize */
613 false, /* pc_relative */
614 0, /* bitpos */
615 complain_overflow_dont, /* complain_on_overflow */
616 bfd_elf_generic_reloc, /* special_function */
617 "R_MIPS_GOT_LO16", /* name */
618 true, /* partial_inplace */
619 0x0000ffff, /* src_mask */
620 0x0000ffff, /* dst_mask */
621 false), /* pcrel_offset */
622
623 /* 64 bit subtraction. Presumably not used in 32 bit ELF. */
624 { R_MIPS_SUB },
625
626 /* Used to cause the linker to insert and delete instructions? */
627 { R_MIPS_INSERT_A },
628 { R_MIPS_INSERT_B },
629 { R_MIPS_DELETE },
630
631 /* Get the higher values of a 64 bit addend. Presumably not used in
632 32 bit ELF. */
633 { R_MIPS_HIGHER },
634 { R_MIPS_HIGHEST },
635
636 /* High 16 bits of displacement in global offset table. */
637 /* FIXME: Not handled correctly. */
638 HOWTO (R_MIPS_CALL_HI16, /* type */
639 0, /* rightshift */
640 2, /* size (0 = byte, 1 = short, 2 = long) */
641 16, /* bitsize */
642 false, /* pc_relative */
643 0, /* bitpos */
644 complain_overflow_dont, /* complain_on_overflow */
645 bfd_elf_generic_reloc, /* special_function */
646 "R_MIPS_CALL_HI16", /* name */
647 true, /* partial_inplace */
648 0x0000ffff, /* src_mask */
649 0x0000ffff, /* dst_mask */
650 false), /* pcrel_offset */
651
652 /* Low 16 bits of displacement in global offset table. */
653 /* FIXME: Not handled correctly. */
654 HOWTO (R_MIPS_CALL_LO16, /* type */
655 0, /* rightshift */
656 2, /* size (0 = byte, 1 = short, 2 = long) */
657 16, /* bitsize */
658 false, /* pc_relative */
659 0, /* bitpos */
660 complain_overflow_dont, /* complain_on_overflow */
661 bfd_elf_generic_reloc, /* special_function */
662 "R_MIPS_CALL_LO16", /* name */
663 true, /* partial_inplace */
664 0x0000ffff, /* src_mask */
665 0x0000ffff, /* dst_mask */
b3c0fc57
ILT
666 false) /* pcrel_offset */
667};
668
669/* Do a R_MIPS_HI16 relocation. This has to be done in combination
670 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
671 the HI16. Here we just save the information we need; we do the
672 actual relocation when we see the LO16. MIPS ELF requires that the
abec70b9
ILT
673 LO16 immediately follow the HI16. As a GNU extension, we permit an
674 arbitrary number of HI16 relocs to be associated with a single LO16
675 reloc. This extension permits gcc to output the HI and LO relocs
676 itself. */
b3c0fc57 677
abec70b9
ILT
678struct mips_hi16
679{
680 struct mips_hi16 *next;
681 bfd_byte *addr;
682 bfd_vma addend;
683};
684
685/* FIXME: This should not be a static variable. */
686
687static struct mips_hi16 *mips_hi16_list;
b3c0fc57 688
bc05732b
ILT
689bfd_reloc_status_type
690_bfd_mips_elf_hi16_reloc (abfd,
b3c0fc57
ILT
691 reloc_entry,
692 symbol,
693 data,
694 input_section,
6e07e54f
ILT
695 output_bfd,
696 error_message)
b3c0fc57
ILT
697 bfd *abfd;
698 arelent *reloc_entry;
699 asymbol *symbol;
700 PTR data;
701 asection *input_section;
702 bfd *output_bfd;
6e07e54f 703 char **error_message;
b3c0fc57
ILT
704{
705 bfd_reloc_status_type ret;
706 bfd_vma relocation;
abec70b9 707 struct mips_hi16 *n;
b3c0fc57 708
b3c0fc57
ILT
709 /* If we're relocating, and this an external symbol, we don't want
710 to change anything. */
711 if (output_bfd != (bfd *) NULL
712 && (symbol->flags & BSF_SECTION_SYM) == 0
713 && reloc_entry->addend == 0)
714 {
715 reloc_entry->address += input_section->output_offset;
716 return bfd_reloc_ok;
717 }
718
5b3b9ff6
ILT
719 ret = bfd_reloc_ok;
720
6e07e54f 721 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
5b3b9ff6
ILT
722 {
723 boolean relocateable;
abec70b9 724 bfd_vma gp;
6e07e54f 725
5b3b9ff6
ILT
726 if (ret == bfd_reloc_undefined)
727 abort ();
b3c0fc57 728
5b3b9ff6
ILT
729 if (output_bfd != NULL)
730 relocateable = true;
731 else
732 {
733 relocateable = false;
734 output_bfd = symbol->section->output_section->owner;
735 }
736
737 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
abec70b9 738 error_message, &gp);
5b3b9ff6
ILT
739 if (ret != bfd_reloc_ok)
740 return ret;
741
abec70b9 742 relocation = gp - reloc_entry->address;
5b3b9ff6 743 }
b3c0fc57 744 else
5b3b9ff6
ILT
745 {
746 if (bfd_is_und_section (symbol->section)
747 && output_bfd == (bfd *) NULL)
748 ret = bfd_reloc_undefined;
749
750 if (bfd_is_com_section (symbol->section))
751 relocation = 0;
752 else
753 relocation = symbol->value;
754 }
b3c0fc57
ILT
755
756 relocation += symbol->section->output_section->vma;
757 relocation += symbol->section->output_offset;
758 relocation += reloc_entry->addend;
759
760 if (reloc_entry->address > input_section->_cooked_size)
761 return bfd_reloc_outofrange;
762
763 /* Save the information, and let LO16 do the actual relocation. */
abec70b9
ILT
764 n = (struct mips_hi16 *) bfd_malloc (sizeof *n);
765 if (n == NULL)
766 return bfd_reloc_outofrange;
767 n->addr = (bfd_byte *) data + reloc_entry->address;
768 n->addend = relocation;
769 n->next = mips_hi16_list;
770 mips_hi16_list = n;
b3c0fc57
ILT
771
772 if (output_bfd != (bfd *) NULL)
773 reloc_entry->address += input_section->output_offset;
774
775 return ret;
776}
777
778/* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
779 inplace relocation; this function exists in order to do the
780 R_MIPS_HI16 relocation described above. */
781
bc05732b
ILT
782bfd_reloc_status_type
783_bfd_mips_elf_lo16_reloc (abfd,
b3c0fc57
ILT
784 reloc_entry,
785 symbol,
786 data,
787 input_section,
6e07e54f
ILT
788 output_bfd,
789 error_message)
b3c0fc57
ILT
790 bfd *abfd;
791 arelent *reloc_entry;
792 asymbol *symbol;
793 PTR data;
794 asection *input_section;
795 bfd *output_bfd;
6e07e54f 796 char **error_message;
b3c0fc57 797{
5b3b9ff6 798 arelent gp_disp_relent;
b3c0fc57 799
abec70b9 800 if (mips_hi16_list != NULL)
b3c0fc57 801 {
abec70b9
ILT
802 struct mips_hi16 *l;
803
804 l = mips_hi16_list;
805 while (l != NULL)
5b3b9ff6 806 {
abec70b9
ILT
807 unsigned long insn;
808 unsigned long val;
809 unsigned long vallo;
810 struct mips_hi16 *next;
811
812 /* Do the HI16 relocation. Note that we actually don't need
813 to know anything about the LO16 itself, except where to
814 find the low 16 bits of the addend needed by the LO16. */
815 insn = bfd_get_32 (abfd, l->addr);
816 vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
817 & 0xffff);
818 val = ((insn & 0xffff) << 16) + vallo;
819 val += l->addend;
820
821 /* The low order 16 bits are always treated as a signed
822 value. Therefore, a negative value in the low order bits
823 requires an adjustment in the high order bits. We need
824 to make this adjustment in two ways: once for the bits we
825 took from the data, and once for the bits we are putting
826 back in to the data. */
827 if ((vallo & 0x8000) != 0)
828 val -= 0x10000;
829 if ((val & 0x8000) != 0)
830 val += 0x10000;
831
832 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
833 bfd_put_32 (abfd, insn, l->addr);
834
835 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
836 {
837 gp_disp_relent = *reloc_entry;
838 reloc_entry = &gp_disp_relent;
839 reloc_entry->addend = l->addend;
840 }
841
842 next = l->next;
843 free (l);
844 l = next;
5b3b9ff6 845 }
abec70b9
ILT
846
847 mips_hi16_list = NULL;
5b3b9ff6
ILT
848 }
849 else if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
850 {
851 bfd_reloc_status_type ret;
abec70b9 852 bfd_vma gp, relocation;
5b3b9ff6
ILT
853
854 /* FIXME: Does this case ever occur? */
855
abec70b9 856 ret = mips_elf_final_gp (output_bfd, symbol, true, error_message, &gp);
5b3b9ff6
ILT
857 if (ret != bfd_reloc_ok)
858 return ret;
859
abec70b9 860 relocation = gp - reloc_entry->address;
5b3b9ff6
ILT
861 relocation += symbol->section->output_section->vma;
862 relocation += symbol->section->output_offset;
863 relocation += reloc_entry->addend;
864
865 if (reloc_entry->address > input_section->_cooked_size)
866 return bfd_reloc_outofrange;
867
868 gp_disp_relent = *reloc_entry;
869 reloc_entry = &gp_disp_relent;
870 reloc_entry->addend = relocation - 4;
b3c0fc57
ILT
871 }
872
873 /* Now do the LO16 reloc in the usual way. */
874 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
6e07e54f
ILT
875 input_section, output_bfd, error_message);
876}
877
878/* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
879 table used for PIC code. If the symbol is an external symbol, the
880 instruction is modified to contain the offset of the appropriate
881 entry in the global offset table. If the symbol is a section
882 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
883 addends are combined to form the real addend against the section
884 symbol; the GOT16 is modified to contain the offset of an entry in
885 the global offset table, and the LO16 is modified to offset it
886 appropriately. Thus an offset larger than 16 bits requires a
887 modified value in the global offset table.
888
889 This implementation suffices for the assembler, but the linker does
890 not yet know how to create global offset tables. */
891
bc05732b
ILT
892bfd_reloc_status_type
893_bfd_mips_elf_got16_reloc (abfd,
6e07e54f
ILT
894 reloc_entry,
895 symbol,
896 data,
897 input_section,
898 output_bfd,
899 error_message)
900 bfd *abfd;
901 arelent *reloc_entry;
902 asymbol *symbol;
903 PTR data;
904 asection *input_section;
905 bfd *output_bfd;
906 char **error_message;
907{
908 /* If we're relocating, and this an external symbol, we don't want
909 to change anything. */
910 if (output_bfd != (bfd *) NULL
911 && (symbol->flags & BSF_SECTION_SYM) == 0
912 && reloc_entry->addend == 0)
913 {
914 reloc_entry->address += input_section->output_offset;
915 return bfd_reloc_ok;
916 }
917
918 /* If we're relocating, and this is a local symbol, we can handle it
919 just like HI16. */
920 if (output_bfd != (bfd *) NULL
921 && (symbol->flags & BSF_SECTION_SYM) != 0)
bc05732b
ILT
922 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
923 input_section, output_bfd, error_message);
6e07e54f
ILT
924
925 abort ();
b3c0fc57
ILT
926}
927
5b3b9ff6
ILT
928/* We have to figure out the gp value, so that we can adjust the
929 symbol value correctly. We look up the symbol _gp in the output
930 BFD. If we can't find it, we're stuck. We cache it in the ELF
931 target data. We don't need to adjust the symbol value for an
932 external symbol if we are producing relocateable output. */
a16fcab3 933
b3c0fc57 934static bfd_reloc_status_type
abec70b9 935mips_elf_final_gp (output_bfd, symbol, relocateable, error_message, pgp)
b3c0fc57 936 bfd *output_bfd;
5b3b9ff6
ILT
937 asymbol *symbol;
938 boolean relocateable;
6e07e54f 939 char **error_message;
abec70b9 940 bfd_vma *pgp;
b3c0fc57 941{
24f13b03 942 if (bfd_is_und_section (symbol->section)
5b3b9ff6 943 && ! relocateable)
abec70b9
ILT
944 {
945 *pgp = 0;
946 return bfd_reloc_undefined;
947 }
a16fcab3 948
abec70b9
ILT
949 *pgp = _bfd_get_gp_value (output_bfd);
950 if (*pgp == 0
5b3b9ff6 951 && (! relocateable
b3c0fc57
ILT
952 || (symbol->flags & BSF_SECTION_SYM) != 0))
953 {
5b3b9ff6 954 if (relocateable)
b3c0fc57
ILT
955 {
956 /* Make up a value. */
abec70b9
ILT
957 *pgp = symbol->section->output_section->vma + 0x4000;
958 _bfd_set_gp_value (output_bfd, *pgp);
b3c0fc57
ILT
959 }
960 else
961 {
962 unsigned int count;
963 asymbol **sym;
964 unsigned int i;
965
966 count = bfd_get_symcount (output_bfd);
967 sym = bfd_get_outsymbols (output_bfd);
968
969 if (sym == (asymbol **) NULL)
970 i = count;
971 else
972 {
973 for (i = 0; i < count; i++, sym++)
974 {
975 register CONST char *name;
976
977 name = bfd_asymbol_name (*sym);
978 if (*name == '_' && strcmp (name, "_gp") == 0)
979 {
abec70b9
ILT
980 *pgp = bfd_asymbol_value (*sym);
981 _bfd_set_gp_value (output_bfd, *pgp);
b3c0fc57
ILT
982 break;
983 }
984 }
985 }
986
987 if (i >= count)
988 {
989 /* Only get the error once. */
abec70b9
ILT
990 *pgp = 4;
991 _bfd_set_gp_value (output_bfd, *pgp);
6e07e54f
ILT
992 *error_message =
993 (char *) "GP relative relocation when _gp not defined";
b3c0fc57
ILT
994 return bfd_reloc_dangerous;
995 }
996 }
997 }
998
5b3b9ff6
ILT
999 return bfd_reloc_ok;
1000}
1001
1002/* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
1003 become the offset from the gp register. This function also handles
1004 R_MIPS_LITERAL relocations, although those can be handled more
1005 cleverly because the entries in the .lit8 and .lit4 sections can be
1006 merged. */
1007
1008static bfd_reloc_status_type gprel16_with_gp PARAMS ((bfd *, asymbol *,
1009 arelent *, asection *,
1010 boolean, PTR, bfd_vma));
1011
bc05732b
ILT
1012bfd_reloc_status_type
1013_bfd_mips_elf_gprel16_reloc (abfd, reloc_entry, symbol, data, input_section,
1014 output_bfd, error_message)
5b3b9ff6
ILT
1015 bfd *abfd;
1016 arelent *reloc_entry;
1017 asymbol *symbol;
1018 PTR data;
1019 asection *input_section;
1020 bfd *output_bfd;
1021 char **error_message;
1022{
1023 boolean relocateable;
1024 bfd_reloc_status_type ret;
abec70b9 1025 bfd_vma gp;
5b3b9ff6
ILT
1026
1027 /* If we're relocating, and this is an external symbol with no
1028 addend, we don't want to change anything. We will only have an
1029 addend if this is a newly created reloc, not read from an ELF
1030 file. */
1031 if (output_bfd != (bfd *) NULL
1032 && (symbol->flags & BSF_SECTION_SYM) == 0
1033 && reloc_entry->addend == 0)
1034 {
1035 reloc_entry->address += input_section->output_offset;
1036 return bfd_reloc_ok;
1037 }
1038
1039 if (output_bfd != (bfd *) NULL)
1040 relocateable = true;
1041 else
1042 {
1043 relocateable = false;
1044 output_bfd = symbol->section->output_section->owner;
1045 }
1046
abec70b9
ILT
1047 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1048 &gp);
5b3b9ff6
ILT
1049 if (ret != bfd_reloc_ok)
1050 return ret;
1051
a2546fce 1052 return gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
abec70b9 1053 relocateable, data, gp);
a16fcab3
KR
1054}
1055
1056static bfd_reloc_status_type
1057gprel16_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1058 gp)
1059 bfd *abfd;
1060 asymbol *symbol;
1061 arelent *reloc_entry;
1062 asection *input_section;
1063 boolean relocateable;
1064 PTR data;
1065 bfd_vma gp;
1066{
1067 bfd_vma relocation;
1068 unsigned long insn;
1069 unsigned long val;
1070
b3c0fc57
ILT
1071 if (bfd_is_com_section (symbol->section))
1072 relocation = 0;
1073 else
1074 relocation = symbol->value;
1075
1076 relocation += symbol->section->output_section->vma;
1077 relocation += symbol->section->output_offset;
1078
1079 if (reloc_entry->address > input_section->_cooked_size)
1080 return bfd_reloc_outofrange;
1081
1082 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1083
1084 /* Set val to the offset into the section or symbol. */
bc05732b
ILT
1085 if (reloc_entry->howto->src_mask == 0)
1086 {
1087 /* This case occurs with the 64-bit MIPS ELF ABI. */
1088 val = reloc_entry->addend;
1089 }
1090 else
1091 {
1092 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
1093 if (val & 0x8000)
1094 val -= 0x10000;
1095 }
b3c0fc57
ILT
1096
1097 /* Adjust val for the final section location and GP value. If we
1098 are producing relocateable output, we don't want to do this for
1099 an external symbol. */
5b3b9ff6 1100 if (! relocateable
b3c0fc57 1101 || (symbol->flags & BSF_SECTION_SYM) != 0)
a16fcab3 1102 val += relocation - gp;
b3c0fc57
ILT
1103
1104 insn = (insn &~ 0xffff) | (val & 0xffff);
1105 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1106
5b3b9ff6 1107 if (relocateable)
b3c0fc57
ILT
1108 reloc_entry->address += input_section->output_offset;
1109
1110 /* Make sure it fit in 16 bits. */
1111 if (val >= 0x8000 && val < 0xffff8000)
6e07e54f 1112 return bfd_reloc_overflow;
b3c0fc57
ILT
1113
1114 return bfd_reloc_ok;
1115}
6b4b4d17 1116
5b3b9ff6
ILT
1117/* Do a R_MIPS_GPREL32 relocation. Is this 32 bit value the offset
1118 from the gp register? XXX */
1119
1120static bfd_reloc_status_type gprel32_with_gp PARAMS ((bfd *, asymbol *,
1121 arelent *, asection *,
1122 boolean, PTR, bfd_vma));
1123
bc05732b
ILT
1124bfd_reloc_status_type
1125_bfd_mips_elf_gprel32_reloc (abfd,
5b3b9ff6
ILT
1126 reloc_entry,
1127 symbol,
1128 data,
1129 input_section,
1130 output_bfd,
1131 error_message)
1132 bfd *abfd;
1133 arelent *reloc_entry;
1134 asymbol *symbol;
1135 PTR data;
1136 asection *input_section;
1137 bfd *output_bfd;
1138 char **error_message;
1139{
1140 boolean relocateable;
1141 bfd_reloc_status_type ret;
abec70b9 1142 bfd_vma gp;
5b3b9ff6
ILT
1143
1144 /* If we're relocating, and this is an external symbol with no
1145 addend, we don't want to change anything. We will only have an
1146 addend if this is a newly created reloc, not read from an ELF
1147 file. */
1148 if (output_bfd != (bfd *) NULL
1149 && (symbol->flags & BSF_SECTION_SYM) == 0
1150 && reloc_entry->addend == 0)
1151 {
1152 *error_message = (char *)
1153 "32bits gp relative relocation occurs for an external symbol";
1154 return bfd_reloc_outofrange;
1155 }
1156
1157 if (output_bfd != (bfd *) NULL)
abec70b9
ILT
1158 {
1159 relocateable = true;
1160 gp = _bfd_get_gp_value (output_bfd);
1161 }
5b3b9ff6
ILT
1162 else
1163 {
1164 relocateable = false;
1165 output_bfd = symbol->section->output_section->owner;
1166
1167 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
abec70b9 1168 error_message, &gp);
5b3b9ff6
ILT
1169 if (ret != bfd_reloc_ok)
1170 return ret;
1171 }
1172
1173 return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
abec70b9 1174 relocateable, data, gp);
5b3b9ff6
ILT
1175}
1176
1177static bfd_reloc_status_type
1178gprel32_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1179 gp)
1180 bfd *abfd;
1181 asymbol *symbol;
1182 arelent *reloc_entry;
1183 asection *input_section;
1184 boolean relocateable;
1185 PTR data;
1186 bfd_vma gp;
1187{
1188 bfd_vma relocation;
1189 unsigned long val;
1190
1191 if (bfd_is_com_section (symbol->section))
1192 relocation = 0;
1193 else
1194 relocation = symbol->value;
1195
1196 relocation += symbol->section->output_section->vma;
1197 relocation += symbol->section->output_offset;
1198
1199 if (reloc_entry->address > input_section->_cooked_size)
1200 return bfd_reloc_outofrange;
1201
bc05732b
ILT
1202 if (reloc_entry->howto->src_mask == 0)
1203 {
1204 /* This case arises with the 64-bit MIPS ELF ABI. */
1205 val = 0;
1206 }
1207 else
1208 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
5b3b9ff6
ILT
1209
1210 /* Set val to the offset into the section or symbol. */
1211 val += reloc_entry->addend;
1212
1213 /* Adjust val for the final section location and GP value. If we
1214 are producing relocateable output, we don't want to do this for
1215 an external symbol. */
1216 if (! relocateable
1217 || (symbol->flags & BSF_SECTION_SYM) != 0)
1218 val += relocation - gp;
1219
1220 bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
1221
1222 if (relocateable)
1223 reloc_entry->address += input_section->output_offset;
1224
1225 return bfd_reloc_ok;
1226}
1227
b3c0fc57 1228/* A mapping from BFD reloc types to MIPS ELF reloc types. */
6b4b4d17 1229
b3c0fc57
ILT
1230struct elf_reloc_map {
1231 bfd_reloc_code_real_type bfd_reloc_val;
1232 enum reloc_type elf_reloc_val;
1233};
1234
1235static CONST struct elf_reloc_map mips_reloc_map[] =
1236{
1237 { BFD_RELOC_NONE, R_MIPS_NONE, },
1238 { BFD_RELOC_16, R_MIPS_16 },
1239 { BFD_RELOC_32, R_MIPS_32 },
1240 { BFD_RELOC_CTOR, R_MIPS_32 },
b3c0fc57
ILT
1241 { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
1242 { BFD_RELOC_HI16_S, R_MIPS_HI16 },
1243 { BFD_RELOC_LO16, R_MIPS_LO16 },
1244 { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 },
1245 { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
1246 { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
1247 { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
1248 { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
e9f03cd4
ILT
1249 { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 },
1250 { BFD_RELOC_MIPS_GOT_HI16, R_MIPS_GOT_HI16 },
b7384d22
ILT
1251 { BFD_RELOC_MIPS_GOT_LO16, R_MIPS_GOT_LO16 },
1252 { BFD_RELOC_MIPS_CALL_HI16, R_MIPS_CALL_HI16 },
1253 { BFD_RELOC_MIPS_CALL_LO16, R_MIPS_CALL_LO16 }
b3c0fc57
ILT
1254};
1255
1256/* Given a BFD reloc type, return a howto structure. */
1257
16fe8950 1258static reloc_howto_type *
b3c0fc57
ILT
1259bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1260 bfd *abfd;
1261 bfd_reloc_code_real_type code;
1262{
6ab826bd 1263 unsigned int i;
b3c0fc57
ILT
1264
1265 for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
1266 {
1267 if (mips_reloc_map[i].bfd_reloc_val == code)
1268 return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val];
1269 }
1270 return NULL;
1271}
1272
1273/* Given a MIPS reloc type, fill in an arelent structure. */
1274
1275static void
1276mips_info_to_howto_rel (abfd, cache_ptr, dst)
1277 bfd *abfd;
1278 arelent *cache_ptr;
1279 Elf32_Internal_Rel *dst;
1280{
1281 unsigned int r_type;
1282
1283 r_type = ELF32_R_TYPE (dst->r_info);
1284 BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
1285 cache_ptr->howto = &elf_mips_howto_table[r_type];
1286
1287 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
1288 value for the object file. We get the addend now, rather than
1289 when we do the relocation, because the symbol manipulations done
1290 by the linker may cause us to lose track of the input BFD. */
1291 if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
1292 && (r_type == (unsigned int) R_MIPS_GPREL16
1293 || r_type == (unsigned int) R_MIPS_LITERAL))
1294 cache_ptr->addend = elf_gp (abfd);
1295}
1296\f
1297/* A .reginfo section holds a single Elf32_RegInfo structure. These
1298 routines swap this structure in and out. They are used outside of
1299 BFD, so they are globally visible. */
1300
1301void
1302bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
1303 bfd *abfd;
1304 const Elf32_External_RegInfo *ex;
1305 Elf32_RegInfo *in;
1306{
1307 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1308 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1309 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1310 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1311 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1312 in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value);
1313}
1314
1315void
1316bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
1317 bfd *abfd;
1318 const Elf32_RegInfo *in;
1319 Elf32_External_RegInfo *ex;
1320{
1321 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1322 (bfd_byte *) ex->ri_gprmask);
1323 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1324 (bfd_byte *) ex->ri_cprmask[0]);
1325 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1326 (bfd_byte *) ex->ri_cprmask[1]);
1327 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1328 (bfd_byte *) ex->ri_cprmask[2]);
1329 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1330 (bfd_byte *) ex->ri_cprmask[3]);
1331 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value,
1332 (bfd_byte *) ex->ri_gp_value);
1333}
aac6b32f 1334
903612a2
ILT
1335/* In the 64 bit ABI, the .MIPS.options section holds register
1336 information in an Elf64_Reginfo structure. These routines swap
1337 them in and out. They are globally visible because they are used
1338 outside of BFD. These routines are here so that gas can call them
1339 without worrying about whether the 64 bit ABI has been included. */
1340
1341void
1342bfd_mips_elf64_swap_reginfo_in (abfd, ex, in)
1343 bfd *abfd;
1344 const Elf64_External_RegInfo *ex;
1345 Elf64_Internal_RegInfo *in;
1346{
1347 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1348 in->ri_pad = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_pad);
1349 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1350 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1351 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1352 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1353 in->ri_gp_value = bfd_h_get_64 (abfd, (bfd_byte *) ex->ri_gp_value);
1354}
1355
1356void
1357bfd_mips_elf64_swap_reginfo_out (abfd, in, ex)
1358 bfd *abfd;
1359 const Elf64_Internal_RegInfo *in;
1360 Elf64_External_RegInfo *ex;
1361{
1362 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1363 (bfd_byte *) ex->ri_gprmask);
1364 bfd_h_put_32 (abfd, (bfd_vma) in->ri_pad,
1365 (bfd_byte *) ex->ri_pad);
1366 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1367 (bfd_byte *) ex->ri_cprmask[0]);
1368 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1369 (bfd_byte *) ex->ri_cprmask[1]);
1370 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1371 (bfd_byte *) ex->ri_cprmask[2]);
1372 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1373 (bfd_byte *) ex->ri_cprmask[3]);
1374 bfd_h_put_64 (abfd, (bfd_vma) in->ri_gp_value,
1375 (bfd_byte *) ex->ri_gp_value);
1376}
1377
aac6b32f
ILT
1378/* Swap an entry in a .gptab section. Note that these routines rely
1379 on the equivalence of the two elements of the union. */
1380
1381static void
1382bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
1383 bfd *abfd;
1384 const Elf32_External_gptab *ex;
1385 Elf32_gptab *in;
1386{
1387 in->gt_entry.gt_g_value = bfd_h_get_32 (abfd, ex->gt_entry.gt_g_value);
1388 in->gt_entry.gt_bytes = bfd_h_get_32 (abfd, ex->gt_entry.gt_bytes);
1389}
1390
1391static void
1392bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
1393 bfd *abfd;
1394 const Elf32_gptab *in;
1395 Elf32_External_gptab *ex;
1396{
1397 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value,
1398 ex->gt_entry.gt_g_value);
1399 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes,
1400 ex->gt_entry.gt_bytes);
1401}
5b3b9ff6
ILT
1402
1403static void
1404bfd_elf32_swap_compact_rel_out (abfd, in, ex)
1405 bfd *abfd;
53787b23 1406 const Elf32_compact_rel *in;
5b3b9ff6
ILT
1407 Elf32_External_compact_rel *ex;
1408{
1409 bfd_h_put_32 (abfd, (bfd_vma) in->id1, ex->id1);
1410 bfd_h_put_32 (abfd, (bfd_vma) in->num, ex->num);
1411 bfd_h_put_32 (abfd, (bfd_vma) in->id2, ex->id2);
1412 bfd_h_put_32 (abfd, (bfd_vma) in->offset, ex->offset);
1413 bfd_h_put_32 (abfd, (bfd_vma) in->reserved0, ex->reserved0);
1414 bfd_h_put_32 (abfd, (bfd_vma) in->reserved1, ex->reserved1);
1415}
1416
1417static void
1418bfd_elf32_swap_crinfo_out (abfd, in, ex)
1419 bfd *abfd;
53787b23 1420 const Elf32_crinfo *in;
5b3b9ff6
ILT
1421 Elf32_External_crinfo *ex;
1422{
053baeb1
ILT
1423 unsigned long l;
1424
1425 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
1426 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
1427 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
1428 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
1429 bfd_h_put_32 (abfd, (bfd_vma) l, ex->info);
5b3b9ff6
ILT
1430 bfd_h_put_32 (abfd, (bfd_vma) in->konst, ex->konst);
1431 bfd_h_put_32 (abfd, (bfd_vma) in->vaddr, ex->vaddr);
1432}
bc05732b
ILT
1433
1434/* Swap in an options header. */
1435
1436void
1437bfd_mips_elf_swap_options_in (abfd, ex, in)
1438 bfd *abfd;
1439 const Elf_External_Options *ex;
1440 Elf_Internal_Options *in;
1441{
1442 in->kind = bfd_h_get_8 (abfd, ex->kind);
1443 in->size = bfd_h_get_8 (abfd, ex->size);
1444 in->section = bfd_h_get_16 (abfd, ex->section);
1445 in->info = bfd_h_get_32 (abfd, ex->info);
1446}
1447
1448/* Swap out an options header. */
1449
1450void
1451bfd_mips_elf_swap_options_out (abfd, in, ex)
1452 bfd *abfd;
1453 const Elf_Internal_Options *in;
1454 Elf_External_Options *ex;
1455{
1456 bfd_h_put_8 (abfd, in->kind, ex->kind);
1457 bfd_h_put_8 (abfd, in->size, ex->size);
1458 bfd_h_put_16 (abfd, in->section, ex->section);
1459 bfd_h_put_32 (abfd, in->info, ex->info);
1460}
5b3b9ff6
ILT
1461\f
1462/* Determine whether a symbol is global for the purposes of splitting
1463 the symbol table into global symbols and local symbols. At least
6e07e54f
ILT
1464 on Irix 5, this split must be between section symbols and all other
1465 symbols. On most ELF targets the split is between static symbols
1466 and externally visible symbols. */
1467
1468/*ARGSUSED*/
1469static boolean
1470mips_elf_sym_is_global (abfd, sym)
1471 bfd *abfd;
1472 asymbol *sym;
1473{
1474 return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false;
1475}
1476\f
00176555
ILT
1477/* Set the right machine number for a MIPS ELF file. This is used for
1478 both the 32-bit and the 64-bit ABI. */
6e07e54f 1479
00176555
ILT
1480boolean
1481_bfd_mips_elf_object_p (abfd)
6e07e54f
ILT
1482 bfd *abfd;
1483{
1484 switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
1485 {
1486 default:
1487 case E_MIPS_ARCH_1:
1488 /* Just use the default, which was set in elfcode.h. */
1489 break;
1490
1491 case E_MIPS_ARCH_2:
1492 (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 6000);
1493 break;
1494
1495 case E_MIPS_ARCH_3:
1496 (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 4000);
1497 break;
cabd8fa0
ILT
1498
1499 case E_MIPS_ARCH_4:
1500 (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 8000);
1501 break;
6e07e54f
ILT
1502 }
1503
00176555
ILT
1504 return true;
1505}
1506
1507/* Set the right machine number for a 32-bit MIPS ELF file. */
1508
1509static boolean
1510mips_elf32_object_p (abfd)
1511 bfd *abfd;
1512{
aac6b32f
ILT
1513 /* Irix 5 is broken. Object file symbol tables are not always
1514 sorted correctly such that local symbols precede global symbols,
6c495350
ILT
1515 and the sh_info field in the symbol table is not always right. */
1516 elf_bad_symtab (abfd) = true;
aac6b32f 1517
00176555 1518 return _bfd_mips_elf_object_p (abfd);
6e07e54f
ILT
1519}
1520
1521/* The final processing done just before writing out a MIPS ELF object
1522 file. This gets the MIPS architecture right based on the machine
00176555 1523 number. This is used by both the 32-bit and the 64-bit ABI. */
6e07e54f 1524
24f13b03 1525/*ARGSUSED*/
00176555
ILT
1526void
1527_bfd_mips_elf_final_write_processing (abfd, linker)
6e07e54f 1528 bfd *abfd;
24f13b03 1529 boolean linker;
6e07e54f
ILT
1530{
1531 unsigned long val;
aac6b32f
ILT
1532 unsigned int i;
1533 Elf_Internal_Shdr **hdrpp;
6e07e54f
ILT
1534
1535 switch (bfd_get_mach (abfd))
1536 {
1537 case 3000:
1538 val = E_MIPS_ARCH_1;
1539 break;
1540
1541 case 6000:
1542 val = E_MIPS_ARCH_2;
1543 break;
1544
1545 case 4000:
1546 val = E_MIPS_ARCH_3;
1547 break;
1548
cabd8fa0
ILT
1549 case 8000:
1550 val = E_MIPS_ARCH_4;
1551 break;
1552
6e07e54f 1553 default:
5b3b9ff6
ILT
1554 val = 0;
1555 break;
6e07e54f
ILT
1556 }
1557
1558 elf_elfheader (abfd)->e_flags &=~ EF_MIPS_ARCH;
1559 elf_elfheader (abfd)->e_flags |= val;
aac6b32f
ILT
1560
1561 /* Set the sh_info field for .gptab sections. */
1562 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
1563 i < elf_elfheader (abfd)->e_shnum;
1564 i++, hdrpp++)
1565 {
1566 if ((*hdrpp)->sh_type == SHT_MIPS_GPTAB)
1567 {
1568 const char *name;
1569 asection *sec;
1570
24f13b03
ILT
1571 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
1572 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
aac6b32f
ILT
1573 BFD_ASSERT (name != NULL
1574 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
1575 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
1576 BFD_ASSERT (sec != NULL);
1577 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
1578 }
1579 }
6e07e54f
ILT
1580}
1581\f
5b3b9ff6
ILT
1582/* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
1583
bc05732b
ILT
1584boolean
1585_bfd_mips_elf_set_private_flags (abfd, flags)
5b3b9ff6
ILT
1586 bfd *abfd;
1587 flagword flags;
1588{
1589 BFD_ASSERT (!elf_flags_init (abfd)
1590 || elf_elfheader (abfd)->e_flags == flags);
1591
1592 elf_elfheader (abfd)->e_flags = flags;
1593 elf_flags_init (abfd) = true;
1594 return true;
1595}
1596
1597/* Copy backend specific data from one object module to another */
1598
bc05732b
ILT
1599boolean
1600_bfd_mips_elf_copy_private_bfd_data (ibfd, obfd)
5b3b9ff6
ILT
1601 bfd *ibfd;
1602 bfd *obfd;
1603{
1604 /* This function is selected based on the input vector. We only
1605 want to copy information over if the output BFD also uses Elf
1606 format. */
1607 if (bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1608 return true;
1609
1610 BFD_ASSERT (!elf_flags_init (obfd)
1611 || (elf_elfheader (obfd)->e_flags
1612 == elf_elfheader (ibfd)->e_flags));
1613
d1bf45aa 1614 elf_gp (obfd) = elf_gp (ibfd);
5b3b9ff6
ILT
1615 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1616 elf_flags_init (obfd) = true;
1617 return true;
1618}
1619
1620/* Merge backend specific data from an object file to the output
1621 object file when linking. */
1622
bc05732b
ILT
1623boolean
1624_bfd_mips_elf_merge_private_bfd_data (ibfd, obfd)
5b3b9ff6
ILT
1625 bfd *ibfd;
1626 bfd *obfd;
1627{
1628 flagword old_flags;
1629 flagword new_flags;
1630
1631 /* Check if we have the same endianess */
1632 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1633 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1634 {
1635 (*_bfd_error_handler)
1636 ("%s: compiled for a %s endian system and target is %s endian",
1637 bfd_get_filename (ibfd),
1638 bfd_big_endian (ibfd) ? "big" : "little",
1639 bfd_big_endian (obfd) ? "big" : "little");
1640
1641 bfd_set_error (bfd_error_wrong_format);
1642 return false;
1643 }
1644
1645 /* This function is selected based on the input vector. We only
1646 want to copy information over if the output BFD also uses Elf
1647 format. */
1648 if (bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1649 return true;
1650
1651 new_flags = elf_elfheader (ibfd)->e_flags;
1652 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
1653 old_flags = elf_elfheader (obfd)->e_flags;
1654
1655 if (!elf_flags_init (obfd)) /* First call, no flags set */
1656 {
1657 elf_flags_init (obfd) = true;
1658 elf_elfheader (obfd)->e_flags = new_flags;
1659 }
1660 else if (((new_flags ^ old_flags) & ~EF_MIPS_NOREORDER)
1661 == 0) /* Compatible flags are ok */
1662 ;
1663 else /* Incompatible flags */
1664 {
1665 /* Warn about -fPIC mismatch */
1666 if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC))
1667 {
1668 new_flags &= ~EF_MIPS_PIC;
1669 (*_bfd_error_handler)
1670 ("%s: needs all files compiled with -fPIC",
1671 bfd_get_filename (ibfd));
1672 }
1673
1674 if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC))
1675 {
1676 new_flags &= ~EF_MIPS_CPIC;
1677 (*_bfd_error_handler)
1678 ("%s: needs all files compiled with -mabicalls",
1679 bfd_get_filename (ibfd));
1680 }
1681
1682 /* Warn about any other mismatches */
1683 if (new_flags != old_flags)
1684 (*_bfd_error_handler)
1685 ("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)",
1686 bfd_get_filename (ibfd), (unsigned long) new_flags,
1687 (unsigned long) old_flags);
1688
1689 bfd_set_error (bfd_error_bad_value);
1690 return false;
1691 }
1692
1693 return true;
1694}
1695\f
b3c0fc57
ILT
1696/* Handle a MIPS specific section when reading an object file. This
1697 is called when elfcode.h finds a section with an unknown type.
d1bf45aa
ILT
1698 This routine supports both the 32-bit and 64-bit ELF ABI.
1699
b3c0fc57
ILT
1700 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
1701 how to. */
1702
d1bf45aa
ILT
1703boolean
1704_bfd_mips_elf_section_from_shdr (abfd, hdr, name)
b3c0fc57 1705 bfd *abfd;
d1bf45aa
ILT
1706 Elf_Internal_Shdr *hdr;
1707 const char *name;
b3c0fc57
ILT
1708{
1709 /* There ought to be a place to keep ELF backend specific flags, but
1710 at the moment there isn't one. We just keep track of the
1711 sections by their name, instead. Fortunately, the ABI gives
1712 suggested names for all the MIPS specific sections, so we will
1713 probably get away with this. */
1714 switch (hdr->sh_type)
1715 {
1716 case SHT_MIPS_LIBLIST:
1717 if (strcmp (name, ".liblist") != 0)
1718 return false;
1719 break;
6a5848cd
ILT
1720 case SHT_MIPS_MSYM:
1721 if (strcmp (name, ".msym") != 0)
1722 return false;
1723 break;
b3c0fc57
ILT
1724 case SHT_MIPS_CONFLICT:
1725 if (strcmp (name, ".conflict") != 0)
1726 return false;
1727 break;
1728 case SHT_MIPS_GPTAB:
1729 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
1730 return false;
1731 break;
1732 case SHT_MIPS_UCODE:
1733 if (strcmp (name, ".ucode") != 0)
1734 return false;
1735 break;
1736 case SHT_MIPS_DEBUG:
1737 if (strcmp (name, ".mdebug") != 0)
1738 return false;
1739 break;
1740 case SHT_MIPS_REGINFO:
1741 if (strcmp (name, ".reginfo") != 0
1742 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
1743 return false;
1744 break;
6e07e54f 1745 case SHT_MIPS_OPTIONS:
bc05732b
ILT
1746 if (strcmp (name, ".options") != 0
1747 && strcmp (name, ".MIPS.options") != 0)
6e07e54f
ILT
1748 return false;
1749 break;
6a5848cd
ILT
1750 case SHT_MIPS_DWARF:
1751 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
1752 return false;
1753 break;
1754 case SHT_MIPS_EVENTS:
1755 if (strncmp (name, ".MIPS.events.", sizeof ".MIPS.events." - 1) != 0)
1756 return false;
1757 break;
b3c0fc57
ILT
1758 default:
1759 return false;
1760 }
1761
497c5434
ILT
1762 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1763 return false;
b3c0fc57 1764
497c5434
ILT
1765 if (hdr->sh_type == SHT_MIPS_DEBUG)
1766 {
d1bf45aa
ILT
1767 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
1768 (bfd_get_section_flags (abfd,
1769 hdr->bfd_section)
497c5434
ILT
1770 | SEC_DEBUGGING)))
1771 return false;
1772 }
b3c0fc57 1773
d1bf45aa
ILT
1774 return true;
1775}
1776
1777/* Handle a 32-bit MIPS ELF specific section. */
1778
1779static boolean
1780mips_elf32_section_from_shdr (abfd, hdr, name)
1781 bfd *abfd;
1782 Elf_Internal_Shdr *hdr;
1783 char *name;
1784{
1785 if (! _bfd_mips_elf_section_from_shdr (abfd, hdr, name))
1786 return false;
1787
497c5434 1788 /* FIXME: We should record sh_info for a .gptab section. */
b3c0fc57 1789
497c5434
ILT
1790 /* For a .reginfo section, set the gp value in the tdata information
1791 from the contents of this section. We need the gp value while
d1bf45aa
ILT
1792 processing relocs, so we just get it now. The .reginfo section
1793 is not used in the 64-bit MIPS ELF ABI. */
497c5434
ILT
1794 if (hdr->sh_type == SHT_MIPS_REGINFO)
1795 {
1796 Elf32_External_RegInfo ext;
1797 Elf32_RegInfo s;
b3c0fc57 1798
d1bf45aa 1799 if (! bfd_get_section_contents (abfd, hdr->bfd_section, (PTR) &ext,
497c5434
ILT
1800 (file_ptr) 0, sizeof ext))
1801 return false;
1802 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
1803 elf_gp (abfd) = s.ri_gp_value;
b3c0fc57
ILT
1804 }
1805
bc05732b
ILT
1806 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
1807 set the gp value based on what we find. We may see both
1808 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
1809 they should agree. */
1810 if (hdr->sh_type == SHT_MIPS_OPTIONS)
1811 {
1812 bfd_byte *contents, *l, *lend;
1813
1814 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
1815 if (contents == NULL)
1816 return false;
1817 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
1818 (file_ptr) 0, hdr->sh_size))
1819 {
1820 free (contents);
1821 return false;
1822 }
1823 l = contents;
1824 lend = contents + hdr->sh_size;
1825 while (l + sizeof (Elf_External_Options) <= lend)
1826 {
1827 Elf_Internal_Options intopt;
1828
1829 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
1830 &intopt);
1831 if (intopt.kind == ODK_REGINFO)
1832 {
1833 Elf32_RegInfo intreg;
1834
1835 bfd_mips_elf32_swap_reginfo_in
1836 (abfd,
1837 ((Elf32_External_RegInfo *)
1838 (l + sizeof (Elf_External_Options))),
1839 &intreg);
1840 elf_gp (abfd) = intreg.ri_gp_value;
1841 }
1842 l += intopt.size;
1843 }
1844 free (contents);
1845 }
1846
b3c0fc57
ILT
1847 return true;
1848}
1849
1850/* Set the correct type for a MIPS ELF section. We do this by the
00176555
ILT
1851 section name, which is a hack, but ought to work. This routine is
1852 used by both the 32-bit and the 64-bit ABI. */
b3c0fc57 1853
00176555
ILT
1854boolean
1855_bfd_mips_elf_fake_sections (abfd, hdr, sec)
b3c0fc57
ILT
1856 bfd *abfd;
1857 Elf32_Internal_Shdr *hdr;
1858 asection *sec;
1859{
1860 register const char *name;
1861
1862 name = bfd_get_section_name (abfd, sec);
1863
1864 if (strcmp (name, ".liblist") == 0)
1865 {
1866 hdr->sh_type = SHT_MIPS_LIBLIST;
1867 hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
1868 /* FIXME: Set the sh_link field. */
1869 }
6a5848cd
ILT
1870 else if (strcmp (name, ".msym") == 0)
1871 {
1872 hdr->sh_type = SHT_MIPS_MSYM;
1873 hdr->sh_entsize = 8;
1874 /* FIXME: Set the sh_info field. */
1875 }
b3c0fc57
ILT
1876 else if (strcmp (name, ".conflict") == 0)
1877 hdr->sh_type = SHT_MIPS_CONFLICT;
1878 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
1879 {
1880 hdr->sh_type = SHT_MIPS_GPTAB;
aac6b32f 1881 hdr->sh_entsize = sizeof (Elf32_External_gptab);
00176555 1882 /* The sh_info field is set in final_write_processing. */
b3c0fc57
ILT
1883 }
1884 else if (strcmp (name, ".ucode") == 0)
1885 hdr->sh_type = SHT_MIPS_UCODE;
1886 else if (strcmp (name, ".mdebug") == 0)
6e07e54f
ILT
1887 {
1888 hdr->sh_type = SHT_MIPS_DEBUG;
5b3b9ff6
ILT
1889 /* In a shared object on Irix 5.3, the .mdebug section has an
1890 entsize of 0. FIXME: Does this matter? */
1891 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
1892 hdr->sh_entsize = 0;
1893 else
1894 hdr->sh_entsize = 1;
6e07e54f 1895 }
b3c0fc57
ILT
1896 else if (strcmp (name, ".reginfo") == 0)
1897 {
1898 hdr->sh_type = SHT_MIPS_REGINFO;
5b3b9ff6
ILT
1899 /* In a shared object on Irix 5.3, the .reginfo section has an
1900 entsize of 0x18. FIXME: Does this matter? */
1901 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
1902 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
1903 else
1904 hdr->sh_entsize = 1;
b3c0fc57
ILT
1905
1906 /* Force the section size to the correct value, even if the
1907 linker thinks it is larger. The link routine below will only
1908 write out this much data for .reginfo. */
1909 hdr->sh_size = sec->_raw_size = sizeof (Elf32_External_RegInfo);
1910 }
5b3b9ff6
ILT
1911 else if (SGI_COMPAT (abfd)
1912 && (strcmp (name, ".hash") == 0
1913 || strcmp (name, ".dynamic") == 0
1914 || strcmp (name, ".dynstr") == 0))
1915 {
1916 hdr->sh_entsize = 0;
1917 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
1918 }
1919 else if (strcmp (name, ".got") == 0
1920 || strcmp (name, ".sdata") == 0
1921 || strcmp (name, ".sbss") == 0
1922 || strcmp (name, ".lit4") == 0
1923 || strcmp (name, ".lit8") == 0)
1924 hdr->sh_flags |= SHF_MIPS_GPREL;
bc05732b
ILT
1925 else if (strcmp (name, ".options") == 0
1926 || strcmp (name, ".MIPS.options") == 0)
6e07e54f
ILT
1927 {
1928 hdr->sh_type = SHT_MIPS_OPTIONS;
1929 hdr->sh_entsize = 1;
1930 }
6a5848cd
ILT
1931 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
1932 hdr->sh_type = SHT_MIPS_DWARF;
1933 else if (strncmp (name, ".MIPS.events.", sizeof ".MIPS.events." - 1) == 0)
1934 hdr->sh_type = SHT_MIPS_EVENTS;
b3c0fc57
ILT
1935
1936 return true;
1937}
1938
1939/* Given a BFD section, try to locate the corresponding ELF section
00176555
ILT
1940 index. This is used by both the 32-bit and the 64-bit ABI.
1941 Actually, it's not clear to me that the 64-bit ABI supports these,
1942 but for non-PIC objects we will certainly want support for at least
1943 the .scommon section. */
b3c0fc57 1944
00176555
ILT
1945boolean
1946_bfd_mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
b3c0fc57
ILT
1947 bfd *abfd;
1948 Elf32_Internal_Shdr *hdr;
1949 asection *sec;
1950 int *retval;
1951{
1952 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
1953 {
1954 *retval = SHN_MIPS_SCOMMON;
1955 return true;
1956 }
82b1edf7
KR
1957 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
1958 {
1959 *retval = SHN_MIPS_ACOMMON;
1960 return true;
1961 }
b3c0fc57
ILT
1962 return false;
1963}
1964
bc05732b
ILT
1965/* When are writing out the .options or .MIPS.options section,
1966 remember the bytes we are writing out, so that we can install the
1967 GP value in the section_processing routine. */
1968
1969boolean
1970_bfd_mips_elf_set_section_contents (abfd, section, location, offset, count)
1971 bfd *abfd;
1972 sec_ptr section;
1973 PTR location;
1974 file_ptr offset;
1975 bfd_size_type count;
1976{
1977 if (strcmp (section->name, ".options") == 0
1978 || strcmp (section->name, ".MIPS.options") == 0)
1979 {
1980 bfd_byte *c;
1981
1982 if (elf_section_data (section) == NULL)
1983 {
1984 section->used_by_bfd =
1985 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
1986 if (elf_section_data (section) == NULL)
1987 return false;
1988 }
1989 c = (bfd_byte *) elf_section_data (section)->tdata;
1990 if (c == NULL)
1991 {
1992 bfd_size_type size;
1993
1994 if (section->_cooked_size != 0)
1995 size = section->_cooked_size;
1996 else
1997 size = section->_raw_size;
1998 c = (PTR) bfd_zalloc (abfd, size);
1999 if (c == NULL)
2000 return false;
2001 elf_section_data (section)->tdata = (PTR) c;
2002 }
2003
2004 memcpy (c + offset, location, count);
2005 }
2006
2007 return _bfd_elf_set_section_contents (abfd, section, location, offset,
2008 count);
2009}
2010
2011/* Work over a section just before writing it out. This routine is
2012 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
2013 sections that need the SHF_MIPS_GPREL flag by name; there has to be
2014 a better way. */
b3c0fc57 2015
00176555
ILT
2016boolean
2017_bfd_mips_elf_section_processing (abfd, hdr)
b3c0fc57 2018 bfd *abfd;
00176555 2019 Elf_Internal_Shdr *hdr;
b3c0fc57 2020{
24f13b03 2021 if (hdr->bfd_section != NULL)
b3c0fc57 2022 {
24f13b03 2023 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
b3c0fc57
ILT
2024
2025 if (strcmp (name, ".sdata") == 0)
2026 {
2027 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2028 hdr->sh_type = SHT_PROGBITS;
2029 }
2030 else if (strcmp (name, ".sbss") == 0)
2031 {
2032 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2033 hdr->sh_type = SHT_NOBITS;
2034 }
2035 else if (strcmp (name, ".lit8") == 0
2036 || strcmp (name, ".lit4") == 0)
2037 {
2038 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2039 hdr->sh_type = SHT_PROGBITS;
2040 }
5b3b9ff6
ILT
2041 else if (strcmp (name, ".compact_rel") == 0)
2042 {
2043 hdr->sh_flags = 0;
2044 hdr->sh_type = SHT_PROGBITS;
2045 }
2046 else if (strcmp (name, ".rtproc") == 0)
2047 {
2048 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
2049 {
2050 unsigned int adjust;
2051
2052 adjust = hdr->sh_size % hdr->sh_addralign;
2053 if (adjust != 0)
2054 hdr->sh_size += hdr->sh_addralign - adjust;
2055 }
2056 }
b3c0fc57
ILT
2057 }
2058
2059 return true;
2060}
00176555
ILT
2061
2062/* Work over a section just before writing it out. We update the GP
bc05732b
ILT
2063 value in the SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS sections based
2064 on the value we are using. */
00176555
ILT
2065
2066static boolean
2067mips_elf32_section_processing (abfd, hdr)
2068 bfd *abfd;
2069 Elf32_Internal_Shdr *hdr;
2070{
2071 if (hdr->sh_type == SHT_MIPS_REGINFO)
2072 {
2073 bfd_byte buf[4];
2074
2075 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
2076 BFD_ASSERT (hdr->contents == NULL);
2077
2078 if (bfd_seek (abfd,
2079 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
2080 SEEK_SET) == -1)
2081 return false;
2082 bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf);
2083 if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4)
2084 return false;
2085 }
2086
bc05732b
ILT
2087 if (hdr->sh_type == SHT_MIPS_OPTIONS
2088 && hdr->bfd_section != NULL
2089 && elf_section_data (hdr->bfd_section) != NULL
2090 && elf_section_data (hdr->bfd_section)->tdata != NULL)
2091 {
2092 bfd_byte *contents, *l, *lend;
2093
2094 /* We stored the section contents in the elf_section_data tdata
2095 field in the set_section_contents routine. We save the
2096 section contents so that we don't have to read them again.
2097 At this point we know that elf_gp is set, so we can look
2098 through the section contents to see if there is an
2099 ODK_REGINFO structure. */
2100
2101 contents = (bfd_byte *) elf_section_data (hdr->bfd_section)->tdata;
2102 l = contents;
2103 lend = contents + hdr->sh_size;
2104 while (l + sizeof (Elf_External_Options) <= lend)
2105 {
2106 Elf_Internal_Options intopt;
2107
2108 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2109 &intopt);
2110 if (intopt.kind == ODK_REGINFO)
2111 {
2112 bfd_byte buf[4];
2113
2114 if (bfd_seek (abfd,
2115 (hdr->sh_offset
2116 + (l - contents)
2117 + sizeof (Elf_External_Options)
2118 + (sizeof (Elf32_External_RegInfo) - 4)),
2119 SEEK_SET) == -1)
2120 return false;
2121 bfd_h_put_32 (abfd, elf_gp (abfd), buf);
2122 if (bfd_write (buf, 1, 4, abfd) != 4)
2123 return false;
2124 }
2125 l += intopt.size;
2126 }
2127 }
2128
00176555
ILT
2129 return _bfd_mips_elf_section_processing (abfd, hdr);
2130}
b3c0fc57 2131\f
aac6b32f
ILT
2132/* MIPS ELF uses two common sections. One is the usual one, and the
2133 other is for small objects. All the small objects are kept
2134 together, and then referenced via the gp pointer, which yields
2135 faster assembler code. This is what we use for the small common
2136 section. This approach is copied from ecoff.c. */
2137static asection mips_elf_scom_section;
2138static asymbol mips_elf_scom_symbol;
2139static asymbol *mips_elf_scom_symbol_ptr;
2140
2141/* MIPS ELF also uses an acommon section, which represents an
2142 allocated common symbol which may be overridden by a
2143 definition in a shared library. */
2144static asection mips_elf_acom_section;
2145static asymbol mips_elf_acom_symbol;
2146static asymbol *mips_elf_acom_symbol_ptr;
2147
5b3b9ff6
ILT
2148/* The Irix 5 support uses two virtual sections, which represent
2149 text/data symbols defined in dynamic objects. */
2150static asection mips_elf_text_section;
2151static asection *mips_elf_text_section_ptr;
2152static asymbol mips_elf_text_symbol;
2153static asymbol *mips_elf_text_symbol_ptr;
2154
2155static asection mips_elf_data_section;
2156static asection *mips_elf_data_section_ptr;
2157static asymbol mips_elf_data_symbol;
2158static asymbol *mips_elf_data_symbol_ptr;
2159
00176555
ILT
2160/* Handle the special MIPS section numbers that a symbol may use.
2161 This is used for both the 32-bit and the 64-bit ABI. */
aac6b32f 2162
00176555
ILT
2163void
2164_bfd_mips_elf_symbol_processing (abfd, asym)
aac6b32f
ILT
2165 bfd *abfd;
2166 asymbol *asym;
2167{
2168 elf_symbol_type *elfsym;
2169
2170 elfsym = (elf_symbol_type *) asym;
2171 switch (elfsym->internal_elf_sym.st_shndx)
2172 {
2173 case SHN_MIPS_ACOMMON:
2174 /* This section is used in a dynamically linked executable file.
2175 It is an allocated common section. The dynamic linker can
2176 either resolve these symbols to something in a shared
2177 library, or it can just leave them here. For our purposes,
2178 we can consider these symbols to be in a new section. */
2179 if (mips_elf_acom_section.name == NULL)
2180 {
2181 /* Initialize the acommon section. */
2182 mips_elf_acom_section.name = ".acommon";
6ab826bd 2183 mips_elf_acom_section.flags = SEC_ALLOC;
aac6b32f
ILT
2184 mips_elf_acom_section.output_section = &mips_elf_acom_section;
2185 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
2186 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
2187 mips_elf_acom_symbol.name = ".acommon";
2188 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
2189 mips_elf_acom_symbol.section = &mips_elf_acom_section;
2190 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
2191 }
2192 asym->section = &mips_elf_acom_section;
2193 break;
2194
2195 case SHN_COMMON:
2196 /* Common symbols less than the GP size are automatically
2197 treated as SHN_MIPS_SCOMMON symbols. */
2198 if (asym->value > elf_gp_size (abfd))
2199 break;
2200 /* Fall through. */
2201 case SHN_MIPS_SCOMMON:
2202 if (mips_elf_scom_section.name == NULL)
2203 {
2204 /* Initialize the small common section. */
2205 mips_elf_scom_section.name = ".scommon";
2206 mips_elf_scom_section.flags = SEC_IS_COMMON;
2207 mips_elf_scom_section.output_section = &mips_elf_scom_section;
2208 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
2209 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
2210 mips_elf_scom_symbol.name = ".scommon";
2211 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
2212 mips_elf_scom_symbol.section = &mips_elf_scom_section;
2213 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
2214 }
2215 asym->section = &mips_elf_scom_section;
2216 asym->value = elfsym->internal_elf_sym.st_size;
2217 break;
2218
2219 case SHN_MIPS_SUNDEFINED:
24f13b03 2220 asym->section = bfd_und_section_ptr;
aac6b32f 2221 break;
5b3b9ff6
ILT
2222
2223#if 0 /* for SGI_COMPAT */
2224 case SHN_MIPS_TEXT:
2225 asym->section = mips_elf_text_section_ptr;
2226 break;
2227
2228 case SHN_MIPS_DATA:
2229 asym->section = mips_elf_data_section_ptr;
2230 break;
2231#endif
2232 }
2233}
2234\f
2235/* When creating an Irix 5 executable, we need REGINFO and RTPROC
2236 segments. */
2237
2238static int
2239mips_elf_additional_program_headers (abfd)
2240 bfd *abfd;
2241{
2242 asection *s;
2243 int ret;
2244
2245 ret = 0;
2246
2247 if (! SGI_COMPAT (abfd))
2248 return ret;
2249
2250 s = bfd_get_section_by_name (abfd, ".reginfo");
2251 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2252 {
2253 /* We need a PT_MIPS_REGINFO segment. */
2254 ++ret;
2255 }
2256
53787b23 2257 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL
5b3b9ff6
ILT
2258 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
2259 {
2260 /* We need a PT_MIPS_RTPROC segment. */
2261 ++ret;
2262 }
2263
2264 return ret;
2265}
2266
2267/* Modify the segment map for an Irix 5 executable. */
2268
2269static boolean
2270mips_elf_modify_segment_map (abfd)
2271 bfd *abfd;
2272{
2273 asection *s;
2274 struct elf_segment_map *m, **pm;
2275
2276 if (! SGI_COMPAT (abfd))
2277 return true;
2278
2279 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
2280 segment. */
2281 s = bfd_get_section_by_name (abfd, ".reginfo");
2282 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2283 {
2284 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2285 if (m->p_type == PT_MIPS_REGINFO)
2286 break;
2287 if (m == NULL)
2288 {
2289 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
2290 if (m == NULL)
2291 return false;
2292
2293 m->p_type = PT_MIPS_REGINFO;
2294 m->count = 1;
2295 m->sections[0] = s;
2296
2297 /* We want to put it after the PHDR and INTERP segments. */
2298 pm = &elf_tdata (abfd)->segment_map;
2299 while (*pm != NULL
2300 && ((*pm)->p_type == PT_PHDR
2301 || (*pm)->p_type == PT_INTERP))
2302 pm = &(*pm)->next;
2303
2304 m->next = *pm;
2305 *pm = m;
2306 }
2307 }
2308
2309 /* If there are .dynamic and .mdebug sections, we make a room for
2310 the RTPROC header. FIXME: Rewrite without section names. */
2311 if (bfd_get_section_by_name (abfd, ".interp") == NULL
2312 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
2313 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
2314 {
2315 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2316 if (m->p_type == PT_MIPS_RTPROC)
2317 break;
2318 if (m == NULL)
2319 {
2320 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
2321 if (m == NULL)
2322 return false;
2323
2324 m->p_type = PT_MIPS_RTPROC;
2325
2326 s = bfd_get_section_by_name (abfd, ".rtproc");
2327 if (s == NULL)
2328 {
2329 m->count = 0;
2330 m->p_flags = 0;
2331 m->p_flags_valid = 1;
2332 }
2333 else
2334 {
2335 m->count = 1;
2336 m->sections[0] = s;
2337 }
2338
2339 /* We want to put it after the DYNAMIC segment. */
2340 pm = &elf_tdata (abfd)->segment_map;
2341 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
2342 pm = &(*pm)->next;
2343 if (*pm != NULL)
2344 pm = &(*pm)->next;
2345
2346 m->next = *pm;
2347 *pm = m;
2348 }
2349 }
2350
2351 /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic, .dynstr,
2352 .dynsym, and .hash sections, and everything in between. */
2353 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
2354 if ((*pm)->p_type == PT_DYNAMIC)
2355 break;
2356 m = *pm;
2357 if (m != NULL
2358 && m->count == 1
2359 && strcmp (m->sections[0]->name, ".dynamic") == 0)
2360 {
2361 static const char *sec_names[] =
2362 { ".dynamic", ".dynstr", ".dynsym", ".hash" };
2363 bfd_vma low, high;
2364 unsigned int i, c;
2365 struct elf_segment_map *n;
2366
2367 low = 0xffffffff;
2368 high = 0;
2369 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
2370 {
2371 s = bfd_get_section_by_name (abfd, sec_names[i]);
2372 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2373 {
2374 bfd_size_type sz;
2375
2376 if (low > s->vma)
2377 low = s->vma;
2378 sz = s->_cooked_size;
2379 if (sz == 0)
2380 sz = s->_raw_size;
2381 if (high < s->vma + sz)
2382 high = s->vma + sz;
2383 }
2384 }
2385
2386 c = 0;
2387 for (s = abfd->sections; s != NULL; s = s->next)
2388 if ((s->flags & SEC_LOAD) != 0
2389 && s->vma >= low
2390 && ((s->vma
2391 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
2392 <= high))
2393 ++c;
2394
2395 n = ((struct elf_segment_map *)
2396 bfd_zalloc (abfd, sizeof *n + (c - 1) * sizeof (asection *)));
2397 if (n == NULL)
2398 return false;
2399 *n = *m;
2400 n->count = c;
2401
2402 i = 0;
2403 for (s = abfd->sections; s != NULL; s = s->next)
2404 {
2405 if ((s->flags & SEC_LOAD) != 0
2406 && s->vma >= low
2407 && ((s->vma
2408 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
2409 <= high))
2410 {
2411 n->sections[i] = s;
2412 ++i;
2413 }
2414 }
2415
2416 *pm = n;
aac6b32f 2417 }
5b3b9ff6
ILT
2418
2419 return true;
2420}
2421\f
2422/* The structure of the runtime procedure descriptor created by the
2423 loader for use by the static exception system. */
2424
2425typedef struct runtime_pdr {
2426 bfd_vma adr; /* memory address of start of procedure */
2427 long regmask; /* save register mask */
2428 long regoffset; /* save register offset */
2429 long fregmask; /* save floating point register mask */
2430 long fregoffset; /* save floating point register offset */
2431 long frameoffset; /* frame size */
2432 short framereg; /* frame pointer register */
2433 short pcreg; /* offset or reg of return pc */
2434 long irpss; /* index into the runtime string table */
2435 long reserved;
2436 struct exception_info *exception_info;/* pointer to exception array */
2437} RPDR, *pRPDR;
2438#define cbRPDR sizeof(RPDR)
2439#define rpdNil ((pRPDR) 0)
2440
2441/* Swap RPDR (runtime procedure table entry) for output. */
2442
2443static void ecoff_swap_rpdr_out
2444 PARAMS ((bfd *, const RPDR *, struct rpdr_ext *));
2445
2446static void
2447ecoff_swap_rpdr_out (abfd, in, ex)
2448 bfd *abfd;
2449 const RPDR *in;
2450 struct rpdr_ext *ex;
2451{
2452 /* ecoff_put_off was defined in ecoffswap.h. */
2453 ecoff_put_off (abfd, in->adr, (bfd_byte *) ex->p_adr);
2454 bfd_h_put_32 (abfd, in->regmask, (bfd_byte *) ex->p_regmask);
2455 bfd_h_put_32 (abfd, in->regoffset, (bfd_byte *) ex->p_regoffset);
2456 bfd_h_put_32 (abfd, in->fregmask, (bfd_byte *) ex->p_fregmask);
2457 bfd_h_put_32 (abfd, in->fregoffset, (bfd_byte *) ex->p_fregoffset);
2458 bfd_h_put_32 (abfd, in->frameoffset, (bfd_byte *) ex->p_frameoffset);
2459
2460 bfd_h_put_16 (abfd, in->framereg, (bfd_byte *) ex->p_framereg);
2461 bfd_h_put_16 (abfd, in->pcreg, (bfd_byte *) ex->p_pcreg);
2462
2463 bfd_h_put_32 (abfd, in->irpss, (bfd_byte *) ex->p_irpss);
2464#if 0 /* FIXME */
2465 ecoff_put_off (abfd, in->exception_info, (bfd_byte *) ex->p_exception_info);
2466#endif
aac6b32f
ILT
2467}
2468\f
6e07e54f
ILT
2469/* Read ECOFF debugging information from a .mdebug section into a
2470 ecoff_debug_info structure. */
b3c0fc57 2471
00176555
ILT
2472boolean
2473_bfd_mips_elf_read_ecoff_info (abfd, section, debug)
b3c0fc57 2474 bfd *abfd;
6e07e54f
ILT
2475 asection *section;
2476 struct ecoff_debug_info *debug;
b3c0fc57 2477{
6e07e54f
ILT
2478 HDRR *symhdr;
2479 const struct ecoff_debug_swap *swap;
a3a33af3 2480 char *ext_hdr = NULL;
6e07e54f
ILT
2481
2482 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
2483
5b3b9ff6 2484 ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
a3a33af3 2485 if (ext_hdr == NULL && swap->external_hdr_size != 0)
5b3b9ff6 2486 goto error_return;
6e07e54f
ILT
2487
2488 if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
2489 swap->external_hdr_size)
2490 == false)
a3a33af3 2491 goto error_return;
6e07e54f
ILT
2492
2493 symhdr = &debug->symbolic_header;
2494 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
2495
2496 /* The symbolic header contains absolute file offsets and sizes to
2497 read. */
2498#define READ(ptr, offset, count, size, type) \
2499 if (symhdr->count == 0) \
2500 debug->ptr = NULL; \
2501 else \
2502 { \
5b3b9ff6 2503 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
6e07e54f 2504 if (debug->ptr == NULL) \
5b3b9ff6 2505 goto error_return; \
6e07e54f
ILT
2506 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
2507 || (bfd_read (debug->ptr, size, symhdr->count, \
2508 abfd) != size * symhdr->count)) \
a3a33af3 2509 goto error_return; \
6e07e54f
ILT
2510 }
2511
2512 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
2513 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
2514 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
2515 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
2516 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
2517 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
2518 union aux_ext *);
2519 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
2520 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
2521 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
2522 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
aac6b32f
ILT
2523 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
2524#undef READ
9783e04a
DM
2525
2526 debug->fdr = NULL;
a3a33af3 2527 debug->adjust = NULL;
6e07e54f
ILT
2528
2529 return true;
a3a33af3
ILT
2530
2531 error_return:
2532 if (ext_hdr != NULL)
2533 free (ext_hdr);
a3a33af3
ILT
2534 if (debug->line != NULL)
2535 free (debug->line);
2536 if (debug->external_dnr != NULL)
2537 free (debug->external_dnr);
2538 if (debug->external_pdr != NULL)
2539 free (debug->external_pdr);
2540 if (debug->external_sym != NULL)
2541 free (debug->external_sym);
2542 if (debug->external_opt != NULL)
2543 free (debug->external_opt);
2544 if (debug->external_aux != NULL)
2545 free (debug->external_aux);
2546 if (debug->ss != NULL)
2547 free (debug->ss);
2548 if (debug->ssext != NULL)
2549 free (debug->ssext);
2550 if (debug->external_fdr != NULL)
2551 free (debug->external_fdr);
2552 if (debug->external_rfd != NULL)
2553 free (debug->external_rfd);
aac6b32f
ILT
2554 if (debug->external_ext != NULL)
2555 free (debug->external_ext);
a3a33af3 2556 return false;
6e07e54f 2557}
a16fcab3 2558\f
a2546fce
ILT
2559/* MIPS ELF local labels start with '$', not 'L'. */
2560
2561/*ARGSUSED*/
2562static boolean
2563mips_elf_is_local_label (abfd, symbol)
2564 bfd *abfd;
2565 asymbol *symbol;
2566{
2567 return symbol->name[0] == '$';
2568}
2569
a16fcab3
KR
2570/* MIPS ELF uses a special find_nearest_line routine in order the
2571 handle the ECOFF debugging information. */
2572
2573struct mips_elf_find_line
2574{
2575 struct ecoff_debug_info d;
2576 struct ecoff_find_line i;
2577};
6e07e54f 2578
bc05732b
ILT
2579boolean
2580_bfd_mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2581 functionname_ptr, line_ptr)
a16fcab3
KR
2582 bfd *abfd;
2583 asection *section;
2584 asymbol **symbols;
2585 bfd_vma offset;
2586 const char **filename_ptr;
2587 const char **functionname_ptr;
2588 unsigned int *line_ptr;
2589{
2590 asection *msec;
2591
2592 msec = bfd_get_section_by_name (abfd, ".mdebug");
2593 if (msec != NULL)
2594 {
6ab826bd 2595 flagword origflags;
a16fcab3
KR
2596 struct mips_elf_find_line *fi;
2597 const struct ecoff_debug_swap * const swap =
2598 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
2599
6ab826bd
FF
2600 /* If we are called during a link, mips_elf_final_link may have
2601 cleared the SEC_HAS_CONTENTS field. We force it back on here
2602 if appropriate (which it normally will be). */
2603 origflags = msec->flags;
2604 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
2605 msec->flags |= SEC_HAS_CONTENTS;
2606
a16fcab3
KR
2607 fi = elf_tdata (abfd)->find_line_info;
2608 if (fi == NULL)
2609 {
2610 bfd_size_type external_fdr_size;
2611 char *fraw_src;
2612 char *fraw_end;
2613 struct fdr *fdr_ptr;
2614
2615 fi = ((struct mips_elf_find_line *)
abec70b9 2616 bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
a16fcab3
KR
2617 if (fi == NULL)
2618 {
6ab826bd 2619 msec->flags = origflags;
a16fcab3
KR
2620 return false;
2621 }
2622
00176555 2623 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
6ab826bd
FF
2624 {
2625 msec->flags = origflags;
2626 return false;
2627 }
a16fcab3
KR
2628
2629 /* Swap in the FDR information. */
2630 fi->d.fdr = ((struct fdr *)
2631 bfd_alloc (abfd,
2632 (fi->d.symbolic_header.ifdMax *
2633 sizeof (struct fdr))));
2634 if (fi->d.fdr == NULL)
2635 {
6ab826bd 2636 msec->flags = origflags;
a16fcab3
KR
2637 return false;
2638 }
2639 external_fdr_size = swap->external_fdr_size;
2640 fdr_ptr = fi->d.fdr;
2641 fraw_src = (char *) fi->d.external_fdr;
2642 fraw_end = (fraw_src
2643 + fi->d.symbolic_header.ifdMax * external_fdr_size);
2644 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
2645 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
2646
2647 elf_tdata (abfd)->find_line_info = fi;
2648
2649 /* Note that we don't bother to ever free this information.
2650 find_nearest_line is either called all the time, as in
2651 objdump -l, so the information should be saved, or it is
2652 rarely called, as in ld error messages, so the memory
2653 wasted is unimportant. Still, it would probably be a
2654 good idea for free_cached_info to throw it away. */
2655 }
2656
2657 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
2658 &fi->i, filename_ptr, functionname_ptr,
2659 line_ptr))
6ab826bd
FF
2660 {
2661 msec->flags = origflags;
2662 return true;
2663 }
2664
2665 msec->flags = origflags;
a16fcab3
KR
2666 }
2667
2668 /* Fall back on the generic ELF find_nearest_line routine. */
2669
ede4eed4
KR
2670 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
2671 filename_ptr, functionname_ptr,
2672 line_ptr);
a16fcab3
KR
2673}
2674\f
aac6b32f
ILT
2675/* The MIPS ELF linker needs additional information for each symbol in
2676 the global hash table. */
2677
2678struct mips_elf_link_hash_entry
2679{
2680 struct elf_link_hash_entry root;
6e07e54f 2681
aac6b32f
ILT
2682 /* External symbol information. */
2683 EXTR esym;
abec70b9
ILT
2684
2685 /* Number of MIPS_32 or MIPS_REL32 relocs against this symbol. */
2686 unsigned int mips_32_relocs;
aac6b32f
ILT
2687};
2688
2689/* MIPS ELF linker hash table. */
2690
2691struct mips_elf_link_hash_table
2692{
2693 struct elf_link_hash_table root;
5b3b9ff6
ILT
2694 /* String section indices for the dynamic section symbols. */
2695 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
2696 /* The number of .rtproc entries. */
2697 bfd_size_type procedure_count;
53787b23
ILT
2698 /* The size of the .compact_rel section (if SGI_COMPAT). */
2699 bfd_size_type compact_rel_size;
aac6b32f
ILT
2700};
2701
2702/* Look up an entry in a MIPS ELF linker hash table. */
2703
2704#define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
2705 ((struct mips_elf_link_hash_entry *) \
2706 elf_link_hash_lookup (&(table)->root, (string), (create), \
2707 (copy), (follow)))
2708
2709/* Traverse a MIPS ELF linker hash table. */
2710
2711#define mips_elf_link_hash_traverse(table, func, info) \
2712 (elf_link_hash_traverse \
2713 (&(table)->root, \
2714 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
2715 (info)))
2716
2717/* Get the MIPS ELF linker hash table from a link_info structure. */
2718
2719#define mips_elf_hash_table(p) \
2720 ((struct mips_elf_link_hash_table *) ((p)->hash))
2721
2722static boolean mips_elf_output_extsym
2723 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
2724
2725/* Create an entry in a MIPS ELF linker hash table. */
2726
2727static struct bfd_hash_entry *
2728mips_elf_link_hash_newfunc (entry, table, string)
2729 struct bfd_hash_entry *entry;
2730 struct bfd_hash_table *table;
2731 const char *string;
2732{
2733 struct mips_elf_link_hash_entry *ret =
2734 (struct mips_elf_link_hash_entry *) entry;
2735
2736 /* Allocate the structure if it has not already been allocated by a
2737 subclass. */
2738 if (ret == (struct mips_elf_link_hash_entry *) NULL)
2739 ret = ((struct mips_elf_link_hash_entry *)
2740 bfd_hash_allocate (table,
2741 sizeof (struct mips_elf_link_hash_entry)));
2742 if (ret == (struct mips_elf_link_hash_entry *) NULL)
a9713b91 2743 return (struct bfd_hash_entry *) ret;
aac6b32f
ILT
2744
2745 /* Call the allocation method of the superclass. */
2746 ret = ((struct mips_elf_link_hash_entry *)
2747 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2748 table, string));
2749 if (ret != (struct mips_elf_link_hash_entry *) NULL)
2750 {
2751 /* Set local fields. */
2752 memset (&ret->esym, 0, sizeof (EXTR));
2753 /* We use -2 as a marker to indicate that the information has
2754 not been set. -1 means there is no associated ifd. */
2755 ret->esym.ifd = -2;
abec70b9 2756 ret->mips_32_relocs = 0;
aac6b32f
ILT
2757 }
2758
2759 return (struct bfd_hash_entry *) ret;
2760}
2761
2762/* Create a MIPS ELF linker hash table. */
2763
2764static struct bfd_link_hash_table *
2765mips_elf_link_hash_table_create (abfd)
2766 bfd *abfd;
2767{
2768 struct mips_elf_link_hash_table *ret;
5b3b9ff6 2769 unsigned int i;
aac6b32f
ILT
2770
2771 ret = ((struct mips_elf_link_hash_table *)
2772 bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table)));
2773 if (ret == (struct mips_elf_link_hash_table *) NULL)
a9713b91 2774 return NULL;
aac6b32f
ILT
2775
2776 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
2777 mips_elf_link_hash_newfunc))
2778 {
2779 bfd_release (abfd, ret);
2780 return NULL;
2781 }
2782
5b3b9ff6
ILT
2783 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
2784 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
2785 ret->procedure_count = 0;
53787b23 2786 ret->compact_rel_size = 0;
5b3b9ff6 2787
aac6b32f
ILT
2788 return &ret->root.root;
2789}
2790
2791/* Hook called by the linker routine which adds symbols from an object
2792 file. We must handle the special MIPS section numbers here. */
2793
2794/*ARGSUSED*/
6e07e54f 2795static boolean
aac6b32f
ILT
2796mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2797 bfd *abfd;
2798 struct bfd_link_info *info;
2799 const Elf_Internal_Sym *sym;
2800 const char **namep;
2801 flagword *flagsp;
2802 asection **secp;
2803 bfd_vma *valp;
2804{
53787b23
ILT
2805 if (SGI_COMPAT (abfd)
2806 && (abfd->flags & DYNAMIC) != 0
2807 && strcmp (*namep, "_rld_new_interface") == 0)
2808 {
2809 /* Skip Irix 5 rld entry name. */
2810 *namep = NULL;
2811 return true;
2812 }
2813
aac6b32f
ILT
2814 switch (sym->st_shndx)
2815 {
2816 case SHN_COMMON:
2817 /* Common symbols less than the GP size are automatically
2818 treated as SHN_MIPS_SCOMMON symbols. */
2819 if (sym->st_size > elf_gp_size (abfd))
2820 break;
2821 /* Fall through. */
2822 case SHN_MIPS_SCOMMON:
2823 *secp = bfd_make_section_old_way (abfd, ".scommon");
2824 (*secp)->flags |= SEC_IS_COMMON;
2825 *valp = sym->st_size;
2826 break;
2827
5b3b9ff6
ILT
2828 case SHN_MIPS_TEXT:
2829 /* This section is used in a shared object. */
2830 if (mips_elf_text_section_ptr == NULL)
2831 {
2832 /* Initialize the section. */
2833 mips_elf_text_section.name = ".text";
2834 mips_elf_text_section.flags = SEC_NO_FLAGS;
2835 mips_elf_text_section.output_section = NULL;
2836 mips_elf_text_section.owner = abfd;
2837 mips_elf_text_section.symbol = &mips_elf_text_symbol;
2838 mips_elf_text_section.symbol_ptr_ptr = &mips_elf_text_symbol_ptr;
2839 mips_elf_text_symbol.name = ".text";
2840 mips_elf_text_symbol.flags = BSF_SECTION_SYM;
2841 mips_elf_text_symbol.section = &mips_elf_text_section;
2842 mips_elf_text_symbol_ptr = &mips_elf_text_symbol;
2843 mips_elf_text_section_ptr = &mips_elf_text_section;
2844 }
2845 if (info->shared)
2846 *secp = bfd_und_section_ptr;
2847 else
2848 *secp = mips_elf_text_section_ptr;
2849 break;
2850
53787b23
ILT
2851 case SHN_MIPS_ACOMMON:
2852 /* Fall through. XXX Can we treat this as allocated data? */
5b3b9ff6
ILT
2853 case SHN_MIPS_DATA:
2854 /* This section is used in a shared object. */
2855 if (mips_elf_data_section_ptr == NULL)
2856 {
2857 /* Initialize the section. */
2858 mips_elf_data_section.name = ".data";
2859 mips_elf_data_section.flags = SEC_NO_FLAGS;
2860 mips_elf_data_section.output_section = NULL;
2861 mips_elf_data_section.owner = abfd;
2862 mips_elf_data_section.symbol = &mips_elf_data_symbol;
2863 mips_elf_data_section.symbol_ptr_ptr = &mips_elf_data_symbol_ptr;
2864 mips_elf_data_symbol.name = ".data";
2865 mips_elf_data_symbol.flags = BSF_SECTION_SYM;
2866 mips_elf_data_symbol.section = &mips_elf_data_section;
2867 mips_elf_data_symbol_ptr = &mips_elf_data_symbol;
2868 mips_elf_data_section_ptr = &mips_elf_data_section;
2869 }
2870 if (info->shared)
2871 *secp = bfd_und_section_ptr;
2872 else
2873 *secp = mips_elf_data_section_ptr;
2874 break;
2875
aac6b32f 2876 case SHN_MIPS_SUNDEFINED:
24f13b03 2877 *secp = bfd_und_section_ptr;
aac6b32f
ILT
2878 break;
2879 }
2880
2881 return true;
2882}
2883
2884/* Structure used to pass information to mips_elf_output_extsym. */
2885
2886struct extsym_info
6e07e54f 2887{
aac6b32f
ILT
2888 bfd *abfd;
2889 struct bfd_link_info *info;
2890 struct ecoff_debug_info *debug;
9783e04a 2891 const struct ecoff_debug_swap *swap;
aac6b32f
ILT
2892 boolean failed;
2893};
9783e04a 2894
aac6b32f
ILT
2895/* This routine is used to write out ECOFF debugging external symbol
2896 information. It is called via mips_elf_link_hash_traverse. The
2897 ECOFF external symbol information must match the ELF external
2898 symbol information. Unfortunately, at this point we don't know
2899 whether a symbol is required by reloc information, so the two
2900 tables may wind up being different. We must sort out the external
2901 symbol information before we can set the final size of the .mdebug
2902 section, and we must set the size of the .mdebug section before we
2903 can relocate any sections, and we can't know which symbols are
2904 required by relocation until we relocate the sections.
2905 Fortunately, it is relatively unlikely that any symbol will be
2906 stripped but required by a reloc. In particular, it can not happen
2907 when generating a final executable. */
2908
2909static boolean
2910mips_elf_output_extsym (h, data)
2911 struct mips_elf_link_hash_entry *h;
2912 PTR data;
2913{
2914 struct extsym_info *einfo = (struct extsym_info *) data;
2915 boolean strip;
5b3b9ff6 2916 asection *sec, *output_section;
aac6b32f
ILT
2917
2918 if (h->root.indx == -2)
2919 strip = false;
2920 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2921 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
2922 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2923 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
2924 strip = true;
2925 else if (einfo->info->strip == strip_all
2926 || (einfo->info->strip == strip_some
2927 && bfd_hash_lookup (einfo->info->keep_hash,
2928 h->root.root.root.string,
2929 false, false) == NULL))
2930 strip = true;
2931 else
2932 strip = false;
2933
2934 if (strip)
2935 return true;
6e07e54f 2936
aac6b32f 2937 if (h->esym.ifd == -2)
6e07e54f 2938 {
aac6b32f
ILT
2939 h->esym.jmptbl = 0;
2940 h->esym.cobol_main = 0;
2941 h->esym.weakext = 0;
2942 h->esym.reserved = 0;
2943 h->esym.ifd = ifdNil;
2944 h->esym.asym.value = 0;
2945 h->esym.asym.st = stGlobal;
2946
5b3b9ff6
ILT
2947 if (SGI_COMPAT (einfo->abfd)
2948 && (h->root.root.type == bfd_link_hash_undefined
2949 || h->root.root.type == bfd_link_hash_undefweak))
2950 {
2951 const char *name;
2952
2953 /* Use undefined class. Also, set class and type for some
2954 special symbols. */
2955 name = h->root.root.root.string;
2956 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2957 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2958 {
2959 h->esym.asym.sc = scData;
2960 h->esym.asym.st = stLabel;
2961 h->esym.asym.value = 0;
2962 }
2963 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2964 {
2965 h->esym.asym.sc = scAbs;
2966 h->esym.asym.st = stLabel;
2967 h->esym.asym.value =
2968 mips_elf_hash_table (einfo->info)->procedure_count;
2969 }
2970 else if (strcmp (name, "_gp_disp") == 0)
2971 {
2972 h->esym.asym.sc = scAbs;
2973 h->esym.asym.st = stLabel;
2974 h->esym.asym.value = elf_gp (einfo->abfd);
2975 }
2976 else
2977 h->esym.asym.sc = scUndefined;
2978 }
2979 else if (h->root.root.type != bfd_link_hash_defined
a16fcab3 2980 && h->root.root.type != bfd_link_hash_defweak)
aac6b32f
ILT
2981 h->esym.asym.sc = scAbs;
2982 else
2983 {
aac6b32f
ILT
2984 const char *name;
2985
5b3b9ff6
ILT
2986 sec = h->root.root.u.def.section;
2987 output_section = sec->output_section;
2988
2989 /* When making a shared library and symbol h is the one from
2990 the another shared library, OUTPUT_SECTION may be null. */
2991 if (output_section == NULL)
2992 h->esym.asym.sc = scUndefined;
aac6b32f 2993 else
5b3b9ff6
ILT
2994 {
2995 name = bfd_section_name (output_section->owner, output_section);
2996
2997 if (strcmp (name, ".text") == 0)
2998 h->esym.asym.sc = scText;
2999 else if (strcmp (name, ".data") == 0)
3000 h->esym.asym.sc = scData;
3001 else if (strcmp (name, ".sdata") == 0)
3002 h->esym.asym.sc = scSData;
3003 else if (strcmp (name, ".rodata") == 0
3004 || strcmp (name, ".rdata") == 0)
3005 h->esym.asym.sc = scRData;
3006 else if (strcmp (name, ".bss") == 0)
3007 h->esym.asym.sc = scBss;
3008 else if (strcmp (name, ".sbss") == 0)
3009 h->esym.asym.sc = scSBss;
3010 else if (strcmp (name, ".init") == 0)
3011 h->esym.asym.sc = scInit;
3012 else if (strcmp (name, ".fini") == 0)
3013 h->esym.asym.sc = scFini;
3014 else
3015 h->esym.asym.sc = scAbs;
3016 }
aac6b32f
ILT
3017 }
3018
3019 h->esym.asym.reserved = 0;
3020 h->esym.asym.index = indexNil;
6e07e54f
ILT
3021 }
3022
16fe8950
ILT
3023 if (h->root.root.type == bfd_link_hash_common)
3024 h->esym.asym.value = h->root.root.u.c.size;
a16fcab3
KR
3025 else if (h->root.root.type == bfd_link_hash_defined
3026 || h->root.root.type == bfd_link_hash_defweak)
16fe8950 3027 {
16fe8950
ILT
3028 if (h->esym.asym.sc == scCommon)
3029 h->esym.asym.sc = scBss;
3030 else if (h->esym.asym.sc == scSCommon)
3031 h->esym.asym.sc = scSBss;
3032
3033 sec = h->root.root.u.def.section;
5b3b9ff6
ILT
3034 output_section = sec->output_section;
3035 if (output_section != NULL)
3036 h->esym.asym.value = (h->root.root.u.def.value
3037 + sec->output_offset
3038 + output_section->vma);
3039 else
3040 h->esym.asym.value = 0;
16fe8950 3041 }
5b3b9ff6
ILT
3042 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
3043 {
3044 /* Set type and value for a symbol with a function stub. */
3045 h->esym.asym.st = stProc;
3046 sec = h->root.root.u.def.section;
3047 if (sec == NULL)
3048 h->esym.asym.value = 0;
3049 else
3050 {
3051 output_section = sec->output_section;
3052 if (output_section != NULL)
3053 h->esym.asym.value = (h->root.plt_offset
3054 + sec->output_offset
3055 + output_section->vma);
3056 else
3057 h->esym.asym.value = 0;
3058 }
3059#if 0 /* FIXME? */
3060 h->esym.ifd = 0;
3061#endif
3062 }
aac6b32f
ILT
3063
3064 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3065 h->root.root.root.string,
3066 &h->esym))
3067 {
3068 einfo->failed = true;
3069 return false;
3070 }
6e07e54f
ILT
3071
3072 return true;
3073}
3074
5b3b9ff6
ILT
3075/* Create a runtime procedure table from the .mdebug section. */
3076
3077static boolean
3078mips_elf_create_procedure_table (handle, abfd, info, s, debug)
3079 PTR handle;
3080 bfd *abfd;
3081 struct bfd_link_info *info;
3082 asection *s;
3083 struct ecoff_debug_info *debug;
3084{
3085 const struct ecoff_debug_swap *swap;
3086 HDRR *hdr = &debug->symbolic_header;
3087 RPDR *rpdr, *rp;
3088 struct rpdr_ext *erp;
3089 PTR rtproc;
3090 struct pdr_ext *epdr;
3091 struct sym_ext *esym;
3092 char *ss, **sv;
3093 char *str;
3094 unsigned long size, count;
3095 unsigned long sindex;
3096 unsigned long i;
3097 PDR pdr;
3098 SYMR sym;
3099 const char *no_name_func = "static procedure (no name)";
3100
3101 epdr = NULL;
3102 rpdr = NULL;
3103 esym = NULL;
3104 ss = NULL;
abec70b9 3105 sv = NULL;
5b3b9ff6
ILT
3106
3107 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3108
3109 sindex = strlen (no_name_func) + 1;
3110 count = hdr->ipdMax;
3111 if (count > 0)
3112 {
3113 size = swap->external_pdr_size;
3114
3115 epdr = (struct pdr_ext *) bfd_malloc (size * count);
3116 if (epdr == NULL)
3117 goto error_return;
3118
3119 if (! _bfd_ecoff_get_accumulated_pdr (handle, (PTR) epdr))
3120 goto error_return;
3121
3122 size = sizeof (RPDR);
3123 rp = rpdr = (RPDR *) bfd_malloc (size * count);
3124 if (rpdr == NULL)
3125 goto error_return;
3126
3127 sv = (char **) bfd_malloc (sizeof (char *) * count);
3128 if (sv == NULL)
3129 goto error_return;
3130
3131 count = hdr->isymMax;
3132 size = swap->external_sym_size;
3133 esym = (struct sym_ext *) bfd_malloc (size * count);
3134 if (esym == NULL)
3135 goto error_return;
3136
3137 if (! _bfd_ecoff_get_accumulated_sym (handle, (PTR) esym))
3138 goto error_return;
3139
3140 count = hdr->issMax;
3141 ss = (char *) bfd_malloc (count);
3142 if (ss == NULL)
3143 goto error_return;
3144 if (! _bfd_ecoff_get_accumulated_ss (handle, (PTR) ss))
3145 goto error_return;
3146
3147 count = hdr->ipdMax;
abec70b9 3148 for (i = 0; i < count; i++, rp++)
5b3b9ff6 3149 {
abec70b9 3150 (*swap->swap_pdr_in) (abfd, (PTR) (epdr + i), &pdr);
5b3b9ff6
ILT
3151 (*swap->swap_sym_in) (abfd, (PTR) &esym[pdr.isym], &sym);
3152 rp->adr = sym.value;
3153 rp->regmask = pdr.regmask;
3154 rp->regoffset = pdr.regoffset;
3155 rp->fregmask = pdr.fregmask;
3156 rp->fregoffset = pdr.fregoffset;
3157 rp->frameoffset = pdr.frameoffset;
3158 rp->framereg = pdr.framereg;
3159 rp->pcreg = pdr.pcreg;
3160 rp->irpss = sindex;
3161 sv[i] = ss + sym.iss;
3162 sindex += strlen (sv[i]) + 1;
3163 }
3164 }
3165
3166 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
3167 size = BFD_ALIGN (size, 16);
3168 rtproc = (PTR) bfd_alloc (abfd, size);
3169 if (rtproc == NULL)
3170 {
3171 mips_elf_hash_table (info)->procedure_count = 0;
3172 goto error_return;
3173 }
3174
3175 mips_elf_hash_table (info)->procedure_count = count + 2;
3176
3177 erp = (struct rpdr_ext *) rtproc;
abec70b9 3178 memset (erp, 0, sizeof (struct rpdr_ext));
5b3b9ff6
ILT
3179 erp++;
3180 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
3181 strcpy (str, no_name_func);
3182 str += strlen (no_name_func) + 1;
3183 for (i = 0; i < count; i++)
3184 {
3185 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
3186 strcpy (str, sv[i]);
3187 str += strlen (sv[i]) + 1;
3188 }
3189 ecoff_put_off (abfd, (bfd_vma) -1, (bfd_byte *) (erp + count)->p_adr);
3190
3191 /* Set the size and contents of .rtproc section. */
3192 s->_raw_size = size;
3193 s->contents = rtproc;
3194
3195 /* Skip this section later on (I don't think this currently
3196 matters, but someday it might). */
3197 s->link_order_head = (struct bfd_link_order *) NULL;
3198
abec70b9
ILT
3199 if (epdr != NULL)
3200 free (epdr);
3201 if (rpdr != NULL)
3202 free (rpdr);
3203 if (esym != NULL)
3204 free (esym);
3205 if (ss != NULL)
3206 free (ss);
3207 if (sv != NULL)
3208 free (sv);
5b3b9ff6
ILT
3209
3210 return true;
3211
3212 error_return:
3213 if (epdr != NULL)
3214 free (epdr);
3215 if (rpdr != NULL)
3216 free (rpdr);
3217 if (esym != NULL)
3218 free (esym);
3219 if (ss != NULL)
3220 free (ss);
3221 if (sv != NULL)
3222 free (sv);
3223 return false;
3224}
3225
aac6b32f 3226/* A comparison routine used to sort .gptab entries. */
6e07e54f 3227
aac6b32f
ILT
3228static int
3229gptab_compare (p1, p2)
3230 const PTR p1;
3231 const PTR p2;
6e07e54f 3232{
aac6b32f
ILT
3233 const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
3234 const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
3235
3236 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
6e07e54f
ILT
3237}
3238
3239/* We need to use a special link routine to handle the .reginfo and
3240 the .mdebug sections. We need to merge all instances of these
3241 sections together, not write them all out sequentially. */
3242
3243static boolean
3244mips_elf_final_link (abfd, info)
3245 bfd *abfd;
3246 struct bfd_link_info *info;
3247{
6e07e54f
ILT
3248 asection **secpp;
3249 asection *o;
3250 struct bfd_link_order *p;
aac6b32f 3251 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
5b3b9ff6 3252 asection *rtproc_sec;
b3c0fc57 3253 Elf32_RegInfo reginfo;
6e07e54f
ILT
3254 struct ecoff_debug_info debug;
3255 const struct ecoff_debug_swap *swap
3256 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3257 HDRR *symhdr = &debug.symbolic_header;
9783e04a 3258 PTR mdebug_handle = NULL;
6e07e54f 3259
497c5434 3260 /* Drop the .options section, since it has special semantics which I
aac6b32f
ILT
3261 haven't bothered to figure out. */
3262 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
6e07e54f 3263 {
aac6b32f 3264 if (strcmp ((*secpp)->name, ".options") == 0)
6e07e54f 3265 {
aac6b32f
ILT
3266 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
3267 if (p->type == bfd_indirect_link_order)
3268 p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
3269 (*secpp)->link_order_head = NULL;
6e07e54f
ILT
3270 *secpp = (*secpp)->next;
3271 --abfd->section_count;
aac6b32f 3272 break;
6e07e54f 3273 }
6e07e54f 3274 }
b3c0fc57 3275
5b3b9ff6
ILT
3276 /* Get a value for the GP register. */
3277 if (elf_gp (abfd) == 0)
b3c0fc57 3278 {
5b3b9ff6
ILT
3279 struct bfd_link_hash_entry *h;
3280
3281 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
3282 if (h != (struct bfd_link_hash_entry *) NULL
3283 && h->type == bfd_link_hash_defined)
3284 elf_gp (abfd) = (h->u.def.value
3285 + h->u.def.section->output_section->vma
3286 + h->u.def.section->output_offset);
3287 else if (info->relocateable)
b3c0fc57 3288 {
5b3b9ff6 3289 bfd_vma lo;
b3c0fc57 3290
5b3b9ff6
ILT
3291 /* Make up a value. */
3292 lo = (bfd_vma) -1;
3293 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3294 {
3295 if (o->vma < lo
3296 && (strcmp (o->name, ".sbss") == 0
3297 || strcmp (o->name, ".sdata") == 0
3298 || strcmp (o->name, ".lit4") == 0
3299 || strcmp (o->name, ".lit8") == 0))
3300 lo = o->vma;
3301 }
3302 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
3303 }
3304 else
3305 {
3306 /* If the relocate_section function needs to do a reloc
3307 involving the GP value, it should make a reloc_dangerous
3308 callback to warn that GP is not defined. */
3309 }
3310 }
3311
3312 /* Go through the sections and collect the .reginfo and .mdebug
3313 information. */
3314 reginfo_sec = NULL;
3315 mdebug_sec = NULL;
3316 gptab_data_sec = NULL;
3317 gptab_bss_sec = NULL;
3318 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3319 {
3320 if (strcmp (o->name, ".reginfo") == 0)
3321 {
3322 memset (&reginfo, 0, sizeof reginfo);
3323
3324 /* We have found the .reginfo section in the output file.
3325 Look through all the link_orders comprising it and merge
3326 the information together. */
3327 for (p = o->link_order_head;
3328 p != (struct bfd_link_order *) NULL;
3329 p = p->next)
3330 {
3331 asection *input_section;
6e07e54f
ILT
3332 bfd *input_bfd;
3333 Elf32_External_RegInfo ext;
3334 Elf32_RegInfo sub;
3335
3336 if (p->type != bfd_indirect_link_order)
aac6b32f
ILT
3337 {
3338 if (p->type == bfd_fill_link_order)
3339 continue;
3340 abort ();
3341 }
6e07e54f
ILT
3342
3343 input_section = p->u.indirect.section;
3344 input_bfd = input_section->owner;
a16fcab3
KR
3345
3346 /* The linker emulation code has probably clobbered the
3347 size to be zero bytes. */
3348 if (input_section->_raw_size == 0)
3349 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
3350
6e07e54f
ILT
3351 if (! bfd_get_section_contents (input_bfd, input_section,
3352 (PTR) &ext,
3353 (file_ptr) 0,
3354 sizeof ext))
3355 return false;
3356
3357 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
3358
3359 reginfo.ri_gprmask |= sub.ri_gprmask;
3360 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
3361 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
3362 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
3363 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
3364
3365 /* ri_gp_value is set by the function
00176555 3366 mips_elf32_section_processing when the section is
6e07e54f 3367 finally written out. */
aac6b32f
ILT
3368
3369 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3370 elf_link_input_bfd ignores this section. */
3371 input_section->flags &=~ SEC_HAS_CONTENTS;
6e07e54f
ILT
3372 }
3373
3374 /* Force the section size to the value we want. */
3375 o->_raw_size = sizeof (Elf32_External_RegInfo);
3376
aac6b32f
ILT
3377 /* Skip this section later on (I don't think this currently
3378 matters, but someday it might). */
6e07e54f
ILT
3379 o->link_order_head = (struct bfd_link_order *) NULL;
3380
3381 reginfo_sec = o;
3382 }
3383
3384 if (strcmp (o->name, ".mdebug") == 0)
3385 {
aac6b32f
ILT
3386 struct extsym_info einfo;
3387
6e07e54f
ILT
3388 /* We have found the .mdebug section in the output file.
3389 Look through all the link_orders comprising it and merge
3390 the information together. */
3391 symhdr->magic = swap->sym_magic;
3392 /* FIXME: What should the version stamp be? */
3393 symhdr->vstamp = 0;
3394 symhdr->ilineMax = 0;
3395 symhdr->cbLine = 0;
3396 symhdr->idnMax = 0;
3397 symhdr->ipdMax = 0;
3398 symhdr->isymMax = 0;
3399 symhdr->ioptMax = 0;
3400 symhdr->iauxMax = 0;
3401 symhdr->issMax = 0;
3402 symhdr->issExtMax = 0;
3403 symhdr->ifdMax = 0;
3404 symhdr->crfd = 0;
3405 symhdr->iextMax = 0;
3406
3407 /* We accumulate the debugging information itself in the
3408 debug_info structure. */
9783e04a
DM
3409 debug.line = NULL;
3410 debug.external_dnr = NULL;
3411 debug.external_pdr = NULL;
3412 debug.external_sym = NULL;
3413 debug.external_opt = NULL;
3414 debug.external_aux = NULL;
3415 debug.ss = NULL;
6e07e54f 3416 debug.ssext = debug.ssext_end = NULL;
9783e04a
DM
3417 debug.external_fdr = NULL;
3418 debug.external_rfd = NULL;
6e07e54f
ILT
3419 debug.external_ext = debug.external_ext_end = NULL;
3420
9783e04a
DM
3421 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
3422 if (mdebug_handle == (PTR) NULL)
3423 return false;
3424
5b3b9ff6
ILT
3425 if (SGI_COMPAT (abfd))
3426 {
3427 asection *s;
3428 EXTR esym;
3429 bfd_vma last;
3430 unsigned int i;
3431 static const char * const name[] =
3432 { ".text", ".init", ".fini", ".data",
3433 ".rodata", ".sdata", ".sbss", ".bss" };
3434 static const int sc[] = { scText, scInit, scFini, scData,
3435 scRData, scSData, scSBss, scBss };
3436
3437 esym.jmptbl = 0;
3438 esym.cobol_main = 0;
3439 esym.weakext = 0;
3440 esym.reserved = 0;
3441 esym.ifd = ifdNil;
3442 esym.asym.iss = issNil;
3443 esym.asym.st = stLocal;
3444 esym.asym.reserved = 0;
3445 esym.asym.index = indexNil;
3446 for (i = 0; i < 8; i++)
3447 {
3448 esym.asym.sc = sc[i];
3449 s = bfd_get_section_by_name (abfd, name[i]);
3450 if (s != NULL)
3451 {
3452 esym.asym.value = s->vma;
3453 last = s->vma + s->_raw_size;
3454 }
3455 else
3456 esym.asym.value = last;
3457
3458 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
3459 name[i], &esym))
3460 return false;
3461 }
3462 }
3463
6e07e54f
ILT
3464 for (p = o->link_order_head;
3465 p != (struct bfd_link_order *) NULL;
3466 p = p->next)
3467 {
3468 asection *input_section;
3469 bfd *input_bfd;
3470 const struct ecoff_debug_swap *input_swap;
3471 struct ecoff_debug_info input_debug;
aac6b32f
ILT
3472 char *eraw_src;
3473 char *eraw_end;
6e07e54f
ILT
3474
3475 if (p->type != bfd_indirect_link_order)
aac6b32f
ILT
3476 {
3477 if (p->type == bfd_fill_link_order)
3478 continue;
3479 abort ();
3480 }
6e07e54f
ILT
3481
3482 input_section = p->u.indirect.section;
3483 input_bfd = input_section->owner;
3484
3485 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
3486 || (get_elf_backend_data (input_bfd)
3487 ->elf_backend_ecoff_debug_swap) == NULL)
b3c0fc57 3488 {
6e07e54f
ILT
3489 /* I don't know what a non MIPS ELF bfd would be
3490 doing with a .mdebug section, but I don't really
3491 want to deal with it. */
3492 continue;
b3c0fc57 3493 }
6e07e54f
ILT
3494
3495 input_swap = (get_elf_backend_data (input_bfd)
3496 ->elf_backend_ecoff_debug_swap);
3497
3498 BFD_ASSERT (p->size == input_section->_raw_size);
3499
3500 /* The ECOFF linking code expects that we have already
3501 read in the debugging information and set up an
3502 ecoff_debug_info structure, so we do that now. */
00176555
ILT
3503 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
3504 &input_debug))
6e07e54f
ILT
3505 return false;
3506
3507 if (! (bfd_ecoff_debug_accumulate
9783e04a
DM
3508 (mdebug_handle, abfd, &debug, swap, input_bfd,
3509 &input_debug, input_swap, info)))
6e07e54f
ILT
3510 return false;
3511
3512 /* Loop through the external symbols. For each one with
aac6b32f
ILT
3513 interesting information, try to find the symbol in
3514 the linker global hash table and save the information
3515 for the output external symbols. */
3516 eraw_src = input_debug.external_ext;
3517 eraw_end = (eraw_src
3518 + (input_debug.symbolic_header.iextMax
3519 * input_swap->external_ext_size));
3520 for (;
3521 eraw_src < eraw_end;
3522 eraw_src += input_swap->external_ext_size)
6e07e54f 3523 {
aac6b32f
ILT
3524 EXTR ext;
3525 const char *name;
3526 struct mips_elf_link_hash_entry *h;
3527
3528 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
3529 if (ext.asym.sc == scNil
3530 || ext.asym.sc == scUndefined
3531 || ext.asym.sc == scSUndefined)
3532 continue;
3533
3534 name = input_debug.ssext + ext.asym.iss;
3535 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
3536 name, false, false, true);
3537 if (h == NULL || h->esym.ifd != -2)
3538 continue;
3539
3540 if (ext.ifd != -1)
6e07e54f 3541 {
aac6b32f
ILT
3542 BFD_ASSERT (ext.ifd
3543 < input_debug.symbolic_header.ifdMax);
3544 ext.ifd = input_debug.ifdmap[ext.ifd];
6e07e54f 3545 }
aac6b32f
ILT
3546
3547 h->esym = ext;
6e07e54f
ILT
3548 }
3549
aac6b32f 3550 /* Free up the information we just read. */
6e07e54f
ILT
3551 free (input_debug.line);
3552 free (input_debug.external_dnr);
3553 free (input_debug.external_pdr);
3554 free (input_debug.external_sym);
3555 free (input_debug.external_opt);
3556 free (input_debug.external_aux);
3557 free (input_debug.ss);
3558 free (input_debug.ssext);
3559 free (input_debug.external_fdr);
3560 free (input_debug.external_rfd);
aac6b32f
ILT
3561 free (input_debug.external_ext);
3562
3563 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3564 elf_link_input_bfd ignores this section. */
3565 input_section->flags &=~ SEC_HAS_CONTENTS;
b3c0fc57
ILT
3566 }
3567
5b3b9ff6
ILT
3568 if (SGI_COMPAT (abfd) && info->shared)
3569 {
3570 /* Create .rtproc section. */
3571 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
3572 if (rtproc_sec == NULL)
3573 {
3574 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
3575 | SEC_READONLY);
3576
3577 rtproc_sec = bfd_make_section (abfd, ".rtproc");
3578 if (rtproc_sec == NULL
3579 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
3580 || ! bfd_set_section_alignment (abfd, rtproc_sec, 12))
3581 return false;
3582 }
3583
3584 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
3585 info, rtproc_sec, &debug))
3586 return false;
3587 }
3588
6e07e54f 3589 /* Build the external symbol information. */
aac6b32f
ILT
3590 einfo.abfd = abfd;
3591 einfo.info = info;
3592 einfo.debug = &debug;
3593 einfo.swap = swap;
3594 einfo.failed = false;
3595 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
3596 mips_elf_output_extsym,
3597 (PTR) &einfo);
3598 if (einfo.failed)
b3c0fc57
ILT
3599 return false;
3600
aac6b32f 3601 /* Set the size of the .mdebug section. */
6e07e54f
ILT
3602 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
3603
aac6b32f
ILT
3604 /* Skip this section later on (I don't think this currently
3605 matters, but someday it might). */
6e07e54f
ILT
3606 o->link_order_head = (struct bfd_link_order *) NULL;
3607
3608 mdebug_sec = o;
3609 }
6e07e54f 3610
aac6b32f 3611 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
6e07e54f 3612 {
aac6b32f
ILT
3613 const char *subname;
3614 unsigned int c;
3615 Elf32_gptab *tab;
3616 Elf32_External_gptab *ext_tab;
3617 unsigned int i;
3618
3619 /* The .gptab.sdata and .gptab.sbss sections hold
3620 information describing how the small data area would
3621 change depending upon the -G switch. These sections
3622 not used in executables files. */
3623 if (! info->relocateable)
3624 {
3625 asection **secpp;
3626
3627 for (p = o->link_order_head;
3628 p != (struct bfd_link_order *) NULL;
3629 p = p->next)
3630 {
3631 asection *input_section;
3632
3633 if (p->type != bfd_indirect_link_order)
3634 {
3635 if (p->type == bfd_fill_link_order)
3636 continue;
3637 abort ();
3638 }
3639
3640 input_section = p->u.indirect.section;
3641
3642 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3643 elf_link_input_bfd ignores this section. */
3644 input_section->flags &=~ SEC_HAS_CONTENTS;
3645 }
3646
3647 /* Skip this section later on (I don't think this
3648 currently matters, but someday it might). */
3649 o->link_order_head = (struct bfd_link_order *) NULL;
3650
3651 /* Really remove the section. */
3652 for (secpp = &abfd->sections;
3653 *secpp != o;
3654 secpp = &(*secpp)->next)
3655 ;
3656 *secpp = (*secpp)->next;
3657 --abfd->section_count;
3658
3659 continue;
3660 }
3661
3662 /* There is one gptab for initialized data, and one for
3663 uninitialized data. */
3664 if (strcmp (o->name, ".gptab.sdata") == 0)
3665 gptab_data_sec = o;
3666 else if (strcmp (o->name, ".gptab.sbss") == 0)
3667 gptab_bss_sec = o;
3668 else
3669 {
a9713b91
ILT
3670 (*_bfd_error_handler)
3671 ("%s: illegal section name `%s'",
3672 bfd_get_filename (abfd), o->name);
aac6b32f
ILT
3673 bfd_set_error (bfd_error_nonrepresentable_section);
3674 return false;
3675 }
3676
3677 /* The linker script always combines .gptab.data and
3678 .gptab.sdata into .gptab.sdata, and likewise for
3679 .gptab.bss and .gptab.sbss. It is possible that there is
3680 no .sdata or .sbss section in the output file, in which
3681 case we must change the name of the output section. */
3682 subname = o->name + sizeof ".gptab" - 1;
3683 if (bfd_get_section_by_name (abfd, subname) == NULL)
3684 {
3685 if (o == gptab_data_sec)
3686 o->name = ".gptab.data";
3687 else
3688 o->name = ".gptab.bss";
3689 subname = o->name + sizeof ".gptab" - 1;
3690 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
3691 }
3692
3693 /* Set up the first entry. */
3694 c = 1;
5b3b9ff6 3695 tab = (Elf32_gptab *) bfd_malloc (c * sizeof (Elf32_gptab));
aac6b32f 3696 if (tab == NULL)
5b3b9ff6 3697 return false;
aac6b32f
ILT
3698 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
3699 tab[0].gt_header.gt_unused = 0;
3700
3701 /* Combine the input sections. */
6e07e54f
ILT
3702 for (p = o->link_order_head;
3703 p != (struct bfd_link_order *) NULL;
3704 p = p->next)
3705 {
aac6b32f
ILT
3706 asection *input_section;
3707 bfd *input_bfd;
3708 bfd_size_type size;
3709 unsigned long last;
3710 bfd_size_type gpentry;
3711
3712 if (p->type != bfd_indirect_link_order)
6e07e54f 3713 {
aac6b32f
ILT
3714 if (p->type == bfd_fill_link_order)
3715 continue;
3716 abort ();
3717 }
6e07e54f 3718
aac6b32f
ILT
3719 input_section = p->u.indirect.section;
3720 input_bfd = input_section->owner;
3721
3722 /* Combine the gptab entries for this input section one
3723 by one. We know that the input gptab entries are
3724 sorted by ascending -G value. */
3725 size = bfd_section_size (input_bfd, input_section);
3726 last = 0;
3727 for (gpentry = sizeof (Elf32_External_gptab);
3728 gpentry < size;
3729 gpentry += sizeof (Elf32_External_gptab))
3730 {
3731 Elf32_External_gptab ext_gptab;
3732 Elf32_gptab int_gptab;
3733 unsigned long val;
3734 unsigned long add;
3735 boolean exact;
3736 unsigned int look;
3737
3738 if (! (bfd_get_section_contents
3739 (input_bfd, input_section, (PTR) &ext_gptab,
3740 gpentry, sizeof (Elf32_External_gptab))))
9783e04a 3741 {
aac6b32f 3742 free (tab);
9783e04a
DM
3743 return false;
3744 }
aac6b32f
ILT
3745
3746 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
3747 &int_gptab);
3748 val = int_gptab.gt_entry.gt_g_value;
3749 add = int_gptab.gt_entry.gt_bytes - last;
3750
3751 exact = false;
3752 for (look = 1; look < c; look++)
3753 {
3754 if (tab[look].gt_entry.gt_g_value >= val)
3755 tab[look].gt_entry.gt_bytes += add;
3756
3757 if (tab[look].gt_entry.gt_g_value == val)
3758 exact = true;
3759 }
3760
3761 if (! exact)
3762 {
3763 Elf32_gptab *new_tab;
3764 unsigned int max;
3765
3766 /* We need a new table entry. */
3767 new_tab = ((Elf32_gptab *)
5b3b9ff6
ILT
3768 bfd_realloc ((PTR) tab,
3769 (c + 1) * sizeof (Elf32_gptab)));
aac6b32f
ILT
3770 if (new_tab == NULL)
3771 {
aac6b32f
ILT
3772 free (tab);
3773 return false;
3774 }
3775 tab = new_tab;
3776 tab[c].gt_entry.gt_g_value = val;
3777 tab[c].gt_entry.gt_bytes = add;
3778
3779 /* Merge in the size for the next smallest -G
3780 value, since that will be implied by this new
3781 value. */
3782 max = 0;
3783 for (look = 1; look < c; look++)
3784 {
3785 if (tab[look].gt_entry.gt_g_value < val
3786 && (max == 0
3787 || (tab[look].gt_entry.gt_g_value
3788 > tab[max].gt_entry.gt_g_value)))
3789 max = look;
3790 }
3791 if (max != 0)
3792 tab[c].gt_entry.gt_bytes +=
3793 tab[max].gt_entry.gt_bytes;
3794
3795 ++c;
3796 }
3797
3798 last = int_gptab.gt_entry.gt_bytes;
6e07e54f 3799 }
aac6b32f
ILT
3800
3801 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3802 elf_link_input_bfd ignores this section. */
3803 input_section->flags &=~ SEC_HAS_CONTENTS;
6e07e54f 3804 }
aac6b32f
ILT
3805
3806 /* The table must be sorted by -G value. */
3807 if (c > 2)
3808 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
3809
3810 /* Swap out the table. */
3811 ext_tab = ((Elf32_External_gptab *)
3812 bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
3813 if (ext_tab == NULL)
6e07e54f 3814 {
aac6b32f
ILT
3815 free (tab);
3816 return false;
3817 }
3818
3819 for (i = 0; i < c; i++)
3820 bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
3821 free (tab);
3822
3823 o->_raw_size = c * sizeof (Elf32_External_gptab);
3824 o->contents = (bfd_byte *) ext_tab;
3825
3826 /* Skip this section later on (I don't think this currently
3827 matters, but someday it might). */
3828 o->link_order_head = (struct bfd_link_order *) NULL;
3829 }
3830 }
3831
aac6b32f
ILT
3832 /* Invoke the regular ELF backend linker to do all the work. */
3833 if (! bfd_elf32_bfd_final_link (abfd, info))
3834 return false;
3835
3836 /* Now write out the computed sections. */
3837
6e07e54f
ILT
3838 if (reginfo_sec != (asection *) NULL)
3839 {
3840 Elf32_External_RegInfo ext;
3841
3842 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
3843 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
3844 (file_ptr) 0, sizeof ext))
3845 return false;
3846 }
b3c0fc57 3847
6e07e54f
ILT
3848 if (mdebug_sec != (asection *) NULL)
3849 {
aac6b32f 3850 BFD_ASSERT (abfd->output_has_begun);
9783e04a
DM
3851 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
3852 swap, info,
3853 mdebug_sec->filepos))
6e07e54f 3854 return false;
9783e04a
DM
3855
3856 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
b3c0fc57
ILT
3857 }
3858
aac6b32f 3859 if (gptab_data_sec != (asection *) NULL)
6e07e54f 3860 {
aac6b32f
ILT
3861 if (! bfd_set_section_contents (abfd, gptab_data_sec,
3862 gptab_data_sec->contents,
3863 (file_ptr) 0,
3864 gptab_data_sec->_raw_size))
3865 return false;
3866 }
3867
3868 if (gptab_bss_sec != (asection *) NULL)
3869 {
3870 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
3871 gptab_bss_sec->contents,
3872 (file_ptr) 0,
3873 gptab_bss_sec->_raw_size))
3874 return false;
6e07e54f
ILT
3875 }
3876
5b3b9ff6
ILT
3877 if (SGI_COMPAT (abfd))
3878 {
3879 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
3880 if (rtproc_sec != NULL)
3881 {
3882 if (! bfd_set_section_contents (abfd, rtproc_sec,
3883 rtproc_sec->contents,
3884 (file_ptr) 0,
3885 rtproc_sec->_raw_size))
3886 return false;
3887 }
3888 }
3889
6e07e54f 3890 return true;
b3c0fc57 3891}
6e07e54f 3892
aac6b32f 3893/* Handle a MIPS ELF HI16 reloc. */
b3c0fc57
ILT
3894
3895static void
aac6b32f
ILT
3896mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
3897 bfd *input_bfd;
3898 Elf_Internal_Rela *relhi;
3899 Elf_Internal_Rela *rello;
3900 bfd_byte *contents;
3901 bfd_vma addend;
b3c0fc57 3902{
aac6b32f
ILT
3903 bfd_vma insn;
3904 bfd_vma addlo;
b3c0fc57 3905
aac6b32f
ILT
3906 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
3907
3908 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
3909 addlo &= 0xffff;
3910
3911 addend += ((insn & 0xffff) << 16) + addlo;
3912
3913 if ((addlo & 0x8000) != 0)
3914 addend -= 0x10000;
3915 if ((addend & 0x8000) != 0)
3916 addend += 0x10000;
3917
3918 bfd_put_32 (input_bfd,
3919 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
3920 contents + relhi->r_offset);
3921}
3922
5b3b9ff6
ILT
3923/* Handle a MIPS ELF local GOT16 reloc. */
3924
3925static void
3926mips_elf_relocate_got_local (output_bfd, input_bfd, sgot, relhi, rello,
3927 contents, addend)
3928 bfd *output_bfd;
3929 bfd *input_bfd;
3930 asection *sgot;
3931 Elf_Internal_Rela *relhi;
3932 Elf_Internal_Rela *rello;
3933 bfd_byte *contents;
3934 bfd_vma addend;
3935{
3936 int local_gotno;
3937 int i;
3938 bfd_vma insn;
3939 bfd_vma addlo;
3940 bfd_vma address;
3941 bfd_vma hipage;
3942 bfd_byte *got_contents;
3943 struct mips_got_info *g;
3944
3945 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
3946
3947 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
3948 addlo &= 0xffff;
3949
3950 addend += ((insn & 0xffff) << 16) + addlo;
3951
3952 if ((addlo & 0x8000) != 0)
3953 addend -= 0x10000;
3954 if ((addend & 0x8000) != 0)
3955 addend += 0x10000;
3956
3957 /* Get a got entry representing requested hipage. */
3958 BFD_ASSERT (elf_section_data (sgot) != NULL);
3959 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
3960 BFD_ASSERT (g != NULL);
3961
3962 local_gotno = g->local_gotno;
3963 got_contents = sgot->contents;
3964 hipage = addend & 0xffff0000;
3965
3966 for (i = MIPS_RESERVED_GOTNO; i < local_gotno; i++)
3967 {
3968 address = bfd_get_32 (input_bfd, got_contents + i * 4);
3969 if (hipage == (address & 0xffff0000))
3970 break;
3971 if (address == (bfd_vma) 0)
3972 {
3973 bfd_put_32 (input_bfd, hipage, got_contents + i * 4);
3974 break;
3975 }
3976 }
3977
3978 BFD_ASSERT (i < local_gotno);
3979#if 1
3980 if (i == local_gotno)
3981 (*_bfd_error_handler)
3982 ("ELF MIPS linker: more got entries are needed for hipage: %x",
3983 hipage);
3984#endif
3985
3986 i = - ELF_MIPS_GP_OFFSET (output_bfd) + i * 4;
3987 bfd_put_32 (input_bfd, (insn & 0xffff0000) | (i & 0xffff),
3988 contents + relhi->r_offset);
3989}
3990
3991/* Handle MIPS ELF CALL16 reloc and global GOT16 reloc. */
3992
3993static void
3994mips_elf_relocate_global_got (input_bfd, rel, contents, offset)
3995 bfd *input_bfd;
3996 Elf_Internal_Rela *rel;
3997 bfd_byte *contents;
3998 bfd_vma offset;
3999{
4000 bfd_vma insn;
4001
4002 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
4003 bfd_put_32 (input_bfd,
4004 (insn & 0xffff0000) | (offset & 0xffff),
4005 contents + rel->r_offset);
4006}
4007
aac6b32f
ILT
4008/* Relocate a MIPS ELF section. */
4009
4010static boolean
4011mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
82b1edf7 4012 contents, relocs, local_syms, local_sections)
aac6b32f
ILT
4013 bfd *output_bfd;
4014 struct bfd_link_info *info;
4015 bfd *input_bfd;
4016 asection *input_section;
4017 bfd_byte *contents;
4018 Elf_Internal_Rela *relocs;
4019 Elf_Internal_Sym *local_syms;
4020 asection **local_sections;
4021{
4022 Elf_Internal_Shdr *symtab_hdr;
4023 size_t locsymcount;
4024 size_t extsymoff;
5b3b9ff6
ILT
4025 asection *sgot, *sreloc, *scpt;
4026 bfd *dynobj;
abec70b9 4027 bfd_vma gp;
aac6b32f
ILT
4028 Elf_Internal_Rela *rel;
4029 Elf_Internal_Rela *relend;
5b3b9ff6 4030 struct mips_got_info *g;
aac6b32f 4031
5b3b9ff6 4032 dynobj = elf_hash_table (info)->dynobj;
aac6b32f
ILT
4033 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4034
5b3b9ff6
ILT
4035 sgot = NULL;
4036 sreloc = NULL;
4037 if (dynobj == NULL || ! SGI_COMPAT (output_bfd))
4038 scpt = NULL;
4039 else
4040 scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
4041 g = NULL;
4042
aac6b32f 4043 if (elf_bad_symtab (input_bfd))
b3c0fc57 4044 {
aac6b32f
ILT
4045 locsymcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
4046 extsymoff = 0;
4047 }
4048 else
4049 {
4050 locsymcount = symtab_hdr->sh_info;
4051 extsymoff = symtab_hdr->sh_info;
4052 }
4053
abec70b9
ILT
4054 gp = _bfd_get_gp_value (output_bfd);
4055
aac6b32f
ILT
4056 rel = relocs;
4057 relend = relocs + input_section->reloc_count;
4058 for (; rel < relend; rel++)
4059 {
4060 int r_type;
82b1edf7 4061 reloc_howto_type *howto;
6ab826bd 4062 unsigned long r_symndx;
aac6b32f
ILT
4063 bfd_vma addend;
4064 struct elf_link_hash_entry *h;
4065 asection *sec;
4066 Elf_Internal_Sym *sym;
4067 bfd_reloc_status_type r;
4068
4069 r_type = ELF32_R_TYPE (rel->r_info);
4070 if (r_type < 0 || r_type >= (int) R_MIPS_max)
6e07e54f 4071 {
aac6b32f
ILT
4072 bfd_set_error (bfd_error_bad_value);
4073 return false;
6e07e54f 4074 }
aac6b32f 4075 howto = elf_mips_howto_table + r_type;
b3c0fc57 4076
5b3b9ff6
ILT
4077 if (dynobj != NULL
4078 && (r_type == R_MIPS_CALL16
4079 || r_type == R_MIPS_GOT16
4080 || r_type == R_MIPS_CALL_HI16
4081 || r_type == R_MIPS_CALL_LO16
4082 || r_type == R_MIPS_GOT_HI16
4083 || r_type == R_MIPS_GOT_LO16))
4084 {
4085 /* We need the .got section. */
4086 if (sgot == NULL)
4087 {
4088 sgot = bfd_get_section_by_name (dynobj, ".got");
4089 BFD_ASSERT (sgot != NULL);
4090 BFD_ASSERT (elf_section_data (sgot) != NULL);
4091 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4092 BFD_ASSERT (g != NULL);
4093 }
4094 }
4095
aac6b32f
ILT
4096 r_symndx = ELF32_R_SYM (rel->r_info);
4097
4098 /* Mix in the change in GP address for a GP relative reloc. */
4099 if (r_type != R_MIPS_GPREL16
4100 && r_type != R_MIPS_LITERAL
4101 && r_type != R_MIPS_GPREL32)
4102 addend = 0;
4103 else
b3c0fc57 4104 {
cba3f8a9 4105 if (gp == 0)
aac6b32f
ILT
4106 {
4107 if (! ((*info->callbacks->reloc_dangerous)
4108 (info,
4109 "GP relative relocation when GP not defined",
4110 input_bfd, input_section,
4111 rel->r_offset)))
4112 return false;
4113 /* Only give the error once per link. */
cba3f8a9
ILT
4114 gp = 4;
4115 _bfd_set_gp_value (output_bfd, gp);
aac6b32f
ILT
4116 }
4117
4118 if (r_symndx < extsymoff
4119 || (elf_bad_symtab (input_bfd)
4120 && local_sections[r_symndx] != NULL))
4121 {
4122 /* This is a relocation against a section. The current
4123 addend in the instruction is the difference between
4124 INPUT_SECTION->vma and the GP value of INPUT_BFD. We
4125 must change this to be the difference between the
4126 final definition (which will end up in RELOCATION)
4127 and the GP value of OUTPUT_BFD (which is in GP). */
cba3f8a9 4128 addend = elf_gp (input_bfd) - gp;
aac6b32f
ILT
4129 }
4130 else if (! info->relocateable)
4131 {
4132 /* We are doing a final link. The current addend in the
4133 instruction is simply the desired offset into the
4134 symbol (normally zero). We want the instruction to
4135 hold the difference between the final definition of
4136 the symbol (which will end up in RELOCATION) and the
4137 GP value of OUTPUT_BFD (which is in GP). */
cba3f8a9 4138 addend = - gp;
aac6b32f
ILT
4139 }
4140 else
4141 {
4142 /* We are generating relocateable output, and we aren't
4143 going to define this symbol, so we just leave the
4144 instruction alone. */
4145 addend = 0;
4146 }
b3c0fc57 4147 }
b3c0fc57 4148
aac6b32f
ILT
4149 h = NULL;
4150 sym = NULL;
4151 sec = NULL;
4152 if (info->relocateable)
4153 {
4154 /* This is a relocateable link. We don't have to change
4155 anything, unless the reloc is against a section symbol,
4156 in which case we have to adjust according to where the
4157 section symbol winds up in the output section. */
4158 if (r_symndx >= locsymcount
4159 || (elf_bad_symtab (input_bfd)
4160 && local_sections[r_symndx] == NULL))
4161 r = bfd_reloc_ok;
4162 else
4163 {
4164 sym = local_syms + r_symndx;
4165 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4166 r = bfd_reloc_ok;
4167 else
4168 {
4169 sec = local_sections[r_symndx];
4170
4171 /* It would be logical to add sym->st_value here,
4172 but Irix 5 sometimes generates a garbage symbol
4173 value. */
4174 addend += sec->output_offset;
4175
5b3b9ff6
ILT
4176 /* If this is HI16 or GOT16 with an associated LO16,
4177 adjust the addend accordingly. Otherwise, just
aac6b32f 4178 relocate. */
abec70b9 4179 if (r_type != R_MIPS_HI16 && r_type != R_MIPS_GOT16)
aac6b32f
ILT
4180 r = _bfd_relocate_contents (howto, input_bfd,
4181 addend,
4182 contents + rel->r_offset);
4183 else
4184 {
abec70b9
ILT
4185 Elf_Internal_Rela *lorel;
4186
4187 /* As a GNU extension, permit an arbitrary
4188 number of R_MIPS_HI16 relocs before the
4189 R_MIPS_LO16 reloc. This permits gcc to emit
4190 the HI and LO relocs itself. */
4191 if (r_type == R_MIPS_GOT16)
4192 lorel = rel + 1;
4193 else
4194 {
4195 for (lorel = rel + 1;
4196 (lorel < relend
4197 && (ELF32_R_TYPE (lorel->r_info)
4198 == R_MIPS_HI16));
4199 lorel++)
4200 ;
4201 }
4202 if (lorel < relend
4203 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
4204 {
4205 mips_elf_relocate_hi16 (input_bfd, rel, lorel,
4206 contents, addend);
4207 r = bfd_reloc_ok;
4208 }
4209 else
4210 r = _bfd_relocate_contents (howto, input_bfd,
4211 addend,
4212 contents + rel->r_offset);
aac6b32f
ILT
4213 }
4214 }
4215 }
4216 }
4217 else
4218 {
4219 bfd_vma relocation;
5b3b9ff6 4220 boolean local;
aac6b32f
ILT
4221
4222 /* This is a final link. */
4223 sym = NULL;
4224 if (r_symndx < extsymoff
4225 || (elf_bad_symtab (input_bfd)
4226 && local_sections[r_symndx] != NULL))
4227 {
5b3b9ff6 4228 local = true;
aac6b32f
ILT
4229 sym = local_syms + r_symndx;
4230 sec = local_sections[r_symndx];
4231 relocation = (sec->output_section->vma
4232 + sec->output_offset);
4233
4234 /* It would be logical to always add sym->st_value here,
4235 but Irix 5 sometimes generates a garbage symbol
4236 value. */
4237 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4238 relocation += sym->st_value;
4239 }
4240 else
4241 {
4242 long indx;
4243
5b3b9ff6 4244 local = false;
aac6b32f
ILT
4245 indx = r_symndx - extsymoff;
4246 h = elf_sym_hashes (input_bfd)[indx];
e9f03cd4
ILT
4247 while (h->root.type == bfd_link_hash_indirect
4248 || h->root.type == bfd_link_hash_warning)
4249 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5b3b9ff6
ILT
4250 if (strcmp (h->root.root.string, "_gp_disp") == 0)
4251 {
cba3f8a9 4252 if (gp == 0)
5b3b9ff6
ILT
4253 {
4254 if (! ((*info->callbacks->reloc_dangerous)
4255 (info,
4256 "_gp_disp used when GP not defined",
4257 input_bfd, input_section,
4258 rel->r_offset)))
4259 return false;
4260 /* Only give the error once per link. */
cba3f8a9
ILT
4261 gp = 4;
4262 _bfd_set_gp_value (output_bfd, gp);
5b3b9ff6
ILT
4263 relocation = 0;
4264 }
4265 else
4266 {
4267 sec = input_section;
4268 if (sec->output_section != NULL)
cba3f8a9 4269 relocation = (gp
5b3b9ff6
ILT
4270 - (rel->r_offset
4271 + sec->output_section->vma
4272 + sec->output_offset));
4273 else
cba3f8a9 4274 relocation = gp - rel->r_offset;
5b3b9ff6
ILT
4275 if (r_type == R_MIPS_LO16)
4276 relocation += 4;
4277 }
4278 }
4279 else if (h->root.type == bfd_link_hash_defined
a16fcab3 4280 || h->root.type == bfd_link_hash_defweak)
aac6b32f
ILT
4281 {
4282 sec = h->root.u.def.section;
5b3b9ff6
ILT
4283 if (sec->output_section == NULL)
4284 relocation = 0;
4285 else
4286 relocation = (h->root.u.def.value
4287 + sec->output_section->vma
4288 + sec->output_offset);
aac6b32f 4289 }
a16fcab3 4290 else if (h->root.type == bfd_link_hash_undefweak)
aac6b32f 4291 relocation = 0;
5b3b9ff6
ILT
4292 else if (info->shared && ! info->symbolic)
4293 relocation = 0;
53787b23
ILT
4294 else if (strcmp (h->root.root.string, "_DYNAMIC_LINK") == 0)
4295 {
4296 /* If this is a dynamic link, we should have created
4297 a _DYNAMIC_LINK symbol in
4298 mips_elf_create_dynamic_sections. Otherwise, we
4299 should define the symbol with a value of 0.
4300 FIXME: It should probably get into the symbol
4301 table somehow as well. */
4302 BFD_ASSERT (! info->shared);
4303 BFD_ASSERT (bfd_get_section_by_name (output_bfd,
4304 ".dynamic") == NULL);
4305 relocation = 0;
4306 }
aac6b32f
ILT
4307 else
4308 {
4309 if (! ((*info->callbacks->undefined_symbol)
4310 (info, h->root.root.string, input_bfd,
4311 input_section, rel->r_offset)))
4312 return false;
4313 relocation = 0;
4314 }
4315 }
4316
abec70b9 4317 if (r_type == R_MIPS_HI16)
aac6b32f 4318 {
abec70b9
ILT
4319 Elf_Internal_Rela *lorel;
4320
4321 /* As a GNU extension, permit an arbitrary number of
4322 R_MIPS_HI16 relocs before the R_MIPS_LO16 reloc.
4323 This permits gcc to emit the HI and LO relocs itself. */
4324 for (lorel = rel + 1;
4325 (lorel < relend
4326 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_HI16);
4327 lorel++)
4328 ;
4329 if (lorel < relend
4330 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
4331 {
4332 mips_elf_relocate_hi16 (input_bfd, rel, lorel,
4333 contents, relocation + addend);
4334 r = bfd_reloc_ok;
4335 }
4336 else
4337 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4338 contents, rel->r_offset,
4339 relocation, addend);
aac6b32f 4340 }
5b3b9ff6
ILT
4341 else if (r_type == R_MIPS_GOT16 && local)
4342 {
abec70b9 4343 /* GOT16 must also have an associated LO16 in the local
5b3b9ff6
ILT
4344 case. In this case, the addend is extracted and the
4345 section in which the referenced object is determined.
4346 Then the final address of the object is computed and
4347 the GOT entry for the hipage (an aligned 64kb chunk)
4348 is added to .got section if needed. The offset field
4349 of the GOT16-relocated instruction is replaced by the
4350 index of this GOT entry for the hipage. */
4351 if ((rel + 1) < relend
4352 && ELF32_R_TYPE ((rel + 1)->r_info) == R_MIPS_LO16)
4353 {
4354 mips_elf_relocate_got_local (output_bfd, input_bfd, sgot,
4355 rel, rel + 1,
4356 contents,
4357 relocation + addend);
4358 r = bfd_reloc_ok;
4359 }
4360 else
4361 r = bfd_reloc_outofrange;
4362 }
4363 else if (r_type == R_MIPS_CALL16
4364 || r_type == R_MIPS_GOT16
4365 || r_type == R_MIPS_CALL_LO16
4366 || r_type == R_MIPS_GOT_LO16)
4367 {
4368 bfd_vma offset;
4369
4370 /* This symbol must be registered as a global symbol
4371 having the corresponding got entry. */
4372 BFD_ASSERT (h->got_offset != (bfd_vma) -1);
4373
4374 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
4375 BFD_ASSERT (g->local_gotno <= offset
4376 && offset < sgot->_raw_size);
4377 bfd_put_32 (output_bfd, relocation + addend,
4378 sgot->contents + offset);
4379 offset = (sgot->output_section->vma + sgot->output_offset
cba3f8a9 4380 + offset - gp);
5b3b9ff6
ILT
4381 mips_elf_relocate_global_got (input_bfd, rel, contents,
4382 offset);
4383 r = bfd_reloc_ok;
4384 }
4385 else if (r_type == R_MIPS_CALL_HI16
4386 || r_type == R_MIPS_GOT_HI16)
4387 {
4388 bfd_vma offset;
4389
4390 /* This must be a global symbol with a got entry. The
4391 next reloc must be the corresponding LO16 reloc. */
4392 BFD_ASSERT (h != NULL && h->got_offset != (bfd_vma) -1);
4393 BFD_ASSERT ((rel + 1) < relend);
4394 BFD_ASSERT (ELF32_R_TYPE ((rel + 1)->r_info)
4395 == (r_type == R_MIPS_CALL_HI16
4396 ? R_MIPS_CALL_LO16
4397 : R_MIPS_GOT_LO16));
4398
4399 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
4400 BFD_ASSERT (g->local_gotno <= offset
4401 && offset < sgot->_raw_size);
4402 bfd_put_32 (output_bfd, relocation + addend,
4403 sgot->contents + offset);
4404 offset = (sgot->output_section->vma + sgot->output_offset
cba3f8a9 4405 + offset - gp);
5b3b9ff6
ILT
4406 mips_elf_relocate_hi16 (input_bfd, rel, rel + 1, contents,
4407 offset);
4408 r = bfd_reloc_ok;
4409 }
4410 else if (r_type == R_MIPS_REL32
4411 || r_type == R_MIPS_32)
4412 {
4413 Elf_Internal_Rel outrel;
4414 Elf32_crinfo cptrel;
4415 bfd_byte *cr;
4416
abec70b9 4417 if ((info->shared
9b292d4c
ILT
4418 || (elf_hash_table (info)->dynamic_sections_created
4419 && h != NULL
abec70b9
ILT
4420 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
4421 == 0)))
5b3b9ff6
ILT
4422 && (input_section->flags & SEC_ALLOC) != 0)
4423 {
4424 /* When generating a shared object, these
4425 relocations are copied into the output file to be
4426 resolved at run time. */
4427 if (sreloc == NULL)
4428 {
4429 sreloc = bfd_get_section_by_name (dynobj, ".rel.dyn");
4430 BFD_ASSERT (sreloc != NULL);
4431 }
4432
4433 outrel.r_offset = (rel->r_offset
4434 + input_section->output_section->vma
4435 + input_section->output_offset);
4436
4437 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
4438
53787b23
ILT
4439 if (h != NULL
4440 && (! info->symbolic
4441 || (h->elf_link_hash_flags
4442 & ELF_LINK_HASH_DEF_REGULAR) == 0))
5b3b9ff6
ILT
4443 {
4444 BFD_ASSERT (h->dynindx != -1);
4445 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_REL32);
4446 sec = input_section;
4447 }
4448 else
4449 {
4450 long indx;
4451
53787b23
ILT
4452 if (h == NULL)
4453 sec = local_sections[r_symndx];
4454 else
4455 {
4456 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4457 || (h->root.type
4458 == bfd_link_hash_defweak));
4459 sec = h->root.u.def.section;
4460 }
5b3b9ff6
ILT
4461 if (sec != NULL && bfd_is_abs_section (sec))
4462 indx = 0;
4463 else if (sec == NULL || sec->owner == NULL)
4464 {
4465 bfd_set_error (bfd_error_bad_value);
4466 return false;
4467 }
4468 else
4469 {
4470 asection *osec;
4471
4472 osec = sec->output_section;
4473 indx = elf_section_data (osec)->dynindx;
4474 if (indx == 0)
4475 abort ();
4476 }
4477
4478 outrel.r_info = ELF32_R_INFO (indx, R_MIPS_REL32);
4479 addend += relocation;
4480 }
4481
4482 bfd_put_32 (output_bfd, addend, contents + rel->r_offset);
4483 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
4484 (((Elf32_External_Rel *)
4485 sreloc->contents)
4486 + sreloc->reloc_count));
4487 ++sreloc->reloc_count;
4488
4489 if (SGI_COMPAT (output_bfd))
4490 {
4491 if (scpt == NULL)
4492 continue;
4493
4494 /* Make an entry of compact relocation info. */
4495 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
4496 cptrel.vaddr = (rel->r_offset
4497 + input_section->output_section->vma
4498 + input_section->output_offset);
4499 if (r_type == R_MIPS_REL32)
4500 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
4501 else
4502 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
53787b23 4503 mips_elf_set_cr_dist2to (cptrel, 0);
5b3b9ff6
ILT
4504 cptrel.konst = addend;
4505
4506 cr = (scpt->contents
4507 + sizeof (Elf32_External_compact_rel));
4508 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
4509 ((Elf32_External_crinfo *) cr
4510 + scpt->reloc_count));
4511 ++scpt->reloc_count;
4512 }
4513
4514 /* This reloc will be computed at runtime, so
4515 there's no need to do anything now. */
4516 continue;
4517 }
4518 else
4519 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4520 contents, rel->r_offset,
4521 relocation, addend);
4522 }
4523 else
4524 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4525 contents, rel->r_offset,
4526 relocation, addend);
4527
4528 if (SGI_COMPAT (abfd)
4529 && scpt != NULL
4530 && (input_section->flags & SEC_ALLOC) != 0)
4531 {
4532 Elf32_crinfo cptrel;
4533 bfd_byte *cr;
4534
4535 /* Make an entry of compact relocation info. */
4536 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
4537 cptrel.vaddr = (rel->r_offset
4538 + input_section->output_section->vma
4539 + input_section->output_offset);
4540
4541 switch (r_type)
4542 {
4543 case R_MIPS_26:
4544 mips_elf_set_cr_type (cptrel, CRT_MIPS_JMPAD);
53787b23
ILT
4545 /* XXX How should we set dist2to in this case. */
4546 mips_elf_set_cr_dist2to (cptrel, 8);
4547 cptrel.konst = addend + relocation;
5b3b9ff6
ILT
4548 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
4549 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
4550 ((Elf32_External_crinfo *) cr
4551 + scpt->reloc_count));
4552 ++scpt->reloc_count;
4553 break;
4554
4555 case R_MIPS_GPREL16:
4556 case R_MIPS_LITERAL:
4557 case R_MIPS_GPREL32:
4558 mips_elf_set_cr_type (cptrel, CRT_MIPS_GPHI_LO);
cba3f8a9 4559 cptrel.konst = gp - cptrel.vaddr;
53787b23 4560 mips_elf_set_cr_dist2to (cptrel, 4);
5b3b9ff6
ILT
4561 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
4562 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
4563 ((Elf32_External_crinfo *) cr
4564 + scpt->reloc_count));
4565 ++scpt->reloc_count;
4566 break;
4567
4568 default:
4569 break;
4570 }
4571 }
aac6b32f
ILT
4572 }
4573
4574 if (r != bfd_reloc_ok)
4575 {
4576 switch (r)
4577 {
4578 default:
4579 case bfd_reloc_outofrange:
4580 abort ();
4581 case bfd_reloc_overflow:
4582 {
4583 const char *name;
4584
4585 if (h != NULL)
4586 name = h->root.root.string;
4587 else
4588 {
ede4eed4
KR
4589 name = bfd_elf_string_from_elf_section (input_bfd,
4590 symtab_hdr->sh_link,
4591 sym->st_name);
aac6b32f
ILT
4592 if (name == NULL)
4593 return false;
4594 if (*name == '\0')
4595 name = bfd_section_name (input_bfd, sec);
4596 }
4597 if (! ((*info->callbacks->reloc_overflow)
4598 (info, name, howto->name, (bfd_vma) 0,
4599 input_bfd, input_section, rel->r_offset)))
4600 return false;
4601 }
4602 break;
4603 }
4604 }
b3c0fc57 4605 }
aac6b32f
ILT
4606
4607 return true;
b3c0fc57
ILT
4608}
4609\f
5b3b9ff6
ILT
4610/* Functions for the dynamic linker. */
4611
4612/* The name of the dynamic interpreter. This is put in the .interp
4613 section. */
4614
4615#define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
4616
4617/* Create dynamic sections when linking against a dynamic object. */
5b3b9ff6
ILT
4618
4619static boolean
4620mips_elf_create_dynamic_sections (abfd, info)
a16fcab3 4621 bfd *abfd;
5b3b9ff6 4622 struct bfd_link_info *info;
a16fcab3 4623{
5b3b9ff6
ILT
4624 struct elf_link_hash_entry *h;
4625 flagword flags;
4626 register asection *s;
4627 const char * const *namep;
a16fcab3 4628
5b3b9ff6
ILT
4629 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4630 | SEC_READONLY);
a16fcab3 4631
5b3b9ff6
ILT
4632 /* Mips ABI requests the .dynamic section to be read only. */
4633 s = bfd_get_section_by_name (abfd, ".dynamic");
4634 if (s != NULL)
4635 {
4636 if (! bfd_set_section_flags (abfd, s, flags))
4637 return false;
4638 }
a16fcab3 4639
5b3b9ff6
ILT
4640 /* We need to create .got section. */
4641 if (! mips_elf_create_got_section (abfd, info))
4642 return false;
4643
4644 /* Create .stub section. */
4645 if (bfd_get_section_by_name (abfd, ".stub") == NULL)
a16fcab3 4646 {
5b3b9ff6
ILT
4647 s = bfd_make_section (abfd, ".stub");
4648 if (s == NULL
4649 || ! bfd_set_section_flags (abfd, s, flags)
4650 || ! bfd_set_section_alignment (abfd, s, 2))
4651 return false;
4652 }
4653
4654 if (SGI_COMPAT (abfd))
4655 {
4656 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
4657 {
4658 h = NULL;
4659 if (! (_bfd_generic_link_add_one_symbol
4660 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
4661 (bfd_vma) 0, (const char *) NULL, false,
4662 get_elf_backend_data (abfd)->collect,
4663 (struct bfd_link_hash_entry **) &h)))
4664 return false;
e9f03cd4 4665 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5b3b9ff6
ILT
4666 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4667 h->type = STT_SECTION;
4668
53787b23 4669 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5b3b9ff6
ILT
4670 return false;
4671 }
4672
53787b23
ILT
4673 /* We need to create a .compact_rel section. */
4674 if (! mips_elf_create_compact_rel_section (abfd, info))
4675 return false;
5b3b9ff6
ILT
4676
4677 /* Change aligments of some sections. */
4678 s = bfd_get_section_by_name (abfd, ".hash");
4679 if (s != NULL)
4680 bfd_set_section_alignment (abfd, s, 4);
4681 s = bfd_get_section_by_name (abfd, ".dynsym");
4682 if (s != NULL)
4683 bfd_set_section_alignment (abfd, s, 4);
4684 s = bfd_get_section_by_name (abfd, ".dynstr");
4685 if (s != NULL)
4686 bfd_set_section_alignment (abfd, s, 4);
4687 s = bfd_get_section_by_name (abfd, ".reginfo");
4688 if (s != NULL)
4689 bfd_set_section_alignment (abfd, s, 4);
4690 s = bfd_get_section_by_name (abfd, ".dynamic");
4691 if (s != NULL)
4692 bfd_set_section_alignment (abfd, s, 4);
a16fcab3
KR
4693 }
4694
53787b23
ILT
4695 if (!info->shared)
4696 {
4697 h = NULL;
4698 if (! (_bfd_generic_link_add_one_symbol
4699 (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
4700 (bfd_vma) 0, (const char *) NULL, false,
4701 get_elf_backend_data (abfd)->collect,
4702 (struct bfd_link_hash_entry **) &h)))
4703 return false;
e9f03cd4 4704 h->elf_link_hash_flags ^=~ ELF_LINK_NON_ELF;
53787b23
ILT
4705 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4706 h->type = STT_SECTION;
4707
4708 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4709 return false;
4710 }
4711
5b3b9ff6
ILT
4712 return true;
4713}
4714
53787b23
ILT
4715/* Create the .compact_rel section. */
4716
4717static boolean
4718mips_elf_create_compact_rel_section (abfd, info)
4719 bfd *abfd;
4720 struct bfd_link_info *info;
4721{
4722 flagword flags;
4723 register asection *s;
4724
4725 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
4726 {
4727 flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_READONLY;
4728
4729 s = bfd_make_section (abfd, ".compact_rel");
4730 if (s == NULL
4731 || ! bfd_set_section_flags (abfd, s, flags)
4732 || ! bfd_set_section_alignment (abfd, s, 2))
4733 return false;
4734
4735 s->_raw_size = sizeof (Elf32_External_compact_rel);
4736 }
4737
4738 return true;
4739}
4740
5b3b9ff6
ILT
4741/* Create the .got section to hold the global offset table. */
4742
4743static boolean
4744mips_elf_create_got_section (abfd, info)
4745 bfd *abfd;
4746 struct bfd_link_info *info;
4747{
4748 flagword flags;
4749 register asection *s;
4750 struct elf_link_hash_entry *h;
4751 struct mips_got_info *g;
4752
4753 /* This function may be called more than once. */
4754 if (bfd_get_section_by_name (abfd, ".got") != NULL)
4755 return true;
4756
4757 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
4758
4759 s = bfd_make_section (abfd, ".got");
4760 if (s == NULL
4761 || ! bfd_set_section_flags (abfd, s, flags)
4762 || ! bfd_set_section_alignment (abfd, s, 4))
4763 return false;
4764
4765 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
4766 linker script because we don't want to define the symbol if we
53787b23 4767 are not creating a global offset table. */
5b3b9ff6
ILT
4768 h = NULL;
4769 if (! (_bfd_generic_link_add_one_symbol
4770 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
4771 (bfd_vma) 0, (const char *) NULL, false,
4772 get_elf_backend_data (abfd)->collect,
4773 (struct bfd_link_hash_entry **) &h)))
4774 return false;
e9f03cd4 4775 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5b3b9ff6
ILT
4776 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4777 h->type = STT_OBJECT;
4778
4779 if (info->shared
4780 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
4781 return false;
4782
4783 /* The first several global offset table entries are reserved. */
4784 s->_raw_size = MIPS_RESERVED_GOTNO * 4;
4785
4786 g = (struct mips_got_info *) bfd_alloc (abfd,
4787 sizeof (struct mips_got_info));
4788 if (g == NULL)
4789 return false;
4790 g->global_gotsym = 0;
4791 g->local_gotno = MIPS_RESERVED_GOTNO;
4792 if (elf_section_data (s) == NULL)
4793 {
4794 s->used_by_bfd =
4795 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
4796 if (elf_section_data (s) == NULL)
4797 return false;
4798 }
4799 elf_section_data (s)->tdata = (PTR) g;
4800
4801 return true;
4802}
4803
4804/* Look through the relocs for a section during the first phase, and
4805 allocate space in the global offset table. */
4806
4807static boolean
4808mips_elf_check_relocs (abfd, info, sec, relocs)
4809 bfd *abfd;
4810 struct bfd_link_info *info;
4811 asection *sec;
4812 const Elf_Internal_Rela *relocs;
4813{
4814 bfd *dynobj;
4815 Elf_Internal_Shdr *symtab_hdr;
4816 struct elf_link_hash_entry **sym_hashes;
4817 struct mips_got_info *g;
4818 size_t extsymoff;
4819 const Elf_Internal_Rela *rel;
4820 const Elf_Internal_Rela *rel_end;
4821 asection *sgot;
4822 asection *sreloc;
5b3b9ff6
ILT
4823
4824 if (info->relocateable)
4825 return true;
4826
4827 dynobj = elf_hash_table (info)->dynobj;
4828 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4829 sym_hashes = elf_sym_hashes (abfd);
4830 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
4831
4832 sgot = NULL;
4833 sreloc = NULL;
5b3b9ff6
ILT
4834
4835 rel_end = relocs + sec->reloc_count;
4836 for (rel = relocs; rel < rel_end; rel++)
4837 {
4838 unsigned long r_symndx;
4839 struct elf_link_hash_entry *h;
4840
4841 r_symndx = ELF32_R_SYM (rel->r_info);
4842
4843 if (r_symndx < extsymoff)
4844 h = NULL;
4845 else
4846 h = sym_hashes[r_symndx - extsymoff];
4847
4848 /* Some relocs require a global offset table. */
4849 if (dynobj == NULL)
4850 {
4851 switch (ELF32_R_TYPE (rel->r_info))
4852 {
4853 case R_MIPS_GOT16:
4854 case R_MIPS_CALL16:
4855 case R_MIPS_CALL_HI16:
4856 case R_MIPS_CALL_LO16:
4857 case R_MIPS_GOT_HI16:
4858 case R_MIPS_GOT_LO16:
4859 elf_hash_table (info)->dynobj = dynobj = abfd;
4860 if (! mips_elf_create_got_section (dynobj, info))
4861 return false;
4862 break;
4863
4864 default:
4865 break;
4866 }
4867 }
4868
4869 switch (ELF32_R_TYPE (rel->r_info))
4870 {
4871 case R_MIPS_CALL16:
4872 case R_MIPS_CALL_HI16:
4873 case R_MIPS_CALL_LO16:
4874 /* This symbol requires a global offset table entry. */
4875 if (sgot == NULL)
4876 {
4877 sgot = bfd_get_section_by_name (dynobj, ".got");
4878 BFD_ASSERT (sgot != NULL);
4879 BFD_ASSERT (elf_section_data (sgot) != NULL);
4880 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4881 BFD_ASSERT (g != NULL);
4882 }
4883
4884 BFD_ASSERT (h != NULL);
4885
4886 /* Make sure this symbol is output as a dynamic symbol. */
4887 if (h->dynindx == -1)
4888 {
4889 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4890 return false;
4891 }
4892
4893 if (h->got_offset != (bfd_vma) -1)
4894 {
4895 /* We have already allocated space in the .got. */
4896 break;
4897 }
4898
4899 /* Note the index of the first global got symbol in .dynsym. */
4900 if (g->global_gotsym == 0
4901 || g->global_gotsym > (unsigned long) h->dynindx)
4902 g->global_gotsym = h->dynindx;
4903
4904 /* Make this symbol to have the corresponding got entry. */
4905 h->got_offset = 0;
4906
4907 /* We need a stub, not a plt entry for the undefined
4908 function. But we record it as if it needs plt. See
4909 elf_adjust_dynamic_symbol in elflink.h. */
4910 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
4911 h->type = STT_FUNC;
4912
4913 break;
4914
4915 case R_MIPS_GOT16:
4916 case R_MIPS_GOT_HI16:
4917 case R_MIPS_GOT_LO16:
4918 /* This symbol requires a global offset table entry. */
4919 if (sgot == NULL)
4920 {
4921 sgot = bfd_get_section_by_name (dynobj, ".got");
4922 BFD_ASSERT (sgot != NULL);
4923 BFD_ASSERT (elf_section_data (sgot) != NULL);
4924 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4925 BFD_ASSERT (g != NULL);
4926 }
4927
4928 if (h != NULL)
4929 {
4930 /* Make sure this symbol is output as a dynamic symbol. */
4931 if (h->dynindx == -1)
4932 {
4933 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4934 return false;
4935 }
4936
4937 if (h->got_offset != (bfd_vma) -1)
4938 {
4939 /* We have already allocated space in the .got. */
4940 break;
4941 }
4942 /* Note the index of the first global got symbol in
4943 .dynsym. */
4944 if (g->global_gotsym == 0
4945 || g->global_gotsym > (unsigned long) h->dynindx)
4946 g->global_gotsym = h->dynindx;
4947
4948 /* Make this symbol to be the global got symbol. */
4949 h->got_offset = 0;
4950 }
4951
4952 break;
4953
4954 case R_MIPS_32:
4955 case R_MIPS_REL32:
abec70b9 4956 if ((info->shared || h != NULL)
5b3b9ff6
ILT
4957 && (sec->flags & SEC_ALLOC) != 0)
4958 {
9b292d4c 4959 if (info->shared)
5b3b9ff6 4960 {
9b292d4c
ILT
4961 /* When creating a shared object, we must copy these
4962 reloc types into the output file as R_MIPS_REL32
4963 relocs. We create the .rel.dyn reloc section in
4964 dynobj and make room for this reloc. */
5b3b9ff6
ILT
4965 if (sreloc == NULL)
4966 {
9b292d4c 4967 const char *name = ".rel.dyn";
5b3b9ff6 4968
9b292d4c
ILT
4969 sreloc = bfd_get_section_by_name (dynobj, name);
4970 if (sreloc == NULL)
4971 {
4972 sreloc = bfd_make_section (dynobj, name);
4973 if (sreloc == NULL
4974 || ! bfd_set_section_flags (dynobj, sreloc,
4975 (SEC_ALLOC
4976 | SEC_LOAD
4977 | SEC_HAS_CONTENTS
4978 | SEC_IN_MEMORY
4979 | SEC_READONLY))
4980 || ! bfd_set_section_alignment (dynobj, sreloc,
4981 4))
4982 return false;
4983
4984 /* Add a null element. */
4985 sreloc->_raw_size += sizeof (Elf32_External_Rel);
4986 ++sreloc->reloc_count;
4987 }
5b3b9ff6 4988 }
5b3b9ff6 4989
9b292d4c
ILT
4990 sreloc->_raw_size += sizeof (Elf32_External_Rel);
4991 }
abec70b9
ILT
4992 else
4993 {
4994 struct mips_elf_link_hash_entry *hmips;
4995
4996 /* We only need to copy this reloc if the symbol is
4997 defined in a dynamic object. */
4998 hmips = (struct mips_elf_link_hash_entry *) h;
4999 ++hmips->mips_32_relocs;
5000 }
5b3b9ff6
ILT
5001 }
5002
53787b23
ILT
5003 if (SGI_COMPAT (abfd))
5004 mips_elf_hash_table (info)->compact_rel_size +=
5005 sizeof (Elf32_External_crinfo);
5006
5b3b9ff6
ILT
5007 break;
5008
5009 case R_MIPS_26:
5010 case R_MIPS_GPREL16:
5011 case R_MIPS_LITERAL:
5012 case R_MIPS_GPREL32:
53787b23
ILT
5013 if (SGI_COMPAT (abfd))
5014 mips_elf_hash_table (info)->compact_rel_size +=
5015 sizeof (Elf32_External_crinfo);
5b3b9ff6
ILT
5016 break;
5017
5018 default:
5019 break;
5020 }
5021 }
5022
5023 return true;
5024}
5025
5026/* Adjust a symbol defined by a dynamic object and referenced by a
5027 regular object. The current definition is in some section of the
5028 dynamic object, but we're not including those sections. We have to
5029 change the definition to something the rest of the link can
5030 understand. */
5031
5032static boolean
5033mips_elf_adjust_dynamic_symbol (info, h)
5034 struct bfd_link_info *info;
5035 struct elf_link_hash_entry *h;
5036{
5037 bfd *dynobj;
abec70b9 5038 struct mips_elf_link_hash_entry *hmips;
5b3b9ff6
ILT
5039 asection *s;
5040
5041 dynobj = elf_hash_table (info)->dynobj;
5042
5043 /* Make sure we know what is going on here. */
5044 BFD_ASSERT (dynobj != NULL
5045 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
5046 || h->weakdef != NULL
5047 || ((h->elf_link_hash_flags
5048 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5049 && (h->elf_link_hash_flags
5050 & ELF_LINK_HASH_REF_REGULAR) != 0
5051 && (h->elf_link_hash_flags
5052 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
5053
abec70b9
ILT
5054 /* If this symbol is defined in a dynamic object, we need to copy
5055 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
5056 file. */
5057 hmips = (struct mips_elf_link_hash_entry *) h;
5058 if (! info->relocateable
5059 && hmips->mips_32_relocs != 0
5060 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
5061 {
5062 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
5063 BFD_ASSERT (s != NULL);
5064
5065 s->_raw_size += hmips->mips_32_relocs * sizeof (Elf32_External_Rel);
5066 }
5067
5b3b9ff6
ILT
5068 /* For a function, create a stub, if needed. */
5069 if (h->type == STT_FUNC
5070 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
5071 {
5072 if (! elf_hash_table (info)->dynamic_sections_created)
5073 return true;
5074
5075 /* If this symbol is not defined in a regular file, then set
5076 the symbol to the stub location. This is required to make
5077 function pointers compare as equal between the normal
5078 executable and the shared library. */
5079 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
5080 {
5081 /* We need .stub section. */
5082 s = bfd_get_section_by_name (dynobj, ".stub");
5083 BFD_ASSERT (s != NULL);
5084
5085 h->root.u.def.section = s;
5086 h->root.u.def.value = s->_raw_size;
5087
5088 /* XXX Write this stub address somewhere. */
5089 h->plt_offset = s->_raw_size;
5090
5091 /* Make room for this stub code. */
5092 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
5093
5094 /* The last half word of the stub will be filled with the index
5095 of this symbol in .dynsym section. */
5096 return true;
5097 }
5098 }
5099
5100 /* If this is a weak symbol, and there is a real definition, the
5101 processor independent code will have arranged for us to see the
5102 real definition first, and we can just use the same value. */
5103 if (h->weakdef != NULL)
5104 {
5105 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
5106 || h->weakdef->root.type == bfd_link_hash_defweak);
5107 h->root.u.def.section = h->weakdef->root.u.def.section;
5108 h->root.u.def.value = h->weakdef->root.u.def.value;
5109 return true;
5110 }
5111
5112 /* This is a reference to a symbol defined by a dynamic object which
5113 is not a function. */
5114
5115 return true;
5116}
5117
5118/* Set the sizes of the dynamic sections. */
5119
5120static boolean
5121mips_elf_size_dynamic_sections (output_bfd, info)
5122 bfd *output_bfd;
5123 struct bfd_link_info *info;
5124{
5125 bfd *dynobj;
5126 asection *s;
5127 boolean reltext;
5128 asection *sgot;
5129 struct mips_got_info *g;
5130
5131 dynobj = elf_hash_table (info)->dynobj;
5132 BFD_ASSERT (dynobj != NULL);
5133
5134 if (elf_hash_table (info)->dynamic_sections_created)
5135 {
5136 /* Set the contents of the .interp section to the interpreter. */
5137 if (! info->shared)
5138 {
5139 s = bfd_get_section_by_name (dynobj, ".interp");
5140 BFD_ASSERT (s != NULL);
5141 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
5142 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5143 }
5144 }
5145
5146 /* Recompute the size of .got for local entires (reserved and
5147 hipages) if needed. To estimate it, get the upper bound of total
5148 size of loadable sections. */
5149 sgot = bfd_get_section_by_name (dynobj, ".got");
5150
5151 if (sgot != NULL)
5152 {
5153 bfd_size_type loadable_size = 0;
5154 bfd_size_type local_gotno;
5155 struct _bfd *sub;
5156
5157 BFD_ASSERT (elf_section_data (sgot) != NULL);
5158 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5159 BFD_ASSERT (g != NULL);
5160
5161 for (sub = info->input_bfds; sub; sub = sub->link_next)
5162 for (s = sub->sections; s != NULL; s = s->next)
5163 {
5164 if ((s->flags & SEC_ALLOC) == 0)
5165 continue;
5166 loadable_size += (s->_raw_size + 0xf) & ~0xf;
5167 }
5168
5169 loadable_size += MIPS_FUNCTION_STUB_SIZE;
5170
5171 /* Assume there are two loadable segments consisting of
5172 contiguous sections. Is 5 enough? */
5173 local_gotno = (loadable_size >> 16) + 5 + MIPS_RESERVED_GOTNO;
5174 g->local_gotno = local_gotno;
5175 sgot->_raw_size += local_gotno * 4;
5176 }
5177
5178 /* The check_relocs and adjust_dynamic_symbol entry points have
5179 determined the sizes of the various dynamic sections. Allocate
5180 memory for them. */
5181 reltext = false;
5182 for (s = dynobj->sections; s != NULL; s = s->next)
5183 {
5184 const char *name;
5185 boolean strip;
5186
5187 /* It's OK to base decisions on the section name, because none
5188 of the dynobj section names depend upon the input files. */
5189 name = bfd_get_section_name (dynobj, s);
5190
5191 if ((s->flags & SEC_IN_MEMORY) == 0)
5192 continue;
5193
5194 strip = false;
5195
5196 if (strncmp (name, ".rel", 4) == 0)
5197 {
5198 if (s->_raw_size == 0)
5199 strip = true;
5200 else
5201 {
5202 asection *target;
5203
5204 /* If this relocation section applies to a read only
5205 section, then we probably need a DT_TEXTREL entry.
5206 If the relocation section is .rel.dyn, we always
5207 assert a DT_TEXTREL entry rather than testing whether
5208 there exists a relocation to a read only section or
5209 not. */
5210 target = bfd_get_section_by_name (output_bfd, name + 4);
5211 if ((target != NULL && (target->flags & SEC_READONLY) != 0)
5212 || strcmp (name, ".rel.dyn") == 0)
5213 reltext = true;
5214
5215 /* We use the reloc_count field as a counter if we need
5216 to copy relocs into the output file. */
5217 if (strcmp (name, ".rel.dyn") != 0)
5218 s->reloc_count = 0;
5219 }
5220 }
5221 else if (strncmp (name, ".got", 4) == 0)
5222 {
5223 int i;
5224
5225 BFD_ASSERT (elf_section_data (s) != NULL);
5226 g = (struct mips_got_info *) elf_section_data (s)->tdata;
5227 BFD_ASSERT (g != NULL);
5228
5229 /* Fix the size of .got section for the correspondence of
5230 global symbols and got entries. This adds some useless
5231 got entries. Is this required by ABI really? */
5232 i = elf_hash_table (info)->dynsymcount - g->global_gotsym;
5233 s->_raw_size += i * 4;
5234 }
5235 else if (strncmp (name, ".stub", 5) == 0)
5236 {
5237 /* Irix rld assumes that the function stub isn't at the end
5238 of .text section. So put a dummy. XXX */
5239 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
5240 }
53787b23
ILT
5241 else if (SGI_COMPAT (output_bfd)
5242 && strncmp (name, ".compact_rel", 12) == 0)
5243 s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
5244 else if (strncmp (name, ".init", 5) != 0)
5b3b9ff6
ILT
5245 {
5246 /* It's not one of our sections, so don't allocate space. */
5247 continue;
5248 }
5249
5250 if (strip)
5251 {
5252 asection **spp;
5253
5254 for (spp = &s->output_section->owner->sections;
5255 *spp != s->output_section;
5256 spp = &(*spp)->next)
5257 ;
5258 *spp = s->output_section->next;
5259 --s->output_section->owner->section_count;
5260
5261 continue;
5262 }
5263
5264 /* Allocate memory for the section contents. */
5265 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
5266 if (s->contents == NULL && s->_raw_size != 0)
5267 {
5268 bfd_set_error (bfd_error_no_memory);
5269 return false;
5270 }
5271 memset (s->contents, 0, s->_raw_size);
5272 }
5273
5274 if (elf_hash_table (info)->dynamic_sections_created)
5275 {
5276 /* Add some entries to the .dynamic section. We fill in the
5277 values later, in elf_mips_finish_dynamic_sections, but we
5278 must add the entries now so that we get the correct size for
5279 the .dynamic section. The DT_DEBUG entry is filled in by the
5280 dynamic linker and used by the debugger. */
5281 if (! info->shared)
5282 {
5283 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
5284 return false;
5285 }
5286
5287 if (reltext)
5288 {
5289 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
5290 return false;
5291 }
5292
5293 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
5294 return false;
5295
5296 if (bfd_get_section_by_name (dynobj, ".rel.dyn"))
5297 {
5298 if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0))
5299 return false;
5300
5301 if (! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0))
5302 return false;
5303
5304 if (! bfd_elf32_add_dynamic_entry (info, DT_RELENT, 0))
5305 return false;
5306 }
5307
5308 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICTNO, 0))
5309 return false;
5310
5311 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLISTNO, 0))
5312 return false;
5313
53787b23 5314 if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
5b3b9ff6
ILT
5315 {
5316 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICT, 0))
5317 return false;
5318
5319 s = bfd_get_section_by_name (dynobj, ".liblist");
5320 BFD_ASSERT (s != NULL);
5321
5322 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLIST, 0))
5323 return false;
5324 }
5325
5326 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_VERSION, 0))
5327 return false;
5328
5329 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_FLAGS, 0))
5330 return false;
5331
53787b23
ILT
5332#if 0
5333 /* Time stamps in executable files are a bad idea. */
5b3b9ff6
ILT
5334 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_TIME_STAMP, 0))
5335 return false;
53787b23 5336#endif
5b3b9ff6
ILT
5337
5338#if 0 /* FIXME */
5339 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_ICHECKSUM, 0))
5340 return false;
5341#endif
5342
5343#if 0 /* FIXME */
5344 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_IVERSION, 0))
5345 return false;
5346#endif
5347
5348 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_BASE_ADDRESS, 0))
5349 return false;
5350
5351 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LOCAL_GOTNO, 0))
5352 return false;
5353
5354 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_SYMTABNO, 0))
5355 return false;
5356
5357 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_UNREFEXTNO, 0))
5358 return false;
5359
5360 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_GOTSYM, 0))
5361 return false;
5362
5363 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_HIPAGENO, 0))
5364 return false;
5365
5366#if 0 /* (SGI_COMPAT) */
5367 if (! bfd_get_section_by_name (dynobj, ".init"))
5368 if (! bfd_elf32_add_dynamic_entry (info, DT_INIT, 0))
5369 return false;
5370
5371 if (! bfd_get_section_by_name (dynobj, ".fini"))
5372 if (! bfd_elf32_add_dynamic_entry (info, DT_FINI, 0))
5373 return false;
5374#endif
5375 }
5376
53787b23
ILT
5377 /* If we use dynamic linking, we generate a section symbol for each
5378 output section. These are local symbols, which means that they
5379 must come first in the dynamic symbol table.
5b3b9ff6
ILT
5380 That means we must increment the dynamic symbol index of every
5381 other dynamic symbol. */
53787b23
ILT
5382 {
5383 const char * const *namep;
5384 unsigned int c, i;
5385 bfd_size_type strindex;
5386 struct bfd_strtab_hash *dynstr;
5387 struct mips_got_info *g;
5388
9b292d4c 5389 c = 0;
53787b23
ILT
5390 if (elf_hash_table (info)->dynamic_sections_created)
5391 {
5392 if (SGI_COMPAT (output_bfd))
5393 {
5394 c = SIZEOF_MIPS_DYNSYM_SECNAMES - 1;
5395 elf_link_hash_traverse (elf_hash_table (info),
5396 mips_elf_adjust_dynindx,
5397 (PTR) &c);
5398 elf_hash_table (info)->dynsymcount += c;
5399
5400 dynstr = elf_hash_table (info)->dynstr;
5401 BFD_ASSERT (dynstr != NULL);
5402
5403 for (i = 1, namep = mips_elf_dynsym_sec_names;
5404 *namep != NULL;
5405 i++, namep++)
5406 {
5407 s = bfd_get_section_by_name (output_bfd, *namep);
5408 if (s != NULL)
5409 elf_section_data (s)->dynindx = i;
5b3b9ff6 5410
53787b23
ILT
5411 strindex = _bfd_stringtab_add (dynstr, *namep, true, false);
5412 if (strindex == (bfd_size_type) -1)
5413 return false;
5b3b9ff6 5414
53787b23
ILT
5415 mips_elf_hash_table (info)->dynsym_sec_strindex[i] = strindex;
5416 }
5417 }
5418 else
5419 {
5420 c = bfd_count_sections (output_bfd);
5421 elf_link_hash_traverse (elf_hash_table (info),
5422 mips_elf_adjust_dynindx,
5423 (PTR) &c);
5424 elf_hash_table (info)->dynsymcount += c;
5b3b9ff6 5425
53787b23
ILT
5426 for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++)
5427 {
5428 elf_section_data (s)->dynindx = i;
5429 /* These symbols will have no names, so we don't need to
5430 fiddle with dynstr_index. */
5431 }
5432 }
5433 }
5b3b9ff6 5434
53787b23
ILT
5435 s = bfd_get_section_by_name (dynobj, ".got");
5436 BFD_ASSERT (s != NULL);
5437 BFD_ASSERT (elf_section_data (s) != NULL);
5438 g = (struct mips_got_info *) elf_section_data (s)->tdata;
5439 BFD_ASSERT (g != NULL);
5b3b9ff6 5440
53787b23
ILT
5441 /* If there are no global got symbols, fake the last symbol so for
5442 safety. */
5443 if (g->global_gotsym)
5444 g->global_gotsym += c;
5445 else
5446 g->global_gotsym = elf_hash_table (info)->dynsymcount - 1;
5447 }
5b3b9ff6
ILT
5448
5449 return true;
5450}
5451
5452/* Increment the index of a dynamic symbol by a given amount. Called
5453 via elf_link_hash_traverse. */
5454
5455static boolean
5456mips_elf_adjust_dynindx (h, cparg)
5457 struct elf_link_hash_entry *h;
5458 PTR cparg;
5459{
53787b23 5460 unsigned int *cp = (unsigned int *) cparg;
5b3b9ff6
ILT
5461
5462 if (h->dynindx != -1)
5463 h->dynindx += *cp;
5464 return true;
5465}
5466
5467/* Finish up dynamic symbol handling. We set the contents of various
5468 dynamic sections here. */
5469
5470static boolean
5471mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
5472 bfd *output_bfd;
5473 struct bfd_link_info *info;
5474 struct elf_link_hash_entry *h;
5475 Elf_Internal_Sym *sym;
5476{
5477 bfd *dynobj;
5478 bfd_vma gval;
5479 asection *sgot;
5480 struct mips_got_info *g;
5481 const char *name;
5482
5483 dynobj = elf_hash_table (info)->dynobj;
5484 gval = sym->st_value;
5485
5486 if (h->plt_offset != (bfd_vma) -1)
5487 {
5488 asection *s;
5489 bfd_byte *p;
5490 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
5491
5492 /* This symbol has a stub. Set it up. */
5493
5494 BFD_ASSERT (h->dynindx != -1);
5495
5496 s = bfd_get_section_by_name (dynobj, ".stub");
5497 BFD_ASSERT (s != NULL);
5498
5499 /* Fill the stub. */
5500 p = stub;
5501 bfd_put_32 (output_bfd, STUB_LW(output_bfd), p);
5502 p += 4;
5503 bfd_put_32 (output_bfd, STUB_MOVE, p);
5504 p += 4;
5505
5506 /* FIXME: Can h->dynindex be more than 64K? */
5507 if (h->dynindx & 0xffff0000)
5508 return false;
5509
5510 bfd_put_32 (output_bfd, STUB_JALR, p);
5511 p += 4;
5512 bfd_put_32 (output_bfd, STUB_LI16 + h->dynindx, p);
5513
5514 BFD_ASSERT (h->plt_offset <= s->_raw_size);
5515 memcpy (s->contents + h->plt_offset, stub, MIPS_FUNCTION_STUB_SIZE);
5516
5517 /* Mark the symbol as undefined. plt_offset != -1 occurs
5518 only for the referenced symbol. */
5519 sym->st_shndx = SHN_UNDEF;
5520
5521 /* The run-time linker uses the st_value field of the symbol
5522 to reset the global offset table entry for this external
5523 to its stub address when unlinking a shared object. */
5524 gval = s->output_section->vma + s->output_offset + h->plt_offset;
5525 sym->st_value = gval;
5526 }
5527
5528 BFD_ASSERT (h->dynindx != -1);
5529
5530 sgot = bfd_get_section_by_name (dynobj, ".got");
5531 BFD_ASSERT (sgot != NULL);
5532 BFD_ASSERT (elf_section_data (sgot) != NULL);
5533 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5534 BFD_ASSERT (g != NULL);
5535
5536 if ((unsigned long) h->dynindx >= g->global_gotsym)
5537 {
5538 bfd_size_type offset;
5539
5540 /* This symbol has an entry in the global offset table. Set its
5541 value to the corresponding got entry, if needed. */
5542 if (h->got_offset == (bfd_vma) -1)
5543 {
5544 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
5545 BFD_ASSERT (g->local_gotno * 4 <= offset
5546 && offset < sgot->_raw_size);
5547 bfd_put_32 (output_bfd, gval, sgot->contents + offset);
5548 }
5549 }
5550
5551 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
5552 name = h->root.root.string;
5553 if (strcmp (name, "_DYNAMIC") == 0
5554 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
5555 sym->st_shndx = SHN_ABS;
53787b23
ILT
5556 else if (strcmp (name, "_DYNAMIC_LINK") == 0)
5557 {
5558 sym->st_shndx = SHN_ABS;
5559 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5560 sym->st_value = 1;
5561 }
5562 else if (SGI_COMPAT (output_bfd))
5b3b9ff6
ILT
5563 {
5564 if (strcmp (name, "_gp_disp") == 0)
5565 {
5566 sym->st_shndx = SHN_ABS;
5567 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
abec70b9 5568 sym->st_value = elf_gp (output_bfd);
5b3b9ff6
ILT
5569 }
5570 else if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
5571 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
5572 {
5573 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5574 sym->st_other = STO_PROTECTED;
5575 sym->st_value = 0;
5576 sym->st_shndx = SHN_MIPS_DATA;
5577 }
5578 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
5579 {
5580 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5581 sym->st_other = STO_PROTECTED;
5582 sym->st_value = mips_elf_hash_table (info)->procedure_count;
5583 sym->st_shndx = SHN_ABS;
5584 }
53787b23 5585 else if (sym->st_shndx != SHN_UNDEF)
5b3b9ff6
ILT
5586 {
5587 if (h->type == STT_FUNC)
5588 sym->st_shndx = SHN_MIPS_TEXT;
5589 else if (h->type == STT_OBJECT)
5590 sym->st_shndx = SHN_MIPS_DATA;
5591 }
5592 }
5593
5594 return true;
5595}
5596
5597/* Finish up the dynamic sections. */
5598
5599static boolean
5600mips_elf_finish_dynamic_sections (output_bfd, info)
5601 bfd *output_bfd;
5602 struct bfd_link_info *info;
5603{
5604 bfd *dynobj;
5605 asection *sdyn;
5606 asection *sgot;
5607 struct mips_got_info *g;
5608
5609 dynobj = elf_hash_table (info)->dynobj;
5610
5611 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5612
5613 sgot = bfd_get_section_by_name (dynobj, ".got");
5614 BFD_ASSERT (sgot != NULL);
5615
5616 BFD_ASSERT (elf_section_data (sgot) != NULL);
5617 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5618 BFD_ASSERT (g != NULL);
5619
5620 if (elf_hash_table (info)->dynamic_sections_created)
5621 {
5622 Elf32_External_Dyn *dyncon, *dynconend;
5623
5624 BFD_ASSERT (sdyn != NULL);
5625
5626 dyncon = (Elf32_External_Dyn *) sdyn->contents;
5627 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
5628 for (; dyncon < dynconend; dyncon++)
5629 {
5630 Elf_Internal_Dyn dyn;
5631 const char *name;
5632 size_t elemsize;
5633 asection *s;
5634
5635 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5636
5637 switch (dyn.d_tag)
5638 {
5639 default:
5640 break;
5641
5642 case DT_RELENT:
5643 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
5644 BFD_ASSERT (s != NULL);
5645 dyn.d_un.d_val = sizeof (Elf32_External_Rel);
5646 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5647 break;
5648
5649 case DT_STRSZ:
5650 /* Rewrite DT_STRSZ. */
5651 dyn.d_un.d_val =
5652 _bfd_stringtab_size (elf_hash_table (info)->dynstr);
5653 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5654 break;
5655
5656 case DT_PLTGOT:
5657 name = ".got";
5658 goto get_vma;
5659 case DT_MIPS_CONFLICT:
5660 name = ".conflict";
5661 goto get_vma;
5662 case DT_MIPS_LIBLIST:
5663 name = ".liblist";
5664 get_vma:
5665 s = bfd_get_section_by_name (output_bfd, name);
5666 BFD_ASSERT (s != NULL);
5667 dyn.d_un.d_ptr = s->vma;
5668 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5669 break;
5670
5671 case DT_MIPS_RLD_VERSION:
5672 dyn.d_un.d_val = 1; /* XXX */
5673 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5674 break;
5675
5676 case DT_MIPS_FLAGS:
5677 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
5678 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5679 break;
5680
5681 case DT_MIPS_CONFLICTNO:
5682 name = ".conflict";
5683 elemsize = sizeof (Elf32_Conflict);
5684 goto set_elemno;
5685
5686 case DT_MIPS_LIBLISTNO:
5687 name = ".liblist";
5688 elemsize = sizeof (Elf32_Lib);
5689 set_elemno:
5690 s = bfd_get_section_by_name (output_bfd, name);
5691 if (s != NULL)
5692 {
5693 if (s->_cooked_size != 0)
5694 dyn.d_un.d_val = s->_cooked_size / elemsize;
5695 else
5696 dyn.d_un.d_val = s->_raw_size / elemsize;
5697 }
5698 else
5699 dyn.d_un.d_val = 0;
5700
5701 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5702 break;
5703
5704 case DT_MIPS_TIME_STAMP:
5705 time ((time_t *) &dyn.d_un.d_val);
5706 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5707 break;
5708
5709 case DT_MIPS_ICHECKSUM:
5710 /* XXX FIXME: */
5711 break;
5712
5713 case DT_MIPS_IVERSION:
5714 /* XXX FIXME: */
5715 break;
5716
5717 case DT_MIPS_BASE_ADDRESS:
5718 s = output_bfd->sections;
5719 BFD_ASSERT (s != NULL);
5720 dyn.d_un.d_ptr = s->vma & ~(0xffff);
5721 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5722 break;
5723
5724 case DT_MIPS_LOCAL_GOTNO:
5725 dyn.d_un.d_val = g->local_gotno;
5726 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5727 break;
5728
5729 case DT_MIPS_SYMTABNO:
5730 name = ".dynsym";
5731 elemsize = sizeof (Elf32_External_Sym);
5732 s = bfd_get_section_by_name (output_bfd, name);
5733 BFD_ASSERT (s != NULL);
5734
5735 if (s->_cooked_size != 0)
5736 dyn.d_un.d_val = s->_cooked_size / elemsize;
5737 else
5738 dyn.d_un.d_val = s->_raw_size / elemsize;
5739 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5740 break;
5741
5742 case DT_MIPS_UNREFEXTNO:
5743 /* XXX FIXME: */
5744 dyn.d_un.d_val = SIZEOF_MIPS_DYNSYM_SECNAMES;
5745 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5746 break;
5747
5748 case DT_MIPS_GOTSYM:
5749 dyn.d_un.d_val = g->global_gotsym;
5750 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5751 break;
5752
5753 case DT_MIPS_HIPAGENO:
5754 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
5755 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5756 break;
5757
5758 }
5759 }
5760 }
5761
5762 /* The first entry of the global offset table will be filled at
5763 runtime. The second entry will be used by some runtime loaders.
5764 This isn't the case of Irix rld. */
5765 if (sgot->_raw_size > 0)
5766 {
5767 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
5768 bfd_put_32 (output_bfd, (bfd_vma) 0x80000000, sgot->contents + 4);
5769 }
5770
5771 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
5772
53787b23
ILT
5773 {
5774 asection *sdynsym;
5775 asection *s;
5776 unsigned int i;
5777 bfd_vma last;
5778 Elf_Internal_Sym sym;
5779 long dindx;
5780 const char *name;
5781 const char * const * namep = mips_elf_dynsym_sec_names;
5782 Elf32_compact_rel cpt;
5783
5784 /* Set up the section symbols for the output sections. SGI sets
5785 the STT_NOTYPE attribute for these symbols. Should we do so? */
5786
5787 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
5788 if (sdynsym != NULL)
5789 {
5790 if (SGI_COMPAT (output_bfd))
5791 {
5792 sym.st_size = 0;
5793 sym.st_name = 0;
5794 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
5795 sym.st_other = 0;
5b3b9ff6 5796
53787b23
ILT
5797 i = 0;
5798 while ((name = *namep++) != NULL)
5799 {
5800 s = bfd_get_section_by_name (output_bfd, name);
5801 if (s != NULL)
5802 {
5803 sym.st_value = s->vma;
5804 dindx = elf_section_data (s)->dynindx;
5805 last = s->vma + s->_raw_size;
5806 }
5807 else
5808 {
5809 sym.st_value = last;
5810 dindx++;
5811 }
5b3b9ff6 5812
53787b23
ILT
5813 sym.st_shndx = (i < MIPS_TEXT_DYNSYM_SECNO
5814 ? SHN_MIPS_TEXT
5815 : SHN_MIPS_DATA);
5816 ++i;
5817 sym.st_name =
5818 mips_elf_hash_table (info)->dynsym_sec_strindex[dindx];
5819
5820 bfd_elf32_swap_symbol_out (output_bfd, &sym,
5821 (((Elf32_External_Sym *)
5822 sdynsym->contents)
5823 + dindx));
5824 }
5b3b9ff6 5825
53787b23
ILT
5826 /* Set the sh_info field of the output .dynsym section to
5827 the index of the first global symbol. */
5828 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
5829 SIZEOF_MIPS_DYNSYM_SECNAMES;
5830 }
5831 else
5832 {
5833 sym.st_size = 0;
5834 sym.st_name = 0;
5835 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5836 sym.st_other = 0;
5b3b9ff6 5837
53787b23
ILT
5838 for (s = output_bfd->sections; s != NULL; s = s->next)
5839 {
5840 int indx;
5b3b9ff6 5841
53787b23 5842 sym.st_value = s->vma;
5b3b9ff6 5843
53787b23
ILT
5844 indx = elf_section_data (s)->this_idx;
5845 BFD_ASSERT (indx > 0);
5846 sym.st_shndx = indx;
5b3b9ff6 5847
53787b23
ILT
5848 bfd_elf32_swap_symbol_out (output_bfd, &sym,
5849 (((Elf32_External_Sym *)
5850 sdynsym->contents)
5851 + elf_section_data (s)->dynindx));
5852 }
5b3b9ff6 5853
53787b23
ILT
5854 /* Set the sh_info field of the output .dynsym section to
5855 the index of the first global symbol. */
5856 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
5857 bfd_count_sections (output_bfd) + 1;
5858 }
5859 }
5b3b9ff6 5860
53787b23
ILT
5861 if (SGI_COMPAT (output_bfd))
5862 {
5863 /* Write .compact_rel section out. */
5864 s = bfd_get_section_by_name (dynobj, ".compact_rel");
5865 if (s != NULL)
5866 {
5867 cpt.id1 = 1;
5868 cpt.num = s->reloc_count;
5869 cpt.id2 = 2;
5870 cpt.offset = (s->output_section->filepos
5871 + sizeof (Elf32_External_compact_rel));
5872 cpt.reserved0 = 0;
5873 cpt.reserved1 = 0;
5874 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
5875 ((Elf32_External_compact_rel *)
5876 s->contents));
5877
5878 /* Clean up a dummy stub function entry in .text. */
5879 s = bfd_get_section_by_name (dynobj, ".stub");
5880 if (s != NULL)
5881 {
5882 file_ptr dummy_offset;
5b3b9ff6 5883
53787b23
ILT
5884 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
5885 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
5886 memset (s->contents + dummy_offset, 0,
5887 MIPS_FUNCTION_STUB_SIZE);
5888 }
5889 }
5890 }
5b3b9ff6 5891
53787b23
ILT
5892 /* Clean up a first relocation in .rel.dyn. */
5893 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
5894 if (s != NULL)
5895 memset (s->contents, 0, sizeof (Elf32_External_Rel));
5896 }
5b3b9ff6
ILT
5897
5898 return true;
5899}
5900\f
5901/* This is almost identical to bfd_generic_get_... except that some
5902 MIPS relocations need to be handled specially. Sigh. */
5903
5904static bfd_byte *
5905elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
5906 relocateable, symbols)
5907 bfd *abfd;
5908 struct bfd_link_info *link_info;
5909 struct bfd_link_order *link_order;
5910 bfd_byte *data;
5911 boolean relocateable;
5912 asymbol **symbols;
5913{
5914 /* Get enough memory to hold the stuff */
5915 bfd *input_bfd = link_order->u.indirect.section->owner;
5916 asection *input_section = link_order->u.indirect.section;
5917
5918 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
5919 arelent **reloc_vector = NULL;
5920 long reloc_count;
5921
5922 if (reloc_size < 0)
5923 goto error_return;
5924
5925 reloc_vector = (arelent **) bfd_malloc (reloc_size);
5926 if (reloc_vector == NULL && reloc_size != 0)
5927 goto error_return;
5928
a16fcab3
KR
5929 /* read in the section */
5930 if (!bfd_get_section_contents (input_bfd,
5931 input_section,
5932 (PTR) data,
5933 0,
5934 input_section->_raw_size))
5935 goto error_return;
5936
5937 /* We're not relaxing the section, so just copy the size info */
5938 input_section->_cooked_size = input_section->_raw_size;
5939 input_section->reloc_done = true;
5940
5941 reloc_count = bfd_canonicalize_reloc (input_bfd,
5942 input_section,
5943 reloc_vector,
5944 symbols);
5945 if (reloc_count < 0)
5946 goto error_return;
5947
5948 if (reloc_count > 0)
5949 {
5950 arelent **parent;
5951 /* for mips */
5952 int gp_found;
ede4eed4 5953 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
a16fcab3
KR
5954
5955 {
5956 struct bfd_hash_entry *h;
5957 struct bfd_link_hash_entry *lh;
5958 /* Skip all this stuff if we aren't mixing formats. */
5959 if (abfd && input_bfd
5960 && abfd->xvec == input_bfd->xvec)
5961 lh = 0;
5962 else
5963 {
a2546fce 5964 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
a16fcab3
KR
5965 lh = (struct bfd_link_hash_entry *) h;
5966 }
5967 lookup:
5968 if (lh)
5969 {
5970 switch (lh->type)
5971 {
5972 case bfd_link_hash_undefined:
5973 case bfd_link_hash_undefweak:
5974 case bfd_link_hash_common:
5975 gp_found = 0;
5976 break;
5977 case bfd_link_hash_defined:
5978 case bfd_link_hash_defweak:
5979 gp_found = 1;
5980 gp = lh->u.def.value;
5981 break;
5982 case bfd_link_hash_indirect:
5983 case bfd_link_hash_warning:
5984 lh = lh->u.i.link;
5985 /* @@FIXME ignoring warning for now */
5986 goto lookup;
5987 case bfd_link_hash_new:
5988 default:
5989 abort ();
5990 }
5991 }
5992 else
5993 gp_found = 0;
5994 }
5995 /* end mips */
5996 for (parent = reloc_vector; *parent != (arelent *) NULL;
5997 parent++)
5998 {
5999 char *error_message = (char *) NULL;
6000 bfd_reloc_status_type r;
6001
6002 /* Specific to MIPS: Deal with relocation types that require
6003 knowing the gp of the output bfd. */
6004 asymbol *sym = *(*parent)->sym_ptr_ptr;
6005 if (bfd_is_abs_section (sym->section) && abfd)
6006 {
6007 /* The special_function wouldn't get called anyways. */
6008 }
6009 else if (!gp_found)
6010 {
6011 /* The gp isn't there; let the special function code
6012 fall over on its own. */
6013 }
5b3b9ff6 6014 else if ((*parent)->howto->special_function
bc05732b 6015 == _bfd_mips_elf_gprel16_reloc)
a16fcab3
KR
6016 {
6017 /* bypass special_function call */
6018 r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
6019 relocateable, (PTR) data, gp);
6020 goto skip_bfd_perform_relocation;
6021 }
6022 /* end mips specific stuff */
6023
6024 r = bfd_perform_relocation (input_bfd,
6025 *parent,
6026 (PTR) data,
6027 input_section,
6028 relocateable ? abfd : (bfd *) NULL,
6029 &error_message);
6030 skip_bfd_perform_relocation:
6031
6032 if (relocateable)
6033 {
6034 asection *os = input_section->output_section;
6035
6036 /* A partial link, so keep the relocs */
6037 os->orelocation[os->reloc_count] = *parent;
6038 os->reloc_count++;
6039 }
6040
6041 if (r != bfd_reloc_ok)
6042 {
6043 switch (r)
6044 {
6045 case bfd_reloc_undefined:
6046 if (!((*link_info->callbacks->undefined_symbol)
6047 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
6048 input_bfd, input_section, (*parent)->address)))
6049 goto error_return;
6050 break;
6051 case bfd_reloc_dangerous:
6052 BFD_ASSERT (error_message != (char *) NULL);
6053 if (!((*link_info->callbacks->reloc_dangerous)
6054 (link_info, error_message, input_bfd, input_section,
6055 (*parent)->address)))
6056 goto error_return;
6057 break;
6058 case bfd_reloc_overflow:
6059 if (!((*link_info->callbacks->reloc_overflow)
6060 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
6061 (*parent)->howto->name, (*parent)->addend,
6062 input_bfd, input_section, (*parent)->address)))
6063 goto error_return;
6064 break;
6065 case bfd_reloc_outofrange:
6066 default:
6067 abort ();
6068 break;
6069 }
6070
6071 }
6072 }
6073 }
6074 if (reloc_vector != NULL)
6075 free (reloc_vector);
6076 return data;
6077
6078error_return:
6079 if (reloc_vector != NULL)
6080 free (reloc_vector);
6081 return NULL;
6082}
5b3b9ff6
ILT
6083#define bfd_elf32_bfd_get_relocated_section_contents \
6084 elf32_mips_get_relocated_section_contents
a16fcab3 6085\f
6e07e54f
ILT
6086/* ECOFF swapping routines. These are used when dealing with the
6087 .mdebug section, which is in the ECOFF debugging format. */
00176555 6088static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap =
6e07e54f
ILT
6089{
6090 /* Symbol table magic number. */
6091 magicSym,
6092 /* Alignment of debugging information. E.g., 4. */
6093 4,
6094 /* Sizes of external symbolic information. */
6095 sizeof (struct hdr_ext),
6096 sizeof (struct dnr_ext),
6097 sizeof (struct pdr_ext),
6098 sizeof (struct sym_ext),
6099 sizeof (struct opt_ext),
6100 sizeof (struct fdr_ext),
6101 sizeof (struct rfd_ext),
6102 sizeof (struct ext_ext),
6103 /* Functions to swap in external symbolic data. */
6104 ecoff_swap_hdr_in,
6105 ecoff_swap_dnr_in,
6106 ecoff_swap_pdr_in,
6107 ecoff_swap_sym_in,
6108 ecoff_swap_opt_in,
6109 ecoff_swap_fdr_in,
6110 ecoff_swap_rfd_in,
6111 ecoff_swap_ext_in,
75f3ef7a
ILT
6112 _bfd_ecoff_swap_tir_in,
6113 _bfd_ecoff_swap_rndx_in,
6e07e54f
ILT
6114 /* Functions to swap out external symbolic data. */
6115 ecoff_swap_hdr_out,
6116 ecoff_swap_dnr_out,
6117 ecoff_swap_pdr_out,
6118 ecoff_swap_sym_out,
6119 ecoff_swap_opt_out,
6120 ecoff_swap_fdr_out,
6121 ecoff_swap_rfd_out,
aac6b32f 6122 ecoff_swap_ext_out,
75f3ef7a
ILT
6123 _bfd_ecoff_swap_tir_out,
6124 _bfd_ecoff_swap_rndx_out,
aac6b32f 6125 /* Function to read in symbolic data. */
00176555 6126 _bfd_mips_elf_read_ecoff_info
6e07e54f
ILT
6127};
6128\f
6b4b4d17
JK
6129#define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
6130#define TARGET_LITTLE_NAME "elf32-littlemips"
6131#define TARGET_BIG_SYM bfd_elf32_bigmips_vec
6132#define TARGET_BIG_NAME "elf32-bigmips"
6133#define ELF_ARCH bfd_arch_mips
6e07e54f 6134#define ELF_MACHINE_CODE EM_MIPS
9b292d4c
ILT
6135
6136/* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
6137 a value of 0x1000, and we are compatible. */
6138#define ELF_MAXPAGESIZE 0x1000
6139
497c5434 6140#define elf_backend_collect true
5b3b9ff6 6141#define elf_backend_type_change_ok true
b3c0fc57
ILT
6142#define elf_info_to_howto 0
6143#define elf_info_to_howto_rel mips_info_to_howto_rel
6e07e54f 6144#define elf_backend_sym_is_global mips_elf_sym_is_global
00176555 6145#define elf_backend_object_p mips_elf32_object_p
d1bf45aa 6146#define elf_backend_section_from_shdr mips_elf32_section_from_shdr
00176555 6147#define elf_backend_fake_sections _bfd_mips_elf_fake_sections
b3c0fc57 6148#define elf_backend_section_from_bfd_section \
00176555
ILT
6149 _bfd_mips_elf_section_from_bfd_section
6150#define elf_backend_section_processing mips_elf32_section_processing
6151#define elf_backend_symbol_processing _bfd_mips_elf_symbol_processing
5b3b9ff6
ILT
6152#define elf_backend_additional_program_headers \
6153 mips_elf_additional_program_headers
6154#define elf_backend_modify_segment_map mips_elf_modify_segment_map
6e07e54f 6155#define elf_backend_final_write_processing \
00176555
ILT
6156 _bfd_mips_elf_final_write_processing
6157#define elf_backend_ecoff_debug_swap &mips_elf32_ecoff_debug_swap
b3c0fc57 6158
a2546fce 6159#define bfd_elf32_bfd_is_local_label mips_elf_is_local_label
bc05732b
ILT
6160#define bfd_elf32_find_nearest_line _bfd_mips_elf_find_nearest_line
6161#define bfd_elf32_set_section_contents _bfd_mips_elf_set_section_contents
aac6b32f
ILT
6162#define bfd_elf32_bfd_link_hash_table_create \
6163 mips_elf_link_hash_table_create
6e07e54f 6164#define bfd_elf32_bfd_final_link mips_elf_final_link
5b3b9ff6 6165#define bfd_elf32_bfd_copy_private_bfd_data \
bc05732b 6166 _bfd_mips_elf_copy_private_bfd_data
5b3b9ff6 6167#define bfd_elf32_bfd_merge_private_bfd_data \
bc05732b
ILT
6168 _bfd_mips_elf_merge_private_bfd_data
6169#define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
aac6b32f 6170#define elf_backend_add_symbol_hook mips_elf_add_symbol_hook
5b3b9ff6
ILT
6171#define elf_backend_create_dynamic_sections \
6172 mips_elf_create_dynamic_sections
6173#define elf_backend_check_relocs mips_elf_check_relocs
6174#define elf_backend_adjust_dynamic_symbol \
6175 mips_elf_adjust_dynamic_symbol
6176#define elf_backend_size_dynamic_sections \
6177 mips_elf_size_dynamic_sections
6178#define elf_backend_relocate_section mips_elf_relocate_section
6179#define elf_backend_finish_dynamic_symbol \
6180 mips_elf_finish_dynamic_symbol
6181#define elf_backend_finish_dynamic_sections \
6182 mips_elf_finish_dynamic_sections
6b4b4d17
JK
6183
6184#include "elf32-target.h"
This page took 0.411349 seconds and 4 git commands to generate.