Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* SPARC-specific support for 32-bit ELF |
f0abc2a1 | 2 | Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, |
b2a8e766 | 3 | 2003, 2004 Free Software Foundation, Inc. |
252b5132 | 4 | |
ae9a127f | 5 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 6 | |
ae9a127f NC |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
252b5132 | 11 | |
ae9a127f NC |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
252b5132 | 16 | |
ae9a127f NC |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
252b5132 RH |
20 | |
21 | #include "bfd.h" | |
22 | #include "sysdep.h" | |
23 | #include "bfdlink.h" | |
24 | #include "libbfd.h" | |
25 | #include "elf-bfd.h" | |
26 | #include "elf/sparc.h" | |
f7775d95 | 27 | #include "opcode/sparc.h" |
252b5132 RH |
28 | |
29 | static reloc_howto_type *elf32_sparc_reloc_type_lookup | |
30 | PARAMS ((bfd *, bfd_reloc_code_real_type)); | |
31 | static void elf32_sparc_info_to_howto | |
32 | PARAMS ((bfd *, arelent *, Elf_Internal_Rela *)); | |
b34976b6 | 33 | static bfd_boolean elf32_sparc_check_relocs |
252b5132 RH |
34 | PARAMS ((bfd *, struct bfd_link_info *, asection *, |
35 | const Elf_Internal_Rela *)); | |
b34976b6 | 36 | static bfd_boolean elf32_sparc_adjust_dynamic_symbol |
252b5132 | 37 | PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *)); |
b9734f35 JJ |
38 | static bfd_boolean allocate_dynrelocs |
39 | PARAMS ((struct elf_link_hash_entry *, PTR)); | |
40 | static bfd_boolean readonly_dynrelocs | |
41 | PARAMS ((struct elf_link_hash_entry *, PTR)); | |
151e5294 EB |
42 | static bfd_boolean elf32_sparc_omit_section_dynsym |
43 | PARAMS ((bfd *, struct bfd_link_info *, asection *)); | |
b34976b6 | 44 | static bfd_boolean elf32_sparc_size_dynamic_sections |
252b5132 | 45 | PARAMS ((bfd *, struct bfd_link_info *)); |
f0abc2a1 AM |
46 | static bfd_boolean elf32_sparc_new_section_hook |
47 | PARAMS ((bfd *, asection *)); | |
b34976b6 AM |
48 | static bfd_boolean elf32_sparc_relax_section |
49 | PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *)); | |
b9734f35 JJ |
50 | static bfd_vma dtpoff_base |
51 | PARAMS ((struct bfd_link_info *)); | |
52 | static bfd_vma tpoff | |
53 | PARAMS ((struct bfd_link_info *, bfd_vma)); | |
b34976b6 | 54 | static bfd_boolean elf32_sparc_relocate_section |
252b5132 RH |
55 | PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, |
56 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **)); | |
b34976b6 | 57 | static bfd_boolean elf32_sparc_finish_dynamic_symbol |
252b5132 RH |
58 | PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *, |
59 | Elf_Internal_Sym *)); | |
b34976b6 | 60 | static bfd_boolean elf32_sparc_finish_dynamic_sections |
252b5132 | 61 | PARAMS ((bfd *, struct bfd_link_info *)); |
b34976b6 AM |
62 | static bfd_boolean elf32_sparc_merge_private_bfd_data |
63 | PARAMS ((bfd *, bfd *)); | |
b9734f35 JJ |
64 | static struct bfd_hash_entry *link_hash_newfunc |
65 | PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *)); | |
66 | static struct bfd_link_hash_table *elf32_sparc_link_hash_table_create | |
67 | PARAMS ((bfd *)); | |
26e41594 AM |
68 | static bfd_boolean create_got_section |
69 | PARAMS ((bfd *, struct bfd_link_info *)); | |
b9734f35 JJ |
70 | static bfd_boolean elf32_sparc_create_dynamic_sections |
71 | PARAMS ((bfd *, struct bfd_link_info *)); | |
72 | static void elf32_sparc_copy_indirect_symbol | |
9c5bfbb7 | 73 | PARAMS ((const struct elf_backend_data *, struct elf_link_hash_entry *, |
b9734f35 JJ |
74 | struct elf_link_hash_entry *)); |
75 | static int elf32_sparc_tls_transition | |
76 | PARAMS ((struct bfd_link_info *, bfd *, int, int)); | |
77 | ||
78 | static bfd_boolean elf32_sparc_mkobject | |
79 | PARAMS ((bfd *)); | |
b34976b6 | 80 | static bfd_boolean elf32_sparc_object_p |
252b5132 RH |
81 | PARAMS ((bfd *)); |
82 | static void elf32_sparc_final_write_processing | |
b34976b6 | 83 | PARAMS ((bfd *, bfd_boolean)); |
db6751f2 | 84 | static enum elf_reloc_type_class elf32_sparc_reloc_type_class |
f51e552e | 85 | PARAMS ((const Elf_Internal_Rela *)); |
917583ad | 86 | static asection * elf32_sparc_gc_mark_hook |
1e2f5b6e | 87 | PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *, |
917583ad | 88 | struct elf_link_hash_entry *, Elf_Internal_Sym *)); |
b34976b6 | 89 | static bfd_boolean elf32_sparc_gc_sweep_hook |
917583ad NC |
90 | PARAMS ((bfd *, struct bfd_link_info *, asection *, |
91 | const Elf_Internal_Rela *)); | |
252b5132 RH |
92 | \f |
93 | /* The relocation "howto" table. */ | |
94 | ||
95 | static bfd_reloc_status_type sparc_elf_notsupported_reloc | |
96 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); | |
97 | static bfd_reloc_status_type sparc_elf_wdisp16_reloc | |
98 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); | |
b9734f35 JJ |
99 | static bfd_reloc_status_type sparc_elf_hix22_reloc |
100 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); | |
101 | static bfd_reloc_status_type sparc_elf_lox10_reloc | |
102 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); | |
252b5132 RH |
103 | |
104 | reloc_howto_type _bfd_sparc_elf_howto_table[] = | |
105 | { | |
b34976b6 AM |
106 | HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE), |
107 | HOWTO(R_SPARC_8, 0,0, 8,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_8", FALSE,0,0x000000ff,TRUE), | |
108 | HOWTO(R_SPARC_16, 0,1,16,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_16", FALSE,0,0x0000ffff,TRUE), | |
109 | HOWTO(R_SPARC_32, 0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_32", FALSE,0,0xffffffff,TRUE), | |
110 | HOWTO(R_SPARC_DISP8, 0,0, 8,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP8", FALSE,0,0x000000ff,TRUE), | |
111 | HOWTO(R_SPARC_DISP16, 0,1,16,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP16", FALSE,0,0x0000ffff,TRUE), | |
112 | HOWTO(R_SPARC_DISP32, 0,2,32,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP32", FALSE,0,0xffffffff,TRUE), | |
113 | HOWTO(R_SPARC_WDISP30, 2,2,30,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP30", FALSE,0,0x3fffffff,TRUE), | |
114 | HOWTO(R_SPARC_WDISP22, 2,2,22,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP22", FALSE,0,0x003fffff,TRUE), | |
115 | HOWTO(R_SPARC_HI22, 10,2,22,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_HI22", FALSE,0,0x003fffff,TRUE), | |
116 | HOWTO(R_SPARC_22, 0,2,22,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_22", FALSE,0,0x003fffff,TRUE), | |
117 | HOWTO(R_SPARC_13, 0,2,13,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_13", FALSE,0,0x00001fff,TRUE), | |
118 | HOWTO(R_SPARC_LO10, 0,2,10,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_LO10", FALSE,0,0x000003ff,TRUE), | |
119 | HOWTO(R_SPARC_GOT10, 0,2,10,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT10", FALSE,0,0x000003ff,TRUE), | |
120 | HOWTO(R_SPARC_GOT13, 0,2,13,FALSE,0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_GOT13", FALSE,0,0x00001fff,TRUE), | |
121 | HOWTO(R_SPARC_GOT22, 10,2,22,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT22", FALSE,0,0x003fffff,TRUE), | |
122 | HOWTO(R_SPARC_PC10, 0,2,10,TRUE, 0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_PC10", FALSE,0,0x000003ff,TRUE), | |
123 | HOWTO(R_SPARC_PC22, 10,2,22,TRUE, 0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_PC22", FALSE,0,0x003fffff,TRUE), | |
124 | HOWTO(R_SPARC_WPLT30, 2,2,30,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WPLT30", FALSE,0,0x3fffffff,TRUE), | |
125 | HOWTO(R_SPARC_COPY, 0,0,00,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_COPY", FALSE,0,0x00000000,TRUE), | |
126 | HOWTO(R_SPARC_GLOB_DAT, 0,0,00,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GLOB_DAT",FALSE,0,0x00000000,TRUE), | |
127 | HOWTO(R_SPARC_JMP_SLOT, 0,0,00,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_JMP_SLOT",FALSE,0,0x00000000,TRUE), | |
128 | HOWTO(R_SPARC_RELATIVE, 0,0,00,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_RELATIVE",FALSE,0,0x00000000,TRUE), | |
129 | HOWTO(R_SPARC_UA32, 0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA32", FALSE,0,0xffffffff,TRUE), | |
130 | HOWTO(R_SPARC_PLT32, 0,0,00,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_PLT32", FALSE,0,0xffffffff,TRUE), | |
131 | HOWTO(R_SPARC_HIPLT22, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HIPLT22", FALSE,0,0x00000000,TRUE), | |
132 | HOWTO(R_SPARC_LOPLT10, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_LOPLT10", FALSE,0,0x00000000,TRUE), | |
133 | HOWTO(R_SPARC_PCPLT32, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT32", FALSE,0,0x00000000,TRUE), | |
134 | HOWTO(R_SPARC_PCPLT22, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT22", FALSE,0,0x00000000,TRUE), | |
135 | HOWTO(R_SPARC_PCPLT10, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT10", FALSE,0,0x00000000,TRUE), | |
136 | HOWTO(R_SPARC_10, 0,2,10,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_10", FALSE,0,0x000003ff,TRUE), | |
137 | HOWTO(R_SPARC_11, 0,2,11,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_11", FALSE,0,0x000007ff,TRUE), | |
252b5132 RH |
138 | /* These are for sparc64 in a 64 bit environment. |
139 | Values need to be here because the table is indexed by reloc number. */ | |
b34976b6 AM |
140 | HOWTO(R_SPARC_64, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_64", FALSE,0,0x00000000,TRUE), |
141 | HOWTO(R_SPARC_OLO10, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_OLO10", FALSE,0,0x00000000,TRUE), | |
142 | HOWTO(R_SPARC_HH22, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HH22", FALSE,0,0x00000000,TRUE), | |
143 | HOWTO(R_SPARC_HM10, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HM10", FALSE,0,0x00000000,TRUE), | |
144 | HOWTO(R_SPARC_LM22, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_LM22", FALSE,0,0x00000000,TRUE), | |
145 | HOWTO(R_SPARC_PC_HH22, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PC_HH22", FALSE,0,0x00000000,TRUE), | |
146 | HOWTO(R_SPARC_PC_HM10, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PC_HM10", FALSE,0,0x00000000,TRUE), | |
147 | HOWTO(R_SPARC_PC_LM22, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PC_LM22", FALSE,0,0x00000000,TRUE), | |
252b5132 RH |
148 | /* End sparc64 in 64 bit environment values. |
149 | The following are for sparc64 in a 32 bit environment. */ | |
b34976b6 AM |
150 | HOWTO(R_SPARC_WDISP16, 2,2,16,TRUE, 0,complain_overflow_signed, sparc_elf_wdisp16_reloc,"R_SPARC_WDISP16", FALSE,0,0x00000000,TRUE), |
151 | HOWTO(R_SPARC_WDISP19, 2,2,19,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP19", FALSE,0,0x0007ffff,TRUE), | |
152 | HOWTO(R_SPARC_UNUSED_42, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_UNUSED_42",FALSE,0,0x00000000,TRUE), | |
153 | HOWTO(R_SPARC_7, 0,2, 7,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_7", FALSE,0,0x0000007f,TRUE), | |
154 | HOWTO(R_SPARC_5, 0,2, 5,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_5", FALSE,0,0x0000001f,TRUE), | |
155 | HOWTO(R_SPARC_6, 0,2, 6,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_6", FALSE,0,0x0000003f,TRUE), | |
156 | HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE), | |
157 | HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE), | |
158 | HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE), | |
159 | HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE), | |
160 | HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE), | |
161 | HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE), | |
162 | HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE), | |
163 | HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE), | |
164 | HOWTO(R_SPARC_UA64, 0,0, 0,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_UA64", FALSE,0,0x00000000,TRUE), | |
165 | HOWTO(R_SPARC_UA16, 0,1,16,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA16", FALSE,0,0x0000ffff,TRUE), | |
b9734f35 JJ |
166 | HOWTO(R_SPARC_TLS_GD_HI22,10,2,22,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_GD_HI22",FALSE,0,0x003fffff,TRUE), |
167 | HOWTO(R_SPARC_TLS_GD_LO10,0,2,10,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_GD_LO10",FALSE,0,0x000003ff,TRUE), | |
168 | HOWTO(R_SPARC_TLS_GD_ADD,0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_GD_ADD",FALSE,0,0x00000000,TRUE), | |
169 | HOWTO(R_SPARC_TLS_GD_CALL,2,2,30,TRUE,0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_TLS_GD_CALL",FALSE,0,0x3fffffff,TRUE), | |
170 | HOWTO(R_SPARC_TLS_LDM_HI22,10,2,22,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_LDM_HI22",FALSE,0,0x003fffff,TRUE), | |
171 | HOWTO(R_SPARC_TLS_LDM_LO10,0,2,10,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_LDM_LO10",FALSE,0,0x000003ff,TRUE), | |
172 | HOWTO(R_SPARC_TLS_LDM_ADD,0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_LDM_ADD",FALSE,0,0x00000000,TRUE), | |
173 | HOWTO(R_SPARC_TLS_LDM_CALL,2,2,30,TRUE,0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_TLS_LDM_CALL",FALSE,0,0x3fffffff,TRUE), | |
174 | HOWTO(R_SPARC_TLS_LDO_HIX22,0,2,0,FALSE,0,complain_overflow_bitfield,sparc_elf_hix22_reloc,"R_SPARC_TLS_LDO_HIX22",FALSE,0,0x003fffff, FALSE), | |
175 | HOWTO(R_SPARC_TLS_LDO_LOX10,0,2,0,FALSE,0,complain_overflow_dont, sparc_elf_lox10_reloc, "R_SPARC_TLS_LDO_LOX10",FALSE,0,0x000003ff, FALSE), | |
176 | HOWTO(R_SPARC_TLS_LDO_ADD,0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_LDO_ADD",FALSE,0,0x00000000,TRUE), | |
177 | HOWTO(R_SPARC_TLS_IE_HI22,10,2,22,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_IE_HI22",FALSE,0,0x003fffff,TRUE), | |
178 | HOWTO(R_SPARC_TLS_IE_LO10,0,2,10,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_IE_LO10",FALSE,0,0x000003ff,TRUE), | |
179 | HOWTO(R_SPARC_TLS_IE_LD,0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_IE_LD",FALSE,0,0x00000000,TRUE), | |
180 | HOWTO(R_SPARC_TLS_IE_LDX,0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_IE_LDX",FALSE,0,0x00000000,TRUE), | |
181 | HOWTO(R_SPARC_TLS_IE_ADD,0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_IE_ADD",FALSE,0,0x00000000,TRUE), | |
182 | HOWTO(R_SPARC_TLS_LE_HIX22,0,2,0,FALSE,0,complain_overflow_bitfield,sparc_elf_hix22_reloc, "R_SPARC_TLS_LE_HIX22",FALSE,0,0x003fffff, FALSE), | |
183 | HOWTO(R_SPARC_TLS_LE_LOX10,0,2,0,FALSE,0,complain_overflow_dont, sparc_elf_lox10_reloc, "R_SPARC_TLS_LE_LOX10",FALSE,0,0x000003ff, FALSE), | |
184 | HOWTO(R_SPARC_TLS_DTPMOD32,0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_DTPMOD32",FALSE,0,0x00000000,TRUE), | |
185 | HOWTO(R_SPARC_TLS_DTPMOD64,0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_DTPMOD64",FALSE,0,0x00000000,TRUE), | |
186 | HOWTO(R_SPARC_TLS_DTPOFF32,0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_TLS_DTPOFF32",FALSE,0,0xffffffff,TRUE), | |
187 | HOWTO(R_SPARC_TLS_DTPOFF64,0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_DTPOFF64",FALSE,0,0x00000000,TRUE), | |
188 | HOWTO(R_SPARC_TLS_TPOFF32,0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_TPOFF32",FALSE,0,0x00000000,TRUE), | |
189 | HOWTO(R_SPARC_TLS_TPOFF64,0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_TPOFF64",FALSE,0,0x00000000,TRUE) | |
252b5132 RH |
190 | }; |
191 | static reloc_howto_type elf32_sparc_vtinherit_howto = | |
b34976b6 | 192 | HOWTO (R_SPARC_GNU_VTINHERIT, 0,2,0,FALSE,0,complain_overflow_dont, NULL, "R_SPARC_GNU_VTINHERIT", FALSE,0, 0, FALSE); |
252b5132 | 193 | static reloc_howto_type elf32_sparc_vtentry_howto = |
b34976b6 | 194 | HOWTO (R_SPARC_GNU_VTENTRY, 0,2,0,FALSE,0,complain_overflow_dont, _bfd_elf_rel_vtable_reloc_fn,"R_SPARC_GNU_VTENTRY", FALSE,0,0, FALSE); |
b9734f35 JJ |
195 | static reloc_howto_type elf32_sparc_rev32_howto = |
196 | HOWTO(R_SPARC_REV32, 0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_REV32", FALSE,0,0xffffffff,TRUE); | |
252b5132 | 197 | |
252b5132 RH |
198 | struct elf_reloc_map { |
199 | bfd_reloc_code_real_type bfd_reloc_val; | |
200 | unsigned char elf_reloc_val; | |
201 | }; | |
202 | ||
dc810e39 | 203 | static const struct elf_reloc_map sparc_reloc_map[] = |
252b5132 RH |
204 | { |
205 | { BFD_RELOC_NONE, R_SPARC_NONE, }, | |
206 | { BFD_RELOC_16, R_SPARC_16, }, | |
bd5e6e7e | 207 | { BFD_RELOC_16_PCREL, R_SPARC_DISP16 }, |
252b5132 RH |
208 | { BFD_RELOC_8, R_SPARC_8 }, |
209 | { BFD_RELOC_8_PCREL, R_SPARC_DISP8 }, | |
210 | { BFD_RELOC_CTOR, R_SPARC_32 }, | |
211 | { BFD_RELOC_32, R_SPARC_32 }, | |
212 | { BFD_RELOC_32_PCREL, R_SPARC_DISP32 }, | |
213 | { BFD_RELOC_HI22, R_SPARC_HI22 }, | |
214 | { BFD_RELOC_LO10, R_SPARC_LO10, }, | |
215 | { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 }, | |
bd5e6e7e | 216 | { BFD_RELOC_SPARC_PLT32, R_SPARC_PLT32 }, |
252b5132 RH |
217 | { BFD_RELOC_SPARC22, R_SPARC_22 }, |
218 | { BFD_RELOC_SPARC13, R_SPARC_13 }, | |
219 | { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 }, | |
220 | { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 }, | |
221 | { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 }, | |
222 | { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 }, | |
223 | { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 }, | |
224 | { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 }, | |
225 | { BFD_RELOC_SPARC_COPY, R_SPARC_COPY }, | |
226 | { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT }, | |
227 | { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT }, | |
228 | { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE }, | |
229 | { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 }, | |
0f2712ed NC |
230 | { BFD_RELOC_SPARC_UA16, R_SPARC_UA16 }, |
231 | { BFD_RELOC_SPARC_UA32, R_SPARC_UA32 }, | |
232 | { BFD_RELOC_SPARC_UA64, R_SPARC_UA64 }, | |
233 | { BFD_RELOC_SPARC_10, R_SPARC_10 }, | |
234 | { BFD_RELOC_SPARC_11, R_SPARC_11 }, | |
235 | { BFD_RELOC_SPARC_64, R_SPARC_64 }, | |
236 | { BFD_RELOC_SPARC_OLO10, R_SPARC_OLO10 }, | |
237 | { BFD_RELOC_SPARC_HH22, R_SPARC_HH22 }, | |
238 | { BFD_RELOC_SPARC_HM10, R_SPARC_HM10 }, | |
239 | { BFD_RELOC_SPARC_LM22, R_SPARC_LM22 }, | |
240 | { BFD_RELOC_SPARC_PC_HH22, R_SPARC_PC_HH22 }, | |
241 | { BFD_RELOC_SPARC_PC_HM10, R_SPARC_PC_HM10 }, | |
242 | { BFD_RELOC_SPARC_PC_LM22, R_SPARC_PC_LM22 }, | |
243 | { BFD_RELOC_SPARC_WDISP16, R_SPARC_WDISP16 }, | |
244 | { BFD_RELOC_SPARC_WDISP19, R_SPARC_WDISP19 }, | |
245 | { BFD_RELOC_SPARC_7, R_SPARC_7 }, | |
246 | { BFD_RELOC_SPARC_5, R_SPARC_5 }, | |
247 | { BFD_RELOC_SPARC_6, R_SPARC_6 }, | |
b9734f35 JJ |
248 | { BFD_RELOC_SPARC_TLS_GD_HI22, R_SPARC_TLS_GD_HI22 }, |
249 | { BFD_RELOC_SPARC_TLS_GD_LO10, R_SPARC_TLS_GD_LO10 }, | |
250 | { BFD_RELOC_SPARC_TLS_GD_ADD, R_SPARC_TLS_GD_ADD }, | |
251 | { BFD_RELOC_SPARC_TLS_GD_CALL, R_SPARC_TLS_GD_CALL }, | |
252 | { BFD_RELOC_SPARC_TLS_LDM_HI22, R_SPARC_TLS_LDM_HI22 }, | |
253 | { BFD_RELOC_SPARC_TLS_LDM_LO10, R_SPARC_TLS_LDM_LO10 }, | |
254 | { BFD_RELOC_SPARC_TLS_LDM_ADD, R_SPARC_TLS_LDM_ADD }, | |
255 | { BFD_RELOC_SPARC_TLS_LDM_CALL, R_SPARC_TLS_LDM_CALL }, | |
256 | { BFD_RELOC_SPARC_TLS_LDO_HIX22, R_SPARC_TLS_LDO_HIX22 }, | |
257 | { BFD_RELOC_SPARC_TLS_LDO_LOX10, R_SPARC_TLS_LDO_LOX10 }, | |
258 | { BFD_RELOC_SPARC_TLS_LDO_ADD, R_SPARC_TLS_LDO_ADD }, | |
259 | { BFD_RELOC_SPARC_TLS_IE_HI22, R_SPARC_TLS_IE_HI22 }, | |
260 | { BFD_RELOC_SPARC_TLS_IE_LO10, R_SPARC_TLS_IE_LO10 }, | |
261 | { BFD_RELOC_SPARC_TLS_IE_LD, R_SPARC_TLS_IE_LD }, | |
262 | { BFD_RELOC_SPARC_TLS_IE_LDX, R_SPARC_TLS_IE_LDX }, | |
263 | { BFD_RELOC_SPARC_TLS_IE_ADD, R_SPARC_TLS_IE_ADD }, | |
264 | { BFD_RELOC_SPARC_TLS_LE_HIX22, R_SPARC_TLS_LE_HIX22 }, | |
265 | { BFD_RELOC_SPARC_TLS_LE_LOX10, R_SPARC_TLS_LE_LOX10 }, | |
266 | { BFD_RELOC_SPARC_TLS_DTPMOD32, R_SPARC_TLS_DTPMOD32 }, | |
267 | { BFD_RELOC_SPARC_TLS_DTPMOD64, R_SPARC_TLS_DTPMOD64 }, | |
268 | { BFD_RELOC_SPARC_TLS_DTPOFF32, R_SPARC_TLS_DTPOFF32 }, | |
269 | { BFD_RELOC_SPARC_TLS_DTPOFF64, R_SPARC_TLS_DTPOFF64 }, | |
270 | { BFD_RELOC_SPARC_TLS_TPOFF32, R_SPARC_TLS_TPOFF32 }, | |
271 | { BFD_RELOC_SPARC_TLS_TPOFF64, R_SPARC_TLS_TPOFF64 }, | |
0f2712ed NC |
272 | { BFD_RELOC_VTABLE_INHERIT, R_SPARC_GNU_VTINHERIT }, |
273 | { BFD_RELOC_VTABLE_ENTRY, R_SPARC_GNU_VTENTRY }, | |
b9734f35 | 274 | { BFD_RELOC_SPARC_REV32, R_SPARC_REV32 } |
252b5132 RH |
275 | }; |
276 | ||
277 | static reloc_howto_type * | |
278 | elf32_sparc_reloc_type_lookup (abfd, code) | |
5f771d47 | 279 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
280 | bfd_reloc_code_real_type code; |
281 | { | |
282 | unsigned int i; | |
435b1e90 | 283 | |
252b5132 RH |
284 | switch (code) |
285 | { | |
286 | case BFD_RELOC_VTABLE_INHERIT: | |
287 | return &elf32_sparc_vtinherit_howto; | |
288 | ||
289 | case BFD_RELOC_VTABLE_ENTRY: | |
290 | return &elf32_sparc_vtentry_howto; | |
291 | ||
b9734f35 JJ |
292 | case BFD_RELOC_SPARC_REV32: |
293 | return &elf32_sparc_rev32_howto; | |
294 | ||
252b5132 | 295 | default: |
26e41594 AM |
296 | for (i = 0; |
297 | i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map); | |
298 | i++) | |
299 | { | |
300 | if (sparc_reloc_map[i].bfd_reloc_val == code) | |
301 | return (_bfd_sparc_elf_howto_table | |
302 | + (int) sparc_reloc_map[i].elf_reloc_val); | |
303 | } | |
252b5132 RH |
304 | } |
305 | bfd_set_error (bfd_error_bad_value); | |
306 | return NULL; | |
307 | } | |
308 | ||
309 | /* We need to use ELF32_R_TYPE so we have our own copy of this function, | |
310 | and elf64-sparc.c has its own copy. */ | |
311 | ||
312 | static void | |
313 | elf32_sparc_info_to_howto (abfd, cache_ptr, dst) | |
5f771d47 | 314 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
315 | arelent *cache_ptr; |
316 | Elf_Internal_Rela *dst; | |
317 | { | |
60dac299 RH |
318 | switch (ELF32_R_TYPE(dst->r_info)) |
319 | { | |
320 | case R_SPARC_GNU_VTINHERIT: | |
321 | cache_ptr->howto = &elf32_sparc_vtinherit_howto; | |
322 | break; | |
323 | ||
324 | case R_SPARC_GNU_VTENTRY: | |
325 | cache_ptr->howto = &elf32_sparc_vtentry_howto; | |
326 | break; | |
327 | ||
b9734f35 JJ |
328 | case R_SPARC_REV32: |
329 | cache_ptr->howto = &elf32_sparc_rev32_howto; | |
330 | break; | |
331 | ||
60dac299 RH |
332 | default: |
333 | BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_SPARC_max_std); | |
334 | cache_ptr->howto = &_bfd_sparc_elf_howto_table[ELF32_R_TYPE(dst->r_info)]; | |
335 | } | |
252b5132 RH |
336 | } |
337 | \f | |
338 | /* For unsupported relocs. */ | |
339 | ||
340 | static bfd_reloc_status_type | |
341 | sparc_elf_notsupported_reloc (abfd, | |
342 | reloc_entry, | |
343 | symbol, | |
344 | data, | |
345 | input_section, | |
346 | output_bfd, | |
347 | error_message) | |
5f771d47 ILT |
348 | bfd *abfd ATTRIBUTE_UNUSED; |
349 | arelent *reloc_entry ATTRIBUTE_UNUSED; | |
350 | asymbol *symbol ATTRIBUTE_UNUSED; | |
351 | PTR data ATTRIBUTE_UNUSED; | |
352 | asection *input_section ATTRIBUTE_UNUSED; | |
353 | bfd *output_bfd ATTRIBUTE_UNUSED; | |
354 | char **error_message ATTRIBUTE_UNUSED; | |
252b5132 RH |
355 | { |
356 | return bfd_reloc_notsupported; | |
357 | } | |
358 | ||
359 | /* Handle the WDISP16 reloc. */ | |
360 | ||
361 | static bfd_reloc_status_type | |
362 | sparc_elf_wdisp16_reloc (abfd, | |
363 | reloc_entry, | |
364 | symbol, | |
365 | data, | |
366 | input_section, | |
367 | output_bfd, | |
368 | error_message) | |
369 | bfd *abfd; | |
370 | arelent *reloc_entry; | |
371 | asymbol *symbol; | |
372 | PTR data; | |
373 | asection *input_section; | |
374 | bfd *output_bfd; | |
5f771d47 | 375 | char **error_message ATTRIBUTE_UNUSED; |
252b5132 RH |
376 | { |
377 | bfd_vma relocation; | |
378 | bfd_vma x; | |
379 | ||
380 | if (output_bfd != (bfd *) NULL | |
381 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
382 | && (! reloc_entry->howto->partial_inplace | |
383 | || reloc_entry->addend == 0)) | |
384 | { | |
385 | reloc_entry->address += input_section->output_offset; | |
386 | return bfd_reloc_ok; | |
387 | } | |
388 | ||
389 | if (output_bfd != NULL) | |
390 | return bfd_reloc_continue; | |
391 | ||
07515404 | 392 | if (reloc_entry->address > bfd_get_section_limit (abfd, input_section)) |
252b5132 RH |
393 | return bfd_reloc_outofrange; |
394 | ||
395 | relocation = (symbol->value | |
396 | + symbol->section->output_section->vma | |
397 | + symbol->section->output_offset); | |
398 | relocation += reloc_entry->addend; | |
399 | relocation -= (input_section->output_section->vma | |
400 | + input_section->output_offset); | |
401 | relocation -= reloc_entry->address; | |
402 | ||
403 | x = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address); | |
404 | x |= ((((relocation >> 2) & 0xc000) << 6) | |
405 | | ((relocation >> 2) & 0x3fff)); | |
406 | bfd_put_32 (abfd, x, (bfd_byte *) data + reloc_entry->address); | |
407 | ||
408 | if ((bfd_signed_vma) relocation < - 0x40000 | |
409 | || (bfd_signed_vma) relocation > 0x3ffff) | |
410 | return bfd_reloc_overflow; | |
411 | else | |
412 | return bfd_reloc_ok; | |
413 | } | |
b9734f35 JJ |
414 | |
415 | /* Handle the HIX22 reloc. */ | |
416 | ||
417 | static bfd_reloc_status_type | |
418 | sparc_elf_hix22_reloc (abfd, | |
419 | reloc_entry, | |
420 | symbol, | |
421 | data, | |
422 | input_section, | |
423 | output_bfd, | |
424 | error_message) | |
425 | bfd *abfd; | |
426 | arelent *reloc_entry; | |
427 | asymbol *symbol; | |
428 | PTR data; | |
429 | asection *input_section; | |
430 | bfd *output_bfd; | |
431 | char **error_message ATTRIBUTE_UNUSED; | |
432 | { | |
433 | bfd_vma relocation; | |
434 | bfd_vma insn; | |
435 | ||
436 | if (output_bfd != (bfd *) NULL | |
437 | && (symbol->flags & BSF_SECTION_SYM) == 0) | |
438 | { | |
439 | reloc_entry->address += input_section->output_offset; | |
440 | return bfd_reloc_ok; | |
441 | } | |
442 | ||
443 | if (output_bfd != NULL) | |
444 | return bfd_reloc_continue; | |
445 | ||
07515404 | 446 | if (reloc_entry->address > bfd_get_section_limit (abfd, input_section)) |
b9734f35 JJ |
447 | return bfd_reloc_outofrange; |
448 | ||
449 | relocation = (symbol->value | |
450 | + symbol->section->output_section->vma | |
451 | + symbol->section->output_offset); | |
452 | relocation += reloc_entry->addend; | |
453 | insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address); | |
454 | ||
455 | insn = (insn &~ (bfd_vma) 0x3fffff) | (((~relocation) >> 10) & 0x3fffff); | |
456 | bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address); | |
457 | ||
458 | return bfd_reloc_ok; | |
459 | } | |
460 | ||
461 | /* Handle the LOX10 reloc. */ | |
462 | ||
463 | static bfd_reloc_status_type | |
464 | sparc_elf_lox10_reloc (abfd, | |
465 | reloc_entry, | |
466 | symbol, | |
467 | data, | |
468 | input_section, | |
469 | output_bfd, | |
470 | error_message) | |
471 | bfd *abfd; | |
472 | arelent *reloc_entry; | |
473 | asymbol *symbol; | |
474 | PTR data; | |
475 | asection *input_section; | |
476 | bfd *output_bfd; | |
477 | char **error_message ATTRIBUTE_UNUSED; | |
478 | { | |
479 | bfd_vma relocation; | |
480 | bfd_vma insn; | |
481 | ||
482 | if (output_bfd != (bfd *) NULL | |
483 | && (symbol->flags & BSF_SECTION_SYM) == 0) | |
484 | { | |
485 | reloc_entry->address += input_section->output_offset; | |
486 | return bfd_reloc_ok; | |
487 | } | |
488 | ||
489 | if (output_bfd != NULL) | |
490 | return bfd_reloc_continue; | |
491 | ||
07515404 | 492 | if (reloc_entry->address > bfd_get_section_limit (abfd, input_section)) |
b9734f35 JJ |
493 | return bfd_reloc_outofrange; |
494 | ||
495 | relocation = (symbol->value | |
496 | + symbol->section->output_section->vma | |
497 | + symbol->section->output_offset); | |
498 | relocation += reloc_entry->addend; | |
499 | insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address); | |
500 | ||
501 | insn = (insn &~ (bfd_vma) 0x1fff) | 0x1c00 | (relocation & 0x3ff); | |
502 | bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address); | |
503 | ||
504 | return bfd_reloc_ok; | |
505 | } | |
0647ed71 MK |
506 | \f |
507 | /* Support for core dump NOTE sections. */ | |
508 | ||
509 | static bfd_boolean | |
510 | elf32_sparc_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) | |
511 | { | |
512 | switch (note->descsz) | |
513 | { | |
514 | default: | |
515 | return FALSE; | |
516 | ||
517 | case 260: /* Solaris prpsinfo_t. */ | |
518 | elf_tdata (abfd)->core_program | |
519 | = _bfd_elfcore_strndup (abfd, note->descdata + 84, 16); | |
520 | elf_tdata (abfd)->core_command | |
521 | = _bfd_elfcore_strndup (abfd, note->descdata + 100, 80); | |
522 | break; | |
523 | ||
524 | case 336: /* Solaris psinfo_t. */ | |
525 | elf_tdata (abfd)->core_program | |
526 | = _bfd_elfcore_strndup (abfd, note->descdata + 88, 16); | |
527 | elf_tdata (abfd)->core_command | |
528 | = _bfd_elfcore_strndup (abfd, note->descdata + 104, 80); | |
529 | break; | |
530 | } | |
531 | ||
532 | return TRUE; | |
533 | } | |
534 | ||
252b5132 RH |
535 | \f |
536 | /* Functions for the SPARC ELF linker. */ | |
537 | ||
538 | /* The name of the dynamic interpreter. This is put in the .interp | |
539 | section. */ | |
540 | ||
541 | #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1" | |
542 | ||
543 | /* The nop opcode we use. */ | |
544 | ||
545 | #define SPARC_NOP 0x01000000 | |
546 | ||
547 | /* The size in bytes of an entry in the procedure linkage table. */ | |
548 | ||
549 | #define PLT_ENTRY_SIZE 12 | |
550 | ||
551 | /* The first four entries in a procedure linkage table are reserved, | |
552 | and the initial contents are unimportant (we zero them out). | |
553 | Subsequent entries look like this. See the SVR4 ABI SPARC | |
554 | supplement to see how this works. */ | |
555 | ||
556 | /* sethi %hi(.-.plt0),%g1. We fill in the address later. */ | |
557 | #define PLT_ENTRY_WORD0 0x03000000 | |
558 | /* b,a .plt0. We fill in the offset later. */ | |
559 | #define PLT_ENTRY_WORD1 0x30800000 | |
560 | /* nop. */ | |
561 | #define PLT_ENTRY_WORD2 SPARC_NOP | |
562 | ||
b9734f35 JJ |
563 | /* The SPARC linker needs to keep track of the number of relocs that it |
564 | decides to copy as dynamic relocs in check_relocs for each symbol. | |
565 | This is so that it can later discard them if they are found to be | |
566 | unnecessary. We store the information in a field extending the | |
567 | regular ELF linker hash table. */ | |
568 | ||
569 | struct elf32_sparc_dyn_relocs | |
570 | { | |
571 | struct elf32_sparc_dyn_relocs *next; | |
572 | ||
573 | /* The input section of the reloc. */ | |
574 | asection *sec; | |
575 | ||
576 | /* Total number of relocs copied for the input section. */ | |
577 | bfd_size_type count; | |
578 | ||
579 | /* Number of pc-relative relocs copied for the input section. */ | |
580 | bfd_size_type pc_count; | |
581 | }; | |
582 | ||
583 | /* SPARC ELF linker hash entry. */ | |
584 | ||
585 | struct elf32_sparc_link_hash_entry | |
586 | { | |
587 | struct elf_link_hash_entry elf; | |
588 | ||
589 | /* Track dynamic relocs copied for this symbol. */ | |
590 | struct elf32_sparc_dyn_relocs *dyn_relocs; | |
591 | ||
592 | #define GOT_UNKNOWN 0 | |
593 | #define GOT_NORMAL 1 | |
594 | #define GOT_TLS_GD 2 | |
595 | #define GOT_TLS_IE 3 | |
596 | unsigned char tls_type; | |
597 | }; | |
598 | ||
599 | #define elf32_sparc_hash_entry(ent) ((struct elf32_sparc_link_hash_entry *)(ent)) | |
600 | ||
601 | struct elf32_sparc_obj_tdata | |
602 | { | |
603 | struct elf_obj_tdata root; | |
604 | ||
605 | /* tls_type for each local got entry. */ | |
606 | char *local_got_tls_type; | |
607 | ||
608 | /* TRUE if TLS GD relocs has been seen for this object. */ | |
609 | bfd_boolean has_tlsgd; | |
610 | }; | |
611 | ||
612 | #define elf32_sparc_tdata(abfd) \ | |
613 | ((struct elf32_sparc_obj_tdata *) (abfd)->tdata.any) | |
614 | ||
615 | #define elf32_sparc_local_got_tls_type(abfd) \ | |
616 | (elf32_sparc_tdata (abfd)->local_got_tls_type) | |
617 | ||
618 | static bfd_boolean | |
619 | elf32_sparc_mkobject (abfd) | |
620 | bfd *abfd; | |
621 | { | |
622 | bfd_size_type amt = sizeof (struct elf32_sparc_obj_tdata); | |
623 | abfd->tdata.any = bfd_zalloc (abfd, amt); | |
624 | if (abfd->tdata.any == NULL) | |
625 | return FALSE; | |
626 | return TRUE; | |
627 | } | |
628 | ||
629 | /* SPARC ELF linker hash table. */ | |
630 | ||
631 | struct elf32_sparc_link_hash_table | |
632 | { | |
633 | struct elf_link_hash_table elf; | |
634 | ||
635 | /* Short-cuts to get to dynamic linker sections. */ | |
636 | asection *sgot; | |
637 | asection *srelgot; | |
638 | asection *splt; | |
639 | asection *srelplt; | |
640 | asection *sdynbss; | |
641 | asection *srelbss; | |
642 | ||
643 | union { | |
644 | bfd_signed_vma refcount; | |
645 | bfd_vma offset; | |
646 | } tls_ldm_got; | |
647 | ||
648 | /* Small local sym to section mapping cache. */ | |
649 | struct sym_sec_cache sym_sec; | |
650 | }; | |
651 | ||
652 | /* Get the SPARC ELF linker hash table from a link_info structure. */ | |
653 | ||
654 | #define elf32_sparc_hash_table(p) \ | |
655 | ((struct elf32_sparc_link_hash_table *) ((p)->hash)) | |
656 | ||
657 | /* Create an entry in an i386 ELF linker hash table. */ | |
658 | ||
659 | static struct bfd_hash_entry * | |
660 | link_hash_newfunc (entry, table, string) | |
661 | struct bfd_hash_entry *entry; | |
662 | struct bfd_hash_table *table; | |
663 | const char *string; | |
664 | { | |
665 | /* Allocate the structure if it has not already been allocated by a | |
666 | subclass. */ | |
667 | if (entry == NULL) | |
668 | { | |
669 | entry = bfd_hash_allocate (table, | |
26e41594 | 670 | sizeof (struct elf32_sparc_link_hash_entry)); |
b9734f35 | 671 | if (entry == NULL) |
26e41594 | 672 | return entry; |
b9734f35 JJ |
673 | } |
674 | ||
675 | /* Call the allocation method of the superclass. */ | |
676 | entry = _bfd_elf_link_hash_newfunc (entry, table, string); | |
677 | if (entry != NULL) | |
678 | { | |
679 | struct elf32_sparc_link_hash_entry *eh; | |
680 | ||
681 | eh = (struct elf32_sparc_link_hash_entry *) entry; | |
682 | eh->dyn_relocs = NULL; | |
683 | eh->tls_type = GOT_UNKNOWN; | |
684 | } | |
685 | ||
686 | return entry; | |
687 | } | |
688 | ||
689 | /* Create a SPARC ELF linker hash table. */ | |
690 | ||
691 | static struct bfd_link_hash_table * | |
692 | elf32_sparc_link_hash_table_create (abfd) | |
693 | bfd *abfd; | |
694 | { | |
695 | struct elf32_sparc_link_hash_table *ret; | |
696 | bfd_size_type amt = sizeof (struct elf32_sparc_link_hash_table); | |
697 | ||
01aac484 | 698 | ret = (struct elf32_sparc_link_hash_table *) bfd_zmalloc (amt); |
b9734f35 JJ |
699 | if (ret == NULL) |
700 | return NULL; | |
701 | ||
702 | if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc)) | |
703 | { | |
704 | free (ret); | |
705 | return NULL; | |
706 | } | |
707 | ||
b9734f35 JJ |
708 | return &ret->elf.root; |
709 | } | |
710 | ||
711 | /* Create .got and .rela.got sections in DYNOBJ, and set up | |
712 | shortcuts to them in our hash table. */ | |
713 | ||
714 | static bfd_boolean | |
715 | create_got_section (dynobj, info) | |
716 | bfd *dynobj; | |
717 | struct bfd_link_info *info; | |
718 | { | |
719 | struct elf32_sparc_link_hash_table *htab; | |
720 | ||
721 | if (! _bfd_elf_create_got_section (dynobj, info)) | |
722 | return FALSE; | |
723 | ||
724 | htab = elf32_sparc_hash_table (info); | |
725 | htab->sgot = bfd_get_section_by_name (dynobj, ".got"); | |
01aac484 | 726 | BFD_ASSERT (htab->sgot != NULL); |
b9734f35 JJ |
727 | |
728 | htab->srelgot = bfd_make_section (dynobj, ".rela.got"); | |
729 | if (htab->srelgot == NULL | |
01aac484 EB |
730 | || ! bfd_set_section_flags (dynobj, htab->srelgot, SEC_ALLOC |
731 | | SEC_LOAD | |
732 | | SEC_HAS_CONTENTS | |
733 | | SEC_IN_MEMORY | |
734 | | SEC_LINKER_CREATED | |
735 | | SEC_READONLY) | |
b9734f35 JJ |
736 | || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2)) |
737 | return FALSE; | |
738 | return TRUE; | |
739 | } | |
740 | ||
741 | /* Create .plt, .rela.plt, .got, .rela.got, .dynbss, and | |
742 | .rela.bss sections in DYNOBJ, and set up shortcuts to them in our | |
743 | hash table. */ | |
744 | ||
745 | static bfd_boolean | |
746 | elf32_sparc_create_dynamic_sections (dynobj, info) | |
747 | bfd *dynobj; | |
748 | struct bfd_link_info *info; | |
749 | { | |
750 | struct elf32_sparc_link_hash_table *htab; | |
751 | ||
752 | htab = elf32_sparc_hash_table (info); | |
753 | if (!htab->sgot && !create_got_section (dynobj, info)) | |
754 | return FALSE; | |
755 | ||
756 | if (!_bfd_elf_create_dynamic_sections (dynobj, info)) | |
757 | return FALSE; | |
758 | ||
759 | htab->splt = bfd_get_section_by_name (dynobj, ".plt"); | |
760 | htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt"); | |
761 | htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss"); | |
762 | if (!info->shared) | |
763 | htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss"); | |
764 | ||
765 | if (!htab->splt || !htab->srelplt || !htab->sdynbss | |
766 | || (!info->shared && !htab->srelbss)) | |
767 | abort (); | |
768 | ||
769 | return TRUE; | |
770 | } | |
771 | ||
772 | /* Copy the extra info we tack onto an elf_link_hash_entry. */ | |
773 | ||
774 | static void | |
775 | elf32_sparc_copy_indirect_symbol (bed, dir, ind) | |
9c5bfbb7 | 776 | const struct elf_backend_data *bed; |
b9734f35 JJ |
777 | struct elf_link_hash_entry *dir, *ind; |
778 | { | |
779 | struct elf32_sparc_link_hash_entry *edir, *eind; | |
780 | ||
781 | edir = (struct elf32_sparc_link_hash_entry *) dir; | |
782 | eind = (struct elf32_sparc_link_hash_entry *) ind; | |
783 | ||
784 | if (eind->dyn_relocs != NULL) | |
785 | { | |
786 | if (edir->dyn_relocs != NULL) | |
26e41594 AM |
787 | { |
788 | struct elf32_sparc_dyn_relocs **pp; | |
789 | struct elf32_sparc_dyn_relocs *p; | |
790 | ||
791 | if (ind->root.type == bfd_link_hash_indirect) | |
792 | abort (); | |
793 | ||
794 | /* Add reloc counts against the weak sym to the strong sym | |
795 | list. Merge any entries against the same section. */ | |
796 | for (pp = &eind->dyn_relocs; (p = *pp) != NULL; ) | |
797 | { | |
798 | struct elf32_sparc_dyn_relocs *q; | |
799 | ||
800 | for (q = edir->dyn_relocs; q != NULL; q = q->next) | |
801 | if (q->sec == p->sec) | |
802 | { | |
803 | q->pc_count += p->pc_count; | |
804 | q->count += p->count; | |
805 | *pp = p->next; | |
806 | break; | |
807 | } | |
808 | if (q == NULL) | |
809 | pp = &p->next; | |
810 | } | |
811 | *pp = edir->dyn_relocs; | |
812 | } | |
b9734f35 JJ |
813 | |
814 | edir->dyn_relocs = eind->dyn_relocs; | |
815 | eind->dyn_relocs = NULL; | |
816 | } | |
817 | ||
818 | if (ind->root.type == bfd_link_hash_indirect | |
819 | && dir->got.refcount <= 0) | |
820 | { | |
821 | edir->tls_type = eind->tls_type; | |
822 | eind->tls_type = GOT_UNKNOWN; | |
823 | } | |
824 | _bfd_elf_link_hash_copy_indirect (bed, dir, ind); | |
825 | } | |
826 | ||
827 | static int | |
828 | elf32_sparc_tls_transition (info, abfd, r_type, is_local) | |
829 | struct bfd_link_info *info; | |
830 | bfd *abfd; | |
831 | int r_type; | |
832 | int is_local; | |
833 | { | |
834 | if (r_type == R_SPARC_TLS_GD_HI22 | |
835 | && ! elf32_sparc_tdata (abfd)->has_tlsgd) | |
836 | r_type = R_SPARC_REV32; | |
837 | ||
838 | if (info->shared) | |
839 | return r_type; | |
840 | ||
841 | switch (r_type) | |
842 | { | |
843 | case R_SPARC_TLS_GD_HI22: | |
844 | if (is_local) | |
845 | return R_SPARC_TLS_LE_HIX22; | |
846 | return R_SPARC_TLS_IE_HI22; | |
847 | case R_SPARC_TLS_GD_LO10: | |
848 | if (is_local) | |
26e41594 | 849 | return R_SPARC_TLS_LE_LOX10; |
b9734f35 JJ |
850 | return R_SPARC_TLS_IE_LO10; |
851 | case R_SPARC_TLS_IE_HI22: | |
852 | if (is_local) | |
26e41594 | 853 | return R_SPARC_TLS_LE_HIX22; |
b9734f35 JJ |
854 | return r_type; |
855 | case R_SPARC_TLS_IE_LO10: | |
856 | if (is_local) | |
26e41594 | 857 | return R_SPARC_TLS_LE_LOX10; |
b9734f35 JJ |
858 | return r_type; |
859 | case R_SPARC_TLS_LDM_HI22: | |
860 | return R_SPARC_TLS_LE_HIX22; | |
861 | case R_SPARC_TLS_LDM_LO10: | |
862 | return R_SPARC_TLS_LE_LOX10; | |
863 | } | |
864 | ||
865 | return r_type; | |
866 | } | |
867 | ||
252b5132 RH |
868 | /* Look through the relocs for a section during the first phase, and |
869 | allocate space in the global offset table or procedure linkage | |
870 | table. */ | |
871 | ||
b34976b6 | 872 | static bfd_boolean |
252b5132 RH |
873 | elf32_sparc_check_relocs (abfd, info, sec, relocs) |
874 | bfd *abfd; | |
875 | struct bfd_link_info *info; | |
876 | asection *sec; | |
877 | const Elf_Internal_Rela *relocs; | |
878 | { | |
b9734f35 | 879 | struct elf32_sparc_link_hash_table *htab; |
252b5132 RH |
880 | Elf_Internal_Shdr *symtab_hdr; |
881 | struct elf_link_hash_entry **sym_hashes; | |
882 | bfd_vma *local_got_offsets; | |
883 | const Elf_Internal_Rela *rel; | |
884 | const Elf_Internal_Rela *rel_end; | |
252b5132 | 885 | asection *sreloc; |
b9734f35 | 886 | bfd_boolean checked_tlsgd = FALSE; |
252b5132 | 887 | |
1049f94e | 888 | if (info->relocatable) |
b34976b6 | 889 | return TRUE; |
252b5132 | 890 | |
b9734f35 | 891 | htab = elf32_sparc_hash_table (info); |
252b5132 RH |
892 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
893 | sym_hashes = elf_sym_hashes (abfd); | |
894 | local_got_offsets = elf_local_got_offsets (abfd); | |
895 | ||
252b5132 RH |
896 | sreloc = NULL; |
897 | ||
898 | rel_end = relocs + sec->reloc_count; | |
899 | for (rel = relocs; rel < rel_end; rel++) | |
900 | { | |
b9734f35 | 901 | unsigned int r_type; |
252b5132 RH |
902 | unsigned long r_symndx; |
903 | struct elf_link_hash_entry *h; | |
904 | ||
905 | r_symndx = ELF32_R_SYM (rel->r_info); | |
b9734f35 JJ |
906 | r_type = ELF32_R_TYPE (rel->r_info); |
907 | ||
908 | if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr)) | |
26e41594 | 909 | { |
d003868e AM |
910 | (*_bfd_error_handler) (_("%B: bad symbol index: %d"), |
911 | abfd, r_symndx); | |
26e41594 AM |
912 | return FALSE; |
913 | } | |
b9734f35 | 914 | |
252b5132 RH |
915 | if (r_symndx < symtab_hdr->sh_info) |
916 | h = NULL; | |
917 | else | |
918 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
919 | ||
b9734f35 JJ |
920 | /* Compatibility with old R_SPARC_REV32 reloc conflicting |
921 | with R_SPARC_TLS_GD_HI22. */ | |
922 | if (! checked_tlsgd) | |
923 | switch (r_type) | |
924 | { | |
925 | case R_SPARC_TLS_GD_HI22: | |
926 | { | |
927 | const Elf_Internal_Rela *relt; | |
928 | ||
929 | for (relt = rel + 1; relt < rel_end; relt++) | |
930 | if (ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_LO10 | |
931 | || ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_ADD | |
932 | || ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_CALL) | |
933 | break; | |
934 | checked_tlsgd = TRUE; | |
935 | elf32_sparc_tdata (abfd)->has_tlsgd = relt < rel_end; | |
936 | } | |
937 | break; | |
938 | case R_SPARC_TLS_GD_LO10: | |
939 | case R_SPARC_TLS_GD_ADD: | |
940 | case R_SPARC_TLS_GD_CALL: | |
941 | checked_tlsgd = TRUE; | |
942 | elf32_sparc_tdata (abfd)->has_tlsgd = TRUE; | |
943 | break; | |
944 | } | |
945 | ||
946 | r_type = elf32_sparc_tls_transition (info, abfd, r_type, h == NULL); | |
947 | switch (r_type) | |
252b5132 | 948 | { |
b9734f35 JJ |
949 | case R_SPARC_TLS_LDM_HI22: |
950 | case R_SPARC_TLS_LDM_LO10: | |
951 | htab->tls_ldm_got.refcount += 1; | |
952 | break; | |
953 | ||
954 | case R_SPARC_TLS_LE_HIX22: | |
955 | case R_SPARC_TLS_LE_LOX10: | |
956 | if (info->shared) | |
957 | goto r_sparc_plt32; | |
958 | break; | |
959 | ||
960 | case R_SPARC_TLS_IE_HI22: | |
961 | case R_SPARC_TLS_IE_LO10: | |
962 | if (info->shared) | |
963 | info->flags |= DF_STATIC_TLS; | |
964 | /* Fall through */ | |
965 | ||
252b5132 RH |
966 | case R_SPARC_GOT10: |
967 | case R_SPARC_GOT13: | |
968 | case R_SPARC_GOT22: | |
b9734f35 JJ |
969 | case R_SPARC_TLS_GD_HI22: |
970 | case R_SPARC_TLS_GD_LO10: | |
252b5132 | 971 | /* This symbol requires a global offset table entry. */ |
b9734f35 JJ |
972 | { |
973 | int tls_type, old_tls_type; | |
252b5132 | 974 | |
b9734f35 JJ |
975 | switch (r_type) |
976 | { | |
977 | default: | |
978 | case R_SPARC_GOT10: | |
979 | case R_SPARC_GOT13: | |
980 | case R_SPARC_GOT22: | |
981 | tls_type = GOT_NORMAL; | |
982 | break; | |
983 | case R_SPARC_TLS_GD_HI22: | |
984 | case R_SPARC_TLS_GD_LO10: | |
985 | tls_type = GOT_TLS_GD; | |
986 | break; | |
987 | case R_SPARC_TLS_IE_HI22: | |
988 | case R_SPARC_TLS_IE_LO10: | |
989 | tls_type = GOT_TLS_IE; | |
990 | break; | |
991 | } | |
252b5132 | 992 | |
b9734f35 JJ |
993 | if (h != NULL) |
994 | { | |
995 | h->got.refcount += 1; | |
996 | old_tls_type = elf32_sparc_hash_entry(h)->tls_type; | |
997 | } | |
998 | else | |
999 | { | |
1000 | bfd_signed_vma *local_got_refcounts; | |
252b5132 | 1001 | |
b9734f35 JJ |
1002 | /* This is a global offset table entry for a local symbol. */ |
1003 | local_got_refcounts = elf_local_got_refcounts (abfd); | |
1004 | if (local_got_refcounts == NULL) | |
1005 | { | |
1006 | bfd_size_type size; | |
252b5132 | 1007 | |
b9734f35 JJ |
1008 | size = symtab_hdr->sh_info; |
1009 | size *= (sizeof (bfd_signed_vma) + sizeof(char)); | |
1010 | local_got_refcounts = ((bfd_signed_vma *) | |
1011 | bfd_zalloc (abfd, size)); | |
1012 | if (local_got_refcounts == NULL) | |
1013 | return FALSE; | |
1014 | elf_local_got_refcounts (abfd) = local_got_refcounts; | |
1015 | elf32_sparc_local_got_tls_type (abfd) | |
1016 | = (char *) (local_got_refcounts + symtab_hdr->sh_info); | |
1017 | } | |
1018 | local_got_refcounts[r_symndx] += 1; | |
1019 | old_tls_type = elf32_sparc_local_got_tls_type (abfd) [r_symndx]; | |
1020 | } | |
252b5132 | 1021 | |
b9734f35 JJ |
1022 | /* If a TLS symbol is accessed using IE at least once, |
1023 | there is no point to use dynamic model for it. */ | |
1024 | if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN | |
1025 | && (old_tls_type != GOT_TLS_GD | |
1026 | || tls_type != GOT_TLS_IE)) | |
1027 | { | |
1028 | if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD) | |
1029 | tls_type = old_tls_type; | |
1030 | else | |
1031 | { | |
1032 | (*_bfd_error_handler) | |
d003868e AM |
1033 | (_("%B: `%s' accessed both as normal and thread local symbol"), |
1034 | abfd, h ? h->root.root.string : "<local>"); | |
b34976b6 | 1035 | return FALSE; |
b9734f35 JJ |
1036 | } |
1037 | } | |
252b5132 | 1038 | |
b9734f35 JJ |
1039 | if (old_tls_type != tls_type) |
1040 | { | |
1041 | if (h != NULL) | |
1042 | elf32_sparc_hash_entry (h)->tls_type = tls_type; | |
1043 | else | |
1044 | elf32_sparc_local_got_tls_type (abfd) [r_symndx] = tls_type; | |
1045 | } | |
1046 | } | |
252b5132 | 1047 | |
2293c499 AM |
1048 | if (htab->sgot == NULL) |
1049 | { | |
1050 | if (htab->elf.dynobj == NULL) | |
1051 | htab->elf.dynobj = abfd; | |
1052 | if (!create_got_section (htab->elf.dynobj, info)) | |
1053 | return FALSE; | |
1054 | } | |
b9734f35 | 1055 | break; |
252b5132 | 1056 | |
b9734f35 JJ |
1057 | case R_SPARC_TLS_GD_CALL: |
1058 | case R_SPARC_TLS_LDM_CALL: | |
1059 | if (info->shared) | |
1060 | { | |
1061 | /* These are basically R_SPARC_TLS_WPLT30 relocs against | |
1062 | __tls_get_addr. */ | |
1063 | struct bfd_link_hash_entry *bh = NULL; | |
1064 | if (! _bfd_generic_link_add_one_symbol (info, abfd, | |
1065 | "__tls_get_addr", 0, | |
1066 | bfd_und_section_ptr, 0, | |
1067 | NULL, FALSE, FALSE, | |
1068 | &bh)) | |
1069 | return FALSE; | |
1070 | h = (struct elf_link_hash_entry *) bh; | |
252b5132 | 1071 | } |
b9734f35 JJ |
1072 | else |
1073 | break; | |
1074 | /* Fall through */ | |
252b5132 | 1075 | |
bd5e6e7e | 1076 | case R_SPARC_PLT32: |
252b5132 RH |
1077 | case R_SPARC_WPLT30: |
1078 | /* This symbol requires a procedure linkage table entry. We | |
26e41594 AM |
1079 | actually build the entry in adjust_dynamic_symbol, |
1080 | because this might be a case of linking PIC code without | |
1081 | linking in any dynamic objects, in which case we don't | |
1082 | need to generate a procedure linkage table after all. */ | |
252b5132 RH |
1083 | |
1084 | if (h == NULL) | |
1085 | { | |
1086 | /* The Solaris native assembler will generate a WPLT30 | |
26e41594 AM |
1087 | reloc for a local symbol if you assemble a call from |
1088 | one section to another when using -K pic. We treat | |
1089 | it as WDISP30. */ | |
b9734f35 | 1090 | if (ELF32_R_TYPE (rel->r_info) == R_SPARC_PLT32) |
bd5e6e7e | 1091 | goto r_sparc_plt32; |
252b5132 RH |
1092 | break; |
1093 | } | |
1094 | ||
f5385ebf | 1095 | h->needs_plt = 1; |
252b5132 | 1096 | |
b9734f35 | 1097 | if (ELF32_R_TYPE (rel->r_info) == R_SPARC_PLT32) |
bd5e6e7e | 1098 | goto r_sparc_plt32; |
b9734f35 | 1099 | h->plt.refcount += 1; |
252b5132 RH |
1100 | break; |
1101 | ||
1102 | case R_SPARC_PC10: | |
1103 | case R_SPARC_PC22: | |
7843f00e | 1104 | if (h != NULL) |
f5385ebf | 1105 | h->non_got_ref = 1; |
7843f00e | 1106 | |
252b5132 RH |
1107 | if (h != NULL |
1108 | && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0) | |
1109 | break; | |
1110 | /* Fall through. */ | |
b9734f35 | 1111 | |
252b5132 RH |
1112 | case R_SPARC_DISP8: |
1113 | case R_SPARC_DISP16: | |
1114 | case R_SPARC_DISP32: | |
1115 | case R_SPARC_WDISP30: | |
1116 | case R_SPARC_WDISP22: | |
1117 | case R_SPARC_WDISP19: | |
1118 | case R_SPARC_WDISP16: | |
252b5132 RH |
1119 | case R_SPARC_8: |
1120 | case R_SPARC_16: | |
1121 | case R_SPARC_32: | |
1122 | case R_SPARC_HI22: | |
1123 | case R_SPARC_22: | |
1124 | case R_SPARC_13: | |
1125 | case R_SPARC_LO10: | |
0f2712ed | 1126 | case R_SPARC_UA16: |
252b5132 | 1127 | case R_SPARC_UA32: |
7843f00e | 1128 | if (h != NULL) |
f5385ebf | 1129 | h->non_got_ref = 1; |
7843f00e | 1130 | |
bd5e6e7e | 1131 | r_sparc_plt32: |
b9734f35 JJ |
1132 | if (h != NULL && !info->shared) |
1133 | { | |
1134 | /* We may need a .plt entry if the function this reloc | |
1135 | refers to is in a shared lib. */ | |
1136 | h->plt.refcount += 1; | |
1137 | } | |
1138 | ||
1139 | /* If we are creating a shared library, and this is a reloc | |
1140 | against a global symbol, or a non PC relative reloc | |
1141 | against a local symbol, then we need to copy the reloc | |
1142 | into the shared library. However, if we are linking with | |
1143 | -Bsymbolic, we do not need to copy a reloc against a | |
1144 | global symbol which is defined in an object we are | |
1145 | including in the link (i.e., DEF_REGULAR is set). At | |
1146 | this point we have not seen all the input files, so it is | |
1147 | possible that DEF_REGULAR is not set now but will be set | |
1148 | later (it is never cleared). In case of a weak definition, | |
1149 | DEF_REGULAR may be cleared later by a strong definition in | |
1150 | a shared library. We account for that possibility below by | |
1151 | storing information in the relocs_copied field of the hash | |
1152 | table entry. A similar situation occurs when creating | |
1153 | shared libraries and symbol visibility changes render the | |
1154 | symbol local. | |
1155 | ||
1156 | If on the other hand, we are creating an executable, we | |
1157 | may need to keep relocations for symbols satisfied by a | |
1158 | dynamic library if we manage to avoid copy relocs for the | |
1159 | symbol. */ | |
1160 | if ((info->shared | |
1161 | && (sec->flags & SEC_ALLOC) != 0 | |
1162 | && (! _bfd_sparc_elf_howto_table[r_type].pc_relative | |
1163 | || (h != NULL | |
1164 | && (! info->symbolic | |
1165 | || h->root.type == bfd_link_hash_defweak | |
f5385ebf | 1166 | || !h->def_regular)))) |
b9734f35 JJ |
1167 | || (!info->shared |
1168 | && (sec->flags & SEC_ALLOC) != 0 | |
1169 | && h != NULL | |
1170 | && (h->root.type == bfd_link_hash_defweak | |
f5385ebf | 1171 | || !h->def_regular))) |
252b5132 | 1172 | { |
b9734f35 JJ |
1173 | struct elf32_sparc_dyn_relocs *p; |
1174 | struct elf32_sparc_dyn_relocs **head; | |
1175 | ||
252b5132 | 1176 | /* When creating a shared object, we must copy these |
26e41594 AM |
1177 | relocs into the output file. We create a reloc |
1178 | section in dynobj and make room for the reloc. */ | |
252b5132 RH |
1179 | if (sreloc == NULL) |
1180 | { | |
1181 | const char *name; | |
b9734f35 | 1182 | bfd *dynobj; |
252b5132 RH |
1183 | |
1184 | name = (bfd_elf_string_from_elf_section | |
1185 | (abfd, | |
1186 | elf_elfheader (abfd)->e_shstrndx, | |
1187 | elf_section_data (sec)->rel_hdr.sh_name)); | |
1188 | if (name == NULL) | |
b34976b6 | 1189 | return FALSE; |
252b5132 RH |
1190 | |
1191 | BFD_ASSERT (strncmp (name, ".rela", 5) == 0 | |
1192 | && strcmp (bfd_get_section_name (abfd, sec), | |
1193 | name + 5) == 0); | |
1194 | ||
b9734f35 JJ |
1195 | if (htab->elf.dynobj == NULL) |
1196 | htab->elf.dynobj = abfd; | |
1197 | dynobj = htab->elf.dynobj; | |
1198 | ||
252b5132 RH |
1199 | sreloc = bfd_get_section_by_name (dynobj, name); |
1200 | if (sreloc == NULL) | |
1201 | { | |
1202 | flagword flags; | |
1203 | ||
1204 | sreloc = bfd_make_section (dynobj, name); | |
1205 | flags = (SEC_HAS_CONTENTS | SEC_READONLY | |
1206 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
1207 | if ((sec->flags & SEC_ALLOC) != 0) | |
1208 | flags |= SEC_ALLOC | SEC_LOAD; | |
1209 | if (sreloc == NULL | |
1210 | || ! bfd_set_section_flags (dynobj, sreloc, flags) | |
1211 | || ! bfd_set_section_alignment (dynobj, sreloc, 2)) | |
b34976b6 | 1212 | return FALSE; |
252b5132 | 1213 | } |
b9734f35 | 1214 | elf_section_data (sec)->sreloc = sreloc; |
252b5132 RH |
1215 | } |
1216 | ||
b9734f35 JJ |
1217 | /* If this is a global symbol, we count the number of |
1218 | relocations we need for this symbol. */ | |
1219 | if (h != NULL) | |
1220 | head = &((struct elf32_sparc_link_hash_entry *) h)->dyn_relocs; | |
1221 | else | |
1222 | { | |
1223 | /* Track dynamic relocs needed for local syms too. | |
1224 | We really need local syms available to do this | |
1225 | easily. Oh well. */ | |
1226 | ||
1227 | asection *s; | |
1228 | s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, | |
1229 | sec, r_symndx); | |
1230 | if (s == NULL) | |
1231 | return FALSE; | |
1232 | ||
1233 | head = ((struct elf32_sparc_dyn_relocs **) | |
1234 | &elf_section_data (s)->local_dynrel); | |
1235 | } | |
1236 | ||
1237 | p = *head; | |
1238 | if (p == NULL || p->sec != sec) | |
1239 | { | |
1240 | bfd_size_type amt = sizeof *p; | |
1241 | p = ((struct elf32_sparc_dyn_relocs *) | |
1242 | bfd_alloc (htab->elf.dynobj, amt)); | |
1243 | if (p == NULL) | |
1244 | return FALSE; | |
1245 | p->next = *head; | |
1246 | *head = p; | |
1247 | p->sec = sec; | |
1248 | p->count = 0; | |
1249 | p->pc_count = 0; | |
1250 | } | |
1251 | ||
1252 | p->count += 1; | |
1253 | if (_bfd_sparc_elf_howto_table[r_type].pc_relative) | |
1254 | p->pc_count += 1; | |
252b5132 RH |
1255 | } |
1256 | ||
1257 | break; | |
1258 | ||
26e41594 | 1259 | case R_SPARC_GNU_VTINHERIT: |
c152c796 | 1260 | if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset)) |
26e41594 AM |
1261 | return FALSE; |
1262 | break; | |
252b5132 | 1263 | |
26e41594 | 1264 | case R_SPARC_GNU_VTENTRY: |
c152c796 | 1265 | if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend)) |
26e41594 AM |
1266 | return FALSE; |
1267 | break; | |
252b5132 RH |
1268 | |
1269 | default: | |
1270 | break; | |
1271 | } | |
1272 | } | |
1273 | ||
b34976b6 | 1274 | return TRUE; |
252b5132 RH |
1275 | } |
1276 | ||
1277 | static asection * | |
1e2f5b6e AM |
1278 | elf32_sparc_gc_mark_hook (sec, info, rel, h, sym) |
1279 | asection *sec; | |
5f771d47 | 1280 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
1281 | Elf_Internal_Rela *rel; |
1282 | struct elf_link_hash_entry *h; | |
1283 | Elf_Internal_Sym *sym; | |
1284 | { | |
252b5132 RH |
1285 | if (h != NULL) |
1286 | { | |
1287 | switch (ELF32_R_TYPE (rel->r_info)) | |
1288 | { | |
1289 | case R_SPARC_GNU_VTINHERIT: | |
1290 | case R_SPARC_GNU_VTENTRY: | |
26e41594 | 1291 | break; |
252b5132 RH |
1292 | |
1293 | default: | |
26e41594 AM |
1294 | switch (h->root.type) |
1295 | { | |
1296 | case bfd_link_hash_defined: | |
1297 | case bfd_link_hash_defweak: | |
1298 | return h->root.u.def.section; | |
252b5132 | 1299 | |
26e41594 AM |
1300 | case bfd_link_hash_common: |
1301 | return h->root.u.c.p->section; | |
e049a0de ILT |
1302 | |
1303 | default: | |
1304 | break; | |
26e41594 | 1305 | } |
252b5132 RH |
1306 | } |
1307 | } | |
1308 | else | |
1e2f5b6e | 1309 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); |
252b5132 RH |
1310 | |
1311 | return NULL; | |
1312 | } | |
1313 | ||
1314 | /* Update the got entry reference counts for the section being removed. */ | |
b34976b6 | 1315 | static bfd_boolean |
252b5132 RH |
1316 | elf32_sparc_gc_sweep_hook (abfd, info, sec, relocs) |
1317 | bfd *abfd; | |
5f771d47 | 1318 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
1319 | asection *sec; |
1320 | const Elf_Internal_Rela *relocs; | |
1321 | { | |
252b5132 RH |
1322 | Elf_Internal_Shdr *symtab_hdr; |
1323 | struct elf_link_hash_entry **sym_hashes; | |
1324 | bfd_signed_vma *local_got_refcounts; | |
1325 | const Elf_Internal_Rela *rel, *relend; | |
26e41594 AM |
1326 | |
1327 | elf_section_data (sec)->local_dynrel = NULL; | |
252b5132 RH |
1328 | |
1329 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
1330 | sym_hashes = elf_sym_hashes (abfd); | |
1331 | local_got_refcounts = elf_local_got_refcounts (abfd); | |
1332 | ||
1333 | relend = relocs + sec->reloc_count; | |
1334 | for (rel = relocs; rel < relend; rel++) | |
26e41594 AM |
1335 | { |
1336 | unsigned long r_symndx; | |
1337 | unsigned int r_type; | |
1338 | struct elf_link_hash_entry *h = NULL; | |
b9734f35 | 1339 | |
26e41594 AM |
1340 | r_symndx = ELF32_R_SYM (rel->r_info); |
1341 | if (r_symndx >= symtab_hdr->sh_info) | |
1342 | { | |
1343 | struct elf32_sparc_link_hash_entry *eh; | |
1344 | struct elf32_sparc_dyn_relocs **pp; | |
1345 | struct elf32_sparc_dyn_relocs *p; | |
b9734f35 | 1346 | |
26e41594 AM |
1347 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; |
1348 | eh = (struct elf32_sparc_link_hash_entry *) h; | |
1349 | for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next) | |
1350 | if (p->sec == sec) | |
1351 | { | |
1352 | /* Everything must go for SEC. */ | |
1353 | *pp = p->next; | |
1354 | break; | |
1355 | } | |
1356 | } | |
b9734f35 | 1357 | |
26e41594 AM |
1358 | r_type = ELF32_R_TYPE (rel->r_info); |
1359 | r_type = elf32_sparc_tls_transition (info, abfd, r_type, h != NULL); | |
1360 | switch (r_type) | |
1361 | { | |
1362 | case R_SPARC_TLS_LDM_HI22: | |
1363 | case R_SPARC_TLS_LDM_LO10: | |
1364 | if (elf32_sparc_hash_table (info)->tls_ldm_got.refcount > 0) | |
1365 | elf32_sparc_hash_table (info)->tls_ldm_got.refcount -= 1; | |
1366 | break; | |
b9734f35 | 1367 | |
26e41594 AM |
1368 | case R_SPARC_TLS_GD_HI22: |
1369 | case R_SPARC_TLS_GD_LO10: | |
1370 | case R_SPARC_TLS_IE_HI22: | |
1371 | case R_SPARC_TLS_IE_LO10: | |
1372 | case R_SPARC_GOT10: | |
1373 | case R_SPARC_GOT13: | |
1374 | case R_SPARC_GOT22: | |
1375 | if (h != NULL) | |
1376 | { | |
1377 | if (h->got.refcount > 0) | |
1378 | h->got.refcount--; | |
1379 | } | |
1380 | else | |
1381 | { | |
1382 | if (local_got_refcounts[r_symndx] > 0) | |
1383 | local_got_refcounts[r_symndx]--; | |
1384 | } | |
1385 | break; | |
b9734f35 | 1386 | |
26e41594 AM |
1387 | case R_SPARC_PC10: |
1388 | case R_SPARC_PC22: | |
1389 | if (h != NULL | |
1390 | && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0) | |
1391 | break; | |
1392 | /* Fall through. */ | |
b9734f35 | 1393 | |
26e41594 AM |
1394 | case R_SPARC_DISP8: |
1395 | case R_SPARC_DISP16: | |
1396 | case R_SPARC_DISP32: | |
1397 | case R_SPARC_WDISP30: | |
1398 | case R_SPARC_WDISP22: | |
1399 | case R_SPARC_WDISP19: | |
1400 | case R_SPARC_WDISP16: | |
1401 | case R_SPARC_8: | |
1402 | case R_SPARC_16: | |
1403 | case R_SPARC_32: | |
1404 | case R_SPARC_HI22: | |
1405 | case R_SPARC_22: | |
1406 | case R_SPARC_13: | |
1407 | case R_SPARC_LO10: | |
1408 | case R_SPARC_UA16: | |
1409 | case R_SPARC_UA32: | |
1410 | case R_SPARC_PLT32: | |
1411 | if (info->shared) | |
1412 | break; | |
1413 | /* Fall through. */ | |
b9734f35 | 1414 | |
26e41594 AM |
1415 | case R_SPARC_WPLT30: |
1416 | if (h != NULL) | |
1417 | { | |
1418 | if (h->plt.refcount > 0) | |
1419 | h->plt.refcount--; | |
1420 | } | |
1421 | break; | |
252b5132 | 1422 | |
26e41594 AM |
1423 | default: |
1424 | break; | |
1425 | } | |
1426 | } | |
252b5132 | 1427 | |
b34976b6 | 1428 | return TRUE; |
252b5132 RH |
1429 | } |
1430 | ||
1431 | /* Adjust a symbol defined by a dynamic object and referenced by a | |
1432 | regular object. The current definition is in some section of the | |
1433 | dynamic object, but we're not including those sections. We have to | |
1434 | change the definition to something the rest of the link can | |
1435 | understand. */ | |
1436 | ||
b34976b6 | 1437 | static bfd_boolean |
252b5132 RH |
1438 | elf32_sparc_adjust_dynamic_symbol (info, h) |
1439 | struct bfd_link_info *info; | |
1440 | struct elf_link_hash_entry *h; | |
1441 | { | |
b9734f35 JJ |
1442 | struct elf32_sparc_link_hash_table *htab; |
1443 | struct elf32_sparc_link_hash_entry * eh; | |
1444 | struct elf32_sparc_dyn_relocs *p; | |
252b5132 RH |
1445 | asection *s; |
1446 | unsigned int power_of_two; | |
1447 | ||
b9734f35 | 1448 | htab = elf32_sparc_hash_table (info); |
252b5132 RH |
1449 | |
1450 | /* Make sure we know what is going on here. */ | |
b9734f35 | 1451 | BFD_ASSERT (htab->elf.dynobj != NULL |
f5385ebf | 1452 | && (h->needs_plt |
f6e332e6 | 1453 | || h->u.weakdef != NULL |
f5385ebf AM |
1454 | || (h->def_dynamic |
1455 | && h->ref_regular | |
1456 | && !h->def_regular))); | |
252b5132 RH |
1457 | |
1458 | /* If this is a function, put it in the procedure linkage table. We | |
1459 | will fill in the contents of the procedure linkage table later | |
1460 | (although we could actually do it here). The STT_NOTYPE | |
1461 | condition is a hack specifically for the Oracle libraries | |
1462 | delivered for Solaris; for some inexplicable reason, they define | |
1463 | some of their functions as STT_NOTYPE when they really should be | |
1464 | STT_FUNC. */ | |
1465 | if (h->type == STT_FUNC | |
f5385ebf | 1466 | || h->needs_plt |
252b5132 RH |
1467 | || (h->type == STT_NOTYPE |
1468 | && (h->root.type == bfd_link_hash_defined | |
1469 | || h->root.type == bfd_link_hash_defweak) | |
1470 | && (h->root.u.def.section->flags & SEC_CODE) != 0)) | |
1471 | { | |
b9734f35 JJ |
1472 | if (h->plt.refcount <= 0 |
1473 | || (! info->shared | |
f5385ebf AM |
1474 | && !h->def_dynamic |
1475 | && !h->ref_dynamic | |
b9734f35 JJ |
1476 | && h->root.type != bfd_link_hash_undefweak |
1477 | && h->root.type != bfd_link_hash_undefined)) | |
252b5132 RH |
1478 | { |
1479 | /* This case can occur if we saw a WPLT30 reloc in an input | |
b9734f35 JJ |
1480 | file, but the symbol was never referred to by a dynamic |
1481 | object, or if all references were garbage collected. In | |
1482 | such a case, we don't actually need to build a procedure | |
1483 | linkage table, and we can just do a WDISP30 reloc instead. */ | |
1484 | h->plt.offset = (bfd_vma) -1; | |
f5385ebf | 1485 | h->needs_plt = 0; |
252b5132 RH |
1486 | } |
1487 | ||
b34976b6 | 1488 | return TRUE; |
252b5132 | 1489 | } |
b9734f35 JJ |
1490 | else |
1491 | h->plt.offset = (bfd_vma) -1; | |
252b5132 RH |
1492 | |
1493 | /* If this is a weak symbol, and there is a real definition, the | |
1494 | processor independent code will have arranged for us to see the | |
1495 | real definition first, and we can just use the same value. */ | |
f6e332e6 | 1496 | if (h->u.weakdef != NULL) |
252b5132 | 1497 | { |
f6e332e6 AM |
1498 | BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined |
1499 | || h->u.weakdef->root.type == bfd_link_hash_defweak); | |
1500 | h->root.u.def.section = h->u.weakdef->root.u.def.section; | |
1501 | h->root.u.def.value = h->u.weakdef->root.u.def.value; | |
b34976b6 | 1502 | return TRUE; |
252b5132 RH |
1503 | } |
1504 | ||
1505 | /* This is a reference to a symbol defined by a dynamic object which | |
1506 | is not a function. */ | |
1507 | ||
1508 | /* If we are creating a shared library, we must presume that the | |
1509 | only references to the symbol are via the global offset table. | |
1510 | For such cases we need not do anything here; the relocations will | |
1511 | be handled correctly by relocate_section. */ | |
1512 | if (info->shared) | |
b34976b6 | 1513 | return TRUE; |
252b5132 | 1514 | |
7843f00e ILT |
1515 | /* If there are no references to this symbol that do not use the |
1516 | GOT, we don't need to generate a copy reloc. */ | |
f5385ebf | 1517 | if (!h->non_got_ref) |
b34976b6 | 1518 | return TRUE; |
7843f00e | 1519 | |
b9734f35 JJ |
1520 | eh = (struct elf32_sparc_link_hash_entry *) h; |
1521 | for (p = eh->dyn_relocs; p != NULL; p = p->next) | |
1522 | { | |
1523 | s = p->sec->output_section; | |
1524 | if (s != NULL && (s->flags & SEC_READONLY) != 0) | |
1525 | break; | |
1526 | } | |
1527 | ||
1528 | /* If we didn't find any dynamic relocs in read-only sections, then | |
1529 | we'll be keeping the dynamic relocs and avoiding the copy reloc. */ | |
1530 | if (p == NULL) | |
1531 | { | |
f5385ebf | 1532 | h->non_got_ref = 0; |
b9734f35 JJ |
1533 | return TRUE; |
1534 | } | |
1535 | ||
252b5132 RH |
1536 | /* We must allocate the symbol in our .dynbss section, which will |
1537 | become part of the .bss section of the executable. There will be | |
1538 | an entry for this symbol in the .dynsym section. The dynamic | |
1539 | object will contain position independent code, so all references | |
1540 | from the dynamic object to this symbol will go through the global | |
1541 | offset table. The dynamic linker will use the .dynsym entry to | |
1542 | determine the address it must put in the global offset table, so | |
1543 | both the dynamic object and the regular object will refer to the | |
1544 | same memory location for the variable. */ | |
1545 | ||
b9734f35 JJ |
1546 | /* We must generate a R_SPARC_COPY reloc to tell the dynamic linker |
1547 | to copy the initial value out of the dynamic object and into the | |
1548 | runtime process image. We need to remember the offset into the | |
1549 | .rel.bss section we are going to use. */ | |
1550 | if ((h->root.u.def.section->flags & SEC_ALLOC) != 0) | |
1551 | { | |
eea6121a | 1552 | htab->srelbss->size += sizeof (Elf32_External_Rela); |
f5385ebf | 1553 | h->needs_copy = 1; |
b9734f35 JJ |
1554 | } |
1555 | ||
1556 | /* We need to figure out the alignment required for this symbol. I | |
1557 | have no idea how ELF linkers handle this. */ | |
1558 | power_of_two = bfd_log2 (h->size); | |
1559 | if (power_of_two > 3) | |
1560 | power_of_two = 3; | |
1561 | ||
1562 | /* Apply the required alignment. */ | |
1563 | s = htab->sdynbss; | |
eea6121a | 1564 | s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two)); |
b9734f35 JJ |
1565 | if (power_of_two > bfd_get_section_alignment (dynobj, s)) |
1566 | { | |
1567 | if (! bfd_set_section_alignment (dynobj, s, power_of_two)) | |
1568 | return FALSE; | |
1569 | } | |
1570 | ||
1571 | /* Define the symbol as being at this point in the section. */ | |
1572 | h->root.u.def.section = s; | |
eea6121a | 1573 | h->root.u.def.value = s->size; |
b9734f35 JJ |
1574 | |
1575 | /* Increment the section size to make room for the symbol. */ | |
eea6121a | 1576 | s->size += h->size; |
b9734f35 JJ |
1577 | |
1578 | return TRUE; | |
1579 | } | |
1580 | ||
b9734f35 JJ |
1581 | /* Allocate space in .plt, .got and associated reloc sections for |
1582 | dynamic relocs. */ | |
1583 | ||
1584 | static bfd_boolean | |
1585 | allocate_dynrelocs (h, inf) | |
1586 | struct elf_link_hash_entry *h; | |
1587 | PTR inf; | |
1588 | { | |
1589 | struct bfd_link_info *info; | |
1590 | struct elf32_sparc_link_hash_table *htab; | |
1591 | struct elf32_sparc_link_hash_entry *eh; | |
1592 | struct elf32_sparc_dyn_relocs *p; | |
1593 | ||
1594 | if (h->root.type == bfd_link_hash_indirect) | |
1595 | return TRUE; | |
1596 | ||
1597 | if (h->root.type == bfd_link_hash_warning) | |
1598 | /* When warning symbols are created, they **replace** the "real" | |
1599 | entry in the hash table, thus we never get to see the real | |
1600 | symbol in a hash traversal. So look at it now. */ | |
1601 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1602 | ||
1603 | info = (struct bfd_link_info *) inf; | |
1604 | htab = elf32_sparc_hash_table (info); | |
1605 | ||
1606 | if (htab->elf.dynamic_sections_created | |
1607 | && h->plt.refcount > 0) | |
1608 | { | |
1609 | /* Make sure this symbol is output as a dynamic symbol. | |
1610 | Undefined weak syms won't yet be marked as dynamic. */ | |
1611 | if (h->dynindx == -1 | |
f5385ebf | 1612 | && !h->forced_local) |
b9734f35 | 1613 | { |
c152c796 | 1614 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
b9734f35 JJ |
1615 | return FALSE; |
1616 | } | |
1617 | ||
c152c796 | 1618 | if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h)) |
b9734f35 JJ |
1619 | { |
1620 | asection *s = htab->splt; | |
1621 | ||
1622 | /* The first four entries in .plt are reserved. */ | |
eea6121a AM |
1623 | if (s->size == 0) |
1624 | s->size = 4 * PLT_ENTRY_SIZE; | |
26e41594 | 1625 | |
b9734f35 | 1626 | /* The procedure linkage table has a maximum size. */ |
eea6121a | 1627 | if (s->size >= 0x400000) |
b9734f35 JJ |
1628 | { |
1629 | bfd_set_error (bfd_error_bad_value); | |
1630 | return FALSE; | |
1631 | } | |
1632 | ||
eea6121a | 1633 | h->plt.offset = s->size; |
b9734f35 JJ |
1634 | |
1635 | /* If this symbol is not defined in a regular file, and we are | |
1636 | not generating a shared library, then set the symbol to this | |
1637 | location in the .plt. This is required to make function | |
1638 | pointers compare as equal between the normal executable and | |
1639 | the shared library. */ | |
1640 | if (! info->shared | |
f5385ebf | 1641 | && !h->def_regular) |
b9734f35 JJ |
1642 | { |
1643 | h->root.u.def.section = s; | |
1644 | h->root.u.def.value = h->plt.offset; | |
1645 | } | |
1646 | ||
1647 | /* Make room for this entry. */ | |
eea6121a | 1648 | s->size += PLT_ENTRY_SIZE; |
b9734f35 JJ |
1649 | |
1650 | /* We also need to make an entry in the .rela.plt section. */ | |
eea6121a | 1651 | htab->srelplt->size += sizeof (Elf32_External_Rela); |
b9734f35 JJ |
1652 | } |
1653 | else | |
1654 | { | |
1655 | h->plt.offset = (bfd_vma) -1; | |
f5385ebf | 1656 | h->needs_plt = 0; |
b9734f35 JJ |
1657 | } |
1658 | } | |
1659 | else | |
1660 | { | |
1661 | h->plt.offset = (bfd_vma) -1; | |
f5385ebf | 1662 | h->needs_plt = 0; |
b9734f35 JJ |
1663 | } |
1664 | ||
1665 | /* If R_SPARC_TLS_IE_{HI22,LO10} symbol is now local to the binary, | |
1666 | make it a R_SPARC_TLS_LE_{HI22,LO10} requiring no TLS entry. */ | |
1667 | if (h->got.refcount > 0 | |
1668 | && !info->shared | |
1669 | && h->dynindx == -1 | |
1670 | && elf32_sparc_hash_entry(h)->tls_type == GOT_TLS_IE) | |
1671 | h->got.offset = (bfd_vma) -1; | |
1672 | else if (h->got.refcount > 0) | |
1673 | { | |
1674 | asection *s; | |
1675 | bfd_boolean dyn; | |
1676 | int tls_type = elf32_sparc_hash_entry(h)->tls_type; | |
1677 | ||
1678 | /* Make sure this symbol is output as a dynamic symbol. | |
1679 | Undefined weak syms won't yet be marked as dynamic. */ | |
1680 | if (h->dynindx == -1 | |
f5385ebf | 1681 | && !h->forced_local) |
b9734f35 | 1682 | { |
c152c796 | 1683 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
b9734f35 JJ |
1684 | return FALSE; |
1685 | } | |
1686 | ||
1687 | s = htab->sgot; | |
eea6121a AM |
1688 | h->got.offset = s->size; |
1689 | s->size += 4; | |
b9734f35 JJ |
1690 | /* R_SPARC_TLS_GD_HI{22,LO10} needs 2 consecutive GOT slots. */ |
1691 | if (tls_type == GOT_TLS_GD) | |
eea6121a | 1692 | s->size += 4; |
b9734f35 JJ |
1693 | dyn = htab->elf.dynamic_sections_created; |
1694 | /* R_SPARC_TLS_IE_{HI22,LO10} needs one dynamic relocation, | |
1695 | R_SPARC_TLS_GD_{HI22,LO10} needs one if local symbol and two if | |
1696 | global. */ | |
1697 | if ((tls_type == GOT_TLS_GD && h->dynindx == -1) | |
1698 | || tls_type == GOT_TLS_IE) | |
eea6121a | 1699 | htab->srelgot->size += sizeof (Elf32_External_Rela); |
b9734f35 | 1700 | else if (tls_type == GOT_TLS_GD) |
eea6121a | 1701 | htab->srelgot->size += 2 * sizeof (Elf32_External_Rela); |
c152c796 | 1702 | else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)) |
eea6121a | 1703 | htab->srelgot->size += sizeof (Elf32_External_Rela); |
b9734f35 JJ |
1704 | } |
1705 | else | |
1706 | h->got.offset = (bfd_vma) -1; | |
1707 | ||
1708 | eh = (struct elf32_sparc_link_hash_entry *) h; | |
1709 | if (eh->dyn_relocs == NULL) | |
1710 | return TRUE; | |
1711 | ||
1712 | /* In the shared -Bsymbolic case, discard space allocated for | |
1713 | dynamic pc-relative relocs against symbols which turn out to be | |
1714 | defined in regular objects. For the normal shared case, discard | |
1715 | space for pc-relative relocs that have become local due to symbol | |
1716 | visibility changes. */ | |
1717 | ||
1718 | if (info->shared) | |
1719 | { | |
f5385ebf AM |
1720 | if (h->def_regular |
1721 | && (h->forced_local | |
b9734f35 JJ |
1722 | || info->symbolic)) |
1723 | { | |
1724 | struct elf32_sparc_dyn_relocs **pp; | |
1725 | ||
1726 | for (pp = &eh->dyn_relocs; (p = *pp) != NULL; ) | |
1727 | { | |
1728 | p->count -= p->pc_count; | |
1729 | p->pc_count = 0; | |
1730 | if (p->count == 0) | |
1731 | *pp = p->next; | |
1732 | else | |
1733 | pp = &p->next; | |
1734 | } | |
1735 | } | |
1736 | } | |
1737 | else | |
1738 | { | |
1739 | /* For the non-shared case, discard space for relocs against | |
1740 | symbols which turn out to need copy relocs or are not | |
1741 | dynamic. */ | |
1742 | ||
f5385ebf AM |
1743 | if (!h->non_got_ref |
1744 | && ((h->def_dynamic | |
1745 | && !h->def_regular) | |
b9734f35 JJ |
1746 | || (htab->elf.dynamic_sections_created |
1747 | && (h->root.type == bfd_link_hash_undefweak | |
1748 | || h->root.type == bfd_link_hash_undefined)))) | |
1749 | { | |
1750 | /* Make sure this symbol is output as a dynamic symbol. | |
1751 | Undefined weak syms won't yet be marked as dynamic. */ | |
1752 | if (h->dynindx == -1 | |
f5385ebf | 1753 | && !h->forced_local) |
b9734f35 | 1754 | { |
c152c796 | 1755 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
b9734f35 JJ |
1756 | return FALSE; |
1757 | } | |
1758 | ||
1759 | /* If that succeeded, we know we'll be keeping all the | |
1760 | relocs. */ | |
1761 | if (h->dynindx != -1) | |
1762 | goto keep; | |
1763 | } | |
252b5132 | 1764 | |
b9734f35 | 1765 | eh->dyn_relocs = NULL; |
252b5132 | 1766 | |
b9734f35 | 1767 | keep: ; |
252b5132 RH |
1768 | } |
1769 | ||
b9734f35 JJ |
1770 | /* Finally, allocate space. */ |
1771 | for (p = eh->dyn_relocs; p != NULL; p = p->next) | |
252b5132 | 1772 | { |
b9734f35 | 1773 | asection *sreloc = elf_section_data (p->sec)->sreloc; |
eea6121a | 1774 | sreloc->size += p->count * sizeof (Elf32_External_Rela); |
252b5132 RH |
1775 | } |
1776 | ||
b9734f35 JJ |
1777 | return TRUE; |
1778 | } | |
252b5132 | 1779 | |
b9734f35 | 1780 | /* Find any dynamic relocs that apply to read-only sections. */ |
252b5132 | 1781 | |
b9734f35 JJ |
1782 | static bfd_boolean |
1783 | readonly_dynrelocs (h, inf) | |
1784 | struct elf_link_hash_entry *h; | |
1785 | PTR inf; | |
1786 | { | |
1787 | struct elf32_sparc_link_hash_entry *eh; | |
1788 | struct elf32_sparc_dyn_relocs *p; | |
1789 | ||
1790 | if (h->root.type == bfd_link_hash_warning) | |
1791 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1792 | ||
1793 | eh = (struct elf32_sparc_link_hash_entry *) h; | |
1794 | for (p = eh->dyn_relocs; p != NULL; p = p->next) | |
1795 | { | |
1796 | asection *s = p->sec->output_section; | |
1797 | ||
1798 | if (s != NULL && (s->flags & SEC_READONLY) != 0) | |
1799 | { | |
1800 | struct bfd_link_info *info = (struct bfd_link_info *) inf; | |
1801 | ||
1802 | info->flags |= DF_TEXTREL; | |
1803 | ||
1804 | /* Not an error, just cut short the traversal. */ | |
1805 | return FALSE; | |
1806 | } | |
1807 | } | |
b34976b6 | 1808 | return TRUE; |
252b5132 RH |
1809 | } |
1810 | ||
151e5294 EB |
1811 | /* Return true if the dynamic symbol for a given section should be |
1812 | omitted when creating a shared library. */ | |
1813 | ||
1814 | static bfd_boolean | |
1815 | elf32_sparc_omit_section_dynsym (bfd *output_bfd, | |
1816 | struct bfd_link_info *info, | |
1817 | asection *p) | |
1818 | { | |
1819 | /* We keep the .got section symbol so that explicit relocations | |
1820 | against the _GLOBAL_OFFSET_TABLE_ symbol emitted in PIC mode | |
1821 | can be turned into relocations against the .got symbol. */ | |
1822 | if (strcmp (p->name, ".got") == 0) | |
1823 | return FALSE; | |
1824 | ||
1825 | return _bfd_elf_link_omit_section_dynsym (output_bfd, info, p); | |
1826 | } | |
1827 | ||
252b5132 RH |
1828 | /* Set the sizes of the dynamic sections. */ |
1829 | ||
b34976b6 | 1830 | static bfd_boolean |
252b5132 | 1831 | elf32_sparc_size_dynamic_sections (output_bfd, info) |
db6751f2 | 1832 | bfd *output_bfd ATTRIBUTE_UNUSED; |
252b5132 RH |
1833 | struct bfd_link_info *info; |
1834 | { | |
b9734f35 | 1835 | struct elf32_sparc_link_hash_table *htab; |
252b5132 RH |
1836 | bfd *dynobj; |
1837 | asection *s; | |
b9734f35 | 1838 | bfd *ibfd; |
252b5132 | 1839 | |
b9734f35 JJ |
1840 | htab = elf32_sparc_hash_table (info); |
1841 | dynobj = htab->elf.dynobj; | |
252b5132 RH |
1842 | BFD_ASSERT (dynobj != NULL); |
1843 | ||
1844 | if (elf_hash_table (info)->dynamic_sections_created) | |
1845 | { | |
1846 | /* Set the contents of the .interp section to the interpreter. */ | |
36af4a4e | 1847 | if (info->executable) |
252b5132 RH |
1848 | { |
1849 | s = bfd_get_section_by_name (dynobj, ".interp"); | |
1850 | BFD_ASSERT (s != NULL); | |
eea6121a | 1851 | s->size = sizeof ELF_DYNAMIC_INTERPRETER; |
252b5132 RH |
1852 | s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; |
1853 | } | |
b9734f35 | 1854 | } |
252b5132 | 1855 | |
b9734f35 JJ |
1856 | /* Set up .got offsets for local syms, and space for local dynamic |
1857 | relocs. */ | |
1858 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) | |
1859 | { | |
1860 | bfd_signed_vma *local_got; | |
1861 | bfd_signed_vma *end_local_got; | |
1862 | char *local_tls_type; | |
1863 | bfd_size_type locsymcount; | |
1864 | Elf_Internal_Shdr *symtab_hdr; | |
1865 | asection *srel; | |
1866 | ||
1867 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
1868 | continue; | |
1869 | ||
1870 | for (s = ibfd->sections; s != NULL; s = s->next) | |
1871 | { | |
1872 | struct elf32_sparc_dyn_relocs *p; | |
1873 | ||
1874 | for (p = *((struct elf32_sparc_dyn_relocs **) | |
1875 | &elf_section_data (s)->local_dynrel); | |
1876 | p != NULL; | |
1877 | p = p->next) | |
1878 | { | |
1879 | if (!bfd_is_abs_section (p->sec) | |
1880 | && bfd_is_abs_section (p->sec->output_section)) | |
1881 | { | |
1882 | /* Input section has been discarded, either because | |
1883 | it is a copy of a linkonce section or due to | |
1884 | linker script /DISCARD/, so we'll be discarding | |
1885 | the relocs too. */ | |
1886 | } | |
1887 | else if (p->count != 0) | |
1888 | { | |
1889 | srel = elf_section_data (p->sec)->sreloc; | |
eea6121a | 1890 | srel->size += p->count * sizeof (Elf32_External_Rela); |
b9734f35 JJ |
1891 | if ((p->sec->output_section->flags & SEC_READONLY) != 0) |
1892 | info->flags |= DF_TEXTREL; | |
1893 | } | |
1894 | } | |
1895 | } | |
1896 | ||
1897 | local_got = elf_local_got_refcounts (ibfd); | |
1898 | if (!local_got) | |
1899 | continue; | |
1900 | ||
1901 | symtab_hdr = &elf_tdata (ibfd)->symtab_hdr; | |
1902 | locsymcount = symtab_hdr->sh_info; | |
1903 | end_local_got = local_got + locsymcount; | |
1904 | local_tls_type = elf32_sparc_local_got_tls_type (ibfd); | |
1905 | s = htab->sgot; | |
1906 | srel = htab->srelgot; | |
1907 | for (; local_got < end_local_got; ++local_got, ++local_tls_type) | |
1908 | { | |
1909 | if (*local_got > 0) | |
1910 | { | |
eea6121a AM |
1911 | *local_got = s->size; |
1912 | s->size += 4; | |
b9734f35 | 1913 | if (*local_tls_type == GOT_TLS_GD) |
eea6121a | 1914 | s->size += 4; |
b9734f35 JJ |
1915 | if (info->shared |
1916 | || *local_tls_type == GOT_TLS_GD | |
1917 | || *local_tls_type == GOT_TLS_IE) | |
eea6121a | 1918 | srel->size += sizeof (Elf32_External_Rela); |
b9734f35 JJ |
1919 | } |
1920 | else | |
1921 | *local_got = (bfd_vma) -1; | |
1922 | } | |
1923 | } | |
1924 | ||
1925 | if (htab->tls_ldm_got.refcount > 0) | |
1926 | { | |
1927 | /* Allocate 2 got entries and 1 dynamic reloc for | |
1928 | R_SPARC_TLS_LDM_{HI22,LO10} relocs. */ | |
eea6121a AM |
1929 | htab->tls_ldm_got.offset = htab->sgot->size; |
1930 | htab->sgot->size += 8; | |
1931 | htab->srelgot->size += sizeof (Elf32_External_Rela); | |
252b5132 RH |
1932 | } |
1933 | else | |
b9734f35 JJ |
1934 | htab->tls_ldm_got.offset = -1; |
1935 | ||
1936 | /* Allocate global sym .plt and .got entries, and space for global | |
1937 | sym dynamic relocs. */ | |
1938 | elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info); | |
1939 | ||
1940 | if (elf_hash_table (info)->dynamic_sections_created) | |
252b5132 | 1941 | { |
b9734f35 | 1942 | /* Make space for the trailing nop in .plt. */ |
eea6121a AM |
1943 | if (htab->splt->size > 0) |
1944 | htab->splt->size += 4; | |
b9734f35 JJ |
1945 | |
1946 | /* If the .got section is more than 0x1000 bytes, we add | |
1947 | 0x1000 to the value of _GLOBAL_OFFSET_TABLE_, so that 13 | |
1948 | bit relocations have a greater chance of working. */ | |
eea6121a | 1949 | if (htab->sgot->size >= 0x1000 |
b9734f35 JJ |
1950 | && elf_hash_table (info)->hgot->root.u.def.value == 0) |
1951 | elf_hash_table (info)->hgot->root.u.def.value = 0x1000; | |
252b5132 RH |
1952 | } |
1953 | ||
1954 | /* The check_relocs and adjust_dynamic_symbol entry points have | |
1955 | determined the sizes of the various dynamic sections. Allocate | |
1956 | memory for them. */ | |
252b5132 RH |
1957 | for (s = dynobj->sections; s != NULL; s = s->next) |
1958 | { | |
1959 | const char *name; | |
b9734f35 | 1960 | bfd_boolean strip = FALSE; |
252b5132 RH |
1961 | |
1962 | if ((s->flags & SEC_LINKER_CREATED) == 0) | |
1963 | continue; | |
1964 | ||
1965 | /* It's OK to base decisions on the section name, because none | |
1966 | of the dynobj section names depend upon the input files. */ | |
1967 | name = bfd_get_section_name (dynobj, s); | |
1968 | ||
252b5132 RH |
1969 | if (strncmp (name, ".rela", 5) == 0) |
1970 | { | |
eea6121a | 1971 | if (s->size == 0) |
252b5132 RH |
1972 | { |
1973 | /* If we don't need this section, strip it from the | |
1974 | output file. This is to handle .rela.bss and | |
1975 | .rel.plt. We must create it in | |
1976 | create_dynamic_sections, because it must be created | |
1977 | before the linker maps input sections to output | |
1978 | sections. The linker does that before | |
1979 | adjust_dynamic_symbol is called, and it is that | |
1980 | function which decides whether anything needs to go | |
1981 | into these sections. */ | |
b34976b6 | 1982 | strip = TRUE; |
252b5132 RH |
1983 | } |
1984 | else | |
1985 | { | |
252b5132 RH |
1986 | /* We use the reloc_count field as a counter if we need |
1987 | to copy relocs into the output file. */ | |
1988 | s->reloc_count = 0; | |
1989 | } | |
1990 | } | |
b9734f35 | 1991 | else if (s != htab->splt && s != htab->sgot) |
252b5132 RH |
1992 | { |
1993 | /* It's not one of our sections, so don't allocate space. */ | |
1994 | continue; | |
1995 | } | |
1996 | ||
1997 | if (strip) | |
1998 | { | |
7f8d5fc9 | 1999 | _bfd_strip_section_from_output (info, s); |
252b5132 RH |
2000 | continue; |
2001 | } | |
2002 | ||
2003 | /* Allocate memory for the section contents. */ | |
7a9af8c4 NC |
2004 | /* FIXME: This should be a call to bfd_alloc not bfd_zalloc. |
2005 | Unused entries should be reclaimed before the section's contents | |
2006 | are written out, but at the moment this does not happen. Thus in | |
2007 | order to prevent writing out garbage, we initialise the section's | |
2008 | contents to zero. */ | |
eea6121a AM |
2009 | s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size); |
2010 | if (s->contents == NULL && s->size != 0) | |
b34976b6 | 2011 | return FALSE; |
252b5132 RH |
2012 | } |
2013 | ||
2014 | if (elf_hash_table (info)->dynamic_sections_created) | |
2015 | { | |
2016 | /* Add some entries to the .dynamic section. We fill in the | |
2017 | values later, in elf32_sparc_finish_dynamic_sections, but we | |
2018 | must add the entries now so that we get the correct size for | |
2019 | the .dynamic section. The DT_DEBUG entry is filled in by the | |
2020 | dynamic linker and used by the debugger. */ | |
dc810e39 | 2021 | #define add_dynamic_entry(TAG, VAL) \ |
5a580b3a | 2022 | _bfd_elf_add_dynamic_entry (info, TAG, VAL) |
dc810e39 | 2023 | |
36af4a4e | 2024 | if (info->executable) |
252b5132 | 2025 | { |
dc810e39 | 2026 | if (!add_dynamic_entry (DT_DEBUG, 0)) |
b34976b6 | 2027 | return FALSE; |
252b5132 RH |
2028 | } |
2029 | ||
eea6121a | 2030 | if (htab->srelplt->size != 0) |
252b5132 | 2031 | { |
dc810e39 AM |
2032 | if (!add_dynamic_entry (DT_PLTGOT, 0) |
2033 | || !add_dynamic_entry (DT_PLTRELSZ, 0) | |
2034 | || !add_dynamic_entry (DT_PLTREL, DT_RELA) | |
2035 | || !add_dynamic_entry (DT_JMPREL, 0)) | |
b34976b6 | 2036 | return FALSE; |
252b5132 RH |
2037 | } |
2038 | ||
dc810e39 AM |
2039 | if (!add_dynamic_entry (DT_RELA, 0) |
2040 | || !add_dynamic_entry (DT_RELASZ, 0) | |
2041 | || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela))) | |
b34976b6 | 2042 | return FALSE; |
252b5132 | 2043 | |
b9734f35 JJ |
2044 | /* If any dynamic relocs apply to a read-only section, |
2045 | then we need a DT_TEXTREL entry. */ | |
2046 | if ((info->flags & DF_TEXTREL) == 0) | |
2047 | elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, | |
2048 | (PTR) info); | |
2049 | ||
db6751f2 | 2050 | if (info->flags & DF_TEXTREL) |
252b5132 | 2051 | { |
dc810e39 | 2052 | if (!add_dynamic_entry (DT_TEXTREL, 0)) |
b34976b6 | 2053 | return FALSE; |
252b5132 RH |
2054 | } |
2055 | } | |
dc810e39 | 2056 | #undef add_dynamic_entry |
252b5132 | 2057 | |
b34976b6 | 2058 | return TRUE; |
252b5132 RH |
2059 | } |
2060 | ||
f0abc2a1 AM |
2061 | struct elf32_sparc_section_data |
2062 | { | |
2063 | struct bfd_elf_section_data elf; | |
2064 | unsigned int do_relax; | |
2065 | }; | |
2066 | ||
2067 | #define sec_do_relax(sec) \ | |
68bfbfcc | 2068 | ((struct elf32_sparc_section_data *) elf_section_data (sec))->do_relax |
f0abc2a1 AM |
2069 | |
2070 | static bfd_boolean | |
2071 | elf32_sparc_new_section_hook (abfd, sec) | |
2072 | bfd *abfd; | |
2073 | asection *sec; | |
2074 | { | |
2075 | struct elf32_sparc_section_data *sdata; | |
2076 | bfd_size_type amt = sizeof (*sdata); | |
2077 | ||
2078 | sdata = (struct elf32_sparc_section_data *) bfd_zalloc (abfd, amt); | |
2079 | if (sdata == NULL) | |
2080 | return FALSE; | |
2081 | sec->used_by_bfd = (PTR) sdata; | |
2082 | ||
2083 | return _bfd_elf_new_section_hook (abfd, sec); | |
2084 | } | |
f7775d95 | 2085 | |
b34976b6 | 2086 | static bfd_boolean |
f7775d95 JJ |
2087 | elf32_sparc_relax_section (abfd, section, link_info, again) |
2088 | bfd *abfd ATTRIBUTE_UNUSED; | |
2089 | asection *section ATTRIBUTE_UNUSED; | |
2090 | struct bfd_link_info *link_info ATTRIBUTE_UNUSED; | |
b34976b6 | 2091 | bfd_boolean *again; |
f7775d95 | 2092 | { |
b34976b6 | 2093 | *again = FALSE; |
f0abc2a1 | 2094 | sec_do_relax (section) = 1; |
b34976b6 | 2095 | return TRUE; |
f7775d95 JJ |
2096 | } |
2097 | ||
b9734f35 JJ |
2098 | /* Return the base VMA address which should be subtracted from real addresses |
2099 | when resolving @dtpoff relocation. | |
2100 | This is PT_TLS segment p_vaddr. */ | |
2101 | ||
2102 | static bfd_vma | |
2103 | dtpoff_base (info) | |
2104 | struct bfd_link_info *info; | |
2105 | { | |
e1918d23 AM |
2106 | /* If tls_sec is NULL, we should have signalled an error already. */ |
2107 | if (elf_hash_table (info)->tls_sec == NULL) | |
b9734f35 | 2108 | return 0; |
e1918d23 | 2109 | return elf_hash_table (info)->tls_sec->vma; |
b9734f35 JJ |
2110 | } |
2111 | ||
2112 | /* Return the relocation value for @tpoff relocation | |
2113 | if STT_TLS virtual address is ADDRESS. */ | |
2114 | ||
2115 | static bfd_vma | |
2116 | tpoff (info, address) | |
2117 | struct bfd_link_info *info; | |
2118 | bfd_vma address; | |
2119 | { | |
e1918d23 | 2120 | struct elf_link_hash_table *htab = elf_hash_table (info); |
b9734f35 | 2121 | |
e1918d23 AM |
2122 | /* If tls_sec is NULL, we should have signalled an error already. */ |
2123 | if (htab->tls_sec == NULL) | |
b9734f35 | 2124 | return 0; |
e1918d23 | 2125 | return address - htab->tls_size - htab->tls_sec->vma; |
b9734f35 | 2126 | } |
e36f7d53 | 2127 | |
252b5132 RH |
2128 | /* Relocate a SPARC ELF section. */ |
2129 | ||
b34976b6 | 2130 | static bfd_boolean |
252b5132 RH |
2131 | elf32_sparc_relocate_section (output_bfd, info, input_bfd, input_section, |
2132 | contents, relocs, local_syms, local_sections) | |
2133 | bfd *output_bfd; | |
2134 | struct bfd_link_info *info; | |
2135 | bfd *input_bfd; | |
2136 | asection *input_section; | |
2137 | bfd_byte *contents; | |
2138 | Elf_Internal_Rela *relocs; | |
2139 | Elf_Internal_Sym *local_syms; | |
2140 | asection **local_sections; | |
2141 | { | |
b9734f35 | 2142 | struct elf32_sparc_link_hash_table *htab; |
252b5132 RH |
2143 | Elf_Internal_Shdr *symtab_hdr; |
2144 | struct elf_link_hash_entry **sym_hashes; | |
2145 | bfd_vma *local_got_offsets; | |
2146 | bfd_vma got_base; | |
252b5132 RH |
2147 | asection *sreloc; |
2148 | Elf_Internal_Rela *rel; | |
2149 | Elf_Internal_Rela *relend; | |
2150 | ||
1049f94e | 2151 | if (info->relocatable) |
b34976b6 | 2152 | return TRUE; |
f0fe0e16 | 2153 | |
b9734f35 | 2154 | htab = elf32_sparc_hash_table (info); |
252b5132 RH |
2155 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
2156 | sym_hashes = elf_sym_hashes (input_bfd); | |
2157 | local_got_offsets = elf_local_got_offsets (input_bfd); | |
2158 | ||
2159 | if (elf_hash_table (info)->hgot == NULL) | |
2160 | got_base = 0; | |
2161 | else | |
2162 | got_base = elf_hash_table (info)->hgot->root.u.def.value; | |
2163 | ||
b9734f35 | 2164 | sreloc = elf_section_data (input_section)->sreloc; |
252b5132 RH |
2165 | |
2166 | rel = relocs; | |
2167 | relend = relocs + input_section->reloc_count; | |
2168 | for (; rel < relend; rel++) | |
2169 | { | |
b9734f35 | 2170 | int r_type, tls_type; |
252b5132 RH |
2171 | reloc_howto_type *howto; |
2172 | unsigned long r_symndx; | |
2173 | struct elf_link_hash_entry *h; | |
2174 | Elf_Internal_Sym *sym; | |
2175 | asection *sec; | |
e36f7d53 | 2176 | bfd_vma relocation, off; |
252b5132 | 2177 | bfd_reloc_status_type r; |
b34976b6 AM |
2178 | bfd_boolean is_plt = FALSE; |
2179 | bfd_boolean unresolved_reloc; | |
252b5132 RH |
2180 | |
2181 | r_type = ELF32_R_TYPE (rel->r_info); | |
2182 | ||
435b1e90 | 2183 | if (r_type == R_SPARC_GNU_VTINHERIT |
26e41594 AM |
2184 | || r_type == R_SPARC_GNU_VTENTRY) |
2185 | continue; | |
252b5132 | 2186 | |
60dac299 | 2187 | if (r_type < 0 || r_type >= (int) R_SPARC_max_std) |
252b5132 RH |
2188 | { |
2189 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 2190 | return FALSE; |
252b5132 RH |
2191 | } |
2192 | howto = _bfd_sparc_elf_howto_table + r_type; | |
2193 | ||
252b5132 | 2194 | /* This is a final link. */ |
f0fe0e16 | 2195 | r_symndx = ELF32_R_SYM (rel->r_info); |
252b5132 RH |
2196 | h = NULL; |
2197 | sym = NULL; | |
2198 | sec = NULL; | |
b34976b6 | 2199 | unresolved_reloc = FALSE; |
252b5132 RH |
2200 | if (r_symndx < symtab_hdr->sh_info) |
2201 | { | |
2202 | sym = local_syms + r_symndx; | |
2203 | sec = local_sections[r_symndx]; | |
8517fae7 | 2204 | relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel); |
252b5132 RH |
2205 | } |
2206 | else | |
2207 | { | |
560e09e9 | 2208 | bfd_boolean warned ATTRIBUTE_UNUSED; |
e36f7d53 | 2209 | |
b2a8e766 AM |
2210 | RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel, |
2211 | r_symndx, symtab_hdr, sym_hashes, | |
2212 | h, sec, relocation, | |
2213 | unresolved_reloc, warned); | |
252b5132 RH |
2214 | } |
2215 | ||
2216 | switch (r_type) | |
2217 | { | |
2218 | case R_SPARC_GOT10: | |
2219 | case R_SPARC_GOT13: | |
2220 | case R_SPARC_GOT22: | |
2221 | /* Relocation is to the entry for this symbol in the global | |
26e41594 | 2222 | offset table. */ |
b9734f35 JJ |
2223 | if (htab->sgot == NULL) |
2224 | abort (); | |
252b5132 RH |
2225 | |
2226 | if (h != NULL) | |
2227 | { | |
b34976b6 | 2228 | bfd_boolean dyn; |
252b5132 RH |
2229 | |
2230 | off = h->got.offset; | |
2231 | BFD_ASSERT (off != (bfd_vma) -1); | |
e36f7d53 | 2232 | dyn = elf_hash_table (info)->dynamic_sections_created; |
252b5132 | 2233 | |
c152c796 | 2234 | if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h) |
252b5132 | 2235 | || (info->shared |
e36f7d53 RH |
2236 | && (info->symbolic |
2237 | || h->dynindx == -1 | |
f5385ebf AM |
2238 | || h->forced_local) |
2239 | && h->def_regular)) | |
252b5132 RH |
2240 | { |
2241 | /* This is actually a static link, or it is a | |
26e41594 AM |
2242 | -Bsymbolic link and the symbol is defined |
2243 | locally, or the symbol was forced to be local | |
2244 | because of a version file. We must initialize | |
2245 | this entry in the global offset table. Since the | |
2246 | offset must always be a multiple of 4, we use the | |
2247 | least significant bit to record whether we have | |
2248 | initialized it already. | |
252b5132 RH |
2249 | |
2250 | When doing a dynamic link, we create a .rela.got | |
2251 | relocation entry to initialize the value. This | |
2252 | is done in the finish_dynamic_symbol routine. */ | |
2253 | if ((off & 1) != 0) | |
2254 | off &= ~1; | |
2255 | else | |
2256 | { | |
2257 | bfd_put_32 (output_bfd, relocation, | |
b9734f35 | 2258 | htab->sgot->contents + off); |
252b5132 RH |
2259 | h->got.offset |= 1; |
2260 | } | |
2261 | } | |
e36f7d53 | 2262 | else |
b34976b6 | 2263 | unresolved_reloc = FALSE; |
252b5132 RH |
2264 | } |
2265 | else | |
2266 | { | |
252b5132 RH |
2267 | BFD_ASSERT (local_got_offsets != NULL |
2268 | && local_got_offsets[r_symndx] != (bfd_vma) -1); | |
2269 | ||
2270 | off = local_got_offsets[r_symndx]; | |
2271 | ||
2272 | /* The offset must always be a multiple of 4. We use | |
2273 | the least significant bit to record whether we have | |
2274 | already processed this entry. */ | |
2275 | if ((off & 1) != 0) | |
2276 | off &= ~1; | |
2277 | else | |
2278 | { | |
252b5132 RH |
2279 | |
2280 | if (info->shared) | |
2281 | { | |
b34976b6 | 2282 | asection *s; |
252b5132 | 2283 | Elf_Internal_Rela outrel; |
b34976b6 | 2284 | bfd_byte *loc; |
252b5132 RH |
2285 | |
2286 | /* We need to generate a R_SPARC_RELATIVE reloc | |
2287 | for the dynamic linker. */ | |
b9734f35 | 2288 | s = htab->srelgot; |
b34976b6 | 2289 | BFD_ASSERT (s != NULL); |
252b5132 | 2290 | |
b9734f35 JJ |
2291 | outrel.r_offset = (htab->sgot->output_section->vma |
2292 | + htab->sgot->output_offset | |
252b5132 RH |
2293 | + off); |
2294 | outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE); | |
6b3ac709 JJ |
2295 | outrel.r_addend = relocation; |
2296 | relocation = 0; | |
b34976b6 AM |
2297 | loc = s->contents; |
2298 | loc += s->reloc_count++ * sizeof (Elf32_External_Rela); | |
2299 | bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc); | |
252b5132 RH |
2300 | } |
2301 | ||
b9734f35 JJ |
2302 | bfd_put_32 (output_bfd, relocation, |
2303 | htab->sgot->contents + off); | |
252b5132 RH |
2304 | local_got_offsets[r_symndx] |= 1; |
2305 | } | |
252b5132 | 2306 | } |
b9734f35 | 2307 | relocation = htab->sgot->output_offset + off - got_base; |
252b5132 RH |
2308 | break; |
2309 | ||
bd5e6e7e JJ |
2310 | case R_SPARC_PLT32: |
2311 | if (h == NULL || h->plt.offset == (bfd_vma) -1) | |
2312 | { | |
2313 | r_type = R_SPARC_32; | |
2314 | goto r_sparc_plt32; | |
2315 | } | |
2316 | /* Fall through. */ | |
252b5132 | 2317 | case R_SPARC_WPLT30: |
b9734f35 | 2318 | r_sparc_wplt30: |
252b5132 | 2319 | /* Relocation is to the entry for this symbol in the |
26e41594 | 2320 | procedure linkage table. */ |
252b5132 RH |
2321 | |
2322 | /* The Solaris native assembler will generate a WPLT30 reloc | |
2323 | for a local symbol if you assemble a call from one | |
2324 | section to another when using -K pic. We treat it as | |
2325 | WDISP30. */ | |
2326 | if (h == NULL) | |
2327 | break; | |
2328 | ||
6de00a78 | 2329 | if (h->plt.offset == (bfd_vma) -1 || htab->splt == NULL) |
252b5132 RH |
2330 | { |
2331 | /* We didn't make a PLT entry for this symbol. This | |
26e41594 AM |
2332 | happens when statically linking PIC code, or when |
2333 | using -Bsymbolic. */ | |
252b5132 RH |
2334 | break; |
2335 | } | |
2336 | ||
b9734f35 JJ |
2337 | relocation = (htab->splt->output_section->vma |
2338 | + htab->splt->output_offset | |
252b5132 | 2339 | + h->plt.offset); |
b34976b6 | 2340 | unresolved_reloc = FALSE; |
bd5e6e7e JJ |
2341 | if (r_type == R_SPARC_PLT32) |
2342 | { | |
2343 | r_type = R_SPARC_32; | |
b34976b6 | 2344 | is_plt = TRUE; |
bd5e6e7e JJ |
2345 | goto r_sparc_plt32; |
2346 | } | |
252b5132 RH |
2347 | break; |
2348 | ||
2349 | case R_SPARC_PC10: | |
2350 | case R_SPARC_PC22: | |
2351 | if (h != NULL | |
2352 | && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0) | |
2353 | break; | |
2354 | /* Fall through. */ | |
2355 | case R_SPARC_DISP8: | |
2356 | case R_SPARC_DISP16: | |
2357 | case R_SPARC_DISP32: | |
2358 | case R_SPARC_WDISP30: | |
2359 | case R_SPARC_WDISP22: | |
2360 | case R_SPARC_WDISP19: | |
2361 | case R_SPARC_WDISP16: | |
252b5132 RH |
2362 | case R_SPARC_8: |
2363 | case R_SPARC_16: | |
2364 | case R_SPARC_32: | |
2365 | case R_SPARC_HI22: | |
2366 | case R_SPARC_22: | |
2367 | case R_SPARC_13: | |
2368 | case R_SPARC_LO10: | |
0f2712ed | 2369 | case R_SPARC_UA16: |
252b5132 | 2370 | case R_SPARC_UA32: |
bd5e6e7e | 2371 | r_sparc_plt32: |
b9734f35 JJ |
2372 | /* r_symndx will be zero only for relocs against symbols |
2373 | from removed linkonce sections, or sections discarded by | |
2374 | a linker script. */ | |
2375 | if (r_symndx == 0 | |
2376 | || (input_section->flags & SEC_ALLOC) == 0) | |
2377 | break; | |
2378 | ||
2379 | if ((info->shared | |
4dc570c2 JJ |
2380 | && (h == NULL |
2381 | || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
2382 | || h->root.type != bfd_link_hash_undefweak) | |
b9734f35 JJ |
2383 | && (! howto->pc_relative |
2384 | || (h != NULL | |
2385 | && h->dynindx != -1 | |
2386 | && (! info->symbolic | |
f5385ebf | 2387 | || !h->def_regular)))) |
b9734f35 JJ |
2388 | || (!info->shared |
2389 | && h != NULL | |
2390 | && h->dynindx != -1 | |
f5385ebf AM |
2391 | && !h->non_got_ref |
2392 | && ((h->def_dynamic | |
2393 | && !h->def_regular) | |
b9734f35 JJ |
2394 | || h->root.type == bfd_link_hash_undefweak |
2395 | || h->root.type == bfd_link_hash_undefined))) | |
252b5132 RH |
2396 | { |
2397 | Elf_Internal_Rela outrel; | |
b34976b6 AM |
2398 | bfd_byte *loc; |
2399 | bfd_boolean skip, relocate = FALSE; | |
252b5132 RH |
2400 | |
2401 | /* When generating a shared object, these relocations | |
26e41594 AM |
2402 | are copied into the output file to be resolved at run |
2403 | time. */ | |
252b5132 | 2404 | |
b9734f35 | 2405 | BFD_ASSERT (sreloc != NULL); |
252b5132 | 2406 | |
b34976b6 | 2407 | skip = FALSE; |
252b5132 | 2408 | |
c629eae0 JJ |
2409 | outrel.r_offset = |
2410 | _bfd_elf_section_offset (output_bfd, info, input_section, | |
2411 | rel->r_offset); | |
2412 | if (outrel.r_offset == (bfd_vma) -1) | |
b34976b6 | 2413 | skip = TRUE; |
0bb2d96a | 2414 | else if (outrel.r_offset == (bfd_vma) -2) |
b34976b6 | 2415 | skip = TRUE, relocate = TRUE; |
252b5132 RH |
2416 | outrel.r_offset += (input_section->output_section->vma |
2417 | + input_section->output_offset); | |
2418 | ||
20278fa3 JJ |
2419 | /* Optimize unaligned reloc usage now that we know where |
2420 | it finally resides. */ | |
2421 | switch (r_type) | |
2422 | { | |
2423 | case R_SPARC_16: | |
2424 | if (outrel.r_offset & 1) | |
2425 | r_type = R_SPARC_UA16; | |
2426 | break; | |
2427 | case R_SPARC_UA16: | |
2428 | if (!(outrel.r_offset & 1)) | |
2429 | r_type = R_SPARC_16; | |
2430 | break; | |
2431 | case R_SPARC_32: | |
2432 | if (outrel.r_offset & 3) | |
2433 | r_type = R_SPARC_UA32; | |
2434 | break; | |
2435 | case R_SPARC_UA32: | |
2436 | if (!(outrel.r_offset & 3)) | |
2437 | r_type = R_SPARC_32; | |
2438 | break; | |
b88a866a JJ |
2439 | case R_SPARC_DISP8: |
2440 | case R_SPARC_DISP16: | |
2441 | case R_SPARC_DISP32: | |
2442 | /* If the symbol is not dynamic, we should not keep | |
2443 | a dynamic relocation. But an .rela.* slot has been | |
2444 | allocated for it, output R_SPARC_NONE. | |
2445 | FIXME: Add code tracking needed dynamic relocs as | |
2446 | e.g. i386 has. */ | |
2447 | if (h->dynindx == -1) | |
b34976b6 | 2448 | skip = TRUE, relocate = TRUE; |
b88a866a | 2449 | break; |
20278fa3 JJ |
2450 | } |
2451 | ||
252b5132 RH |
2452 | if (skip) |
2453 | memset (&outrel, 0, sizeof outrel); | |
2454 | /* h->dynindx may be -1 if the symbol was marked to | |
26e41594 | 2455 | become local. */ |
bd5e6e7e | 2456 | else if (h != NULL && ! is_plt |
252b5132 | 2457 | && ((! info->symbolic && h->dynindx != -1) |
f5385ebf | 2458 | || !h->def_regular)) |
252b5132 RH |
2459 | { |
2460 | BFD_ASSERT (h->dynindx != -1); | |
2461 | outrel.r_info = ELF32_R_INFO (h->dynindx, r_type); | |
2462 | outrel.r_addend = rel->r_addend; | |
2463 | } | |
2464 | else | |
2465 | { | |
1fbc4a84 | 2466 | if (r_type == R_SPARC_32) |
252b5132 RH |
2467 | { |
2468 | outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE); | |
2469 | outrel.r_addend = relocation + rel->r_addend; | |
2470 | } | |
2471 | else | |
2472 | { | |
2473 | long indx; | |
2474 | ||
bd5e6e7e | 2475 | if (is_plt) |
b9734f35 | 2476 | sec = htab->splt; |
8517fae7 AM |
2477 | |
2478 | if (bfd_is_abs_section (sec)) | |
252b5132 RH |
2479 | indx = 0; |
2480 | else if (sec == NULL || sec->owner == NULL) | |
2481 | { | |
2482 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 2483 | return FALSE; |
252b5132 RH |
2484 | } |
2485 | else | |
2486 | { | |
2487 | asection *osec; | |
2488 | ||
2489 | osec = sec->output_section; | |
2490 | indx = elf_section_data (osec)->dynindx; | |
2491 | ||
2492 | /* FIXME: we really should be able to link non-pic | |
2493 | shared libraries. */ | |
2494 | if (indx == 0) | |
2495 | { | |
2496 | BFD_FAIL (); | |
2497 | (*_bfd_error_handler) | |
d003868e AM |
2498 | (_("%B: probably compiled without -fPIC?"), |
2499 | input_bfd); | |
252b5132 | 2500 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 2501 | return FALSE; |
252b5132 RH |
2502 | } |
2503 | } | |
2504 | ||
2505 | outrel.r_info = ELF32_R_INFO (indx, r_type); | |
840a9995 | 2506 | outrel.r_addend = relocation + rel->r_addend; |
252b5132 RH |
2507 | } |
2508 | } | |
2509 | ||
b34976b6 AM |
2510 | loc = sreloc->contents; |
2511 | loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela); | |
2512 | bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc); | |
252b5132 RH |
2513 | |
2514 | /* This reloc will be computed at runtime, so there's no | |
26e41594 | 2515 | need to do anything now. */ |
0bb2d96a JJ |
2516 | if (! relocate) |
2517 | continue; | |
252b5132 RH |
2518 | } |
2519 | break; | |
2520 | ||
b9734f35 JJ |
2521 | case R_SPARC_TLS_GD_HI22: |
2522 | if (! elf32_sparc_tdata (input_bfd)->has_tlsgd) | |
2523 | { | |
2524 | /* R_SPARC_REV32 used the same reloc number as | |
2525 | R_SPARC_TLS_GD_HI22. */ | |
2526 | r_type = R_SPARC_REV32; | |
2527 | break; | |
2528 | } | |
2529 | /* Fall through */ | |
2530 | ||
2531 | case R_SPARC_TLS_GD_LO10: | |
2532 | case R_SPARC_TLS_IE_HI22: | |
2533 | case R_SPARC_TLS_IE_LO10: | |
2534 | r_type = elf32_sparc_tls_transition (info, input_bfd, r_type, | |
2535 | h == NULL); | |
2536 | tls_type = GOT_UNKNOWN; | |
2537 | if (h == NULL && local_got_offsets) | |
2538 | tls_type = elf32_sparc_local_got_tls_type (input_bfd) [r_symndx]; | |
2539 | else if (h != NULL) | |
2540 | { | |
2541 | tls_type = elf32_sparc_hash_entry(h)->tls_type; | |
2542 | if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE) | |
2543 | switch (ELF32_R_TYPE (rel->r_info)) | |
2544 | { | |
2545 | case R_SPARC_TLS_GD_HI22: | |
2546 | case R_SPARC_TLS_IE_HI22: | |
2547 | r_type = R_SPARC_TLS_LE_HIX22; | |
2548 | break; | |
2549 | default: | |
2550 | r_type = R_SPARC_TLS_LE_LOX10; | |
2551 | break; | |
2552 | } | |
2553 | } | |
2554 | if (tls_type == GOT_TLS_IE) | |
2555 | switch (r_type) | |
2556 | { | |
2557 | case R_SPARC_TLS_GD_HI22: | |
2558 | r_type = R_SPARC_TLS_IE_HI22; | |
2559 | break; | |
2560 | case R_SPARC_TLS_GD_LO10: | |
2561 | r_type = R_SPARC_TLS_IE_LO10; | |
2562 | break; | |
2563 | } | |
2564 | ||
2565 | if (r_type == R_SPARC_TLS_LE_HIX22) | |
2566 | { | |
2567 | relocation = tpoff (info, relocation); | |
2568 | break; | |
2569 | } | |
2570 | if (r_type == R_SPARC_TLS_LE_LOX10) | |
2571 | { | |
2572 | /* Change add into xor. */ | |
2573 | relocation = tpoff (info, relocation); | |
2574 | bfd_put_32 (output_bfd, (bfd_get_32 (input_bfd, | |
2575 | contents + rel->r_offset) | |
2576 | | 0x80182000), contents + rel->r_offset); | |
2577 | break; | |
2578 | } | |
2579 | ||
26e41594 | 2580 | if (h != NULL) |
b9734f35 JJ |
2581 | { |
2582 | off = h->got.offset; | |
2583 | h->got.offset |= 1; | |
2584 | } | |
2585 | else | |
2586 | { | |
2587 | BFD_ASSERT (local_got_offsets != NULL); | |
2588 | off = local_got_offsets[r_symndx]; | |
2589 | local_got_offsets[r_symndx] |= 1; | |
2590 | } | |
26e41594 | 2591 | |
b9734f35 JJ |
2592 | r_sparc_tlsldm: |
2593 | if (htab->sgot == NULL) | |
2594 | abort (); | |
2595 | ||
2596 | if ((off & 1) != 0) | |
2597 | off &= ~1; | |
26e41594 | 2598 | else |
b9734f35 JJ |
2599 | { |
2600 | Elf_Internal_Rela outrel; | |
2601 | Elf32_External_Rela *loc; | |
2602 | int dr_type, indx; | |
2603 | ||
2604 | if (htab->srelgot == NULL) | |
2605 | abort (); | |
2606 | ||
2607 | bfd_put_32 (output_bfd, 0, htab->sgot->contents + off); | |
2608 | outrel.r_offset = (htab->sgot->output_section->vma | |
2609 | + htab->sgot->output_offset + off); | |
2610 | indx = h && h->dynindx != -1 ? h->dynindx : 0; | |
2611 | if (r_type == R_SPARC_TLS_IE_HI22 | |
2612 | || r_type == R_SPARC_TLS_IE_LO10) | |
2613 | dr_type = R_SPARC_TLS_TPOFF32; | |
2614 | else | |
2615 | dr_type = R_SPARC_TLS_DTPMOD32; | |
2616 | if (dr_type == R_SPARC_TLS_TPOFF32 && indx == 0) | |
2617 | outrel.r_addend = relocation - dtpoff_base (info); | |
2618 | else | |
2619 | outrel.r_addend = 0; | |
2620 | outrel.r_info = ELF32_R_INFO (indx, dr_type); | |
2621 | loc = (Elf32_External_Rela *) htab->srelgot->contents; | |
2622 | loc += htab->srelgot->reloc_count++; | |
2623 | bfd_elf32_swap_reloca_out (output_bfd, &outrel, | |
2624 | (bfd_byte *) loc); | |
2625 | ||
2626 | if (r_type == R_SPARC_TLS_GD_HI22 | |
2627 | || r_type == R_SPARC_TLS_GD_LO10) | |
2628 | { | |
2629 | if (indx == 0) | |
2630 | { | |
2631 | BFD_ASSERT (! unresolved_reloc); | |
2632 | bfd_put_32 (output_bfd, | |
2633 | relocation - dtpoff_base (info), | |
2634 | htab->sgot->contents + off + 4); | |
2635 | } | |
2636 | else | |
2637 | { | |
2638 | bfd_put_32 (output_bfd, 0, | |
2639 | htab->sgot->contents + off + 4); | |
2640 | outrel.r_info = ELF32_R_INFO (indx, | |
2641 | R_SPARC_TLS_DTPOFF32); | |
2642 | outrel.r_offset += 4; | |
2643 | htab->srelgot->reloc_count++; | |
2644 | loc++; | |
2645 | bfd_elf32_swap_reloca_out (output_bfd, &outrel, | |
2646 | (bfd_byte *) loc); | |
2647 | } | |
2648 | } | |
2649 | else if (dr_type == R_SPARC_TLS_DTPMOD32) | |
2650 | { | |
2651 | bfd_put_32 (output_bfd, 0, | |
2652 | htab->sgot->contents + off + 4); | |
2653 | } | |
2654 | } | |
2655 | ||
2656 | if (off >= (bfd_vma) -2) | |
2657 | abort (); | |
2658 | ||
2659 | relocation = htab->sgot->output_offset + off - got_base; | |
2660 | unresolved_reloc = FALSE; | |
2661 | howto = _bfd_sparc_elf_howto_table + r_type; | |
2662 | break; | |
2663 | ||
2664 | case R_SPARC_TLS_LDM_HI22: | |
2665 | case R_SPARC_TLS_LDM_LO10: | |
2666 | if (! info->shared) | |
2667 | { | |
2668 | bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset); | |
2669 | continue; | |
2670 | } | |
2671 | off = htab->tls_ldm_got.offset; | |
2672 | htab->tls_ldm_got.offset |= 1; | |
2673 | goto r_sparc_tlsldm; | |
2674 | ||
2675 | case R_SPARC_TLS_LDO_HIX22: | |
2676 | case R_SPARC_TLS_LDO_LOX10: | |
2677 | if (info->shared) | |
2678 | relocation -= dtpoff_base (info); | |
2679 | else | |
2680 | relocation = tpoff (info, relocation); | |
2681 | break; | |
2682 | ||
2683 | case R_SPARC_TLS_LE_HIX22: | |
2684 | case R_SPARC_TLS_LE_LOX10: | |
2685 | if (info->shared) | |
2686 | { | |
2687 | Elf_Internal_Rela outrel; | |
2688 | bfd_boolean skip, relocate = FALSE; | |
2689 | ||
2690 | BFD_ASSERT (sreloc != NULL); | |
2691 | skip = FALSE; | |
2692 | outrel.r_offset = | |
2693 | _bfd_elf_section_offset (output_bfd, info, input_section, | |
2694 | rel->r_offset); | |
2695 | if (outrel.r_offset == (bfd_vma) -1) | |
2696 | skip = TRUE; | |
2697 | else if (outrel.r_offset == (bfd_vma) -2) | |
2698 | skip = TRUE, relocate = TRUE; | |
2699 | outrel.r_offset += (input_section->output_section->vma | |
2700 | + input_section->output_offset); | |
2701 | if (skip) | |
2702 | memset (&outrel, 0, sizeof outrel); | |
2703 | else | |
2704 | { | |
2705 | outrel.r_info = ELF32_R_INFO (0, r_type); | |
2706 | outrel.r_addend = relocation - dtpoff_base (info) | |
2707 | + rel->r_addend; | |
2708 | } | |
2709 | ||
2710 | bfd_elf32_swap_reloca_out (output_bfd, &outrel, | |
2711 | (bfd_byte *) (((Elf32_External_Rela *) | |
2712 | sreloc->contents) | |
2713 | + sreloc->reloc_count)); | |
2714 | ++sreloc->reloc_count; | |
2715 | continue; | |
2716 | } | |
2717 | relocation = tpoff (info, relocation); | |
2718 | break; | |
2719 | ||
2720 | case R_SPARC_TLS_LDM_CALL: | |
2721 | if (! info->shared) | |
2722 | { | |
2723 | /* mov %g0, %o0 */ | |
2724 | bfd_put_32 (output_bfd, 0x90100000, contents + rel->r_offset); | |
2725 | continue; | |
2726 | } | |
2727 | /* Fall through */ | |
2728 | ||
2729 | case R_SPARC_TLS_GD_CALL: | |
2730 | tls_type = GOT_UNKNOWN; | |
2731 | if (h == NULL && local_got_offsets) | |
2732 | tls_type = elf32_sparc_local_got_tls_type (input_bfd) [r_symndx]; | |
2733 | else if (h != NULL) | |
2734 | tls_type = elf32_sparc_hash_entry(h)->tls_type; | |
2735 | if (! info->shared | |
2736 | || (r_type == R_SPARC_TLS_GD_CALL && tls_type == GOT_TLS_IE)) | |
2737 | { | |
2738 | bfd_vma insn; | |
2739 | ||
2740 | if (!info->shared && (h == NULL || h->dynindx == -1)) | |
2741 | { | |
2742 | /* GD -> LE */ | |
2743 | bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset); | |
2744 | continue; | |
2745 | } | |
2746 | ||
2747 | /* GD -> IE */ | |
2748 | if (rel + 1 < relend | |
2749 | && ELF32_R_TYPE (rel[1].r_info) == R_SPARC_TLS_GD_ADD | |
2750 | && rel[1].r_offset == rel->r_offset + 4 | |
2751 | && ELF32_R_SYM (rel[1].r_info) == r_symndx | |
2752 | && (((insn = bfd_get_32 (input_bfd, | |
2753 | contents + rel[1].r_offset)) | |
2754 | >> 25) & 0x1f) == 8) | |
2755 | { | |
2756 | /* We have | |
2757 | call __tls_get_addr, %tgd_call(foo) | |
2758 | add %reg1, %reg2, %o0, %tgd_add(foo) | |
2759 | and change it into IE: | |
2760 | ld [%reg1 + %reg2], %o0, %tie_ld(foo) | |
2761 | add %g7, %o0, %o0, %tie_add(foo). | |
2762 | add is 0x80000000 | (rd << 25) | (rs1 << 14) | rs2, | |
2763 | ld is 0xc0000000 | (rd << 25) | (rs1 << 14) | rs2. */ | |
2764 | bfd_put_32 (output_bfd, insn | 0xc0000000, | |
2765 | contents + rel->r_offset); | |
2766 | bfd_put_32 (output_bfd, 0x9001c008, | |
2767 | contents + rel->r_offset + 4); | |
2768 | rel++; | |
2769 | continue; | |
2770 | } | |
2771 | ||
2772 | bfd_put_32 (output_bfd, 0x9001c008, contents + rel->r_offset); | |
2773 | continue; | |
2774 | } | |
2775 | ||
2776 | h = (struct elf_link_hash_entry *) | |
2777 | bfd_link_hash_lookup (info->hash, "__tls_get_addr", FALSE, | |
2778 | FALSE, TRUE); | |
2779 | BFD_ASSERT (h != NULL); | |
2780 | r_type = R_SPARC_WPLT30; | |
2781 | howto = _bfd_sparc_elf_howto_table + r_type; | |
2782 | goto r_sparc_wplt30; | |
2783 | ||
2784 | case R_SPARC_TLS_GD_ADD: | |
2785 | tls_type = GOT_UNKNOWN; | |
2786 | if (h == NULL && local_got_offsets) | |
2787 | tls_type = elf32_sparc_local_got_tls_type (input_bfd) [r_symndx]; | |
2788 | else if (h != NULL) | |
2789 | tls_type = elf32_sparc_hash_entry(h)->tls_type; | |
2790 | if (! info->shared || tls_type == GOT_TLS_IE) | |
2791 | { | |
2792 | /* add %reg1, %reg2, %reg3, %tgd_add(foo) | |
2793 | changed into IE: | |
2794 | ld [%reg1 + %reg2], %reg3, %tie_ld(foo) | |
2795 | or LE: | |
2796 | add %g7, %reg2, %reg3. */ | |
2797 | bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset); | |
2798 | if ((h != NULL && h->dynindx != -1) || info->shared) | |
2799 | relocation = insn | 0xc0000000; | |
2800 | else | |
2801 | relocation = (insn & ~0x7c000) | 0x1c000; | |
2802 | bfd_put_32 (output_bfd, relocation, contents + rel->r_offset); | |
2803 | } | |
2804 | continue; | |
2805 | ||
2806 | case R_SPARC_TLS_LDM_ADD: | |
2807 | if (! info->shared) | |
2808 | bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset); | |
2809 | continue; | |
2810 | ||
2811 | case R_SPARC_TLS_LDO_ADD: | |
2812 | if (! info->shared) | |
2813 | { | |
2814 | /* Change rs1 into %g7. */ | |
2815 | bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset); | |
2816 | insn = (insn & ~0x7c000) | 0x1c000; | |
2817 | bfd_put_32 (output_bfd, insn, contents + rel->r_offset); | |
2818 | } | |
2819 | continue; | |
2820 | ||
2821 | case R_SPARC_TLS_IE_LD: | |
2822 | case R_SPARC_TLS_IE_LDX: | |
2823 | if (! info->shared && (h == NULL || h->dynindx == -1)) | |
2824 | { | |
2825 | bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset); | |
2826 | int rs2 = insn & 0x1f; | |
2827 | int rd = (insn >> 25) & 0x1f; | |
2828 | ||
2829 | if (rs2 == rd) | |
2830 | relocation = SPARC_NOP; | |
2831 | else | |
2832 | relocation = 0x80100000 | (insn & 0x3e00001f); | |
2833 | bfd_put_32 (output_bfd, relocation, contents + rel->r_offset); | |
2834 | } | |
2835 | continue; | |
2836 | ||
2837 | case R_SPARC_TLS_IE_ADD: | |
2838 | /* Totally useless relocation. */ | |
2839 | continue; | |
2840 | ||
2841 | case R_SPARC_TLS_DTPOFF32: | |
2842 | relocation -= dtpoff_base (info); | |
2843 | break; | |
2844 | ||
252b5132 RH |
2845 | default: |
2846 | break; | |
2847 | } | |
2848 | ||
239e1f3a AM |
2849 | /* Dynamic relocs are not propagated for SEC_DEBUGGING sections |
2850 | because such sections are not SEC_ALLOC and thus ld.so will | |
2851 | not process them. */ | |
e36f7d53 | 2852 | if (unresolved_reloc |
239e1f3a | 2853 | && !((input_section->flags & SEC_DEBUGGING) != 0 |
f5385ebf | 2854 | && h->def_dynamic)) |
e36f7d53 | 2855 | (*_bfd_error_handler) |
d003868e AM |
2856 | (_("%B(%A+0x%lx): unresolvable relocation against symbol `%s'"), |
2857 | input_bfd, | |
2858 | input_section, | |
e36f7d53 RH |
2859 | (long) rel->r_offset, |
2860 | h->root.root.string); | |
2861 | ||
f7775d95 | 2862 | r = bfd_reloc_continue; |
252b5132 RH |
2863 | if (r_type == R_SPARC_WDISP16) |
2864 | { | |
2865 | bfd_vma x; | |
2866 | ||
2867 | relocation += rel->r_addend; | |
2868 | relocation -= (input_section->output_section->vma | |
2869 | + input_section->output_offset); | |
2870 | relocation -= rel->r_offset; | |
2871 | ||
2872 | x = bfd_get_32 (input_bfd, contents + rel->r_offset); | |
2873 | x |= ((((relocation >> 2) & 0xc000) << 6) | |
2874 | | ((relocation >> 2) & 0x3fff)); | |
2875 | bfd_put_32 (input_bfd, x, contents + rel->r_offset); | |
2876 | ||
2877 | if ((bfd_signed_vma) relocation < - 0x40000 | |
2878 | || (bfd_signed_vma) relocation > 0x3ffff) | |
2879 | r = bfd_reloc_overflow; | |
2880 | else | |
2881 | r = bfd_reloc_ok; | |
2882 | } | |
2883 | else if (r_type == R_SPARC_REV32) | |
2884 | { | |
2885 | bfd_vma x; | |
2886 | ||
2887 | relocation = relocation + rel->r_addend; | |
2888 | ||
2889 | x = bfd_get_32 (input_bfd, contents + rel->r_offset); | |
2890 | x = x + relocation; | |
2891 | bfd_putl32 (/*input_bfd,*/ x, contents + rel->r_offset); | |
b9734f35 JJ |
2892 | r = bfd_reloc_ok; |
2893 | } | |
2894 | else if (r_type == R_SPARC_TLS_LDO_HIX22 | |
2895 | || r_type == R_SPARC_TLS_LE_HIX22) | |
2896 | { | |
2897 | bfd_vma x; | |
2898 | ||
2899 | relocation += rel->r_addend; | |
2900 | relocation = relocation ^ 0xffffffff; | |
2901 | ||
2902 | x = bfd_get_32 (input_bfd, contents + rel->r_offset); | |
2903 | x = (x & ~(bfd_vma) 0x3fffff) | ((relocation >> 10) & 0x3fffff); | |
2904 | bfd_put_32 (input_bfd, x, contents + rel->r_offset); | |
2905 | r = bfd_reloc_ok; | |
2906 | } | |
2907 | else if (r_type == R_SPARC_TLS_LDO_LOX10 | |
2908 | || r_type == R_SPARC_TLS_LE_LOX10) | |
2909 | { | |
2910 | bfd_vma x; | |
2911 | ||
2912 | relocation += rel->r_addend; | |
2913 | relocation = (relocation & 0x3ff) | 0x1c00; | |
2914 | ||
2915 | x = bfd_get_32 (input_bfd, contents + rel->r_offset); | |
2916 | x = (x & ~(bfd_vma) 0x1fff) | relocation; | |
2917 | bfd_put_32 (input_bfd, x, contents + rel->r_offset); | |
2918 | ||
252b5132 RH |
2919 | r = bfd_reloc_ok; |
2920 | } | |
f7775d95 | 2921 | else if ((r_type == R_SPARC_WDISP30 || r_type == R_SPARC_WPLT30) |
f0abc2a1 | 2922 | && sec_do_relax (input_section) |
eea6121a | 2923 | && rel->r_offset + 4 < input_section->size) |
f7775d95 JJ |
2924 | { |
2925 | #define G0 0 | |
2926 | #define O7 15 | |
2927 | #define XCC (2 << 20) | |
2928 | #define COND(x) (((x)&0xf)<<25) | |
2929 | #define CONDA COND(0x8) | |
2930 | #define INSN_BPA (F2(0,1) | CONDA | BPRED | XCC) | |
2931 | #define INSN_BA (F2(0,2) | CONDA) | |
2932 | #define INSN_OR F3(2, 0x2, 0) | |
2933 | #define INSN_NOP F2(0,4) | |
2934 | ||
2935 | bfd_vma x, y; | |
2936 | ||
2937 | /* If the instruction is a call with either: | |
2938 | restore | |
2939 | arithmetic instruction with rd == %o7 | |
2940 | where rs1 != %o7 and rs2 if it is register != %o7 | |
2941 | then we can optimize if the call destination is near | |
2942 | by changing the call into a branch always. */ | |
2943 | x = bfd_get_32 (input_bfd, contents + rel->r_offset); | |
2944 | y = bfd_get_32 (input_bfd, contents + rel->r_offset + 4); | |
2945 | if ((x & OP(~0)) == OP(1) && (y & OP(~0)) == OP(2)) | |
2946 | { | |
2947 | if (((y & OP3(~0)) == OP3(0x3d) /* restore */ | |
2948 | || ((y & OP3(0x28)) == 0 /* arithmetic */ | |
2949 | && (y & RD(~0)) == RD(O7))) | |
2950 | && (y & RS1(~0)) != RS1(O7) | |
2951 | && ((y & F3I(~0)) | |
2952 | || (y & RS2(~0)) != RS2(O7))) | |
2953 | { | |
2954 | bfd_vma reloc; | |
2955 | ||
2956 | reloc = relocation + rel->r_addend - rel->r_offset; | |
2957 | reloc -= (input_section->output_section->vma | |
2958 | + input_section->output_offset); | |
2959 | ||
2960 | /* Ensure the reloc fits into simm22. */ | |
2961 | if ((reloc & 3) == 0 | |
2962 | && ((reloc & ~(bfd_vma)0x7fffff) == 0 | |
2963 | || ((reloc | 0x7fffff) == ~(bfd_vma)0))) | |
2964 | { | |
2965 | reloc >>= 2; | |
435b1e90 | 2966 | |
f7775d95 JJ |
2967 | /* Check whether it fits into simm19 on v9. */ |
2968 | if (((reloc & 0x3c0000) == 0 | |
2969 | || (reloc & 0x3c0000) == 0x3c0000) | |
2970 | && (elf_elfheader (output_bfd)->e_flags & EF_SPARC_32PLUS)) | |
2971 | x = INSN_BPA | (reloc & 0x7ffff); /* ba,pt %xcc */ | |
2972 | else | |
2973 | x = INSN_BA | (reloc & 0x3fffff); /* ba */ | |
2974 | bfd_put_32 (input_bfd, x, contents + rel->r_offset); | |
2975 | r = bfd_reloc_ok; | |
2976 | if (rel->r_offset >= 4 | |
2977 | && (y & (0xffffffff ^ RS1(~0))) | |
2978 | == (INSN_OR | RD(O7) | RS2(G0))) | |
2979 | { | |
2980 | bfd_vma z; | |
2981 | unsigned int reg; | |
2982 | ||
2983 | z = bfd_get_32 (input_bfd, | |
2984 | contents + rel->r_offset - 4); | |
2985 | if ((z & (0xffffffff ^ RD(~0))) | |
2986 | != (INSN_OR | RS1(O7) | RS2(G0))) | |
2987 | break; | |
2988 | ||
2989 | /* The sequence was | |
2990 | or %o7, %g0, %rN | |
2991 | call foo | |
2992 | or %rN, %g0, %o7 | |
2993 | ||
2994 | If call foo was replaced with ba, replace | |
2995 | or %rN, %g0, %o7 with nop. */ | |
2996 | ||
2997 | reg = (y & RS1(~0)) >> 14; | |
2998 | if (reg != ((z & RD(~0)) >> 25) | |
2999 | || reg == G0 || reg == O7) | |
3000 | break; | |
3001 | ||
dc810e39 | 3002 | bfd_put_32 (input_bfd, (bfd_vma) INSN_NOP, |
f7775d95 JJ |
3003 | contents + rel->r_offset + 4); |
3004 | } | |
3005 | ||
3006 | } | |
3007 | } | |
3008 | } | |
3009 | } | |
3010 | ||
3011 | if (r == bfd_reloc_continue) | |
252b5132 RH |
3012 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, |
3013 | contents, rel->r_offset, | |
3014 | relocation, rel->r_addend); | |
3015 | ||
252b5132 RH |
3016 | if (r != bfd_reloc_ok) |
3017 | { | |
3018 | switch (r) | |
3019 | { | |
3020 | default: | |
3021 | case bfd_reloc_outofrange: | |
3022 | abort (); | |
3023 | case bfd_reloc_overflow: | |
3024 | { | |
3025 | const char *name; | |
3026 | ||
3027 | if (h != NULL) | |
dfeffb9f | 3028 | name = NULL; |
252b5132 RH |
3029 | else |
3030 | { | |
3031 | name = bfd_elf_string_from_elf_section (input_bfd, | |
3032 | symtab_hdr->sh_link, | |
3033 | sym->st_name); | |
3034 | if (name == NULL) | |
b34976b6 | 3035 | return FALSE; |
252b5132 RH |
3036 | if (*name == '\0') |
3037 | name = bfd_section_name (input_bfd, sec); | |
3038 | } | |
3039 | if (! ((*info->callbacks->reloc_overflow) | |
dfeffb9f L |
3040 | (info, (h ? &h->root : NULL), name, howto->name, |
3041 | (bfd_vma) 0, input_bfd, input_section, | |
3042 | rel->r_offset))) | |
b34976b6 | 3043 | return FALSE; |
252b5132 RH |
3044 | } |
3045 | break; | |
3046 | } | |
3047 | } | |
3048 | } | |
3049 | ||
b34976b6 | 3050 | return TRUE; |
252b5132 RH |
3051 | } |
3052 | ||
3053 | /* Finish up dynamic symbol handling. We set the contents of various | |
3054 | dynamic sections here. */ | |
3055 | ||
b34976b6 | 3056 | static bfd_boolean |
252b5132 RH |
3057 | elf32_sparc_finish_dynamic_symbol (output_bfd, info, h, sym) |
3058 | bfd *output_bfd; | |
3059 | struct bfd_link_info *info; | |
3060 | struct elf_link_hash_entry *h; | |
3061 | Elf_Internal_Sym *sym; | |
3062 | { | |
3063 | bfd *dynobj; | |
b9734f35 | 3064 | struct elf32_sparc_link_hash_table *htab; |
26e41594 | 3065 | |
b9734f35 JJ |
3066 | htab = elf32_sparc_hash_table (info); |
3067 | dynobj = htab->elf.dynobj; | |
252b5132 RH |
3068 | |
3069 | if (h->plt.offset != (bfd_vma) -1) | |
3070 | { | |
3071 | asection *splt; | |
3072 | asection *srela; | |
3073 | Elf_Internal_Rela rela; | |
b34976b6 | 3074 | bfd_byte *loc; |
252b5132 RH |
3075 | |
3076 | /* This symbol has an entry in the procedure linkage table. Set | |
26e41594 | 3077 | it up. */ |
252b5132 RH |
3078 | |
3079 | BFD_ASSERT (h->dynindx != -1); | |
3080 | ||
b9734f35 JJ |
3081 | splt = htab->splt; |
3082 | srela = htab->srelplt; | |
252b5132 RH |
3083 | BFD_ASSERT (splt != NULL && srela != NULL); |
3084 | ||
3085 | /* Fill in the entry in the procedure linkage table. */ | |
3086 | bfd_put_32 (output_bfd, | |
3087 | PLT_ENTRY_WORD0 + h->plt.offset, | |
3088 | splt->contents + h->plt.offset); | |
3089 | bfd_put_32 (output_bfd, | |
3090 | (PLT_ENTRY_WORD1 | |
3091 | + (((- (h->plt.offset + 4)) >> 2) & 0x3fffff)), | |
3092 | splt->contents + h->plt.offset + 4); | |
dc810e39 | 3093 | bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD2, |
252b5132 RH |
3094 | splt->contents + h->plt.offset + 8); |
3095 | ||
3096 | /* Fill in the entry in the .rela.plt section. */ | |
3097 | rela.r_offset = (splt->output_section->vma | |
3098 | + splt->output_offset | |
3099 | + h->plt.offset); | |
3100 | rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_JMP_SLOT); | |
3101 | rela.r_addend = 0; | |
b34976b6 AM |
3102 | loc = srela->contents; |
3103 | loc += (h->plt.offset / PLT_ENTRY_SIZE - 4) * sizeof (Elf32_External_Rela); | |
3104 | bfd_elf32_swap_reloca_out (output_bfd, &rela, loc); | |
252b5132 | 3105 | |
f5385ebf | 3106 | if (!h->def_regular) |
252b5132 RH |
3107 | { |
3108 | /* Mark the symbol as undefined, rather than as defined in | |
3109 | the .plt section. Leave the value alone. */ | |
3110 | sym->st_shndx = SHN_UNDEF; | |
8701c1bc JJ |
3111 | /* If the symbol is weak, we do need to clear the value. |
3112 | Otherwise, the PLT entry would provide a definition for | |
3113 | the symbol even if the symbol wasn't defined anywhere, | |
3114 | and so the symbol would never be NULL. */ | |
f5385ebf | 3115 | if (!h->ref_regular_nonweak) |
8701c1bc | 3116 | sym->st_value = 0; |
252b5132 RH |
3117 | } |
3118 | } | |
3119 | ||
b9734f35 JJ |
3120 | if (h->got.offset != (bfd_vma) -1 |
3121 | && elf32_sparc_hash_entry(h)->tls_type != GOT_TLS_GD | |
3122 | && elf32_sparc_hash_entry(h)->tls_type != GOT_TLS_IE) | |
252b5132 RH |
3123 | { |
3124 | asection *sgot; | |
3125 | asection *srela; | |
3126 | Elf_Internal_Rela rela; | |
b34976b6 | 3127 | bfd_byte *loc; |
252b5132 RH |
3128 | |
3129 | /* This symbol has an entry in the global offset table. Set it | |
26e41594 | 3130 | up. */ |
252b5132 | 3131 | |
b9734f35 JJ |
3132 | sgot = htab->sgot; |
3133 | srela = htab->srelgot; | |
252b5132 RH |
3134 | BFD_ASSERT (sgot != NULL && srela != NULL); |
3135 | ||
3136 | rela.r_offset = (sgot->output_section->vma | |
3137 | + sgot->output_offset | |
dc810e39 | 3138 | + (h->got.offset &~ (bfd_vma) 1)); |
252b5132 RH |
3139 | |
3140 | /* If this is a -Bsymbolic link, and the symbol is defined | |
3141 | locally, we just want to emit a RELATIVE reloc. Likewise if | |
3142 | the symbol was forced to be local because of a version file. | |
3143 | The entry in the global offset table will already have been | |
3144 | initialized in the relocate_section function. */ | |
3145 | if (info->shared | |
3146 | && (info->symbolic || h->dynindx == -1) | |
f5385ebf | 3147 | && h->def_regular) |
6b3ac709 JJ |
3148 | { |
3149 | asection *sec = h->root.u.def.section; | |
3150 | rela.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE); | |
3151 | rela.r_addend = (h->root.u.def.value | |
3152 | + sec->output_section->vma | |
3153 | + sec->output_offset); | |
3154 | } | |
252b5132 RH |
3155 | else |
3156 | { | |
252b5132 | 3157 | rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_GLOB_DAT); |
6b3ac709 | 3158 | rela.r_addend = 0; |
252b5132 RH |
3159 | } |
3160 | ||
6b3ac709 JJ |
3161 | bfd_put_32 (output_bfd, (bfd_vma) 0, |
3162 | sgot->contents + (h->got.offset &~ (bfd_vma) 1)); | |
b34976b6 AM |
3163 | loc = srela->contents; |
3164 | loc += srela->reloc_count++ * sizeof (Elf32_External_Rela); | |
3165 | bfd_elf32_swap_reloca_out (output_bfd, &rela, loc); | |
252b5132 RH |
3166 | } |
3167 | ||
f5385ebf | 3168 | if (h->needs_copy) |
252b5132 RH |
3169 | { |
3170 | asection *s; | |
3171 | Elf_Internal_Rela rela; | |
b34976b6 | 3172 | bfd_byte *loc; |
252b5132 RH |
3173 | |
3174 | /* This symbols needs a copy reloc. Set it up. */ | |
3175 | ||
3176 | BFD_ASSERT (h->dynindx != -1); | |
3177 | ||
3178 | s = bfd_get_section_by_name (h->root.u.def.section->owner, | |
3179 | ".rela.bss"); | |
3180 | BFD_ASSERT (s != NULL); | |
3181 | ||
3182 | rela.r_offset = (h->root.u.def.value | |
3183 | + h->root.u.def.section->output_section->vma | |
3184 | + h->root.u.def.section->output_offset); | |
3185 | rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_COPY); | |
3186 | rela.r_addend = 0; | |
b34976b6 AM |
3187 | loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela); |
3188 | bfd_elf32_swap_reloca_out (output_bfd, &rela, loc); | |
252b5132 RH |
3189 | } |
3190 | ||
3191 | /* Mark some specially defined symbols as absolute. */ | |
3192 | if (strcmp (h->root.root.string, "_DYNAMIC") == 0 | |
3193 | || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0 | |
3194 | || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0) | |
3195 | sym->st_shndx = SHN_ABS; | |
3196 | ||
b34976b6 | 3197 | return TRUE; |
252b5132 RH |
3198 | } |
3199 | ||
3200 | /* Finish up the dynamic sections. */ | |
3201 | ||
b34976b6 | 3202 | static bfd_boolean |
252b5132 RH |
3203 | elf32_sparc_finish_dynamic_sections (output_bfd, info) |
3204 | bfd *output_bfd; | |
3205 | struct bfd_link_info *info; | |
3206 | { | |
3207 | bfd *dynobj; | |
3208 | asection *sdyn; | |
b9734f35 | 3209 | struct elf32_sparc_link_hash_table *htab; |
26e41594 | 3210 | |
b9734f35 JJ |
3211 | htab = elf32_sparc_hash_table (info); |
3212 | dynobj = htab->elf.dynobj; | |
252b5132 RH |
3213 | |
3214 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); | |
3215 | ||
3216 | if (elf_hash_table (info)->dynamic_sections_created) | |
3217 | { | |
3218 | asection *splt; | |
3219 | Elf32_External_Dyn *dyncon, *dynconend; | |
3220 | ||
3221 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
3222 | BFD_ASSERT (splt != NULL && sdyn != NULL); | |
3223 | ||
3224 | dyncon = (Elf32_External_Dyn *) sdyn->contents; | |
eea6121a | 3225 | dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size); |
252b5132 RH |
3226 | for (; dyncon < dynconend; dyncon++) |
3227 | { | |
3228 | Elf_Internal_Dyn dyn; | |
3229 | const char *name; | |
b34976b6 | 3230 | bfd_boolean size; |
252b5132 RH |
3231 | |
3232 | bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn); | |
3233 | ||
3234 | switch (dyn.d_tag) | |
3235 | { | |
b34976b6 AM |
3236 | case DT_PLTGOT: name = ".plt"; size = FALSE; break; |
3237 | case DT_PLTRELSZ: name = ".rela.plt"; size = TRUE; break; | |
3238 | case DT_JMPREL: name = ".rela.plt"; size = FALSE; break; | |
3239 | default: name = NULL; size = FALSE; break; | |
252b5132 RH |
3240 | } |
3241 | ||
3242 | if (name != NULL) | |
3243 | { | |
3244 | asection *s; | |
3245 | ||
3246 | s = bfd_get_section_by_name (output_bfd, name); | |
3247 | if (s == NULL) | |
3248 | dyn.d_un.d_val = 0; | |
3249 | else | |
3250 | { | |
3251 | if (! size) | |
3252 | dyn.d_un.d_ptr = s->vma; | |
3253 | else | |
eea6121a | 3254 | dyn.d_un.d_val = s->size; |
252b5132 RH |
3255 | } |
3256 | bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); | |
3257 | } | |
3258 | } | |
3259 | ||
3260 | /* Clear the first four entries in the procedure linkage table, | |
3261 | and put a nop in the last four bytes. */ | |
eea6121a | 3262 | if (splt->size > 0) |
252b5132 RH |
3263 | { |
3264 | memset (splt->contents, 0, 4 * PLT_ENTRY_SIZE); | |
dc810e39 | 3265 | bfd_put_32 (output_bfd, (bfd_vma) SPARC_NOP, |
eea6121a | 3266 | splt->contents + splt->size - 4); |
252b5132 RH |
3267 | } |
3268 | ||
a8a6d7ce | 3269 | elf_section_data (splt->output_section)->this_hdr.sh_entsize = 0; |
252b5132 RH |
3270 | } |
3271 | ||
3272 | /* Set the first entry in the global offset table to the address of | |
3273 | the dynamic section. */ | |
eea6121a | 3274 | if (htab->sgot && htab->sgot->size > 0) |
252b5132 RH |
3275 | { |
3276 | if (sdyn == NULL) | |
b9734f35 | 3277 | bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgot->contents); |
252b5132 RH |
3278 | else |
3279 | bfd_put_32 (output_bfd, | |
3280 | sdyn->output_section->vma + sdyn->output_offset, | |
b9734f35 | 3281 | htab->sgot->contents); |
252b5132 RH |
3282 | } |
3283 | ||
b9734f35 JJ |
3284 | if (htab->sgot) |
3285 | elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 4; | |
252b5132 | 3286 | |
b34976b6 | 3287 | return TRUE; |
252b5132 RH |
3288 | } |
3289 | \f | |
3290 | /* Functions for dealing with the e_flags field. | |
3291 | ||
3292 | We don't define set_private_flags or copy_private_bfd_data because | |
3293 | the only currently defined values are based on the bfd mach number, | |
3294 | so we use the latter instead and defer setting e_flags until the | |
3295 | file is written out. */ | |
3296 | ||
3297 | /* Merge backend specific data from an object file to the output | |
3298 | object file when linking. */ | |
3299 | ||
b34976b6 | 3300 | static bfd_boolean |
252b5132 RH |
3301 | elf32_sparc_merge_private_bfd_data (ibfd, obfd) |
3302 | bfd *ibfd; | |
3303 | bfd *obfd; | |
3304 | { | |
b34976b6 | 3305 | bfd_boolean error; |
7946e94a | 3306 | unsigned long ibfd_mach; |
5f771d47 ILT |
3307 | /* FIXME: This should not be static. */ |
3308 | static unsigned long previous_ibfd_e_flags = (unsigned long) -1; | |
252b5132 RH |
3309 | |
3310 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
3311 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 3312 | return TRUE; |
252b5132 | 3313 | |
b34976b6 | 3314 | error = FALSE; |
252b5132 | 3315 | |
7946e94a JJ |
3316 | ibfd_mach = bfd_get_mach (ibfd); |
3317 | if (bfd_mach_sparc_64bit_p (ibfd_mach)) | |
252b5132 | 3318 | { |
b34976b6 | 3319 | error = TRUE; |
252b5132 | 3320 | (*_bfd_error_handler) |
d003868e | 3321 | (_("%B: compiled for a 64 bit system and target is 32 bit"), ibfd); |
252b5132 | 3322 | } |
37fb6db1 ILT |
3323 | else if ((ibfd->flags & DYNAMIC) == 0) |
3324 | { | |
7946e94a JJ |
3325 | if (bfd_get_mach (obfd) < ibfd_mach) |
3326 | bfd_set_arch_mach (obfd, bfd_arch_sparc, ibfd_mach); | |
37fb6db1 | 3327 | } |
252b5132 RH |
3328 | |
3329 | if (((elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA) | |
3330 | != previous_ibfd_e_flags) | |
5f771d47 | 3331 | && previous_ibfd_e_flags != (unsigned long) -1) |
252b5132 RH |
3332 | { |
3333 | (*_bfd_error_handler) | |
d003868e | 3334 | (_("%B: linking little endian files with big endian files"), ibfd); |
b34976b6 | 3335 | error = TRUE; |
252b5132 RH |
3336 | } |
3337 | previous_ibfd_e_flags = elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA; | |
3338 | ||
3339 | if (error) | |
3340 | { | |
3341 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 3342 | return FALSE; |
252b5132 RH |
3343 | } |
3344 | ||
b34976b6 | 3345 | return TRUE; |
252b5132 RH |
3346 | } |
3347 | \f | |
3348 | /* Set the right machine number. */ | |
3349 | ||
b34976b6 | 3350 | static bfd_boolean |
252b5132 RH |
3351 | elf32_sparc_object_p (abfd) |
3352 | bfd *abfd; | |
3353 | { | |
3354 | if (elf_elfheader (abfd)->e_machine == EM_SPARC32PLUS) | |
3355 | { | |
19f7b010 JJ |
3356 | if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US3) |
3357 | return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, | |
3358 | bfd_mach_sparc_v8plusb); | |
3359 | else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1) | |
252b5132 RH |
3360 | return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, |
3361 | bfd_mach_sparc_v8plusa); | |
3362 | else if (elf_elfheader (abfd)->e_flags & EF_SPARC_32PLUS) | |
3363 | return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, | |
3364 | bfd_mach_sparc_v8plus); | |
3365 | else | |
b34976b6 | 3366 | return FALSE; |
252b5132 RH |
3367 | } |
3368 | else if (elf_elfheader (abfd)->e_flags & EF_SPARC_LEDATA) | |
3369 | return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, | |
20278fa3 | 3370 | bfd_mach_sparc_sparclite_le); |
252b5132 RH |
3371 | else |
3372 | return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc); | |
3373 | } | |
3374 | ||
3375 | /* The final processing done just before writing out the object file. | |
3376 | We need to set the e_machine field appropriately. */ | |
3377 | ||
3378 | static void | |
3379 | elf32_sparc_final_write_processing (abfd, linker) | |
3380 | bfd *abfd; | |
b34976b6 | 3381 | bfd_boolean linker ATTRIBUTE_UNUSED; |
252b5132 RH |
3382 | { |
3383 | switch (bfd_get_mach (abfd)) | |
3384 | { | |
3385 | case bfd_mach_sparc : | |
fca870e4 AM |
3386 | case bfd_mach_sparc_sparclet : |
3387 | case bfd_mach_sparc_sparclite : | |
252b5132 RH |
3388 | break; /* nothing to do */ |
3389 | case bfd_mach_sparc_v8plus : | |
3390 | elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS; | |
3391 | elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK; | |
3392 | elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS; | |
3393 | break; | |
3394 | case bfd_mach_sparc_v8plusa : | |
3395 | elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS; | |
3396 | elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK; | |
3397 | elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1; | |
3398 | break; | |
19f7b010 JJ |
3399 | case bfd_mach_sparc_v8plusb : |
3400 | elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS; | |
3401 | elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK; | |
3402 | elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1 | |
3403 | | EF_SPARC_SUN_US3; | |
3404 | break; | |
252b5132 | 3405 | case bfd_mach_sparc_sparclite_le : |
252b5132 RH |
3406 | elf_elfheader (abfd)->e_flags |= EF_SPARC_LEDATA; |
3407 | break; | |
3408 | default : | |
3409 | abort (); | |
3410 | break; | |
3411 | } | |
3412 | } | |
db6751f2 JJ |
3413 | |
3414 | static enum elf_reloc_type_class | |
f51e552e AM |
3415 | elf32_sparc_reloc_type_class (rela) |
3416 | const Elf_Internal_Rela *rela; | |
db6751f2 | 3417 | { |
f51e552e | 3418 | switch ((int) ELF32_R_TYPE (rela->r_info)) |
db6751f2 JJ |
3419 | { |
3420 | case R_SPARC_RELATIVE: | |
3421 | return reloc_class_relative; | |
3422 | case R_SPARC_JMP_SLOT: | |
3423 | return reloc_class_plt; | |
3424 | case R_SPARC_COPY: | |
3425 | return reloc_class_copy; | |
3426 | default: | |
3427 | return reloc_class_normal; | |
3428 | } | |
3429 | } | |
4c45e5c9 JJ |
3430 | |
3431 | /* Return address for Ith PLT stub in section PLT, for relocation REL | |
3432 | or (bfd_vma) -1 if it should not be included. */ | |
3433 | ||
3434 | static bfd_vma | |
3435 | elf32_sparc_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED, | |
3436 | const asection *plt ATTRIBUTE_UNUSED, | |
3437 | const arelent *rel) | |
3438 | { | |
3439 | return rel->address; | |
3440 | } | |
252b5132 RH |
3441 | \f |
3442 | #define TARGET_BIG_SYM bfd_elf32_sparc_vec | |
3443 | #define TARGET_BIG_NAME "elf32-sparc" | |
3444 | #define ELF_ARCH bfd_arch_sparc | |
3445 | #define ELF_MACHINE_CODE EM_SPARC | |
3446 | #define ELF_MACHINE_ALT1 EM_SPARC32PLUS | |
3447 | #define ELF_MAXPAGESIZE 0x10000 | |
3448 | ||
3449 | #define bfd_elf32_bfd_reloc_type_lookup elf32_sparc_reloc_type_lookup | |
b9734f35 JJ |
3450 | #define bfd_elf32_bfd_link_hash_table_create \ |
3451 | elf32_sparc_link_hash_table_create | |
f7775d95 | 3452 | #define bfd_elf32_bfd_relax_section elf32_sparc_relax_section |
71e5db1c | 3453 | #define bfd_elf32_new_section_hook elf32_sparc_new_section_hook |
252b5132 | 3454 | #define elf_info_to_howto elf32_sparc_info_to_howto |
b9734f35 JJ |
3455 | #define elf_backend_copy_indirect_symbol \ |
3456 | elf32_sparc_copy_indirect_symbol | |
252b5132 | 3457 | #define elf_backend_create_dynamic_sections \ |
b9734f35 | 3458 | elf32_sparc_create_dynamic_sections |
252b5132 RH |
3459 | #define elf_backend_check_relocs elf32_sparc_check_relocs |
3460 | #define elf_backend_adjust_dynamic_symbol \ | |
3461 | elf32_sparc_adjust_dynamic_symbol | |
151e5294 | 3462 | #define elf_backend_omit_section_dynsym elf32_sparc_omit_section_dynsym |
252b5132 RH |
3463 | #define elf_backend_size_dynamic_sections \ |
3464 | elf32_sparc_size_dynamic_sections | |
3465 | #define elf_backend_relocate_section elf32_sparc_relocate_section | |
3466 | #define elf_backend_finish_dynamic_symbol \ | |
3467 | elf32_sparc_finish_dynamic_symbol | |
3468 | #define elf_backend_finish_dynamic_sections \ | |
3469 | elf32_sparc_finish_dynamic_sections | |
3470 | #define bfd_elf32_bfd_merge_private_bfd_data \ | |
3471 | elf32_sparc_merge_private_bfd_data | |
b9734f35 | 3472 | #define bfd_elf32_mkobject elf32_sparc_mkobject |
252b5132 RH |
3473 | #define elf_backend_object_p elf32_sparc_object_p |
3474 | #define elf_backend_final_write_processing \ | |
3475 | elf32_sparc_final_write_processing | |
26e41594 | 3476 | #define elf_backend_gc_mark_hook elf32_sparc_gc_mark_hook |
252b5132 | 3477 | #define elf_backend_gc_sweep_hook elf32_sparc_gc_sweep_hook |
0647ed71 | 3478 | #define elf_backend_grok_psinfo elf32_sparc_grok_psinfo |
db6751f2 | 3479 | #define elf_backend_reloc_type_class elf32_sparc_reloc_type_class |
4c45e5c9 | 3480 | #define elf_backend_plt_sym_val elf32_sparc_plt_sym_val |
252b5132 RH |
3481 | |
3482 | #define elf_backend_can_gc_sections 1 | |
b9734f35 | 3483 | #define elf_backend_can_refcount 1 |
252b5132 RH |
3484 | #define elf_backend_want_got_plt 0 |
3485 | #define elf_backend_plt_readonly 0 | |
3486 | #define elf_backend_want_plt_sym 1 | |
3487 | #define elf_backend_got_header_size 4 | |
f0fe0e16 | 3488 | #define elf_backend_rela_normal 1 |
252b5132 RH |
3489 | |
3490 | #include "elf32-target.h" |