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