]>
Commit | Line | Data |
---|---|---|
12c0daef ILT |
1 | // copy-relocs.cc -- handle COPY relocations for gold. |
2 | ||
b90efa5b | 3 | // Copyright (C) 2006-2015 Free Software Foundation, Inc. |
12c0daef ILT |
4 | // Written by Ian Lance Taylor <[email protected]>. |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
23 | #include "gold.h" | |
24 | ||
25 | #include "symtab.h" | |
26 | #include "copy-relocs.h" | |
27 | ||
28 | namespace gold | |
29 | { | |
30 | ||
12c0daef ILT |
31 | // Copy_relocs methods. |
32 | ||
33 | // Handle a relocation against a symbol which may force us to generate | |
34 | // a COPY reloc. | |
35 | ||
36 | template<int sh_type, int size, bool big_endian> | |
37 | void | |
38 | Copy_relocs<sh_type, size, big_endian>::copy_reloc( | |
39 | Symbol_table* symtab, | |
40 | Layout* layout, | |
41 | Sized_symbol<size>* sym, | |
6fa2a40b | 42 | Sized_relobj_file<size, big_endian>* object, |
12c0daef | 43 | unsigned int shndx, |
ca09d69a | 44 | Output_section* output_section, |
12c0daef ILT |
45 | const Reloc& rel, |
46 | Output_data_reloc<sh_type, true, size, big_endian>* reloc_section) | |
47 | { | |
48 | if (this->need_copy_reloc(sym, object, shndx)) | |
26d3c67d | 49 | this->make_copy_reloc(symtab, layout, sym, reloc_section); |
12c0daef ILT |
50 | else |
51 | { | |
52 | // We may not need a COPY relocation. Save this relocation to | |
53 | // possibly be emitted later. | |
54 | this->save(sym, object, shndx, output_section, rel); | |
55 | } | |
56 | } | |
57 | ||
58 | // Return whether we need a COPY reloc for a relocation against SYM. | |
59 | // The relocation is begin applied to section SHNDX in OBJECT. | |
60 | ||
61 | template<int sh_type, int size, bool big_endian> | |
62 | bool | |
63 | Copy_relocs<sh_type, size, big_endian>::need_copy_reloc( | |
64 | Sized_symbol<size>* sym, | |
6fa2a40b | 65 | Sized_relobj_file<size, big_endian>* object, |
12c0daef ILT |
66 | unsigned int shndx) const |
67 | { | |
966d4097 DK |
68 | if (!parameters->options().copyreloc()) |
69 | return false; | |
12c0daef ILT |
70 | |
71 | if (sym->symsize() == 0) | |
72 | return false; | |
73 | ||
74 | // If this is a readonly section, then we need a COPY reloc. | |
75 | // Otherwise we can use a dynamic reloc. Note that calling | |
76 | // section_flags here can be slow, as the information is not cached; | |
77 | // fortunately we shouldn't see too many potential COPY relocs. | |
78 | if ((object->section_flags(shndx) & elfcpp::SHF_WRITE) == 0) | |
79 | return true; | |
80 | ||
81 | return false; | |
82 | } | |
83 | ||
84 | // Emit a COPY relocation for SYM. | |
85 | ||
86 | template<int sh_type, int size, bool big_endian> | |
87 | void | |
88 | Copy_relocs<sh_type, size, big_endian>::emit_copy_reloc( | |
26d3c67d CC |
89 | Symbol_table* symtab, |
90 | Sized_symbol<size>* sym, | |
91 | Output_data* posd, | |
92 | off_t offset, | |
93 | Output_data_reloc<sh_type, true, size, big_endian>* reloc_section) | |
94 | { | |
95 | // Define the symbol as being copied. | |
96 | symtab->define_with_copy_reloc(sym, posd, offset); | |
97 | ||
98 | // Add the COPY relocation to the dynamic reloc section. | |
83896202 ILT |
99 | reloc_section->add_global_generic(sym, this->copy_reloc_type_, posd, |
100 | offset, 0); | |
26d3c67d CC |
101 | } |
102 | ||
103 | // Make a COPY relocation for SYM and emit it. | |
104 | ||
105 | template<int sh_type, int size, bool big_endian> | |
106 | void | |
107 | Copy_relocs<sh_type, size, big_endian>::make_copy_reloc( | |
12c0daef ILT |
108 | Symbol_table* symtab, |
109 | Layout* layout, | |
110 | Sized_symbol<size>* sym, | |
111 | Output_data_reloc<sh_type, true, size, big_endian>* reloc_section) | |
112 | { | |
966d4097 DK |
113 | // We should not be here if -z nocopyreloc is given. |
114 | gold_assert(parameters->options().copyreloc()); | |
115 | ||
12c0daef ILT |
116 | typename elfcpp::Elf_types<size>::Elf_WXword symsize = sym->symsize(); |
117 | ||
118 | // There is no defined way to determine the required alignment of | |
119 | // the symbol. We know that the symbol is defined in a dynamic | |
120 | // object. We start with the alignment of the section in which it | |
121 | // is defined; presumably we do not require an alignment larger than | |
122 | // that. Then we reduce that alignment if the symbol is not aligned | |
123 | // within the section. | |
124 | gold_assert(sym->is_from_dynobj()); | |
d491d34e ILT |
125 | bool is_ordinary; |
126 | unsigned int shndx = sym->shndx(&is_ordinary); | |
127 | gold_assert(is_ordinary); | |
5f9bcf58 CC |
128 | typename elfcpp::Elf_types<size>::Elf_WXword addralign; |
129 | ||
130 | { | |
131 | // Lock the object so we can read from it. This is only called | |
132 | // single-threaded from scan_relocs, so it is OK to lock. | |
133 | // Unfortunately we have no way to pass in a Task token. | |
134 | const Task* dummy_task = reinterpret_cast<const Task*>(-1); | |
135 | Object* obj = sym->object(); | |
136 | Task_lock_obj<Object> tl(dummy_task, obj); | |
137 | addralign = obj->section_addralign(shndx); | |
138 | } | |
12c0daef ILT |
139 | |
140 | typename Sized_symbol<size>::Value_type value = sym->value(); | |
141 | while ((value & (addralign - 1)) != 0) | |
142 | addralign >>= 1; | |
143 | ||
659948a4 ILT |
144 | // Mark the dynamic object as needed for the --as-needed option. |
145 | sym->object()->set_is_needed(); | |
146 | ||
12c0daef ILT |
147 | if (this->dynbss_ == NULL) |
148 | { | |
7d9e3d98 | 149 | this->dynbss_ = new Output_data_space(addralign, "** dynbss"); |
12c0daef ILT |
150 | layout->add_output_section_data(".bss", |
151 | elfcpp::SHT_NOBITS, | |
152 | elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE, | |
22f0da72 | 153 | this->dynbss_, ORDER_BSS, false); |
12c0daef ILT |
154 | } |
155 | ||
156 | Output_data_space* dynbss = this->dynbss_; | |
157 | ||
158 | if (addralign > dynbss->addralign()) | |
159 | dynbss->set_space_alignment(addralign); | |
160 | ||
161 | section_size_type dynbss_size = | |
162 | convert_to_section_size_type(dynbss->current_data_size()); | |
163 | dynbss_size = align_address(dynbss_size, addralign); | |
164 | section_size_type offset = dynbss_size; | |
165 | dynbss->set_current_data_size(dynbss_size + symsize); | |
166 | ||
26d3c67d | 167 | this->emit_copy_reloc(symtab, sym, dynbss, offset, reloc_section); |
12c0daef ILT |
168 | } |
169 | ||
170 | // Save a relocation to possibly be emitted later. | |
171 | ||
172 | template<int sh_type, int size, bool big_endian> | |
173 | void | |
ef9beddf ILT |
174 | Copy_relocs<sh_type, size, big_endian>::save( |
175 | Symbol* sym, | |
6fa2a40b | 176 | Sized_relobj_file<size, big_endian>* object, |
ef9beddf ILT |
177 | unsigned int shndx, |
178 | Output_section* output_section, | |
179 | const Reloc& rel) | |
12c0daef ILT |
180 | { |
181 | unsigned int reloc_type = elfcpp::elf_r_type<size>(rel.get_r_info()); | |
182 | typename elfcpp::Elf_types<size>::Elf_Addr addend = | |
183 | Reloc_types<sh_type, size, big_endian>::get_reloc_addend_noerror(&rel); | |
184 | this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, object, shndx, | |
185 | output_section, rel.get_r_offset(), | |
186 | addend)); | |
187 | } | |
188 | ||
189 | // Emit any saved relocs. | |
190 | ||
191 | template<int sh_type, int size, bool big_endian> | |
192 | void | |
193 | Copy_relocs<sh_type, size, big_endian>::emit( | |
194 | Output_data_reloc<sh_type, true, size, big_endian>* reloc_section) | |
195 | { | |
196 | for (typename Copy_reloc_entries::iterator p = this->entries_.begin(); | |
197 | p != this->entries_.end(); | |
198 | ++p) | |
91f43acd CC |
199 | { |
200 | Copy_reloc_entry& entry = *p; | |
201 | ||
202 | // If the symbol is no longer defined in a dynamic object, then we | |
203 | // emitted a COPY relocation, and we do not want to emit this | |
204 | // dynamic relocation. | |
205 | if (entry.sym_->is_from_dynobj()) | |
206 | reloc_section->add_global_generic(entry.sym_, entry.reloc_type_, | |
207 | entry.output_section_, entry.relobj_, | |
208 | entry.shndx_, entry.address_, | |
209 | entry.addend_); | |
210 | } | |
12c0daef ILT |
211 | |
212 | // We no longer need the saved information. | |
213 | this->entries_.clear(); | |
214 | } | |
215 | ||
216 | // Instantiate the templates we need. | |
217 | ||
218 | #ifdef HAVE_TARGET_32_LITTLE | |
219 | template | |
220 | class Copy_relocs<elfcpp::SHT_REL, 32, false>; | |
221 | ||
222 | template | |
223 | class Copy_relocs<elfcpp::SHT_RELA, 32, false>; | |
224 | #endif | |
225 | ||
226 | #ifdef HAVE_TARGET_32_BIG | |
227 | template | |
228 | class Copy_relocs<elfcpp::SHT_REL, 32, true>; | |
229 | ||
230 | template | |
231 | class Copy_relocs<elfcpp::SHT_RELA, 32, true>; | |
232 | #endif | |
233 | ||
234 | #ifdef HAVE_TARGET_64_LITTLE | |
235 | template | |
236 | class Copy_relocs<elfcpp::SHT_REL, 64, false>; | |
237 | ||
238 | template | |
239 | class Copy_relocs<elfcpp::SHT_RELA, 64, false>; | |
240 | #endif | |
241 | ||
242 | #ifdef HAVE_TARGET_64_BIG | |
243 | template | |
244 | class Copy_relocs<elfcpp::SHT_REL, 64, true>; | |
245 | ||
246 | template | |
247 | class Copy_relocs<elfcpp::SHT_RELA, 64, true>; | |
248 | #endif | |
249 | ||
250 | } // End namespace gold. |