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