[ARC] BFD fixes.
[deliverable/binutils-gdb.git] / gold / copy-relocs.cc
CommitLineData
12c0daef
ILT
1// copy-relocs.cc -- handle COPY relocations for gold.
2
6f2750fe 3// Copyright (C) 2006-2016 Free Software Foundation, Inc.
12c0daef
ILT
4// Written by Ian Lance Taylor <iant@google.com>.
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
28namespace 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
36template<int sh_type, int size, bool big_endian>
37void
38Copy_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,
859d7987
CC
45 unsigned int r_type,
46 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
47 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
12c0daef
ILT
48 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
49{
50 if (this->need_copy_reloc(sym, object, shndx))
26d3c67d 51 this->make_copy_reloc(symtab, layout, sym, reloc_section);
12c0daef
ILT
52 else
53 {
54 // We may not need a COPY relocation. Save this relocation to
55 // possibly be emitted later.
859d7987
CC
56 this->save(sym, object, shndx, output_section,
57 r_type, r_offset, r_addend);
12c0daef
ILT
58 }
59}
60
61// Return whether we need a COPY reloc for a relocation against SYM.
62// The relocation is begin applied to section SHNDX in OBJECT.
63
64template<int sh_type, int size, bool big_endian>
65bool
66Copy_relocs<sh_type, size, big_endian>::need_copy_reloc(
67 Sized_symbol<size>* sym,
6fa2a40b 68 Sized_relobj_file<size, big_endian>* object,
12c0daef
ILT
69 unsigned int shndx) const
70{
966d4097
DK
71 if (!parameters->options().copyreloc())
72 return false;
12c0daef
ILT
73
74 if (sym->symsize() == 0)
75 return false;
76
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)
82 return true;
83
84 return false;
85}
86
87// Emit a COPY relocation for SYM.
88
89template<int sh_type, int size, bool big_endian>
90void
91Copy_relocs<sh_type, size, big_endian>::emit_copy_reloc(
26d3c67d
CC
92 Symbol_table* symtab,
93 Sized_symbol<size>* sym,
94 Output_data* posd,
95 off_t offset,
96 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
97{
98 // Define the symbol as being copied.
99 symtab->define_with_copy_reloc(sym, posd, offset);
100
101 // Add the COPY relocation to the dynamic reloc section.
83896202
ILT
102 reloc_section->add_global_generic(sym, this->copy_reloc_type_, posd,
103 offset, 0);
26d3c67d
CC
104}
105
106// Make a COPY relocation for SYM and emit it.
107
108template<int sh_type, int size, bool big_endian>
109void
110Copy_relocs<sh_type, size, big_endian>::make_copy_reloc(
12c0daef
ILT
111 Symbol_table* symtab,
112 Layout* layout,
113 Sized_symbol<size>* sym,
114 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
115{
966d4097
DK
116 // We should not be here if -z nocopyreloc is given.
117 gold_assert(parameters->options().copyreloc());
118
12c0daef
ILT
119 typename elfcpp::Elf_types<size>::Elf_WXword symsize = sym->symsize();
120
121 // There is no defined way to determine the required alignment of
122 // the symbol. We know that the symbol is defined in a dynamic
123 // object. We start with the alignment of the section in which it
124 // is defined; presumably we do not require an alignment larger than
125 // that. Then we reduce that alignment if the symbol is not aligned
126 // within the section.
127 gold_assert(sym->is_from_dynobj());
d491d34e
ILT
128 bool is_ordinary;
129 unsigned int shndx = sym->shndx(&is_ordinary);
130 gold_assert(is_ordinary);
5f9bcf58
CC
131 typename elfcpp::Elf_types<size>::Elf_WXword addralign;
132
133 {
134 // Lock the object so we can read from it. This is only called
135 // single-threaded from scan_relocs, so it is OK to lock.
136 // Unfortunately we have no way to pass in a Task token.
137 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
138 Object* obj = sym->object();
139 Task_lock_obj<Object> tl(dummy_task, obj);
140 addralign = obj->section_addralign(shndx);
141 }
12c0daef
ILT
142
143 typename Sized_symbol<size>::Value_type value = sym->value();
144 while ((value & (addralign - 1)) != 0)
145 addralign >>= 1;
146
659948a4
ILT
147 // Mark the dynamic object as needed for the --as-needed option.
148 sym->object()->set_is_needed();
149
12c0daef
ILT
150 if (this->dynbss_ == NULL)
151 {
7d9e3d98 152 this->dynbss_ = new Output_data_space(addralign, "** dynbss");
12c0daef
ILT
153 layout->add_output_section_data(".bss",
154 elfcpp::SHT_NOBITS,
155 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
22f0da72 156 this->dynbss_, ORDER_BSS, false);
12c0daef
ILT
157 }
158
159 Output_data_space* dynbss = this->dynbss_;
160
161 if (addralign > dynbss->addralign())
162 dynbss->set_space_alignment(addralign);
163
164 section_size_type dynbss_size =
165 convert_to_section_size_type(dynbss->current_data_size());
166 dynbss_size = align_address(dynbss_size, addralign);
167 section_size_type offset = dynbss_size;
168 dynbss->set_current_data_size(dynbss_size + symsize);
169
26d3c67d 170 this->emit_copy_reloc(symtab, sym, dynbss, offset, reloc_section);
12c0daef
ILT
171}
172
173// Save a relocation to possibly be emitted later.
174
175template<int sh_type, int size, bool big_endian>
176void
ef9beddf
ILT
177Copy_relocs<sh_type, size, big_endian>::save(
178 Symbol* sym,
6fa2a40b 179 Sized_relobj_file<size, big_endian>* object,
ef9beddf
ILT
180 unsigned int shndx,
181 Output_section* output_section,
859d7987
CC
182 unsigned int r_type,
183 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
184 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend)
12c0daef 185{
859d7987
CC
186 this->entries_.push_back(Copy_reloc_entry(sym, r_type, object, shndx,
187 output_section, r_offset,
188 r_addend));
12c0daef
ILT
189}
190
191// Emit any saved relocs.
192
193template<int sh_type, int size, bool big_endian>
194void
195Copy_relocs<sh_type, size, big_endian>::emit(
196 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
197{
198 for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
199 p != this->entries_.end();
200 ++p)
91f43acd
CC
201 {
202 Copy_reloc_entry& entry = *p;
203
204 // If the symbol is no longer defined in a dynamic object, then we
205 // emitted a COPY relocation, and we do not want to emit this
206 // dynamic relocation.
207 if (entry.sym_->is_from_dynobj())
208 reloc_section->add_global_generic(entry.sym_, entry.reloc_type_,
209 entry.output_section_, entry.relobj_,
210 entry.shndx_, entry.address_,
211 entry.addend_);
212 }
12c0daef
ILT
213
214 // We no longer need the saved information.
215 this->entries_.clear();
216}
217
218// Instantiate the templates we need.
219
220#ifdef HAVE_TARGET_32_LITTLE
221template
222class Copy_relocs<elfcpp::SHT_REL, 32, false>;
223
224template
225class Copy_relocs<elfcpp::SHT_RELA, 32, false>;
226#endif
227
228#ifdef HAVE_TARGET_32_BIG
229template
230class Copy_relocs<elfcpp::SHT_REL, 32, true>;
231
232template
233class Copy_relocs<elfcpp::SHT_RELA, 32, true>;
234#endif
235
236#ifdef HAVE_TARGET_64_LITTLE
237template
238class Copy_relocs<elfcpp::SHT_REL, 64, false>;
239
240template
241class Copy_relocs<elfcpp::SHT_RELA, 64, false>;
242#endif
243
244#ifdef HAVE_TARGET_64_BIG
245template
246class Copy_relocs<elfcpp::SHT_REL, 64, true>;
247
248template
249class Copy_relocs<elfcpp::SHT_RELA, 64, true>;
250#endif
251
252} // End namespace gold.
This page took 0.449801 seconds and 4 git commands to generate.