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