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