1 // copy-relocs.cc -- handle COPY relocations for gold.
3 // Copyright (C) 2006-2016 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
26 #include "copy-relocs.h"
31 // Copy_relocs methods.
33 // Handle a relocation against a symbol which may force us to generate
36 template<int sh_type, int size, bool big_endian>
38 Copy_relocs<sh_type, size, big_endian>::copy_reloc(
41 Sized_symbol<size>* sym,
42 Sized_relobj_file<size, big_endian>* object,
44 Output_section* output_section,
46 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
47 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
48 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
50 if (this->need_copy_reloc(sym, object, shndx))
51 this->make_copy_reloc(symtab, layout, sym, object, reloc_section);
54 // We may not need a COPY relocation. Save this relocation to
55 // possibly be emitted later.
56 this->save(sym, object, shndx, output_section,
57 r_type, r_offset, r_addend);
61 // Return whether we need a COPY reloc for a relocation against SYM.
62 // The relocation is begin applied to section SHNDX in OBJECT.
64 template<int sh_type, int size, bool big_endian>
66 Copy_relocs<sh_type, size, big_endian>::need_copy_reloc(
67 Sized_symbol<size>* sym,
68 Sized_relobj_file<size, big_endian>* object,
69 unsigned int shndx) const
71 if (!parameters->options().copyreloc())
74 if (sym->symsize() == 0)
77 // If this is a readonly section, then we need a COPY reloc.
78 // Otherwise we can use a dynamic reloc. Note that calling
79 // section_flags here can be slow, as the information is not cached;
80 // fortunately we shouldn't see too many potential COPY relocs.
81 if ((object->section_flags(shndx) & elfcpp::SHF_WRITE) == 0)
87 // Emit a COPY relocation for SYM.
89 template<int sh_type, int size, bool big_endian>
91 Copy_relocs<sh_type, size, big_endian>::emit_copy_reloc(
93 Sized_symbol<size>* sym,
96 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
98 // Define the symbol as being copied.
99 symtab->define_with_copy_reloc(sym, posd, offset);
101 // Add the COPY relocation to the dynamic reloc section.
102 reloc_section->add_global_generic(sym, this->copy_reloc_type_, posd,
106 // Make a COPY relocation for SYM and emit it.
108 template<int sh_type, int size, bool big_endian>
110 Copy_relocs<sh_type, size, big_endian>::make_copy_reloc(
111 Symbol_table* symtab,
113 Sized_symbol<size>* sym,
114 Sized_relobj_file<size, big_endian>* object,
115 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
117 // We should not be here if -z nocopyreloc is given.
118 gold_assert(parameters->options().copyreloc());
120 gold_assert(sym->is_from_dynobj());
122 // The symbol must not have protected visibility.
123 if (sym->is_protected())
125 gold_error(_("%s: cannot make copy relocation for "
126 "protected symbol '%s', defined in %s"),
127 object->name().c_str(),
129 sym->object()->name().c_str());
132 typename elfcpp::Elf_types<size>::Elf_WXword symsize = sym->symsize();
134 // There is no defined way to determine the required alignment of
135 // the symbol. We know that the symbol is defined in a dynamic
136 // object. We start with the alignment of the section in which it
137 // is defined; presumably we do not require an alignment larger than
138 // that. Then we reduce that alignment if the symbol is not aligned
139 // within the section.
141 unsigned int shndx = sym->shndx(&is_ordinary);
142 gold_assert(is_ordinary);
143 typename elfcpp::Elf_types<size>::Elf_WXword addralign;
146 // Lock the object so we can read from it. This is only called
147 // single-threaded from scan_relocs, so it is OK to lock.
148 // Unfortunately we have no way to pass in a Task token.
149 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
150 Object* obj = sym->object();
151 Task_lock_obj<Object> tl(dummy_task, obj);
152 addralign = obj->section_addralign(shndx);
155 typename Sized_symbol<size>::Value_type value = sym->value();
156 while ((value & (addralign - 1)) != 0)
159 // Mark the dynamic object as needed for the --as-needed option.
160 sym->object()->set_is_needed();
162 if (this->dynbss_ == NULL)
164 this->dynbss_ = new Output_data_space(addralign, "** dynbss");
165 layout->add_output_section_data(".bss",
167 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
168 this->dynbss_, ORDER_BSS, false);
171 Output_data_space* dynbss = this->dynbss_;
173 if (addralign > dynbss->addralign())
174 dynbss->set_space_alignment(addralign);
176 section_size_type dynbss_size =
177 convert_to_section_size_type(dynbss->current_data_size());
178 dynbss_size = align_address(dynbss_size, addralign);
179 section_size_type offset = dynbss_size;
180 dynbss->set_current_data_size(dynbss_size + symsize);
182 this->emit_copy_reloc(symtab, sym, dynbss, offset, reloc_section);
185 // Save a relocation to possibly be emitted later.
187 template<int sh_type, int size, bool big_endian>
189 Copy_relocs<sh_type, size, big_endian>::save(
191 Sized_relobj_file<size, big_endian>* object,
193 Output_section* output_section,
195 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
196 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend)
198 this->entries_.push_back(Copy_reloc_entry(sym, r_type, object, shndx,
199 output_section, r_offset,
203 // Emit any saved relocs.
205 template<int sh_type, int size, bool big_endian>
207 Copy_relocs<sh_type, size, big_endian>::emit(
208 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
210 for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
211 p != this->entries_.end();
214 Copy_reloc_entry& entry = *p;
216 // If the symbol is no longer defined in a dynamic object, then we
217 // emitted a COPY relocation, and we do not want to emit this
218 // dynamic relocation.
219 if (entry.sym_->is_from_dynobj())
220 reloc_section->add_global_generic(entry.sym_, entry.reloc_type_,
221 entry.output_section_, entry.relobj_,
222 entry.shndx_, entry.address_,
226 // We no longer need the saved information.
227 this->entries_.clear();
230 // Instantiate the templates we need.
232 #ifdef HAVE_TARGET_32_LITTLE
234 class Copy_relocs<elfcpp::SHT_REL, 32, false>;
237 class Copy_relocs<elfcpp::SHT_RELA, 32, false>;
240 #ifdef HAVE_TARGET_32_BIG
242 class Copy_relocs<elfcpp::SHT_REL, 32, true>;
245 class Copy_relocs<elfcpp::SHT_RELA, 32, true>;
248 #ifdef HAVE_TARGET_64_LITTLE
250 class Copy_relocs<elfcpp::SHT_REL, 64, false>;
253 class Copy_relocs<elfcpp::SHT_RELA, 64, false>;
256 #ifdef HAVE_TARGET_64_BIG
258 class Copy_relocs<elfcpp::SHT_REL, 64, true>;
261 class Copy_relocs<elfcpp::SHT_RELA, 64, true>;
264 } // End namespace gold.