Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* MIPS-specific support for 32-bit ELF |
be3ccd9c KH |
2 | Copyright 1993, 94, 95, 96, 97, 98, 99, 2000 |
3 | Free Software Foundation, Inc. | |
252b5132 RH |
4 | |
5 | Most of the information added by Ian Lance Taylor, Cygnus Support, | |
6 | <ian@cygnus.com>. | |
103186c6 MM |
7 | N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC. |
8 | <mark@codesourcery.com> | |
f7cb7d68 UC |
9 | Traditional MIPS targets support added by Koundinya.K, Dansk Data |
10 | Elektronik & Operations Research Group. <kk@ddeorg.soft.net> | |
252b5132 RH |
11 | |
12 | This file is part of BFD, the Binary File Descriptor library. | |
13 | ||
14 | This program is free software; you can redistribute it and/or modify | |
15 | it under the terms of the GNU General Public License as published by | |
16 | the Free Software Foundation; either version 2 of the License, or | |
17 | (at your option) any later version. | |
18 | ||
19 | This program is distributed in the hope that it will be useful, | |
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | GNU General Public License for more details. | |
23 | ||
24 | You should have received a copy of the GNU General Public License | |
25 | along with this program; if not, write to the Free Software | |
26 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
27 | ||
28 | /* This file handles MIPS ELF targets. SGI Irix 5 uses a slightly | |
29 | different MIPS ELF from other targets. This matters when linking. | |
30 | This file supports both, switching at runtime. */ | |
31 | ||
32 | #include "bfd.h" | |
33 | #include "sysdep.h" | |
34 | #include "libbfd.h" | |
35 | #include "bfdlink.h" | |
36 | #include "genlink.h" | |
37 | #include "elf-bfd.h" | |
38 | #include "elf/mips.h" | |
39 | ||
40 | /* Get the ECOFF swapping routines. */ | |
41 | #include "coff/sym.h" | |
42 | #include "coff/symconst.h" | |
43 | #include "coff/internal.h" | |
44 | #include "coff/ecoff.h" | |
45 | #include "coff/mips.h" | |
23e2c83b | 46 | #define ECOFF_SIGNED_32 |
252b5132 RH |
47 | #include "ecoffswap.h" |
48 | ||
7403cb63 MM |
49 | /* This structure is used to hold .got information when linking. It |
50 | is stored in the tdata field of the bfd_elf_section_data structure. */ | |
51 | ||
be3ccd9c | 52 | struct mips_got_info { |
7403cb63 MM |
53 | /* The global symbol in the GOT with the lowest index in the dynamic |
54 | symbol table. */ | |
55 | struct elf_link_hash_entry *global_gotsym; | |
b3be9b46 RH |
56 | /* The number of global .got entries. */ |
57 | unsigned int global_gotno; | |
7403cb63 MM |
58 | /* The number of local .got entries. */ |
59 | unsigned int local_gotno; | |
60 | /* The number of local .got entries we have used. */ | |
61 | unsigned int assigned_gotno; | |
62 | }; | |
63 | ||
64 | /* The MIPS ELF linker needs additional information for each symbol in | |
65 | the global hash table. */ | |
66 | ||
be3ccd9c | 67 | struct mips_elf_link_hash_entry { |
7403cb63 MM |
68 | struct elf_link_hash_entry root; |
69 | ||
70 | /* External symbol information. */ | |
71 | EXTR esym; | |
72 | ||
a3c7651d | 73 | /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against |
be3ccd9c | 74 | this symbol. */ |
a3c7651d | 75 | unsigned int possibly_dynamic_relocs; |
7403cb63 MM |
76 | |
77 | /* The index of the first dynamic relocation (in the .rel.dyn | |
78 | section) against this symbol. */ | |
79 | unsigned int min_dyn_reloc_index; | |
80 | ||
81 | /* If there is a stub that 32 bit functions should use to call this | |
82 | 16 bit function, this points to the section containing the stub. */ | |
83 | asection *fn_stub; | |
84 | ||
85 | /* Whether we need the fn_stub; this is set if this symbol appears | |
86 | in any relocs other than a 16 bit call. */ | |
87 | boolean need_fn_stub; | |
88 | ||
89 | /* If there is a stub that 16 bit functions should use to call this | |
90 | 32 bit function, this points to the section containing the stub. */ | |
91 | asection *call_stub; | |
92 | ||
93 | /* This is like the call_stub field, but it is used if the function | |
94 | being called returns a floating point value. */ | |
95 | asection *call_fp_stub; | |
96 | }; | |
97 | ||
252b5132 RH |
98 | static bfd_reloc_status_type mips32_64bit_reloc |
99 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); | |
100 | static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup | |
101 | PARAMS ((bfd *, bfd_reloc_code_real_type)); | |
c9b3cbf3 RH |
102 | static reloc_howto_type *mips_rtype_to_howto |
103 | PARAMS ((unsigned int)); | |
252b5132 RH |
104 | static void mips_info_to_howto_rel |
105 | PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *)); | |
3f830999 MM |
106 | static void mips_info_to_howto_rela |
107 | PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *)); | |
252b5132 RH |
108 | static void bfd_mips_elf32_swap_gptab_in |
109 | PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *)); | |
110 | static void bfd_mips_elf32_swap_gptab_out | |
111 | PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *)); | |
86033394 | 112 | #if 0 |
be3ccd9c | 113 | static void bfd_mips_elf_swap_msym_in |
c6142e5d | 114 | PARAMS ((bfd *, const Elf32_External_Msym *, Elf32_Internal_Msym *)); |
86033394 | 115 | #endif |
c6142e5d MM |
116 | static void bfd_mips_elf_swap_msym_out |
117 | PARAMS ((bfd *, const Elf32_Internal_Msym *, Elf32_External_Msym *)); | |
252b5132 | 118 | static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *)); |
252b5132 RH |
119 | static boolean mips_elf_create_procedure_table |
120 | PARAMS ((PTR, bfd *, struct bfd_link_info *, asection *, | |
121 | struct ecoff_debug_info *)); | |
252b5132 RH |
122 | static INLINE int elf_mips_isa PARAMS ((flagword)); |
123 | static INLINE int elf_mips_mach PARAMS ((flagword)); | |
103186c6 | 124 | static INLINE char* elf_mips_abi_name PARAMS ((bfd *)); |
252b5132 RH |
125 | static boolean mips_elf_is_local_label_name |
126 | PARAMS ((bfd *, const char *)); | |
127 | static struct bfd_hash_entry *mips_elf_link_hash_newfunc | |
128 | PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *)); | |
252b5132 | 129 | static int gptab_compare PARAMS ((const void *, const void *)); |
252b5132 RH |
130 | static bfd_reloc_status_type mips16_jump_reloc |
131 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); | |
132 | static bfd_reloc_status_type mips16_gprel_reloc | |
133 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); | |
252b5132 RH |
134 | static boolean mips_elf_create_compact_rel_section |
135 | PARAMS ((bfd *, struct bfd_link_info *)); | |
136 | static boolean mips_elf_create_got_section | |
137 | PARAMS ((bfd *, struct bfd_link_info *)); | |
252b5132 RH |
138 | static bfd_reloc_status_type mips_elf_final_gp |
139 | PARAMS ((bfd *, asymbol *, boolean, char **, bfd_vma *)); | |
140 | static bfd_byte *elf32_mips_get_relocated_section_contents | |
141 | PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *, | |
142 | bfd_byte *, boolean, asymbol **)); | |
be3ccd9c | 143 | static asection *mips_elf_create_msym_section |
c6142e5d | 144 | PARAMS ((bfd *)); |
be3ccd9c | 145 | static void mips_elf_irix6_finish_dynamic_symbol |
7403cb63 MM |
146 | PARAMS ((bfd *, const char *, Elf_Internal_Sym *)); |
147 | static bfd_vma mips_elf_sign_extend PARAMS ((bfd_vma, int)); | |
148 | static boolean mips_elf_overflow_p PARAMS ((bfd_vma, int)); | |
149 | static bfd_vma mips_elf_high PARAMS ((bfd_vma)); | |
150 | static bfd_vma mips_elf_higher PARAMS ((bfd_vma)); | |
151 | static bfd_vma mips_elf_highest PARAMS ((bfd_vma)); | |
152 | static bfd_vma mips_elf_global_got_index | |
153 | PARAMS ((bfd *, struct elf_link_hash_entry *)); | |
154 | static bfd_vma mips_elf_local_got_index | |
155 | PARAMS ((bfd *, struct bfd_link_info *, bfd_vma)); | |
156 | static bfd_vma mips_elf_got_offset_from_index | |
157 | PARAMS ((bfd *, bfd *, bfd_vma)); | |
be3ccd9c | 158 | static boolean mips_elf_record_global_got_symbol |
7403cb63 MM |
159 | PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *, |
160 | struct mips_got_info *)); | |
161 | static bfd_vma mips_elf_got_page | |
162 | PARAMS ((bfd *, struct bfd_link_info *, bfd_vma, bfd_vma *)); | |
bb2d6cd7 | 163 | static const Elf_Internal_Rela *mips_elf_next_relocation |
be3ccd9c | 164 | PARAMS ((unsigned int, const Elf_Internal_Rela *, |
bb2d6cd7 | 165 | const Elf_Internal_Rela *)); |
7403cb63 MM |
166 | static bfd_reloc_status_type mips_elf_calculate_relocation |
167 | PARAMS ((bfd *, bfd *, asection *, struct bfd_link_info *, | |
103186c6 | 168 | const Elf_Internal_Rela *, bfd_vma, reloc_howto_type *, |
197b9ca0 MM |
169 | Elf_Internal_Sym *, asection **, bfd_vma *, const char **, |
170 | boolean *)); | |
7403cb63 | 171 | static bfd_vma mips_elf_obtain_contents |
103186c6 | 172 | PARAMS ((reloc_howto_type *, const Elf_Internal_Rela *, bfd *, bfd_byte *)); |
197b9ca0 | 173 | static boolean mips_elf_perform_relocation |
be3ccd9c | 174 | PARAMS ((struct bfd_link_info *, reloc_howto_type *, |
e53bd91b | 175 | const Elf_Internal_Rela *, bfd_vma, |
197b9ca0 | 176 | bfd *, asection *, bfd_byte *, boolean)); |
7403cb63 | 177 | static boolean mips_elf_assign_gp PARAMS ((bfd *, bfd_vma *)); |
be3ccd9c | 178 | static boolean mips_elf_sort_hash_table_f |
7403cb63 | 179 | PARAMS ((struct mips_elf_link_hash_entry *, PTR)); |
be3ccd9c | 180 | static boolean mips_elf_sort_hash_table |
b3be9b46 | 181 | PARAMS ((struct bfd_link_info *, unsigned long)); |
7403cb63 | 182 | static asection * mips_elf_got_section PARAMS ((bfd *)); |
be3ccd9c | 183 | static struct mips_got_info *mips_elf_got_info |
7403cb63 | 184 | PARAMS ((bfd *, asection **)); |
6387d602 | 185 | static boolean mips_elf_local_relocation_p |
b305ef96 | 186 | PARAMS ((bfd *, const Elf_Internal_Rela *, asection **, boolean)); |
be3ccd9c | 187 | static bfd_vma mips_elf_create_local_got_entry |
7403cb63 | 188 | PARAMS ((bfd *, struct mips_got_info *, asection *, bfd_vma)); |
be3ccd9c | 189 | static bfd_vma mips_elf_got16_entry |
b305ef96 | 190 | PARAMS ((bfd *, struct bfd_link_info *, bfd_vma, boolean)); |
be3ccd9c | 191 | static boolean mips_elf_create_dynamic_relocation |
103186c6 | 192 | PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Rela *, |
7b1f1231 | 193 | struct mips_elf_link_hash_entry *, asection *, |
5af5b85b | 194 | bfd_vma, bfd_vma *, asection *, boolean local_p)); |
be3ccd9c | 195 | static void mips_elf_allocate_dynamic_relocations |
103186c6 | 196 | PARAMS ((bfd *, unsigned int)); |
be3ccd9c | 197 | static boolean mips_elf_stub_section_p |
197b9ca0 | 198 | PARAMS ((bfd *, asection *)); |
adb76a3e UC |
199 | static int sort_dynamic_relocs |
200 | PARAMS ((const void *, const void *)); | |
252b5132 | 201 | |
f7cb7d68 UC |
202 | extern const bfd_target bfd_elf32_tradbigmips_vec; |
203 | ||
a94a7c1c | 204 | /* The level of IRIX compatibility we're striving for. */ |
252b5132 | 205 | |
a94a7c1c MM |
206 | typedef enum { |
207 | ict_none, | |
208 | ict_irix5, | |
209 | ict_irix6 | |
210 | } irix_compat_t; | |
211 | ||
adb76a3e UC |
212 | /* This will be used when we sort the dynamic relocation records. */ |
213 | static bfd *reldyn_sorting_bfd; | |
214 | ||
a94a7c1c MM |
215 | /* Nonzero if ABFD is using the N32 ABI. */ |
216 | ||
217 | #define ABI_N32_P(abfd) \ | |
218 | ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0) | |
219 | ||
5e38c3b8 MM |
220 | /* Nonzero if ABFD is using the 64-bit ABI. FIXME: This is never |
221 | true, yet. */ | |
222 | #define ABI_64_P(abfd) \ | |
223 | ((elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) != 0) | |
224 | ||
f7cb7d68 | 225 | /* Depending on the target vector we generate some version of Irix |
be3ccd9c | 226 | executables or "normal" MIPS ELF ABI executables. */ |
a94a7c1c MM |
227 | |
228 | #define IRIX_COMPAT(abfd) \ | |
f7cb7d68 UC |
229 | (abfd->xvec == &bfd_elf32_tradbigmips_vec ? ict_none : \ |
230 | ((ABI_N32_P (abfd) || ABI_64_P (abfd)) ? ict_irix6 : ict_irix5)) | |
a94a7c1c MM |
231 | |
232 | /* Whether we are trying to be compatible with IRIX at all. */ | |
233 | ||
234 | #define SGI_COMPAT(abfd) \ | |
235 | (IRIX_COMPAT (abfd) != ict_none) | |
252b5132 | 236 | |
c6142e5d MM |
237 | /* The name of the msym section. */ |
238 | #define MIPS_ELF_MSYM_SECTION_NAME(abfd) ".msym" | |
239 | ||
303f629d MM |
240 | /* The name of the srdata section. */ |
241 | #define MIPS_ELF_SRDATA_SECTION_NAME(abfd) ".srdata" | |
242 | ||
243 | /* The name of the options section. */ | |
244 | #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \ | |
245 | (IRIX_COMPAT (abfd) == ict_irix6 ? ".MIPS.options" : ".options") | |
246 | ||
247 | /* The name of the stub section. */ | |
248 | #define MIPS_ELF_STUB_SECTION_NAME(abfd) \ | |
249 | (IRIX_COMPAT (abfd) == ict_irix6 ? ".MIPS.stubs" : ".stub") | |
250 | ||
103186c6 MM |
251 | /* The name of the dynamic relocation section. */ |
252 | #define MIPS_ELF_REL_DYN_SECTION_NAME(abfd) ".rel.dyn" | |
253 | ||
254 | /* The size of an external REL relocation. */ | |
255 | #define MIPS_ELF_REL_SIZE(abfd) \ | |
256 | (get_elf_backend_data (abfd)->s->sizeof_rel) | |
257 | ||
258 | /* The size of an external dynamic table entry. */ | |
259 | #define MIPS_ELF_DYN_SIZE(abfd) \ | |
260 | (get_elf_backend_data (abfd)->s->sizeof_dyn) | |
261 | ||
262 | /* The size of a GOT entry. */ | |
263 | #define MIPS_ELF_GOT_SIZE(abfd) \ | |
264 | (get_elf_backend_data (abfd)->s->arch_size / 8) | |
265 | ||
266 | /* The size of a symbol-table entry. */ | |
267 | #define MIPS_ELF_SYM_SIZE(abfd) \ | |
268 | (get_elf_backend_data (abfd)->s->sizeof_sym) | |
269 | ||
270 | /* The default alignment for sections, as a power of two. */ | |
271 | #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \ | |
272 | (get_elf_backend_data (abfd)->s->file_align == 8 ? 3 : 2) | |
273 | ||
274 | /* Get word-sized data. */ | |
275 | #define MIPS_ELF_GET_WORD(abfd, ptr) \ | |
276 | (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr)) | |
277 | ||
278 | /* Put out word-sized data. */ | |
279 | #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \ | |
280 | (ABI_64_P (abfd) \ | |
281 | ? bfd_put_64 (abfd, val, ptr) \ | |
282 | : bfd_put_32 (abfd, val, ptr)) | |
283 | ||
284 | /* Add a dynamic symbol table-entry. */ | |
9ebbd33e | 285 | #ifdef BFD64 |
103186c6 MM |
286 | #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \ |
287 | (ABI_64_P (elf_hash_table (info)->dynobj) \ | |
288 | ? bfd_elf64_add_dynamic_entry (info, tag, val) \ | |
289 | : bfd_elf32_add_dynamic_entry (info, tag, val)) | |
9ebbd33e MM |
290 | #else |
291 | #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \ | |
292 | (ABI_64_P (elf_hash_table (info)->dynobj) \ | |
e049a0de ILT |
293 | ? (abort (), false) \ |
294 | : bfd_elf32_add_dynamic_entry (info, tag, val)) | |
9ebbd33e | 295 | #endif |
103186c6 | 296 | |
252b5132 RH |
297 | /* The number of local .got entries we reserve. */ |
298 | #define MIPS_RESERVED_GOTNO (2) | |
299 | ||
300 | /* Instructions which appear in a stub. For some reason the stub is | |
301 | slightly different on an SGI system. */ | |
302 | #define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000) | |
103186c6 MM |
303 | #define STUB_LW(abfd) \ |
304 | (SGI_COMPAT (abfd) \ | |
305 | ? (ABI_64_P (abfd) \ | |
306 | ? 0xdf998010 /* ld t9,0x8010(gp) */ \ | |
307 | : 0x8f998010) /* lw t9,0x8010(gp) */ \ | |
f7cb7d68 UC |
308 | : 0x8f998010) /* lw t9,0x8000(gp) */ |
309 | #define STUB_MOVE(abfd) \ | |
310 | (SGI_COMPAT (abfd) ? 0x03e07825 : 0x03e07821) /* move t7,ra */ | |
311 | #define STUB_JALR 0x0320f809 /* jal t9 */ | |
312 | #define STUB_LI16(abfd) \ | |
313 | (SGI_COMPAT (abfd) ? 0x34180000 : 0x24180000) /* ori t8,zero,0 */ | |
252b5132 RH |
314 | #define MIPS_FUNCTION_STUB_SIZE (16) |
315 | ||
316 | #if 0 | |
317 | /* We no longer try to identify particular sections for the .dynsym | |
318 | section. When we do, we wind up crashing if there are other random | |
319 | sections with relocations. */ | |
320 | ||
321 | /* Names of sections which appear in the .dynsym section in an Irix 5 | |
322 | executable. */ | |
323 | ||
be3ccd9c | 324 | static const char * const mips_elf_dynsym_sec_names[] = { |
252b5132 RH |
325 | ".text", |
326 | ".init", | |
327 | ".fini", | |
328 | ".data", | |
329 | ".rodata", | |
330 | ".sdata", | |
331 | ".sbss", | |
332 | ".bss", | |
333 | NULL | |
334 | }; | |
335 | ||
336 | #define SIZEOF_MIPS_DYNSYM_SECNAMES \ | |
337 | (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0]) | |
338 | ||
339 | /* The number of entries in mips_elf_dynsym_sec_names which go in the | |
340 | text segment. */ | |
341 | ||
342 | #define MIPS_TEXT_DYNSYM_SECNO (3) | |
343 | ||
344 | #endif /* 0 */ | |
345 | ||
346 | /* The names of the runtime procedure table symbols used on Irix 5. */ | |
347 | ||
be3ccd9c | 348 | static const char * const mips_elf_dynsym_rtproc_names[] = { |
252b5132 RH |
349 | "_procedure_table", |
350 | "_procedure_string_table", | |
351 | "_procedure_table_size", | |
352 | NULL | |
353 | }; | |
354 | ||
355 | /* These structures are used to generate the .compact_rel section on | |
356 | Irix 5. */ | |
357 | ||
be3ccd9c | 358 | typedef struct { |
252b5132 RH |
359 | unsigned long id1; /* Always one? */ |
360 | unsigned long num; /* Number of compact relocation entries. */ | |
361 | unsigned long id2; /* Always two? */ | |
362 | unsigned long offset; /* The file offset of the first relocation. */ | |
363 | unsigned long reserved0; /* Zero? */ | |
364 | unsigned long reserved1; /* Zero? */ | |
365 | } Elf32_compact_rel; | |
366 | ||
be3ccd9c | 367 | typedef struct { |
252b5132 RH |
368 | bfd_byte id1[4]; |
369 | bfd_byte num[4]; | |
370 | bfd_byte id2[4]; | |
371 | bfd_byte offset[4]; | |
372 | bfd_byte reserved0[4]; | |
373 | bfd_byte reserved1[4]; | |
374 | } Elf32_External_compact_rel; | |
375 | ||
be3ccd9c | 376 | typedef struct { |
252b5132 | 377 | unsigned int ctype : 1; /* 1: long 0: short format. See below. */ |
be3ccd9c | 378 | unsigned int rtype : 4; /* Relocation types. See below. */ |
252b5132 RH |
379 | unsigned int dist2to : 8; |
380 | unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */ | |
381 | unsigned long konst; /* KONST field. See below. */ | |
382 | unsigned long vaddr; /* VADDR to be relocated. */ | |
383 | } Elf32_crinfo; | |
384 | ||
be3ccd9c | 385 | typedef struct { |
252b5132 | 386 | unsigned int ctype : 1; /* 1: long 0: short format. See below. */ |
be3ccd9c | 387 | unsigned int rtype : 4; /* Relocation types. See below. */ |
252b5132 RH |
388 | unsigned int dist2to : 8; |
389 | unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */ | |
390 | unsigned long konst; /* KONST field. See below. */ | |
391 | } Elf32_crinfo2; | |
392 | ||
be3ccd9c | 393 | typedef struct { |
252b5132 RH |
394 | bfd_byte info[4]; |
395 | bfd_byte konst[4]; | |
396 | bfd_byte vaddr[4]; | |
397 | } Elf32_External_crinfo; | |
398 | ||
be3ccd9c | 399 | typedef struct { |
252b5132 RH |
400 | bfd_byte info[4]; |
401 | bfd_byte konst[4]; | |
402 | } Elf32_External_crinfo2; | |
403 | ||
404 | /* These are the constants used to swap the bitfields in a crinfo. */ | |
405 | ||
406 | #define CRINFO_CTYPE (0x1) | |
407 | #define CRINFO_CTYPE_SH (31) | |
408 | #define CRINFO_RTYPE (0xf) | |
409 | #define CRINFO_RTYPE_SH (27) | |
410 | #define CRINFO_DIST2TO (0xff) | |
411 | #define CRINFO_DIST2TO_SH (19) | |
412 | #define CRINFO_RELVADDR (0x7ffff) | |
413 | #define CRINFO_RELVADDR_SH (0) | |
414 | ||
415 | /* A compact relocation info has long (3 words) or short (2 words) | |
416 | formats. A short format doesn't have VADDR field and relvaddr | |
417 | fields contains ((VADDR - vaddr of the previous entry) >> 2). */ | |
418 | #define CRF_MIPS_LONG 1 | |
419 | #define CRF_MIPS_SHORT 0 | |
420 | ||
421 | /* There are 4 types of compact relocation at least. The value KONST | |
422 | has different meaning for each type: | |
423 | ||
424 | (type) (konst) | |
425 | CT_MIPS_REL32 Address in data | |
426 | CT_MIPS_WORD Address in word (XXX) | |
427 | CT_MIPS_GPHI_LO GP - vaddr | |
428 | CT_MIPS_JMPAD Address to jump | |
429 | */ | |
430 | ||
431 | #define CRT_MIPS_REL32 0xa | |
432 | #define CRT_MIPS_WORD 0xb | |
433 | #define CRT_MIPS_GPHI_LO 0xc | |
434 | #define CRT_MIPS_JMPAD 0xd | |
435 | ||
436 | #define mips_elf_set_cr_format(x,format) ((x).ctype = (format)) | |
437 | #define mips_elf_set_cr_type(x,type) ((x).rtype = (type)) | |
438 | #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v)) | |
439 | #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2) | |
440 | ||
441 | static void bfd_elf32_swap_compact_rel_out | |
442 | PARAMS ((bfd *, const Elf32_compact_rel *, Elf32_External_compact_rel *)); | |
443 | static void bfd_elf32_swap_crinfo_out | |
444 | PARAMS ((bfd *, const Elf32_crinfo *, Elf32_External_crinfo *)); | |
445 | ||
446 | #define USE_REL 1 /* MIPS uses REL relocations instead of RELA */ | |
447 | ||
3f830999 MM |
448 | /* In case we're on a 32-bit machine, construct a 64-bit "-1" value |
449 | from smaller values. Start with zero, widen, *then* decrement. */ | |
450 | #define MINUS_ONE (((bfd_vma)0) - 1) | |
451 | ||
be3ccd9c | 452 | static reloc_howto_type elf_mips_howto_table[] = { |
252b5132 RH |
453 | /* No relocation. */ |
454 | HOWTO (R_MIPS_NONE, /* type */ | |
455 | 0, /* rightshift */ | |
456 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
457 | 0, /* bitsize */ | |
458 | false, /* pc_relative */ | |
459 | 0, /* bitpos */ | |
460 | complain_overflow_dont, /* complain_on_overflow */ | |
461 | bfd_elf_generic_reloc, /* special_function */ | |
462 | "R_MIPS_NONE", /* name */ | |
463 | false, /* partial_inplace */ | |
464 | 0, /* src_mask */ | |
465 | 0, /* dst_mask */ | |
466 | false), /* pcrel_offset */ | |
467 | ||
468 | /* 16 bit relocation. */ | |
469 | HOWTO (R_MIPS_16, /* type */ | |
470 | 0, /* rightshift */ | |
471 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
472 | 16, /* bitsize */ | |
473 | false, /* pc_relative */ | |
474 | 0, /* bitpos */ | |
475 | complain_overflow_bitfield, /* complain_on_overflow */ | |
476 | bfd_elf_generic_reloc, /* special_function */ | |
477 | "R_MIPS_16", /* name */ | |
478 | true, /* partial_inplace */ | |
479 | 0xffff, /* src_mask */ | |
480 | 0xffff, /* dst_mask */ | |
481 | false), /* pcrel_offset */ | |
482 | ||
483 | /* 32 bit relocation. */ | |
484 | HOWTO (R_MIPS_32, /* type */ | |
485 | 0, /* rightshift */ | |
486 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
487 | 32, /* bitsize */ | |
488 | false, /* pc_relative */ | |
489 | 0, /* bitpos */ | |
490 | complain_overflow_bitfield, /* complain_on_overflow */ | |
491 | bfd_elf_generic_reloc, /* special_function */ | |
492 | "R_MIPS_32", /* name */ | |
493 | true, /* partial_inplace */ | |
494 | 0xffffffff, /* src_mask */ | |
495 | 0xffffffff, /* dst_mask */ | |
496 | false), /* pcrel_offset */ | |
497 | ||
498 | /* 32 bit symbol relative relocation. */ | |
499 | HOWTO (R_MIPS_REL32, /* type */ | |
500 | 0, /* rightshift */ | |
501 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
502 | 32, /* bitsize */ | |
503 | false, /* pc_relative */ | |
504 | 0, /* bitpos */ | |
505 | complain_overflow_bitfield, /* complain_on_overflow */ | |
506 | bfd_elf_generic_reloc, /* special_function */ | |
507 | "R_MIPS_REL32", /* name */ | |
508 | true, /* partial_inplace */ | |
509 | 0xffffffff, /* src_mask */ | |
510 | 0xffffffff, /* dst_mask */ | |
511 | false), /* pcrel_offset */ | |
512 | ||
513 | /* 26 bit branch address. */ | |
514 | HOWTO (R_MIPS_26, /* type */ | |
515 | 2, /* rightshift */ | |
516 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
517 | 26, /* bitsize */ | |
518 | false, /* pc_relative */ | |
519 | 0, /* bitpos */ | |
520 | complain_overflow_dont, /* complain_on_overflow */ | |
521 | /* This needs complex overflow | |
522 | detection, because the upper four | |
523 | bits must match the PC. */ | |
524 | bfd_elf_generic_reloc, /* special_function */ | |
525 | "R_MIPS_26", /* name */ | |
526 | true, /* partial_inplace */ | |
527 | 0x3ffffff, /* src_mask */ | |
528 | 0x3ffffff, /* dst_mask */ | |
529 | false), /* pcrel_offset */ | |
530 | ||
531 | /* High 16 bits of symbol value. */ | |
532 | HOWTO (R_MIPS_HI16, /* type */ | |
533 | 0, /* rightshift */ | |
534 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
535 | 16, /* bitsize */ | |
536 | false, /* pc_relative */ | |
537 | 0, /* bitpos */ | |
538 | complain_overflow_dont, /* complain_on_overflow */ | |
539 | _bfd_mips_elf_hi16_reloc, /* special_function */ | |
540 | "R_MIPS_HI16", /* name */ | |
541 | true, /* partial_inplace */ | |
542 | 0xffff, /* src_mask */ | |
543 | 0xffff, /* dst_mask */ | |
544 | false), /* pcrel_offset */ | |
545 | ||
546 | /* Low 16 bits of symbol value. */ | |
547 | HOWTO (R_MIPS_LO16, /* type */ | |
548 | 0, /* rightshift */ | |
549 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
550 | 16, /* bitsize */ | |
551 | false, /* pc_relative */ | |
552 | 0, /* bitpos */ | |
553 | complain_overflow_dont, /* complain_on_overflow */ | |
554 | _bfd_mips_elf_lo16_reloc, /* special_function */ | |
555 | "R_MIPS_LO16", /* name */ | |
556 | true, /* partial_inplace */ | |
557 | 0xffff, /* src_mask */ | |
558 | 0xffff, /* dst_mask */ | |
559 | false), /* pcrel_offset */ | |
560 | ||
561 | /* GP relative reference. */ | |
562 | HOWTO (R_MIPS_GPREL16, /* type */ | |
563 | 0, /* rightshift */ | |
564 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
565 | 16, /* bitsize */ | |
566 | false, /* pc_relative */ | |
567 | 0, /* bitpos */ | |
568 | complain_overflow_signed, /* complain_on_overflow */ | |
569 | _bfd_mips_elf_gprel16_reloc, /* special_function */ | |
570 | "R_MIPS_GPREL16", /* name */ | |
571 | true, /* partial_inplace */ | |
572 | 0xffff, /* src_mask */ | |
573 | 0xffff, /* dst_mask */ | |
574 | false), /* pcrel_offset */ | |
575 | ||
576 | /* Reference to literal section. */ | |
577 | HOWTO (R_MIPS_LITERAL, /* type */ | |
578 | 0, /* rightshift */ | |
579 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
580 | 16, /* bitsize */ | |
581 | false, /* pc_relative */ | |
582 | 0, /* bitpos */ | |
583 | complain_overflow_signed, /* complain_on_overflow */ | |
584 | _bfd_mips_elf_gprel16_reloc, /* special_function */ | |
585 | "R_MIPS_LITERAL", /* name */ | |
586 | true, /* partial_inplace */ | |
587 | 0xffff, /* src_mask */ | |
588 | 0xffff, /* dst_mask */ | |
589 | false), /* pcrel_offset */ | |
590 | ||
591 | /* Reference to global offset table. */ | |
592 | HOWTO (R_MIPS_GOT16, /* type */ | |
593 | 0, /* rightshift */ | |
594 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
595 | 16, /* bitsize */ | |
596 | false, /* pc_relative */ | |
597 | 0, /* bitpos */ | |
598 | complain_overflow_signed, /* complain_on_overflow */ | |
599 | _bfd_mips_elf_got16_reloc, /* special_function */ | |
600 | "R_MIPS_GOT16", /* name */ | |
601 | false, /* partial_inplace */ | |
b944b044 | 602 | 0xffff, /* src_mask */ |
252b5132 RH |
603 | 0xffff, /* dst_mask */ |
604 | false), /* pcrel_offset */ | |
605 | ||
606 | /* 16 bit PC relative reference. */ | |
607 | HOWTO (R_MIPS_PC16, /* type */ | |
608 | 0, /* rightshift */ | |
609 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
610 | 16, /* bitsize */ | |
611 | true, /* pc_relative */ | |
612 | 0, /* bitpos */ | |
613 | complain_overflow_signed, /* complain_on_overflow */ | |
614 | bfd_elf_generic_reloc, /* special_function */ | |
615 | "R_MIPS_PC16", /* name */ | |
616 | true, /* partial_inplace */ | |
617 | 0xffff, /* src_mask */ | |
618 | 0xffff, /* dst_mask */ | |
bb2d6cd7 | 619 | true), /* pcrel_offset */ |
252b5132 RH |
620 | |
621 | /* 16 bit call through global offset table. */ | |
252b5132 RH |
622 | HOWTO (R_MIPS_CALL16, /* type */ |
623 | 0, /* rightshift */ | |
624 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
625 | 16, /* bitsize */ | |
626 | false, /* pc_relative */ | |
627 | 0, /* bitpos */ | |
628 | complain_overflow_signed, /* complain_on_overflow */ | |
629 | bfd_elf_generic_reloc, /* special_function */ | |
630 | "R_MIPS_CALL16", /* name */ | |
631 | false, /* partial_inplace */ | |
b944b044 | 632 | 0xffff, /* src_mask */ |
252b5132 RH |
633 | 0xffff, /* dst_mask */ |
634 | false), /* pcrel_offset */ | |
635 | ||
636 | /* 32 bit GP relative reference. */ | |
637 | HOWTO (R_MIPS_GPREL32, /* type */ | |
638 | 0, /* rightshift */ | |
639 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
640 | 32, /* bitsize */ | |
641 | false, /* pc_relative */ | |
642 | 0, /* bitpos */ | |
643 | complain_overflow_bitfield, /* complain_on_overflow */ | |
644 | _bfd_mips_elf_gprel32_reloc, /* special_function */ | |
645 | "R_MIPS_GPREL32", /* name */ | |
646 | true, /* partial_inplace */ | |
647 | 0xffffffff, /* src_mask */ | |
648 | 0xffffffff, /* dst_mask */ | |
649 | false), /* pcrel_offset */ | |
650 | ||
651 | /* The remaining relocs are defined on Irix 5, although they are | |
652 | not defined by the ABI. */ | |
5f771d47 ILT |
653 | EMPTY_HOWTO (13), |
654 | EMPTY_HOWTO (14), | |
655 | EMPTY_HOWTO (15), | |
252b5132 RH |
656 | |
657 | /* A 5 bit shift field. */ | |
658 | HOWTO (R_MIPS_SHIFT5, /* type */ | |
659 | 0, /* rightshift */ | |
660 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
661 | 5, /* bitsize */ | |
662 | false, /* pc_relative */ | |
663 | 6, /* bitpos */ | |
664 | complain_overflow_bitfield, /* complain_on_overflow */ | |
665 | bfd_elf_generic_reloc, /* special_function */ | |
666 | "R_MIPS_SHIFT5", /* name */ | |
667 | true, /* partial_inplace */ | |
668 | 0x000007c0, /* src_mask */ | |
669 | 0x000007c0, /* dst_mask */ | |
670 | false), /* pcrel_offset */ | |
671 | ||
672 | /* A 6 bit shift field. */ | |
673 | /* FIXME: This is not handled correctly; a special function is | |
674 | needed to put the most significant bit in the right place. */ | |
675 | HOWTO (R_MIPS_SHIFT6, /* type */ | |
676 | 0, /* rightshift */ | |
677 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
678 | 6, /* bitsize */ | |
679 | false, /* pc_relative */ | |
680 | 6, /* bitpos */ | |
681 | complain_overflow_bitfield, /* complain_on_overflow */ | |
682 | bfd_elf_generic_reloc, /* special_function */ | |
683 | "R_MIPS_SHIFT6", /* name */ | |
684 | true, /* partial_inplace */ | |
685 | 0x000007c4, /* src_mask */ | |
686 | 0x000007c4, /* dst_mask */ | |
687 | false), /* pcrel_offset */ | |
688 | ||
a3c7651d | 689 | /* A 64 bit relocation. */ |
252b5132 RH |
690 | HOWTO (R_MIPS_64, /* type */ |
691 | 0, /* rightshift */ | |
a3c7651d MM |
692 | 4, /* size (0 = byte, 1 = short, 2 = long) */ |
693 | 64, /* bitsize */ | |
252b5132 RH |
694 | false, /* pc_relative */ |
695 | 0, /* bitpos */ | |
696 | complain_overflow_bitfield, /* complain_on_overflow */ | |
697 | mips32_64bit_reloc, /* special_function */ | |
698 | "R_MIPS_64", /* name */ | |
699 | true, /* partial_inplace */ | |
a3c7651d MM |
700 | MINUS_ONE, /* src_mask */ |
701 | MINUS_ONE, /* dst_mask */ | |
252b5132 RH |
702 | false), /* pcrel_offset */ |
703 | ||
704 | /* Displacement in the global offset table. */ | |
252b5132 RH |
705 | HOWTO (R_MIPS_GOT_DISP, /* type */ |
706 | 0, /* rightshift */ | |
707 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
708 | 16, /* bitsize */ | |
709 | false, /* pc_relative */ | |
710 | 0, /* bitpos */ | |
711 | complain_overflow_bitfield, /* complain_on_overflow */ | |
712 | bfd_elf_generic_reloc, /* special_function */ | |
713 | "R_MIPS_GOT_DISP", /* name */ | |
714 | true, /* partial_inplace */ | |
715 | 0x0000ffff, /* src_mask */ | |
716 | 0x0000ffff, /* dst_mask */ | |
717 | false), /* pcrel_offset */ | |
718 | ||
719 | /* Displacement to page pointer in the global offset table. */ | |
252b5132 RH |
720 | HOWTO (R_MIPS_GOT_PAGE, /* type */ |
721 | 0, /* rightshift */ | |
722 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
723 | 16, /* bitsize */ | |
724 | false, /* pc_relative */ | |
725 | 0, /* bitpos */ | |
726 | complain_overflow_bitfield, /* complain_on_overflow */ | |
727 | bfd_elf_generic_reloc, /* special_function */ | |
728 | "R_MIPS_GOT_PAGE", /* name */ | |
729 | true, /* partial_inplace */ | |
730 | 0x0000ffff, /* src_mask */ | |
731 | 0x0000ffff, /* dst_mask */ | |
732 | false), /* pcrel_offset */ | |
733 | ||
734 | /* Offset from page pointer in the global offset table. */ | |
252b5132 RH |
735 | HOWTO (R_MIPS_GOT_OFST, /* type */ |
736 | 0, /* rightshift */ | |
737 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
738 | 16, /* bitsize */ | |
739 | false, /* pc_relative */ | |
740 | 0, /* bitpos */ | |
741 | complain_overflow_bitfield, /* complain_on_overflow */ | |
742 | bfd_elf_generic_reloc, /* special_function */ | |
743 | "R_MIPS_GOT_OFST", /* name */ | |
744 | true, /* partial_inplace */ | |
745 | 0x0000ffff, /* src_mask */ | |
746 | 0x0000ffff, /* dst_mask */ | |
747 | false), /* pcrel_offset */ | |
748 | ||
749 | /* High 16 bits of displacement in global offset table. */ | |
252b5132 RH |
750 | HOWTO (R_MIPS_GOT_HI16, /* type */ |
751 | 0, /* rightshift */ | |
752 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
753 | 16, /* bitsize */ | |
754 | false, /* pc_relative */ | |
755 | 0, /* bitpos */ | |
756 | complain_overflow_dont, /* complain_on_overflow */ | |
757 | bfd_elf_generic_reloc, /* special_function */ | |
758 | "R_MIPS_GOT_HI16", /* name */ | |
759 | true, /* partial_inplace */ | |
760 | 0x0000ffff, /* src_mask */ | |
761 | 0x0000ffff, /* dst_mask */ | |
762 | false), /* pcrel_offset */ | |
763 | ||
764 | /* Low 16 bits of displacement in global offset table. */ | |
252b5132 RH |
765 | HOWTO (R_MIPS_GOT_LO16, /* type */ |
766 | 0, /* rightshift */ | |
767 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
768 | 16, /* bitsize */ | |
769 | false, /* pc_relative */ | |
770 | 0, /* bitpos */ | |
771 | complain_overflow_dont, /* complain_on_overflow */ | |
772 | bfd_elf_generic_reloc, /* special_function */ | |
773 | "R_MIPS_GOT_LO16", /* name */ | |
774 | true, /* partial_inplace */ | |
775 | 0x0000ffff, /* src_mask */ | |
776 | 0x0000ffff, /* dst_mask */ | |
777 | false), /* pcrel_offset */ | |
778 | ||
3f830999 | 779 | /* 64 bit subtraction. Used in the N32 ABI. */ |
3f830999 MM |
780 | HOWTO (R_MIPS_SUB, /* type */ |
781 | 0, /* rightshift */ | |
782 | 4, /* size (0 = byte, 1 = short, 2 = long) */ | |
783 | 64, /* bitsize */ | |
784 | false, /* pc_relative */ | |
785 | 0, /* bitpos */ | |
786 | complain_overflow_bitfield, /* complain_on_overflow */ | |
787 | bfd_elf_generic_reloc, /* special_function */ | |
788 | "R_MIPS_SUB", /* name */ | |
789 | true, /* partial_inplace */ | |
790 | MINUS_ONE, /* src_mask */ | |
791 | MINUS_ONE, /* dst_mask */ | |
792 | false), /* pcrel_offset */ | |
252b5132 RH |
793 | |
794 | /* Used to cause the linker to insert and delete instructions? */ | |
5f771d47 ILT |
795 | EMPTY_HOWTO (R_MIPS_INSERT_A), |
796 | EMPTY_HOWTO (R_MIPS_INSERT_B), | |
797 | EMPTY_HOWTO (R_MIPS_DELETE), | |
252b5132 | 798 | |
103186c6 MM |
799 | /* Get the higher value of a 64 bit addend. */ |
800 | HOWTO (R_MIPS_HIGHER, /* type */ | |
801 | 0, /* rightshift */ | |
802 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
803 | 16, /* bitsize */ | |
804 | false, /* pc_relative */ | |
805 | 0, /* bitpos */ | |
806 | complain_overflow_dont, /* complain_on_overflow */ | |
807 | bfd_elf_generic_reloc, /* special_function */ | |
808 | "R_MIPS_HIGHER", /* name */ | |
809 | true, /* partial_inplace */ | |
810 | 0, /* src_mask */ | |
811 | 0xffff, /* dst_mask */ | |
812 | false), /* pcrel_offset */ | |
813 | ||
814 | /* Get the highest value of a 64 bit addend. */ | |
815 | HOWTO (R_MIPS_HIGHEST, /* type */ | |
816 | 0, /* rightshift */ | |
817 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
818 | 16, /* bitsize */ | |
819 | false, /* pc_relative */ | |
820 | 0, /* bitpos */ | |
821 | complain_overflow_dont, /* complain_on_overflow */ | |
822 | bfd_elf_generic_reloc, /* special_function */ | |
823 | "R_MIPS_HIGHEST", /* name */ | |
824 | true, /* partial_inplace */ | |
825 | 0, /* src_mask */ | |
826 | 0xffff, /* dst_mask */ | |
827 | false), /* pcrel_offset */ | |
252b5132 RH |
828 | |
829 | /* High 16 bits of displacement in global offset table. */ | |
252b5132 RH |
830 | HOWTO (R_MIPS_CALL_HI16, /* type */ |
831 | 0, /* rightshift */ | |
832 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
833 | 16, /* bitsize */ | |
834 | false, /* pc_relative */ | |
835 | 0, /* bitpos */ | |
836 | complain_overflow_dont, /* complain_on_overflow */ | |
837 | bfd_elf_generic_reloc, /* special_function */ | |
838 | "R_MIPS_CALL_HI16", /* name */ | |
839 | true, /* partial_inplace */ | |
840 | 0x0000ffff, /* src_mask */ | |
841 | 0x0000ffff, /* dst_mask */ | |
842 | false), /* pcrel_offset */ | |
843 | ||
844 | /* Low 16 bits of displacement in global offset table. */ | |
252b5132 RH |
845 | HOWTO (R_MIPS_CALL_LO16, /* type */ |
846 | 0, /* rightshift */ | |
847 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
848 | 16, /* bitsize */ | |
849 | false, /* pc_relative */ | |
850 | 0, /* bitpos */ | |
851 | complain_overflow_dont, /* complain_on_overflow */ | |
852 | bfd_elf_generic_reloc, /* special_function */ | |
853 | "R_MIPS_CALL_LO16", /* name */ | |
854 | true, /* partial_inplace */ | |
855 | 0x0000ffff, /* src_mask */ | |
856 | 0x0000ffff, /* dst_mask */ | |
857 | false), /* pcrel_offset */ | |
858 | ||
7403cb63 MM |
859 | /* Section displacement. */ |
860 | HOWTO (R_MIPS_SCN_DISP, /* type */ | |
861 | 0, /* rightshift */ | |
862 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
863 | 32, /* bitsize */ | |
864 | false, /* pc_relative */ | |
865 | 0, /* bitpos */ | |
866 | complain_overflow_dont, /* complain_on_overflow */ | |
867 | bfd_elf_generic_reloc, /* special_function */ | |
868 | "R_MIPS_SCN_DISP", /* name */ | |
869 | false, /* partial_inplace */ | |
870 | 0xffffffff, /* src_mask */ | |
871 | 0xffffffff, /* dst_mask */ | |
872 | false), /* pcrel_offset */ | |
873 | ||
5f771d47 ILT |
874 | EMPTY_HOWTO (R_MIPS_REL16), |
875 | EMPTY_HOWTO (R_MIPS_ADD_IMMEDIATE), | |
876 | EMPTY_HOWTO (R_MIPS_PJUMP), | |
877 | EMPTY_HOWTO (R_MIPS_RELGOT), | |
d2905643 | 878 | |
be3ccd9c | 879 | /* Protected jump conversion. This is an optimization hint. No |
d2905643 MM |
880 | relocation is required for correctness. */ |
881 | HOWTO (R_MIPS_JALR, /* type */ | |
882 | 0, /* rightshift */ | |
883 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
884 | 0, /* bitsize */ | |
885 | false, /* pc_relative */ | |
886 | 0, /* bitpos */ | |
887 | complain_overflow_dont, /* complain_on_overflow */ | |
888 | bfd_elf_generic_reloc, /* special_function */ | |
889 | "R_MIPS_JALR", /* name */ | |
890 | false, /* partial_inplace */ | |
891 | 0x00000000, /* src_mask */ | |
892 | 0x00000000, /* dst_mask */ | |
893 | false), /* pcrel_offset */ | |
252b5132 RH |
894 | }; |
895 | ||
896 | /* The reloc used for BFD_RELOC_CTOR when doing a 64 bit link. This | |
897 | is a hack to make the linker think that we need 64 bit values. */ | |
898 | static reloc_howto_type elf_mips_ctor64_howto = | |
899 | HOWTO (R_MIPS_64, /* type */ | |
900 | 0, /* rightshift */ | |
901 | 4, /* size (0 = byte, 1 = short, 2 = long) */ | |
902 | 32, /* bitsize */ | |
903 | false, /* pc_relative */ | |
904 | 0, /* bitpos */ | |
905 | complain_overflow_signed, /* complain_on_overflow */ | |
906 | mips32_64bit_reloc, /* special_function */ | |
907 | "R_MIPS_64", /* name */ | |
908 | true, /* partial_inplace */ | |
909 | 0xffffffff, /* src_mask */ | |
910 | 0xffffffff, /* dst_mask */ | |
911 | false); /* pcrel_offset */ | |
912 | ||
913 | /* The reloc used for the mips16 jump instruction. */ | |
914 | static reloc_howto_type elf_mips16_jump_howto = | |
915 | HOWTO (R_MIPS16_26, /* type */ | |
916 | 2, /* rightshift */ | |
917 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
918 | 26, /* bitsize */ | |
919 | false, /* pc_relative */ | |
920 | 0, /* bitpos */ | |
921 | complain_overflow_dont, /* complain_on_overflow */ | |
922 | /* This needs complex overflow | |
923 | detection, because the upper four | |
924 | bits must match the PC. */ | |
925 | mips16_jump_reloc, /* special_function */ | |
926 | "R_MIPS16_26", /* name */ | |
927 | true, /* partial_inplace */ | |
928 | 0x3ffffff, /* src_mask */ | |
929 | 0x3ffffff, /* dst_mask */ | |
930 | false); /* pcrel_offset */ | |
931 | ||
b7233c24 | 932 | /* The reloc used for the mips16 gprel instruction. */ |
252b5132 RH |
933 | static reloc_howto_type elf_mips16_gprel_howto = |
934 | HOWTO (R_MIPS16_GPREL, /* type */ | |
935 | 0, /* rightshift */ | |
936 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
937 | 16, /* bitsize */ | |
938 | false, /* pc_relative */ | |
939 | 0, /* bitpos */ | |
940 | complain_overflow_signed, /* complain_on_overflow */ | |
941 | mips16_gprel_reloc, /* special_function */ | |
942 | "R_MIPS16_GPREL", /* name */ | |
943 | true, /* partial_inplace */ | |
b7233c24 MM |
944 | 0x07ff001f, /* src_mask */ |
945 | 0x07ff001f, /* dst_mask */ | |
252b5132 RH |
946 | false); /* pcrel_offset */ |
947 | ||
bb2d6cd7 GK |
948 | /* GNU extensions for embedded-pic. */ |
949 | /* High 16 bits of symbol value, pc-relative. */ | |
950 | static reloc_howto_type elf_mips_gnu_rel_hi16 = | |
951 | HOWTO (R_MIPS_GNU_REL_HI16, /* type */ | |
952 | 0, /* rightshift */ | |
953 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
954 | 16, /* bitsize */ | |
955 | true, /* pc_relative */ | |
956 | 0, /* bitpos */ | |
957 | complain_overflow_dont, /* complain_on_overflow */ | |
958 | _bfd_mips_elf_hi16_reloc, /* special_function */ | |
959 | "R_MIPS_GNU_REL_HI16", /* name */ | |
960 | true, /* partial_inplace */ | |
961 | 0xffff, /* src_mask */ | |
962 | 0xffff, /* dst_mask */ | |
963 | true); /* pcrel_offset */ | |
964 | ||
965 | /* Low 16 bits of symbol value, pc-relative. */ | |
966 | static reloc_howto_type elf_mips_gnu_rel_lo16 = | |
967 | HOWTO (R_MIPS_GNU_REL_LO16, /* type */ | |
968 | 0, /* rightshift */ | |
969 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
970 | 16, /* bitsize */ | |
971 | true, /* pc_relative */ | |
972 | 0, /* bitpos */ | |
973 | complain_overflow_dont, /* complain_on_overflow */ | |
974 | _bfd_mips_elf_lo16_reloc, /* special_function */ | |
975 | "R_MIPS_GNU_REL_LO16", /* name */ | |
976 | true, /* partial_inplace */ | |
977 | 0xffff, /* src_mask */ | |
978 | 0xffff, /* dst_mask */ | |
979 | true); /* pcrel_offset */ | |
980 | ||
981 | /* 16 bit offset for pc-relative branches. */ | |
982 | static reloc_howto_type elf_mips_gnu_rel16_s2 = | |
983 | HOWTO (R_MIPS_GNU_REL16_S2, /* type */ | |
984 | 2, /* rightshift */ | |
985 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
986 | 16, /* bitsize */ | |
987 | true, /* pc_relative */ | |
988 | 0, /* bitpos */ | |
989 | complain_overflow_signed, /* complain_on_overflow */ | |
990 | bfd_elf_generic_reloc, /* special_function */ | |
991 | "R_MIPS_GNU_REL16_S2", /* name */ | |
992 | true, /* partial_inplace */ | |
993 | 0xffff, /* src_mask */ | |
994 | 0xffff, /* dst_mask */ | |
995 | true); /* pcrel_offset */ | |
996 | ||
997 | /* 64 bit pc-relative. */ | |
998 | static reloc_howto_type elf_mips_gnu_pcrel64 = | |
999 | HOWTO (R_MIPS_PC64, /* type */ | |
1000 | 0, /* rightshift */ | |
1001 | 4, /* size (0 = byte, 1 = short, 2 = long) */ | |
1002 | 64, /* bitsize */ | |
1003 | true, /* pc_relative */ | |
1004 | 0, /* bitpos */ | |
1005 | complain_overflow_signed, /* complain_on_overflow */ | |
1006 | bfd_elf_generic_reloc, /* special_function */ | |
1007 | "R_MIPS_PC64", /* name */ | |
1008 | true, /* partial_inplace */ | |
1009 | MINUS_ONE, /* src_mask */ | |
1010 | MINUS_ONE, /* dst_mask */ | |
1011 | true); /* pcrel_offset */ | |
1012 | ||
1013 | /* 32 bit pc-relative. */ | |
1014 | static reloc_howto_type elf_mips_gnu_pcrel32 = | |
1015 | HOWTO (R_MIPS_PC32, /* type */ | |
1016 | 0, /* rightshift */ | |
1017 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
1018 | 32, /* bitsize */ | |
1019 | true, /* pc_relative */ | |
1020 | 0, /* bitpos */ | |
1021 | complain_overflow_signed, /* complain_on_overflow */ | |
1022 | bfd_elf_generic_reloc, /* special_function */ | |
1023 | "R_MIPS_PC32", /* name */ | |
1024 | true, /* partial_inplace */ | |
1025 | 0xffffffff, /* src_mask */ | |
1026 | 0xffffffff, /* dst_mask */ | |
1027 | true); /* pcrel_offset */ | |
1028 | ||
252b5132 RH |
1029 | /* GNU extension to record C++ vtable hierarchy */ |
1030 | static reloc_howto_type elf_mips_gnu_vtinherit_howto = | |
1031 | HOWTO (R_MIPS_GNU_VTINHERIT, /* type */ | |
1032 | 0, /* rightshift */ | |
1033 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
1034 | 0, /* bitsize */ | |
1035 | false, /* pc_relative */ | |
1036 | 0, /* bitpos */ | |
1037 | complain_overflow_dont, /* complain_on_overflow */ | |
1038 | NULL, /* special_function */ | |
1039 | "R_MIPS_GNU_VTINHERIT", /* name */ | |
1040 | false, /* partial_inplace */ | |
1041 | 0, /* src_mask */ | |
1042 | 0, /* dst_mask */ | |
1043 | false); /* pcrel_offset */ | |
1044 | ||
1045 | /* GNU extension to record C++ vtable member usage */ | |
1046 | static reloc_howto_type elf_mips_gnu_vtentry_howto = | |
1047 | HOWTO (R_MIPS_GNU_VTENTRY, /* type */ | |
1048 | 0, /* rightshift */ | |
1049 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
1050 | 0, /* bitsize */ | |
1051 | false, /* pc_relative */ | |
1052 | 0, /* bitpos */ | |
1053 | complain_overflow_dont, /* complain_on_overflow */ | |
1054 | _bfd_elf_rel_vtable_reloc_fn, /* special_function */ | |
1055 | "R_MIPS_GNU_VTENTRY", /* name */ | |
1056 | false, /* partial_inplace */ | |
1057 | 0, /* src_mask */ | |
1058 | 0, /* dst_mask */ | |
1059 | false); /* pcrel_offset */ | |
1060 | ||
1061 | /* Do a R_MIPS_HI16 relocation. This has to be done in combination | |
1062 | with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to | |
1063 | the HI16. Here we just save the information we need; we do the | |
1064 | actual relocation when we see the LO16. MIPS ELF requires that the | |
1065 | LO16 immediately follow the HI16. As a GNU extension, we permit an | |
1066 | arbitrary number of HI16 relocs to be associated with a single LO16 | |
1067 | reloc. This extension permits gcc to output the HI and LO relocs | |
1068 | itself. */ | |
1069 | ||
be3ccd9c | 1070 | struct mips_hi16 { |
252b5132 RH |
1071 | struct mips_hi16 *next; |
1072 | bfd_byte *addr; | |
1073 | bfd_vma addend; | |
1074 | }; | |
1075 | ||
1076 | /* FIXME: This should not be a static variable. */ | |
1077 | ||
1078 | static struct mips_hi16 *mips_hi16_list; | |
1079 | ||
1080 | bfd_reloc_status_type | |
1081 | _bfd_mips_elf_hi16_reloc (abfd, | |
1082 | reloc_entry, | |
1083 | symbol, | |
1084 | data, | |
1085 | input_section, | |
1086 | output_bfd, | |
1087 | error_message) | |
5f771d47 | 1088 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
1089 | arelent *reloc_entry; |
1090 | asymbol *symbol; | |
1091 | PTR data; | |
1092 | asection *input_section; | |
1093 | bfd *output_bfd; | |
1094 | char **error_message; | |
1095 | { | |
1096 | bfd_reloc_status_type ret; | |
1097 | bfd_vma relocation; | |
1098 | struct mips_hi16 *n; | |
1099 | ||
1100 | /* If we're relocating, and this an external symbol, we don't want | |
1101 | to change anything. */ | |
1102 | if (output_bfd != (bfd *) NULL | |
1103 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
1104 | && reloc_entry->addend == 0) | |
1105 | { | |
1106 | reloc_entry->address += input_section->output_offset; | |
1107 | return bfd_reloc_ok; | |
1108 | } | |
1109 | ||
1110 | ret = bfd_reloc_ok; | |
1111 | ||
1112 | if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0) | |
1113 | { | |
1114 | boolean relocateable; | |
1115 | bfd_vma gp; | |
1116 | ||
1117 | if (ret == bfd_reloc_undefined) | |
1118 | abort (); | |
1119 | ||
1120 | if (output_bfd != NULL) | |
1121 | relocateable = true; | |
1122 | else | |
1123 | { | |
1124 | relocateable = false; | |
1125 | output_bfd = symbol->section->output_section->owner; | |
1126 | } | |
1127 | ||
1128 | ret = mips_elf_final_gp (output_bfd, symbol, relocateable, | |
1129 | error_message, &gp); | |
1130 | if (ret != bfd_reloc_ok) | |
1131 | return ret; | |
1132 | ||
1133 | relocation = gp - reloc_entry->address; | |
1134 | } | |
1135 | else | |
1136 | { | |
1137 | if (bfd_is_und_section (symbol->section) | |
1138 | && output_bfd == (bfd *) NULL) | |
1139 | ret = bfd_reloc_undefined; | |
1140 | ||
1141 | if (bfd_is_com_section (symbol->section)) | |
1142 | relocation = 0; | |
1143 | else | |
1144 | relocation = symbol->value; | |
1145 | } | |
1146 | ||
1147 | relocation += symbol->section->output_section->vma; | |
1148 | relocation += symbol->section->output_offset; | |
1149 | relocation += reloc_entry->addend; | |
1150 | ||
1151 | if (reloc_entry->address > input_section->_cooked_size) | |
1152 | return bfd_reloc_outofrange; | |
1153 | ||
1154 | /* Save the information, and let LO16 do the actual relocation. */ | |
1155 | n = (struct mips_hi16 *) bfd_malloc (sizeof *n); | |
1156 | if (n == NULL) | |
1157 | return bfd_reloc_outofrange; | |
1158 | n->addr = (bfd_byte *) data + reloc_entry->address; | |
1159 | n->addend = relocation; | |
1160 | n->next = mips_hi16_list; | |
1161 | mips_hi16_list = n; | |
1162 | ||
1163 | if (output_bfd != (bfd *) NULL) | |
1164 | reloc_entry->address += input_section->output_offset; | |
1165 | ||
1166 | return ret; | |
1167 | } | |
1168 | ||
1169 | /* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit | |
1170 | inplace relocation; this function exists in order to do the | |
1171 | R_MIPS_HI16 relocation described above. */ | |
1172 | ||
1173 | bfd_reloc_status_type | |
1174 | _bfd_mips_elf_lo16_reloc (abfd, | |
1175 | reloc_entry, | |
1176 | symbol, | |
1177 | data, | |
1178 | input_section, | |
1179 | output_bfd, | |
1180 | error_message) | |
1181 | bfd *abfd; | |
1182 | arelent *reloc_entry; | |
1183 | asymbol *symbol; | |
1184 | PTR data; | |
1185 | asection *input_section; | |
1186 | bfd *output_bfd; | |
1187 | char **error_message; | |
1188 | { | |
1189 | arelent gp_disp_relent; | |
1190 | ||
1191 | if (mips_hi16_list != NULL) | |
1192 | { | |
1193 | struct mips_hi16 *l; | |
1194 | ||
1195 | l = mips_hi16_list; | |
1196 | while (l != NULL) | |
1197 | { | |
1198 | unsigned long insn; | |
1199 | unsigned long val; | |
1200 | unsigned long vallo; | |
1201 | struct mips_hi16 *next; | |
1202 | ||
1203 | /* Do the HI16 relocation. Note that we actually don't need | |
1204 | to know anything about the LO16 itself, except where to | |
1205 | find the low 16 bits of the addend needed by the LO16. */ | |
1206 | insn = bfd_get_32 (abfd, l->addr); | |
1207 | vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address) | |
1208 | & 0xffff); | |
1209 | val = ((insn & 0xffff) << 16) + vallo; | |
1210 | val += l->addend; | |
1211 | ||
1212 | /* The low order 16 bits are always treated as a signed | |
1213 | value. Therefore, a negative value in the low order bits | |
1214 | requires an adjustment in the high order bits. We need | |
1215 | to make this adjustment in two ways: once for the bits we | |
1216 | took from the data, and once for the bits we are putting | |
1217 | back in to the data. */ | |
1218 | if ((vallo & 0x8000) != 0) | |
1219 | val -= 0x10000; | |
1220 | if ((val & 0x8000) != 0) | |
1221 | val += 0x10000; | |
1222 | ||
be3ccd9c | 1223 | insn = (insn & ~0xffff) | ((val >> 16) & 0xffff); |
252b5132 RH |
1224 | bfd_put_32 (abfd, insn, l->addr); |
1225 | ||
1226 | if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0) | |
1227 | { | |
1228 | gp_disp_relent = *reloc_entry; | |
1229 | reloc_entry = &gp_disp_relent; | |
1230 | reloc_entry->addend = l->addend; | |
1231 | } | |
1232 | ||
1233 | next = l->next; | |
1234 | free (l); | |
1235 | l = next; | |
1236 | } | |
1237 | ||
1238 | mips_hi16_list = NULL; | |
1239 | } | |
1240 | else if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0) | |
1241 | { | |
1242 | bfd_reloc_status_type ret; | |
1243 | bfd_vma gp, relocation; | |
1244 | ||
1245 | /* FIXME: Does this case ever occur? */ | |
1246 | ||
1247 | ret = mips_elf_final_gp (output_bfd, symbol, true, error_message, &gp); | |
1248 | if (ret != bfd_reloc_ok) | |
1249 | return ret; | |
1250 | ||
1251 | relocation = gp - reloc_entry->address; | |
1252 | relocation += symbol->section->output_section->vma; | |
1253 | relocation += symbol->section->output_offset; | |
1254 | relocation += reloc_entry->addend; | |
1255 | ||
1256 | if (reloc_entry->address > input_section->_cooked_size) | |
1257 | return bfd_reloc_outofrange; | |
1258 | ||
1259 | gp_disp_relent = *reloc_entry; | |
1260 | reloc_entry = &gp_disp_relent; | |
1261 | reloc_entry->addend = relocation - 4; | |
1262 | } | |
1263 | ||
1264 | /* Now do the LO16 reloc in the usual way. */ | |
1265 | return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data, | |
1266 | input_section, output_bfd, error_message); | |
1267 | } | |
1268 | ||
1269 | /* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset | |
1270 | table used for PIC code. If the symbol is an external symbol, the | |
1271 | instruction is modified to contain the offset of the appropriate | |
1272 | entry in the global offset table. If the symbol is a section | |
1273 | symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit | |
1274 | addends are combined to form the real addend against the section | |
1275 | symbol; the GOT16 is modified to contain the offset of an entry in | |
1276 | the global offset table, and the LO16 is modified to offset it | |
1277 | appropriately. Thus an offset larger than 16 bits requires a | |
1278 | modified value in the global offset table. | |
1279 | ||
1280 | This implementation suffices for the assembler, but the linker does | |
1281 | not yet know how to create global offset tables. */ | |
1282 | ||
1283 | bfd_reloc_status_type | |
1284 | _bfd_mips_elf_got16_reloc (abfd, | |
1285 | reloc_entry, | |
1286 | symbol, | |
1287 | data, | |
1288 | input_section, | |
1289 | output_bfd, | |
1290 | error_message) | |
1291 | bfd *abfd; | |
1292 | arelent *reloc_entry; | |
1293 | asymbol *symbol; | |
1294 | PTR data; | |
1295 | asection *input_section; | |
1296 | bfd *output_bfd; | |
1297 | char **error_message; | |
1298 | { | |
1299 | /* If we're relocating, and this an external symbol, we don't want | |
1300 | to change anything. */ | |
1301 | if (output_bfd != (bfd *) NULL | |
1302 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
1303 | && reloc_entry->addend == 0) | |
1304 | { | |
1305 | reloc_entry->address += input_section->output_offset; | |
1306 | return bfd_reloc_ok; | |
1307 | } | |
1308 | ||
1309 | /* If we're relocating, and this is a local symbol, we can handle it | |
1310 | just like HI16. */ | |
1311 | if (output_bfd != (bfd *) NULL | |
1312 | && (symbol->flags & BSF_SECTION_SYM) != 0) | |
1313 | return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data, | |
1314 | input_section, output_bfd, error_message); | |
1315 | ||
1316 | abort (); | |
1317 | } | |
1318 | ||
7403cb63 MM |
1319 | /* Set the GP value for OUTPUT_BFD. Returns false if this is a |
1320 | dangerous relocation. */ | |
1321 | ||
1322 | static boolean | |
1323 | mips_elf_assign_gp (output_bfd, pgp) | |
1324 | bfd *output_bfd; | |
1325 | bfd_vma *pgp; | |
1326 | { | |
1327 | unsigned int count; | |
1328 | asymbol **sym; | |
1329 | unsigned int i; | |
1330 | ||
1331 | /* If we've already figured out what GP will be, just return it. */ | |
1332 | *pgp = _bfd_get_gp_value (output_bfd); | |
1333 | if (*pgp) | |
1334 | return true; | |
1335 | ||
1336 | count = bfd_get_symcount (output_bfd); | |
1337 | sym = bfd_get_outsymbols (output_bfd); | |
1338 | ||
1339 | /* The linker script will have created a symbol named `_gp' with the | |
1340 | appropriate value. */ | |
1341 | if (sym == (asymbol **) NULL) | |
1342 | i = count; | |
1343 | else | |
1344 | { | |
1345 | for (i = 0; i < count; i++, sym++) | |
1346 | { | |
1347 | register CONST char *name; | |
1348 | ||
1349 | name = bfd_asymbol_name (*sym); | |
1350 | if (*name == '_' && strcmp (name, "_gp") == 0) | |
1351 | { | |
1352 | *pgp = bfd_asymbol_value (*sym); | |
1353 | _bfd_set_gp_value (output_bfd, *pgp); | |
1354 | break; | |
1355 | } | |
1356 | } | |
1357 | } | |
1358 | ||
1359 | if (i >= count) | |
1360 | { | |
1361 | /* Only get the error once. */ | |
1362 | *pgp = 4; | |
1363 | _bfd_set_gp_value (output_bfd, *pgp); | |
1364 | return false; | |
1365 | } | |
1366 | ||
1367 | return true; | |
1368 | } | |
1369 | ||
252b5132 RH |
1370 | /* We have to figure out the gp value, so that we can adjust the |
1371 | symbol value correctly. We look up the symbol _gp in the output | |
1372 | BFD. If we can't find it, we're stuck. We cache it in the ELF | |
1373 | target data. We don't need to adjust the symbol value for an | |
1374 | external symbol if we are producing relocateable output. */ | |
1375 | ||
1376 | static bfd_reloc_status_type | |
1377 | mips_elf_final_gp (output_bfd, symbol, relocateable, error_message, pgp) | |
1378 | bfd *output_bfd; | |
1379 | asymbol *symbol; | |
1380 | boolean relocateable; | |
1381 | char **error_message; | |
1382 | bfd_vma *pgp; | |
1383 | { | |
1384 | if (bfd_is_und_section (symbol->section) | |
1385 | && ! relocateable) | |
1386 | { | |
1387 | *pgp = 0; | |
1388 | return bfd_reloc_undefined; | |
1389 | } | |
1390 | ||
1391 | *pgp = _bfd_get_gp_value (output_bfd); | |
1392 | if (*pgp == 0 | |
1393 | && (! relocateable | |
1394 | || (symbol->flags & BSF_SECTION_SYM) != 0)) | |
1395 | { | |
1396 | if (relocateable) | |
1397 | { | |
1398 | /* Make up a value. */ | |
1399 | *pgp = symbol->section->output_section->vma + 0x4000; | |
1400 | _bfd_set_gp_value (output_bfd, *pgp); | |
1401 | } | |
7403cb63 | 1402 | else if (!mips_elf_assign_gp (output_bfd, pgp)) |
252b5132 | 1403 | { |
7403cb63 MM |
1404 | *error_message = |
1405 | (char *) _("GP relative relocation when _gp not defined"); | |
1406 | return bfd_reloc_dangerous; | |
252b5132 RH |
1407 | } |
1408 | } | |
1409 | ||
1410 | return bfd_reloc_ok; | |
1411 | } | |
1412 | ||
1413 | /* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must | |
1414 | become the offset from the gp register. This function also handles | |
1415 | R_MIPS_LITERAL relocations, although those can be handled more | |
1416 | cleverly because the entries in the .lit8 and .lit4 sections can be | |
1417 | merged. */ | |
1418 | ||
1419 | static bfd_reloc_status_type gprel16_with_gp PARAMS ((bfd *, asymbol *, | |
1420 | arelent *, asection *, | |
1421 | boolean, PTR, bfd_vma)); | |
1422 | ||
1423 | bfd_reloc_status_type | |
1424 | _bfd_mips_elf_gprel16_reloc (abfd, reloc_entry, symbol, data, input_section, | |
1425 | output_bfd, error_message) | |
1426 | bfd *abfd; | |
1427 | arelent *reloc_entry; | |
1428 | asymbol *symbol; | |
1429 | PTR data; | |
1430 | asection *input_section; | |
1431 | bfd *output_bfd; | |
1432 | char **error_message; | |
1433 | { | |
1434 | boolean relocateable; | |
1435 | bfd_reloc_status_type ret; | |
1436 | bfd_vma gp; | |
1437 | ||
1438 | /* If we're relocating, and this is an external symbol with no | |
1439 | addend, we don't want to change anything. We will only have an | |
1440 | addend if this is a newly created reloc, not read from an ELF | |
1441 | file. */ | |
1442 | if (output_bfd != (bfd *) NULL | |
1443 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
1444 | && reloc_entry->addend == 0) | |
1445 | { | |
1446 | reloc_entry->address += input_section->output_offset; | |
1447 | return bfd_reloc_ok; | |
1448 | } | |
1449 | ||
1450 | if (output_bfd != (bfd *) NULL) | |
1451 | relocateable = true; | |
1452 | else | |
1453 | { | |
1454 | relocateable = false; | |
1455 | output_bfd = symbol->section->output_section->owner; | |
1456 | } | |
1457 | ||
1458 | ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message, | |
1459 | &gp); | |
1460 | if (ret != bfd_reloc_ok) | |
1461 | return ret; | |
1462 | ||
1463 | return gprel16_with_gp (abfd, symbol, reloc_entry, input_section, | |
1464 | relocateable, data, gp); | |
1465 | } | |
1466 | ||
1467 | static bfd_reloc_status_type | |
1468 | gprel16_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data, | |
1469 | gp) | |
1470 | bfd *abfd; | |
1471 | asymbol *symbol; | |
1472 | arelent *reloc_entry; | |
1473 | asection *input_section; | |
1474 | boolean relocateable; | |
1475 | PTR data; | |
1476 | bfd_vma gp; | |
1477 | { | |
1478 | bfd_vma relocation; | |
1479 | unsigned long insn; | |
1480 | unsigned long val; | |
1481 | ||
1482 | if (bfd_is_com_section (symbol->section)) | |
1483 | relocation = 0; | |
1484 | else | |
1485 | relocation = symbol->value; | |
1486 | ||
1487 | relocation += symbol->section->output_section->vma; | |
1488 | relocation += symbol->section->output_offset; | |
1489 | ||
1490 | if (reloc_entry->address > input_section->_cooked_size) | |
1491 | return bfd_reloc_outofrange; | |
1492 | ||
1493 | insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address); | |
1494 | ||
1495 | /* Set val to the offset into the section or symbol. */ | |
1496 | if (reloc_entry->howto->src_mask == 0) | |
1497 | { | |
1498 | /* This case occurs with the 64-bit MIPS ELF ABI. */ | |
1499 | val = reloc_entry->addend; | |
1500 | } | |
1501 | else | |
1502 | { | |
1503 | val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff; | |
1504 | if (val & 0x8000) | |
1505 | val -= 0x10000; | |
1506 | } | |
1507 | ||
1508 | /* Adjust val for the final section location and GP value. If we | |
1509 | are producing relocateable output, we don't want to do this for | |
1510 | an external symbol. */ | |
1511 | if (! relocateable | |
1512 | || (symbol->flags & BSF_SECTION_SYM) != 0) | |
1513 | val += relocation - gp; | |
1514 | ||
be3ccd9c | 1515 | insn = (insn & ~0xffff) | (val & 0xffff); |
252b5132 RH |
1516 | bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address); |
1517 | ||
1518 | if (relocateable) | |
1519 | reloc_entry->address += input_section->output_offset; | |
1520 | ||
1521 | /* Make sure it fit in 16 bits. */ | |
43cbcf28 | 1522 | if ((long) val >= 0x8000 || (long) val < -0x8000) |
252b5132 RH |
1523 | return bfd_reloc_overflow; |
1524 | ||
1525 | return bfd_reloc_ok; | |
1526 | } | |
1527 | ||
1528 | /* Do a R_MIPS_GPREL32 relocation. Is this 32 bit value the offset | |
1529 | from the gp register? XXX */ | |
1530 | ||
1531 | static bfd_reloc_status_type gprel32_with_gp PARAMS ((bfd *, asymbol *, | |
1532 | arelent *, asection *, | |
1533 | boolean, PTR, bfd_vma)); | |
1534 | ||
1535 | bfd_reloc_status_type | |
1536 | _bfd_mips_elf_gprel32_reloc (abfd, | |
1537 | reloc_entry, | |
1538 | symbol, | |
1539 | data, | |
1540 | input_section, | |
1541 | output_bfd, | |
1542 | error_message) | |
1543 | bfd *abfd; | |
1544 | arelent *reloc_entry; | |
1545 | asymbol *symbol; | |
1546 | PTR data; | |
1547 | asection *input_section; | |
1548 | bfd *output_bfd; | |
1549 | char **error_message; | |
1550 | { | |
1551 | boolean relocateable; | |
1552 | bfd_reloc_status_type ret; | |
1553 | bfd_vma gp; | |
1554 | ||
1555 | /* If we're relocating, and this is an external symbol with no | |
1556 | addend, we don't want to change anything. We will only have an | |
1557 | addend if this is a newly created reloc, not read from an ELF | |
1558 | file. */ | |
1559 | if (output_bfd != (bfd *) NULL | |
1560 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
1561 | && reloc_entry->addend == 0) | |
1562 | { | |
1563 | *error_message = (char *) | |
1564 | _("32bits gp relative relocation occurs for an external symbol"); | |
1565 | return bfd_reloc_outofrange; | |
1566 | } | |
1567 | ||
1568 | if (output_bfd != (bfd *) NULL) | |
1569 | { | |
1570 | relocateable = true; | |
1571 | gp = _bfd_get_gp_value (output_bfd); | |
1572 | } | |
1573 | else | |
1574 | { | |
1575 | relocateable = false; | |
1576 | output_bfd = symbol->section->output_section->owner; | |
1577 | ||
1578 | ret = mips_elf_final_gp (output_bfd, symbol, relocateable, | |
1579 | error_message, &gp); | |
1580 | if (ret != bfd_reloc_ok) | |
1581 | return ret; | |
1582 | } | |
1583 | ||
1584 | return gprel32_with_gp (abfd, symbol, reloc_entry, input_section, | |
1585 | relocateable, data, gp); | |
1586 | } | |
1587 | ||
1588 | static bfd_reloc_status_type | |
1589 | gprel32_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data, | |
1590 | gp) | |
1591 | bfd *abfd; | |
1592 | asymbol *symbol; | |
1593 | arelent *reloc_entry; | |
1594 | asection *input_section; | |
1595 | boolean relocateable; | |
1596 | PTR data; | |
1597 | bfd_vma gp; | |
1598 | { | |
1599 | bfd_vma relocation; | |
1600 | unsigned long val; | |
1601 | ||
1602 | if (bfd_is_com_section (symbol->section)) | |
1603 | relocation = 0; | |
1604 | else | |
1605 | relocation = symbol->value; | |
1606 | ||
1607 | relocation += symbol->section->output_section->vma; | |
1608 | relocation += symbol->section->output_offset; | |
1609 | ||
1610 | if (reloc_entry->address > input_section->_cooked_size) | |
1611 | return bfd_reloc_outofrange; | |
1612 | ||
1613 | if (reloc_entry->howto->src_mask == 0) | |
1614 | { | |
1615 | /* This case arises with the 64-bit MIPS ELF ABI. */ | |
1616 | val = 0; | |
1617 | } | |
1618 | else | |
1619 | val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address); | |
1620 | ||
1621 | /* Set val to the offset into the section or symbol. */ | |
1622 | val += reloc_entry->addend; | |
1623 | ||
1624 | /* Adjust val for the final section location and GP value. If we | |
1625 | are producing relocateable output, we don't want to do this for | |
1626 | an external symbol. */ | |
1627 | if (! relocateable | |
1628 | || (symbol->flags & BSF_SECTION_SYM) != 0) | |
1629 | val += relocation - gp; | |
1630 | ||
1631 | bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address); | |
1632 | ||
1633 | if (relocateable) | |
1634 | reloc_entry->address += input_section->output_offset; | |
1635 | ||
1636 | return bfd_reloc_ok; | |
1637 | } | |
1638 | ||
1639 | /* Handle a 64 bit reloc in a 32 bit MIPS ELF file. These are | |
1640 | generated when addreses are 64 bits. The upper 32 bits are a simle | |
1641 | sign extension. */ | |
1642 | ||
1643 | static bfd_reloc_status_type | |
1644 | mips32_64bit_reloc (abfd, reloc_entry, symbol, data, input_section, | |
1645 | output_bfd, error_message) | |
1646 | bfd *abfd; | |
1647 | arelent *reloc_entry; | |
1648 | asymbol *symbol; | |
1649 | PTR data; | |
1650 | asection *input_section; | |
1651 | bfd *output_bfd; | |
1652 | char **error_message; | |
1653 | { | |
1654 | bfd_reloc_status_type r; | |
1655 | arelent reloc32; | |
1656 | unsigned long val; | |
1657 | bfd_size_type addr; | |
1658 | ||
1659 | r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data, | |
1660 | input_section, output_bfd, error_message); | |
1661 | if (r != bfd_reloc_continue) | |
1662 | return r; | |
1663 | ||
1664 | /* Do a normal 32 bit relocation on the lower 32 bits. */ | |
1665 | reloc32 = *reloc_entry; | |
1666 | if (bfd_big_endian (abfd)) | |
1667 | reloc32.address += 4; | |
1668 | reloc32.howto = &elf_mips_howto_table[R_MIPS_32]; | |
1669 | r = bfd_perform_relocation (abfd, &reloc32, data, input_section, | |
1670 | output_bfd, error_message); | |
1671 | ||
1672 | /* Sign extend into the upper 32 bits. */ | |
1673 | val = bfd_get_32 (abfd, (bfd_byte *) data + reloc32.address); | |
1674 | if ((val & 0x80000000) != 0) | |
1675 | val = 0xffffffff; | |
1676 | else | |
1677 | val = 0; | |
1678 | addr = reloc_entry->address; | |
1679 | if (bfd_little_endian (abfd)) | |
1680 | addr += 4; | |
1681 | bfd_put_32 (abfd, val, (bfd_byte *) data + addr); | |
1682 | ||
1683 | return r; | |
1684 | } | |
1685 | ||
1686 | /* Handle a mips16 jump. */ | |
1687 | ||
1688 | static bfd_reloc_status_type | |
1689 | mips16_jump_reloc (abfd, reloc_entry, symbol, data, input_section, | |
1690 | output_bfd, error_message) | |
5f771d47 | 1691 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
1692 | arelent *reloc_entry; |
1693 | asymbol *symbol; | |
5f771d47 | 1694 | PTR data ATTRIBUTE_UNUSED; |
252b5132 RH |
1695 | asection *input_section; |
1696 | bfd *output_bfd; | |
5f771d47 | 1697 | char **error_message ATTRIBUTE_UNUSED; |
252b5132 RH |
1698 | { |
1699 | if (output_bfd != (bfd *) NULL | |
1700 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
1701 | && reloc_entry->addend == 0) | |
1702 | { | |
1703 | reloc_entry->address += input_section->output_offset; | |
1704 | return bfd_reloc_ok; | |
1705 | } | |
1706 | ||
1707 | /* FIXME. */ | |
1708 | { | |
1709 | static boolean warned; | |
1710 | ||
1711 | if (! warned) | |
1712 | (*_bfd_error_handler) | |
1713 | (_("Linking mips16 objects into %s format is not supported"), | |
1714 | bfd_get_target (input_section->output_section->owner)); | |
1715 | warned = true; | |
1716 | } | |
1717 | ||
1718 | return bfd_reloc_undefined; | |
1719 | } | |
1720 | ||
1721 | /* Handle a mips16 GP relative reloc. */ | |
1722 | ||
1723 | static bfd_reloc_status_type | |
1724 | mips16_gprel_reloc (abfd, reloc_entry, symbol, data, input_section, | |
1725 | output_bfd, error_message) | |
1726 | bfd *abfd; | |
1727 | arelent *reloc_entry; | |
1728 | asymbol *symbol; | |
1729 | PTR data; | |
1730 | asection *input_section; | |
1731 | bfd *output_bfd; | |
1732 | char **error_message; | |
1733 | { | |
1734 | boolean relocateable; | |
1735 | bfd_reloc_status_type ret; | |
1736 | bfd_vma gp; | |
1737 | unsigned short extend, insn; | |
1738 | unsigned long final; | |
1739 | ||
1740 | /* If we're relocating, and this is an external symbol with no | |
1741 | addend, we don't want to change anything. We will only have an | |
1742 | addend if this is a newly created reloc, not read from an ELF | |
1743 | file. */ | |
1744 | if (output_bfd != NULL | |
1745 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
1746 | && reloc_entry->addend == 0) | |
1747 | { | |
1748 | reloc_entry->address += input_section->output_offset; | |
1749 | return bfd_reloc_ok; | |
1750 | } | |
1751 | ||
1752 | if (output_bfd != NULL) | |
1753 | relocateable = true; | |
1754 | else | |
1755 | { | |
1756 | relocateable = false; | |
1757 | output_bfd = symbol->section->output_section->owner; | |
1758 | } | |
1759 | ||
1760 | ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message, | |
1761 | &gp); | |
1762 | if (ret != bfd_reloc_ok) | |
1763 | return ret; | |
1764 | ||
1765 | if (reloc_entry->address > input_section->_cooked_size) | |
1766 | return bfd_reloc_outofrange; | |
1767 | ||
1768 | /* Pick up the mips16 extend instruction and the real instruction. */ | |
1769 | extend = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address); | |
1770 | insn = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address + 2); | |
1771 | ||
1772 | /* Stuff the current addend back as a 32 bit value, do the usual | |
1773 | relocation, and then clean up. */ | |
1774 | bfd_put_32 (abfd, | |
1775 | (((extend & 0x1f) << 11) | |
1776 | | (extend & 0x7e0) | |
1777 | | (insn & 0x1f)), | |
1778 | (bfd_byte *) data + reloc_entry->address); | |
1779 | ||
1780 | ret = gprel16_with_gp (abfd, symbol, reloc_entry, input_section, | |
1781 | relocateable, data, gp); | |
1782 | ||
1783 | final = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address); | |
1784 | bfd_put_16 (abfd, | |
1785 | ((extend & 0xf800) | |
1786 | | ((final >> 11) & 0x1f) | |
1787 | | (final & 0x7e0)), | |
1788 | (bfd_byte *) data + reloc_entry->address); | |
1789 | bfd_put_16 (abfd, | |
1790 | ((insn & 0xffe0) | |
1791 | | (final & 0x1f)), | |
1792 | (bfd_byte *) data + reloc_entry->address + 2); | |
1793 | ||
1794 | return ret; | |
1795 | } | |
1796 | ||
1797 | /* Return the ISA for a MIPS e_flags value. */ | |
1798 | ||
1799 | static INLINE int | |
1800 | elf_mips_isa (flags) | |
1801 | flagword flags; | |
1802 | { | |
1803 | switch (flags & EF_MIPS_ARCH) | |
1804 | { | |
1805 | case E_MIPS_ARCH_1: | |
1806 | return 1; | |
1807 | case E_MIPS_ARCH_2: | |
1808 | return 2; | |
1809 | case E_MIPS_ARCH_3: | |
1810 | return 3; | |
1811 | case E_MIPS_ARCH_4: | |
1812 | return 4; | |
84ea6cf2 NC |
1813 | case E_MIPS_ARCH_5: |
1814 | return 5; | |
e7af610e NC |
1815 | case E_MIPS_ARCH_32: |
1816 | return 32; | |
84ea6cf2 NC |
1817 | case E_MIPS_ARCH_64: |
1818 | return 64; | |
252b5132 RH |
1819 | } |
1820 | return 4; | |
1821 | } | |
1822 | ||
1823 | /* Return the MACH for a MIPS e_flags value. */ | |
1824 | ||
1825 | static INLINE int | |
1826 | elf_mips_mach (flags) | |
1827 | flagword flags; | |
1828 | { | |
1829 | switch (flags & EF_MIPS_MACH) | |
1830 | { | |
1831 | case E_MIPS_MACH_3900: | |
1832 | return bfd_mach_mips3900; | |
1833 | ||
1834 | case E_MIPS_MACH_4010: | |
1835 | return bfd_mach_mips4010; | |
1836 | ||
1837 | case E_MIPS_MACH_4100: | |
1838 | return bfd_mach_mips4100; | |
1839 | ||
1840 | case E_MIPS_MACH_4111: | |
1841 | return bfd_mach_mips4111; | |
1842 | ||
1843 | case E_MIPS_MACH_4650: | |
1844 | return bfd_mach_mips4650; | |
1845 | ||
e7af610e NC |
1846 | case E_MIPS_MACH_MIPS32_4K: |
1847 | return bfd_mach_mips32_4k; | |
156c2f8b | 1848 | |
252b5132 RH |
1849 | default: |
1850 | switch (flags & EF_MIPS_ARCH) | |
1851 | { | |
1852 | default: | |
1853 | case E_MIPS_ARCH_1: | |
1854 | return bfd_mach_mips3000; | |
1855 | break; | |
1856 | ||
1857 | case E_MIPS_ARCH_2: | |
1858 | return bfd_mach_mips6000; | |
1859 | break; | |
1860 | ||
1861 | case E_MIPS_ARCH_3: | |
1862 | return bfd_mach_mips4000; | |
1863 | break; | |
1864 | ||
1865 | case E_MIPS_ARCH_4: | |
1866 | return bfd_mach_mips8000; | |
1867 | break; | |
e7af610e | 1868 | |
84ea6cf2 NC |
1869 | case E_MIPS_ARCH_5: |
1870 | return bfd_mach_mips5; | |
1871 | break; | |
1872 | ||
e7af610e NC |
1873 | case E_MIPS_ARCH_32: |
1874 | return bfd_mach_mips32; | |
1875 | break; | |
84ea6cf2 NC |
1876 | |
1877 | case E_MIPS_ARCH_64: | |
1878 | return bfd_mach_mips64; | |
1879 | break; | |
252b5132 RH |
1880 | } |
1881 | } | |
1882 | ||
1883 | return 0; | |
1884 | } | |
1885 | ||
be3ccd9c | 1886 | /* Return printable name for ABI. */ |
252b5132 | 1887 | |
be3ccd9c | 1888 | static INLINE char * |
103186c6 MM |
1889 | elf_mips_abi_name (abfd) |
1890 | bfd *abfd; | |
252b5132 | 1891 | { |
103186c6 MM |
1892 | flagword flags; |
1893 | ||
1894 | if (ABI_N32_P (abfd)) | |
1895 | return "N32"; | |
1896 | else if (ABI_64_P (abfd)) | |
1897 | return "64"; | |
be3ccd9c | 1898 | |
103186c6 | 1899 | flags = elf_elfheader (abfd)->e_flags; |
252b5132 RH |
1900 | switch (flags & EF_MIPS_ABI) |
1901 | { | |
1902 | case 0: | |
1903 | return "none"; | |
1904 | case E_MIPS_ABI_O32: | |
1905 | return "O32"; | |
1906 | case E_MIPS_ABI_O64: | |
1907 | return "O64"; | |
1908 | case E_MIPS_ABI_EABI32: | |
1909 | return "EABI32"; | |
1910 | case E_MIPS_ABI_EABI64: | |
1911 | return "EABI64"; | |
1912 | default: | |
1913 | return "unknown abi"; | |
1914 | } | |
1915 | } | |
1916 | ||
1917 | /* A mapping from BFD reloc types to MIPS ELF reloc types. */ | |
1918 | ||
1919 | struct elf_reloc_map { | |
1920 | bfd_reloc_code_real_type bfd_reloc_val; | |
1921 | enum elf_mips_reloc_type elf_reloc_val; | |
1922 | }; | |
1923 | ||
be3ccd9c | 1924 | static CONST struct elf_reloc_map mips_reloc_map[] = { |
252b5132 RH |
1925 | { BFD_RELOC_NONE, R_MIPS_NONE, }, |
1926 | { BFD_RELOC_16, R_MIPS_16 }, | |
1927 | { BFD_RELOC_32, R_MIPS_32 }, | |
1928 | { BFD_RELOC_64, R_MIPS_64 }, | |
1929 | { BFD_RELOC_MIPS_JMP, R_MIPS_26 }, | |
1930 | { BFD_RELOC_HI16_S, R_MIPS_HI16 }, | |
1931 | { BFD_RELOC_LO16, R_MIPS_LO16 }, | |
1932 | { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 }, | |
1933 | { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL }, | |
1934 | { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 }, | |
1935 | { BFD_RELOC_16_PCREL, R_MIPS_PC16 }, | |
1936 | { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 }, | |
1937 | { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 }, | |
1938 | { BFD_RELOC_MIPS_GOT_HI16, R_MIPS_GOT_HI16 }, | |
1939 | { BFD_RELOC_MIPS_GOT_LO16, R_MIPS_GOT_LO16 }, | |
1940 | { BFD_RELOC_MIPS_CALL_HI16, R_MIPS_CALL_HI16 }, | |
3f830999 MM |
1941 | { BFD_RELOC_MIPS_CALL_LO16, R_MIPS_CALL_LO16 }, |
1942 | { BFD_RELOC_MIPS_SUB, R_MIPS_SUB }, | |
1943 | { BFD_RELOC_MIPS_GOT_PAGE, R_MIPS_GOT_PAGE }, | |
1944 | { BFD_RELOC_MIPS_GOT_OFST, R_MIPS_GOT_OFST }, | |
1945 | { BFD_RELOC_MIPS_GOT_DISP, R_MIPS_GOT_DISP } | |
252b5132 RH |
1946 | }; |
1947 | ||
1948 | /* Given a BFD reloc type, return a howto structure. */ | |
1949 | ||
1950 | static reloc_howto_type * | |
1951 | bfd_elf32_bfd_reloc_type_lookup (abfd, code) | |
1952 | bfd *abfd; | |
1953 | bfd_reloc_code_real_type code; | |
1954 | { | |
1955 | unsigned int i; | |
1956 | ||
1957 | for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++) | |
1958 | { | |
1959 | if (mips_reloc_map[i].bfd_reloc_val == code) | |
1960 | return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val]; | |
1961 | } | |
1962 | ||
1963 | switch (code) | |
1964 | { | |
1965 | default: | |
1966 | bfd_set_error (bfd_error_bad_value); | |
1967 | return NULL; | |
1968 | ||
1969 | case BFD_RELOC_CTOR: | |
1970 | /* We need to handle BFD_RELOC_CTOR specially. | |
1971 | Select the right relocation (R_MIPS_32 or R_MIPS_64) based on the | |
1972 | size of addresses on this architecture. */ | |
1973 | if (bfd_arch_bits_per_address (abfd) == 32) | |
1974 | return &elf_mips_howto_table[(int) R_MIPS_32]; | |
1975 | else | |
1976 | return &elf_mips_ctor64_howto; | |
1977 | ||
1978 | case BFD_RELOC_MIPS16_JMP: | |
1979 | return &elf_mips16_jump_howto; | |
1980 | case BFD_RELOC_MIPS16_GPREL: | |
1981 | return &elf_mips16_gprel_howto; | |
1982 | case BFD_RELOC_VTABLE_INHERIT: | |
1983 | return &elf_mips_gnu_vtinherit_howto; | |
1984 | case BFD_RELOC_VTABLE_ENTRY: | |
1985 | return &elf_mips_gnu_vtentry_howto; | |
bb2d6cd7 GK |
1986 | case BFD_RELOC_PCREL_HI16_S: |
1987 | return &elf_mips_gnu_rel_hi16; | |
1988 | case BFD_RELOC_PCREL_LO16: | |
1989 | return &elf_mips_gnu_rel_lo16; | |
1990 | case BFD_RELOC_16_PCREL_S2: | |
1991 | return &elf_mips_gnu_rel16_s2; | |
1992 | case BFD_RELOC_64_PCREL: | |
1993 | return &elf_mips_gnu_pcrel64; | |
1994 | case BFD_RELOC_32_PCREL: | |
1995 | return &elf_mips_gnu_pcrel32; | |
252b5132 RH |
1996 | } |
1997 | } | |
1998 | ||
3f830999 | 1999 | /* Given a MIPS Elf32_Internal_Rel, fill in an arelent structure. */ |
252b5132 | 2000 | |
c9b3cbf3 RH |
2001 | static reloc_howto_type * |
2002 | mips_rtype_to_howto (r_type) | |
2003 | unsigned int r_type; | |
252b5132 | 2004 | { |
252b5132 RH |
2005 | switch (r_type) |
2006 | { | |
2007 | case R_MIPS16_26: | |
c9b3cbf3 | 2008 | return &elf_mips16_jump_howto; |
252b5132 RH |
2009 | break; |
2010 | case R_MIPS16_GPREL: | |
c9b3cbf3 | 2011 | return &elf_mips16_gprel_howto; |
252b5132 RH |
2012 | break; |
2013 | case R_MIPS_GNU_VTINHERIT: | |
c9b3cbf3 | 2014 | return &elf_mips_gnu_vtinherit_howto; |
252b5132 RH |
2015 | break; |
2016 | case R_MIPS_GNU_VTENTRY: | |
c9b3cbf3 | 2017 | return &elf_mips_gnu_vtentry_howto; |
252b5132 | 2018 | break; |
bb2d6cd7 GK |
2019 | case R_MIPS_GNU_REL_HI16: |
2020 | return &elf_mips_gnu_rel_hi16; | |
2021 | break; | |
2022 | case R_MIPS_GNU_REL_LO16: | |
2023 | return &elf_mips_gnu_rel_lo16; | |
2024 | break; | |
2025 | case R_MIPS_GNU_REL16_S2: | |
2026 | return &elf_mips_gnu_rel16_s2; | |
2027 | break; | |
2028 | case R_MIPS_PC64: | |
2029 | return &elf_mips_gnu_pcrel64; | |
2030 | break; | |
2031 | case R_MIPS_PC32: | |
2032 | return &elf_mips_gnu_pcrel32; | |
2033 | break; | |
252b5132 RH |
2034 | |
2035 | default: | |
2036 | BFD_ASSERT (r_type < (unsigned int) R_MIPS_max); | |
c9b3cbf3 | 2037 | return &elf_mips_howto_table[r_type]; |
252b5132 RH |
2038 | break; |
2039 | } | |
c9b3cbf3 RH |
2040 | } |
2041 | ||
2042 | /* Given a MIPS Elf32_Internal_Rel, fill in an arelent structure. */ | |
2043 | ||
2044 | static void | |
2045 | mips_info_to_howto_rel (abfd, cache_ptr, dst) | |
2046 | bfd *abfd; | |
2047 | arelent *cache_ptr; | |
2048 | Elf32_Internal_Rel *dst; | |
2049 | { | |
2050 | unsigned int r_type; | |
2051 | ||
2052 | r_type = ELF32_R_TYPE (dst->r_info); | |
2053 | cache_ptr->howto = mips_rtype_to_howto (r_type); | |
252b5132 RH |
2054 | |
2055 | /* The addend for a GPREL16 or LITERAL relocation comes from the GP | |
2056 | value for the object file. We get the addend now, rather than | |
2057 | when we do the relocation, because the symbol manipulations done | |
2058 | by the linker may cause us to lose track of the input BFD. */ | |
2059 | if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0 | |
2060 | && (r_type == (unsigned int) R_MIPS_GPREL16 | |
2061 | || r_type == (unsigned int) R_MIPS_LITERAL)) | |
2062 | cache_ptr->addend = elf_gp (abfd); | |
2063 | } | |
3f830999 MM |
2064 | |
2065 | /* Given a MIPS Elf32_Internal_Rela, fill in an arelent structure. */ | |
2066 | ||
2067 | static void | |
2068 | mips_info_to_howto_rela (abfd, cache_ptr, dst) | |
2069 | bfd *abfd; | |
2070 | arelent *cache_ptr; | |
2071 | Elf32_Internal_Rela *dst; | |
2072 | { | |
2073 | /* Since an Elf32_Internal_Rel is an initial prefix of an | |
2074 | Elf32_Internal_Rela, we can just use mips_info_to_howto_rel | |
2075 | above. */ | |
2076 | mips_info_to_howto_rel (abfd, cache_ptr, (Elf32_Internal_Rel *) dst); | |
2077 | ||
2078 | /* If we ever need to do any extra processing with dst->r_addend | |
2079 | (the field omitted in an Elf32_Internal_Rel) we can do it here. */ | |
2080 | } | |
252b5132 RH |
2081 | \f |
2082 | /* A .reginfo section holds a single Elf32_RegInfo structure. These | |
2083 | routines swap this structure in and out. They are used outside of | |
2084 | BFD, so they are globally visible. */ | |
2085 | ||
2086 | void | |
2087 | bfd_mips_elf32_swap_reginfo_in (abfd, ex, in) | |
2088 | bfd *abfd; | |
2089 | const Elf32_External_RegInfo *ex; | |
2090 | Elf32_RegInfo *in; | |
2091 | { | |
2092 | in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask); | |
2093 | in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]); | |
2094 | in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]); | |
2095 | in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]); | |
2096 | in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]); | |
2097 | in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value); | |
2098 | } | |
2099 | ||
2100 | void | |
2101 | bfd_mips_elf32_swap_reginfo_out (abfd, in, ex) | |
2102 | bfd *abfd; | |
2103 | const Elf32_RegInfo *in; | |
2104 | Elf32_External_RegInfo *ex; | |
2105 | { | |
2106 | bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask, | |
2107 | (bfd_byte *) ex->ri_gprmask); | |
2108 | bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0], | |
2109 | (bfd_byte *) ex->ri_cprmask[0]); | |
2110 | bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1], | |
2111 | (bfd_byte *) ex->ri_cprmask[1]); | |
2112 | bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2], | |
2113 | (bfd_byte *) ex->ri_cprmask[2]); | |
2114 | bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3], | |
2115 | (bfd_byte *) ex->ri_cprmask[3]); | |
2116 | bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value, | |
2117 | (bfd_byte *) ex->ri_gp_value); | |
2118 | } | |
2119 | ||
2120 | /* In the 64 bit ABI, the .MIPS.options section holds register | |
2121 | information in an Elf64_Reginfo structure. These routines swap | |
2122 | them in and out. They are globally visible because they are used | |
2123 | outside of BFD. These routines are here so that gas can call them | |
2124 | without worrying about whether the 64 bit ABI has been included. */ | |
2125 | ||
2126 | void | |
2127 | bfd_mips_elf64_swap_reginfo_in (abfd, ex, in) | |
2128 | bfd *abfd; | |
2129 | const Elf64_External_RegInfo *ex; | |
2130 | Elf64_Internal_RegInfo *in; | |
2131 | { | |
2132 | in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask); | |
2133 | in->ri_pad = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_pad); | |
2134 | in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]); | |
2135 | in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]); | |
2136 | in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]); | |
2137 | in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]); | |
2138 | in->ri_gp_value = bfd_h_get_64 (abfd, (bfd_byte *) ex->ri_gp_value); | |
2139 | } | |
2140 | ||
2141 | void | |
2142 | bfd_mips_elf64_swap_reginfo_out (abfd, in, ex) | |
2143 | bfd *abfd; | |
2144 | const Elf64_Internal_RegInfo *in; | |
2145 | Elf64_External_RegInfo *ex; | |
2146 | { | |
2147 | bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask, | |
2148 | (bfd_byte *) ex->ri_gprmask); | |
2149 | bfd_h_put_32 (abfd, (bfd_vma) in->ri_pad, | |
2150 | (bfd_byte *) ex->ri_pad); | |
2151 | bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0], | |
2152 | (bfd_byte *) ex->ri_cprmask[0]); | |
2153 | bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1], | |
2154 | (bfd_byte *) ex->ri_cprmask[1]); | |
2155 | bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2], | |
2156 | (bfd_byte *) ex->ri_cprmask[2]); | |
2157 | bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3], | |
2158 | (bfd_byte *) ex->ri_cprmask[3]); | |
2159 | bfd_h_put_64 (abfd, (bfd_vma) in->ri_gp_value, | |
2160 | (bfd_byte *) ex->ri_gp_value); | |
2161 | } | |
2162 | ||
2163 | /* Swap an entry in a .gptab section. Note that these routines rely | |
2164 | on the equivalence of the two elements of the union. */ | |
2165 | ||
2166 | static void | |
2167 | bfd_mips_elf32_swap_gptab_in (abfd, ex, in) | |
2168 | bfd *abfd; | |
2169 | const Elf32_External_gptab *ex; | |
2170 | Elf32_gptab *in; | |
2171 | { | |
2172 | in->gt_entry.gt_g_value = bfd_h_get_32 (abfd, ex->gt_entry.gt_g_value); | |
2173 | in->gt_entry.gt_bytes = bfd_h_get_32 (abfd, ex->gt_entry.gt_bytes); | |
2174 | } | |
2175 | ||
2176 | static void | |
2177 | bfd_mips_elf32_swap_gptab_out (abfd, in, ex) | |
2178 | bfd *abfd; | |
2179 | const Elf32_gptab *in; | |
2180 | Elf32_External_gptab *ex; | |
2181 | { | |
2182 | bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value, | |
2183 | ex->gt_entry.gt_g_value); | |
2184 | bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes, | |
2185 | ex->gt_entry.gt_bytes); | |
2186 | } | |
2187 | ||
2188 | static void | |
2189 | bfd_elf32_swap_compact_rel_out (abfd, in, ex) | |
2190 | bfd *abfd; | |
2191 | const Elf32_compact_rel *in; | |
2192 | Elf32_External_compact_rel *ex; | |
2193 | { | |
2194 | bfd_h_put_32 (abfd, (bfd_vma) in->id1, ex->id1); | |
2195 | bfd_h_put_32 (abfd, (bfd_vma) in->num, ex->num); | |
2196 | bfd_h_put_32 (abfd, (bfd_vma) in->id2, ex->id2); | |
2197 | bfd_h_put_32 (abfd, (bfd_vma) in->offset, ex->offset); | |
2198 | bfd_h_put_32 (abfd, (bfd_vma) in->reserved0, ex->reserved0); | |
2199 | bfd_h_put_32 (abfd, (bfd_vma) in->reserved1, ex->reserved1); | |
2200 | } | |
2201 | ||
2202 | static void | |
2203 | bfd_elf32_swap_crinfo_out (abfd, in, ex) | |
2204 | bfd *abfd; | |
2205 | const Elf32_crinfo *in; | |
2206 | Elf32_External_crinfo *ex; | |
2207 | { | |
2208 | unsigned long l; | |
2209 | ||
2210 | l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH) | |
2211 | | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH) | |
2212 | | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH) | |
2213 | | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH)); | |
2214 | bfd_h_put_32 (abfd, (bfd_vma) l, ex->info); | |
2215 | bfd_h_put_32 (abfd, (bfd_vma) in->konst, ex->konst); | |
2216 | bfd_h_put_32 (abfd, (bfd_vma) in->vaddr, ex->vaddr); | |
2217 | } | |
2218 | ||
2219 | /* Swap in an options header. */ | |
2220 | ||
2221 | void | |
2222 | bfd_mips_elf_swap_options_in (abfd, ex, in) | |
2223 | bfd *abfd; | |
2224 | const Elf_External_Options *ex; | |
2225 | Elf_Internal_Options *in; | |
2226 | { | |
2227 | in->kind = bfd_h_get_8 (abfd, ex->kind); | |
2228 | in->size = bfd_h_get_8 (abfd, ex->size); | |
2229 | in->section = bfd_h_get_16 (abfd, ex->section); | |
2230 | in->info = bfd_h_get_32 (abfd, ex->info); | |
2231 | } | |
2232 | ||
2233 | /* Swap out an options header. */ | |
2234 | ||
2235 | void | |
2236 | bfd_mips_elf_swap_options_out (abfd, in, ex) | |
2237 | bfd *abfd; | |
2238 | const Elf_Internal_Options *in; | |
2239 | Elf_External_Options *ex; | |
2240 | { | |
2241 | bfd_h_put_8 (abfd, in->kind, ex->kind); | |
2242 | bfd_h_put_8 (abfd, in->size, ex->size); | |
2243 | bfd_h_put_16 (abfd, in->section, ex->section); | |
2244 | bfd_h_put_32 (abfd, in->info, ex->info); | |
2245 | } | |
86033394 | 2246 | #if 0 |
c6142e5d MM |
2247 | /* Swap in an MSYM entry. */ |
2248 | ||
2249 | static void | |
2250 | bfd_mips_elf_swap_msym_in (abfd, ex, in) | |
2251 | bfd *abfd; | |
2252 | const Elf32_External_Msym *ex; | |
2253 | Elf32_Internal_Msym *in; | |
2254 | { | |
2255 | in->ms_hash_value = bfd_h_get_32 (abfd, ex->ms_hash_value); | |
2256 | in->ms_info = bfd_h_get_32 (abfd, ex->ms_info); | |
2257 | } | |
86033394 | 2258 | #endif |
c6142e5d MM |
2259 | /* Swap out an MSYM entry. */ |
2260 | ||
2261 | static void | |
2262 | bfd_mips_elf_swap_msym_out (abfd, in, ex) | |
2263 | bfd *abfd; | |
2264 | const Elf32_Internal_Msym *in; | |
2265 | Elf32_External_Msym *ex; | |
2266 | { | |
2267 | bfd_h_put_32 (abfd, in->ms_hash_value, ex->ms_hash_value); | |
2268 | bfd_h_put_32 (abfd, in->ms_info, ex->ms_info); | |
2269 | } | |
252b5132 RH |
2270 | \f |
2271 | /* Determine whether a symbol is global for the purposes of splitting | |
2272 | the symbol table into global symbols and local symbols. At least | |
2273 | on Irix 5, this split must be between section symbols and all other | |
2274 | symbols. On most ELF targets the split is between static symbols | |
2275 | and externally visible symbols. */ | |
2276 | ||
252b5132 RH |
2277 | static boolean |
2278 | mips_elf_sym_is_global (abfd, sym) | |
5f771d47 | 2279 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
2280 | asymbol *sym; |
2281 | { | |
2282 | return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false; | |
2283 | } | |
2284 | \f | |
2285 | /* Set the right machine number for a MIPS ELF file. This is used for | |
2286 | both the 32-bit and the 64-bit ABI. */ | |
2287 | ||
2288 | boolean | |
2289 | _bfd_mips_elf_object_p (abfd) | |
2290 | bfd *abfd; | |
2291 | { | |
103186c6 | 2292 | /* Irix 5 and 6 is broken. Object file symbol tables are not always |
252b5132 RH |
2293 | sorted correctly such that local symbols precede global symbols, |
2294 | and the sh_info field in the symbol table is not always right. */ | |
2295 | elf_bad_symtab (abfd) = true; | |
2296 | ||
103186c6 MM |
2297 | bfd_default_set_arch_mach (abfd, bfd_arch_mips, |
2298 | elf_mips_mach (elf_elfheader (abfd)->e_flags)); | |
2299 | return true; | |
252b5132 RH |
2300 | } |
2301 | ||
2302 | /* The final processing done just before writing out a MIPS ELF object | |
2303 | file. This gets the MIPS architecture right based on the machine | |
2304 | number. This is used by both the 32-bit and the 64-bit ABI. */ | |
2305 | ||
252b5132 RH |
2306 | void |
2307 | _bfd_mips_elf_final_write_processing (abfd, linker) | |
2308 | bfd *abfd; | |
5f771d47 | 2309 | boolean linker ATTRIBUTE_UNUSED; |
252b5132 RH |
2310 | { |
2311 | unsigned long val; | |
2312 | unsigned int i; | |
2313 | Elf_Internal_Shdr **hdrpp; | |
2314 | const char *name; | |
2315 | asection *sec; | |
2316 | ||
2317 | switch (bfd_get_mach (abfd)) | |
2318 | { | |
2319 | default: | |
2320 | case bfd_mach_mips3000: | |
2321 | val = E_MIPS_ARCH_1; | |
2322 | break; | |
2323 | ||
2324 | case bfd_mach_mips3900: | |
2325 | val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900; | |
2326 | break; | |
2327 | ||
2328 | case bfd_mach_mips6000: | |
2329 | val = E_MIPS_ARCH_2; | |
2330 | break; | |
2331 | ||
2332 | case bfd_mach_mips4000: | |
2333 | case bfd_mach_mips4300: | |
2334 | val = E_MIPS_ARCH_3; | |
2335 | break; | |
2336 | ||
2337 | case bfd_mach_mips4010: | |
2338 | val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010; | |
2339 | break; | |
2340 | ||
2341 | case bfd_mach_mips4100: | |
2342 | val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100; | |
2343 | break; | |
2344 | ||
2345 | case bfd_mach_mips4111: | |
2346 | val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111; | |
2347 | break; | |
2348 | ||
2349 | case bfd_mach_mips4650: | |
2350 | val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650; | |
2351 | break; | |
2352 | ||
2353 | case bfd_mach_mips8000: | |
2354 | val = E_MIPS_ARCH_4; | |
2355 | break; | |
156c2f8b | 2356 | |
e7af610e NC |
2357 | case bfd_mach_mips32: |
2358 | val = E_MIPS_ARCH_32; | |
2359 | break; | |
2360 | ||
2361 | case bfd_mach_mips32_4k: | |
2362 | val = E_MIPS_ARCH_32 | E_MIPS_MACH_MIPS32_4K; | |
156c2f8b | 2363 | break; |
84ea6cf2 NC |
2364 | |
2365 | case bfd_mach_mips5: | |
2366 | val = E_MIPS_ARCH_5; | |
2367 | break; | |
2368 | ||
2369 | case bfd_mach_mips64: | |
2370 | val = E_MIPS_ARCH_64; | |
2371 | break; | |
252b5132 RH |
2372 | } |
2373 | ||
be3ccd9c | 2374 | elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH); |
252b5132 RH |
2375 | elf_elfheader (abfd)->e_flags |= val; |
2376 | ||
2377 | /* Set the sh_info field for .gptab sections and other appropriate | |
2378 | info for each special section. */ | |
2379 | for (i = 1, hdrpp = elf_elfsections (abfd) + 1; | |
2380 | i < elf_elfheader (abfd)->e_shnum; | |
2381 | i++, hdrpp++) | |
2382 | { | |
2383 | switch ((*hdrpp)->sh_type) | |
2384 | { | |
c6142e5d | 2385 | case SHT_MIPS_MSYM: |
252b5132 RH |
2386 | case SHT_MIPS_LIBLIST: |
2387 | sec = bfd_get_section_by_name (abfd, ".dynstr"); | |
2388 | if (sec != NULL) | |
2389 | (*hdrpp)->sh_link = elf_section_data (sec)->this_idx; | |
2390 | break; | |
2391 | ||
2392 | case SHT_MIPS_GPTAB: | |
2393 | BFD_ASSERT ((*hdrpp)->bfd_section != NULL); | |
2394 | name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section); | |
2395 | BFD_ASSERT (name != NULL | |
2396 | && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0); | |
2397 | sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1); | |
2398 | BFD_ASSERT (sec != NULL); | |
2399 | (*hdrpp)->sh_info = elf_section_data (sec)->this_idx; | |
2400 | break; | |
2401 | ||
2402 | case SHT_MIPS_CONTENT: | |
2403 | BFD_ASSERT ((*hdrpp)->bfd_section != NULL); | |
2404 | name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section); | |
2405 | BFD_ASSERT (name != NULL | |
2406 | && strncmp (name, ".MIPS.content", | |
2407 | sizeof ".MIPS.content" - 1) == 0); | |
2408 | sec = bfd_get_section_by_name (abfd, | |
2409 | name + sizeof ".MIPS.content" - 1); | |
2410 | BFD_ASSERT (sec != NULL); | |
3f830999 | 2411 | (*hdrpp)->sh_link = elf_section_data (sec)->this_idx; |
252b5132 RH |
2412 | break; |
2413 | ||
2414 | case SHT_MIPS_SYMBOL_LIB: | |
2415 | sec = bfd_get_section_by_name (abfd, ".dynsym"); | |
2416 | if (sec != NULL) | |
2417 | (*hdrpp)->sh_link = elf_section_data (sec)->this_idx; | |
2418 | sec = bfd_get_section_by_name (abfd, ".liblist"); | |
2419 | if (sec != NULL) | |
2420 | (*hdrpp)->sh_info = elf_section_data (sec)->this_idx; | |
2421 | break; | |
2422 | ||
2423 | case SHT_MIPS_EVENTS: | |
2424 | BFD_ASSERT ((*hdrpp)->bfd_section != NULL); | |
2425 | name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section); | |
2426 | BFD_ASSERT (name != NULL); | |
2427 | if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0) | |
2428 | sec = bfd_get_section_by_name (abfd, | |
2429 | name + sizeof ".MIPS.events" - 1); | |
2430 | else | |
2431 | { | |
2432 | BFD_ASSERT (strncmp (name, ".MIPS.post_rel", | |
2433 | sizeof ".MIPS.post_rel" - 1) == 0); | |
2434 | sec = bfd_get_section_by_name (abfd, | |
2435 | (name | |
2436 | + sizeof ".MIPS.post_rel" - 1)); | |
2437 | } | |
2438 | BFD_ASSERT (sec != NULL); | |
2439 | (*hdrpp)->sh_link = elf_section_data (sec)->this_idx; | |
2440 | break; | |
2441 | ||
2442 | } | |
2443 | } | |
2444 | } | |
2445 | \f | |
be3ccd9c | 2446 | /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */ |
252b5132 RH |
2447 | |
2448 | boolean | |
2449 | _bfd_mips_elf_set_private_flags (abfd, flags) | |
2450 | bfd *abfd; | |
2451 | flagword flags; | |
2452 | { | |
2453 | BFD_ASSERT (!elf_flags_init (abfd) | |
2454 | || elf_elfheader (abfd)->e_flags == flags); | |
2455 | ||
2456 | elf_elfheader (abfd)->e_flags = flags; | |
2457 | elf_flags_init (abfd) = true; | |
2458 | return true; | |
2459 | } | |
2460 | ||
2461 | /* Copy backend specific data from one object module to another */ | |
2462 | ||
2463 | boolean | |
2464 | _bfd_mips_elf_copy_private_bfd_data (ibfd, obfd) | |
2465 | bfd *ibfd; | |
2466 | bfd *obfd; | |
2467 | { | |
2468 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
2469 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
2470 | return true; | |
2471 | ||
2472 | BFD_ASSERT (!elf_flags_init (obfd) | |
2473 | || (elf_elfheader (obfd)->e_flags | |
2474 | == elf_elfheader (ibfd)->e_flags)); | |
2475 | ||
2476 | elf_gp (obfd) = elf_gp (ibfd); | |
2477 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
2478 | elf_flags_init (obfd) = true; | |
2479 | return true; | |
2480 | } | |
2481 | ||
2482 | /* Merge backend specific data from an object file to the output | |
2483 | object file when linking. */ | |
2484 | ||
2485 | boolean | |
2486 | _bfd_mips_elf_merge_private_bfd_data (ibfd, obfd) | |
2487 | bfd *ibfd; | |
2488 | bfd *obfd; | |
2489 | { | |
2490 | flagword old_flags; | |
2491 | flagword new_flags; | |
2492 | boolean ok; | |
2493 | ||
2494 | /* Check if we have the same endianess */ | |
1fe494a5 NC |
2495 | if (_bfd_generic_verify_endian_match (ibfd, obfd) == false) |
2496 | return false; | |
252b5132 RH |
2497 | |
2498 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
2499 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
2500 | return true; | |
2501 | ||
2502 | new_flags = elf_elfheader (ibfd)->e_flags; | |
2503 | elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER; | |
2504 | old_flags = elf_elfheader (obfd)->e_flags; | |
2505 | ||
2506 | if (! elf_flags_init (obfd)) | |
2507 | { | |
2508 | elf_flags_init (obfd) = true; | |
2509 | elf_elfheader (obfd)->e_flags = new_flags; | |
be3ccd9c | 2510 | elf_elfheader (obfd)->e_ident[EI_CLASS] |
103186c6 | 2511 | = elf_elfheader (ibfd)->e_ident[EI_CLASS]; |
252b5132 RH |
2512 | |
2513 | if (bfd_get_arch (obfd) == bfd_get_arch (ibfd) | |
2514 | && bfd_get_arch_info (obfd)->the_default) | |
2515 | { | |
2516 | if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), | |
2517 | bfd_get_mach (ibfd))) | |
2518 | return false; | |
2519 | } | |
2520 | ||
2521 | return true; | |
2522 | } | |
2523 | ||
2524 | /* Check flag compatibility. */ | |
2525 | ||
2526 | new_flags &= ~EF_MIPS_NOREORDER; | |
2527 | old_flags &= ~EF_MIPS_NOREORDER; | |
2528 | ||
2529 | if (new_flags == old_flags) | |
2530 | return true; | |
2531 | ||
2532 | ok = true; | |
2533 | ||
2534 | if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC)) | |
2535 | { | |
2536 | new_flags &= ~EF_MIPS_PIC; | |
2537 | old_flags &= ~EF_MIPS_PIC; | |
2538 | (*_bfd_error_handler) | |
2539 | (_("%s: linking PIC files with non-PIC files"), | |
2540 | bfd_get_filename (ibfd)); | |
2541 | ok = false; | |
2542 | } | |
2543 | ||
2544 | if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC)) | |
2545 | { | |
2546 | new_flags &= ~EF_MIPS_CPIC; | |
2547 | old_flags &= ~EF_MIPS_CPIC; | |
2548 | (*_bfd_error_handler) | |
2549 | (_("%s: linking abicalls files with non-abicalls files"), | |
2550 | bfd_get_filename (ibfd)); | |
2551 | ok = false; | |
2552 | } | |
2553 | ||
be3ccd9c | 2554 | /* Compare the ISA's. */ |
252b5132 RH |
2555 | if ((new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH)) |
2556 | != (old_flags & (EF_MIPS_ARCH | EF_MIPS_MACH))) | |
2557 | { | |
2558 | int new_mach = new_flags & EF_MIPS_MACH; | |
2559 | int old_mach = old_flags & EF_MIPS_MACH; | |
2560 | int new_isa = elf_mips_isa (new_flags); | |
2561 | int old_isa = elf_mips_isa (old_flags); | |
2562 | ||
2563 | /* If either has no machine specified, just compare the general isa's. | |
be3ccd9c KH |
2564 | Some combinations of machines are ok, if the isa's match. */ |
2565 | if (! new_mach | |
252b5132 RH |
2566 | || ! old_mach |
2567 | || new_mach == old_mach | |
2568 | ) | |
2569 | { | |
e7af610e NC |
2570 | /* Don't warn about mixing code using 32-bit ISAs, or mixing code |
2571 | using 64-bit ISAs. They will normally use the same data sizes | |
2572 | and calling conventions. */ | |
252b5132 | 2573 | |
e7af610e NC |
2574 | if (( (new_isa == 1 || new_isa == 2 || new_isa == 32) |
2575 | ^ (old_isa == 1 || old_isa == 2 || old_isa == 32)) != 0) | |
252b5132 RH |
2576 | { |
2577 | (*_bfd_error_handler) | |
2578 | (_("%s: ISA mismatch (-mips%d) with previous modules (-mips%d)"), | |
2579 | bfd_get_filename (ibfd), new_isa, old_isa); | |
2580 | ok = false; | |
2581 | } | |
2582 | } | |
2583 | ||
2584 | else | |
2585 | { | |
2586 | (*_bfd_error_handler) | |
2587 | (_("%s: ISA mismatch (%d) with previous modules (%d)"), | |
2588 | bfd_get_filename (ibfd), | |
2589 | elf_mips_mach (new_flags), | |
2590 | elf_mips_mach (old_flags)); | |
2591 | ok = false; | |
2592 | } | |
2593 | ||
be3ccd9c KH |
2594 | new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH); |
2595 | old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH); | |
252b5132 RH |
2596 | } |
2597 | ||
103186c6 MM |
2598 | /* Compare ABI's. The 64-bit ABI does not use EF_MIPS_ABI. But, it |
2599 | does set EI_CLASS differently from any 32-bit ABI. */ | |
2600 | if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI) | |
be3ccd9c | 2601 | || (elf_elfheader (ibfd)->e_ident[EI_CLASS] |
103186c6 | 2602 | != elf_elfheader (obfd)->e_ident[EI_CLASS])) |
252b5132 | 2603 | { |
be3ccd9c | 2604 | /* Only error if both are set (to different values). */ |
103186c6 | 2605 | if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI)) |
be3ccd9c | 2606 | || (elf_elfheader (ibfd)->e_ident[EI_CLASS] |
103186c6 | 2607 | != elf_elfheader (obfd)->e_ident[EI_CLASS])) |
252b5132 RH |
2608 | { |
2609 | (*_bfd_error_handler) | |
2610 | (_("%s: ABI mismatch: linking %s module with previous %s modules"), | |
2611 | bfd_get_filename (ibfd), | |
103186c6 MM |
2612 | elf_mips_abi_name (ibfd), |
2613 | elf_mips_abi_name (obfd)); | |
252b5132 RH |
2614 | ok = false; |
2615 | } | |
2616 | new_flags &= ~EF_MIPS_ABI; | |
2617 | old_flags &= ~EF_MIPS_ABI; | |
2618 | } | |
2619 | ||
2620 | /* Warn about any other mismatches */ | |
2621 | if (new_flags != old_flags) | |
2622 | { | |
2623 | (*_bfd_error_handler) | |
2624 | (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"), | |
2625 | bfd_get_filename (ibfd), (unsigned long) new_flags, | |
2626 | (unsigned long) old_flags); | |
2627 | ok = false; | |
2628 | } | |
2629 | ||
2630 | if (! ok) | |
2631 | { | |
2632 | bfd_set_error (bfd_error_bad_value); | |
2633 | return false; | |
2634 | } | |
2635 | ||
2636 | return true; | |
2637 | } | |
2638 | \f | |
103186c6 | 2639 | boolean |
252b5132 RH |
2640 | _bfd_mips_elf_print_private_bfd_data (abfd, ptr) |
2641 | bfd *abfd; | |
2642 | PTR ptr; | |
2643 | { | |
2644 | FILE *file = (FILE *) ptr; | |
2645 | ||
2646 | BFD_ASSERT (abfd != NULL && ptr != NULL); | |
2647 | ||
2648 | /* Print normal ELF private data. */ | |
2649 | _bfd_elf_print_private_bfd_data (abfd, ptr); | |
2650 | ||
2651 | /* xgettext:c-format */ | |
be3ccd9c | 2652 | fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags); |
252b5132 RH |
2653 | |
2654 | if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32) | |
be3ccd9c | 2655 | fprintf (file, _(" [abi=O32]")); |
252b5132 | 2656 | else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64) |
be3ccd9c | 2657 | fprintf (file, _(" [abi=O64]")); |
252b5132 | 2658 | else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32) |
be3ccd9c | 2659 | fprintf (file, _(" [abi=EABI32]")); |
252b5132 | 2660 | else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64) |
be3ccd9c | 2661 | fprintf (file, _(" [abi=EABI64]")); |
252b5132 | 2662 | else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI)) |
be3ccd9c | 2663 | fprintf (file, _(" [abi unknown]")); |
103186c6 | 2664 | else if (ABI_N32_P (abfd)) |
be3ccd9c | 2665 | fprintf (file, _(" [abi=N32]")); |
103186c6 | 2666 | else if (ABI_64_P (abfd)) |
be3ccd9c | 2667 | fprintf (file, _(" [abi=64]")); |
252b5132 | 2668 | else |
be3ccd9c | 2669 | fprintf (file, _(" [no abi set]")); |
252b5132 RH |
2670 | |
2671 | if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1) | |
be3ccd9c | 2672 | fprintf (file, _(" [mips1]")); |
252b5132 | 2673 | else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2) |
be3ccd9c | 2674 | fprintf (file, _(" [mips2]")); |
252b5132 | 2675 | else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3) |
be3ccd9c | 2676 | fprintf (file, _(" [mips3]")); |
252b5132 | 2677 | else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4) |
be3ccd9c | 2678 | fprintf (file, _(" [mips4]")); |
84ea6cf2 NC |
2679 | else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5) |
2680 | fprintf (file, _ (" [mips5]")); | |
e7af610e NC |
2681 | else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32) |
2682 | fprintf (file, _ (" [mips32]")); | |
84ea6cf2 NC |
2683 | else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64) |
2684 | fprintf (file, _ (" [mips64]")); | |
252b5132 | 2685 | else |
be3ccd9c | 2686 | fprintf (file, _(" [unknown ISA]")); |
252b5132 RH |
2687 | |
2688 | if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE) | |
be3ccd9c | 2689 | fprintf (file, _(" [32bitmode]")); |
252b5132 | 2690 | else |
be3ccd9c | 2691 | fprintf (file, _(" [not 32bitmode]")); |
252b5132 RH |
2692 | |
2693 | fputc ('\n', file); | |
2694 | ||
2695 | return true; | |
2696 | } | |
2697 | \f | |
2698 | /* Handle a MIPS specific section when reading an object file. This | |
2699 | is called when elfcode.h finds a section with an unknown type. | |
2700 | This routine supports both the 32-bit and 64-bit ELF ABI. | |
2701 | ||
2702 | FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure | |
2703 | how to. */ | |
2704 | ||
2705 | boolean | |
2706 | _bfd_mips_elf_section_from_shdr (abfd, hdr, name) | |
2707 | bfd *abfd; | |
2708 | Elf_Internal_Shdr *hdr; | |
103186c6 | 2709 | char *name; |
252b5132 RH |
2710 | { |
2711 | flagword flags = 0; | |
2712 | ||
2713 | /* There ought to be a place to keep ELF backend specific flags, but | |
2714 | at the moment there isn't one. We just keep track of the | |
2715 | sections by their name, instead. Fortunately, the ABI gives | |
2716 | suggested names for all the MIPS specific sections, so we will | |
2717 | probably get away with this. */ | |
2718 | switch (hdr->sh_type) | |
2719 | { | |
2720 | case SHT_MIPS_LIBLIST: | |
2721 | if (strcmp (name, ".liblist") != 0) | |
2722 | return false; | |
2723 | break; | |
2724 | case SHT_MIPS_MSYM: | |
c6142e5d | 2725 | if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (abfd)) != 0) |
252b5132 RH |
2726 | return false; |
2727 | break; | |
2728 | case SHT_MIPS_CONFLICT: | |
2729 | if (strcmp (name, ".conflict") != 0) | |
2730 | return false; | |
2731 | break; | |
2732 | case SHT_MIPS_GPTAB: | |
2733 | if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0) | |
2734 | return false; | |
2735 | break; | |
2736 | case SHT_MIPS_UCODE: | |
2737 | if (strcmp (name, ".ucode") != 0) | |
2738 | return false; | |
2739 | break; | |
2740 | case SHT_MIPS_DEBUG: | |
2741 | if (strcmp (name, ".mdebug") != 0) | |
2742 | return false; | |
2743 | flags = SEC_DEBUGGING; | |
2744 | break; | |
2745 | case SHT_MIPS_REGINFO: | |
2746 | if (strcmp (name, ".reginfo") != 0 | |
2747 | || hdr->sh_size != sizeof (Elf32_External_RegInfo)) | |
2748 | return false; | |
2749 | flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE); | |
2750 | break; | |
2751 | case SHT_MIPS_IFACE: | |
2752 | if (strcmp (name, ".MIPS.interfaces") != 0) | |
2753 | return false; | |
2754 | break; | |
2755 | case SHT_MIPS_CONTENT: | |
2756 | if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0) | |
2757 | return false; | |
2758 | break; | |
2759 | case SHT_MIPS_OPTIONS: | |
303f629d | 2760 | if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) != 0) |
252b5132 RH |
2761 | return false; |
2762 | break; | |
2763 | case SHT_MIPS_DWARF: | |
2764 | if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0) | |
2765 | return false; | |
2766 | break; | |
2767 | case SHT_MIPS_SYMBOL_LIB: | |
2768 | if (strcmp (name, ".MIPS.symlib") != 0) | |
2769 | return false; | |
2770 | break; | |
2771 | case SHT_MIPS_EVENTS: | |
2772 | if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0 | |
2773 | && strncmp (name, ".MIPS.post_rel", | |
2774 | sizeof ".MIPS.post_rel" - 1) != 0) | |
2775 | return false; | |
2776 | break; | |
2777 | default: | |
2778 | return false; | |
2779 | } | |
2780 | ||
2781 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name)) | |
2782 | return false; | |
2783 | ||
2784 | if (flags) | |
2785 | { | |
2786 | if (! bfd_set_section_flags (abfd, hdr->bfd_section, | |
2787 | (bfd_get_section_flags (abfd, | |
2788 | hdr->bfd_section) | |
2789 | | flags))) | |
2790 | return false; | |
2791 | } | |
2792 | ||
252b5132 RH |
2793 | /* FIXME: We should record sh_info for a .gptab section. */ |
2794 | ||
2795 | /* For a .reginfo section, set the gp value in the tdata information | |
2796 | from the contents of this section. We need the gp value while | |
2797 | processing relocs, so we just get it now. The .reginfo section | |
2798 | is not used in the 64-bit MIPS ELF ABI. */ | |
2799 | if (hdr->sh_type == SHT_MIPS_REGINFO) | |
2800 | { | |
2801 | Elf32_External_RegInfo ext; | |
2802 | Elf32_RegInfo s; | |
2803 | ||
2804 | if (! bfd_get_section_contents (abfd, hdr->bfd_section, (PTR) &ext, | |
2805 | (file_ptr) 0, sizeof ext)) | |
2806 | return false; | |
2807 | bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s); | |
2808 | elf_gp (abfd) = s.ri_gp_value; | |
2809 | } | |
2810 | ||
2811 | /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and | |
2812 | set the gp value based on what we find. We may see both | |
2813 | SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case, | |
2814 | they should agree. */ | |
2815 | if (hdr->sh_type == SHT_MIPS_OPTIONS) | |
2816 | { | |
2817 | bfd_byte *contents, *l, *lend; | |
2818 | ||
2819 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); | |
2820 | if (contents == NULL) | |
2821 | return false; | |
2822 | if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents, | |
2823 | (file_ptr) 0, hdr->sh_size)) | |
2824 | { | |
2825 | free (contents); | |
2826 | return false; | |
2827 | } | |
2828 | l = contents; | |
2829 | lend = contents + hdr->sh_size; | |
2830 | while (l + sizeof (Elf_External_Options) <= lend) | |
2831 | { | |
2832 | Elf_Internal_Options intopt; | |
2833 | ||
2834 | bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l, | |
2835 | &intopt); | |
103186c6 MM |
2836 | if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO) |
2837 | { | |
2838 | Elf64_Internal_RegInfo intreg; | |
2839 | ||
2840 | bfd_mips_elf64_swap_reginfo_in | |
2841 | (abfd, | |
2842 | ((Elf64_External_RegInfo *) | |
2843 | (l + sizeof (Elf_External_Options))), | |
2844 | &intreg); | |
2845 | elf_gp (abfd) = intreg.ri_gp_value; | |
2846 | } | |
2847 | else if (intopt.kind == ODK_REGINFO) | |
252b5132 RH |
2848 | { |
2849 | Elf32_RegInfo intreg; | |
2850 | ||
2851 | bfd_mips_elf32_swap_reginfo_in | |
2852 | (abfd, | |
2853 | ((Elf32_External_RegInfo *) | |
2854 | (l + sizeof (Elf_External_Options))), | |
2855 | &intreg); | |
2856 | elf_gp (abfd) = intreg.ri_gp_value; | |
2857 | } | |
2858 | l += intopt.size; | |
2859 | } | |
2860 | free (contents); | |
2861 | } | |
2862 | ||
2863 | return true; | |
2864 | } | |
2865 | ||
2866 | /* Set the correct type for a MIPS ELF section. We do this by the | |
2867 | section name, which is a hack, but ought to work. This routine is | |
2868 | used by both the 32-bit and the 64-bit ABI. */ | |
2869 | ||
2870 | boolean | |
2871 | _bfd_mips_elf_fake_sections (abfd, hdr, sec) | |
2872 | bfd *abfd; | |
2873 | Elf32_Internal_Shdr *hdr; | |
2874 | asection *sec; | |
2875 | { | |
2876 | register const char *name; | |
2877 | ||
2878 | name = bfd_get_section_name (abfd, sec); | |
2879 | ||
2880 | if (strcmp (name, ".liblist") == 0) | |
2881 | { | |
2882 | hdr->sh_type = SHT_MIPS_LIBLIST; | |
2883 | hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib); | |
2884 | /* The sh_link field is set in final_write_processing. */ | |
2885 | } | |
252b5132 RH |
2886 | else if (strcmp (name, ".conflict") == 0) |
2887 | hdr->sh_type = SHT_MIPS_CONFLICT; | |
2888 | else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0) | |
2889 | { | |
2890 | hdr->sh_type = SHT_MIPS_GPTAB; | |
2891 | hdr->sh_entsize = sizeof (Elf32_External_gptab); | |
2892 | /* The sh_info field is set in final_write_processing. */ | |
2893 | } | |
2894 | else if (strcmp (name, ".ucode") == 0) | |
2895 | hdr->sh_type = SHT_MIPS_UCODE; | |
2896 | else if (strcmp (name, ".mdebug") == 0) | |
2897 | { | |
2898 | hdr->sh_type = SHT_MIPS_DEBUG; | |
2899 | /* In a shared object on Irix 5.3, the .mdebug section has an | |
2900 | entsize of 0. FIXME: Does this matter? */ | |
2901 | if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0) | |
2902 | hdr->sh_entsize = 0; | |
2903 | else | |
2904 | hdr->sh_entsize = 1; | |
2905 | } | |
2906 | else if (strcmp (name, ".reginfo") == 0) | |
2907 | { | |
2908 | hdr->sh_type = SHT_MIPS_REGINFO; | |
2909 | /* In a shared object on Irix 5.3, the .reginfo section has an | |
2910 | entsize of 0x18. FIXME: Does this matter? */ | |
f7cb7d68 | 2911 | if (SGI_COMPAT (abfd)) |
be3ccd9c KH |
2912 | { |
2913 | if ((abfd->flags & DYNAMIC) != 0) | |
2914 | hdr->sh_entsize = sizeof (Elf32_External_RegInfo); | |
2915 | else | |
2916 | hdr->sh_entsize = 1; | |
2917 | } | |
252b5132 | 2918 | else |
be3ccd9c | 2919 | hdr->sh_entsize = sizeof (Elf32_External_RegInfo); |
252b5132 RH |
2920 | } |
2921 | else if (SGI_COMPAT (abfd) | |
2922 | && (strcmp (name, ".hash") == 0 | |
2923 | || strcmp (name, ".dynamic") == 0 | |
2924 | || strcmp (name, ".dynstr") == 0)) | |
2925 | { | |
be3ccd9c KH |
2926 | if (SGI_COMPAT (abfd)) |
2927 | hdr->sh_entsize = 0; | |
252b5132 RH |
2928 | #if 0 |
2929 | /* This isn't how the Irix 6 linker behaves. */ | |
2930 | hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES; | |
2931 | #endif | |
2932 | } | |
2933 | else if (strcmp (name, ".got") == 0 | |
303f629d | 2934 | || strcmp (name, MIPS_ELF_SRDATA_SECTION_NAME (abfd)) == 0 |
252b5132 RH |
2935 | || strcmp (name, ".sdata") == 0 |
2936 | || strcmp (name, ".sbss") == 0 | |
2937 | || strcmp (name, ".lit4") == 0 | |
2938 | || strcmp (name, ".lit8") == 0) | |
2939 | hdr->sh_flags |= SHF_MIPS_GPREL; | |
2940 | else if (strcmp (name, ".MIPS.interfaces") == 0) | |
2941 | { | |
2942 | hdr->sh_type = SHT_MIPS_IFACE; | |
2943 | hdr->sh_flags |= SHF_MIPS_NOSTRIP; | |
2944 | } | |
3f830999 | 2945 | else if (strncmp (name, ".MIPS.content", strlen (".MIPS.content")) == 0) |
252b5132 RH |
2946 | { |
2947 | hdr->sh_type = SHT_MIPS_CONTENT; | |
3f830999 | 2948 | hdr->sh_flags |= SHF_MIPS_NOSTRIP; |
252b5132 RH |
2949 | /* The sh_info field is set in final_write_processing. */ |
2950 | } | |
303f629d | 2951 | else if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0) |
252b5132 RH |
2952 | { |
2953 | hdr->sh_type = SHT_MIPS_OPTIONS; | |
2954 | hdr->sh_entsize = 1; | |
2955 | hdr->sh_flags |= SHF_MIPS_NOSTRIP; | |
2956 | } | |
2957 | else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0) | |
2958 | hdr->sh_type = SHT_MIPS_DWARF; | |
2959 | else if (strcmp (name, ".MIPS.symlib") == 0) | |
2960 | { | |
2961 | hdr->sh_type = SHT_MIPS_SYMBOL_LIB; | |
2962 | /* The sh_link and sh_info fields are set in | |
2963 | final_write_processing. */ | |
2964 | } | |
2965 | else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0 | |
2966 | || strncmp (name, ".MIPS.post_rel", | |
2967 | sizeof ".MIPS.post_rel" - 1) == 0) | |
2968 | { | |
2969 | hdr->sh_type = SHT_MIPS_EVENTS; | |
2970 | hdr->sh_flags |= SHF_MIPS_NOSTRIP; | |
2971 | /* The sh_link field is set in final_write_processing. */ | |
2972 | } | |
c6142e5d MM |
2973 | else if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (abfd)) == 0) |
2974 | { | |
2975 | hdr->sh_type = SHT_MIPS_MSYM; | |
2976 | hdr->sh_flags |= SHF_ALLOC; | |
2977 | hdr->sh_entsize = 8; | |
2978 | } | |
252b5132 | 2979 | |
23bc299b MM |
2980 | /* The generic elf_fake_sections will set up REL_HDR using the |
2981 | default kind of relocations. But, we may actually need both | |
2982 | kinds of relocations, so we set up the second header here. */ | |
2983 | if ((sec->flags & SEC_RELOC) != 0) | |
2984 | { | |
2985 | struct bfd_elf_section_data *esd; | |
2986 | ||
2987 | esd = elf_section_data (sec); | |
2988 | BFD_ASSERT (esd->rel_hdr2 == NULL); | |
be3ccd9c | 2989 | esd->rel_hdr2 |
23bc299b MM |
2990 | = (Elf_Internal_Shdr *) bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr)); |
2991 | if (!esd->rel_hdr2) | |
2992 | return false; | |
2993 | _bfd_elf_init_reloc_shdr (abfd, esd->rel_hdr2, sec, | |
2994 | !elf_section_data (sec)->use_rela_p); | |
2995 | } | |
2996 | ||
252b5132 RH |
2997 | return true; |
2998 | } | |
2999 | ||
3000 | /* Given a BFD section, try to locate the corresponding ELF section | |
3001 | index. This is used by both the 32-bit and the 64-bit ABI. | |
3002 | Actually, it's not clear to me that the 64-bit ABI supports these, | |
3003 | but for non-PIC objects we will certainly want support for at least | |
3004 | the .scommon section. */ | |
3005 | ||
3006 | boolean | |
3007 | _bfd_mips_elf_section_from_bfd_section (abfd, hdr, sec, retval) | |
5f771d47 ILT |
3008 | bfd *abfd ATTRIBUTE_UNUSED; |
3009 | Elf32_Internal_Shdr *hdr ATTRIBUTE_UNUSED; | |
252b5132 RH |
3010 | asection *sec; |
3011 | int *retval; | |
3012 | { | |
3013 | if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0) | |
3014 | { | |
3015 | *retval = SHN_MIPS_SCOMMON; | |
3016 | return true; | |
3017 | } | |
3018 | if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0) | |
3019 | { | |
3020 | *retval = SHN_MIPS_ACOMMON; | |
3021 | return true; | |
3022 | } | |
3023 | return false; | |
3024 | } | |
3025 | ||
3026 | /* When are writing out the .options or .MIPS.options section, | |
3027 | remember the bytes we are writing out, so that we can install the | |
3028 | GP value in the section_processing routine. */ | |
3029 | ||
3030 | boolean | |
3031 | _bfd_mips_elf_set_section_contents (abfd, section, location, offset, count) | |
3032 | bfd *abfd; | |
3033 | sec_ptr section; | |
3034 | PTR location; | |
3035 | file_ptr offset; | |
3036 | bfd_size_type count; | |
3037 | { | |
303f629d | 3038 | if (strcmp (section->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0) |
252b5132 RH |
3039 | { |
3040 | bfd_byte *c; | |
3041 | ||
3042 | if (elf_section_data (section) == NULL) | |
3043 | { | |
3044 | section->used_by_bfd = | |
3045 | (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data)); | |
3046 | if (elf_section_data (section) == NULL) | |
3047 | return false; | |
3048 | } | |
3049 | c = (bfd_byte *) elf_section_data (section)->tdata; | |
3050 | if (c == NULL) | |
3051 | { | |
3052 | bfd_size_type size; | |
3053 | ||
3054 | if (section->_cooked_size != 0) | |
3055 | size = section->_cooked_size; | |
3056 | else | |
3057 | size = section->_raw_size; | |
3058 | c = (bfd_byte *) bfd_zalloc (abfd, size); | |
3059 | if (c == NULL) | |
3060 | return false; | |
3061 | elf_section_data (section)->tdata = (PTR) c; | |
3062 | } | |
3063 | ||
3064 | memcpy (c + offset, location, count); | |
3065 | } | |
3066 | ||
3067 | return _bfd_elf_set_section_contents (abfd, section, location, offset, | |
3068 | count); | |
3069 | } | |
3070 | ||
3071 | /* Work over a section just before writing it out. This routine is | |
3072 | used by both the 32-bit and the 64-bit ABI. FIXME: We recognize | |
3073 | sections that need the SHF_MIPS_GPREL flag by name; there has to be | |
3074 | a better way. */ | |
3075 | ||
3076 | boolean | |
3077 | _bfd_mips_elf_section_processing (abfd, hdr) | |
3078 | bfd *abfd; | |
3079 | Elf_Internal_Shdr *hdr; | |
252b5132 | 3080 | { |
cc3bfcee ILT |
3081 | if (hdr->sh_type == SHT_MIPS_REGINFO |
3082 | && hdr->sh_size > 0) | |
252b5132 RH |
3083 | { |
3084 | bfd_byte buf[4]; | |
3085 | ||
3086 | BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo)); | |
3087 | BFD_ASSERT (hdr->contents == NULL); | |
3088 | ||
3089 | if (bfd_seek (abfd, | |
3090 | hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4, | |
3091 | SEEK_SET) == -1) | |
3092 | return false; | |
3093 | bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf); | |
3094 | if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4) | |
3095 | return false; | |
3096 | } | |
3097 | ||
3098 | if (hdr->sh_type == SHT_MIPS_OPTIONS | |
3099 | && hdr->bfd_section != NULL | |
3100 | && elf_section_data (hdr->bfd_section) != NULL | |
3101 | && elf_section_data (hdr->bfd_section)->tdata != NULL) | |
3102 | { | |
3103 | bfd_byte *contents, *l, *lend; | |
3104 | ||
3105 | /* We stored the section contents in the elf_section_data tdata | |
3106 | field in the set_section_contents routine. We save the | |
3107 | section contents so that we don't have to read them again. | |
3108 | At this point we know that elf_gp is set, so we can look | |
3109 | through the section contents to see if there is an | |
3110 | ODK_REGINFO structure. */ | |
3111 | ||
3112 | contents = (bfd_byte *) elf_section_data (hdr->bfd_section)->tdata; | |
3113 | l = contents; | |
3114 | lend = contents + hdr->sh_size; | |
3115 | while (l + sizeof (Elf_External_Options) <= lend) | |
3116 | { | |
3117 | Elf_Internal_Options intopt; | |
3118 | ||
3119 | bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l, | |
3120 | &intopt); | |
103186c6 MM |
3121 | if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO) |
3122 | { | |
3123 | bfd_byte buf[8]; | |
3124 | ||
3125 | if (bfd_seek (abfd, | |
3126 | (hdr->sh_offset | |
3127 | + (l - contents) | |
3128 | + sizeof (Elf_External_Options) | |
3129 | + (sizeof (Elf64_External_RegInfo) - 8)), | |
3130 | SEEK_SET) == -1) | |
3131 | return false; | |
3132 | bfd_h_put_64 (abfd, elf_gp (abfd), buf); | |
3133 | if (bfd_write (buf, 1, 8, abfd) != 8) | |
3134 | return false; | |
3135 | } | |
3136 | else if (intopt.kind == ODK_REGINFO) | |
252b5132 RH |
3137 | { |
3138 | bfd_byte buf[4]; | |
3139 | ||
3140 | if (bfd_seek (abfd, | |
3141 | (hdr->sh_offset | |
3142 | + (l - contents) | |
3143 | + sizeof (Elf_External_Options) | |
3144 | + (sizeof (Elf32_External_RegInfo) - 4)), | |
be3ccd9c | 3145 | SEEK_SET) == -1) |
252b5132 RH |
3146 | return false; |
3147 | bfd_h_put_32 (abfd, elf_gp (abfd), buf); | |
3148 | if (bfd_write (buf, 1, 4, abfd) != 4) | |
3149 | return false; | |
3150 | } | |
3151 | l += intopt.size; | |
3152 | } | |
3153 | } | |
3154 | ||
103186c6 MM |
3155 | if (hdr->bfd_section != NULL) |
3156 | { | |
3157 | const char *name = bfd_get_section_name (abfd, hdr->bfd_section); | |
3158 | ||
3159 | if (strcmp (name, ".sdata") == 0 | |
3160 | || strcmp (name, ".lit8") == 0 | |
3161 | || strcmp (name, ".lit4") == 0) | |
3162 | { | |
3163 | hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL; | |
3164 | hdr->sh_type = SHT_PROGBITS; | |
3165 | } | |
3166 | else if (strcmp (name, ".sbss") == 0) | |
3167 | { | |
3168 | hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL; | |
3169 | hdr->sh_type = SHT_NOBITS; | |
3170 | } | |
3171 | else if (strcmp (name, MIPS_ELF_SRDATA_SECTION_NAME (abfd)) == 0) | |
3172 | { | |
3173 | hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL; | |
3174 | hdr->sh_type = SHT_PROGBITS; | |
3175 | } | |
3176 | else if (strcmp (name, ".compact_rel") == 0) | |
3177 | { | |
3178 | hdr->sh_flags = 0; | |
3179 | hdr->sh_type = SHT_PROGBITS; | |
3180 | } | |
3181 | else if (strcmp (name, ".rtproc") == 0) | |
3182 | { | |
3183 | if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0) | |
3184 | { | |
3185 | unsigned int adjust; | |
3186 | ||
3187 | adjust = hdr->sh_size % hdr->sh_addralign; | |
3188 | if (adjust != 0) | |
3189 | hdr->sh_size += hdr->sh_addralign - adjust; | |
3190 | } | |
3191 | } | |
3192 | } | |
3193 | ||
3194 | return true; | |
252b5132 RH |
3195 | } |
3196 | \f | |
3197 | /* MIPS ELF uses two common sections. One is the usual one, and the | |
3198 | other is for small objects. All the small objects are kept | |
3199 | together, and then referenced via the gp pointer, which yields | |
3200 | faster assembler code. This is what we use for the small common | |
3201 | section. This approach is copied from ecoff.c. */ | |
3202 | static asection mips_elf_scom_section; | |
3203 | static asymbol mips_elf_scom_symbol; | |
3204 | static asymbol *mips_elf_scom_symbol_ptr; | |
3205 | ||
3206 | /* MIPS ELF also uses an acommon section, which represents an | |
3207 | allocated common symbol which may be overridden by a | |
3208 | definition in a shared library. */ | |
3209 | static asection mips_elf_acom_section; | |
3210 | static asymbol mips_elf_acom_symbol; | |
3211 | static asymbol *mips_elf_acom_symbol_ptr; | |
3212 | ||
252b5132 RH |
3213 | /* Handle the special MIPS section numbers that a symbol may use. |
3214 | This is used for both the 32-bit and the 64-bit ABI. */ | |
3215 | ||
3216 | void | |
3217 | _bfd_mips_elf_symbol_processing (abfd, asym) | |
3218 | bfd *abfd; | |
3219 | asymbol *asym; | |
3220 | { | |
3221 | elf_symbol_type *elfsym; | |
3222 | ||
3223 | elfsym = (elf_symbol_type *) asym; | |
3224 | switch (elfsym->internal_elf_sym.st_shndx) | |
3225 | { | |
3226 | case SHN_MIPS_ACOMMON: | |
3227 | /* This section is used in a dynamically linked executable file. | |
3228 | It is an allocated common section. The dynamic linker can | |
3229 | either resolve these symbols to something in a shared | |
3230 | library, or it can just leave them here. For our purposes, | |
3231 | we can consider these symbols to be in a new section. */ | |
3232 | if (mips_elf_acom_section.name == NULL) | |
3233 | { | |
3234 | /* Initialize the acommon section. */ | |
3235 | mips_elf_acom_section.name = ".acommon"; | |
3236 | mips_elf_acom_section.flags = SEC_ALLOC; | |
3237 | mips_elf_acom_section.output_section = &mips_elf_acom_section; | |
3238 | mips_elf_acom_section.symbol = &mips_elf_acom_symbol; | |
3239 | mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr; | |
3240 | mips_elf_acom_symbol.name = ".acommon"; | |
3241 | mips_elf_acom_symbol.flags = BSF_SECTION_SYM; | |
3242 | mips_elf_acom_symbol.section = &mips_elf_acom_section; | |
3243 | mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol; | |
3244 | } | |
3245 | asym->section = &mips_elf_acom_section; | |
3246 | break; | |
3247 | ||
3248 | case SHN_COMMON: | |
3249 | /* Common symbols less than the GP size are automatically | |
7403cb63 MM |
3250 | treated as SHN_MIPS_SCOMMON symbols on IRIX5. */ |
3251 | if (asym->value > elf_gp_size (abfd) | |
3252 | || IRIX_COMPAT (abfd) == ict_irix6) | |
252b5132 RH |
3253 | break; |
3254 | /* Fall through. */ | |
3255 | case SHN_MIPS_SCOMMON: | |
3256 | if (mips_elf_scom_section.name == NULL) | |
3257 | { | |
3258 | /* Initialize the small common section. */ | |
3259 | mips_elf_scom_section.name = ".scommon"; | |
3260 | mips_elf_scom_section.flags = SEC_IS_COMMON; | |
3261 | mips_elf_scom_section.output_section = &mips_elf_scom_section; | |
3262 | mips_elf_scom_section.symbol = &mips_elf_scom_symbol; | |
3263 | mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr; | |
3264 | mips_elf_scom_symbol.name = ".scommon"; | |
3265 | mips_elf_scom_symbol.flags = BSF_SECTION_SYM; | |
3266 | mips_elf_scom_symbol.section = &mips_elf_scom_section; | |
3267 | mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol; | |
3268 | } | |
3269 | asym->section = &mips_elf_scom_section; | |
3270 | asym->value = elfsym->internal_elf_sym.st_size; | |
3271 | break; | |
3272 | ||
3273 | case SHN_MIPS_SUNDEFINED: | |
3274 | asym->section = bfd_und_section_ptr; | |
3275 | break; | |
3276 | ||
3277 | #if 0 /* for SGI_COMPAT */ | |
3278 | case SHN_MIPS_TEXT: | |
3279 | asym->section = mips_elf_text_section_ptr; | |
3280 | break; | |
3281 | ||
3282 | case SHN_MIPS_DATA: | |
3283 | asym->section = mips_elf_data_section_ptr; | |
3284 | break; | |
3285 | #endif | |
3286 | } | |
3287 | } | |
3288 | \f | |
3289 | /* When creating an Irix 5 executable, we need REGINFO and RTPROC | |
3290 | segments. */ | |
3291 | ||
103186c6 MM |
3292 | int |
3293 | _bfd_mips_elf_additional_program_headers (abfd) | |
252b5132 RH |
3294 | bfd *abfd; |
3295 | { | |
3296 | asection *s; | |
303f629d | 3297 | int ret = 0; |
252b5132 | 3298 | |
303f629d | 3299 | /* See if we need a PT_MIPS_REGINFO segment. */ |
252b5132 | 3300 | s = bfd_get_section_by_name (abfd, ".reginfo"); |
303f629d MM |
3301 | if (s && (s->flags & SEC_LOAD)) |
3302 | ++ret; | |
252b5132 | 3303 | |
303f629d MM |
3304 | /* See if we need a PT_MIPS_OPTIONS segment. */ |
3305 | if (IRIX_COMPAT (abfd) == ict_irix6 | |
be3ccd9c | 3306 | && bfd_get_section_by_name (abfd, |
303f629d MM |
3307 | MIPS_ELF_OPTIONS_SECTION_NAME (abfd))) |
3308 | ++ret; | |
3309 | ||
3310 | /* See if we need a PT_MIPS_RTPROC segment. */ | |
3311 | if (IRIX_COMPAT (abfd) == ict_irix5 | |
3312 | && bfd_get_section_by_name (abfd, ".dynamic") | |
3313 | && bfd_get_section_by_name (abfd, ".mdebug")) | |
3314 | ++ret; | |
252b5132 RH |
3315 | |
3316 | return ret; | |
3317 | } | |
3318 | ||
3319 | /* Modify the segment map for an Irix 5 executable. */ | |
3320 | ||
103186c6 MM |
3321 | boolean |
3322 | _bfd_mips_elf_modify_segment_map (abfd) | |
252b5132 RH |
3323 | bfd *abfd; |
3324 | { | |
3325 | asection *s; | |
3326 | struct elf_segment_map *m, **pm; | |
3327 | ||
252b5132 RH |
3328 | /* If there is a .reginfo section, we need a PT_MIPS_REGINFO |
3329 | segment. */ | |
3330 | s = bfd_get_section_by_name (abfd, ".reginfo"); | |
3331 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3332 | { | |
3333 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) | |
3334 | if (m->p_type == PT_MIPS_REGINFO) | |
3335 | break; | |
3336 | if (m == NULL) | |
3337 | { | |
3338 | m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m); | |
3339 | if (m == NULL) | |
3340 | return false; | |
3341 | ||
3342 | m->p_type = PT_MIPS_REGINFO; | |
3343 | m->count = 1; | |
3344 | m->sections[0] = s; | |
3345 | ||
3346 | /* We want to put it after the PHDR and INTERP segments. */ | |
3347 | pm = &elf_tdata (abfd)->segment_map; | |
3348 | while (*pm != NULL | |
3349 | && ((*pm)->p_type == PT_PHDR | |
3350 | || (*pm)->p_type == PT_INTERP)) | |
3351 | pm = &(*pm)->next; | |
3352 | ||
3353 | m->next = *pm; | |
3354 | *pm = m; | |
3355 | } | |
3356 | } | |
3357 | ||
303f629d MM |
3358 | /* For IRIX 6, we don't have .mdebug sections, nor does anything but |
3359 | .dynamic end up in PT_DYNAMIC. However, we do have to insert a | |
3360 | PT_OPTIONS segement immediately following the program header | |
3361 | table. */ | |
3362 | if (IRIX_COMPAT (abfd) == ict_irix6) | |
252b5132 | 3363 | { |
303f629d MM |
3364 | asection *s; |
3365 | ||
3366 | for (s = abfd->sections; s; s = s->next) | |
3367 | if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS) | |
252b5132 | 3368 | break; |
303f629d MM |
3369 | |
3370 | if (s) | |
252b5132 | 3371 | { |
303f629d MM |
3372 | struct elf_segment_map *options_segment; |
3373 | ||
435394bf MM |
3374 | /* Usually, there's a program header table. But, sometimes |
3375 | there's not (like when running the `ld' testsuite). So, | |
3376 | if there's no program header table, we just put the | |
3377 | options segement at the end. */ | |
be3ccd9c | 3378 | for (pm = &elf_tdata (abfd)->segment_map; |
435394bf MM |
3379 | *pm != NULL; |
3380 | pm = &(*pm)->next) | |
3381 | if ((*pm)->p_type == PT_PHDR) | |
303f629d MM |
3382 | break; |
3383 | ||
be3ccd9c | 3384 | options_segment = bfd_zalloc (abfd, |
303f629d | 3385 | sizeof (struct elf_segment_map)); |
435394bf | 3386 | options_segment->next = *pm; |
303f629d MM |
3387 | options_segment->p_type = PT_MIPS_OPTIONS; |
3388 | options_segment->p_flags = PF_R; | |
3389 | options_segment->p_flags_valid = true; | |
3390 | options_segment->count = 1; | |
3391 | options_segment->sections[0] = s; | |
435394bf | 3392 | *pm = options_segment; |
303f629d MM |
3393 | } |
3394 | } | |
3395 | else | |
3396 | { | |
f7cb7d68 | 3397 | if (IRIX_COMPAT (abfd) == ict_irix5) |
303f629d | 3398 | { |
f7cb7d68 UC |
3399 | /* If there are .dynamic and .mdebug sections, we make a room |
3400 | for the RTPROC header. FIXME: Rewrite without section names. */ | |
3401 | if (bfd_get_section_by_name (abfd, ".interp") == NULL | |
3402 | && bfd_get_section_by_name (abfd, ".dynamic") != NULL | |
3403 | && bfd_get_section_by_name (abfd, ".mdebug") != NULL) | |
252b5132 | 3404 | { |
f7cb7d68 UC |
3405 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) |
3406 | if (m->p_type == PT_MIPS_RTPROC) | |
3407 | break; | |
303f629d | 3408 | if (m == NULL) |
f7cb7d68 UC |
3409 | { |
3410 | m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m); | |
3411 | if (m == NULL) | |
3412 | return false; | |
252b5132 | 3413 | |
f7cb7d68 | 3414 | m->p_type = PT_MIPS_RTPROC; |
252b5132 | 3415 | |
f7cb7d68 UC |
3416 | s = bfd_get_section_by_name (abfd, ".rtproc"); |
3417 | if (s == NULL) | |
3418 | { | |
3419 | m->count = 0; | |
3420 | m->p_flags = 0; | |
3421 | m->p_flags_valid = 1; | |
3422 | } | |
3423 | else | |
3424 | { | |
3425 | m->count = 1; | |
3426 | m->sections[0] = s; | |
3427 | } | |
303f629d | 3428 | |
f7cb7d68 UC |
3429 | /* We want to put it after the DYNAMIC segment. */ |
3430 | pm = &elf_tdata (abfd)->segment_map; | |
3431 | while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC) | |
3432 | pm = &(*pm)->next; | |
3433 | if (*pm != NULL) | |
3434 | pm = &(*pm)->next; | |
303f629d | 3435 | |
f7cb7d68 UC |
3436 | m->next = *pm; |
3437 | *pm = m; | |
3438 | } | |
303f629d | 3439 | } |
252b5132 | 3440 | } |
303f629d MM |
3441 | /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic, |
3442 | .dynstr, .dynsym, and .hash sections, and everything in | |
3443 | between. */ | |
f7cb7d68 UC |
3444 | for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; |
3445 | pm = &(*pm)->next) | |
303f629d MM |
3446 | if ((*pm)->p_type == PT_DYNAMIC) |
3447 | break; | |
3448 | m = *pm; | |
f7cb7d68 UC |
3449 | if (IRIX_COMPAT (abfd) == ict_none) |
3450 | { | |
3451 | /* For a normal mips executable the permissions for the PT_DYNAMIC | |
3452 | segment are read, write and execute. We do that here since | |
3453 | the code in elf.c sets only the read permission. This matters | |
be3ccd9c | 3454 | sometimes for the dynamic linker. */ |
f7cb7d68 UC |
3455 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) |
3456 | { | |
3457 | m->p_flags = PF_R | PF_W | PF_X; | |
3458 | m->p_flags_valid = 1; | |
3459 | } | |
3460 | } | |
303f629d | 3461 | if (m != NULL |
f7cb7d68 | 3462 | && m->count == 1 && strcmp (m->sections[0]->name, ".dynamic") == 0) |
252b5132 | 3463 | { |
be3ccd9c KH |
3464 | static const char *sec_names[] = { |
3465 | ".dynamic", ".dynstr", ".dynsym", ".hash" | |
3466 | }; | |
303f629d MM |
3467 | bfd_vma low, high; |
3468 | unsigned int i, c; | |
3469 | struct elf_segment_map *n; | |
3470 | ||
3471 | low = 0xffffffff; | |
3472 | high = 0; | |
3473 | for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++) | |
252b5132 | 3474 | { |
303f629d MM |
3475 | s = bfd_get_section_by_name (abfd, sec_names[i]); |
3476 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3477 | { | |
3478 | bfd_size_type sz; | |
3479 | ||
3480 | if (low > s->vma) | |
3481 | low = s->vma; | |
3482 | sz = s->_cooked_size; | |
3483 | if (sz == 0) | |
3484 | sz = s->_raw_size; | |
3485 | if (high < s->vma + sz) | |
3486 | high = s->vma + sz; | |
3487 | } | |
252b5132 | 3488 | } |
252b5132 | 3489 | |
303f629d MM |
3490 | c = 0; |
3491 | for (s = abfd->sections; s != NULL; s = s->next) | |
3492 | if ((s->flags & SEC_LOAD) != 0 | |
3493 | && s->vma >= low | |
3494 | && ((s->vma | |
f7cb7d68 UC |
3495 | + (s->_cooked_size != |
3496 | 0 ? s->_cooked_size : s->_raw_size)) <= high)) | |
303f629d MM |
3497 | ++c; |
3498 | ||
3499 | n = ((struct elf_segment_map *) | |
3500 | bfd_zalloc (abfd, sizeof *n + (c - 1) * sizeof (asection *))); | |
3501 | if (n == NULL) | |
3502 | return false; | |
3503 | *n = *m; | |
3504 | n->count = c; | |
252b5132 | 3505 | |
303f629d MM |
3506 | i = 0; |
3507 | for (s = abfd->sections; s != NULL; s = s->next) | |
252b5132 | 3508 | { |
303f629d MM |
3509 | if ((s->flags & SEC_LOAD) != 0 |
3510 | && s->vma >= low | |
3511 | && ((s->vma | |
3512 | + (s->_cooked_size != 0 ? | |
f7cb7d68 | 3513 | s->_cooked_size : s->_raw_size)) <= high)) |
303f629d MM |
3514 | { |
3515 | n->sections[i] = s; | |
3516 | ++i; | |
3517 | } | |
252b5132 | 3518 | } |
252b5132 | 3519 | |
303f629d MM |
3520 | *pm = n; |
3521 | } | |
252b5132 RH |
3522 | } |
3523 | ||
3524 | return true; | |
3525 | } | |
3526 | \f | |
3527 | /* The structure of the runtime procedure descriptor created by the | |
3528 | loader for use by the static exception system. */ | |
3529 | ||
3530 | typedef struct runtime_pdr { | |
3531 | bfd_vma adr; /* memory address of start of procedure */ | |
3532 | long regmask; /* save register mask */ | |
3533 | long regoffset; /* save register offset */ | |
3534 | long fregmask; /* save floating point register mask */ | |
3535 | long fregoffset; /* save floating point register offset */ | |
3536 | long frameoffset; /* frame size */ | |
3537 | short framereg; /* frame pointer register */ | |
3538 | short pcreg; /* offset or reg of return pc */ | |
3539 | long irpss; /* index into the runtime string table */ | |
3540 | long reserved; | |
3541 | struct exception_info *exception_info;/* pointer to exception array */ | |
3542 | } RPDR, *pRPDR; | |
be3ccd9c | 3543 | #define cbRPDR sizeof (RPDR) |
252b5132 RH |
3544 | #define rpdNil ((pRPDR) 0) |
3545 | ||
3546 | /* Swap RPDR (runtime procedure table entry) for output. */ | |
3547 | ||
3548 | static void ecoff_swap_rpdr_out | |
3549 | PARAMS ((bfd *, const RPDR *, struct rpdr_ext *)); | |
3550 | ||
3551 | static void | |
3552 | ecoff_swap_rpdr_out (abfd, in, ex) | |
3553 | bfd *abfd; | |
3554 | const RPDR *in; | |
3555 | struct rpdr_ext *ex; | |
3556 | { | |
3557 | /* ecoff_put_off was defined in ecoffswap.h. */ | |
3558 | ecoff_put_off (abfd, in->adr, (bfd_byte *) ex->p_adr); | |
3559 | bfd_h_put_32 (abfd, in->regmask, (bfd_byte *) ex->p_regmask); | |
3560 | bfd_h_put_32 (abfd, in->regoffset, (bfd_byte *) ex->p_regoffset); | |
3561 | bfd_h_put_32 (abfd, in->fregmask, (bfd_byte *) ex->p_fregmask); | |
3562 | bfd_h_put_32 (abfd, in->fregoffset, (bfd_byte *) ex->p_fregoffset); | |
3563 | bfd_h_put_32 (abfd, in->frameoffset, (bfd_byte *) ex->p_frameoffset); | |
3564 | ||
3565 | bfd_h_put_16 (abfd, in->framereg, (bfd_byte *) ex->p_framereg); | |
3566 | bfd_h_put_16 (abfd, in->pcreg, (bfd_byte *) ex->p_pcreg); | |
3567 | ||
3568 | bfd_h_put_32 (abfd, in->irpss, (bfd_byte *) ex->p_irpss); | |
3569 | #if 0 /* FIXME */ | |
3570 | ecoff_put_off (abfd, in->exception_info, (bfd_byte *) ex->p_exception_info); | |
3571 | #endif | |
3572 | } | |
3573 | \f | |
3574 | /* Read ECOFF debugging information from a .mdebug section into a | |
3575 | ecoff_debug_info structure. */ | |
3576 | ||
3577 | boolean | |
3578 | _bfd_mips_elf_read_ecoff_info (abfd, section, debug) | |
3579 | bfd *abfd; | |
3580 | asection *section; | |
3581 | struct ecoff_debug_info *debug; | |
3582 | { | |
3583 | HDRR *symhdr; | |
3584 | const struct ecoff_debug_swap *swap; | |
3585 | char *ext_hdr = NULL; | |
3586 | ||
3587 | swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap; | |
be3ccd9c | 3588 | memset (debug, 0, sizeof (*debug)); |
252b5132 RH |
3589 | |
3590 | ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size); | |
3591 | if (ext_hdr == NULL && swap->external_hdr_size != 0) | |
3592 | goto error_return; | |
3593 | ||
3594 | if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0, | |
3595 | swap->external_hdr_size) | |
3596 | == false) | |
3597 | goto error_return; | |
3598 | ||
3599 | symhdr = &debug->symbolic_header; | |
3600 | (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr); | |
3601 | ||
3602 | /* The symbolic header contains absolute file offsets and sizes to | |
3603 | read. */ | |
3604 | #define READ(ptr, offset, count, size, type) \ | |
3605 | if (symhdr->count == 0) \ | |
3606 | debug->ptr = NULL; \ | |
3607 | else \ | |
3608 | { \ | |
3609 | debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \ | |
3610 | if (debug->ptr == NULL) \ | |
3611 | goto error_return; \ | |
3612 | if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \ | |
3613 | || (bfd_read (debug->ptr, size, symhdr->count, \ | |
3614 | abfd) != size * symhdr->count)) \ | |
3615 | goto error_return; \ | |
3616 | } | |
3617 | ||
3618 | READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *); | |
3619 | READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR); | |
3620 | READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR); | |
3621 | READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR); | |
3622 | READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR); | |
3623 | READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext), | |
3624 | union aux_ext *); | |
3625 | READ (ss, cbSsOffset, issMax, sizeof (char), char *); | |
3626 | READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *); | |
3627 | READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR); | |
3628 | READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR); | |
3629 | READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR); | |
3630 | #undef READ | |
3631 | ||
3632 | debug->fdr = NULL; | |
3633 | debug->adjust = NULL; | |
3634 | ||
3635 | return true; | |
3636 | ||
3637 | error_return: | |
3638 | if (ext_hdr != NULL) | |
3639 | free (ext_hdr); | |
3640 | if (debug->line != NULL) | |
3641 | free (debug->line); | |
3642 | if (debug->external_dnr != NULL) | |
3643 | free (debug->external_dnr); | |
3644 | if (debug->external_pdr != NULL) | |
3645 | free (debug->external_pdr); | |
3646 | if (debug->external_sym != NULL) | |
3647 | free (debug->external_sym); | |
3648 | if (debug->external_opt != NULL) | |
3649 | free (debug->external_opt); | |
3650 | if (debug->external_aux != NULL) | |
3651 | free (debug->external_aux); | |
3652 | if (debug->ss != NULL) | |
3653 | free (debug->ss); | |
3654 | if (debug->ssext != NULL) | |
3655 | free (debug->ssext); | |
3656 | if (debug->external_fdr != NULL) | |
3657 | free (debug->external_fdr); | |
3658 | if (debug->external_rfd != NULL) | |
3659 | free (debug->external_rfd); | |
3660 | if (debug->external_ext != NULL) | |
3661 | free (debug->external_ext); | |
3662 | return false; | |
3663 | } | |
3664 | \f | |
3665 | /* MIPS ELF local labels start with '$', not 'L'. */ | |
3666 | ||
252b5132 RH |
3667 | static boolean |
3668 | mips_elf_is_local_label_name (abfd, name) | |
3669 | bfd *abfd; | |
3670 | const char *name; | |
3671 | { | |
3672 | if (name[0] == '$') | |
3673 | return true; | |
3674 | ||
3675 | /* On Irix 6, the labels go back to starting with '.', so we accept | |
3676 | the generic ELF local label syntax as well. */ | |
3677 | return _bfd_elf_is_local_label_name (abfd, name); | |
3678 | } | |
3679 | ||
3680 | /* MIPS ELF uses a special find_nearest_line routine in order the | |
3681 | handle the ECOFF debugging information. */ | |
3682 | ||
be3ccd9c | 3683 | struct mips_elf_find_line { |
252b5132 RH |
3684 | struct ecoff_debug_info d; |
3685 | struct ecoff_find_line i; | |
3686 | }; | |
3687 | ||
3688 | boolean | |
3689 | _bfd_mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr, | |
3690 | functionname_ptr, line_ptr) | |
3691 | bfd *abfd; | |
3692 | asection *section; | |
3693 | asymbol **symbols; | |
3694 | bfd_vma offset; | |
3695 | const char **filename_ptr; | |
3696 | const char **functionname_ptr; | |
3697 | unsigned int *line_ptr; | |
3698 | { | |
3699 | asection *msec; | |
3700 | ||
3701 | if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset, | |
3702 | filename_ptr, functionname_ptr, | |
3703 | line_ptr)) | |
3704 | return true; | |
3705 | ||
3706 | if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset, | |
3707 | filename_ptr, functionname_ptr, | |
be3ccd9c | 3708 | line_ptr, |
5e38c3b8 | 3709 | ABI_64_P (abfd) ? 8 : 0)) |
252b5132 RH |
3710 | return true; |
3711 | ||
3712 | msec = bfd_get_section_by_name (abfd, ".mdebug"); | |
3713 | if (msec != NULL) | |
3714 | { | |
3715 | flagword origflags; | |
3716 | struct mips_elf_find_line *fi; | |
3717 | const struct ecoff_debug_swap * const swap = | |
3718 | get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap; | |
3719 | ||
3720 | /* If we are called during a link, mips_elf_final_link may have | |
3721 | cleared the SEC_HAS_CONTENTS field. We force it back on here | |
3722 | if appropriate (which it normally will be). */ | |
3723 | origflags = msec->flags; | |
3724 | if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS) | |
3725 | msec->flags |= SEC_HAS_CONTENTS; | |
3726 | ||
3727 | fi = elf_tdata (abfd)->find_line_info; | |
3728 | if (fi == NULL) | |
3729 | { | |
3730 | bfd_size_type external_fdr_size; | |
3731 | char *fraw_src; | |
3732 | char *fraw_end; | |
3733 | struct fdr *fdr_ptr; | |
3734 | ||
3735 | fi = ((struct mips_elf_find_line *) | |
3736 | bfd_zalloc (abfd, sizeof (struct mips_elf_find_line))); | |
3737 | if (fi == NULL) | |
3738 | { | |
3739 | msec->flags = origflags; | |
3740 | return false; | |
3741 | } | |
3742 | ||
3743 | if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d)) | |
3744 | { | |
3745 | msec->flags = origflags; | |
3746 | return false; | |
3747 | } | |
3748 | ||
3749 | /* Swap in the FDR information. */ | |
3750 | fi->d.fdr = ((struct fdr *) | |
3751 | bfd_alloc (abfd, | |
3752 | (fi->d.symbolic_header.ifdMax * | |
3753 | sizeof (struct fdr)))); | |
3754 | if (fi->d.fdr == NULL) | |
3755 | { | |
3756 | msec->flags = origflags; | |
3757 | return false; | |
3758 | } | |
3759 | external_fdr_size = swap->external_fdr_size; | |
3760 | fdr_ptr = fi->d.fdr; | |
3761 | fraw_src = (char *) fi->d.external_fdr; | |
3762 | fraw_end = (fraw_src | |
3763 | + fi->d.symbolic_header.ifdMax * external_fdr_size); | |
3764 | for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++) | |
3765 | (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr); | |
3766 | ||
3767 | elf_tdata (abfd)->find_line_info = fi; | |
3768 | ||
3769 | /* Note that we don't bother to ever free this information. | |
3770 | find_nearest_line is either called all the time, as in | |
3771 | objdump -l, so the information should be saved, or it is | |
3772 | rarely called, as in ld error messages, so the memory | |
3773 | wasted is unimportant. Still, it would probably be a | |
3774 | good idea for free_cached_info to throw it away. */ | |
3775 | } | |
3776 | ||
3777 | if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap, | |
3778 | &fi->i, filename_ptr, functionname_ptr, | |
3779 | line_ptr)) | |
3780 | { | |
3781 | msec->flags = origflags; | |
3782 | return true; | |
3783 | } | |
3784 | ||
3785 | msec->flags = origflags; | |
3786 | } | |
3787 | ||
3788 | /* Fall back on the generic ELF find_nearest_line routine. */ | |
3789 | ||
3790 | return _bfd_elf_find_nearest_line (abfd, section, symbols, offset, | |
3791 | filename_ptr, functionname_ptr, | |
3792 | line_ptr); | |
3793 | } | |
3794 | \f | |
3795 | /* The mips16 compiler uses a couple of special sections to handle | |
3796 | floating point arguments. | |
3797 | ||
3798 | Section names that look like .mips16.fn.FNNAME contain stubs that | |
3799 | copy floating point arguments from the fp regs to the gp regs and | |
3800 | then jump to FNNAME. If any 32 bit function calls FNNAME, the | |
3801 | call should be redirected to the stub instead. If no 32 bit | |
3802 | function calls FNNAME, the stub should be discarded. We need to | |
3803 | consider any reference to the function, not just a call, because | |
3804 | if the address of the function is taken we will need the stub, | |
3805 | since the address might be passed to a 32 bit function. | |
3806 | ||
3807 | Section names that look like .mips16.call.FNNAME contain stubs | |
3808 | that copy floating point arguments from the gp regs to the fp | |
3809 | regs and then jump to FNNAME. If FNNAME is a 32 bit function, | |
3810 | then any 16 bit function that calls FNNAME should be redirected | |
3811 | to the stub instead. If FNNAME is not a 32 bit function, the | |
3812 | stub should be discarded. | |
3813 | ||
3814 | .mips16.call.fp.FNNAME sections are similar, but contain stubs | |
3815 | which call FNNAME and then copy the return value from the fp regs | |
3816 | to the gp regs. These stubs store the return value in $18 while | |
3817 | calling FNNAME; any function which might call one of these stubs | |
3818 | must arrange to save $18 around the call. (This case is not | |
3819 | needed for 32 bit functions that call 16 bit functions, because | |
3820 | 16 bit functions always return floating point values in both | |
3821 | $f0/$f1 and $2/$3.) | |
3822 | ||
3823 | Note that in all cases FNNAME might be defined statically. | |
3824 | Therefore, FNNAME is not used literally. Instead, the relocation | |
3825 | information will indicate which symbol the section is for. | |
3826 | ||
3827 | We record any stubs that we find in the symbol table. */ | |
3828 | ||
3829 | #define FN_STUB ".mips16.fn." | |
3830 | #define CALL_STUB ".mips16.call." | |
3831 | #define CALL_FP_STUB ".mips16.call.fp." | |
3832 | ||
252b5132 RH |
3833 | /* MIPS ELF linker hash table. */ |
3834 | ||
be3ccd9c | 3835 | struct mips_elf_link_hash_table { |
252b5132 RH |
3836 | struct elf_link_hash_table root; |
3837 | #if 0 | |
3838 | /* We no longer use this. */ | |
3839 | /* String section indices for the dynamic section symbols. */ | |
3840 | bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES]; | |
3841 | #endif | |
3842 | /* The number of .rtproc entries. */ | |
3843 | bfd_size_type procedure_count; | |
3844 | /* The size of the .compact_rel section (if SGI_COMPAT). */ | |
3845 | bfd_size_type compact_rel_size; | |
3846 | /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic | |
be3ccd9c | 3847 | entry is set to the address of __rld_obj_head as in Irix 5. */ |
252b5132 RH |
3848 | boolean use_rld_obj_head; |
3849 | /* This is the value of the __rld_map or __rld_obj_head symbol. */ | |
3850 | bfd_vma rld_value; | |
be3ccd9c | 3851 | /* This is set if we see any mips16 stub sections. */ |
252b5132 RH |
3852 | boolean mips16_stubs_seen; |
3853 | }; | |
3854 | ||
3855 | /* Look up an entry in a MIPS ELF linker hash table. */ | |
3856 | ||
3857 | #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \ | |
3858 | ((struct mips_elf_link_hash_entry *) \ | |
3859 | elf_link_hash_lookup (&(table)->root, (string), (create), \ | |
3860 | (copy), (follow))) | |
3861 | ||
3862 | /* Traverse a MIPS ELF linker hash table. */ | |
3863 | ||
3864 | #define mips_elf_link_hash_traverse(table, func, info) \ | |
3865 | (elf_link_hash_traverse \ | |
3866 | (&(table)->root, \ | |
3867 | (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \ | |
3868 | (info))) | |
3869 | ||
3870 | /* Get the MIPS ELF linker hash table from a link_info structure. */ | |
3871 | ||
3872 | #define mips_elf_hash_table(p) \ | |
3873 | ((struct mips_elf_link_hash_table *) ((p)->hash)) | |
3874 | ||
3875 | static boolean mips_elf_output_extsym | |
3876 | PARAMS ((struct mips_elf_link_hash_entry *, PTR)); | |
3877 | ||
3878 | /* Create an entry in a MIPS ELF linker hash table. */ | |
3879 | ||
3880 | static struct bfd_hash_entry * | |
3881 | mips_elf_link_hash_newfunc (entry, table, string) | |
3882 | struct bfd_hash_entry *entry; | |
3883 | struct bfd_hash_table *table; | |
3884 | const char *string; | |
3885 | { | |
3886 | struct mips_elf_link_hash_entry *ret = | |
3887 | (struct mips_elf_link_hash_entry *) entry; | |
3888 | ||
3889 | /* Allocate the structure if it has not already been allocated by a | |
3890 | subclass. */ | |
3891 | if (ret == (struct mips_elf_link_hash_entry *) NULL) | |
3892 | ret = ((struct mips_elf_link_hash_entry *) | |
3893 | bfd_hash_allocate (table, | |
3894 | sizeof (struct mips_elf_link_hash_entry))); | |
3895 | if (ret == (struct mips_elf_link_hash_entry *) NULL) | |
3896 | return (struct bfd_hash_entry *) ret; | |
3897 | ||
3898 | /* Call the allocation method of the superclass. */ | |
3899 | ret = ((struct mips_elf_link_hash_entry *) | |
3900 | _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, | |
3901 | table, string)); | |
3902 | if (ret != (struct mips_elf_link_hash_entry *) NULL) | |
3903 | { | |
3904 | /* Set local fields. */ | |
3905 | memset (&ret->esym, 0, sizeof (EXTR)); | |
3906 | /* We use -2 as a marker to indicate that the information has | |
3907 | not been set. -1 means there is no associated ifd. */ | |
3908 | ret->esym.ifd = -2; | |
a3c7651d | 3909 | ret->possibly_dynamic_relocs = 0; |
c6142e5d | 3910 | ret->min_dyn_reloc_index = 0; |
252b5132 RH |
3911 | ret->fn_stub = NULL; |
3912 | ret->need_fn_stub = false; | |
3913 | ret->call_stub = NULL; | |
3914 | ret->call_fp_stub = NULL; | |
3915 | } | |
3916 | ||
3917 | return (struct bfd_hash_entry *) ret; | |
3918 | } | |
3919 | ||
b305ef96 | 3920 | void |
be3ccd9c | 3921 | _bfd_mips_elf_hide_symbol (info, h) |
b305ef96 UC |
3922 | struct bfd_link_info *info; |
3923 | struct mips_elf_link_hash_entry *h; | |
3924 | { | |
3925 | bfd *dynobj; | |
3926 | asection *got; | |
3927 | struct mips_got_info *g; | |
3928 | dynobj = elf_hash_table (info)->dynobj; | |
3929 | got = bfd_get_section_by_name (dynobj, ".got"); | |
3930 | g = (struct mips_got_info *) elf_section_data (got)->tdata; | |
3931 | ||
3932 | h->root.elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT; | |
3933 | h->root.plt.offset = (bfd_vma) -1; | |
3934 | h->root.dynindx = -1; | |
3935 | ||
3936 | /* FIXME: Do we allocate too much GOT space here? */ | |
3937 | g->local_gotno++; | |
3938 | got->_raw_size += MIPS_ELF_GOT_SIZE (dynobj); | |
3939 | } | |
3940 | ||
252b5132 RH |
3941 | /* Create a MIPS ELF linker hash table. */ |
3942 | ||
103186c6 MM |
3943 | struct bfd_link_hash_table * |
3944 | _bfd_mips_elf_link_hash_table_create (abfd) | |
252b5132 RH |
3945 | bfd *abfd; |
3946 | { | |
3947 | struct mips_elf_link_hash_table *ret; | |
3948 | ||
3949 | ret = ((struct mips_elf_link_hash_table *) | |
3950 | bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table))); | |
3951 | if (ret == (struct mips_elf_link_hash_table *) NULL) | |
3952 | return NULL; | |
3953 | ||
3954 | if (! _bfd_elf_link_hash_table_init (&ret->root, abfd, | |
3955 | mips_elf_link_hash_newfunc)) | |
3956 | { | |
3957 | bfd_release (abfd, ret); | |
3958 | return NULL; | |
3959 | } | |
3960 | ||
3961 | #if 0 | |
3962 | /* We no longer use this. */ | |
3963 | for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++) | |
3964 | ret->dynsym_sec_strindex[i] = (bfd_size_type) -1; | |
3965 | #endif | |
3966 | ret->procedure_count = 0; | |
3967 | ret->compact_rel_size = 0; | |
3968 | ret->use_rld_obj_head = false; | |
3969 | ret->rld_value = 0; | |
3970 | ret->mips16_stubs_seen = false; | |
3971 | ||
3972 | return &ret->root.root; | |
3973 | } | |
3974 | ||
3975 | /* Hook called by the linker routine which adds symbols from an object | |
3976 | file. We must handle the special MIPS section numbers here. */ | |
3977 | ||
103186c6 MM |
3978 | boolean |
3979 | _bfd_mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp) | |
252b5132 RH |
3980 | bfd *abfd; |
3981 | struct bfd_link_info *info; | |
3982 | const Elf_Internal_Sym *sym; | |
3983 | const char **namep; | |
5f771d47 | 3984 | flagword *flagsp ATTRIBUTE_UNUSED; |
252b5132 RH |
3985 | asection **secp; |
3986 | bfd_vma *valp; | |
3987 | { | |
3988 | if (SGI_COMPAT (abfd) | |
3989 | && (abfd->flags & DYNAMIC) != 0 | |
3990 | && strcmp (*namep, "_rld_new_interface") == 0) | |
3991 | { | |
3992 | /* Skip Irix 5 rld entry name. */ | |
3993 | *namep = NULL; | |
3994 | return true; | |
3995 | } | |
3996 | ||
3997 | switch (sym->st_shndx) | |
3998 | { | |
3999 | case SHN_COMMON: | |
4000 | /* Common symbols less than the GP size are automatically | |
4001 | treated as SHN_MIPS_SCOMMON symbols. */ | |
7403cb63 MM |
4002 | if (sym->st_size > elf_gp_size (abfd) |
4003 | || IRIX_COMPAT (abfd) == ict_irix6) | |
252b5132 RH |
4004 | break; |
4005 | /* Fall through. */ | |
4006 | case SHN_MIPS_SCOMMON: | |
4007 | *secp = bfd_make_section_old_way (abfd, ".scommon"); | |
4008 | (*secp)->flags |= SEC_IS_COMMON; | |
4009 | *valp = sym->st_size; | |
4010 | break; | |
4011 | ||
4012 | case SHN_MIPS_TEXT: | |
4013 | /* This section is used in a shared object. */ | |
b305ef96 | 4014 | if (elf_tdata (abfd)->elf_text_section == NULL) |
252b5132 | 4015 | { |
b305ef96 UC |
4016 | asymbol *elf_text_symbol; |
4017 | asection *elf_text_section; | |
4018 | ||
4019 | elf_text_section = bfd_zalloc (abfd, sizeof (asection)); | |
4020 | if (elf_text_section == NULL) | |
4021 | return false; | |
4022 | ||
4023 | elf_text_symbol = bfd_zalloc (abfd, sizeof (asymbol)); | |
4024 | if (elf_text_symbol == NULL) | |
4025 | return false; | |
4026 | ||
252b5132 | 4027 | /* Initialize the section. */ |
b305ef96 UC |
4028 | |
4029 | elf_tdata (abfd)->elf_text_section = elf_text_section; | |
4030 | elf_tdata (abfd)->elf_text_symbol = elf_text_symbol; | |
4031 | ||
4032 | elf_text_section->symbol = elf_text_symbol; | |
4033 | elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol; | |
4034 | ||
4035 | elf_text_section->name = ".text"; | |
4036 | elf_text_section->flags = SEC_NO_FLAGS; | |
4037 | elf_text_section->output_section = NULL; | |
4038 | elf_text_section->owner = abfd; | |
4039 | elf_text_symbol->name = ".text"; | |
4040 | elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC; | |
4041 | elf_text_symbol->section = elf_text_section; | |
252b5132 RH |
4042 | } |
4043 | /* This code used to do *secp = bfd_und_section_ptr if | |
4044 | info->shared. I don't know why, and that doesn't make sense, | |
4045 | so I took it out. */ | |
b305ef96 | 4046 | *secp = elf_tdata (abfd)->elf_text_section; |
252b5132 RH |
4047 | break; |
4048 | ||
4049 | case SHN_MIPS_ACOMMON: | |
4050 | /* Fall through. XXX Can we treat this as allocated data? */ | |
4051 | case SHN_MIPS_DATA: | |
4052 | /* This section is used in a shared object. */ | |
b305ef96 | 4053 | if (elf_tdata (abfd)->elf_data_section == NULL) |
252b5132 | 4054 | { |
b305ef96 UC |
4055 | asymbol *elf_data_symbol; |
4056 | asection *elf_data_section; | |
4057 | ||
4058 | elf_data_section = bfd_zalloc (abfd, sizeof (asection)); | |
4059 | if (elf_data_section == NULL) | |
4060 | return false; | |
4061 | ||
4062 | elf_data_symbol = bfd_zalloc (abfd, sizeof (asymbol)); | |
4063 | if (elf_data_symbol == NULL) | |
4064 | return false; | |
4065 | ||
252b5132 | 4066 | /* Initialize the section. */ |
b305ef96 UC |
4067 | |
4068 | elf_tdata (abfd)->elf_data_section = elf_data_section; | |
4069 | elf_tdata (abfd)->elf_data_symbol = elf_data_symbol; | |
4070 | ||
4071 | elf_data_section->symbol = elf_data_symbol; | |
4072 | elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol; | |
4073 | ||
4074 | elf_data_section->name = ".data"; | |
4075 | elf_data_section->flags = SEC_NO_FLAGS; | |
4076 | elf_data_section->output_section = NULL; | |
4077 | elf_data_section->owner = abfd; | |
4078 | elf_data_symbol->name = ".data"; | |
4079 | elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC; | |
4080 | elf_data_symbol->section = elf_data_section; | |
252b5132 RH |
4081 | } |
4082 | /* This code used to do *secp = bfd_und_section_ptr if | |
4083 | info->shared. I don't know why, and that doesn't make sense, | |
4084 | so I took it out. */ | |
b305ef96 | 4085 | *secp = elf_tdata (abfd)->elf_data_section; |
252b5132 RH |
4086 | break; |
4087 | ||
4088 | case SHN_MIPS_SUNDEFINED: | |
4089 | *secp = bfd_und_section_ptr; | |
4090 | break; | |
4091 | } | |
4092 | ||
4093 | if (SGI_COMPAT (abfd) | |
4094 | && ! info->shared | |
4095 | && info->hash->creator == abfd->xvec | |
4096 | && strcmp (*namep, "__rld_obj_head") == 0) | |
4097 | { | |
4098 | struct elf_link_hash_entry *h; | |
4099 | ||
4100 | /* Mark __rld_obj_head as dynamic. */ | |
4101 | h = NULL; | |
4102 | if (! (_bfd_generic_link_add_one_symbol | |
4103 | (info, abfd, *namep, BSF_GLOBAL, *secp, | |
4104 | (bfd_vma) *valp, (const char *) NULL, false, | |
4105 | get_elf_backend_data (abfd)->collect, | |
4106 | (struct bfd_link_hash_entry **) &h))) | |
4107 | return false; | |
be3ccd9c | 4108 | h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF; |
252b5132 RH |
4109 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; |
4110 | h->type = STT_OBJECT; | |
4111 | ||
4112 | if (! bfd_elf32_link_record_dynamic_symbol (info, h)) | |
4113 | return false; | |
4114 | ||
4115 | mips_elf_hash_table (info)->use_rld_obj_head = true; | |
4116 | } | |
4117 | ||
4118 | /* If this is a mips16 text symbol, add 1 to the value to make it | |
4119 | odd. This will cause something like .word SYM to come up with | |
4120 | the right value when it is loaded into the PC. */ | |
4121 | if (sym->st_other == STO_MIPS16) | |
4122 | ++*valp; | |
4123 | ||
4124 | return true; | |
4125 | } | |
4126 | ||
4127 | /* Structure used to pass information to mips_elf_output_extsym. */ | |
4128 | ||
be3ccd9c | 4129 | struct extsym_info { |
252b5132 RH |
4130 | bfd *abfd; |
4131 | struct bfd_link_info *info; | |
4132 | struct ecoff_debug_info *debug; | |
4133 | const struct ecoff_debug_swap *swap; | |
4134 | boolean failed; | |
4135 | }; | |
4136 | ||
4137 | /* This routine is used to write out ECOFF debugging external symbol | |
4138 | information. It is called via mips_elf_link_hash_traverse. The | |
4139 | ECOFF external symbol information must match the ELF external | |
4140 | symbol information. Unfortunately, at this point we don't know | |
4141 | whether a symbol is required by reloc information, so the two | |
4142 | tables may wind up being different. We must sort out the external | |
4143 | symbol information before we can set the final size of the .mdebug | |
4144 | section, and we must set the size of the .mdebug section before we | |
4145 | can relocate any sections, and we can't know which symbols are | |
4146 | required by relocation until we relocate the sections. | |
4147 | Fortunately, it is relatively unlikely that any symbol will be | |
4148 | stripped but required by a reloc. In particular, it can not happen | |
4149 | when generating a final executable. */ | |
4150 | ||
4151 | static boolean | |
4152 | mips_elf_output_extsym (h, data) | |
4153 | struct mips_elf_link_hash_entry *h; | |
4154 | PTR data; | |
4155 | { | |
4156 | struct extsym_info *einfo = (struct extsym_info *) data; | |
4157 | boolean strip; | |
4158 | asection *sec, *output_section; | |
4159 | ||
4160 | if (h->root.indx == -2) | |
4161 | strip = false; | |
4162 | else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
4163 | || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0) | |
4164 | && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0 | |
4165 | && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0) | |
4166 | strip = true; | |
4167 | else if (einfo->info->strip == strip_all | |
4168 | || (einfo->info->strip == strip_some | |
4169 | && bfd_hash_lookup (einfo->info->keep_hash, | |
4170 | h->root.root.root.string, | |
4171 | false, false) == NULL)) | |
4172 | strip = true; | |
4173 | else | |
4174 | strip = false; | |
4175 | ||
4176 | if (strip) | |
4177 | return true; | |
4178 | ||
4179 | if (h->esym.ifd == -2) | |
4180 | { | |
4181 | h->esym.jmptbl = 0; | |
4182 | h->esym.cobol_main = 0; | |
4183 | h->esym.weakext = 0; | |
4184 | h->esym.reserved = 0; | |
4185 | h->esym.ifd = ifdNil; | |
4186 | h->esym.asym.value = 0; | |
4187 | h->esym.asym.st = stGlobal; | |
4188 | ||
f7cb7d68 | 4189 | if (h->root.root.type == bfd_link_hash_undefined |
be3ccd9c | 4190 | || h->root.root.type == bfd_link_hash_undefweak) |
252b5132 RH |
4191 | { |
4192 | const char *name; | |
4193 | ||
4194 | /* Use undefined class. Also, set class and type for some | |
4195 | special symbols. */ | |
4196 | name = h->root.root.root.string; | |
4197 | if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0 | |
4198 | || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0) | |
4199 | { | |
4200 | h->esym.asym.sc = scData; | |
4201 | h->esym.asym.st = stLabel; | |
4202 | h->esym.asym.value = 0; | |
4203 | } | |
4204 | else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0) | |
4205 | { | |
4206 | h->esym.asym.sc = scAbs; | |
4207 | h->esym.asym.st = stLabel; | |
4208 | h->esym.asym.value = | |
4209 | mips_elf_hash_table (einfo->info)->procedure_count; | |
4210 | } | |
4211 | else if (strcmp (name, "_gp_disp") == 0) | |
4212 | { | |
4213 | h->esym.asym.sc = scAbs; | |
4214 | h->esym.asym.st = stLabel; | |
4215 | h->esym.asym.value = elf_gp (einfo->abfd); | |
4216 | } | |
4217 | else | |
4218 | h->esym.asym.sc = scUndefined; | |
4219 | } | |
4220 | else if (h->root.root.type != bfd_link_hash_defined | |
4221 | && h->root.root.type != bfd_link_hash_defweak) | |
4222 | h->esym.asym.sc = scAbs; | |
4223 | else | |
4224 | { | |
4225 | const char *name; | |
4226 | ||
4227 | sec = h->root.root.u.def.section; | |
4228 | output_section = sec->output_section; | |
4229 | ||
4230 | /* When making a shared library and symbol h is the one from | |
4231 | the another shared library, OUTPUT_SECTION may be null. */ | |
4232 | if (output_section == NULL) | |
4233 | h->esym.asym.sc = scUndefined; | |
4234 | else | |
4235 | { | |
4236 | name = bfd_section_name (output_section->owner, output_section); | |
4237 | ||
4238 | if (strcmp (name, ".text") == 0) | |
4239 | h->esym.asym.sc = scText; | |
4240 | else if (strcmp (name, ".data") == 0) | |
4241 | h->esym.asym.sc = scData; | |
4242 | else if (strcmp (name, ".sdata") == 0) | |
4243 | h->esym.asym.sc = scSData; | |
4244 | else if (strcmp (name, ".rodata") == 0 | |
4245 | || strcmp (name, ".rdata") == 0) | |
4246 | h->esym.asym.sc = scRData; | |
4247 | else if (strcmp (name, ".bss") == 0) | |
4248 | h->esym.asym.sc = scBss; | |
4249 | else if (strcmp (name, ".sbss") == 0) | |
4250 | h->esym.asym.sc = scSBss; | |
4251 | else if (strcmp (name, ".init") == 0) | |
4252 | h->esym.asym.sc = scInit; | |
4253 | else if (strcmp (name, ".fini") == 0) | |
4254 | h->esym.asym.sc = scFini; | |
4255 | else | |
4256 | h->esym.asym.sc = scAbs; | |
4257 | } | |
4258 | } | |
4259 | ||
4260 | h->esym.asym.reserved = 0; | |
4261 | h->esym.asym.index = indexNil; | |
4262 | } | |
4263 | ||
4264 | if (h->root.root.type == bfd_link_hash_common) | |
4265 | h->esym.asym.value = h->root.root.u.c.size; | |
4266 | else if (h->root.root.type == bfd_link_hash_defined | |
4267 | || h->root.root.type == bfd_link_hash_defweak) | |
4268 | { | |
4269 | if (h->esym.asym.sc == scCommon) | |
4270 | h->esym.asym.sc = scBss; | |
4271 | else if (h->esym.asym.sc == scSCommon) | |
4272 | h->esym.asym.sc = scSBss; | |
4273 | ||
4274 | sec = h->root.root.u.def.section; | |
4275 | output_section = sec->output_section; | |
4276 | if (output_section != NULL) | |
4277 | h->esym.asym.value = (h->root.root.u.def.value | |
4278 | + sec->output_offset | |
4279 | + output_section->vma); | |
4280 | else | |
4281 | h->esym.asym.value = 0; | |
4282 | } | |
4283 | else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0) | |
4284 | { | |
4285 | /* Set type and value for a symbol with a function stub. */ | |
4286 | h->esym.asym.st = stProc; | |
4287 | sec = h->root.root.u.def.section; | |
4288 | if (sec == NULL) | |
4289 | h->esym.asym.value = 0; | |
4290 | else | |
4291 | { | |
4292 | output_section = sec->output_section; | |
4293 | if (output_section != NULL) | |
4294 | h->esym.asym.value = (h->root.plt.offset | |
4295 | + sec->output_offset | |
4296 | + output_section->vma); | |
4297 | else | |
4298 | h->esym.asym.value = 0; | |
4299 | } | |
4300 | #if 0 /* FIXME? */ | |
4301 | h->esym.ifd = 0; | |
4302 | #endif | |
4303 | } | |
4304 | ||
4305 | if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap, | |
4306 | h->root.root.root.string, | |
4307 | &h->esym)) | |
4308 | { | |
4309 | einfo->failed = true; | |
4310 | return false; | |
4311 | } | |
4312 | ||
4313 | return true; | |
4314 | } | |
4315 | ||
4316 | /* Create a runtime procedure table from the .mdebug section. */ | |
4317 | ||
4318 | static boolean | |
4319 | mips_elf_create_procedure_table (handle, abfd, info, s, debug) | |
4320 | PTR handle; | |
4321 | bfd *abfd; | |
4322 | struct bfd_link_info *info; | |
4323 | asection *s; | |
4324 | struct ecoff_debug_info *debug; | |
4325 | { | |
4326 | const struct ecoff_debug_swap *swap; | |
4327 | HDRR *hdr = &debug->symbolic_header; | |
4328 | RPDR *rpdr, *rp; | |
4329 | struct rpdr_ext *erp; | |
4330 | PTR rtproc; | |
4331 | struct pdr_ext *epdr; | |
4332 | struct sym_ext *esym; | |
4333 | char *ss, **sv; | |
4334 | char *str; | |
4335 | unsigned long size, count; | |
4336 | unsigned long sindex; | |
4337 | unsigned long i; | |
4338 | PDR pdr; | |
4339 | SYMR sym; | |
4340 | const char *no_name_func = _("static procedure (no name)"); | |
4341 | ||
4342 | epdr = NULL; | |
4343 | rpdr = NULL; | |
4344 | esym = NULL; | |
4345 | ss = NULL; | |
4346 | sv = NULL; | |
4347 | ||
4348 | swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap; | |
4349 | ||
4350 | sindex = strlen (no_name_func) + 1; | |
4351 | count = hdr->ipdMax; | |
4352 | if (count > 0) | |
4353 | { | |
4354 | size = swap->external_pdr_size; | |
4355 | ||
4356 | epdr = (struct pdr_ext *) bfd_malloc (size * count); | |
4357 | if (epdr == NULL) | |
4358 | goto error_return; | |
4359 | ||
4360 | if (! _bfd_ecoff_get_accumulated_pdr (handle, (PTR) epdr)) | |
4361 | goto error_return; | |
4362 | ||
4363 | size = sizeof (RPDR); | |
4364 | rp = rpdr = (RPDR *) bfd_malloc (size * count); | |
4365 | if (rpdr == NULL) | |
4366 | goto error_return; | |
4367 | ||
4368 | sv = (char **) bfd_malloc (sizeof (char *) * count); | |
4369 | if (sv == NULL) | |
4370 | goto error_return; | |
4371 | ||
4372 | count = hdr->isymMax; | |
4373 | size = swap->external_sym_size; | |
4374 | esym = (struct sym_ext *) bfd_malloc (size * count); | |
4375 | if (esym == NULL) | |
4376 | goto error_return; | |
4377 | ||
4378 | if (! _bfd_ecoff_get_accumulated_sym (handle, (PTR) esym)) | |
4379 | goto error_return; | |
4380 | ||
4381 | count = hdr->issMax; | |
4382 | ss = (char *) bfd_malloc (count); | |
4383 | if (ss == NULL) | |
4384 | goto error_return; | |
4385 | if (! _bfd_ecoff_get_accumulated_ss (handle, (PTR) ss)) | |
4386 | goto error_return; | |
4387 | ||
4388 | count = hdr->ipdMax; | |
4389 | for (i = 0; i < count; i++, rp++) | |
4390 | { | |
4391 | (*swap->swap_pdr_in) (abfd, (PTR) (epdr + i), &pdr); | |
4392 | (*swap->swap_sym_in) (abfd, (PTR) &esym[pdr.isym], &sym); | |
4393 | rp->adr = sym.value; | |
4394 | rp->regmask = pdr.regmask; | |
4395 | rp->regoffset = pdr.regoffset; | |
4396 | rp->fregmask = pdr.fregmask; | |
4397 | rp->fregoffset = pdr.fregoffset; | |
4398 | rp->frameoffset = pdr.frameoffset; | |
4399 | rp->framereg = pdr.framereg; | |
4400 | rp->pcreg = pdr.pcreg; | |
4401 | rp->irpss = sindex; | |
4402 | sv[i] = ss + sym.iss; | |
4403 | sindex += strlen (sv[i]) + 1; | |
4404 | } | |
4405 | } | |
4406 | ||
4407 | size = sizeof (struct rpdr_ext) * (count + 2) + sindex; | |
4408 | size = BFD_ALIGN (size, 16); | |
4409 | rtproc = (PTR) bfd_alloc (abfd, size); | |
4410 | if (rtproc == NULL) | |
4411 | { | |
4412 | mips_elf_hash_table (info)->procedure_count = 0; | |
4413 | goto error_return; | |
4414 | } | |
4415 | ||
4416 | mips_elf_hash_table (info)->procedure_count = count + 2; | |
4417 | ||
4418 | erp = (struct rpdr_ext *) rtproc; | |
4419 | memset (erp, 0, sizeof (struct rpdr_ext)); | |
4420 | erp++; | |
4421 | str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2); | |
4422 | strcpy (str, no_name_func); | |
4423 | str += strlen (no_name_func) + 1; | |
4424 | for (i = 0; i < count; i++) | |
4425 | { | |
4426 | ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i); | |
4427 | strcpy (str, sv[i]); | |
4428 | str += strlen (sv[i]) + 1; | |
4429 | } | |
4430 | ecoff_put_off (abfd, (bfd_vma) -1, (bfd_byte *) (erp + count)->p_adr); | |
4431 | ||
4432 | /* Set the size and contents of .rtproc section. */ | |
4433 | s->_raw_size = size; | |
4434 | s->contents = (bfd_byte *) rtproc; | |
4435 | ||
4436 | /* Skip this section later on (I don't think this currently | |
4437 | matters, but someday it might). */ | |
4438 | s->link_order_head = (struct bfd_link_order *) NULL; | |
4439 | ||
4440 | if (epdr != NULL) | |
4441 | free (epdr); | |
4442 | if (rpdr != NULL) | |
4443 | free (rpdr); | |
4444 | if (esym != NULL) | |
4445 | free (esym); | |
4446 | if (ss != NULL) | |
4447 | free (ss); | |
4448 | if (sv != NULL) | |
4449 | free (sv); | |
4450 | ||
4451 | return true; | |
4452 | ||
4453 | error_return: | |
4454 | if (epdr != NULL) | |
4455 | free (epdr); | |
4456 | if (rpdr != NULL) | |
4457 | free (rpdr); | |
4458 | if (esym != NULL) | |
4459 | free (esym); | |
4460 | if (ss != NULL) | |
4461 | free (ss); | |
4462 | if (sv != NULL) | |
4463 | free (sv); | |
4464 | return false; | |
4465 | } | |
4466 | ||
4467 | /* A comparison routine used to sort .gptab entries. */ | |
4468 | ||
4469 | static int | |
4470 | gptab_compare (p1, p2) | |
4471 | const PTR p1; | |
4472 | const PTR p2; | |
4473 | { | |
4474 | const Elf32_gptab *a1 = (const Elf32_gptab *) p1; | |
4475 | const Elf32_gptab *a2 = (const Elf32_gptab *) p2; | |
4476 | ||
4477 | return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value; | |
4478 | } | |
4479 | ||
4480 | /* We need to use a special link routine to handle the .reginfo and | |
4481 | the .mdebug sections. We need to merge all instances of these | |
4482 | sections together, not write them all out sequentially. */ | |
4483 | ||
103186c6 MM |
4484 | boolean |
4485 | _bfd_mips_elf_final_link (abfd, info) | |
252b5132 RH |
4486 | bfd *abfd; |
4487 | struct bfd_link_info *info; | |
4488 | { | |
4489 | asection **secpp; | |
4490 | asection *o; | |
4491 | struct bfd_link_order *p; | |
4492 | asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec; | |
4493 | asection *rtproc_sec; | |
4494 | Elf32_RegInfo reginfo; | |
4495 | struct ecoff_debug_info debug; | |
4496 | const struct ecoff_debug_swap *swap | |
4497 | = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap; | |
4498 | HDRR *symhdr = &debug.symbolic_header; | |
4499 | PTR mdebug_handle = NULL; | |
f7cb7d68 UC |
4500 | asection *s; |
4501 | EXTR esym; | |
4502 | bfd_vma last; | |
4503 | unsigned int i; | |
be3ccd9c KH |
4504 | static const char * const name[] = { |
4505 | ".text", ".init", ".fini", ".data", | |
4506 | ".rodata", ".sdata", ".sbss", ".bss" | |
4507 | }; | |
4508 | static const int sc[] = { | |
4509 | scText, scInit, scFini, scData, | |
4510 | scRData, scSData, scSBss, scBss | |
4511 | }; | |
252b5132 | 4512 | |
303f629d MM |
4513 | /* If all the things we linked together were PIC, but we're |
4514 | producing an executable (rather than a shared object), then the | |
4515 | resulting file is CPIC (i.e., it calls PIC code.) */ | |
0dda5f7a ILT |
4516 | if (!info->shared |
4517 | && !info->relocateable | |
4518 | && elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) | |
252b5132 | 4519 | { |
303f629d MM |
4520 | elf_elfheader (abfd)->e_flags &= ~EF_MIPS_PIC; |
4521 | elf_elfheader (abfd)->e_flags |= EF_MIPS_CPIC; | |
252b5132 RH |
4522 | } |
4523 | ||
b3be9b46 RH |
4524 | /* We'd carefully arranged the dynamic symbol indices, and then the |
4525 | generic size_dynamic_sections renumbered them out from under us. | |
4526 | Rather than trying somehow to prevent the renumbering, just do | |
4527 | the sort again. */ | |
441d6d79 | 4528 | if (elf_hash_table (info)->dynamic_sections_created) |
b3be9b46 RH |
4529 | { |
4530 | bfd *dynobj; | |
4531 | asection *got; | |
4532 | struct mips_got_info *g; | |
4533 | ||
435394bf MM |
4534 | /* When we resort, we must tell mips_elf_sort_hash_table what |
4535 | the lowest index it may use is. That's the number of section | |
4536 | symbols we're going to add. The generic ELF linker only | |
4537 | adds these symbols when building a shared object. Note that | |
4538 | we count the sections after (possibly) removing the .options | |
4539 | section above. */ | |
be3ccd9c | 4540 | if (!mips_elf_sort_hash_table (info, (info->shared |
435394bf MM |
4541 | ? bfd_count_sections (abfd) + 1 |
4542 | : 1))) | |
be3ccd9c | 4543 | return false; |
b3be9b46 RH |
4544 | |
4545 | /* Make sure we didn't grow the global .got region. */ | |
4546 | dynobj = elf_hash_table (info)->dynobj; | |
4547 | got = bfd_get_section_by_name (dynobj, ".got"); | |
4548 | g = (struct mips_got_info *) elf_section_data (got)->tdata; | |
4549 | ||
8b237a89 MM |
4550 | if (g->global_gotsym != NULL) |
4551 | BFD_ASSERT ((elf_hash_table (info)->dynsymcount | |
4552 | - g->global_gotsym->dynindx) | |
4553 | <= g->global_gotno); | |
b3be9b46 RH |
4554 | } |
4555 | ||
303f629d MM |
4556 | /* On IRIX5, we omit the .options section. On IRIX6, however, we |
4557 | include it, even though we don't process it quite right. (Some | |
4558 | entries are supposed to be merged.) Empirically, we seem to be | |
4559 | better off including it then not. */ | |
f7cb7d68 | 4560 | if (IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none) |
303f629d MM |
4561 | for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next) |
4562 | { | |
4563 | if (strcmp ((*secpp)->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0) | |
4564 | { | |
4565 | for (p = (*secpp)->link_order_head; p != NULL; p = p->next) | |
4566 | if (p->type == bfd_indirect_link_order) | |
be3ccd9c | 4567 | p->u.indirect.section->flags &= ~SEC_HAS_CONTENTS; |
303f629d MM |
4568 | (*secpp)->link_order_head = NULL; |
4569 | *secpp = (*secpp)->next; | |
4570 | --abfd->section_count; | |
be3ccd9c | 4571 | |
303f629d MM |
4572 | break; |
4573 | } | |
4574 | } | |
4575 | ||
252b5132 RH |
4576 | /* Get a value for the GP register. */ |
4577 | if (elf_gp (abfd) == 0) | |
4578 | { | |
4579 | struct bfd_link_hash_entry *h; | |
4580 | ||
4581 | h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true); | |
4582 | if (h != (struct bfd_link_hash_entry *) NULL | |
4583 | && h->type == bfd_link_hash_defined) | |
4584 | elf_gp (abfd) = (h->u.def.value | |
4585 | + h->u.def.section->output_section->vma | |
4586 | + h->u.def.section->output_offset); | |
0db63c18 MM |
4587 | else if (info->relocateable) |
4588 | { | |
4589 | bfd_vma lo; | |
4590 | ||
4591 | /* Find the GP-relative section with the lowest offset. */ | |
4592 | lo = (bfd_vma) -1; | |
4593 | for (o = abfd->sections; o != (asection *) NULL; o = o->next) | |
be3ccd9c | 4594 | if (o->vma < lo |
0db63c18 MM |
4595 | && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL)) |
4596 | lo = o->vma; | |
4597 | ||
4598 | /* And calculate GP relative to that. */ | |
4599 | elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd); | |
4600 | } | |
252b5132 RH |
4601 | else |
4602 | { | |
4603 | /* If the relocate_section function needs to do a reloc | |
4604 | involving the GP value, it should make a reloc_dangerous | |
4605 | callback to warn that GP is not defined. */ | |
4606 | } | |
4607 | } | |
4608 | ||
4609 | /* Go through the sections and collect the .reginfo and .mdebug | |
4610 | information. */ | |
4611 | reginfo_sec = NULL; | |
4612 | mdebug_sec = NULL; | |
4613 | gptab_data_sec = NULL; | |
4614 | gptab_bss_sec = NULL; | |
4615 | for (o = abfd->sections; o != (asection *) NULL; o = o->next) | |
4616 | { | |
4617 | if (strcmp (o->name, ".reginfo") == 0) | |
4618 | { | |
4619 | memset (®info, 0, sizeof reginfo); | |
4620 | ||
4621 | /* We have found the .reginfo section in the output file. | |
4622 | Look through all the link_orders comprising it and merge | |
4623 | the information together. */ | |
4624 | for (p = o->link_order_head; | |
4625 | p != (struct bfd_link_order *) NULL; | |
4626 | p = p->next) | |
4627 | { | |
4628 | asection *input_section; | |
4629 | bfd *input_bfd; | |
4630 | Elf32_External_RegInfo ext; | |
4631 | Elf32_RegInfo sub; | |
4632 | ||
4633 | if (p->type != bfd_indirect_link_order) | |
4634 | { | |
4635 | if (p->type == bfd_fill_link_order) | |
4636 | continue; | |
4637 | abort (); | |
4638 | } | |
4639 | ||
4640 | input_section = p->u.indirect.section; | |
4641 | input_bfd = input_section->owner; | |
4642 | ||
4643 | /* The linker emulation code has probably clobbered the | |
4644 | size to be zero bytes. */ | |
4645 | if (input_section->_raw_size == 0) | |
4646 | input_section->_raw_size = sizeof (Elf32_External_RegInfo); | |
4647 | ||
4648 | if (! bfd_get_section_contents (input_bfd, input_section, | |
4649 | (PTR) &ext, | |
4650 | (file_ptr) 0, | |
4651 | sizeof ext)) | |
4652 | return false; | |
4653 | ||
4654 | bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub); | |
4655 | ||
4656 | reginfo.ri_gprmask |= sub.ri_gprmask; | |
4657 | reginfo.ri_cprmask[0] |= sub.ri_cprmask[0]; | |
4658 | reginfo.ri_cprmask[1] |= sub.ri_cprmask[1]; | |
4659 | reginfo.ri_cprmask[2] |= sub.ri_cprmask[2]; | |
4660 | reginfo.ri_cprmask[3] |= sub.ri_cprmask[3]; | |
4661 | ||
4662 | /* ri_gp_value is set by the function | |
4663 | mips_elf32_section_processing when the section is | |
4664 | finally written out. */ | |
4665 | ||
4666 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
4667 | elf_link_input_bfd ignores this section. */ | |
be3ccd9c | 4668 | input_section->flags &= ~SEC_HAS_CONTENTS; |
252b5132 RH |
4669 | } |
4670 | ||
4671 | /* Size has been set in mips_elf_always_size_sections */ | |
4672 | BFD_ASSERT(o->_raw_size == sizeof (Elf32_External_RegInfo)); | |
4673 | ||
4674 | /* Skip this section later on (I don't think this currently | |
4675 | matters, but someday it might). */ | |
4676 | o->link_order_head = (struct bfd_link_order *) NULL; | |
4677 | ||
4678 | reginfo_sec = o; | |
4679 | } | |
4680 | ||
4681 | if (strcmp (o->name, ".mdebug") == 0) | |
4682 | { | |
4683 | struct extsym_info einfo; | |
4684 | ||
4685 | /* We have found the .mdebug section in the output file. | |
4686 | Look through all the link_orders comprising it and merge | |
4687 | the information together. */ | |
4688 | symhdr->magic = swap->sym_magic; | |
4689 | /* FIXME: What should the version stamp be? */ | |
4690 | symhdr->vstamp = 0; | |
4691 | symhdr->ilineMax = 0; | |
4692 | symhdr->cbLine = 0; | |
4693 | symhdr->idnMax = 0; | |
4694 | symhdr->ipdMax = 0; | |
4695 | symhdr->isymMax = 0; | |
4696 | symhdr->ioptMax = 0; | |
4697 | symhdr->iauxMax = 0; | |
4698 | symhdr->issMax = 0; | |
4699 | symhdr->issExtMax = 0; | |
4700 | symhdr->ifdMax = 0; | |
4701 | symhdr->crfd = 0; | |
4702 | symhdr->iextMax = 0; | |
4703 | ||
4704 | /* We accumulate the debugging information itself in the | |
4705 | debug_info structure. */ | |
4706 | debug.line = NULL; | |
4707 | debug.external_dnr = NULL; | |
4708 | debug.external_pdr = NULL; | |
4709 | debug.external_sym = NULL; | |
4710 | debug.external_opt = NULL; | |
4711 | debug.external_aux = NULL; | |
4712 | debug.ss = NULL; | |
4713 | debug.ssext = debug.ssext_end = NULL; | |
4714 | debug.external_fdr = NULL; | |
4715 | debug.external_rfd = NULL; | |
4716 | debug.external_ext = debug.external_ext_end = NULL; | |
4717 | ||
4718 | mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info); | |
4719 | if (mdebug_handle == (PTR) NULL) | |
4720 | return false; | |
4721 | ||
be3ccd9c KH |
4722 | esym.jmptbl = 0; |
4723 | esym.cobol_main = 0; | |
4724 | esym.weakext = 0; | |
4725 | esym.reserved = 0; | |
4726 | esym.ifd = ifdNil; | |
4727 | esym.asym.iss = issNil; | |
4728 | esym.asym.st = stLocal; | |
4729 | esym.asym.reserved = 0; | |
4730 | esym.asym.index = indexNil; | |
4731 | last = 0; | |
4732 | for (i = 0; i < 8; i++) | |
4733 | { | |
4734 | esym.asym.sc = sc[i]; | |
4735 | s = bfd_get_section_by_name (abfd, name[i]); | |
4736 | if (s != NULL) | |
4737 | { | |
4738 | esym.asym.value = s->vma; | |
4739 | last = s->vma + s->_raw_size; | |
4740 | } | |
4741 | else | |
4742 | esym.asym.value = last; | |
4743 | if (!bfd_ecoff_debug_one_external (abfd, &debug, swap, | |
4744 | name[i], &esym)) | |
4745 | return false; | |
4746 | } | |
252b5132 RH |
4747 | |
4748 | for (p = o->link_order_head; | |
4749 | p != (struct bfd_link_order *) NULL; | |
4750 | p = p->next) | |
4751 | { | |
4752 | asection *input_section; | |
4753 | bfd *input_bfd; | |
4754 | const struct ecoff_debug_swap *input_swap; | |
4755 | struct ecoff_debug_info input_debug; | |
4756 | char *eraw_src; | |
4757 | char *eraw_end; | |
4758 | ||
4759 | if (p->type != bfd_indirect_link_order) | |
4760 | { | |
4761 | if (p->type == bfd_fill_link_order) | |
4762 | continue; | |
4763 | abort (); | |
4764 | } | |
4765 | ||
4766 | input_section = p->u.indirect.section; | |
4767 | input_bfd = input_section->owner; | |
4768 | ||
4769 | if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour | |
4770 | || (get_elf_backend_data (input_bfd) | |
4771 | ->elf_backend_ecoff_debug_swap) == NULL) | |
4772 | { | |
4773 | /* I don't know what a non MIPS ELF bfd would be | |
4774 | doing with a .mdebug section, but I don't really | |
4775 | want to deal with it. */ | |
4776 | continue; | |
4777 | } | |
4778 | ||
4779 | input_swap = (get_elf_backend_data (input_bfd) | |
4780 | ->elf_backend_ecoff_debug_swap); | |
4781 | ||
4782 | BFD_ASSERT (p->size == input_section->_raw_size); | |
4783 | ||
4784 | /* The ECOFF linking code expects that we have already | |
4785 | read in the debugging information and set up an | |
4786 | ecoff_debug_info structure, so we do that now. */ | |
4787 | if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section, | |
4788 | &input_debug)) | |
4789 | return false; | |
4790 | ||
4791 | if (! (bfd_ecoff_debug_accumulate | |
4792 | (mdebug_handle, abfd, &debug, swap, input_bfd, | |
4793 | &input_debug, input_swap, info))) | |
4794 | return false; | |
4795 | ||
4796 | /* Loop through the external symbols. For each one with | |
4797 | interesting information, try to find the symbol in | |
4798 | the linker global hash table and save the information | |
4799 | for the output external symbols. */ | |
4800 | eraw_src = input_debug.external_ext; | |
4801 | eraw_end = (eraw_src | |
4802 | + (input_debug.symbolic_header.iextMax | |
4803 | * input_swap->external_ext_size)); | |
4804 | for (; | |
4805 | eraw_src < eraw_end; | |
4806 | eraw_src += input_swap->external_ext_size) | |
4807 | { | |
4808 | EXTR ext; | |
4809 | const char *name; | |
4810 | struct mips_elf_link_hash_entry *h; | |
4811 | ||
4812 | (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext); | |
4813 | if (ext.asym.sc == scNil | |
4814 | || ext.asym.sc == scUndefined | |
4815 | || ext.asym.sc == scSUndefined) | |
4816 | continue; | |
4817 | ||
4818 | name = input_debug.ssext + ext.asym.iss; | |
4819 | h = mips_elf_link_hash_lookup (mips_elf_hash_table (info), | |
4820 | name, false, false, true); | |
4821 | if (h == NULL || h->esym.ifd != -2) | |
4822 | continue; | |
4823 | ||
4824 | if (ext.ifd != -1) | |
4825 | { | |
4826 | BFD_ASSERT (ext.ifd | |
4827 | < input_debug.symbolic_header.ifdMax); | |
4828 | ext.ifd = input_debug.ifdmap[ext.ifd]; | |
4829 | } | |
4830 | ||
4831 | h->esym = ext; | |
4832 | } | |
4833 | ||
4834 | /* Free up the information we just read. */ | |
4835 | free (input_debug.line); | |
4836 | free (input_debug.external_dnr); | |
4837 | free (input_debug.external_pdr); | |
4838 | free (input_debug.external_sym); | |
4839 | free (input_debug.external_opt); | |
4840 | free (input_debug.external_aux); | |
4841 | free (input_debug.ss); | |
4842 | free (input_debug.ssext); | |
4843 | free (input_debug.external_fdr); | |
4844 | free (input_debug.external_rfd); | |
4845 | free (input_debug.external_ext); | |
4846 | ||
4847 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
4848 | elf_link_input_bfd ignores this section. */ | |
be3ccd9c | 4849 | input_section->flags &= ~SEC_HAS_CONTENTS; |
252b5132 RH |
4850 | } |
4851 | ||
4852 | if (SGI_COMPAT (abfd) && info->shared) | |
4853 | { | |
4854 | /* Create .rtproc section. */ | |
4855 | rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc"); | |
4856 | if (rtproc_sec == NULL) | |
4857 | { | |
4858 | flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | |
4859 | | SEC_LINKER_CREATED | SEC_READONLY); | |
4860 | ||
4861 | rtproc_sec = bfd_make_section (abfd, ".rtproc"); | |
4862 | if (rtproc_sec == NULL | |
4863 | || ! bfd_set_section_flags (abfd, rtproc_sec, flags) | |
4864 | || ! bfd_set_section_alignment (abfd, rtproc_sec, 4)) | |
4865 | return false; | |
4866 | } | |
4867 | ||
4868 | if (! mips_elf_create_procedure_table (mdebug_handle, abfd, | |
4869 | info, rtproc_sec, &debug)) | |
4870 | return false; | |
4871 | } | |
4872 | ||
4873 | /* Build the external symbol information. */ | |
4874 | einfo.abfd = abfd; | |
4875 | einfo.info = info; | |
4876 | einfo.debug = &debug; | |
4877 | einfo.swap = swap; | |
4878 | einfo.failed = false; | |
4879 | mips_elf_link_hash_traverse (mips_elf_hash_table (info), | |
4880 | mips_elf_output_extsym, | |
4881 | (PTR) &einfo); | |
4882 | if (einfo.failed) | |
4883 | return false; | |
4884 | ||
4885 | /* Set the size of the .mdebug section. */ | |
4886 | o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap); | |
4887 | ||
4888 | /* Skip this section later on (I don't think this currently | |
4889 | matters, but someday it might). */ | |
4890 | o->link_order_head = (struct bfd_link_order *) NULL; | |
4891 | ||
4892 | mdebug_sec = o; | |
4893 | } | |
4894 | ||
4895 | if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0) | |
4896 | { | |
4897 | const char *subname; | |
4898 | unsigned int c; | |
4899 | Elf32_gptab *tab; | |
4900 | Elf32_External_gptab *ext_tab; | |
4901 | unsigned int i; | |
4902 | ||
4903 | /* The .gptab.sdata and .gptab.sbss sections hold | |
4904 | information describing how the small data area would | |
4905 | change depending upon the -G switch. These sections | |
4906 | not used in executables files. */ | |
4907 | if (! info->relocateable) | |
4908 | { | |
4909 | asection **secpp; | |
4910 | ||
4911 | for (p = o->link_order_head; | |
4912 | p != (struct bfd_link_order *) NULL; | |
4913 | p = p->next) | |
4914 | { | |
4915 | asection *input_section; | |
4916 | ||
4917 | if (p->type != bfd_indirect_link_order) | |
4918 | { | |
4919 | if (p->type == bfd_fill_link_order) | |
4920 | continue; | |
4921 | abort (); | |
4922 | } | |
4923 | ||
4924 | input_section = p->u.indirect.section; | |
4925 | ||
4926 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
4927 | elf_link_input_bfd ignores this section. */ | |
be3ccd9c | 4928 | input_section->flags &= ~SEC_HAS_CONTENTS; |
252b5132 RH |
4929 | } |
4930 | ||
4931 | /* Skip this section later on (I don't think this | |
4932 | currently matters, but someday it might). */ | |
4933 | o->link_order_head = (struct bfd_link_order *) NULL; | |
4934 | ||
4935 | /* Really remove the section. */ | |
4936 | for (secpp = &abfd->sections; | |
4937 | *secpp != o; | |
4938 | secpp = &(*secpp)->next) | |
4939 | ; | |
4940 | *secpp = (*secpp)->next; | |
4941 | --abfd->section_count; | |
4942 | ||
4943 | continue; | |
4944 | } | |
4945 | ||
4946 | /* There is one gptab for initialized data, and one for | |
4947 | uninitialized data. */ | |
4948 | if (strcmp (o->name, ".gptab.sdata") == 0) | |
4949 | gptab_data_sec = o; | |
4950 | else if (strcmp (o->name, ".gptab.sbss") == 0) | |
4951 | gptab_bss_sec = o; | |
4952 | else | |
4953 | { | |
4954 | (*_bfd_error_handler) | |
4955 | (_("%s: illegal section name `%s'"), | |
4956 | bfd_get_filename (abfd), o->name); | |
4957 | bfd_set_error (bfd_error_nonrepresentable_section); | |
4958 | return false; | |
4959 | } | |
4960 | ||
4961 | /* The linker script always combines .gptab.data and | |
4962 | .gptab.sdata into .gptab.sdata, and likewise for | |
4963 | .gptab.bss and .gptab.sbss. It is possible that there is | |
4964 | no .sdata or .sbss section in the output file, in which | |
4965 | case we must change the name of the output section. */ | |
4966 | subname = o->name + sizeof ".gptab" - 1; | |
4967 | if (bfd_get_section_by_name (abfd, subname) == NULL) | |
4968 | { | |
4969 | if (o == gptab_data_sec) | |
4970 | o->name = ".gptab.data"; | |
4971 | else | |
4972 | o->name = ".gptab.bss"; | |
4973 | subname = o->name + sizeof ".gptab" - 1; | |
4974 | BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL); | |
4975 | } | |
4976 | ||
4977 | /* Set up the first entry. */ | |
4978 | c = 1; | |
4979 | tab = (Elf32_gptab *) bfd_malloc (c * sizeof (Elf32_gptab)); | |
4980 | if (tab == NULL) | |
4981 | return false; | |
4982 | tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd); | |
4983 | tab[0].gt_header.gt_unused = 0; | |
4984 | ||
4985 | /* Combine the input sections. */ | |
4986 | for (p = o->link_order_head; | |
4987 | p != (struct bfd_link_order *) NULL; | |
4988 | p = p->next) | |
4989 | { | |
4990 | asection *input_section; | |
4991 | bfd *input_bfd; | |
4992 | bfd_size_type size; | |
4993 | unsigned long last; | |
4994 | bfd_size_type gpentry; | |
4995 | ||
4996 | if (p->type != bfd_indirect_link_order) | |
4997 | { | |
4998 | if (p->type == bfd_fill_link_order) | |
4999 | continue; | |
5000 | abort (); | |
5001 | } | |
5002 | ||
5003 | input_section = p->u.indirect.section; | |
5004 | input_bfd = input_section->owner; | |
5005 | ||
5006 | /* Combine the gptab entries for this input section one | |
5007 | by one. We know that the input gptab entries are | |
5008 | sorted by ascending -G value. */ | |
5009 | size = bfd_section_size (input_bfd, input_section); | |
5010 | last = 0; | |
5011 | for (gpentry = sizeof (Elf32_External_gptab); | |
5012 | gpentry < size; | |
5013 | gpentry += sizeof (Elf32_External_gptab)) | |
5014 | { | |
5015 | Elf32_External_gptab ext_gptab; | |
5016 | Elf32_gptab int_gptab; | |
5017 | unsigned long val; | |
5018 | unsigned long add; | |
5019 | boolean exact; | |
5020 | unsigned int look; | |
5021 | ||
5022 | if (! (bfd_get_section_contents | |
5023 | (input_bfd, input_section, (PTR) &ext_gptab, | |
5024 | gpentry, sizeof (Elf32_External_gptab)))) | |
5025 | { | |
5026 | free (tab); | |
5027 | return false; | |
5028 | } | |
5029 | ||
5030 | bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab, | |
5031 | &int_gptab); | |
5032 | val = int_gptab.gt_entry.gt_g_value; | |
5033 | add = int_gptab.gt_entry.gt_bytes - last; | |
5034 | ||
5035 | exact = false; | |
5036 | for (look = 1; look < c; look++) | |
5037 | { | |
5038 | if (tab[look].gt_entry.gt_g_value >= val) | |
5039 | tab[look].gt_entry.gt_bytes += add; | |
5040 | ||
5041 | if (tab[look].gt_entry.gt_g_value == val) | |
5042 | exact = true; | |
5043 | } | |
5044 | ||
5045 | if (! exact) | |
5046 | { | |
5047 | Elf32_gptab *new_tab; | |
5048 | unsigned int max; | |
5049 | ||
5050 | /* We need a new table entry. */ | |
5051 | new_tab = ((Elf32_gptab *) | |
5052 | bfd_realloc ((PTR) tab, | |
5053 | (c + 1) * sizeof (Elf32_gptab))); | |
5054 | if (new_tab == NULL) | |
5055 | { | |
5056 | free (tab); | |
5057 | return false; | |
5058 | } | |
5059 | tab = new_tab; | |
5060 | tab[c].gt_entry.gt_g_value = val; | |
5061 | tab[c].gt_entry.gt_bytes = add; | |
5062 | ||
5063 | /* Merge in the size for the next smallest -G | |
5064 | value, since that will be implied by this new | |
5065 | value. */ | |
5066 | max = 0; | |
5067 | for (look = 1; look < c; look++) | |
5068 | { | |
5069 | if (tab[look].gt_entry.gt_g_value < val | |
5070 | && (max == 0 | |
5071 | || (tab[look].gt_entry.gt_g_value | |
5072 | > tab[max].gt_entry.gt_g_value))) | |
5073 | max = look; | |
5074 | } | |
5075 | if (max != 0) | |
5076 | tab[c].gt_entry.gt_bytes += | |
5077 | tab[max].gt_entry.gt_bytes; | |
5078 | ||
5079 | ++c; | |
5080 | } | |
5081 | ||
5082 | last = int_gptab.gt_entry.gt_bytes; | |
5083 | } | |
5084 | ||
5085 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
5086 | elf_link_input_bfd ignores this section. */ | |
be3ccd9c | 5087 | input_section->flags &= ~SEC_HAS_CONTENTS; |
252b5132 RH |
5088 | } |
5089 | ||
5090 | /* The table must be sorted by -G value. */ | |
5091 | if (c > 2) | |
5092 | qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare); | |
5093 | ||
5094 | /* Swap out the table. */ | |
5095 | ext_tab = ((Elf32_External_gptab *) | |
5096 | bfd_alloc (abfd, c * sizeof (Elf32_External_gptab))); | |
5097 | if (ext_tab == NULL) | |
5098 | { | |
5099 | free (tab); | |
5100 | return false; | |
5101 | } | |
5102 | ||
5103 | for (i = 0; i < c; i++) | |
5104 | bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i); | |
5105 | free (tab); | |
5106 | ||
5107 | o->_raw_size = c * sizeof (Elf32_External_gptab); | |
5108 | o->contents = (bfd_byte *) ext_tab; | |
5109 | ||
5110 | /* Skip this section later on (I don't think this currently | |
5111 | matters, but someday it might). */ | |
5112 | o->link_order_head = (struct bfd_link_order *) NULL; | |
5113 | } | |
5114 | } | |
5115 | ||
5116 | /* Invoke the regular ELF backend linker to do all the work. */ | |
9ebbd33e MM |
5117 | if (ABI_64_P (abfd)) |
5118 | { | |
5119 | #ifdef BFD64 | |
5120 | if (!bfd_elf64_bfd_final_link (abfd, info)) | |
5121 | return false; | |
5122 | #else | |
5123 | abort (); | |
103186c6 | 5124 | return false; |
9ebbd33e MM |
5125 | #endif /* BFD64 */ |
5126 | } | |
5127 | else if (!bfd_elf32_bfd_final_link (abfd, info)) | |
5128 | return false; | |
252b5132 RH |
5129 | |
5130 | /* Now write out the computed sections. */ | |
5131 | ||
5132 | if (reginfo_sec != (asection *) NULL) | |
5133 | { | |
5134 | Elf32_External_RegInfo ext; | |
5135 | ||
5136 | bfd_mips_elf32_swap_reginfo_out (abfd, ®info, &ext); | |
5137 | if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext, | |
5138 | (file_ptr) 0, sizeof ext)) | |
5139 | return false; | |
5140 | } | |
5141 | ||
5142 | if (mdebug_sec != (asection *) NULL) | |
5143 | { | |
5144 | BFD_ASSERT (abfd->output_has_begun); | |
5145 | if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug, | |
5146 | swap, info, | |
5147 | mdebug_sec->filepos)) | |
5148 | return false; | |
5149 | ||
5150 | bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info); | |
5151 | } | |
5152 | ||
5153 | if (gptab_data_sec != (asection *) NULL) | |
5154 | { | |
5155 | if (! bfd_set_section_contents (abfd, gptab_data_sec, | |
5156 | gptab_data_sec->contents, | |
5157 | (file_ptr) 0, | |
5158 | gptab_data_sec->_raw_size)) | |
5159 | return false; | |
5160 | } | |
5161 | ||
5162 | if (gptab_bss_sec != (asection *) NULL) | |
5163 | { | |
5164 | if (! bfd_set_section_contents (abfd, gptab_bss_sec, | |
5165 | gptab_bss_sec->contents, | |
5166 | (file_ptr) 0, | |
5167 | gptab_bss_sec->_raw_size)) | |
5168 | return false; | |
5169 | } | |
5170 | ||
5171 | if (SGI_COMPAT (abfd)) | |
5172 | { | |
5173 | rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc"); | |
5174 | if (rtproc_sec != NULL) | |
5175 | { | |
5176 | if (! bfd_set_section_contents (abfd, rtproc_sec, | |
5177 | rtproc_sec->contents, | |
5178 | (file_ptr) 0, | |
5179 | rtproc_sec->_raw_size)) | |
5180 | return false; | |
5181 | } | |
5182 | } | |
5183 | ||
5184 | return true; | |
5185 | } | |
5186 | ||
adb76a3e UC |
5187 | /* This function is called via qsort() to sort the dynamic relocation |
5188 | entries by increasing r_symndx value. */ | |
5189 | ||
5190 | static int | |
be3ccd9c KH |
5191 | sort_dynamic_relocs (arg1, arg2) |
5192 | const PTR arg1; | |
5193 | const PTR arg2; | |
adb76a3e UC |
5194 | { |
5195 | const Elf32_External_Rel *ext_reloc1 = (const Elf32_External_Rel *) arg1; | |
5196 | const Elf32_External_Rel *ext_reloc2 = (const Elf32_External_Rel *) arg2; | |
5197 | ||
5198 | Elf_Internal_Rel int_reloc1; | |
5199 | Elf_Internal_Rel int_reloc2; | |
5200 | ||
be3ccd9c KH |
5201 | bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, ext_reloc1, &int_reloc1); |
5202 | bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, ext_reloc2, &int_reloc2); | |
adb76a3e | 5203 | |
be3ccd9c | 5204 | return (ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info)); |
adb76a3e UC |
5205 | } |
5206 | ||
7403cb63 | 5207 | /* Returns the GOT section for ABFD. */ |
252b5132 | 5208 | |
7403cb63 MM |
5209 | static asection * |
5210 | mips_elf_got_section (abfd) | |
5211 | bfd *abfd; | |
252b5132 | 5212 | { |
7403cb63 MM |
5213 | return bfd_get_section_by_name (abfd, ".got"); |
5214 | } | |
5215 | ||
5216 | /* Returns the GOT information associated with the link indicated by | |
be3ccd9c | 5217 | INFO. If SGOTP is non-NULL, it is filled in with the GOT |
7403cb63 MM |
5218 | section. */ |
5219 | ||
5220 | static struct mips_got_info * | |
5221 | mips_elf_got_info (abfd, sgotp) | |
5222 | bfd *abfd; | |
5223 | asection **sgotp; | |
5224 | { | |
5225 | asection *sgot; | |
252b5132 RH |
5226 | struct mips_got_info *g; |
5227 | ||
7403cb63 MM |
5228 | sgot = mips_elf_got_section (abfd); |
5229 | BFD_ASSERT (sgot != NULL); | |
5230 | BFD_ASSERT (elf_section_data (sgot) != NULL); | |
5231 | g = (struct mips_got_info *) elf_section_data (sgot)->tdata; | |
5232 | BFD_ASSERT (g != NULL); | |
252b5132 | 5233 | |
7403cb63 MM |
5234 | if (sgotp) |
5235 | *sgotp = sgot; | |
5236 | return g; | |
5237 | } | |
252b5132 | 5238 | |
6387d602 ILT |
5239 | /* Return whether a relocation is against a local symbol. */ |
5240 | ||
5241 | static boolean | |
b305ef96 UC |
5242 | mips_elf_local_relocation_p (input_bfd, relocation, local_sections, |
5243 | check_forced) | |
6387d602 ILT |
5244 | bfd *input_bfd; |
5245 | const Elf_Internal_Rela *relocation; | |
5246 | asection **local_sections; | |
b305ef96 | 5247 | boolean check_forced; |
6387d602 ILT |
5248 | { |
5249 | unsigned long r_symndx; | |
5250 | Elf_Internal_Shdr *symtab_hdr; | |
be3ccd9c | 5251 | struct mips_elf_link_hash_entry *h; |
b305ef96 | 5252 | size_t extsymoff; |
6387d602 ILT |
5253 | |
5254 | r_symndx = ELF32_R_SYM (relocation->r_info); | |
5255 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
b305ef96 UC |
5256 | extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info; |
5257 | ||
5258 | if (r_symndx < extsymoff) | |
5259 | return true; | |
5260 | if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL) | |
5261 | return true; | |
5262 | ||
5263 | if (check_forced) | |
6387d602 | 5264 | { |
be3ccd9c | 5265 | /* Look up the hash table to check whether the symbol |
b305ef96 | 5266 | was forced local. */ |
be3ccd9c KH |
5267 | h = (struct mips_elf_link_hash_entry *) |
5268 | elf_sym_hashes (input_bfd) [r_symndx - extsymoff]; | |
5269 | /* Find the real hash-table entry for this symbol. */ | |
5270 | while (h->root.root.type == bfd_link_hash_indirect | |
b305ef96 | 5271 | || h->root.root.type == bfd_link_hash_warning) |
be3ccd9c KH |
5272 | h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link; |
5273 | if ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0) | |
5274 | return true; | |
6387d602 | 5275 | } |
b305ef96 UC |
5276 | |
5277 | return false; | |
6387d602 ILT |
5278 | } |
5279 | ||
7403cb63 | 5280 | /* Sign-extend VALUE, which has the indicated number of BITS. */ |
252b5132 | 5281 | |
7403cb63 MM |
5282 | static bfd_vma |
5283 | mips_elf_sign_extend (value, bits) | |
5284 | bfd_vma value; | |
5285 | int bits; | |
5286 | { | |
be3ccd9c | 5287 | if (value & ((bfd_vma) 1 << (bits - 1))) |
7403cb63 | 5288 | /* VALUE is negative. */ |
be3ccd9c KH |
5289 | value |= ((bfd_vma) - 1) << bits; |
5290 | ||
7403cb63 MM |
5291 | return value; |
5292 | } | |
252b5132 | 5293 | |
7403cb63 MM |
5294 | /* Return non-zero if the indicated VALUE has overflowed the maximum |
5295 | range expressable by a signed number with the indicated number of | |
5296 | BITS. */ | |
252b5132 | 5297 | |
7403cb63 MM |
5298 | static boolean |
5299 | mips_elf_overflow_p (value, bits) | |
5300 | bfd_vma value; | |
5301 | int bits; | |
5302 | { | |
5303 | bfd_signed_vma svalue = (bfd_signed_vma) value; | |
252b5132 | 5304 | |
7403cb63 MM |
5305 | if (svalue > (1 << (bits - 1)) - 1) |
5306 | /* The value is too big. */ | |
5307 | return true; | |
5308 | else if (svalue < -(1 << (bits - 1))) | |
5309 | /* The value is too small. */ | |
5310 | return true; | |
be3ccd9c | 5311 | |
7403cb63 MM |
5312 | /* All is well. */ |
5313 | return false; | |
5314 | } | |
252b5132 | 5315 | |
7403cb63 | 5316 | /* Calculate the %high function. */ |
252b5132 | 5317 | |
7403cb63 MM |
5318 | static bfd_vma |
5319 | mips_elf_high (value) | |
5320 | bfd_vma value; | |
5321 | { | |
5322 | return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff; | |
5323 | } | |
252b5132 | 5324 | |
7403cb63 MM |
5325 | /* Calculate the %higher function. */ |
5326 | ||
5327 | static bfd_vma | |
5328 | mips_elf_higher (value) | |
5f771d47 | 5329 | bfd_vma value ATTRIBUTE_UNUSED; |
7403cb63 MM |
5330 | { |
5331 | #ifdef BFD64 | |
5332 | return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff; | |
5333 | #else | |
5334 | abort (); | |
5335 | return (bfd_vma) -1; | |
5336 | #endif | |
5337 | } | |
5338 | ||
5339 | /* Calculate the %highest function. */ | |
5340 | ||
be3ccd9c | 5341 | static bfd_vma |
7403cb63 | 5342 | mips_elf_highest (value) |
5f771d47 | 5343 | bfd_vma value ATTRIBUTE_UNUSED; |
7403cb63 MM |
5344 | { |
5345 | #ifdef BFD64 | |
0af99795 | 5346 | return ((value + (bfd_vma) 0x800080008000) >> 48) & 0xffff; |
7403cb63 MM |
5347 | #else |
5348 | abort (); | |
5349 | return (bfd_vma) -1; | |
5350 | #endif | |
5351 | } | |
5352 | ||
5353 | /* Returns the GOT index for the global symbol indicated by H. */ | |
5354 | ||
be3ccd9c | 5355 | static bfd_vma |
7403cb63 MM |
5356 | mips_elf_global_got_index (abfd, h) |
5357 | bfd *abfd; | |
5358 | struct elf_link_hash_entry *h; | |
5359 | { | |
5360 | bfd_vma index; | |
5361 | asection *sgot; | |
5362 | struct mips_got_info *g; | |
5363 | ||
5364 | g = mips_elf_got_info (abfd, &sgot); | |
5365 | ||
5366 | /* Once we determine the global GOT entry with the lowest dynamic | |
5367 | symbol table index, we must put all dynamic symbols with greater | |
5368 | indices into the GOT. That makes it easy to calculate the GOT | |
5369 | offset. */ | |
5370 | BFD_ASSERT (h->dynindx >= g->global_gotsym->dynindx); | |
be3ccd9c | 5371 | index = ((h->dynindx - g->global_gotsym->dynindx + g->local_gotno) |
103186c6 | 5372 | * MIPS_ELF_GOT_SIZE (abfd)); |
7403cb63 MM |
5373 | BFD_ASSERT (index < sgot->_raw_size); |
5374 | ||
5375 | return index; | |
5376 | } | |
5377 | ||
5378 | /* Returns the offset for the entry at the INDEXth position | |
5379 | in the GOT. */ | |
5380 | ||
5381 | static bfd_vma | |
5382 | mips_elf_got_offset_from_index (dynobj, output_bfd, index) | |
5383 | bfd *dynobj; | |
5384 | bfd *output_bfd; | |
5385 | bfd_vma index; | |
5386 | { | |
5387 | asection *sgot; | |
5388 | bfd_vma gp; | |
7403cb63 | 5389 | |
103186c6 | 5390 | sgot = mips_elf_got_section (dynobj); |
7403cb63 | 5391 | gp = _bfd_get_gp_value (output_bfd); |
be3ccd9c | 5392 | return (sgot->output_section->vma + sgot->output_offset + index - |
7403cb63 MM |
5393 | gp); |
5394 | } | |
5395 | ||
5396 | /* If H is a symbol that needs a global GOT entry, but has a dynamic | |
5397 | symbol table index lower than any we've seen to date, record it for | |
5398 | posterity. */ | |
5399 | ||
5400 | static boolean | |
5401 | mips_elf_record_global_got_symbol (h, info, g) | |
5402 | struct elf_link_hash_entry *h; | |
5403 | struct bfd_link_info *info; | |
5f771d47 | 5404 | struct mips_got_info *g ATTRIBUTE_UNUSED; |
7403cb63 MM |
5405 | { |
5406 | /* A global symbol in the GOT must also be in the dynamic symbol | |
5407 | table. */ | |
5408 | if (h->dynindx == -1 | |
5409 | && !bfd_elf32_link_record_dynamic_symbol (info, h)) | |
5410 | return false; | |
be3ccd9c | 5411 | |
7403cb63 MM |
5412 | /* If we've already marked this entry as need GOT space, we don't |
5413 | need to do it again. */ | |
5414 | if (h->got.offset != (bfd_vma) - 1) | |
5415 | return true; | |
5416 | ||
5417 | /* By setting this to a value other than -1, we are indicating that | |
5418 | there needs to be a GOT entry for H. */ | |
5419 | h->got.offset = 0; | |
5420 | ||
5421 | return true; | |
5422 | } | |
5423 | ||
5424 | /* This structure is passed to mips_elf_sort_hash_table_f when sorting | |
5425 | the dynamic symbols. */ | |
be3ccd9c KH |
5426 | |
5427 | struct mips_elf_hash_sort_data { | |
7403cb63 MM |
5428 | /* The symbol in the global GOT with the lowest dynamic symbol table |
5429 | index. */ | |
5430 | struct elf_link_hash_entry *low; | |
5431 | /* The least dynamic symbol table index corresponding to a symbol | |
5432 | with a GOT entry. */ | |
5433 | long min_got_dynindx; | |
5434 | /* The greatest dynamic symbol table index not corresponding to a | |
5435 | symbol without a GOT entry. */ | |
5436 | long max_non_got_dynindx; | |
5437 | }; | |
5438 | ||
5439 | /* If H needs a GOT entry, assign it the highest available dynamic | |
be3ccd9c | 5440 | index. Otherwise, assign it the lowest available dynamic |
7403cb63 MM |
5441 | index. */ |
5442 | ||
5443 | static boolean | |
5444 | mips_elf_sort_hash_table_f (h, data) | |
5445 | struct mips_elf_link_hash_entry *h; | |
5446 | PTR data; | |
5447 | { | |
be3ccd9c | 5448 | struct mips_elf_hash_sort_data *hsd |
7403cb63 MM |
5449 | = (struct mips_elf_hash_sort_data *) data; |
5450 | ||
5451 | /* Symbols without dynamic symbol table entries aren't interesting | |
5452 | at all. */ | |
5453 | if (h->root.dynindx == -1) | |
5454 | return true; | |
5455 | ||
5456 | if (h->root.got.offset != 0) | |
5457 | h->root.dynindx = hsd->max_non_got_dynindx++; | |
5458 | else | |
5459 | { | |
5460 | h->root.dynindx = --hsd->min_got_dynindx; | |
5461 | hsd->low = (struct elf_link_hash_entry *) h; | |
5462 | } | |
5463 | ||
5464 | return true; | |
5465 | } | |
5466 | ||
5467 | /* Sort the dynamic symbol table so that symbols that need GOT entries | |
5468 | appear towards the end. This reduces the amount of GOT space | |
b3be9b46 RH |
5469 | required. MAX_LOCAL is used to set the number of local symbols |
5470 | known to be in the dynamic symbol table. During | |
5471 | mips_elf_size_dynamic_sections, this value is 1. Afterward, the | |
5472 | section symbols are added and the count is higher. */ | |
7403cb63 MM |
5473 | |
5474 | static boolean | |
b3be9b46 | 5475 | mips_elf_sort_hash_table (info, max_local) |
7403cb63 | 5476 | struct bfd_link_info *info; |
b3be9b46 | 5477 | unsigned long max_local; |
7403cb63 MM |
5478 | { |
5479 | struct mips_elf_hash_sort_data hsd; | |
5480 | struct mips_got_info *g; | |
5481 | bfd *dynobj; | |
5482 | ||
5483 | dynobj = elf_hash_table (info)->dynobj; | |
5484 | ||
5485 | hsd.low = NULL; | |
5486 | hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount; | |
b3be9b46 | 5487 | hsd.max_non_got_dynindx = max_local; |
be3ccd9c KH |
5488 | mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *) |
5489 | elf_hash_table (info)), | |
5490 | mips_elf_sort_hash_table_f, | |
7403cb63 MM |
5491 | &hsd); |
5492 | ||
5493 | /* There shoud have been enough room in the symbol table to | |
5494 | accomodate both the GOT and non-GOT symbols. */ | |
b305ef96 | 5495 | BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx); |
7403cb63 MM |
5496 | |
5497 | /* Now we know which dynamic symbol has the lowest dynamic symbol | |
5498 | table index in the GOT. */ | |
5499 | g = mips_elf_got_info (dynobj, NULL); | |
5500 | g->global_gotsym = hsd.low; | |
5501 | ||
5502 | return true; | |
5503 | } | |
5504 | ||
5505 | /* Create a local GOT entry for VALUE. Return the index of the entry, | |
5506 | or -1 if it could not be created. */ | |
5507 | ||
5508 | static bfd_vma | |
5509 | mips_elf_create_local_got_entry (abfd, g, sgot, value) | |
5510 | bfd *abfd; | |
5511 | struct mips_got_info *g; | |
5512 | asection *sgot; | |
5513 | bfd_vma value; | |
5514 | { | |
5515 | if (g->assigned_gotno >= g->local_gotno) | |
5516 | { | |
5517 | /* We didn't allocate enough space in the GOT. */ | |
5518 | (*_bfd_error_handler) | |
5519 | (_("not enough GOT space for local GOT entries")); | |
5520 | bfd_set_error (bfd_error_bad_value); | |
5521 | return (bfd_vma) -1; | |
5522 | } | |
5523 | ||
103186c6 | 5524 | MIPS_ELF_PUT_WORD (abfd, value, |
be3ccd9c | 5525 | (sgot->contents |
103186c6 MM |
5526 | + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno)); |
5527 | return MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++; | |
7403cb63 MM |
5528 | } |
5529 | ||
5530 | /* Returns the GOT offset at which the indicated address can be found. | |
5531 | If there is not yet a GOT entry for this value, create one. Returns | |
5532 | -1 if no satisfactory GOT offset can be found. */ | |
5533 | ||
5534 | static bfd_vma | |
5535 | mips_elf_local_got_index (abfd, info, value) | |
5536 | bfd *abfd; | |
5537 | struct bfd_link_info *info; | |
5538 | bfd_vma value; | |
5539 | { | |
5540 | asection *sgot; | |
5541 | struct mips_got_info *g; | |
5542 | bfd_byte *entry; | |
5543 | ||
5544 | g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot); | |
5545 | ||
5546 | /* Look to see if we already have an appropriate entry. */ | |
be3ccd9c KH |
5547 | for (entry = (sgot->contents |
5548 | + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO); | |
103186c6 MM |
5549 | entry != sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno; |
5550 | entry += MIPS_ELF_GOT_SIZE (abfd)) | |
7403cb63 | 5551 | { |
103186c6 | 5552 | bfd_vma address = MIPS_ELF_GET_WORD (abfd, entry); |
7403cb63 MM |
5553 | if (address == value) |
5554 | return entry - sgot->contents; | |
5555 | } | |
5556 | ||
5557 | return mips_elf_create_local_got_entry (abfd, g, sgot, value); | |
5558 | } | |
5559 | ||
5560 | /* Find a GOT entry that is within 32KB of the VALUE. These entries | |
5561 | are supposed to be placed at small offsets in the GOT, i.e., | |
5562 | within 32KB of GP. Return the index into the GOT for this page, | |
5563 | and store the offset from this entry to the desired address in | |
5564 | OFFSETP, if it is non-NULL. */ | |
5565 | ||
5566 | static bfd_vma | |
5567 | mips_elf_got_page (abfd, info, value, offsetp) | |
5568 | bfd *abfd; | |
5569 | struct bfd_link_info *info; | |
5570 | bfd_vma value; | |
5571 | bfd_vma *offsetp; | |
5572 | { | |
5573 | asection *sgot; | |
5574 | struct mips_got_info *g; | |
5575 | bfd_byte *entry; | |
5576 | bfd_byte *last_entry; | |
86033394 | 5577 | bfd_vma index = 0; |
7403cb63 MM |
5578 | bfd_vma address; |
5579 | ||
5580 | g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot); | |
5581 | ||
5582 | /* Look to see if we aleady have an appropriate entry. */ | |
103186c6 | 5583 | last_entry = sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno; |
be3ccd9c | 5584 | for (entry = (sgot->contents |
103186c6 | 5585 | + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO); |
7403cb63 | 5586 | entry != last_entry; |
103186c6 | 5587 | entry += MIPS_ELF_GOT_SIZE (abfd)) |
7403cb63 | 5588 | { |
103186c6 MM |
5589 | address = MIPS_ELF_GET_WORD (abfd, entry); |
5590 | ||
7403cb63 MM |
5591 | if (!mips_elf_overflow_p (value - address, 16)) |
5592 | { | |
5593 | /* This entry will serve as the page pointer. We can add a | |
5594 | 16-bit number to it to get the actual address. */ | |
5595 | index = entry - sgot->contents; | |
5596 | break; | |
252b5132 | 5597 | } |
7403cb63 MM |
5598 | } |
5599 | ||
5600 | /* If we didn't have an appropriate entry, we create one now. */ | |
5601 | if (entry == last_entry) | |
5602 | index = mips_elf_create_local_got_entry (abfd, g, sgot, value); | |
5603 | ||
5604 | if (offsetp) | |
5605 | { | |
103186c6 | 5606 | address = MIPS_ELF_GET_WORD (abfd, entry); |
7403cb63 MM |
5607 | *offsetp = value - address; |
5608 | } | |
5609 | ||
5610 | return index; | |
5611 | } | |
5612 | ||
5613 | /* Find a GOT entry whose higher-order 16 bits are the same as those | |
5614 | for value. Return the index into the GOT for this entry. */ | |
5615 | ||
5616 | static bfd_vma | |
b305ef96 | 5617 | mips_elf_got16_entry (abfd, info, value, external) |
7403cb63 MM |
5618 | bfd *abfd; |
5619 | struct bfd_link_info *info; | |
5620 | bfd_vma value; | |
b305ef96 | 5621 | boolean external; |
7403cb63 MM |
5622 | { |
5623 | asection *sgot; | |
5624 | struct mips_got_info *g; | |
5625 | bfd_byte *entry; | |
5626 | bfd_byte *last_entry; | |
86033394 | 5627 | bfd_vma index = 0; |
7403cb63 MM |
5628 | bfd_vma address; |
5629 | ||
b305ef96 UC |
5630 | if (! external) |
5631 | { | |
5632 | /* Although the ABI says that it is "the high-order 16 bits" that we | |
5633 | want, it is really the %high value. The complete value is | |
5634 | calculated with a `addiu' of a LO16 relocation, just as with a | |
5635 | HI16/LO16 pair. */ | |
5636 | value = mips_elf_high (value) << 16; | |
5637 | } | |
5638 | ||
7403cb63 MM |
5639 | g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot); |
5640 | ||
5641 | /* Look to see if we already have an appropriate entry. */ | |
103186c6 | 5642 | last_entry = sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno; |
be3ccd9c | 5643 | for (entry = (sgot->contents |
103186c6 | 5644 | + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO); |
7403cb63 | 5645 | entry != last_entry; |
103186c6 | 5646 | entry += MIPS_ELF_GOT_SIZE (abfd)) |
7403cb63 | 5647 | { |
103186c6 | 5648 | address = MIPS_ELF_GET_WORD (abfd, entry); |
b305ef96 | 5649 | if (address == value) |
252b5132 | 5650 | { |
b305ef96 UC |
5651 | /* This entry has the right high-order 16 bits, and the low-order |
5652 | 16 bits are set to zero. */ | |
4f2860ca | 5653 | index = entry - sgot->contents; |
7403cb63 MM |
5654 | break; |
5655 | } | |
5656 | } | |
5657 | ||
5658 | /* If we didn't have an appropriate entry, we create one now. */ | |
5659 | if (entry == last_entry) | |
5660 | index = mips_elf_create_local_got_entry (abfd, g, sgot, value); | |
5661 | ||
5662 | return index; | |
5663 | } | |
5664 | ||
bb2d6cd7 | 5665 | /* Returns the first relocation of type r_type found, beginning with |
23b255aa | 5666 | RELOCATION. RELEND is one-past-the-end of the relocation table. */ |
7403cb63 | 5667 | |
23b255aa | 5668 | static const Elf_Internal_Rela * |
bb2d6cd7 GK |
5669 | mips_elf_next_relocation (r_type, relocation, relend) |
5670 | unsigned int r_type; | |
103186c6 MM |
5671 | const Elf_Internal_Rela *relocation; |
5672 | const Elf_Internal_Rela *relend; | |
7403cb63 MM |
5673 | { |
5674 | /* According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must be | |
5675 | immediately following. However, for the IRIX6 ABI, the next | |
5676 | relocation may be a composed relocation consisting of several | |
5677 | relocations for the same address. In that case, the R_MIPS_LO16 | |
435394bf | 5678 | relocation may occur as one of these. We permit a similar |
7403cb63 MM |
5679 | extension in general, as that is useful for GCC. */ |
5680 | while (relocation < relend) | |
5681 | { | |
bb2d6cd7 | 5682 | if (ELF32_R_TYPE (relocation->r_info) == r_type) |
23b255aa | 5683 | return relocation; |
7403cb63 MM |
5684 | |
5685 | ++relocation; | |
5686 | } | |
5687 | ||
5688 | /* We didn't find it. */ | |
6387d602 | 5689 | bfd_set_error (bfd_error_bad_value); |
23b255aa | 5690 | return NULL; |
7403cb63 MM |
5691 | } |
5692 | ||
7b1f1231 MM |
5693 | /* Create a rel.dyn relocation for the dynamic linker to resolve. REL |
5694 | is the original relocation, which is now being transformed into a | |
b305ef96 | 5695 | dynamic relocation. The ADDENDP is adjusted if necessary; the |
7b1f1231 | 5696 | caller should store the result in place of the original addend. */ |
7403cb63 | 5697 | |
7b1f1231 MM |
5698 | static boolean |
5699 | mips_elf_create_dynamic_relocation (output_bfd, info, rel, h, sec, | |
5af5b85b | 5700 | symbol, addendp, input_section, local_p) |
7403cb63 MM |
5701 | bfd *output_bfd; |
5702 | struct bfd_link_info *info; | |
103186c6 | 5703 | const Elf_Internal_Rela *rel; |
7b1f1231 MM |
5704 | struct mips_elf_link_hash_entry *h; |
5705 | asection *sec; | |
5706 | bfd_vma symbol; | |
5707 | bfd_vma *addendp; | |
7403cb63 | 5708 | asection *input_section; |
5af5b85b | 5709 | boolean local_p; |
7403cb63 MM |
5710 | { |
5711 | Elf_Internal_Rel outrel; | |
5712 | boolean skip; | |
5713 | asection *sreloc; | |
5714 | bfd *dynobj; | |
5715 | int r_type; | |
5716 | ||
5717 | r_type = ELF32_R_TYPE (rel->r_info); | |
5718 | dynobj = elf_hash_table (info)->dynobj; | |
be3ccd9c | 5719 | sreloc |
103186c6 MM |
5720 | = bfd_get_section_by_name (dynobj, |
5721 | MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd)); | |
7403cb63 | 5722 | BFD_ASSERT (sreloc != NULL); |
b305ef96 | 5723 | BFD_ASSERT (sreloc->contents != NULL); |
7403cb63 MM |
5724 | |
5725 | skip = false; | |
5726 | ||
7b1f1231 MM |
5727 | /* We begin by assuming that the offset for the dynamic relocation |
5728 | is the same as for the original relocation. We'll adjust this | |
5729 | later to reflect the correct output offsets. */ | |
7403cb63 MM |
5730 | if (elf_section_data (input_section)->stab_info == NULL) |
5731 | outrel.r_offset = rel->r_offset; | |
5732 | else | |
5733 | { | |
7b1f1231 MM |
5734 | /* Except that in a stab section things are more complex. |
5735 | Because we compress stab information, the offset given in the | |
5736 | relocation may not be the one we want; we must let the stabs | |
5737 | machinery tell us the offset. */ | |
be3ccd9c | 5738 | outrel.r_offset |
7b1f1231 MM |
5739 | = (_bfd_stab_section_offset |
5740 | (output_bfd, &elf_hash_table (info)->stab_info, | |
5741 | input_section, | |
5742 | &elf_section_data (input_section)->stab_info, | |
5743 | rel->r_offset)); | |
5744 | /* If we didn't need the relocation at all, this value will be | |
5745 | -1. */ | |
5746 | if (outrel.r_offset == (bfd_vma) -1) | |
7403cb63 | 5747 | skip = true; |
7403cb63 | 5748 | } |
7403cb63 | 5749 | |
b305ef96 | 5750 | /* If we've decided to skip this relocation, just output an empty |
7b1f1231 MM |
5751 | record. Note that R_MIPS_NONE == 0, so that this call to memset |
5752 | is a way of setting R_TYPE to R_MIPS_NONE. */ | |
7403cb63 MM |
5753 | if (skip) |
5754 | memset (&outrel, 0, sizeof (outrel)); | |
7b1f1231 MM |
5755 | else |
5756 | { | |
5757 | long indx; | |
5758 | bfd_vma section_offset; | |
5759 | ||
5760 | /* We must now calculate the dynamic symbol table index to use | |
5761 | in the relocation. */ | |
5762 | if (h != NULL | |
5763 | && (! info->symbolic || (h->root.elf_link_hash_flags | |
5764 | & ELF_LINK_HASH_DEF_REGULAR) == 0)) | |
5765 | { | |
5766 | indx = h->root.dynindx; | |
b305ef96 UC |
5767 | /* h->root.dynindx may be -1 if this symbol was marked to |
5768 | become local. */ | |
5769 | if (indx == -1) | |
be3ccd9c | 5770 | indx = 0; |
7b1f1231 MM |
5771 | } |
5772 | else | |
5773 | { | |
5774 | if (sec != NULL && bfd_is_abs_section (sec)) | |
5775 | indx = 0; | |
5776 | else if (sec == NULL || sec->owner == NULL) | |
5777 | { | |
5778 | bfd_set_error (bfd_error_bad_value); | |
5779 | return false; | |
5780 | } | |
5781 | else | |
5782 | { | |
5783 | indx = elf_section_data (sec->output_section)->dynindx; | |
5784 | if (indx == 0) | |
5785 | abort (); | |
5786 | } | |
5787 | ||
5788 | /* Figure out how far the target of the relocation is from | |
5789 | the beginning of its section. */ | |
5790 | section_offset = symbol - sec->output_section->vma; | |
5791 | /* The relocation we're building is section-relative. | |
5792 | Therefore, the original addend must be adjusted by the | |
5793 | section offset. */ | |
5794 | *addendp += symbol - sec->output_section->vma; | |
5795 | /* Now, the relocation is just against the section. */ | |
5796 | symbol = sec->output_section->vma; | |
5797 | } | |
be3ccd9c KH |
5798 | |
5799 | /* If the relocation is against a local symbol was previously an | |
5800 | absolute relocation, we must adjust it by the value we give | |
5801 | it in the dynamic symbol table. */ | |
5af5b85b | 5802 | if (local_p && r_type != R_MIPS_REL32) |
7b1f1231 MM |
5803 | *addendp += symbol; |
5804 | ||
5805 | /* The relocation is always an REL32 relocation because we don't | |
5806 | know where the shared library will wind up at load-time. */ | |
5807 | outrel.r_info = ELF32_R_INFO (indx, R_MIPS_REL32); | |
5808 | ||
5809 | /* Adjust the output offset of the relocation to reference the | |
5810 | correct location in the output file. */ | |
5811 | outrel.r_offset += (input_section->output_section->vma | |
5812 | + input_section->output_offset); | |
5813 | } | |
7403cb63 | 5814 | |
7b1f1231 MM |
5815 | /* Put the relocation back out. We have to use the special |
5816 | relocation outputter in the 64-bit case since the 64-bit | |
5817 | relocation format is non-standard. */ | |
103186c6 MM |
5818 | if (ABI_64_P (output_bfd)) |
5819 | { | |
5820 | (*get_elf_backend_data (output_bfd)->s->swap_reloc_out) | |
5821 | (output_bfd, &outrel, | |
be3ccd9c | 5822 | (sreloc->contents |
103186c6 MM |
5823 | + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel))); |
5824 | } | |
5825 | else | |
5826 | bfd_elf32_swap_reloc_out (output_bfd, &outrel, | |
5827 | (((Elf32_External_Rel *) | |
5828 | sreloc->contents) | |
5829 | + sreloc->reloc_count)); | |
7b1f1231 MM |
5830 | |
5831 | /* Record the index of the first relocation referencing H. This | |
5832 | information is later emitted in the .msym section. */ | |
5833 | if (h != NULL | |
be3ccd9c | 5834 | && (h->min_dyn_reloc_index == 0 |
7b1f1231 MM |
5835 | || sreloc->reloc_count < h->min_dyn_reloc_index)) |
5836 | h->min_dyn_reloc_index = sreloc->reloc_count; | |
5837 | ||
5838 | /* We've now added another relocation. */ | |
7403cb63 MM |
5839 | ++sreloc->reloc_count; |
5840 | ||
5841 | /* Make sure the output section is writable. The dynamic linker | |
5842 | will be writing to it. */ | |
5843 | elf_section_data (input_section->output_section)->this_hdr.sh_flags | |
5844 | |= SHF_WRITE; | |
5845 | ||
5846 | /* On IRIX5, make an entry of compact relocation info. */ | |
5847 | if (! skip && IRIX_COMPAT (output_bfd) == ict_irix5) | |
5848 | { | |
be3ccd9c | 5849 | asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel"); |
7403cb63 MM |
5850 | bfd_byte *cr; |
5851 | ||
5852 | if (scpt) | |
5853 | { | |
5854 | Elf32_crinfo cptrel; | |
5855 | ||
5856 | mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG); | |
5857 | cptrel.vaddr = (rel->r_offset | |
5858 | + input_section->output_section->vma | |
5859 | + input_section->output_offset); | |
5860 | if (r_type == R_MIPS_REL32) | |
5861 | mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32); | |
252b5132 | 5862 | else |
7403cb63 MM |
5863 | mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD); |
5864 | mips_elf_set_cr_dist2to (cptrel, 0); | |
7b1f1231 | 5865 | cptrel.konst = *addendp; |
7403cb63 MM |
5866 | |
5867 | cr = (scpt->contents | |
5868 | + sizeof (Elf32_External_compact_rel)); | |
5869 | bfd_elf32_swap_crinfo_out (output_bfd, &cptrel, | |
5870 | ((Elf32_External_crinfo *) cr | |
5871 | + scpt->reloc_count)); | |
5872 | ++scpt->reloc_count; | |
5873 | } | |
5874 | } | |
252b5132 | 5875 | |
7b1f1231 | 5876 | return true; |
7403cb63 | 5877 | } |
252b5132 | 5878 | |
7403cb63 MM |
5879 | /* Calculate the value produced by the RELOCATION (which comes from |
5880 | the INPUT_BFD). The ADDEND is the addend to use for this | |
5881 | RELOCATION; RELOCATION->R_ADDEND is ignored. | |
5882 | ||
5883 | The result of the relocation calculation is stored in VALUEP. | |
197b9ca0 MM |
5884 | REQUIRE_JALXP indicates whether or not the opcode used with this |
5885 | relocation must be JALX. | |
7403cb63 MM |
5886 | |
5887 | This function returns bfd_reloc_continue if the caller need take no | |
5888 | further action regarding this relocation, bfd_reloc_notsupported if | |
5889 | something goes dramatically wrong, bfd_reloc_overflow if an | |
5890 | overflow occurs, and bfd_reloc_ok to indicate success. */ | |
5891 | ||
5892 | static bfd_reloc_status_type | |
be3ccd9c | 5893 | mips_elf_calculate_relocation (abfd, |
7403cb63 MM |
5894 | input_bfd, |
5895 | input_section, | |
5896 | info, | |
5897 | relocation, | |
5898 | addend, | |
5899 | howto, | |
7403cb63 MM |
5900 | local_syms, |
5901 | local_sections, | |
5902 | valuep, | |
197b9ca0 | 5903 | namep, |
be3ccd9c | 5904 | require_jalxp) |
7403cb63 MM |
5905 | bfd *abfd; |
5906 | bfd *input_bfd; | |
5907 | asection *input_section; | |
5908 | struct bfd_link_info *info; | |
103186c6 | 5909 | const Elf_Internal_Rela *relocation; |
7403cb63 MM |
5910 | bfd_vma addend; |
5911 | reloc_howto_type *howto; | |
7403cb63 MM |
5912 | Elf_Internal_Sym *local_syms; |
5913 | asection **local_sections; | |
5914 | bfd_vma *valuep; | |
5915 | const char **namep; | |
197b9ca0 | 5916 | boolean *require_jalxp; |
7403cb63 MM |
5917 | { |
5918 | /* The eventual value we will return. */ | |
5919 | bfd_vma value; | |
5920 | /* The address of the symbol against which the relocation is | |
5921 | occurring. */ | |
5922 | bfd_vma symbol = 0; | |
5923 | /* The final GP value to be used for the relocatable, executable, or | |
5924 | shared object file being produced. */ | |
5925 | bfd_vma gp = (bfd_vma) - 1; | |
5926 | /* The place (section offset or address) of the storage unit being | |
5927 | relocated. */ | |
5928 | bfd_vma p; | |
5929 | /* The value of GP used to create the relocatable object. */ | |
5930 | bfd_vma gp0 = (bfd_vma) - 1; | |
5931 | /* The offset into the global offset table at which the address of | |
5932 | the relocation entry symbol, adjusted by the addend, resides | |
5933 | during execution. */ | |
5934 | bfd_vma g = (bfd_vma) - 1; | |
5935 | /* The section in which the symbol referenced by the relocation is | |
5936 | located. */ | |
5937 | asection *sec = NULL; | |
be3ccd9c | 5938 | struct mips_elf_link_hash_entry *h = NULL; |
103186c6 MM |
5939 | /* True if the symbol referred to by this relocation is a local |
5940 | symbol. */ | |
7403cb63 | 5941 | boolean local_p; |
103186c6 | 5942 | /* True if the symbol referred to by this relocation is "_gp_disp". */ |
7403cb63 MM |
5943 | boolean gp_disp_p = false; |
5944 | Elf_Internal_Shdr *symtab_hdr; | |
5945 | size_t extsymoff; | |
103186c6 | 5946 | unsigned long r_symndx; |
7403cb63 | 5947 | int r_type; |
103186c6 MM |
5948 | /* True if overflow occurred during the calculation of the |
5949 | relocation value. */ | |
7403cb63 | 5950 | boolean overflowed_p; |
197b9ca0 MM |
5951 | /* True if this relocation refers to a MIPS16 function. */ |
5952 | boolean target_is_16_bit_code_p = false; | |
7403cb63 MM |
5953 | |
5954 | /* Parse the relocation. */ | |
5955 | r_symndx = ELF32_R_SYM (relocation->r_info); | |
5956 | r_type = ELF32_R_TYPE (relocation->r_info); | |
be3ccd9c | 5957 | p = (input_section->output_section->vma |
7403cb63 MM |
5958 | + input_section->output_offset |
5959 | + relocation->r_offset); | |
5960 | ||
5961 | /* Assume that there will be no overflow. */ | |
5962 | overflowed_p = false; | |
5963 | ||
6387d602 ILT |
5964 | /* Figure out whether or not the symbol is local, and get the offset |
5965 | used in the array of hash table entries. */ | |
7403cb63 | 5966 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
6387d602 | 5967 | local_p = mips_elf_local_relocation_p (input_bfd, relocation, |
b305ef96 | 5968 | local_sections, false); |
6387d602 ILT |
5969 | if (! elf_bad_symtab (input_bfd)) |
5970 | extsymoff = symtab_hdr->sh_info; | |
5971 | else | |
7403cb63 MM |
5972 | { |
5973 | /* The symbol table does not follow the rule that local symbols | |
5974 | must come before globals. */ | |
5975 | extsymoff = 0; | |
7403cb63 | 5976 | } |
be3ccd9c | 5977 | |
7403cb63 MM |
5978 | /* Figure out the value of the symbol. */ |
5979 | if (local_p) | |
5980 | { | |
5981 | Elf_Internal_Sym *sym; | |
5982 | ||
5983 | sym = local_syms + r_symndx; | |
5984 | sec = local_sections[r_symndx]; | |
5985 | ||
5986 | symbol = sec->output_section->vma + sec->output_offset; | |
5987 | if (ELF_ST_TYPE (sym->st_info) != STT_SECTION) | |
5988 | symbol += sym->st_value; | |
5989 | ||
5990 | /* MIPS16 text labels should be treated as odd. */ | |
5991 | if (sym->st_other == STO_MIPS16) | |
5992 | ++symbol; | |
5993 | ||
5994 | /* Record the name of this symbol, for our caller. */ | |
5995 | *namep = bfd_elf_string_from_elf_section (input_bfd, | |
5996 | symtab_hdr->sh_link, | |
5997 | sym->st_name); | |
e049a0de | 5998 | if (*namep == '\0') |
7403cb63 | 5999 | *namep = bfd_section_name (input_bfd, sec); |
197b9ca0 MM |
6000 | |
6001 | target_is_16_bit_code_p = (sym->st_other == STO_MIPS16); | |
7403cb63 MM |
6002 | } |
6003 | else | |
6004 | { | |
6005 | /* For global symbols we look up the symbol in the hash-table. */ | |
be3ccd9c | 6006 | h = ((struct mips_elf_link_hash_entry *) |
7403cb63 MM |
6007 | elf_sym_hashes (input_bfd) [r_symndx - extsymoff]); |
6008 | /* Find the real hash-table entry for this symbol. */ | |
b305ef96 UC |
6009 | while (h->root.root.type == bfd_link_hash_indirect |
6010 | || h->root.root.type == bfd_link_hash_warning) | |
7403cb63 | 6011 | h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link; |
be3ccd9c | 6012 | |
7403cb63 MM |
6013 | /* Record the name of this symbol, for our caller. */ |
6014 | *namep = h->root.root.root.string; | |
6015 | ||
6016 | /* See if this is the special _gp_disp symbol. Note that such a | |
6017 | symbol must always be a global symbol. */ | |
6018 | if (strcmp (h->root.root.root.string, "_gp_disp") == 0) | |
6019 | { | |
6020 | /* Relocations against _gp_disp are permitted only with | |
6021 | R_MIPS_HI16 and R_MIPS_LO16 relocations. */ | |
6022 | if (r_type != R_MIPS_HI16 && r_type != R_MIPS_LO16) | |
6023 | return bfd_reloc_notsupported; | |
6024 | ||
6025 | gp_disp_p = true; | |
6026 | } | |
97a4bb05 MM |
6027 | /* If this symbol is defined, calculate its address. Note that |
6028 | _gp_disp is a magic symbol, always implicitly defined by the | |
6029 | linker, so it's inappropriate to check to see whether or not | |
6030 | its defined. */ | |
6031 | else if ((h->root.root.type == bfd_link_hash_defined | |
6032 | || h->root.root.type == bfd_link_hash_defweak) | |
6033 | && h->root.root.u.def.section) | |
7403cb63 MM |
6034 | { |
6035 | sec = h->root.root.u.def.section; | |
6036 | if (sec->output_section) | |
be3ccd9c | 6037 | symbol = (h->root.root.u.def.value |
7403cb63 MM |
6038 | + sec->output_section->vma |
6039 | + sec->output_offset); | |
252b5132 | 6040 | else |
7403cb63 MM |
6041 | symbol = h->root.root.u.def.value; |
6042 | } | |
97287574 MM |
6043 | else if (h->root.root.type == bfd_link_hash_undefweak) |
6044 | /* We allow relocations against undefined weak symbols, giving | |
6045 | it the value zero, so that you can undefined weak functions | |
6046 | and check to see if they exist by looking at their | |
6047 | addresses. */ | |
6048 | symbol = 0; | |
3a27a730 | 6049 | else if (info->shared && !info->symbolic && !info->no_undefined |
ba09750c | 6050 | && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT) |
8535d39c | 6051 | symbol = 0; |
f7cb7d68 UC |
6052 | else if (strcmp (h->root.root.root.string, "_DYNAMIC_LINK") == 0 || |
6053 | strcmp (h->root.root.root.string, "_DYNAMIC_LINKING") == 0) | |
3811169e MM |
6054 | { |
6055 | /* If this is a dynamic link, we should have created a | |
be3ccd9c | 6056 | _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol |
f7cb7d68 | 6057 | in in mips_elf_create_dynamic_sections. |
3811169e MM |
6058 | Otherwise, we should define the symbol with a value of 0. |
6059 | FIXME: It should probably get into the symbol table | |
6060 | somehow as well. */ | |
6061 | BFD_ASSERT (! info->shared); | |
6062 | BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL); | |
8535d39c | 6063 | symbol = 0; |
3811169e | 6064 | } |
7403cb63 MM |
6065 | else |
6066 | { | |
5cc7c785 L |
6067 | if (! ((*info->callbacks->undefined_symbol) |
6068 | (info, h->root.root.root.string, input_bfd, | |
6069 | input_section, relocation->r_offset, | |
3a27a730 | 6070 | (!info->shared || info->no_undefined |
ba09750c | 6071 | || ELF_ST_VISIBILITY (h->root.other))))) |
5cc7c785 L |
6072 | return bfd_reloc_undefined; |
6073 | symbol = 0; | |
7403cb63 | 6074 | } |
197b9ca0 MM |
6075 | |
6076 | target_is_16_bit_code_p = (h->root.other == STO_MIPS16); | |
6077 | } | |
be3ccd9c | 6078 | |
197b9ca0 MM |
6079 | /* If this is a 32-bit call to a 16-bit function with a stub, we |
6080 | need to redirect the call to the stub, unless we're already *in* | |
6081 | a stub. */ | |
6082 | if (r_type != R_MIPS16_26 && !info->relocateable | |
6083 | && ((h != NULL && h->fn_stub != NULL) | |
6084 | || (local_p && elf_tdata (input_bfd)->local_stubs != NULL | |
6085 | && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL)) | |
6086 | && !mips_elf_stub_section_p (input_bfd, input_section)) | |
6087 | { | |
6088 | /* This is a 32-bit call to a 16-bit function. We should | |
6089 | have already noticed that we were going to need the | |
6090 | stub. */ | |
6091 | if (local_p) | |
6092 | sec = elf_tdata (input_bfd)->local_stubs[r_symndx]; | |
6093 | else | |
6094 | { | |
6095 | BFD_ASSERT (h->need_fn_stub); | |
6096 | sec = h->fn_stub; | |
6097 | } | |
6098 | ||
6099 | symbol = sec->output_section->vma + sec->output_offset; | |
7403cb63 | 6100 | } |
197b9ca0 MM |
6101 | /* If this is a 16-bit call to a 32-bit function with a stub, we |
6102 | need to redirect the call to the stub. */ | |
6103 | else if (r_type == R_MIPS16_26 && !info->relocateable | |
be3ccd9c | 6104 | && h != NULL |
197b9ca0 MM |
6105 | && (h->call_stub != NULL || h->call_fp_stub != NULL) |
6106 | && !target_is_16_bit_code_p) | |
6107 | { | |
6108 | /* If both call_stub and call_fp_stub are defined, we can figure | |
6109 | out which one to use by seeing which one appears in the input | |
6110 | file. */ | |
6111 | if (h->call_stub != NULL && h->call_fp_stub != NULL) | |
6112 | { | |
6113 | asection *o; | |
6114 | ||
6115 | sec = NULL; | |
6116 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
6117 | { | |
6118 | if (strncmp (bfd_get_section_name (input_bfd, o), | |
6119 | CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0) | |
6120 | { | |
6121 | sec = h->call_fp_stub; | |
6122 | break; | |
6123 | } | |
6124 | } | |
6125 | if (sec == NULL) | |
6126 | sec = h->call_stub; | |
6127 | } | |
6128 | else if (h->call_stub != NULL) | |
6129 | sec = h->call_stub; | |
6130 | else | |
6131 | sec = h->call_fp_stub; | |
6132 | ||
6133 | BFD_ASSERT (sec->_raw_size > 0); | |
6134 | symbol = sec->output_section->vma + sec->output_offset; | |
6135 | } | |
6136 | ||
6137 | /* Calls from 16-bit code to 32-bit code and vice versa require the | |
6138 | special jalx instruction. */ | |
6387d602 ILT |
6139 | *require_jalxp = (!info->relocateable |
6140 | && ((r_type == R_MIPS16_26) != target_is_16_bit_code_p)); | |
252b5132 | 6141 | |
b305ef96 UC |
6142 | local_p = mips_elf_local_relocation_p (input_bfd, relocation, |
6143 | local_sections, true); | |
6144 | ||
7403cb63 MM |
6145 | /* If we haven't already determined the GOT offset, or the GP value, |
6146 | and we're going to need it, get it now. */ | |
6147 | switch (r_type) | |
6148 | { | |
6149 | case R_MIPS_CALL16: | |
2841ecd0 | 6150 | case R_MIPS_GOT16: |
7403cb63 MM |
6151 | case R_MIPS_GOT_DISP: |
6152 | case R_MIPS_GOT_HI16: | |
6153 | case R_MIPS_CALL_HI16: | |
6154 | case R_MIPS_GOT_LO16: | |
6155 | case R_MIPS_CALL_LO16: | |
6156 | /* Find the index into the GOT where this value is located. */ | |
4f2860ca | 6157 | if (!local_p) |
7403cb63 MM |
6158 | { |
6159 | BFD_ASSERT (addend == 0); | |
be3ccd9c | 6160 | g = mips_elf_global_got_index |
7403cb63 | 6161 | (elf_hash_table (info)->dynobj, |
be3ccd9c | 6162 | (struct elf_link_hash_entry *) h); |
9a8f3bb7 UC |
6163 | if (! elf_hash_table(info)->dynamic_sections_created |
6164 | || (info->shared | |
6165 | && (info->symbolic || h->root.dynindx == -1) | |
6166 | && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))) | |
6167 | { | |
6168 | /* This is a static link or a -Bsymbolic link. The | |
6169 | symbol is defined locally, or was forced to be local. | |
6170 | We must initialize this entry in the GOT. */ | |
6171 | asection *sgot = mips_elf_got_section(elf_hash_table | |
6172 | (info)->dynobj); | |
6173 | MIPS_ELF_PUT_WORD (elf_hash_table (info)->dynobj, | |
6174 | symbol + addend, sgot->contents + g); | |
6175 | } | |
7403cb63 | 6176 | } |
4f2860ca MM |
6177 | else if (r_type == R_MIPS_GOT16) |
6178 | /* There's no need to create a local GOT entry here; the | |
6179 | calculation for a local GOT16 entry does not involve G. */ | |
6180 | break; | |
7403cb63 MM |
6181 | else |
6182 | { | |
6183 | g = mips_elf_local_got_index (abfd, info, symbol + addend); | |
6184 | if (g == (bfd_vma) -1) | |
6185 | return false; | |
6186 | } | |
252b5132 | 6187 | |
7403cb63 MM |
6188 | /* Convert GOT indices to actual offsets. */ |
6189 | g = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj, | |
6190 | abfd, g); | |
6191 | break; | |
be3ccd9c | 6192 | |
7403cb63 MM |
6193 | case R_MIPS_HI16: |
6194 | case R_MIPS_LO16: | |
6195 | case R_MIPS_GPREL16: | |
6196 | case R_MIPS_GPREL32: | |
0af99795 | 6197 | case R_MIPS_LITERAL: |
7403cb63 MM |
6198 | gp0 = _bfd_get_gp_value (input_bfd); |
6199 | gp = _bfd_get_gp_value (abfd); | |
6200 | break; | |
252b5132 | 6201 | |
7403cb63 MM |
6202 | default: |
6203 | break; | |
6204 | } | |
252b5132 | 6205 | |
7403cb63 MM |
6206 | /* Figure out what kind of relocation is being performed. */ |
6207 | switch (r_type) | |
6208 | { | |
6209 | case R_MIPS_NONE: | |
6210 | return bfd_reloc_continue; | |
252b5132 | 6211 | |
7403cb63 MM |
6212 | case R_MIPS_16: |
6213 | value = symbol + mips_elf_sign_extend (addend, 16); | |
6214 | overflowed_p = mips_elf_overflow_p (value, 16); | |
6215 | break; | |
252b5132 | 6216 | |
7403cb63 MM |
6217 | case R_MIPS_32: |
6218 | case R_MIPS_REL32: | |
a3c7651d | 6219 | case R_MIPS_64: |
7b1f1231 MM |
6220 | if ((info->shared |
6221 | || (elf_hash_table (info)->dynamic_sections_created | |
6222 | && h != NULL | |
b305ef96 UC |
6223 | && ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) |
6224 | != 0))) | |
7b1f1231 | 6225 | && (input_section->flags & SEC_ALLOC) != 0) |
7403cb63 | 6226 | { |
7b1f1231 MM |
6227 | /* If we're creating a shared library, or this relocation is |
6228 | against a symbol in a shared library, then we can't know | |
6229 | where the symbol will end up. So, we create a relocation | |
6230 | record in the output, and leave the job up to the dynamic | |
6231 | linker. */ | |
6232 | value = addend; | |
be3ccd9c KH |
6233 | if (!mips_elf_create_dynamic_relocation (abfd, |
6234 | info, | |
7b1f1231 MM |
6235 | relocation, |
6236 | h, | |
6237 | sec, | |
6238 | symbol, | |
6239 | &value, | |
5af5b85b | 6240 | input_section, local_p)) |
7b1f1231 | 6241 | return false; |
7403cb63 MM |
6242 | } |
6243 | else | |
6244 | { | |
a3c7651d | 6245 | if (r_type != R_MIPS_REL32) |
7403cb63 MM |
6246 | value = symbol + addend; |
6247 | else | |
6248 | value = addend; | |
6249 | } | |
6250 | value &= howto->dst_mask; | |
6251 | break; | |
6252 | ||
bb2d6cd7 GK |
6253 | case R_MIPS_PC32: |
6254 | case R_MIPS_PC64: | |
6255 | case R_MIPS_GNU_REL_LO16: | |
6256 | value = symbol + addend - p; | |
6257 | value &= howto->dst_mask; | |
6258 | break; | |
6259 | ||
6260 | case R_MIPS_GNU_REL16_S2: | |
6261 | value = symbol + mips_elf_sign_extend (addend << 2, 18) - p; | |
6262 | overflowed_p = mips_elf_overflow_p (value, 18); | |
6263 | value = (value >> 2) & howto->dst_mask; | |
6264 | break; | |
6265 | ||
6266 | case R_MIPS_GNU_REL_HI16: | |
6267 | value = mips_elf_high (addend + symbol - p); | |
6268 | value &= howto->dst_mask; | |
6269 | break; | |
6270 | ||
e53bd91b MM |
6271 | case R_MIPS16_26: |
6272 | /* The calculation for R_MIPS_26 is just the same as for an | |
6273 | R_MIPS_26. It's only the storage of the relocated field into | |
1e52e2ee | 6274 | the output file that's different. That's handled in |
e53bd91b MM |
6275 | mips_elf_perform_relocation. So, we just fall through to the |
6276 | R_MIPS_26 case here. */ | |
7403cb63 MM |
6277 | case R_MIPS_26: |
6278 | if (local_p) | |
6279 | value = (((addend << 2) | (p & 0xf0000000)) + symbol) >> 2; | |
6280 | else | |
6281 | value = (mips_elf_sign_extend (addend << 2, 28) + symbol) >> 2; | |
6282 | value &= howto->dst_mask; | |
6283 | break; | |
6284 | ||
6285 | case R_MIPS_HI16: | |
6286 | if (!gp_disp_p) | |
6287 | { | |
6288 | value = mips_elf_high (addend + symbol); | |
6289 | value &= howto->dst_mask; | |
6290 | } | |
6291 | else | |
6292 | { | |
6293 | value = mips_elf_high (addend + gp - p); | |
6294 | overflowed_p = mips_elf_overflow_p (value, 16); | |
6295 | } | |
6296 | break; | |
6297 | ||
6298 | case R_MIPS_LO16: | |
6299 | if (!gp_disp_p) | |
6300 | value = (symbol + addend) & howto->dst_mask; | |
6301 | else | |
6302 | { | |
6303 | value = addend + gp - p + 4; | |
97a4bb05 MM |
6304 | /* The MIPS ABI requires checking the R_MIPS_LO16 relocation |
6305 | for overflow. But, on, say, Irix 5, relocations against | |
6306 | _gp_disp are normally generated from the .cpload | |
6307 | pseudo-op. It generates code that normally looks like | |
6308 | this: | |
6309 | ||
6310 | lui $gp,%hi(_gp_disp) | |
6311 | addiu $gp,$gp,%lo(_gp_disp) | |
6312 | addu $gp,$gp,$t9 | |
6313 | ||
6314 | Here $t9 holds the address of the function being called, | |
6315 | as required by the MIPS ELF ABI. The R_MIPS_LO16 | |
e53bd91b | 6316 | relocation can easily overflow in this situation, but the |
97a4bb05 MM |
6317 | R_MIPS_HI16 relocation will handle the overflow. |
6318 | Therefore, we consider this a bug in the MIPS ABI, and do | |
6319 | not check for overflow here. */ | |
7403cb63 MM |
6320 | } |
6321 | break; | |
6322 | ||
6323 | case R_MIPS_LITERAL: | |
6324 | /* Because we don't merge literal sections, we can handle this | |
6325 | just like R_MIPS_GPREL16. In the long run, we should merge | |
6326 | shared literals, and then we will need to additional work | |
6327 | here. */ | |
6328 | ||
6329 | /* Fall through. */ | |
6330 | ||
b7233c24 MM |
6331 | case R_MIPS16_GPREL: |
6332 | /* The R_MIPS16_GPREL performs the same calculation as | |
6333 | R_MIPS_GPREL16, but stores the relocated bits in a different | |
6334 | order. We don't need to do anything special here; the | |
6335 | differences are handled in mips_elf_perform_relocation. */ | |
7403cb63 MM |
6336 | case R_MIPS_GPREL16: |
6337 | if (local_p) | |
6338 | value = mips_elf_sign_extend (addend, 16) + symbol + gp0 - gp; | |
6339 | else | |
6340 | value = mips_elf_sign_extend (addend, 16) + symbol - gp; | |
6341 | overflowed_p = mips_elf_overflow_p (value, 16); | |
6342 | break; | |
be3ccd9c | 6343 | |
7403cb63 MM |
6344 | case R_MIPS_GOT16: |
6345 | if (local_p) | |
6346 | { | |
b305ef96 | 6347 | boolean forced; |
be3ccd9c | 6348 | |
b305ef96 UC |
6349 | /* The special case is when the symbol is forced to be local. We |
6350 | need the full address in the GOT since no R_MIPS_LO16 relocation | |
6351 | follows. */ | |
6352 | forced = ! mips_elf_local_relocation_p (input_bfd, relocation, | |
6353 | local_sections, false); | |
6354 | value = mips_elf_got16_entry (abfd, info, symbol + addend, forced); | |
7403cb63 MM |
6355 | if (value == (bfd_vma) -1) |
6356 | return false; | |
be3ccd9c | 6357 | value |
7403cb63 MM |
6358 | = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj, |
6359 | abfd, | |
6360 | value); | |
6361 | overflowed_p = mips_elf_overflow_p (value, 16); | |
6362 | break; | |
6363 | } | |
6364 | ||
6365 | /* Fall through. */ | |
6366 | ||
6367 | case R_MIPS_CALL16: | |
6368 | case R_MIPS_GOT_DISP: | |
6369 | value = g; | |
6370 | overflowed_p = mips_elf_overflow_p (value, 16); | |
6371 | break; | |
6372 | ||
6373 | case R_MIPS_GPREL32: | |
6374 | value = (addend + symbol + gp0 - gp) & howto->dst_mask; | |
6375 | break; | |
6376 | ||
6377 | case R_MIPS_PC16: | |
6378 | value = mips_elf_sign_extend (addend, 16) + symbol - p; | |
2a6d49ea | 6379 | value = (bfd_vma) ((bfd_signed_vma) value / 4); |
7403cb63 MM |
6380 | overflowed_p = mips_elf_overflow_p (value, 16); |
6381 | break; | |
6382 | ||
6383 | case R_MIPS_GOT_HI16: | |
6384 | case R_MIPS_CALL_HI16: | |
6385 | /* We're allowed to handle these two relocations identically. | |
6386 | The dynamic linker is allowed to handle the CALL relocations | |
6387 | differently by creating a lazy evaluation stub. */ | |
6388 | value = g; | |
6389 | value = mips_elf_high (value); | |
6390 | value &= howto->dst_mask; | |
6391 | break; | |
6392 | ||
6393 | case R_MIPS_GOT_LO16: | |
6394 | case R_MIPS_CALL_LO16: | |
6395 | value = g & howto->dst_mask; | |
6396 | break; | |
6397 | ||
7403cb63 MM |
6398 | case R_MIPS_GOT_PAGE: |
6399 | value = mips_elf_got_page (abfd, info, symbol + addend, NULL); | |
6400 | if (value == (bfd_vma) -1) | |
6401 | return false; | |
6402 | value = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj, | |
6403 | abfd, | |
6404 | value); | |
6405 | overflowed_p = mips_elf_overflow_p (value, 16); | |
6406 | break; | |
be3ccd9c | 6407 | |
7403cb63 MM |
6408 | case R_MIPS_GOT_OFST: |
6409 | mips_elf_got_page (abfd, info, symbol + addend, &value); | |
6410 | overflowed_p = mips_elf_overflow_p (value, 16); | |
6411 | break; | |
6412 | ||
6413 | case R_MIPS_SUB: | |
6414 | value = symbol - addend; | |
6415 | value &= howto->dst_mask; | |
6416 | break; | |
6417 | ||
6418 | case R_MIPS_HIGHER: | |
6419 | value = mips_elf_higher (addend + symbol); | |
6420 | value &= howto->dst_mask; | |
6421 | break; | |
6422 | ||
6423 | case R_MIPS_HIGHEST: | |
6424 | value = mips_elf_highest (addend + symbol); | |
6425 | value &= howto->dst_mask; | |
6426 | break; | |
be3ccd9c | 6427 | |
7403cb63 MM |
6428 | case R_MIPS_SCN_DISP: |
6429 | value = symbol + addend - sec->output_offset; | |
6430 | value &= howto->dst_mask; | |
6431 | break; | |
6432 | ||
6433 | case R_MIPS_PJUMP: | |
6434 | case R_MIPS_JALR: | |
6435 | /* Both of these may be ignored. R_MIPS_JALR is an optimization | |
6436 | hint; we could improve performance by honoring that hint. */ | |
6437 | return bfd_reloc_continue; | |
6438 | ||
6439 | case R_MIPS_GNU_VTINHERIT: | |
6440 | case R_MIPS_GNU_VTENTRY: | |
6441 | /* We don't do anything with these at present. */ | |
6442 | return bfd_reloc_continue; | |
6443 | ||
7403cb63 MM |
6444 | default: |
6445 | /* An unrecognized relocation type. */ | |
6446 | return bfd_reloc_notsupported; | |
6447 | } | |
6448 | ||
6449 | /* Store the VALUE for our caller. */ | |
6450 | *valuep = value; | |
6451 | return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok; | |
6452 | } | |
6453 | ||
6454 | /* Obtain the field relocated by RELOCATION. */ | |
6455 | ||
6456 | static bfd_vma | |
6457 | mips_elf_obtain_contents (howto, relocation, input_bfd, contents) | |
6458 | reloc_howto_type *howto; | |
103186c6 | 6459 | const Elf_Internal_Rela *relocation; |
7403cb63 MM |
6460 | bfd *input_bfd; |
6461 | bfd_byte *contents; | |
6462 | { | |
6463 | bfd_vma x; | |
6464 | bfd_byte *location = contents + relocation->r_offset; | |
6465 | ||
b7233c24 MM |
6466 | /* Obtain the bytes. */ |
6467 | x = bfd_get (8 * bfd_get_reloc_size (howto), input_bfd, location); | |
7403cb63 | 6468 | |
6296902e MM |
6469 | if ((ELF32_R_TYPE (relocation->r_info) == R_MIPS16_26 |
6470 | || ELF32_R_TYPE (relocation->r_info) == R_MIPS16_GPREL) | |
1e52e2ee MM |
6471 | && bfd_little_endian (input_bfd)) |
6472 | /* The two 16-bit words will be reversed on a little-endian | |
6473 | system. See mips_elf_perform_relocation for more details. */ | |
6474 | x = (((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16)); | |
6475 | ||
7403cb63 MM |
6476 | return x; |
6477 | } | |
6478 | ||
6479 | /* It has been determined that the result of the RELOCATION is the | |
6480 | VALUE. Use HOWTO to place VALUE into the output file at the | |
6481 | appropriate position. The SECTION is the section to which the | |
197b9ca0 MM |
6482 | relocation applies. If REQUIRE_JALX is true, then the opcode used |
6483 | for the relocation must be either JAL or JALX, and it is | |
6484 | unconditionally converted to JALX. | |
7403cb63 MM |
6485 | |
6486 | Returns false if anything goes wrong. */ | |
252b5132 | 6487 | |
197b9ca0 | 6488 | static boolean |
e53bd91b | 6489 | mips_elf_perform_relocation (info, howto, relocation, value, |
be3ccd9c | 6490 | input_bfd, input_section, |
197b9ca0 | 6491 | contents, require_jalx) |
e53bd91b | 6492 | struct bfd_link_info *info; |
7403cb63 | 6493 | reloc_howto_type *howto; |
103186c6 | 6494 | const Elf_Internal_Rela *relocation; |
7403cb63 MM |
6495 | bfd_vma value; |
6496 | bfd *input_bfd; | |
197b9ca0 | 6497 | asection *input_section; |
7403cb63 | 6498 | bfd_byte *contents; |
197b9ca0 | 6499 | boolean require_jalx; |
7403cb63 MM |
6500 | { |
6501 | bfd_vma x; | |
e53bd91b | 6502 | bfd_byte *location; |
197b9ca0 | 6503 | int r_type = ELF32_R_TYPE (relocation->r_info); |
e53bd91b MM |
6504 | |
6505 | /* Figure out where the relocation is occurring. */ | |
6506 | location = contents + relocation->r_offset; | |
252b5132 | 6507 | |
7403cb63 MM |
6508 | /* Obtain the current value. */ |
6509 | x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents); | |
252b5132 | 6510 | |
7403cb63 MM |
6511 | /* Clear the field we are setting. */ |
6512 | x &= ~howto->dst_mask; | |
252b5132 | 6513 | |
e53bd91b MM |
6514 | /* If this is the R_MIPS16_26 relocation, we must store the |
6515 | value in a funny way. */ | |
197b9ca0 | 6516 | if (r_type == R_MIPS16_26) |
7403cb63 | 6517 | { |
e53bd91b MM |
6518 | /* R_MIPS16_26 is used for the mips16 jal and jalx instructions. |
6519 | Most mips16 instructions are 16 bits, but these instructions | |
6520 | are 32 bits. | |
6521 | ||
6522 | The format of these instructions is: | |
6523 | ||
6524 | +--------------+--------------------------------+ | |
6525 | ! JALX ! X! Imm 20:16 ! Imm 25:21 ! | |
6526 | +--------------+--------------------------------+ | |
6527 | ! Immediate 15:0 ! | |
6528 | +-----------------------------------------------+ | |
be3ccd9c | 6529 | |
e53bd91b MM |
6530 | JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx. |
6531 | Note that the immediate value in the first word is swapped. | |
6532 | ||
6533 | When producing a relocateable object file, R_MIPS16_26 is | |
6534 | handled mostly like R_MIPS_26. In particular, the addend is | |
6535 | stored as a straight 26-bit value in a 32-bit instruction. | |
6536 | (gas makes life simpler for itself by never adjusting a | |
6537 | R_MIPS16_26 reloc to be against a section, so the addend is | |
6538 | always zero). However, the 32 bit instruction is stored as 2 | |
6539 | 16-bit values, rather than a single 32-bit value. In a | |
6540 | big-endian file, the result is the same; in a little-endian | |
6541 | file, the two 16-bit halves of the 32 bit value are swapped. | |
6542 | This is so that a disassembler can recognize the jal | |
6543 | instruction. | |
6544 | ||
6545 | When doing a final link, R_MIPS16_26 is treated as a 32 bit | |
6546 | instruction stored as two 16-bit values. The addend A is the | |
6547 | contents of the targ26 field. The calculation is the same as | |
6548 | R_MIPS_26. When storing the calculated value, reorder the | |
6549 | immediate value as shown above, and don't forget to store the | |
6550 | value as two 16-bit values. | |
6551 | ||
6552 | To put it in MIPS ABI terms, the relocation field is T-targ26-16, | |
6553 | defined as | |
be3ccd9c | 6554 | |
e53bd91b MM |
6555 | big-endian: |
6556 | +--------+----------------------+ | |
6557 | | | | | |
6558 | | | targ26-16 | | |
6559 | |31 26|25 0| | |
6560 | +--------+----------------------+ | |
be3ccd9c | 6561 | |
e53bd91b MM |
6562 | little-endian: |
6563 | +----------+------+-------------+ | |
6564 | | | | | | |
6565 | | sub1 | | sub2 | | |
6566 | |0 9|10 15|16 31| | |
6567 | +----------+--------------------+ | |
6568 | where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is | |
6569 | ((sub1 << 16) | sub2)). | |
be3ccd9c | 6570 | |
e53bd91b MM |
6571 | When producing a relocateable object file, the calculation is |
6572 | (((A < 2) | (P & 0xf0000000) + S) >> 2) | |
6573 | When producing a fully linked file, the calculation is | |
6574 | let R = (((A < 2) | (P & 0xf0000000) + S) >> 2) | |
6575 | ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff) */ | |
6576 | ||
6577 | if (!info->relocateable) | |
6578 | /* Shuffle the bits according to the formula above. */ | |
be3ccd9c KH |
6579 | value = (((value & 0x1f0000) << 5) |
6580 | | ((value & 0x3e00000) >> 5) | |
e53bd91b | 6581 | | (value & 0xffff)); |
be3ccd9c | 6582 | |
e53bd91b | 6583 | } |
197b9ca0 | 6584 | else if (r_type == R_MIPS16_GPREL) |
b7233c24 MM |
6585 | { |
6586 | /* R_MIPS16_GPREL is used for GP-relative addressing in mips16 | |
6587 | mode. A typical instruction will have a format like this: | |
6588 | ||
6589 | +--------------+--------------------------------+ | |
6590 | ! EXTEND ! Imm 10:5 ! Imm 15:11 ! | |
6591 | +--------------+--------------------------------+ | |
6592 | ! Major ! rx ! ry ! Imm 4:0 ! | |
6593 | +--------------+--------------------------------+ | |
be3ccd9c | 6594 | |
b7233c24 MM |
6595 | EXTEND is the five bit value 11110. Major is the instruction |
6596 | opcode. | |
be3ccd9c | 6597 | |
b7233c24 MM |
6598 | This is handled exactly like R_MIPS_GPREL16, except that the |
6599 | addend is retrieved and stored as shown in this diagram; that | |
be3ccd9c | 6600 | is, the Imm fields above replace the V-rel16 field. |
b7233c24 | 6601 | |
6296902e MM |
6602 | All we need to do here is shuffle the bits appropriately. As |
6603 | above, the two 16-bit halves must be swapped on a | |
6604 | little-endian system. */ | |
b7233c24 MM |
6605 | value = (((value & 0x7e0) << 16) |
6606 | | ((value & 0xf800) << 5) | |
6607 | | (value & 0x1f)); | |
6608 | } | |
252b5132 | 6609 | |
e53bd91b MM |
6610 | /* Set the field. */ |
6611 | x |= (value & howto->dst_mask); | |
252b5132 | 6612 | |
197b9ca0 MM |
6613 | /* If required, turn JAL into JALX. */ |
6614 | if (require_jalx) | |
6615 | { | |
6616 | boolean ok; | |
6617 | bfd_vma opcode = x >> 26; | |
6618 | bfd_vma jalx_opcode; | |
6619 | ||
6620 | /* Check to see if the opcode is already JAL or JALX. */ | |
6621 | if (r_type == R_MIPS16_26) | |
6622 | { | |
6623 | ok = ((opcode == 0x6) || (opcode == 0x7)); | |
6624 | jalx_opcode = 0x7; | |
6625 | } | |
6626 | else | |
6627 | { | |
6628 | ok = ((opcode == 0x3) || (opcode == 0x1d)); | |
6629 | jalx_opcode = 0x1d; | |
6630 | } | |
6631 | ||
6632 | /* If the opcode is not JAL or JALX, there's a problem. */ | |
6633 | if (!ok) | |
6634 | { | |
6635 | (*_bfd_error_handler) | |
6636 | (_("%s: %s+0x%lx: jump to stub routine which is not jal"), | |
6637 | bfd_get_filename (input_bfd), | |
6638 | input_section->name, | |
6639 | (unsigned long) relocation->r_offset); | |
6640 | bfd_set_error (bfd_error_bad_value); | |
6641 | return false; | |
6642 | } | |
6643 | ||
6644 | /* Make this the JALX opcode. */ | |
6645 | x = (x & ~(0x3f << 26)) | (jalx_opcode << 26); | |
6646 | } | |
6647 | ||
6296902e MM |
6648 | /* Swap the high- and low-order 16 bits on little-endian systems |
6649 | when doing a MIPS16 relocation. */ | |
197b9ca0 | 6650 | if ((r_type == R_MIPS16_GPREL || r_type == R_MIPS16_26) |
6296902e MM |
6651 | && bfd_little_endian (input_bfd)) |
6652 | x = (((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16)); | |
be3ccd9c | 6653 | |
e53bd91b MM |
6654 | /* Put the value into the output. */ |
6655 | bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location); | |
197b9ca0 MM |
6656 | return true; |
6657 | } | |
6658 | ||
6659 | /* Returns true if SECTION is a MIPS16 stub section. */ | |
6660 | ||
6661 | static boolean | |
6662 | mips_elf_stub_section_p (abfd, section) | |
6387d602 | 6663 | bfd *abfd ATTRIBUTE_UNUSED; |
197b9ca0 MM |
6664 | asection *section; |
6665 | { | |
6666 | const char *name = bfd_get_section_name (abfd, section); | |
6667 | ||
6668 | return (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0 | |
6669 | || strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0 | |
6670 | || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0); | |
7403cb63 | 6671 | } |
252b5132 | 6672 | |
7403cb63 | 6673 | /* Relocate a MIPS ELF section. */ |
252b5132 | 6674 | |
103186c6 MM |
6675 | boolean |
6676 | _bfd_mips_elf_relocate_section (output_bfd, info, input_bfd, input_section, | |
6677 | contents, relocs, local_syms, local_sections) | |
7403cb63 MM |
6678 | bfd *output_bfd; |
6679 | struct bfd_link_info *info; | |
6680 | bfd *input_bfd; | |
6681 | asection *input_section; | |
6682 | bfd_byte *contents; | |
6683 | Elf_Internal_Rela *relocs; | |
6684 | Elf_Internal_Sym *local_syms; | |
6685 | asection **local_sections; | |
6686 | { | |
31367b81 | 6687 | Elf_Internal_Rela *rel; |
103186c6 | 6688 | const Elf_Internal_Rela *relend; |
86033394 | 6689 | bfd_vma addend = 0; |
7403cb63 | 6690 | boolean use_saved_addend_p = false; |
103186c6 | 6691 | struct elf_backend_data *bed; |
252b5132 | 6692 | |
103186c6 MM |
6693 | bed = get_elf_backend_data (output_bfd); |
6694 | relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel; | |
7403cb63 MM |
6695 | for (rel = relocs; rel < relend; ++rel) |
6696 | { | |
6697 | const char *name; | |
6698 | bfd_vma value; | |
7403cb63 | 6699 | reloc_howto_type *howto; |
197b9ca0 | 6700 | boolean require_jalx; |
31367b81 MM |
6701 | /* True if the relocation is a RELA relocation, rather than a |
6702 | REL relocation. */ | |
6703 | boolean rela_relocation_p = true; | |
6704 | int r_type = ELF32_R_TYPE (rel->r_info); | |
252b5132 | 6705 | |
7403cb63 | 6706 | /* Find the relocation howto for this relocation. */ |
31367b81 | 6707 | if (r_type == R_MIPS_64 && !ABI_64_P (output_bfd)) |
0af99795 GK |
6708 | { |
6709 | /* Some 32-bit code uses R_MIPS_64. In particular, people use | |
be3ccd9c | 6710 | 64-bit code, but make sure all their addresses are in the |
0af99795 GK |
6711 | lowermost or uppermost 32-bit section of the 64-bit address |
6712 | space. Thus, when they use an R_MIPS_64 they mean what is | |
6713 | usually meant by R_MIPS_32, with the exception that the | |
6714 | stored value is sign-extended to 64 bits. */ | |
6715 | howto = elf_mips_howto_table + R_MIPS_32; | |
6716 | ||
6717 | /* On big-endian systems, we need to lie about the position | |
6718 | of the reloc. */ | |
6719 | if (bfd_big_endian (input_bfd)) | |
be3ccd9c | 6720 | rel->r_offset += 4; |
0af99795 | 6721 | } |
a3c7651d | 6722 | else |
c9b3cbf3 | 6723 | howto = mips_rtype_to_howto (r_type); |
252b5132 | 6724 | |
7403cb63 MM |
6725 | if (!use_saved_addend_p) |
6726 | { | |
6727 | Elf_Internal_Shdr *rel_hdr; | |
6728 | ||
6729 | /* If these relocations were originally of the REL variety, | |
6730 | we must pull the addend out of the field that will be | |
6731 | relocated. Otherwise, we simply use the contents of the | |
6732 | RELA relocation. To determine which flavor or relocation | |
6733 | this is, we depend on the fact that the INPUT_SECTION's | |
6734 | REL_HDR is read before its REL_HDR2. */ | |
6735 | rel_hdr = &elf_section_data (input_section)->rel_hdr; | |
5f771d47 | 6736 | if ((size_t) (rel - relocs) |
103186c6 MM |
6737 | >= (rel_hdr->sh_size / rel_hdr->sh_entsize |
6738 | * bed->s->int_rels_per_ext_rel)) | |
7403cb63 | 6739 | rel_hdr = elf_section_data (input_section)->rel_hdr2; |
103186c6 | 6740 | if (rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (input_bfd)) |
7403cb63 | 6741 | { |
31367b81 MM |
6742 | /* Note that this is a REL relocation. */ |
6743 | rela_relocation_p = false; | |
7403cb63 | 6744 | |
31367b81 | 6745 | /* Get the addend, which is stored in the input file. */ |
be3ccd9c | 6746 | addend = mips_elf_obtain_contents (howto, |
7403cb63 MM |
6747 | rel, |
6748 | input_bfd, | |
6749 | contents); | |
6750 | addend &= howto->src_mask; | |
6751 | ||
6752 | /* For some kinds of relocations, the ADDEND is a | |
6753 | combination of the addend stored in two different | |
6754 | relocations. */ | |
6387d602 | 6755 | if (r_type == R_MIPS_HI16 |
bb2d6cd7 | 6756 | || r_type == R_MIPS_GNU_REL_HI16 |
6387d602 ILT |
6757 | || (r_type == R_MIPS_GOT16 |
6758 | && mips_elf_local_relocation_p (input_bfd, rel, | |
b305ef96 | 6759 | local_sections, false))) |
252b5132 | 6760 | { |
23b255aa MM |
6761 | bfd_vma l; |
6762 | const Elf_Internal_Rela *lo16_relocation; | |
6763 | reloc_howto_type *lo16_howto; | |
bb2d6cd7 | 6764 | int lo; |
23b255aa | 6765 | |
e7c44218 MM |
6766 | /* The combined value is the sum of the HI16 addend, |
6767 | left-shifted by sixteen bits, and the LO16 | |
6768 | addend, sign extended. (Usually, the code does | |
6769 | a `lui' of the HI16 value, and then an `addiu' of | |
be3ccd9c | 6770 | the LO16 value.) |
e7c44218 | 6771 | |
bb2d6cd7 GK |
6772 | Scan ahead to find a matching LO16 relocation. */ |
6773 | if (r_type == R_MIPS_GNU_REL_HI16) | |
6774 | lo = R_MIPS_GNU_REL_LO16; | |
6775 | else | |
6776 | lo = R_MIPS_LO16; | |
be3ccd9c KH |
6777 | lo16_relocation |
6778 | = mips_elf_next_relocation (lo, rel, relend); | |
23b255aa | 6779 | if (lo16_relocation == NULL) |
7403cb63 | 6780 | return false; |
252b5132 | 6781 | |
23b255aa | 6782 | /* Obtain the addend kept there. */ |
bb2d6cd7 | 6783 | lo16_howto = mips_rtype_to_howto (lo); |
23b255aa MM |
6784 | l = mips_elf_obtain_contents (lo16_howto, |
6785 | lo16_relocation, | |
6786 | input_bfd, contents); | |
6787 | l &= lo16_howto->src_mask; | |
e7c44218 | 6788 | l = mips_elf_sign_extend (l, 16); |
23b255aa | 6789 | |
7403cb63 | 6790 | addend <<= 16; |
252b5132 | 6791 | |
7403cb63 | 6792 | /* Compute the combined addend. */ |
e7c44218 | 6793 | addend += l; |
252b5132 | 6794 | } |
b7233c24 MM |
6795 | else if (r_type == R_MIPS16_GPREL) |
6796 | { | |
6797 | /* The addend is scrambled in the object file. See | |
6798 | mips_elf_perform_relocation for details on the | |
6799 | format. */ | |
6800 | addend = (((addend & 0x1f0000) >> 5) | |
6801 | | ((addend & 0x7e00000) >> 16) | |
6802 | | (addend & 0x1f)); | |
6803 | } | |
252b5132 RH |
6804 | } |
6805 | else | |
7403cb63 MM |
6806 | addend = rel->r_addend; |
6807 | } | |
252b5132 | 6808 | |
31367b81 MM |
6809 | if (info->relocateable) |
6810 | { | |
6811 | Elf_Internal_Sym *sym; | |
6812 | unsigned long r_symndx; | |
6813 | ||
7893e6a2 GK |
6814 | if (r_type == R_MIPS_64 && !ABI_64_P (output_bfd) |
6815 | && bfd_big_endian (input_bfd)) | |
6816 | rel->r_offset -= 4; | |
6817 | ||
31367b81 | 6818 | /* Since we're just relocating, all we need to do is copy |
0db63c18 MM |
6819 | the relocations back out to the object file, unless |
6820 | they're against a section symbol, in which case we need | |
6821 | to adjust by the section offset, or unless they're GP | |
6822 | relative in which case we need to adjust by the amount | |
6823 | that we're adjusting GP in this relocateable object. */ | |
31367b81 | 6824 | |
b305ef96 UC |
6825 | if (!mips_elf_local_relocation_p (input_bfd, rel, local_sections, |
6826 | false)) | |
f1a5f37e | 6827 | /* There's nothing to do for non-local relocations. */ |
31367b81 MM |
6828 | continue; |
6829 | ||
be3ccd9c | 6830 | if (r_type == R_MIPS16_GPREL |
0db63c18 | 6831 | || r_type == R_MIPS_GPREL16 |
0af99795 GK |
6832 | || r_type == R_MIPS_GPREL32 |
6833 | || r_type == R_MIPS_LITERAL) | |
0db63c18 MM |
6834 | addend -= (_bfd_get_gp_value (output_bfd) |
6835 | - _bfd_get_gp_value (input_bfd)); | |
bb2d6cd7 GK |
6836 | else if (r_type == R_MIPS_26 || r_type == R_MIPS16_26 |
6837 | || r_type == R_MIPS_GNU_REL16_S2) | |
e7c44218 MM |
6838 | /* The addend is stored without its two least |
6839 | significant bits (which are always zero.) In a | |
6840 | non-relocateable link, calculate_relocation will do | |
6841 | this shift; here, we must do it ourselves. */ | |
6842 | addend <<= 2; | |
31367b81 | 6843 | |
4f2860ca MM |
6844 | r_symndx = ELF32_R_SYM (rel->r_info); |
6845 | sym = local_syms + r_symndx; | |
6846 | if (ELF_ST_TYPE (sym->st_info) == STT_SECTION) | |
6847 | /* Adjust the addend appropriately. */ | |
6848 | addend += local_sections[r_symndx]->output_offset; | |
be3ccd9c | 6849 | |
f1a5f37e MM |
6850 | /* If the relocation is for a R_MIPS_HI16 or R_MIPS_GOT16, |
6851 | then we only want to write out the high-order 16 bits. | |
6852 | The subsequent R_MIPS_LO16 will handle the low-order bits. */ | |
bb2d6cd7 GK |
6853 | if (r_type == R_MIPS_HI16 || r_type == R_MIPS_GOT16 |
6854 | || r_type == R_MIPS_GNU_REL_HI16) | |
23b255aa | 6855 | addend = mips_elf_high (addend); |
5a44662b MM |
6856 | /* If the relocation is for an R_MIPS_26 relocation, then |
6857 | the two low-order bits are not stored in the object file; | |
6858 | they are implicitly zero. */ | |
bb2d6cd7 GK |
6859 | else if (r_type == R_MIPS_26 || r_type == R_MIPS16_26 |
6860 | || r_type == R_MIPS_GNU_REL16_S2) | |
5a44662b | 6861 | addend >>= 2; |
f1a5f37e | 6862 | |
31367b81 MM |
6863 | if (rela_relocation_p) |
6864 | /* If this is a RELA relocation, just update the addend. | |
bb2d6cd7 | 6865 | We have to cast away constness for REL. */ |
31367b81 MM |
6866 | rel->r_addend = addend; |
6867 | else | |
6868 | { | |
6869 | /* Otherwise, we have to write the value back out. Note | |
6870 | that we use the source mask, rather than the | |
6871 | destination mask because the place to which we are | |
6872 | writing will be source of the addend in the final | |
6873 | link. */ | |
6874 | addend &= howto->src_mask; | |
7893e6a2 GK |
6875 | |
6876 | if (r_type == R_MIPS_64 && !ABI_64_P (output_bfd)) | |
6877 | /* See the comment above about using R_MIPS_64 in the 32-bit | |
6878 | ABI. Here, we need to update the addend. It would be | |
6879 | possible to get away with just using the R_MIPS_32 reloc | |
6880 | but for endianness. */ | |
6881 | { | |
6882 | bfd_vma sign_bits; | |
6883 | bfd_vma low_bits; | |
6884 | bfd_vma high_bits; | |
be3ccd9c | 6885 | |
fc633e5b AM |
6886 | if (addend & ((bfd_vma) 1 << 31)) |
6887 | sign_bits = ((bfd_vma) 1 << 32) - 1; | |
7893e6a2 GK |
6888 | else |
6889 | sign_bits = 0; | |
be3ccd9c | 6890 | |
7893e6a2 GK |
6891 | /* If we don't know that we have a 64-bit type, |
6892 | do two separate stores. */ | |
6893 | if (bfd_big_endian (input_bfd)) | |
6894 | { | |
6895 | /* Store the sign-bits (which are most significant) | |
6896 | first. */ | |
6897 | low_bits = sign_bits; | |
6898 | high_bits = addend; | |
6899 | } | |
6900 | else | |
6901 | { | |
6902 | low_bits = addend; | |
6903 | high_bits = sign_bits; | |
6904 | } | |
be3ccd9c | 6905 | bfd_put_32 (input_bfd, low_bits, |
7893e6a2 | 6906 | contents + rel->r_offset); |
be3ccd9c | 6907 | bfd_put_32 (input_bfd, high_bits, |
7893e6a2 GK |
6908 | contents + rel->r_offset + 4); |
6909 | continue; | |
6910 | } | |
6911 | ||
31367b81 | 6912 | if (!mips_elf_perform_relocation (info, howto, rel, addend, |
be3ccd9c | 6913 | input_bfd, input_section, |
31367b81 MM |
6914 | contents, false)) |
6915 | return false; | |
6916 | } | |
6917 | ||
6918 | /* Go on to the next relocation. */ | |
6919 | continue; | |
6920 | } | |
6921 | ||
7403cb63 MM |
6922 | /* In the N32 and 64-bit ABIs there may be multiple consecutive |
6923 | relocations for the same offset. In that case we are | |
6924 | supposed to treat the output of each relocation as the addend | |
6925 | for the next. */ | |
be3ccd9c | 6926 | if (rel + 1 < relend |
103186c6 | 6927 | && rel->r_offset == rel[1].r_offset |
b89db8f2 | 6928 | && ELF32_R_TYPE (rel[1].r_info) != R_MIPS_NONE) |
7403cb63 MM |
6929 | use_saved_addend_p = true; |
6930 | else | |
6931 | use_saved_addend_p = false; | |
6932 | ||
6933 | /* Figure out what value we are supposed to relocate. */ | |
be3ccd9c | 6934 | switch (mips_elf_calculate_relocation (output_bfd, |
7403cb63 MM |
6935 | input_bfd, |
6936 | input_section, | |
6937 | info, | |
6938 | rel, | |
6939 | addend, | |
6940 | howto, | |
7403cb63 MM |
6941 | local_syms, |
6942 | local_sections, | |
6943 | &value, | |
197b9ca0 MM |
6944 | &name, |
6945 | &require_jalx)) | |
7403cb63 MM |
6946 | { |
6947 | case bfd_reloc_continue: | |
6948 | /* There's nothing to do. */ | |
6949 | continue; | |
252b5132 | 6950 | |
7403cb63 | 6951 | case bfd_reloc_undefined: |
6387d602 | 6952 | /* mips_elf_calculate_relocation already called the |
bb2d6cd7 | 6953 | undefined_symbol callback. There's no real point in |
97287574 MM |
6954 | trying to perform the relocation at this point, so we |
6955 | just skip ahead to the next relocation. */ | |
6956 | continue; | |
252b5132 | 6957 | |
7403cb63 MM |
6958 | case bfd_reloc_notsupported: |
6959 | abort (); | |
6960 | break; | |
252b5132 | 6961 | |
7403cb63 MM |
6962 | case bfd_reloc_overflow: |
6963 | if (use_saved_addend_p) | |
6964 | /* Ignore overflow until we reach the last relocation for | |
6965 | a given location. */ | |
6966 | ; | |
6387d602 ILT |
6967 | else |
6968 | { | |
6969 | BFD_ASSERT (name != NULL); | |
6970 | if (! ((*info->callbacks->reloc_overflow) | |
6971 | (info, name, howto->name, (bfd_vma) 0, | |
6972 | input_bfd, input_section, rel->r_offset))) | |
6973 | return false; | |
6974 | } | |
7403cb63 | 6975 | break; |
252b5132 | 6976 | |
7403cb63 MM |
6977 | case bfd_reloc_ok: |
6978 | break; | |
6979 | ||
6980 | default: | |
6981 | abort (); | |
6982 | break; | |
252b5132 RH |
6983 | } |
6984 | ||
7403cb63 MM |
6985 | /* If we've got another relocation for the address, keep going |
6986 | until we reach the last one. */ | |
6987 | if (use_saved_addend_p) | |
252b5132 | 6988 | { |
7403cb63 MM |
6989 | addend = value; |
6990 | continue; | |
252b5132 | 6991 | } |
7403cb63 | 6992 | |
31367b81 | 6993 | if (r_type == R_MIPS_64 && !ABI_64_P (output_bfd)) |
a3c7651d MM |
6994 | /* See the comment above about using R_MIPS_64 in the 32-bit |
6995 | ABI. Until now, we've been using the HOWTO for R_MIPS_32; | |
6996 | that calculated the right value. Now, however, we | |
6997 | sign-extend the 32-bit result to 64-bits, and store it as a | |
6998 | 64-bit value. We are especially generous here in that we | |
6999 | go to extreme lengths to support this usage on systems with | |
7000 | only a 32-bit VMA. */ | |
7001 | { | |
a3c7651d MM |
7002 | bfd_vma sign_bits; |
7003 | bfd_vma low_bits; | |
7004 | bfd_vma high_bits; | |
7005 | ||
fc633e5b AM |
7006 | if (value & ((bfd_vma) 1 << 31)) |
7007 | sign_bits = ((bfd_vma) 1 << 32) - 1; | |
a3c7651d MM |
7008 | else |
7009 | sign_bits = 0; | |
7010 | ||
7893e6a2 GK |
7011 | /* If we don't know that we have a 64-bit type, |
7012 | do two separate stores. */ | |
a3c7651d MM |
7013 | if (bfd_big_endian (input_bfd)) |
7014 | { | |
0af99795 GK |
7015 | /* Undo what we did above. */ |
7016 | rel->r_offset -= 4; | |
a3c7651d MM |
7017 | /* Store the sign-bits (which are most significant) |
7018 | first. */ | |
7019 | low_bits = sign_bits; | |
7020 | high_bits = value; | |
7021 | } | |
7022 | else | |
7023 | { | |
7024 | low_bits = value; | |
7025 | high_bits = sign_bits; | |
7026 | } | |
be3ccd9c | 7027 | bfd_put_32 (input_bfd, low_bits, |
a3c7651d | 7028 | contents + rel->r_offset); |
be3ccd9c | 7029 | bfd_put_32 (input_bfd, high_bits, |
a3c7651d MM |
7030 | contents + rel->r_offset + 4); |
7031 | continue; | |
a3c7651d MM |
7032 | } |
7033 | ||
7403cb63 | 7034 | /* Actually perform the relocation. */ |
be3ccd9c | 7035 | if (!mips_elf_perform_relocation (info, howto, rel, value, input_bfd, |
197b9ca0 MM |
7036 | input_section, contents, |
7037 | require_jalx)) | |
7038 | return false; | |
252b5132 RH |
7039 | } |
7040 | ||
7041 | return true; | |
7042 | } | |
7043 | ||
7044 | /* This hook function is called before the linker writes out a global | |
7045 | symbol. We mark symbols as small common if appropriate. This is | |
7046 | also where we undo the increment of the value for a mips16 symbol. */ | |
7047 | ||
103186c6 MM |
7048 | boolean |
7049 | _bfd_mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec) | |
5f771d47 ILT |
7050 | bfd *abfd ATTRIBUTE_UNUSED; |
7051 | struct bfd_link_info *info ATTRIBUTE_UNUSED; | |
7052 | const char *name ATTRIBUTE_UNUSED; | |
252b5132 RH |
7053 | Elf_Internal_Sym *sym; |
7054 | asection *input_sec; | |
7055 | { | |
7056 | /* If we see a common symbol, which implies a relocatable link, then | |
7057 | if a symbol was small common in an input file, mark it as small | |
7058 | common in the output file. */ | |
7059 | if (sym->st_shndx == SHN_COMMON | |
7060 | && strcmp (input_sec->name, ".scommon") == 0) | |
7061 | sym->st_shndx = SHN_MIPS_SCOMMON; | |
7062 | ||
7063 | if (sym->st_other == STO_MIPS16 | |
7064 | && (sym->st_value & 1) != 0) | |
7065 | --sym->st_value; | |
7066 | ||
7067 | return true; | |
7068 | } | |
7069 | \f | |
7070 | /* Functions for the dynamic linker. */ | |
7071 | ||
7072 | /* The name of the dynamic interpreter. This is put in the .interp | |
7073 | section. */ | |
7074 | ||
103186c6 MM |
7075 | #define ELF_DYNAMIC_INTERPRETER(abfd) \ |
7076 | (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \ | |
7077 | : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \ | |
7078 | : "/usr/lib/libc.so.1") | |
252b5132 RH |
7079 | |
7080 | /* Create dynamic sections when linking against a dynamic object. */ | |
7081 | ||
103186c6 MM |
7082 | boolean |
7083 | _bfd_mips_elf_create_dynamic_sections (abfd, info) | |
252b5132 RH |
7084 | bfd *abfd; |
7085 | struct bfd_link_info *info; | |
7086 | { | |
7087 | struct elf_link_hash_entry *h; | |
7088 | flagword flags; | |
7089 | register asection *s; | |
7090 | const char * const *namep; | |
7091 | ||
7092 | flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | |
7093 | | SEC_LINKER_CREATED | SEC_READONLY); | |
7094 | ||
7095 | /* Mips ABI requests the .dynamic section to be read only. */ | |
7096 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
7097 | if (s != NULL) | |
7098 | { | |
7099 | if (! bfd_set_section_flags (abfd, s, flags)) | |
7100 | return false; | |
7101 | } | |
7102 | ||
7103 | /* We need to create .got section. */ | |
7104 | if (! mips_elf_create_got_section (abfd, info)) | |
7105 | return false; | |
7106 | ||
c6142e5d MM |
7107 | /* Create the .msym section on IRIX6. It is used by the dynamic |
7108 | linker to speed up dynamic relocations, and to avoid computing | |
7109 | the ELF hash for symbols. */ | |
7110 | if (IRIX_COMPAT (abfd) == ict_irix6 | |
7111 | && !mips_elf_create_msym_section (abfd)) | |
7112 | return false; | |
be3ccd9c | 7113 | |
252b5132 | 7114 | /* Create .stub section. */ |
be3ccd9c | 7115 | if (bfd_get_section_by_name (abfd, |
7403cb63 | 7116 | MIPS_ELF_STUB_SECTION_NAME (abfd)) == NULL) |
252b5132 | 7117 | { |
7403cb63 | 7118 | s = bfd_make_section (abfd, MIPS_ELF_STUB_SECTION_NAME (abfd)); |
252b5132 | 7119 | if (s == NULL |
7403cb63 | 7120 | || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE) |
103186c6 MM |
7121 | || ! bfd_set_section_alignment (abfd, s, |
7122 | MIPS_ELF_LOG_FILE_ALIGN (abfd))) | |
252b5132 RH |
7123 | return false; |
7124 | } | |
7125 | ||
31a9bdd9 | 7126 | if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none) |
252b5132 RH |
7127 | && !info->shared |
7128 | && bfd_get_section_by_name (abfd, ".rld_map") == NULL) | |
7129 | { | |
7130 | s = bfd_make_section (abfd, ".rld_map"); | |
7131 | if (s == NULL | |
7132 | || ! bfd_set_section_flags (abfd, s, flags & ~SEC_READONLY) | |
103186c6 MM |
7133 | || ! bfd_set_section_alignment (abfd, s, |
7134 | MIPS_ELF_LOG_FILE_ALIGN (abfd))) | |
252b5132 RH |
7135 | return false; |
7136 | } | |
7137 | ||
303f629d MM |
7138 | /* On IRIX5, we adjust add some additional symbols and change the |
7139 | alignments of several sections. There is no ABI documentation | |
7140 | indicating that this is necessary on IRIX6, nor any evidence that | |
7141 | the linker takes such action. */ | |
7142 | if (IRIX_COMPAT (abfd) == ict_irix5) | |
252b5132 RH |
7143 | { |
7144 | for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++) | |
7145 | { | |
7146 | h = NULL; | |
7147 | if (! (_bfd_generic_link_add_one_symbol | |
7148 | (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, | |
7149 | (bfd_vma) 0, (const char *) NULL, false, | |
7150 | get_elf_backend_data (abfd)->collect, | |
7151 | (struct bfd_link_hash_entry **) &h))) | |
7152 | return false; | |
be3ccd9c | 7153 | h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF; |
252b5132 RH |
7154 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; |
7155 | h->type = STT_SECTION; | |
7156 | ||
7157 | if (! bfd_elf32_link_record_dynamic_symbol (info, h)) | |
7158 | return false; | |
7159 | } | |
7160 | ||
7161 | /* We need to create a .compact_rel section. */ | |
f7cb7d68 | 7162 | if (SGI_COMPAT (abfd)) |
be3ccd9c KH |
7163 | { |
7164 | if (!mips_elf_create_compact_rel_section (abfd, info)) | |
f7cb7d68 | 7165 | return false; |
be3ccd9c | 7166 | } |
252b5132 RH |
7167 | |
7168 | /* Change aligments of some sections. */ | |
7169 | s = bfd_get_section_by_name (abfd, ".hash"); | |
7170 | if (s != NULL) | |
7171 | bfd_set_section_alignment (abfd, s, 4); | |
7172 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
7173 | if (s != NULL) | |
7174 | bfd_set_section_alignment (abfd, s, 4); | |
7175 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
7176 | if (s != NULL) | |
7177 | bfd_set_section_alignment (abfd, s, 4); | |
7178 | s = bfd_get_section_by_name (abfd, ".reginfo"); | |
7179 | if (s != NULL) | |
7180 | bfd_set_section_alignment (abfd, s, 4); | |
7181 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
7182 | if (s != NULL) | |
7183 | bfd_set_section_alignment (abfd, s, 4); | |
7184 | } | |
7185 | ||
7186 | if (!info->shared) | |
7187 | { | |
7188 | h = NULL; | |
f7cb7d68 | 7189 | if (SGI_COMPAT (abfd)) |
be3ccd9c KH |
7190 | { |
7191 | if (!(_bfd_generic_link_add_one_symbol | |
7192 | (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr, | |
7193 | (bfd_vma) 0, (const char *) NULL, false, | |
7194 | get_elf_backend_data (abfd)->collect, | |
7195 | (struct bfd_link_hash_entry **) &h))) | |
f7cb7d68 | 7196 | return false; |
be3ccd9c | 7197 | } |
f7cb7d68 | 7198 | else |
be3ccd9c KH |
7199 | { |
7200 | /* For normal mips it is _DYNAMIC_LINKING. */ | |
7201 | if (!(_bfd_generic_link_add_one_symbol | |
7202 | (info, abfd, "_DYNAMIC_LINKING", BSF_GLOBAL, | |
7203 | bfd_abs_section_ptr, (bfd_vma) 0, (const char *) NULL, false, | |
7204 | get_elf_backend_data (abfd)->collect, | |
7205 | (struct bfd_link_hash_entry **) &h))) | |
7206 | return false; | |
7207 | } | |
7208 | h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF; | |
252b5132 RH |
7209 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; |
7210 | h->type = STT_SECTION; | |
7211 | ||
7212 | if (! bfd_elf32_link_record_dynamic_symbol (info, h)) | |
7213 | return false; | |
7214 | ||
7215 | if (! mips_elf_hash_table (info)->use_rld_obj_head) | |
7216 | { | |
7217 | /* __rld_map is a four byte word located in the .data section | |
7218 | and is filled in by the rtld to contain a pointer to | |
7219 | the _r_debug structure. Its symbol value will be set in | |
7220 | mips_elf_finish_dynamic_symbol. */ | |
7221 | s = bfd_get_section_by_name (abfd, ".rld_map"); | |
7222 | BFD_ASSERT (s != NULL); | |
7223 | ||
7224 | h = NULL; | |
be3ccd9c KH |
7225 | if (SGI_COMPAT (abfd)) |
7226 | { | |
7227 | if (!(_bfd_generic_link_add_one_symbol | |
7228 | (info, abfd, "__rld_map", BSF_GLOBAL, s, | |
7229 | (bfd_vma) 0, (const char *) NULL, false, | |
7230 | get_elf_backend_data (abfd)->collect, | |
7231 | (struct bfd_link_hash_entry **) &h))) | |
7232 | return false; | |
7233 | } | |
7234 | else | |
7235 | { | |
7236 | /* For normal mips the symbol is __RLD_MAP. */ | |
7237 | if (!(_bfd_generic_link_add_one_symbol | |
7238 | (info, abfd, "__RLD_MAP", BSF_GLOBAL, s, | |
7239 | (bfd_vma) 0, (const char *) NULL, false, | |
7240 | get_elf_backend_data (abfd)->collect, | |
7241 | (struct bfd_link_hash_entry **) &h))) | |
7242 | return false; | |
7243 | } | |
7244 | h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF; | |
252b5132 RH |
7245 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; |
7246 | h->type = STT_OBJECT; | |
7247 | ||
7248 | if (! bfd_elf32_link_record_dynamic_symbol (info, h)) | |
7249 | return false; | |
7250 | } | |
7251 | } | |
7252 | ||
7253 | return true; | |
7254 | } | |
7255 | ||
7256 | /* Create the .compact_rel section. */ | |
7257 | ||
7258 | static boolean | |
7259 | mips_elf_create_compact_rel_section (abfd, info) | |
7260 | bfd *abfd; | |
5f771d47 | 7261 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
7262 | { |
7263 | flagword flags; | |
7264 | register asection *s; | |
7265 | ||
7266 | if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL) | |
7267 | { | |
7268 | flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED | |
7269 | | SEC_READONLY); | |
7270 | ||
7271 | s = bfd_make_section (abfd, ".compact_rel"); | |
7272 | if (s == NULL | |
7273 | || ! bfd_set_section_flags (abfd, s, flags) | |
103186c6 MM |
7274 | || ! bfd_set_section_alignment (abfd, s, |
7275 | MIPS_ELF_LOG_FILE_ALIGN (abfd))) | |
252b5132 RH |
7276 | return false; |
7277 | ||
7278 | s->_raw_size = sizeof (Elf32_External_compact_rel); | |
7279 | } | |
7280 | ||
7281 | return true; | |
7282 | } | |
7283 | ||
be3ccd9c | 7284 | /* Create the .got section to hold the global offset table. */ |
252b5132 RH |
7285 | |
7286 | static boolean | |
7287 | mips_elf_create_got_section (abfd, info) | |
7288 | bfd *abfd; | |
7289 | struct bfd_link_info *info; | |
7290 | { | |
7291 | flagword flags; | |
7292 | register asection *s; | |
7293 | struct elf_link_hash_entry *h; | |
7294 | struct mips_got_info *g; | |
7295 | ||
7296 | /* This function may be called more than once. */ | |
103186c6 | 7297 | if (mips_elf_got_section (abfd)) |
252b5132 RH |
7298 | return true; |
7299 | ||
7300 | flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | |
7301 | | SEC_LINKER_CREATED); | |
7302 | ||
7303 | s = bfd_make_section (abfd, ".got"); | |
7304 | if (s == NULL | |
7305 | || ! bfd_set_section_flags (abfd, s, flags) | |
7306 | || ! bfd_set_section_alignment (abfd, s, 4)) | |
7307 | return false; | |
7308 | ||
7309 | /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the | |
7310 | linker script because we don't want to define the symbol if we | |
7311 | are not creating a global offset table. */ | |
7312 | h = NULL; | |
7313 | if (! (_bfd_generic_link_add_one_symbol | |
7314 | (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s, | |
7315 | (bfd_vma) 0, (const char *) NULL, false, | |
7316 | get_elf_backend_data (abfd)->collect, | |
7317 | (struct bfd_link_hash_entry **) &h))) | |
7318 | return false; | |
be3ccd9c | 7319 | h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF; |
252b5132 RH |
7320 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; |
7321 | h->type = STT_OBJECT; | |
7322 | ||
7323 | if (info->shared | |
7324 | && ! bfd_elf32_link_record_dynamic_symbol (info, h)) | |
7325 | return false; | |
7326 | ||
7327 | /* The first several global offset table entries are reserved. */ | |
103186c6 | 7328 | s->_raw_size = MIPS_RESERVED_GOTNO * MIPS_ELF_GOT_SIZE (abfd); |
252b5132 RH |
7329 | |
7330 | g = (struct mips_got_info *) bfd_alloc (abfd, | |
7331 | sizeof (struct mips_got_info)); | |
7332 | if (g == NULL) | |
7333 | return false; | |
7403cb63 | 7334 | g->global_gotsym = NULL; |
252b5132 RH |
7335 | g->local_gotno = MIPS_RESERVED_GOTNO; |
7336 | g->assigned_gotno = MIPS_RESERVED_GOTNO; | |
7337 | if (elf_section_data (s) == NULL) | |
7338 | { | |
7339 | s->used_by_bfd = | |
7340 | (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data)); | |
7341 | if (elf_section_data (s) == NULL) | |
7342 | return false; | |
7343 | } | |
7344 | elf_section_data (s)->tdata = (PTR) g; | |
be3ccd9c | 7345 | elf_section_data (s)->this_hdr.sh_flags |
7403cb63 | 7346 | |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL; |
252b5132 RH |
7347 | |
7348 | return true; | |
7349 | } | |
7350 | ||
c6142e5d MM |
7351 | /* Returns the .msym section for ABFD, creating it if it does not |
7352 | already exist. Returns NULL to indicate error. */ | |
7353 | ||
7354 | static asection * | |
7355 | mips_elf_create_msym_section (abfd) | |
7356 | bfd *abfd; | |
7357 | { | |
7358 | asection *s; | |
7359 | ||
7360 | s = bfd_get_section_by_name (abfd, MIPS_ELF_MSYM_SECTION_NAME (abfd)); | |
be3ccd9c | 7361 | if (!s) |
c6142e5d MM |
7362 | { |
7363 | s = bfd_make_section (abfd, MIPS_ELF_MSYM_SECTION_NAME (abfd)); | |
7364 | if (!s | |
be3ccd9c | 7365 | || !bfd_set_section_flags (abfd, s, |
c6142e5d MM |
7366 | SEC_ALLOC |
7367 | | SEC_LOAD | |
7368 | | SEC_HAS_CONTENTS | |
be3ccd9c | 7369 | | SEC_LINKER_CREATED |
c6142e5d | 7370 | | SEC_READONLY) |
103186c6 MM |
7371 | || !bfd_set_section_alignment (abfd, s, |
7372 | MIPS_ELF_LOG_FILE_ALIGN (abfd))) | |
c6142e5d MM |
7373 | return NULL; |
7374 | } | |
7375 | ||
7376 | return s; | |
7377 | } | |
7378 | ||
103186c6 MM |
7379 | /* Add room for N relocations to the .rel.dyn section in ABFD. */ |
7380 | ||
7381 | static void | |
7382 | mips_elf_allocate_dynamic_relocations (abfd, n) | |
7383 | bfd *abfd; | |
7384 | unsigned int n; | |
7385 | { | |
7386 | asection *s; | |
7387 | ||
7388 | s = bfd_get_section_by_name (abfd, MIPS_ELF_REL_DYN_SECTION_NAME (abfd)); | |
7389 | BFD_ASSERT (s != NULL); | |
be3ccd9c | 7390 | |
103186c6 MM |
7391 | if (s->_raw_size == 0) |
7392 | { | |
be3ccd9c | 7393 | /* Make room for a null element. */ |
103186c6 MM |
7394 | s->_raw_size += MIPS_ELF_REL_SIZE (abfd); |
7395 | ++s->reloc_count; | |
7396 | } | |
7397 | s->_raw_size += n * MIPS_ELF_REL_SIZE (abfd); | |
7398 | } | |
7399 | ||
252b5132 RH |
7400 | /* Look through the relocs for a section during the first phase, and |
7401 | allocate space in the global offset table. */ | |
7402 | ||
103186c6 MM |
7403 | boolean |
7404 | _bfd_mips_elf_check_relocs (abfd, info, sec, relocs) | |
252b5132 RH |
7405 | bfd *abfd; |
7406 | struct bfd_link_info *info; | |
7407 | asection *sec; | |
7408 | const Elf_Internal_Rela *relocs; | |
7409 | { | |
7410 | const char *name; | |
7411 | bfd *dynobj; | |
7412 | Elf_Internal_Shdr *symtab_hdr; | |
7413 | struct elf_link_hash_entry **sym_hashes; | |
7414 | struct mips_got_info *g; | |
7415 | size_t extsymoff; | |
7416 | const Elf_Internal_Rela *rel; | |
7417 | const Elf_Internal_Rela *rel_end; | |
7418 | asection *sgot; | |
7419 | asection *sreloc; | |
103186c6 | 7420 | struct elf_backend_data *bed; |
252b5132 RH |
7421 | |
7422 | if (info->relocateable) | |
7423 | return true; | |
7424 | ||
7425 | dynobj = elf_hash_table (info)->dynobj; | |
7426 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
7427 | sym_hashes = elf_sym_hashes (abfd); | |
7428 | extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info; | |
7429 | ||
7430 | /* Check for the mips16 stub sections. */ | |
7431 | ||
7432 | name = bfd_get_section_name (abfd, sec); | |
7433 | if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0) | |
7434 | { | |
7435 | unsigned long r_symndx; | |
7436 | ||
7437 | /* Look at the relocation information to figure out which symbol | |
7438 | this is for. */ | |
7439 | ||
7440 | r_symndx = ELF32_R_SYM (relocs->r_info); | |
7441 | ||
7442 | if (r_symndx < extsymoff | |
7443 | || sym_hashes[r_symndx - extsymoff] == NULL) | |
7444 | { | |
7445 | asection *o; | |
7446 | ||
7447 | /* This stub is for a local symbol. This stub will only be | |
7448 | needed if there is some relocation in this BFD, other | |
7449 | than a 16 bit function call, which refers to this symbol. */ | |
7450 | for (o = abfd->sections; o != NULL; o = o->next) | |
7451 | { | |
7452 | Elf_Internal_Rela *sec_relocs; | |
7453 | const Elf_Internal_Rela *r, *rend; | |
7454 | ||
7455 | /* We can ignore stub sections when looking for relocs. */ | |
7456 | if ((o->flags & SEC_RELOC) == 0 | |
7457 | || o->reloc_count == 0 | |
7458 | || strncmp (bfd_get_section_name (abfd, o), FN_STUB, | |
7459 | sizeof FN_STUB - 1) == 0 | |
7460 | || strncmp (bfd_get_section_name (abfd, o), CALL_STUB, | |
7461 | sizeof CALL_STUB - 1) == 0 | |
7462 | || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB, | |
7463 | sizeof CALL_FP_STUB - 1) == 0) | |
7464 | continue; | |
7465 | ||
7466 | sec_relocs = (_bfd_elf32_link_read_relocs | |
7467 | (abfd, o, (PTR) NULL, | |
7468 | (Elf_Internal_Rela *) NULL, | |
7469 | info->keep_memory)); | |
7470 | if (sec_relocs == NULL) | |
7471 | return false; | |
7472 | ||
7473 | rend = sec_relocs + o->reloc_count; | |
7474 | for (r = sec_relocs; r < rend; r++) | |
7475 | if (ELF32_R_SYM (r->r_info) == r_symndx | |
7476 | && ELF32_R_TYPE (r->r_info) != R_MIPS16_26) | |
7477 | break; | |
7478 | ||
7479 | if (! info->keep_memory) | |
7480 | free (sec_relocs); | |
7481 | ||
7482 | if (r < rend) | |
7483 | break; | |
7484 | } | |
7485 | ||
7486 | if (o == NULL) | |
7487 | { | |
7488 | /* There is no non-call reloc for this stub, so we do | |
7489 | not need it. Since this function is called before | |
7490 | the linker maps input sections to output sections, we | |
7491 | can easily discard it by setting the SEC_EXCLUDE | |
7492 | flag. */ | |
7493 | sec->flags |= SEC_EXCLUDE; | |
7494 | return true; | |
7495 | } | |
7496 | ||
7497 | /* Record this stub in an array of local symbol stubs for | |
be3ccd9c | 7498 | this BFD. */ |
252b5132 RH |
7499 | if (elf_tdata (abfd)->local_stubs == NULL) |
7500 | { | |
7501 | unsigned long symcount; | |
7502 | asection **n; | |
7503 | ||
7504 | if (elf_bad_symtab (abfd)) | |
103186c6 | 7505 | symcount = symtab_hdr->sh_size / symtab_hdr->sh_entsize; |
252b5132 RH |
7506 | else |
7507 | symcount = symtab_hdr->sh_info; | |
7508 | n = (asection **) bfd_zalloc (abfd, | |
7509 | symcount * sizeof (asection *)); | |
7510 | if (n == NULL) | |
7511 | return false; | |
7512 | elf_tdata (abfd)->local_stubs = n; | |
7513 | } | |
7514 | ||
7515 | elf_tdata (abfd)->local_stubs[r_symndx] = sec; | |
7516 | ||
7517 | /* We don't need to set mips16_stubs_seen in this case. | |
7518 | That flag is used to see whether we need to look through | |
7519 | the global symbol table for stubs. We don't need to set | |
7520 | it here, because we just have a local stub. */ | |
7521 | } | |
7522 | else | |
7523 | { | |
7524 | struct mips_elf_link_hash_entry *h; | |
7525 | ||
7526 | h = ((struct mips_elf_link_hash_entry *) | |
7527 | sym_hashes[r_symndx - extsymoff]); | |
7528 | ||
7529 | /* H is the symbol this stub is for. */ | |
7530 | ||
7531 | h->fn_stub = sec; | |
7532 | mips_elf_hash_table (info)->mips16_stubs_seen = true; | |
7533 | } | |
7534 | } | |
7535 | else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0 | |
7536 | || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0) | |
7537 | { | |
7538 | unsigned long r_symndx; | |
7539 | struct mips_elf_link_hash_entry *h; | |
7540 | asection **loc; | |
7541 | ||
7542 | /* Look at the relocation information to figure out which symbol | |
7543 | this is for. */ | |
7544 | ||
7545 | r_symndx = ELF32_R_SYM (relocs->r_info); | |
7546 | ||
7547 | if (r_symndx < extsymoff | |
7548 | || sym_hashes[r_symndx - extsymoff] == NULL) | |
7549 | { | |
7550 | /* This stub was actually built for a static symbol defined | |
7551 | in the same file. We assume that all static symbols in | |
7552 | mips16 code are themselves mips16, so we can simply | |
7553 | discard this stub. Since this function is called before | |
7554 | the linker maps input sections to output sections, we can | |
7555 | easily discard it by setting the SEC_EXCLUDE flag. */ | |
7556 | sec->flags |= SEC_EXCLUDE; | |
7557 | return true; | |
7558 | } | |
7559 | ||
7560 | h = ((struct mips_elf_link_hash_entry *) | |
7561 | sym_hashes[r_symndx - extsymoff]); | |
7562 | ||
7563 | /* H is the symbol this stub is for. */ | |
7564 | ||
7565 | if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0) | |
7566 | loc = &h->call_fp_stub; | |
7567 | else | |
7568 | loc = &h->call_stub; | |
7569 | ||
7570 | /* If we already have an appropriate stub for this function, we | |
7571 | don't need another one, so we can discard this one. Since | |
7572 | this function is called before the linker maps input sections | |
7573 | to output sections, we can easily discard it by setting the | |
7574 | SEC_EXCLUDE flag. We can also discard this section if we | |
7575 | happen to already know that this is a mips16 function; it is | |
7576 | not necessary to check this here, as it is checked later, but | |
7577 | it is slightly faster to check now. */ | |
7578 | if (*loc != NULL || h->root.other == STO_MIPS16) | |
7579 | { | |
7580 | sec->flags |= SEC_EXCLUDE; | |
7581 | return true; | |
7582 | } | |
7583 | ||
7584 | *loc = sec; | |
7585 | mips_elf_hash_table (info)->mips16_stubs_seen = true; | |
7586 | } | |
7587 | ||
7588 | if (dynobj == NULL) | |
7589 | { | |
7590 | sgot = NULL; | |
7591 | g = NULL; | |
7592 | } | |
7593 | else | |
7594 | { | |
103186c6 | 7595 | sgot = mips_elf_got_section (dynobj); |
252b5132 RH |
7596 | if (sgot == NULL) |
7597 | g = NULL; | |
7598 | else | |
7599 | { | |
7600 | BFD_ASSERT (elf_section_data (sgot) != NULL); | |
7601 | g = (struct mips_got_info *) elf_section_data (sgot)->tdata; | |
7602 | BFD_ASSERT (g != NULL); | |
7603 | } | |
7604 | } | |
7605 | ||
7606 | sreloc = NULL; | |
103186c6 MM |
7607 | bed = get_elf_backend_data (abfd); |
7608 | rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
7609 | for (rel = relocs; rel < rel_end; ++rel) | |
252b5132 RH |
7610 | { |
7611 | unsigned long r_symndx; | |
7403cb63 | 7612 | int r_type; |
252b5132 RH |
7613 | struct elf_link_hash_entry *h; |
7614 | ||
7615 | r_symndx = ELF32_R_SYM (rel->r_info); | |
7403cb63 | 7616 | r_type = ELF32_R_TYPE (rel->r_info); |
252b5132 RH |
7617 | |
7618 | if (r_symndx < extsymoff) | |
7619 | h = NULL; | |
7a3120d9 NC |
7620 | else if (r_symndx >= extsymoff + (symtab_hdr->sh_size / symtab_hdr->sh_entsize)) |
7621 | { | |
7622 | (*_bfd_error_handler) | |
7623 | (_("Malformed reloc detected for section %s"), name); | |
7624 | bfd_set_error (bfd_error_bad_value); | |
7625 | return false; | |
7626 | } | |
252b5132 RH |
7627 | else |
7628 | { | |
7629 | h = sym_hashes[r_symndx - extsymoff]; | |
7630 | ||
7631 | /* This may be an indirect symbol created because of a version. */ | |
7632 | if (h != NULL) | |
7633 | { | |
7634 | while (h->root.type == bfd_link_hash_indirect) | |
7635 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
7636 | } | |
7637 | } | |
7638 | ||
7639 | /* Some relocs require a global offset table. */ | |
7640 | if (dynobj == NULL || sgot == NULL) | |
7641 | { | |
7403cb63 | 7642 | switch (r_type) |
252b5132 RH |
7643 | { |
7644 | case R_MIPS_GOT16: | |
7645 | case R_MIPS_CALL16: | |
7646 | case R_MIPS_CALL_HI16: | |
7647 | case R_MIPS_CALL_LO16: | |
7648 | case R_MIPS_GOT_HI16: | |
7649 | case R_MIPS_GOT_LO16: | |
435394bf MM |
7650 | case R_MIPS_GOT_PAGE: |
7651 | case R_MIPS_GOT_OFST: | |
7652 | case R_MIPS_GOT_DISP: | |
252b5132 RH |
7653 | if (dynobj == NULL) |
7654 | elf_hash_table (info)->dynobj = dynobj = abfd; | |
7655 | if (! mips_elf_create_got_section (dynobj, info)) | |
7656 | return false; | |
7403cb63 | 7657 | g = mips_elf_got_info (dynobj, &sgot); |
252b5132 RH |
7658 | break; |
7659 | ||
7660 | case R_MIPS_32: | |
7661 | case R_MIPS_REL32: | |
a3c7651d | 7662 | case R_MIPS_64: |
252b5132 RH |
7663 | if (dynobj == NULL |
7664 | && (info->shared || h != NULL) | |
7665 | && (sec->flags & SEC_ALLOC) != 0) | |
7666 | elf_hash_table (info)->dynobj = dynobj = abfd; | |
7667 | break; | |
7668 | ||
7669 | default: | |
7670 | break; | |
7671 | } | |
7672 | } | |
7673 | ||
7403cb63 MM |
7674 | if (!h && (r_type == R_MIPS_CALL_LO16 |
7675 | || r_type == R_MIPS_GOT_LO16 | |
9458945f | 7676 | || r_type == R_MIPS_GOT_DISP)) |
252b5132 | 7677 | { |
7403cb63 | 7678 | /* We may need a local GOT entry for this relocation. We |
97287574 MM |
7679 | don't count R_MIPS_GOT_PAGE because we can estimate the |
7680 | maximum number of pages needed by looking at the size of | |
9458945f MM |
7681 | the segment. Similar comments apply to R_MIPS_GOT16. We |
7682 | don't count R_MIPS_GOT_HI16, or R_MIPS_CALL_HI16 because | |
7683 | these are always followed by an R_MIPS_GOT_LO16 or | |
7684 | R_MIPS_CALL_LO16. | |
7403cb63 MM |
7685 | |
7686 | This estimation is very conservative since we can merge | |
7687 | duplicate entries in the GOT. In order to be less | |
7688 | conservative, we could actually build the GOT here, | |
7689 | rather than in relocate_section. */ | |
7690 | g->local_gotno++; | |
a3c7651d | 7691 | sgot->_raw_size += MIPS_ELF_GOT_SIZE (dynobj); |
7403cb63 | 7692 | } |
252b5132 | 7693 | |
7403cb63 MM |
7694 | switch (r_type) |
7695 | { | |
7696 | case R_MIPS_CALL16: | |
252b5132 RH |
7697 | if (h == NULL) |
7698 | { | |
7699 | (*_bfd_error_handler) | |
7700 | (_("%s: CALL16 reloc at 0x%lx not against global symbol"), | |
7701 | bfd_get_filename (abfd), (unsigned long) rel->r_offset); | |
7702 | bfd_set_error (bfd_error_bad_value); | |
7703 | return false; | |
7704 | } | |
7403cb63 | 7705 | /* Fall through. */ |
252b5132 | 7706 | |
7403cb63 MM |
7707 | case R_MIPS_CALL_HI16: |
7708 | case R_MIPS_CALL_LO16: | |
5a44662b MM |
7709 | if (h != NULL) |
7710 | { | |
7711 | /* This symbol requires a global offset table entry. */ | |
7712 | if (!mips_elf_record_global_got_symbol (h, info, g)) | |
7713 | return false; | |
252b5132 | 7714 | |
5a44662b MM |
7715 | /* We need a stub, not a plt entry for the undefined |
7716 | function. But we record it as if it needs plt. See | |
7717 | elf_adjust_dynamic_symbol in elflink.h. */ | |
7718 | h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT; | |
7719 | h->type = STT_FUNC; | |
7720 | } | |
252b5132 RH |
7721 | break; |
7722 | ||
7723 | case R_MIPS_GOT16: | |
7724 | case R_MIPS_GOT_HI16: | |
7725 | case R_MIPS_GOT_LO16: | |
7403cb63 | 7726 | case R_MIPS_GOT_DISP: |
252b5132 | 7727 | /* This symbol requires a global offset table entry. */ |
7403cb63 MM |
7728 | if (h && !mips_elf_record_global_got_symbol (h, info, g)) |
7729 | return false; | |
252b5132 RH |
7730 | break; |
7731 | ||
7732 | case R_MIPS_32: | |
7733 | case R_MIPS_REL32: | |
a3c7651d | 7734 | case R_MIPS_64: |
252b5132 RH |
7735 | if ((info->shared || h != NULL) |
7736 | && (sec->flags & SEC_ALLOC) != 0) | |
7737 | { | |
7738 | if (sreloc == NULL) | |
7739 | { | |
103186c6 | 7740 | const char *name = MIPS_ELF_REL_DYN_SECTION_NAME (dynobj); |
252b5132 RH |
7741 | |
7742 | sreloc = bfd_get_section_by_name (dynobj, name); | |
7743 | if (sreloc == NULL) | |
7744 | { | |
7745 | sreloc = bfd_make_section (dynobj, name); | |
7746 | if (sreloc == NULL | |
7747 | || ! bfd_set_section_flags (dynobj, sreloc, | |
7748 | (SEC_ALLOC | |
7749 | | SEC_LOAD | |
7750 | | SEC_HAS_CONTENTS | |
7751 | | SEC_IN_MEMORY | |
7752 | | SEC_LINKER_CREATED | |
7753 | | SEC_READONLY)) | |
7754 | || ! bfd_set_section_alignment (dynobj, sreloc, | |
7755 | 4)) | |
7756 | return false; | |
7757 | } | |
7758 | } | |
7759 | if (info->shared) | |
103186c6 MM |
7760 | /* When creating a shared object, we must copy these |
7761 | reloc types into the output file as R_MIPS_REL32 | |
7762 | relocs. We make room for this reloc in the | |
7763 | .rel.dyn reloc section. */ | |
7764 | mips_elf_allocate_dynamic_relocations (dynobj, 1); | |
252b5132 RH |
7765 | else |
7766 | { | |
7767 | struct mips_elf_link_hash_entry *hmips; | |
7768 | ||
7769 | /* We only need to copy this reloc if the symbol is | |
7770 | defined in a dynamic object. */ | |
7771 | hmips = (struct mips_elf_link_hash_entry *) h; | |
a3c7651d | 7772 | ++hmips->possibly_dynamic_relocs; |
252b5132 | 7773 | } |
be3ccd9c | 7774 | |
7403cb63 MM |
7775 | /* Even though we don't directly need a GOT entry for |
7776 | this symbol, a symbol must have a dynamic symbol | |
5499724a | 7777 | table index greater that DT_MIPS_GOTSYM if there are |
7403cb63 | 7778 | dynamic relocations against it. */ |
7b1f1231 MM |
7779 | if (h != NULL |
7780 | && !mips_elf_record_global_got_symbol (h, info, g)) | |
7403cb63 | 7781 | return false; |
252b5132 RH |
7782 | } |
7783 | ||
313ba8d6 | 7784 | if (SGI_COMPAT (abfd)) |
252b5132 RH |
7785 | mips_elf_hash_table (info)->compact_rel_size += |
7786 | sizeof (Elf32_External_crinfo); | |
252b5132 RH |
7787 | break; |
7788 | ||
7789 | case R_MIPS_26: | |
7790 | case R_MIPS_GPREL16: | |
7791 | case R_MIPS_LITERAL: | |
7792 | case R_MIPS_GPREL32: | |
313ba8d6 | 7793 | if (SGI_COMPAT (abfd)) |
252b5132 RH |
7794 | mips_elf_hash_table (info)->compact_rel_size += |
7795 | sizeof (Elf32_External_crinfo); | |
7796 | break; | |
7797 | ||
7798 | /* This relocation describes the C++ object vtable hierarchy. | |
7799 | Reconstruct it for later use during GC. */ | |
7800 | case R_MIPS_GNU_VTINHERIT: | |
7801 | if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset)) | |
7802 | return false; | |
7803 | break; | |
7804 | ||
7805 | /* This relocation describes which C++ vtable entries are actually | |
7806 | used. Record for later use during GC. */ | |
7807 | case R_MIPS_GNU_VTENTRY: | |
7808 | if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset)) | |
7809 | return false; | |
7810 | break; | |
7811 | ||
7812 | default: | |
7813 | break; | |
7814 | } | |
7815 | ||
7816 | /* If this reloc is not a 16 bit call, and it has a global | |
7817 | symbol, then we will need the fn_stub if there is one. | |
be3ccd9c | 7818 | References from a stub section do not count. */ |
252b5132 | 7819 | if (h != NULL |
7403cb63 | 7820 | && r_type != R_MIPS16_26 |
252b5132 RH |
7821 | && strncmp (bfd_get_section_name (abfd, sec), FN_STUB, |
7822 | sizeof FN_STUB - 1) != 0 | |
7823 | && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB, | |
7824 | sizeof CALL_STUB - 1) != 0 | |
7825 | && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB, | |
7826 | sizeof CALL_FP_STUB - 1) != 0) | |
7827 | { | |
7828 | struct mips_elf_link_hash_entry *mh; | |
7829 | ||
7830 | mh = (struct mips_elf_link_hash_entry *) h; | |
7831 | mh->need_fn_stub = true; | |
7832 | } | |
7833 | } | |
7834 | ||
7835 | return true; | |
7836 | } | |
7837 | ||
7838 | /* Return the section that should be marked against GC for a given | |
7839 | relocation. */ | |
7840 | ||
103186c6 MM |
7841 | asection * |
7842 | _bfd_mips_elf_gc_mark_hook (abfd, info, rel, h, sym) | |
252b5132 | 7843 | bfd *abfd; |
5f771d47 | 7844 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
7845 | Elf_Internal_Rela *rel; |
7846 | struct elf_link_hash_entry *h; | |
7847 | Elf_Internal_Sym *sym; | |
7848 | { | |
7849 | /* ??? Do mips16 stub sections need to be handled special? */ | |
7850 | ||
7851 | if (h != NULL) | |
7852 | { | |
7853 | switch (ELF32_R_TYPE (rel->r_info)) | |
7854 | { | |
7855 | case R_MIPS_GNU_VTINHERIT: | |
7856 | case R_MIPS_GNU_VTENTRY: | |
7857 | break; | |
7858 | ||
7859 | default: | |
7860 | switch (h->root.type) | |
7861 | { | |
7862 | case bfd_link_hash_defined: | |
7863 | case bfd_link_hash_defweak: | |
7864 | return h->root.u.def.section; | |
7865 | ||
7866 | case bfd_link_hash_common: | |
7867 | return h->root.u.c.p->section; | |
7868 | ||
7869 | default: | |
7870 | break; | |
7871 | } | |
7872 | } | |
7873 | } | |
7874 | else | |
7875 | { | |
7876 | if (!(elf_bad_symtab (abfd) | |
7877 | && ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
7878 | && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE) | |
7879 | && sym->st_shndx != SHN_COMMON)) | |
7880 | { | |
7881 | return bfd_section_from_elf_index (abfd, sym->st_shndx); | |
7882 | } | |
7883 | } | |
7884 | ||
7885 | return NULL; | |
7886 | } | |
7887 | ||
7888 | /* Update the got entry reference counts for the section being removed. */ | |
7889 | ||
103186c6 MM |
7890 | boolean |
7891 | _bfd_mips_elf_gc_sweep_hook (abfd, info, sec, relocs) | |
5f771d47 ILT |
7892 | bfd *abfd ATTRIBUTE_UNUSED; |
7893 | struct bfd_link_info *info ATTRIBUTE_UNUSED; | |
7894 | asection *sec ATTRIBUTE_UNUSED; | |
7895 | const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED; | |
252b5132 RH |
7896 | { |
7897 | #if 0 | |
7898 | Elf_Internal_Shdr *symtab_hdr; | |
7899 | struct elf_link_hash_entry **sym_hashes; | |
7900 | bfd_signed_vma *local_got_refcounts; | |
7901 | const Elf_Internal_Rela *rel, *relend; | |
7902 | unsigned long r_symndx; | |
7903 | struct elf_link_hash_entry *h; | |
7904 | ||
7905 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
7906 | sym_hashes = elf_sym_hashes (abfd); | |
7907 | local_got_refcounts = elf_local_got_refcounts (abfd); | |
7908 | ||
7909 | relend = relocs + sec->reloc_count; | |
7910 | for (rel = relocs; rel < relend; rel++) | |
7911 | switch (ELF32_R_TYPE (rel->r_info)) | |
7912 | { | |
7913 | case R_MIPS_GOT16: | |
7914 | case R_MIPS_CALL16: | |
7915 | case R_MIPS_CALL_HI16: | |
7916 | case R_MIPS_CALL_LO16: | |
7917 | case R_MIPS_GOT_HI16: | |
7918 | case R_MIPS_GOT_LO16: | |
7919 | /* ??? It would seem that the existing MIPS code does no sort | |
7920 | of reference counting or whatnot on its GOT and PLT entries, | |
7921 | so it is not possible to garbage collect them at this time. */ | |
be3ccd9c | 7922 | break; |
252b5132 RH |
7923 | |
7924 | default: | |
7925 | break; | |
7926 | } | |
7927 | #endif | |
7928 | ||
7929 | return true; | |
7930 | } | |
7931 | ||
8a20f077 UC |
7932 | /* Copy data from a MIPS ELF indirect symbol to its direct symbol, |
7933 | hiding the old indirect symbol. Process additional relocation | |
7934 | information. */ | |
7935 | ||
7936 | void | |
7937 | _bfd_mips_elf_copy_indirect_symbol (dir, ind) | |
7938 | struct elf_link_hash_entry *dir, *ind; | |
7939 | { | |
7940 | struct mips_elf_link_hash_entry *dirmips, *indmips; | |
7941 | ||
7942 | _bfd_elf_link_hash_copy_indirect (dir, ind); | |
7943 | ||
7944 | dirmips = (struct mips_elf_link_hash_entry *) dir; | |
7945 | indmips = (struct mips_elf_link_hash_entry *) ind; | |
7946 | dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs; | |
7947 | if (dirmips->min_dyn_reloc_index == 0 | |
7948 | || (indmips->min_dyn_reloc_index != 0 | |
be3ccd9c | 7949 | && indmips->min_dyn_reloc_index < dirmips->min_dyn_reloc_index)) |
8a20f077 UC |
7950 | dirmips->min_dyn_reloc_index = indmips->min_dyn_reloc_index; |
7951 | } | |
7952 | ||
252b5132 RH |
7953 | /* Adjust a symbol defined by a dynamic object and referenced by a |
7954 | regular object. The current definition is in some section of the | |
7955 | dynamic object, but we're not including those sections. We have to | |
7956 | change the definition to something the rest of the link can | |
7957 | understand. */ | |
7958 | ||
103186c6 MM |
7959 | boolean |
7960 | _bfd_mips_elf_adjust_dynamic_symbol (info, h) | |
252b5132 RH |
7961 | struct bfd_link_info *info; |
7962 | struct elf_link_hash_entry *h; | |
7963 | { | |
7964 | bfd *dynobj; | |
7965 | struct mips_elf_link_hash_entry *hmips; | |
7966 | asection *s; | |
7967 | ||
7968 | dynobj = elf_hash_table (info)->dynobj; | |
7969 | ||
7970 | /* Make sure we know what is going on here. */ | |
7971 | BFD_ASSERT (dynobj != NULL | |
7972 | && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) | |
7973 | || h->weakdef != NULL | |
7974 | || ((h->elf_link_hash_flags | |
7975 | & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
7976 | && (h->elf_link_hash_flags | |
7977 | & ELF_LINK_HASH_REF_REGULAR) != 0 | |
7978 | && (h->elf_link_hash_flags | |
7979 | & ELF_LINK_HASH_DEF_REGULAR) == 0))); | |
7980 | ||
7981 | /* If this symbol is defined in a dynamic object, we need to copy | |
7982 | any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output | |
7983 | file. */ | |
7984 | hmips = (struct mips_elf_link_hash_entry *) h; | |
7985 | if (! info->relocateable | |
a3c7651d | 7986 | && hmips->possibly_dynamic_relocs != 0 |
252b5132 | 7987 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) |
be3ccd9c | 7988 | mips_elf_allocate_dynamic_relocations (dynobj, |
a3c7651d | 7989 | hmips->possibly_dynamic_relocs); |
252b5132 | 7990 | |
be3ccd9c | 7991 | /* For a function, create a stub, if needed. */ |
f7cb7d68 | 7992 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0) |
252b5132 RH |
7993 | { |
7994 | if (! elf_hash_table (info)->dynamic_sections_created) | |
7995 | return true; | |
7996 | ||
7997 | /* If this symbol is not defined in a regular file, then set | |
7998 | the symbol to the stub location. This is required to make | |
7999 | function pointers compare as equal between the normal | |
8000 | executable and the shared library. */ | |
8001 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
8002 | { | |
8003 | /* We need .stub section. */ | |
be3ccd9c | 8004 | s = bfd_get_section_by_name (dynobj, |
303f629d | 8005 | MIPS_ELF_STUB_SECTION_NAME (dynobj)); |
252b5132 RH |
8006 | BFD_ASSERT (s != NULL); |
8007 | ||
8008 | h->root.u.def.section = s; | |
8009 | h->root.u.def.value = s->_raw_size; | |
8010 | ||
8011 | /* XXX Write this stub address somewhere. */ | |
8012 | h->plt.offset = s->_raw_size; | |
8013 | ||
8014 | /* Make room for this stub code. */ | |
8015 | s->_raw_size += MIPS_FUNCTION_STUB_SIZE; | |
8016 | ||
8017 | /* The last half word of the stub will be filled with the index | |
8018 | of this symbol in .dynsym section. */ | |
8019 | return true; | |
8020 | } | |
8021 | } | |
f7cb7d68 | 8022 | else if ((h->type == STT_FUNC) |
be3ccd9c | 8023 | && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0) |
f7cb7d68 UC |
8024 | { |
8025 | /* This will set the entry for this symbol in the GOT to 0, and | |
be3ccd9c | 8026 | the dynamic linker will take care of this. */ |
f7cb7d68 UC |
8027 | h->root.u.def.value = 0; |
8028 | return true; | |
8029 | } | |
252b5132 RH |
8030 | |
8031 | /* If this is a weak symbol, and there is a real definition, the | |
8032 | processor independent code will have arranged for us to see the | |
8033 | real definition first, and we can just use the same value. */ | |
8034 | if (h->weakdef != NULL) | |
8035 | { | |
8036 | BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined | |
8037 | || h->weakdef->root.type == bfd_link_hash_defweak); | |
8038 | h->root.u.def.section = h->weakdef->root.u.def.section; | |
8039 | h->root.u.def.value = h->weakdef->root.u.def.value; | |
8040 | return true; | |
8041 | } | |
8042 | ||
8043 | /* This is a reference to a symbol defined by a dynamic object which | |
8044 | is not a function. */ | |
8045 | ||
8046 | return true; | |
8047 | } | |
8048 | ||
8049 | /* This function is called after all the input files have been read, | |
8050 | and the input sections have been assigned to output sections. We | |
8051 | check for any mips16 stub sections that we can discard. */ | |
8052 | ||
8053 | static boolean mips_elf_check_mips16_stubs | |
8054 | PARAMS ((struct mips_elf_link_hash_entry *, PTR)); | |
8055 | ||
103186c6 MM |
8056 | boolean |
8057 | _bfd_mips_elf_always_size_sections (output_bfd, info) | |
252b5132 RH |
8058 | bfd *output_bfd; |
8059 | struct bfd_link_info *info; | |
8060 | { | |
8061 | asection *ri; | |
8062 | ||
8063 | /* The .reginfo section has a fixed size. */ | |
8064 | ri = bfd_get_section_by_name (output_bfd, ".reginfo"); | |
8065 | if (ri != NULL) | |
8066 | bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo)); | |
8067 | ||
8068 | if (info->relocateable | |
8069 | || ! mips_elf_hash_table (info)->mips16_stubs_seen) | |
8070 | return true; | |
8071 | ||
8072 | mips_elf_link_hash_traverse (mips_elf_hash_table (info), | |
8073 | mips_elf_check_mips16_stubs, | |
8074 | (PTR) NULL); | |
8075 | ||
8076 | return true; | |
8077 | } | |
8078 | ||
8079 | /* Check the mips16 stubs for a particular symbol, and see if we can | |
8080 | discard them. */ | |
8081 | ||
252b5132 RH |
8082 | static boolean |
8083 | mips_elf_check_mips16_stubs (h, data) | |
8084 | struct mips_elf_link_hash_entry *h; | |
5f771d47 | 8085 | PTR data ATTRIBUTE_UNUSED; |
252b5132 RH |
8086 | { |
8087 | if (h->fn_stub != NULL | |
8088 | && ! h->need_fn_stub) | |
8089 | { | |
8090 | /* We don't need the fn_stub; the only references to this symbol | |
8091 | are 16 bit calls. Clobber the size to 0 to prevent it from | |
8092 | being included in the link. */ | |
8093 | h->fn_stub->_raw_size = 0; | |
8094 | h->fn_stub->_cooked_size = 0; | |
be3ccd9c | 8095 | h->fn_stub->flags &= ~SEC_RELOC; |
252b5132 RH |
8096 | h->fn_stub->reloc_count = 0; |
8097 | h->fn_stub->flags |= SEC_EXCLUDE; | |
8098 | } | |
8099 | ||
8100 | if (h->call_stub != NULL | |
8101 | && h->root.other == STO_MIPS16) | |
8102 | { | |
8103 | /* We don't need the call_stub; this is a 16 bit function, so | |
8104 | calls from other 16 bit functions are OK. Clobber the size | |
8105 | to 0 to prevent it from being included in the link. */ | |
8106 | h->call_stub->_raw_size = 0; | |
8107 | h->call_stub->_cooked_size = 0; | |
be3ccd9c | 8108 | h->call_stub->flags &= ~SEC_RELOC; |
252b5132 RH |
8109 | h->call_stub->reloc_count = 0; |
8110 | h->call_stub->flags |= SEC_EXCLUDE; | |
8111 | } | |
8112 | ||
8113 | if (h->call_fp_stub != NULL | |
8114 | && h->root.other == STO_MIPS16) | |
8115 | { | |
8116 | /* We don't need the call_stub; this is a 16 bit function, so | |
8117 | calls from other 16 bit functions are OK. Clobber the size | |
8118 | to 0 to prevent it from being included in the link. */ | |
8119 | h->call_fp_stub->_raw_size = 0; | |
8120 | h->call_fp_stub->_cooked_size = 0; | |
be3ccd9c | 8121 | h->call_fp_stub->flags &= ~SEC_RELOC; |
252b5132 RH |
8122 | h->call_fp_stub->reloc_count = 0; |
8123 | h->call_fp_stub->flags |= SEC_EXCLUDE; | |
8124 | } | |
8125 | ||
8126 | return true; | |
8127 | } | |
8128 | ||
8129 | /* Set the sizes of the dynamic sections. */ | |
8130 | ||
103186c6 MM |
8131 | boolean |
8132 | _bfd_mips_elf_size_dynamic_sections (output_bfd, info) | |
252b5132 RH |
8133 | bfd *output_bfd; |
8134 | struct bfd_link_info *info; | |
8135 | { | |
8136 | bfd *dynobj; | |
8137 | asection *s; | |
8138 | boolean reltext; | |
7a12753d | 8139 | struct mips_got_info *g = NULL; |
252b5132 RH |
8140 | |
8141 | dynobj = elf_hash_table (info)->dynobj; | |
8142 | BFD_ASSERT (dynobj != NULL); | |
8143 | ||
8144 | if (elf_hash_table (info)->dynamic_sections_created) | |
8145 | { | |
8146 | /* Set the contents of the .interp section to the interpreter. */ | |
8147 | if (! info->shared) | |
8148 | { | |
8149 | s = bfd_get_section_by_name (dynobj, ".interp"); | |
8150 | BFD_ASSERT (s != NULL); | |
be3ccd9c | 8151 | s->_raw_size |
303f629d | 8152 | = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1; |
be3ccd9c | 8153 | s->contents |
7403cb63 | 8154 | = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd); |
252b5132 RH |
8155 | } |
8156 | } | |
8157 | ||
252b5132 RH |
8158 | /* The check_relocs and adjust_dynamic_symbol entry points have |
8159 | determined the sizes of the various dynamic sections. Allocate | |
8160 | memory for them. */ | |
8161 | reltext = false; | |
8162 | for (s = dynobj->sections; s != NULL; s = s->next) | |
8163 | { | |
8164 | const char *name; | |
8165 | boolean strip; | |
8166 | ||
8167 | /* It's OK to base decisions on the section name, because none | |
8168 | of the dynobj section names depend upon the input files. */ | |
8169 | name = bfd_get_section_name (dynobj, s); | |
8170 | ||
8171 | if ((s->flags & SEC_LINKER_CREATED) == 0) | |
8172 | continue; | |
8173 | ||
8174 | strip = false; | |
8175 | ||
8176 | if (strncmp (name, ".rel", 4) == 0) | |
8177 | { | |
8178 | if (s->_raw_size == 0) | |
8179 | { | |
8180 | /* We only strip the section if the output section name | |
8181 | has the same name. Otherwise, there might be several | |
8182 | input sections for this output section. FIXME: This | |
8183 | code is probably not needed these days anyhow, since | |
8184 | the linker now does not create empty output sections. */ | |
8185 | if (s->output_section != NULL | |
8186 | && strcmp (name, | |
8187 | bfd_get_section_name (s->output_section->owner, | |
8188 | s->output_section)) == 0) | |
8189 | strip = true; | |
8190 | } | |
8191 | else | |
8192 | { | |
8193 | const char *outname; | |
8194 | asection *target; | |
8195 | ||
8196 | /* If this relocation section applies to a read only | |
8197 | section, then we probably need a DT_TEXTREL entry. | |
8198 | If the relocation section is .rel.dyn, we always | |
8199 | assert a DT_TEXTREL entry rather than testing whether | |
8200 | there exists a relocation to a read only section or | |
8201 | not. */ | |
8202 | outname = bfd_get_section_name (output_bfd, | |
8203 | s->output_section); | |
8204 | target = bfd_get_section_by_name (output_bfd, outname + 4); | |
8205 | if ((target != NULL | |
8206 | && (target->flags & SEC_READONLY) != 0 | |
8207 | && (target->flags & SEC_ALLOC) != 0) | |
be3ccd9c | 8208 | || strcmp (outname, |
103186c6 | 8209 | MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd)) == 0) |
252b5132 RH |
8210 | reltext = true; |
8211 | ||
8212 | /* We use the reloc_count field as a counter if we need | |
8213 | to copy relocs into the output file. */ | |
be3ccd9c | 8214 | if (strcmp (name, |
103186c6 | 8215 | MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd)) != 0) |
252b5132 RH |
8216 | s->reloc_count = 0; |
8217 | } | |
8218 | } | |
8219 | else if (strncmp (name, ".got", 4) == 0) | |
8220 | { | |
8221 | int i; | |
be3ccd9c KH |
8222 | bfd_size_type loadable_size = 0; |
8223 | bfd_size_type local_gotno; | |
8224 | struct _bfd *sub; | |
252b5132 | 8225 | |
be3ccd9c | 8226 | BFD_ASSERT (elf_section_data (s) != NULL); |
252b5132 | 8227 | g = (struct mips_got_info *) elf_section_data (s)->tdata; |
be3ccd9c KH |
8228 | BFD_ASSERT (g != NULL); |
8229 | ||
8230 | /* Calculate the total loadable size of the output. That | |
8231 | will give us the maximum number of GOT_PAGE entries | |
8232 | required. */ | |
8233 | for (sub = info->input_bfds; sub; sub = sub->link_next) | |
8234 | { | |
8235 | asection *subsection; | |
8236 | ||
8237 | for (subsection = sub->sections; | |
8238 | subsection; | |
8239 | subsection = subsection->next) | |
8240 | { | |
8241 | if ((subsection->flags & SEC_ALLOC) == 0) | |
8242 | continue; | |
8243 | loadable_size += (subsection->_raw_size + 0xf) & ~0xf; | |
8244 | } | |
8245 | } | |
8246 | loadable_size += MIPS_FUNCTION_STUB_SIZE; | |
8247 | ||
8248 | /* Assume there are two loadable segments consisting of | |
8249 | contiguous sections. Is 5 enough? */ | |
8250 | local_gotno = (loadable_size >> 16) + 5; | |
9458945f MM |
8251 | if (IRIX_COMPAT (output_bfd) == ict_irix6) |
8252 | /* It's possible we will need GOT_PAGE entries as well as | |
8253 | GOT16 entries. Often, these will be able to share GOT | |
8254 | entries, but not always. */ | |
8255 | local_gotno *= 2; | |
8256 | ||
be3ccd9c KH |
8257 | g->local_gotno += local_gotno; |
8258 | s->_raw_size += local_gotno * MIPS_ELF_GOT_SIZE (dynobj); | |
7403cb63 | 8259 | |
be3ccd9c KH |
8260 | /* There has to be a global GOT entry for every symbol with |
8261 | a dynamic symbol table index of DT_MIPS_GOTSYM or | |
8262 | higher. Therefore, it make sense to put those symbols | |
8263 | that need GOT entries at the end of the symbol table. We | |
8264 | do that here. */ | |
b3be9b46 | 8265 | if (!mips_elf_sort_hash_table (info, 1)) |
7403cb63 MM |
8266 | return false; |
8267 | ||
8b237a89 MM |
8268 | if (g->global_gotsym != NULL) |
8269 | i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx; | |
8270 | else | |
8271 | /* If there are no global symbols, or none requiring | |
8272 | relocations, then GLOBAL_GOTSYM will be NULL. */ | |
8273 | i = 0; | |
b3be9b46 | 8274 | g->global_gotno = i; |
103186c6 | 8275 | s->_raw_size += i * MIPS_ELF_GOT_SIZE (dynobj); |
252b5132 | 8276 | } |
303f629d | 8277 | else if (strcmp (name, MIPS_ELF_STUB_SECTION_NAME (output_bfd)) == 0) |
252b5132 RH |
8278 | { |
8279 | /* Irix rld assumes that the function stub isn't at the end | |
8280 | of .text section. So put a dummy. XXX */ | |
8281 | s->_raw_size += MIPS_FUNCTION_STUB_SIZE; | |
8282 | } | |
8283 | else if (! info->shared | |
8284 | && ! mips_elf_hash_table (info)->use_rld_obj_head | |
8285 | && strncmp (name, ".rld_map", 8) == 0) | |
8286 | { | |
8287 | /* We add a room for __rld_map. It will be filled in by the | |
8288 | rtld to contain a pointer to the _r_debug structure. */ | |
8289 | s->_raw_size += 4; | |
8290 | } | |
8291 | else if (SGI_COMPAT (output_bfd) | |
8292 | && strncmp (name, ".compact_rel", 12) == 0) | |
8293 | s->_raw_size += mips_elf_hash_table (info)->compact_rel_size; | |
c6142e5d MM |
8294 | else if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (output_bfd)) |
8295 | == 0) | |
be3ccd9c | 8296 | s->_raw_size = (sizeof (Elf32_External_Msym) |
c6142e5d MM |
8297 | * (elf_hash_table (info)->dynsymcount |
8298 | + bfd_count_sections (output_bfd))); | |
252b5132 RH |
8299 | else if (strncmp (name, ".init", 5) != 0) |
8300 | { | |
8301 | /* It's not one of our sections, so don't allocate space. */ | |
8302 | continue; | |
8303 | } | |
8304 | ||
8305 | if (strip) | |
8306 | { | |
7f8d5fc9 | 8307 | _bfd_strip_section_from_output (info, s); |
252b5132 RH |
8308 | continue; |
8309 | } | |
8310 | ||
8311 | /* Allocate memory for the section contents. */ | |
303f629d | 8312 | s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size); |
252b5132 RH |
8313 | if (s->contents == NULL && s->_raw_size != 0) |
8314 | { | |
8315 | bfd_set_error (bfd_error_no_memory); | |
8316 | return false; | |
8317 | } | |
252b5132 RH |
8318 | } |
8319 | ||
8320 | if (elf_hash_table (info)->dynamic_sections_created) | |
8321 | { | |
8322 | /* Add some entries to the .dynamic section. We fill in the | |
8323 | values later, in elf_mips_finish_dynamic_sections, but we | |
8324 | must add the entries now so that we get the correct size for | |
8325 | the .dynamic section. The DT_DEBUG entry is filled in by the | |
8326 | dynamic linker and used by the debugger. */ | |
8327 | if (! info->shared) | |
8328 | { | |
be3ccd9c KH |
8329 | /* SGI object has the equivalence of DT_DEBUG in the |
8330 | DT_MIPS_RLD_MAP entry. */ | |
8331 | if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0)) | |
8332 | return false; | |
8333 | if (!SGI_COMPAT (output_bfd)) | |
8334 | { | |
8335 | if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0)) | |
8336 | return false; | |
8337 | } | |
8338 | } | |
f7cb7d68 | 8339 | else |
be3ccd9c KH |
8340 | { |
8341 | /* Shared libraries on traditional mips have DT_DEBUG. */ | |
8342 | if (!SGI_COMPAT (output_bfd)) | |
8343 | { | |
8344 | if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0)) | |
8345 | return false; | |
8346 | } | |
8347 | } | |
8348 | if (reltext && SGI_COMPAT (output_bfd)) | |
252b5132 | 8349 | { |
103186c6 | 8350 | if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0)) |
252b5132 | 8351 | return false; |
d6cf2879 | 8352 | info->flags |= DF_TEXTREL; |
252b5132 RH |
8353 | } |
8354 | ||
103186c6 | 8355 | if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0)) |
252b5132 RH |
8356 | return false; |
8357 | ||
103186c6 MM |
8358 | if (bfd_get_section_by_name (dynobj, |
8359 | MIPS_ELF_REL_DYN_SECTION_NAME (dynobj))) | |
252b5132 | 8360 | { |
103186c6 | 8361 | if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0)) |
252b5132 RH |
8362 | return false; |
8363 | ||
103186c6 | 8364 | if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0)) |
252b5132 RH |
8365 | return false; |
8366 | ||
103186c6 | 8367 | if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0)) |
252b5132 RH |
8368 | return false; |
8369 | } | |
8370 | ||
f7cb7d68 | 8371 | if (SGI_COMPAT (output_bfd)) |
be3ccd9c KH |
8372 | { |
8373 | if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICTNO, 0)) | |
f7cb7d68 | 8374 | return false; |
be3ccd9c | 8375 | } |
252b5132 | 8376 | |
f7cb7d68 | 8377 | if (SGI_COMPAT (output_bfd)) |
be3ccd9c KH |
8378 | { |
8379 | if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLISTNO, 0)) | |
f7cb7d68 | 8380 | return false; |
be3ccd9c | 8381 | } |
252b5132 RH |
8382 | |
8383 | if (bfd_get_section_by_name (dynobj, ".conflict") != NULL) | |
8384 | { | |
103186c6 | 8385 | if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICT, 0)) |
252b5132 RH |
8386 | return false; |
8387 | ||
8388 | s = bfd_get_section_by_name (dynobj, ".liblist"); | |
8389 | BFD_ASSERT (s != NULL); | |
8390 | ||
103186c6 | 8391 | if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLIST, 0)) |
252b5132 RH |
8392 | return false; |
8393 | } | |
8394 | ||
103186c6 | 8395 | if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0)) |
252b5132 RH |
8396 | return false; |
8397 | ||
103186c6 | 8398 | if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0)) |
252b5132 RH |
8399 | return false; |
8400 | ||
8401 | #if 0 | |
8402 | /* Time stamps in executable files are a bad idea. */ | |
103186c6 | 8403 | if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_TIME_STAMP, 0)) |
252b5132 RH |
8404 | return false; |
8405 | #endif | |
8406 | ||
8407 | #if 0 /* FIXME */ | |
103186c6 | 8408 | if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_ICHECKSUM, 0)) |
252b5132 RH |
8409 | return false; |
8410 | #endif | |
8411 | ||
8412 | #if 0 /* FIXME */ | |
103186c6 | 8413 | if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_IVERSION, 0)) |
252b5132 RH |
8414 | return false; |
8415 | #endif | |
8416 | ||
103186c6 | 8417 | if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0)) |
252b5132 RH |
8418 | return false; |
8419 | ||
103186c6 | 8420 | if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0)) |
252b5132 RH |
8421 | return false; |
8422 | ||
103186c6 | 8423 | if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0)) |
252b5132 RH |
8424 | return false; |
8425 | ||
103186c6 | 8426 | if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0)) |
252b5132 RH |
8427 | return false; |
8428 | ||
5499724a | 8429 | if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0)) |
252b5132 RH |
8430 | return false; |
8431 | ||
7403cb63 | 8432 | if (IRIX_COMPAT (dynobj) == ict_irix5 |
103186c6 | 8433 | && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0)) |
252b5132 RH |
8434 | return false; |
8435 | ||
7403cb63 | 8436 | if (IRIX_COMPAT (dynobj) == ict_irix6 |
be3ccd9c | 8437 | && (bfd_get_section_by_name |
7403cb63 | 8438 | (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj))) |
103186c6 | 8439 | && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0)) |
7403cb63 | 8440 | return false; |
c6142e5d | 8441 | |
be3ccd9c | 8442 | if (bfd_get_section_by_name (dynobj, |
c6142e5d | 8443 | MIPS_ELF_MSYM_SECTION_NAME (dynobj)) |
103186c6 | 8444 | && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_MSYM, 0)) |
c6142e5d | 8445 | return false; |
252b5132 RH |
8446 | } |
8447 | ||
252b5132 RH |
8448 | return true; |
8449 | } | |
8450 | ||
7403cb63 MM |
8451 | /* If NAME is one of the special IRIX6 symbols defined by the linker, |
8452 | adjust it appropriately now. */ | |
8453 | ||
8454 | static void | |
8455 | mips_elf_irix6_finish_dynamic_symbol (abfd, name, sym) | |
5f771d47 | 8456 | bfd *abfd ATTRIBUTE_UNUSED; |
7403cb63 MM |
8457 | const char *name; |
8458 | Elf_Internal_Sym *sym; | |
8459 | { | |
8460 | /* The linker script takes care of providing names and values for | |
8461 | these, but we must place them into the right sections. */ | |
8462 | static const char* const text_section_symbols[] = { | |
8463 | "_ftext", | |
8464 | "_etext", | |
8465 | "__dso_displacement", | |
8466 | "__elf_header", | |
8467 | "__program_header_table", | |
8468 | NULL | |
8469 | }; | |
8470 | ||
8471 | static const char* const data_section_symbols[] = { | |
8472 | "_fdata", | |
8473 | "_edata", | |
8474 | "_end", | |
8475 | "_fbss", | |
8476 | NULL | |
8477 | }; | |
8478 | ||
8479 | const char* const *p; | |
8480 | int i; | |
8481 | ||
8482 | for (i = 0; i < 2; ++i) | |
be3ccd9c | 8483 | for (p = (i == 0) ? text_section_symbols : data_section_symbols; |
7403cb63 MM |
8484 | *p; |
8485 | ++p) | |
8486 | if (strcmp (*p, name) == 0) | |
8487 | { | |
8488 | /* All of these symbols are given type STT_SECTION by the | |
8489 | IRIX6 linker. */ | |
8490 | sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
be3ccd9c | 8491 | |
7403cb63 MM |
8492 | /* The IRIX linker puts these symbols in special sections. */ |
8493 | if (i == 0) | |
8494 | sym->st_shndx = SHN_MIPS_TEXT; | |
8495 | else | |
8496 | sym->st_shndx = SHN_MIPS_DATA; | |
be3ccd9c | 8497 | |
7403cb63 MM |
8498 | break; |
8499 | } | |
8500 | } | |
8501 | ||
252b5132 RH |
8502 | /* Finish up dynamic symbol handling. We set the contents of various |
8503 | dynamic sections here. */ | |
8504 | ||
103186c6 MM |
8505 | boolean |
8506 | _bfd_mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym) | |
252b5132 RH |
8507 | bfd *output_bfd; |
8508 | struct bfd_link_info *info; | |
8509 | struct elf_link_hash_entry *h; | |
8510 | Elf_Internal_Sym *sym; | |
8511 | { | |
8512 | bfd *dynobj; | |
8513 | bfd_vma gval; | |
8514 | asection *sgot; | |
c6142e5d | 8515 | asection *smsym; |
252b5132 RH |
8516 | struct mips_got_info *g; |
8517 | const char *name; | |
c6142e5d | 8518 | struct mips_elf_link_hash_entry *mh; |
252b5132 RH |
8519 | |
8520 | dynobj = elf_hash_table (info)->dynobj; | |
8521 | gval = sym->st_value; | |
c6142e5d | 8522 | mh = (struct mips_elf_link_hash_entry *) h; |
252b5132 RH |
8523 | |
8524 | if (h->plt.offset != (bfd_vma) -1) | |
8525 | { | |
8526 | asection *s; | |
8527 | bfd_byte *p; | |
8528 | bfd_byte stub[MIPS_FUNCTION_STUB_SIZE]; | |
8529 | ||
8530 | /* This symbol has a stub. Set it up. */ | |
8531 | ||
8532 | BFD_ASSERT (h->dynindx != -1); | |
8533 | ||
be3ccd9c | 8534 | s = bfd_get_section_by_name (dynobj, |
303f629d | 8535 | MIPS_ELF_STUB_SECTION_NAME (dynobj)); |
252b5132 RH |
8536 | BFD_ASSERT (s != NULL); |
8537 | ||
8538 | /* Fill the stub. */ | |
8539 | p = stub; | |
be3ccd9c | 8540 | bfd_put_32 (output_bfd, STUB_LW (output_bfd), p); |
252b5132 | 8541 | p += 4; |
be3ccd9c | 8542 | bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), p); |
252b5132 RH |
8543 | p += 4; |
8544 | ||
8545 | /* FIXME: Can h->dynindex be more than 64K? */ | |
8546 | if (h->dynindx & 0xffff0000) | |
8547 | return false; | |
8548 | ||
8549 | bfd_put_32 (output_bfd, STUB_JALR, p); | |
8550 | p += 4; | |
be3ccd9c | 8551 | bfd_put_32 (output_bfd, STUB_LI16 (output_bfd) + h->dynindx, p); |
252b5132 RH |
8552 | |
8553 | BFD_ASSERT (h->plt.offset <= s->_raw_size); | |
8554 | memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE); | |
8555 | ||
8556 | /* Mark the symbol as undefined. plt.offset != -1 occurs | |
8557 | only for the referenced symbol. */ | |
8558 | sym->st_shndx = SHN_UNDEF; | |
8559 | ||
8560 | /* The run-time linker uses the st_value field of the symbol | |
8561 | to reset the global offset table entry for this external | |
8562 | to its stub address when unlinking a shared object. */ | |
8563 | gval = s->output_section->vma + s->output_offset + h->plt.offset; | |
8564 | sym->st_value = gval; | |
8565 | } | |
8566 | ||
b305ef96 UC |
8567 | BFD_ASSERT (h->dynindx != -1 |
8568 | || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0); | |
252b5132 | 8569 | |
103186c6 | 8570 | sgot = mips_elf_got_section (dynobj); |
252b5132 RH |
8571 | BFD_ASSERT (sgot != NULL); |
8572 | BFD_ASSERT (elf_section_data (sgot) != NULL); | |
8573 | g = (struct mips_got_info *) elf_section_data (sgot)->tdata; | |
8574 | BFD_ASSERT (g != NULL); | |
8575 | ||
7403cb63 MM |
8576 | /* Run through the global symbol table, creating GOT entries for all |
8577 | the symbols that need them. */ | |
8b237a89 MM |
8578 | if (g->global_gotsym != NULL |
8579 | && h->dynindx >= g->global_gotsym->dynindx) | |
252b5132 | 8580 | { |
7403cb63 MM |
8581 | bfd_vma offset; |
8582 | bfd_vma value; | |
252b5132 | 8583 | |
7403cb63 MM |
8584 | if (sym->st_value) |
8585 | value = sym->st_value; | |
8586 | else | |
be3ccd9c KH |
8587 | { |
8588 | /* For an entity defined in a shared object, this will be | |
8589 | NULL. (For functions in shared objects for | |
8590 | which we have created stubs, ST_VALUE will be non-NULL. | |
8591 | That's because such the functions are now no longer defined | |
8592 | in a shared object.) */ | |
8593 | ||
8594 | if (info->shared && h->root.type == bfd_link_hash_undefined) | |
8595 | value = 0; | |
8596 | else | |
8597 | value = h->root.u.def.value; | |
8598 | } | |
7403cb63 | 8599 | offset = mips_elf_global_got_index (dynobj, h); |
103186c6 | 8600 | MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset); |
252b5132 RH |
8601 | } |
8602 | ||
c6142e5d | 8603 | /* Create a .msym entry, if appropriate. */ |
be3ccd9c | 8604 | smsym = bfd_get_section_by_name (dynobj, |
c6142e5d MM |
8605 | MIPS_ELF_MSYM_SECTION_NAME (dynobj)); |
8606 | if (smsym) | |
8607 | { | |
8608 | Elf32_Internal_Msym msym; | |
8609 | ||
8610 | msym.ms_hash_value = bfd_elf_hash (h->root.root.string); | |
8611 | /* It is undocumented what the `1' indicates, but IRIX6 uses | |
8612 | this value. */ | |
8613 | msym.ms_info = ELF32_MS_INFO (mh->min_dyn_reloc_index, 1); | |
be3ccd9c | 8614 | bfd_mips_elf_swap_msym_out |
c6142e5d MM |
8615 | (dynobj, &msym, |
8616 | ((Elf32_External_Msym *) smsym->contents) + h->dynindx); | |
8617 | } | |
8618 | ||
252b5132 RH |
8619 | /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */ |
8620 | name = h->root.root.string; | |
8621 | if (strcmp (name, "_DYNAMIC") == 0 | |
8622 | || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0) | |
8623 | sym->st_shndx = SHN_ABS; | |
f7cb7d68 | 8624 | else if (strcmp (name, "_DYNAMIC_LINK") == 0 |
be3ccd9c | 8625 | || strcmp (name, "_DYNAMIC_LINKING") == 0) |
252b5132 RH |
8626 | { |
8627 | sym->st_shndx = SHN_ABS; | |
8628 | sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
8629 | sym->st_value = 1; | |
8630 | } | |
f7cb7d68 UC |
8631 | else if (strcmp (name, "_gp_disp") == 0) |
8632 | { | |
8633 | sym->st_shndx = SHN_ABS; | |
8634 | sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
8635 | sym->st_value = elf_gp (output_bfd); | |
8636 | } | |
252b5132 RH |
8637 | else if (SGI_COMPAT (output_bfd)) |
8638 | { | |
f7cb7d68 | 8639 | if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0 |
be3ccd9c | 8640 | || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0) |
252b5132 RH |
8641 | { |
8642 | sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
8643 | sym->st_other = STO_PROTECTED; | |
8644 | sym->st_value = 0; | |
8645 | sym->st_shndx = SHN_MIPS_DATA; | |
8646 | } | |
8647 | else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0) | |
8648 | { | |
8649 | sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
8650 | sym->st_other = STO_PROTECTED; | |
8651 | sym->st_value = mips_elf_hash_table (info)->procedure_count; | |
8652 | sym->st_shndx = SHN_ABS; | |
8653 | } | |
8654 | else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS) | |
8655 | { | |
8656 | if (h->type == STT_FUNC) | |
8657 | sym->st_shndx = SHN_MIPS_TEXT; | |
8658 | else if (h->type == STT_OBJECT) | |
8659 | sym->st_shndx = SHN_MIPS_DATA; | |
8660 | } | |
8661 | } | |
8662 | ||
7403cb63 MM |
8663 | /* Handle the IRIX6-specific symbols. */ |
8664 | if (IRIX_COMPAT (output_bfd) == ict_irix6) | |
8665 | mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym); | |
8666 | ||
f7cb7d68 | 8667 | if (! info->shared) |
252b5132 RH |
8668 | { |
8669 | if (! mips_elf_hash_table (info)->use_rld_obj_head | |
31a9bdd9 UC |
8670 | && (strcmp (name, "__rld_map") == 0 |
8671 | || strcmp (name, "__RLD_MAP") == 0)) | |
252b5132 RH |
8672 | { |
8673 | asection *s = bfd_get_section_by_name (dynobj, ".rld_map"); | |
8674 | BFD_ASSERT (s != NULL); | |
8675 | sym->st_value = s->output_section->vma + s->output_offset; | |
8676 | bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents); | |
8677 | if (mips_elf_hash_table (info)->rld_value == 0) | |
8678 | mips_elf_hash_table (info)->rld_value = sym->st_value; | |
8679 | } | |
8680 | else if (mips_elf_hash_table (info)->use_rld_obj_head | |
8681 | && strcmp (name, "__rld_obj_head") == 0) | |
8682 | { | |
303f629d | 8683 | /* IRIX6 does not use a .rld_map section. */ |
f7cb7d68 UC |
8684 | if (IRIX_COMPAT (output_bfd) == ict_irix5 |
8685 | || IRIX_COMPAT (output_bfd) == ict_none) | |
be3ccd9c | 8686 | BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map") |
303f629d | 8687 | != NULL); |
252b5132 RH |
8688 | mips_elf_hash_table (info)->rld_value = sym->st_value; |
8689 | } | |
8690 | } | |
8691 | ||
8692 | /* If this is a mips16 symbol, force the value to be even. */ | |
8693 | if (sym->st_other == STO_MIPS16 | |
8694 | && (sym->st_value & 1) != 0) | |
8695 | --sym->st_value; | |
8696 | ||
8697 | return true; | |
8698 | } | |
8699 | ||
8700 | /* Finish up the dynamic sections. */ | |
8701 | ||
103186c6 MM |
8702 | boolean |
8703 | _bfd_mips_elf_finish_dynamic_sections (output_bfd, info) | |
252b5132 RH |
8704 | bfd *output_bfd; |
8705 | struct bfd_link_info *info; | |
8706 | { | |
8707 | bfd *dynobj; | |
8708 | asection *sdyn; | |
8709 | asection *sgot; | |
8710 | struct mips_got_info *g; | |
8711 | ||
8712 | dynobj = elf_hash_table (info)->dynobj; | |
8713 | ||
8714 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); | |
8715 | ||
103186c6 | 8716 | sgot = mips_elf_got_section (dynobj); |
252b5132 RH |
8717 | if (sgot == NULL) |
8718 | g = NULL; | |
8719 | else | |
8720 | { | |
8721 | BFD_ASSERT (elf_section_data (sgot) != NULL); | |
8722 | g = (struct mips_got_info *) elf_section_data (sgot)->tdata; | |
8723 | BFD_ASSERT (g != NULL); | |
8724 | } | |
8725 | ||
8726 | if (elf_hash_table (info)->dynamic_sections_created) | |
8727 | { | |
103186c6 | 8728 | bfd_byte *b; |
252b5132 RH |
8729 | |
8730 | BFD_ASSERT (sdyn != NULL); | |
8731 | BFD_ASSERT (g != NULL); | |
8732 | ||
103186c6 MM |
8733 | for (b = sdyn->contents; |
8734 | b < sdyn->contents + sdyn->_raw_size; | |
8735 | b += MIPS_ELF_DYN_SIZE (dynobj)) | |
252b5132 RH |
8736 | { |
8737 | Elf_Internal_Dyn dyn; | |
8738 | const char *name; | |
8739 | size_t elemsize; | |
8740 | asection *s; | |
103186c6 | 8741 | boolean swap_out_p; |
252b5132 | 8742 | |
103186c6 MM |
8743 | /* Read in the current dynamic entry. */ |
8744 | (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn); | |
be3ccd9c | 8745 | |
103186c6 MM |
8746 | /* Assume that we're going to modify it and write it out. */ |
8747 | swap_out_p = true; | |
252b5132 RH |
8748 | |
8749 | switch (dyn.d_tag) | |
8750 | { | |
252b5132 | 8751 | case DT_RELENT: |
be3ccd9c | 8752 | s = (bfd_get_section_by_name |
103186c6 MM |
8753 | (dynobj, |
8754 | MIPS_ELF_REL_DYN_SECTION_NAME (dynobj))); | |
252b5132 | 8755 | BFD_ASSERT (s != NULL); |
103186c6 | 8756 | dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj); |
252b5132 RH |
8757 | break; |
8758 | ||
8759 | case DT_STRSZ: | |
8760 | /* Rewrite DT_STRSZ. */ | |
8761 | dyn.d_un.d_val = | |
8762 | _bfd_stringtab_size (elf_hash_table (info)->dynstr); | |
252b5132 RH |
8763 | break; |
8764 | ||
8765 | case DT_PLTGOT: | |
8766 | name = ".got"; | |
8767 | goto get_vma; | |
8768 | case DT_MIPS_CONFLICT: | |
8769 | name = ".conflict"; | |
8770 | goto get_vma; | |
8771 | case DT_MIPS_LIBLIST: | |
8772 | name = ".liblist"; | |
8773 | get_vma: | |
8774 | s = bfd_get_section_by_name (output_bfd, name); | |
8775 | BFD_ASSERT (s != NULL); | |
8776 | dyn.d_un.d_ptr = s->vma; | |
252b5132 RH |
8777 | break; |
8778 | ||
8779 | case DT_MIPS_RLD_VERSION: | |
8780 | dyn.d_un.d_val = 1; /* XXX */ | |
252b5132 RH |
8781 | break; |
8782 | ||
8783 | case DT_MIPS_FLAGS: | |
8784 | dyn.d_un.d_val = RHF_NOTPOT; /* XXX */ | |
252b5132 RH |
8785 | break; |
8786 | ||
8787 | case DT_MIPS_CONFLICTNO: | |
8788 | name = ".conflict"; | |
8789 | elemsize = sizeof (Elf32_Conflict); | |
8790 | goto set_elemno; | |
8791 | ||
8792 | case DT_MIPS_LIBLISTNO: | |
8793 | name = ".liblist"; | |
8794 | elemsize = sizeof (Elf32_Lib); | |
8795 | set_elemno: | |
8796 | s = bfd_get_section_by_name (output_bfd, name); | |
8797 | if (s != NULL) | |
8798 | { | |
8799 | if (s->_cooked_size != 0) | |
8800 | dyn.d_un.d_val = s->_cooked_size / elemsize; | |
8801 | else | |
8802 | dyn.d_un.d_val = s->_raw_size / elemsize; | |
8803 | } | |
8804 | else | |
be3ccd9c | 8805 | dyn.d_un.d_val = 0; |
252b5132 RH |
8806 | break; |
8807 | ||
8808 | case DT_MIPS_TIME_STAMP: | |
8809 | time ((time_t *) &dyn.d_un.d_val); | |
252b5132 RH |
8810 | break; |
8811 | ||
8812 | case DT_MIPS_ICHECKSUM: | |
8813 | /* XXX FIXME: */ | |
103186c6 | 8814 | swap_out_p = false; |
252b5132 RH |
8815 | break; |
8816 | ||
8817 | case DT_MIPS_IVERSION: | |
8818 | /* XXX FIXME: */ | |
103186c6 | 8819 | swap_out_p = false; |
252b5132 RH |
8820 | break; |
8821 | ||
8822 | case DT_MIPS_BASE_ADDRESS: | |
8823 | s = output_bfd->sections; | |
8824 | BFD_ASSERT (s != NULL); | |
8825 | dyn.d_un.d_ptr = s->vma & ~(0xffff); | |
252b5132 RH |
8826 | break; |
8827 | ||
8828 | case DT_MIPS_LOCAL_GOTNO: | |
8829 | dyn.d_un.d_val = g->local_gotno; | |
252b5132 RH |
8830 | break; |
8831 | ||
5499724a MM |
8832 | case DT_MIPS_UNREFEXTNO: |
8833 | /* The index into the dynamic symbol table which is the | |
8834 | entry of the first external symbol that is not | |
8835 | referenced within the same object. */ | |
8836 | dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1; | |
8837 | break; | |
8838 | ||
8839 | case DT_MIPS_GOTSYM: | |
8840 | if (g->global_gotsym) | |
8841 | { | |
8842 | dyn.d_un.d_val = g->global_gotsym->dynindx; | |
8843 | break; | |
8844 | } | |
8845 | /* In case if we don't have global got symbols we default | |
8846 | to setting DT_MIPS_GOTSYM to the same value as | |
8847 | DT_MIPS_SYMTABNO, so we just fall through. */ | |
8848 | ||
252b5132 RH |
8849 | case DT_MIPS_SYMTABNO: |
8850 | name = ".dynsym"; | |
103186c6 | 8851 | elemsize = MIPS_ELF_SYM_SIZE (output_bfd); |
252b5132 RH |
8852 | s = bfd_get_section_by_name (output_bfd, name); |
8853 | BFD_ASSERT (s != NULL); | |
8854 | ||
8855 | if (s->_cooked_size != 0) | |
8856 | dyn.d_un.d_val = s->_cooked_size / elemsize; | |
8857 | else | |
8858 | dyn.d_un.d_val = s->_raw_size / elemsize; | |
252b5132 RH |
8859 | break; |
8860 | ||
252b5132 RH |
8861 | case DT_MIPS_HIPAGENO: |
8862 | dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO; | |
252b5132 RH |
8863 | break; |
8864 | ||
8865 | case DT_MIPS_RLD_MAP: | |
8866 | dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value; | |
252b5132 RH |
8867 | break; |
8868 | ||
7403cb63 | 8869 | case DT_MIPS_OPTIONS: |
be3ccd9c | 8870 | s = (bfd_get_section_by_name |
7403cb63 MM |
8871 | (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd))); |
8872 | dyn.d_un.d_ptr = s->vma; | |
7403cb63 MM |
8873 | break; |
8874 | ||
c6142e5d | 8875 | case DT_MIPS_MSYM: |
be3ccd9c | 8876 | s = (bfd_get_section_by_name |
c6142e5d MM |
8877 | (output_bfd, MIPS_ELF_MSYM_SECTION_NAME (output_bfd))); |
8878 | dyn.d_un.d_ptr = s->vma; | |
103186c6 MM |
8879 | break; |
8880 | ||
8881 | default: | |
8882 | swap_out_p = false; | |
c6142e5d | 8883 | break; |
252b5132 | 8884 | } |
103186c6 MM |
8885 | |
8886 | if (swap_out_p) | |
be3ccd9c | 8887 | (*get_elf_backend_data (dynobj)->s->swap_dyn_out) |
103186c6 | 8888 | (dynobj, &dyn, b); |
252b5132 RH |
8889 | } |
8890 | } | |
8891 | ||
8892 | /* The first entry of the global offset table will be filled at | |
8893 | runtime. The second entry will be used by some runtime loaders. | |
be3ccd9c | 8894 | This isn't the case of Irix rld. */ |
252b5132 RH |
8895 | if (sgot != NULL && sgot->_raw_size > 0) |
8896 | { | |
103186c6 | 8897 | MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents); |
be3ccd9c | 8898 | MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0x80000000, |
103186c6 | 8899 | sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd)); |
252b5132 RH |
8900 | } |
8901 | ||
8902 | if (sgot != NULL) | |
103186c6 MM |
8903 | elf_section_data (sgot->output_section)->this_hdr.sh_entsize |
8904 | = MIPS_ELF_GOT_SIZE (output_bfd); | |
252b5132 RH |
8905 | |
8906 | { | |
c6142e5d | 8907 | asection *smsym; |
252b5132 | 8908 | asection *s; |
252b5132 RH |
8909 | Elf32_compact_rel cpt; |
8910 | ||
30b30c21 RH |
8911 | /* ??? The section symbols for the output sections were set up in |
8912 | _bfd_elf_final_link. SGI sets the STT_NOTYPE attribute for these | |
8913 | symbols. Should we do so? */ | |
252b5132 | 8914 | |
be3ccd9c | 8915 | smsym = bfd_get_section_by_name (dynobj, |
c6142e5d | 8916 | MIPS_ELF_MSYM_SECTION_NAME (dynobj)); |
30b30c21 | 8917 | if (smsym != NULL) |
252b5132 | 8918 | { |
103186c6 | 8919 | Elf32_Internal_Msym msym; |
c6142e5d | 8920 | |
103186c6 MM |
8921 | msym.ms_hash_value = 0; |
8922 | msym.ms_info = ELF32_MS_INFO (0, 1); | |
c6142e5d | 8923 | |
103186c6 MM |
8924 | for (s = output_bfd->sections; s != NULL; s = s->next) |
8925 | { | |
30b30c21 | 8926 | long dynindx = elf_section_data (s)->dynindx; |
252b5132 | 8927 | |
be3ccd9c | 8928 | bfd_mips_elf_swap_msym_out |
30b30c21 RH |
8929 | (output_bfd, &msym, |
8930 | (((Elf32_External_Msym *) smsym->contents) | |
8931 | + dynindx)); | |
8932 | } | |
252b5132 RH |
8933 | } |
8934 | ||
8935 | if (SGI_COMPAT (output_bfd)) | |
8936 | { | |
8937 | /* Write .compact_rel section out. */ | |
8938 | s = bfd_get_section_by_name (dynobj, ".compact_rel"); | |
8939 | if (s != NULL) | |
8940 | { | |
8941 | cpt.id1 = 1; | |
8942 | cpt.num = s->reloc_count; | |
8943 | cpt.id2 = 2; | |
8944 | cpt.offset = (s->output_section->filepos | |
8945 | + sizeof (Elf32_External_compact_rel)); | |
8946 | cpt.reserved0 = 0; | |
8947 | cpt.reserved1 = 0; | |
8948 | bfd_elf32_swap_compact_rel_out (output_bfd, &cpt, | |
8949 | ((Elf32_External_compact_rel *) | |
8950 | s->contents)); | |
8951 | ||
8952 | /* Clean up a dummy stub function entry in .text. */ | |
be3ccd9c | 8953 | s = bfd_get_section_by_name (dynobj, |
303f629d | 8954 | MIPS_ELF_STUB_SECTION_NAME (dynobj)); |
252b5132 RH |
8955 | if (s != NULL) |
8956 | { | |
8957 | file_ptr dummy_offset; | |
8958 | ||
8959 | BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE); | |
8960 | dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE; | |
8961 | memset (s->contents + dummy_offset, 0, | |
8962 | MIPS_FUNCTION_STUB_SIZE); | |
8963 | } | |
8964 | } | |
8965 | } | |
8966 | ||
adb76a3e UC |
8967 | /* We need to sort the entries of the dynamic relocation section. */ |
8968 | ||
8969 | if (!ABI_64_P (output_bfd)) | |
8970 | { | |
be3ccd9c KH |
8971 | asection *reldyn; |
8972 | ||
8973 | reldyn = bfd_get_section_by_name (dynobj, | |
8974 | MIPS_ELF_REL_DYN_SECTION_NAME (dynobj)); | |
8975 | if (reldyn != NULL && reldyn->reloc_count > 2) | |
8976 | { | |
8977 | reldyn_sorting_bfd = output_bfd; | |
8978 | qsort ((Elf32_External_Rel *) reldyn->contents + 1, | |
8979 | (size_t) reldyn->reloc_count - 1, | |
8980 | sizeof (Elf32_External_Rel), sort_dynamic_relocs); | |
8981 | } | |
adb76a3e UC |
8982 | } |
8983 | ||
252b5132 | 8984 | /* Clean up a first relocation in .rel.dyn. */ |
be3ccd9c | 8985 | s = bfd_get_section_by_name (dynobj, |
103186c6 | 8986 | MIPS_ELF_REL_DYN_SECTION_NAME (dynobj)); |
252b5132 | 8987 | if (s != NULL && s->_raw_size > 0) |
103186c6 | 8988 | memset (s->contents, 0, MIPS_ELF_REL_SIZE (dynobj)); |
252b5132 RH |
8989 | } |
8990 | ||
8991 | return true; | |
8992 | } | |
8993 | \f | |
8994 | /* This is almost identical to bfd_generic_get_... except that some | |
8995 | MIPS relocations need to be handled specially. Sigh. */ | |
8996 | ||
8997 | static bfd_byte * | |
8998 | elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data, | |
8999 | relocateable, symbols) | |
9000 | bfd *abfd; | |
9001 | struct bfd_link_info *link_info; | |
9002 | struct bfd_link_order *link_order; | |
9003 | bfd_byte *data; | |
9004 | boolean relocateable; | |
9005 | asymbol **symbols; | |
9006 | { | |
9007 | /* Get enough memory to hold the stuff */ | |
9008 | bfd *input_bfd = link_order->u.indirect.section->owner; | |
9009 | asection *input_section = link_order->u.indirect.section; | |
9010 | ||
9011 | long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section); | |
9012 | arelent **reloc_vector = NULL; | |
9013 | long reloc_count; | |
9014 | ||
9015 | if (reloc_size < 0) | |
9016 | goto error_return; | |
9017 | ||
9018 | reloc_vector = (arelent **) bfd_malloc (reloc_size); | |
9019 | if (reloc_vector == NULL && reloc_size != 0) | |
9020 | goto error_return; | |
9021 | ||
9022 | /* read in the section */ | |
9023 | if (!bfd_get_section_contents (input_bfd, | |
9024 | input_section, | |
9025 | (PTR) data, | |
9026 | 0, | |
9027 | input_section->_raw_size)) | |
9028 | goto error_return; | |
9029 | ||
9030 | /* We're not relaxing the section, so just copy the size info */ | |
9031 | input_section->_cooked_size = input_section->_raw_size; | |
9032 | input_section->reloc_done = true; | |
9033 | ||
9034 | reloc_count = bfd_canonicalize_reloc (input_bfd, | |
9035 | input_section, | |
9036 | reloc_vector, | |
9037 | symbols); | |
9038 | if (reloc_count < 0) | |
9039 | goto error_return; | |
9040 | ||
9041 | if (reloc_count > 0) | |
9042 | { | |
9043 | arelent **parent; | |
9044 | /* for mips */ | |
9045 | int gp_found; | |
9046 | bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */ | |
9047 | ||
9048 | { | |
9049 | struct bfd_hash_entry *h; | |
9050 | struct bfd_link_hash_entry *lh; | |
9051 | /* Skip all this stuff if we aren't mixing formats. */ | |
9052 | if (abfd && input_bfd | |
9053 | && abfd->xvec == input_bfd->xvec) | |
9054 | lh = 0; | |
9055 | else | |
9056 | { | |
9057 | h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false); | |
9058 | lh = (struct bfd_link_hash_entry *) h; | |
9059 | } | |
9060 | lookup: | |
9061 | if (lh) | |
9062 | { | |
9063 | switch (lh->type) | |
9064 | { | |
9065 | case bfd_link_hash_undefined: | |
9066 | case bfd_link_hash_undefweak: | |
9067 | case bfd_link_hash_common: | |
9068 | gp_found = 0; | |
9069 | break; | |
9070 | case bfd_link_hash_defined: | |
9071 | case bfd_link_hash_defweak: | |
9072 | gp_found = 1; | |
9073 | gp = lh->u.def.value; | |
9074 | break; | |
9075 | case bfd_link_hash_indirect: | |
9076 | case bfd_link_hash_warning: | |
9077 | lh = lh->u.i.link; | |
9078 | /* @@FIXME ignoring warning for now */ | |
9079 | goto lookup; | |
9080 | case bfd_link_hash_new: | |
9081 | default: | |
9082 | abort (); | |
9083 | } | |
9084 | } | |
9085 | else | |
9086 | gp_found = 0; | |
9087 | } | |
9088 | /* end mips */ | |
9089 | for (parent = reloc_vector; *parent != (arelent *) NULL; | |
9090 | parent++) | |
9091 | { | |
9092 | char *error_message = (char *) NULL; | |
9093 | bfd_reloc_status_type r; | |
9094 | ||
9095 | /* Specific to MIPS: Deal with relocation types that require | |
9096 | knowing the gp of the output bfd. */ | |
9097 | asymbol *sym = *(*parent)->sym_ptr_ptr; | |
9098 | if (bfd_is_abs_section (sym->section) && abfd) | |
9099 | { | |
9100 | /* The special_function wouldn't get called anyways. */ | |
9101 | } | |
9102 | else if (!gp_found) | |
9103 | { | |
9104 | /* The gp isn't there; let the special function code | |
9105 | fall over on its own. */ | |
9106 | } | |
9107 | else if ((*parent)->howto->special_function | |
9108 | == _bfd_mips_elf_gprel16_reloc) | |
9109 | { | |
9110 | /* bypass special_function call */ | |
9111 | r = gprel16_with_gp (input_bfd, sym, *parent, input_section, | |
9112 | relocateable, (PTR) data, gp); | |
9113 | goto skip_bfd_perform_relocation; | |
9114 | } | |
9115 | /* end mips specific stuff */ | |
9116 | ||
9117 | r = bfd_perform_relocation (input_bfd, | |
9118 | *parent, | |
9119 | (PTR) data, | |
9120 | input_section, | |
9121 | relocateable ? abfd : (bfd *) NULL, | |
9122 | &error_message); | |
9123 | skip_bfd_perform_relocation: | |
9124 | ||
9125 | if (relocateable) | |
9126 | { | |
9127 | asection *os = input_section->output_section; | |
9128 | ||
9129 | /* A partial link, so keep the relocs */ | |
9130 | os->orelocation[os->reloc_count] = *parent; | |
9131 | os->reloc_count++; | |
9132 | } | |
9133 | ||
9134 | if (r != bfd_reloc_ok) | |
9135 | { | |
9136 | switch (r) | |
9137 | { | |
9138 | case bfd_reloc_undefined: | |
9139 | if (!((*link_info->callbacks->undefined_symbol) | |
9140 | (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr), | |
5cc7c785 L |
9141 | input_bfd, input_section, (*parent)->address, |
9142 | true))) | |
252b5132 RH |
9143 | goto error_return; |
9144 | break; | |
9145 | case bfd_reloc_dangerous: | |
9146 | BFD_ASSERT (error_message != (char *) NULL); | |
9147 | if (!((*link_info->callbacks->reloc_dangerous) | |
9148 | (link_info, error_message, input_bfd, input_section, | |
9149 | (*parent)->address))) | |
9150 | goto error_return; | |
9151 | break; | |
9152 | case bfd_reloc_overflow: | |
9153 | if (!((*link_info->callbacks->reloc_overflow) | |
9154 | (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr), | |
9155 | (*parent)->howto->name, (*parent)->addend, | |
9156 | input_bfd, input_section, (*parent)->address))) | |
9157 | goto error_return; | |
9158 | break; | |
9159 | case bfd_reloc_outofrange: | |
9160 | default: | |
9161 | abort (); | |
9162 | break; | |
9163 | } | |
9164 | ||
9165 | } | |
9166 | } | |
9167 | } | |
9168 | if (reloc_vector != NULL) | |
9169 | free (reloc_vector); | |
9170 | return data; | |
9171 | ||
9172 | error_return: | |
9173 | if (reloc_vector != NULL) | |
9174 | free (reloc_vector); | |
9175 | return NULL; | |
9176 | } | |
be3ccd9c | 9177 | |
252b5132 RH |
9178 | #define bfd_elf32_bfd_get_relocated_section_contents \ |
9179 | elf32_mips_get_relocated_section_contents | |
9180 | \f | |
9181 | /* ECOFF swapping routines. These are used when dealing with the | |
9182 | .mdebug section, which is in the ECOFF debugging format. */ | |
be3ccd9c | 9183 | static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap = { |
252b5132 RH |
9184 | /* Symbol table magic number. */ |
9185 | magicSym, | |
9186 | /* Alignment of debugging information. E.g., 4. */ | |
9187 | 4, | |
9188 | /* Sizes of external symbolic information. */ | |
9189 | sizeof (struct hdr_ext), | |
9190 | sizeof (struct dnr_ext), | |
9191 | sizeof (struct pdr_ext), | |
9192 | sizeof (struct sym_ext), | |
9193 | sizeof (struct opt_ext), | |
9194 | sizeof (struct fdr_ext), | |
9195 | sizeof (struct rfd_ext), | |
9196 | sizeof (struct ext_ext), | |
9197 | /* Functions to swap in external symbolic data. */ | |
9198 | ecoff_swap_hdr_in, | |
9199 | ecoff_swap_dnr_in, | |
9200 | ecoff_swap_pdr_in, | |
9201 | ecoff_swap_sym_in, | |
9202 | ecoff_swap_opt_in, | |
9203 | ecoff_swap_fdr_in, | |
9204 | ecoff_swap_rfd_in, | |
9205 | ecoff_swap_ext_in, | |
9206 | _bfd_ecoff_swap_tir_in, | |
9207 | _bfd_ecoff_swap_rndx_in, | |
9208 | /* Functions to swap out external symbolic data. */ | |
9209 | ecoff_swap_hdr_out, | |
9210 | ecoff_swap_dnr_out, | |
9211 | ecoff_swap_pdr_out, | |
9212 | ecoff_swap_sym_out, | |
9213 | ecoff_swap_opt_out, | |
9214 | ecoff_swap_fdr_out, | |
9215 | ecoff_swap_rfd_out, | |
9216 | ecoff_swap_ext_out, | |
9217 | _bfd_ecoff_swap_tir_out, | |
9218 | _bfd_ecoff_swap_rndx_out, | |
9219 | /* Function to read in symbolic data. */ | |
9220 | _bfd_mips_elf_read_ecoff_info | |
9221 | }; | |
9222 | \f | |
9223 | #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec | |
9224 | #define TARGET_LITTLE_NAME "elf32-littlemips" | |
9225 | #define TARGET_BIG_SYM bfd_elf32_bigmips_vec | |
9226 | #define TARGET_BIG_NAME "elf32-bigmips" | |
9227 | #define ELF_ARCH bfd_arch_mips | |
9228 | #define ELF_MACHINE_CODE EM_MIPS | |
9229 | ||
9230 | /* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses | |
9231 | a value of 0x1000, and we are compatible. */ | |
9232 | #define ELF_MAXPAGESIZE 0x1000 | |
9233 | ||
9234 | #define elf_backend_collect true | |
9235 | #define elf_backend_type_change_ok true | |
9236 | #define elf_backend_can_gc_sections true | |
86dc0f79 | 9237 | #define elf_backend_sign_extend_vma true |
3f830999 | 9238 | #define elf_info_to_howto mips_info_to_howto_rela |
252b5132 RH |
9239 | #define elf_info_to_howto_rel mips_info_to_howto_rel |
9240 | #define elf_backend_sym_is_global mips_elf_sym_is_global | |
103186c6 MM |
9241 | #define elf_backend_object_p _bfd_mips_elf_object_p |
9242 | #define elf_backend_section_from_shdr _bfd_mips_elf_section_from_shdr | |
252b5132 RH |
9243 | #define elf_backend_fake_sections _bfd_mips_elf_fake_sections |
9244 | #define elf_backend_section_from_bfd_section \ | |
9245 | _bfd_mips_elf_section_from_bfd_section | |
103186c6 | 9246 | #define elf_backend_section_processing _bfd_mips_elf_section_processing |
252b5132 RH |
9247 | #define elf_backend_symbol_processing _bfd_mips_elf_symbol_processing |
9248 | #define elf_backend_additional_program_headers \ | |
103186c6 MM |
9249 | _bfd_mips_elf_additional_program_headers |
9250 | #define elf_backend_modify_segment_map _bfd_mips_elf_modify_segment_map | |
252b5132 RH |
9251 | #define elf_backend_final_write_processing \ |
9252 | _bfd_mips_elf_final_write_processing | |
9253 | #define elf_backend_ecoff_debug_swap &mips_elf32_ecoff_debug_swap | |
103186c6 MM |
9254 | #define elf_backend_add_symbol_hook _bfd_mips_elf_add_symbol_hook |
9255 | #define elf_backend_create_dynamic_sections \ | |
9256 | _bfd_mips_elf_create_dynamic_sections | |
9257 | #define elf_backend_check_relocs _bfd_mips_elf_check_relocs | |
9258 | #define elf_backend_adjust_dynamic_symbol \ | |
9259 | _bfd_mips_elf_adjust_dynamic_symbol | |
9260 | #define elf_backend_always_size_sections \ | |
9261 | _bfd_mips_elf_always_size_sections | |
9262 | #define elf_backend_size_dynamic_sections \ | |
9263 | _bfd_mips_elf_size_dynamic_sections | |
9264 | #define elf_backend_relocate_section _bfd_mips_elf_relocate_section | |
9265 | #define elf_backend_link_output_symbol_hook \ | |
9266 | _bfd_mips_elf_link_output_symbol_hook | |
9267 | #define elf_backend_finish_dynamic_symbol \ | |
9268 | _bfd_mips_elf_finish_dynamic_symbol | |
9269 | #define elf_backend_finish_dynamic_sections \ | |
9270 | _bfd_mips_elf_finish_dynamic_sections | |
9271 | #define elf_backend_gc_mark_hook _bfd_mips_elf_gc_mark_hook | |
9272 | #define elf_backend_gc_sweep_hook _bfd_mips_elf_gc_sweep_hook | |
9273 | ||
9274 | #define elf_backend_got_header_size (4*MIPS_RESERVED_GOTNO) | |
9275 | #define elf_backend_plt_header_size 0 | |
252b5132 | 9276 | |
8a20f077 UC |
9277 | #define elf_backend_copy_indirect_symbol \ |
9278 | _bfd_mips_elf_copy_indirect_symbol | |
9279 | ||
b305ef96 UC |
9280 | #define elf_backend_hide_symbol _bfd_mips_elf_hide_symbol |
9281 | ||
252b5132 RH |
9282 | #define bfd_elf32_bfd_is_local_label_name \ |
9283 | mips_elf_is_local_label_name | |
9284 | #define bfd_elf32_find_nearest_line _bfd_mips_elf_find_nearest_line | |
9285 | #define bfd_elf32_set_section_contents _bfd_mips_elf_set_section_contents | |
9286 | #define bfd_elf32_bfd_link_hash_table_create \ | |
103186c6 MM |
9287 | _bfd_mips_elf_link_hash_table_create |
9288 | #define bfd_elf32_bfd_final_link _bfd_mips_elf_final_link | |
252b5132 RH |
9289 | #define bfd_elf32_bfd_copy_private_bfd_data \ |
9290 | _bfd_mips_elf_copy_private_bfd_data | |
9291 | #define bfd_elf32_bfd_merge_private_bfd_data \ | |
9292 | _bfd_mips_elf_merge_private_bfd_data | |
9293 | #define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags | |
9294 | #define bfd_elf32_bfd_print_private_bfd_data \ | |
9295 | _bfd_mips_elf_print_private_bfd_data | |
252b5132 | 9296 | #include "elf32-target.h" |
e364195d UC |
9297 | |
9298 | /* Support for traditional mips targets */ | |
9299 | ||
9300 | #define INCLUDED_TARGET_FILE /* More a type of flag */ | |
9301 | ||
9302 | #undef TARGET_LITTLE_SYM | |
9303 | #undef TARGET_LITTLE_NAME | |
9304 | #undef TARGET_BIG_SYM | |
9305 | #undef TARGET_BIG_NAME | |
9306 | ||
9307 | #define TARGET_LITTLE_SYM bfd_elf32_tradlittlemips_vec | |
9308 | #define TARGET_LITTLE_NAME "elf32-tradlittlemips" | |
9309 | #define TARGET_BIG_SYM bfd_elf32_tradbigmips_vec | |
9310 | #define TARGET_BIG_NAME "elf32-tradbigmips" | |
9311 | ||
9312 | /* Include the target file again for this target */ | |
9313 | #include "elf32-target.h" |