Commit | Line | Data |
---|---|---|
a2fb1b05 ILT |
1 | // layout.cc -- lay out output file sections for gold |
2 | ||
6cb15b7f ILT |
3 | // Copyright 2006, 2007 Free Software Foundation, Inc. |
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 | ||
a2fb1b05 ILT |
23 | #include "gold.h" |
24 | ||
a2fb1b05 | 25 | #include <cstring> |
54dc6425 | 26 | #include <algorithm> |
a2fb1b05 ILT |
27 | #include <iostream> |
28 | #include <utility> | |
29 | ||
7e1edb90 | 30 | #include "parameters.h" |
a2fb1b05 | 31 | #include "output.h" |
f6ce93d6 | 32 | #include "symtab.h" |
a3ad94ed | 33 | #include "dynobj.h" |
3151305a | 34 | #include "ehframe.h" |
a2fb1b05 ILT |
35 | #include "layout.h" |
36 | ||
37 | namespace gold | |
38 | { | |
39 | ||
92e059d8 | 40 | // Layout_task_runner methods. |
a2fb1b05 ILT |
41 | |
42 | // Lay out the sections. This is called after all the input objects | |
43 | // have been read. | |
44 | ||
45 | void | |
92e059d8 | 46 | Layout_task_runner::run(Workqueue* workqueue) |
a2fb1b05 | 47 | { |
12e14209 ILT |
48 | off_t file_size = this->layout_->finalize(this->input_objects_, |
49 | this->symtab_); | |
61ba1cf9 ILT |
50 | |
51 | // Now we know the final size of the output file and we know where | |
52 | // each piece of information goes. | |
c51e6221 ILT |
53 | Output_file* of = new Output_file(this->options_, |
54 | this->input_objects_->target()); | |
61ba1cf9 ILT |
55 | of->open(file_size); |
56 | ||
57 | // Queue up the final set of tasks. | |
58 | gold::queue_final_tasks(this->options_, this->input_objects_, | |
12e14209 | 59 | this->symtab_, this->layout_, workqueue, of); |
a2fb1b05 ILT |
60 | } |
61 | ||
62 | // Layout methods. | |
63 | ||
54dc6425 | 64 | Layout::Layout(const General_options& options) |
a3ad94ed | 65 | : options_(options), namepool_(), sympool_(), dynpool_(), signatures_(), |
61ba1cf9 | 66 | section_name_map_(), segment_list_(), section_list_(), |
a3ad94ed | 67 | unattached_section_list_(), special_output_list_(), |
14b31740 | 68 | tls_segment_(NULL), symtab_section_(NULL), |
3151305a | 69 | dynsym_section_(NULL), dynamic_section_(NULL), dynamic_data_(NULL), |
35cdfc9a ILT |
70 | eh_frame_section_(NULL), output_file_size_(-1), |
71 | input_requires_executable_stack_(false), | |
72 | input_with_gnu_stack_note_(false), | |
73 | input_without_gnu_stack_note_(false) | |
54dc6425 ILT |
74 | { |
75 | // Make space for more than enough segments for a typical file. | |
76 | // This is just for efficiency--it's OK if we wind up needing more. | |
a3ad94ed ILT |
77 | this->segment_list_.reserve(12); |
78 | ||
79 | // We expect three unattached Output_data objects: the file header, | |
80 | // the segment headers, and the section headers. | |
81 | this->special_output_list_.reserve(3); | |
54dc6425 ILT |
82 | } |
83 | ||
a2fb1b05 ILT |
84 | // Hash a key we use to look up an output section mapping. |
85 | ||
86 | size_t | |
87 | Layout::Hash_key::operator()(const Layout::Key& k) const | |
88 | { | |
f0641a0b | 89 | return k.first + k.second.first + k.second.second; |
a2fb1b05 ILT |
90 | } |
91 | ||
9e2dcb77 ILT |
92 | // Return whether PREFIX is a prefix of STR. |
93 | ||
94 | static inline bool | |
95 | is_prefix_of(const char* prefix, const char* str) | |
96 | { | |
97 | return strncmp(prefix, str, strlen(prefix)) == 0; | |
98 | } | |
99 | ||
a2fb1b05 ILT |
100 | // Whether to include this section in the link. |
101 | ||
102 | template<int size, bool big_endian> | |
103 | bool | |
9e2dcb77 | 104 | Layout::include_section(Object*, const char* name, |
a2fb1b05 ILT |
105 | const elfcpp::Shdr<size, big_endian>& shdr) |
106 | { | |
107 | // Some section types are never linked. Some are only linked when | |
108 | // doing a relocateable link. | |
109 | switch (shdr.get_sh_type()) | |
110 | { | |
111 | case elfcpp::SHT_NULL: | |
112 | case elfcpp::SHT_SYMTAB: | |
113 | case elfcpp::SHT_DYNSYM: | |
114 | case elfcpp::SHT_STRTAB: | |
115 | case elfcpp::SHT_HASH: | |
116 | case elfcpp::SHT_DYNAMIC: | |
117 | case elfcpp::SHT_SYMTAB_SHNDX: | |
118 | return false; | |
119 | ||
120 | case elfcpp::SHT_RELA: | |
121 | case elfcpp::SHT_REL: | |
122 | case elfcpp::SHT_GROUP: | |
7e1edb90 | 123 | return parameters->output_is_object(); |
a2fb1b05 | 124 | |
9e2dcb77 ILT |
125 | case elfcpp::SHT_PROGBITS: |
126 | if (parameters->strip_debug() | |
127 | && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0) | |
128 | { | |
129 | // Debugging sections can only be recognized by name. | |
130 | if (is_prefix_of(".debug", name) | |
131 | || is_prefix_of(".gnu.linkonce.wi.", name) | |
132 | || is_prefix_of(".line", name) | |
133 | || is_prefix_of(".stab", name)) | |
134 | return false; | |
135 | } | |
136 | return true; | |
137 | ||
a2fb1b05 | 138 | default: |
a2fb1b05 ILT |
139 | return true; |
140 | } | |
141 | } | |
142 | ||
ead1e424 | 143 | // Return an output section named NAME, or NULL if there is none. |
a2fb1b05 | 144 | |
a2fb1b05 | 145 | Output_section* |
ead1e424 | 146 | Layout::find_output_section(const char* name) const |
a2fb1b05 | 147 | { |
ead1e424 ILT |
148 | for (Section_name_map::const_iterator p = this->section_name_map_.begin(); |
149 | p != this->section_name_map_.end(); | |
150 | ++p) | |
f0641a0b | 151 | if (strcmp(p->second->name(), name) == 0) |
ead1e424 ILT |
152 | return p->second; |
153 | return NULL; | |
154 | } | |
a2fb1b05 | 155 | |
ead1e424 ILT |
156 | // Return an output segment of type TYPE, with segment flags SET set |
157 | // and segment flags CLEAR clear. Return NULL if there is none. | |
a2fb1b05 | 158 | |
ead1e424 ILT |
159 | Output_segment* |
160 | Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set, | |
161 | elfcpp::Elf_Word clear) const | |
162 | { | |
163 | for (Segment_list::const_iterator p = this->segment_list_.begin(); | |
164 | p != this->segment_list_.end(); | |
165 | ++p) | |
166 | if (static_cast<elfcpp::PT>((*p)->type()) == type | |
167 | && ((*p)->flags() & set) == set | |
168 | && ((*p)->flags() & clear) == 0) | |
169 | return *p; | |
170 | return NULL; | |
171 | } | |
a2fb1b05 | 172 | |
ead1e424 ILT |
173 | // Return the output section to use for section NAME with type TYPE |
174 | // and section flags FLAGS. | |
a2fb1b05 | 175 | |
ead1e424 | 176 | Output_section* |
f0641a0b ILT |
177 | Layout::get_output_section(const char* name, Stringpool::Key name_key, |
178 | elfcpp::Elf_Word type, elfcpp::Elf_Xword flags) | |
ead1e424 ILT |
179 | { |
180 | // We should ignore some flags. | |
181 | flags &= ~ (elfcpp::SHF_INFO_LINK | |
182 | | elfcpp::SHF_LINK_ORDER | |
b8e6aad9 ILT |
183 | | elfcpp::SHF_GROUP |
184 | | elfcpp::SHF_MERGE | |
185 | | elfcpp::SHF_STRINGS); | |
a2fb1b05 | 186 | |
f0641a0b | 187 | const Key key(name_key, std::make_pair(type, flags)); |
a2fb1b05 ILT |
188 | const std::pair<Key, Output_section*> v(key, NULL); |
189 | std::pair<Section_name_map::iterator, bool> ins( | |
190 | this->section_name_map_.insert(v)); | |
191 | ||
a2fb1b05 | 192 | if (!ins.second) |
ead1e424 | 193 | return ins.first->second; |
a2fb1b05 ILT |
194 | else |
195 | { | |
196 | // This is the first time we've seen this name/type/flags | |
197 | // combination. | |
ead1e424 | 198 | Output_section* os = this->make_output_section(name, type, flags); |
a2fb1b05 | 199 | ins.first->second = os; |
ead1e424 | 200 | return os; |
a2fb1b05 | 201 | } |
ead1e424 ILT |
202 | } |
203 | ||
204 | // Return the output section to use for input section SHNDX, with name | |
205 | // NAME, with header HEADER, from object OBJECT. Set *OFF to the | |
206 | // offset of this input section without the output section. | |
207 | ||
208 | template<int size, bool big_endian> | |
209 | Output_section* | |
f6ce93d6 | 210 | Layout::layout(Relobj* object, unsigned int shndx, const char* name, |
ead1e424 ILT |
211 | const elfcpp::Shdr<size, big_endian>& shdr, off_t* off) |
212 | { | |
213 | if (!this->include_section(object, name, shdr)) | |
214 | return NULL; | |
215 | ||
216 | // If we are not doing a relocateable link, choose the name to use | |
217 | // for the output section. | |
218 | size_t len = strlen(name); | |
7e1edb90 | 219 | if (!parameters->output_is_object()) |
ead1e424 ILT |
220 | name = Layout::output_section_name(name, &len); |
221 | ||
222 | // FIXME: Handle SHF_OS_NONCONFORMING here. | |
223 | ||
224 | // Canonicalize the section name. | |
f0641a0b | 225 | Stringpool::Key name_key; |
cfd73a4e | 226 | name = this->namepool_.add_prefix(name, len, &name_key); |
ead1e424 ILT |
227 | |
228 | // Find the output section. The output section is selected based on | |
229 | // the section name, type, and flags. | |
f0641a0b ILT |
230 | Output_section* os = this->get_output_section(name, name_key, |
231 | shdr.get_sh_type(), | |
ead1e424 | 232 | shdr.get_sh_flags()); |
a2fb1b05 | 233 | |
3151305a ILT |
234 | // Special GNU handling of sections named .eh_frame. |
235 | if (!parameters->output_is_object() | |
236 | && strcmp(name, ".eh_frame") == 0 | |
237 | && shdr.get_sh_size() > 0 | |
238 | && shdr.get_sh_type() == elfcpp::SHT_PROGBITS | |
239 | && shdr.get_sh_flags() == elfcpp::SHF_ALLOC) | |
240 | { | |
241 | this->layout_eh_frame(object, shndx, name, shdr, os, off); | |
242 | return os; | |
243 | } | |
244 | ||
a2fb1b05 ILT |
245 | // FIXME: Handle SHF_LINK_ORDER somewhere. |
246 | ||
ead1e424 | 247 | *off = os->add_input_section(object, shndx, name, shdr); |
a2fb1b05 ILT |
248 | |
249 | return os; | |
250 | } | |
251 | ||
3151305a ILT |
252 | // Special GNU handling of sections named .eh_frame. They will |
253 | // normally hold exception frame data. | |
254 | ||
255 | template<int size, bool big_endian> | |
256 | void | |
257 | Layout::layout_eh_frame(Relobj* object, | |
258 | unsigned int shndx, | |
259 | const char* name, | |
260 | const elfcpp::Shdr<size, big_endian>& shdr, | |
261 | Output_section* os, off_t* off) | |
262 | { | |
263 | if (this->eh_frame_section_ == NULL) | |
264 | { | |
265 | this->eh_frame_section_ = os; | |
266 | ||
267 | if (this->options_.create_eh_frame_hdr()) | |
268 | { | |
269 | Stringpool::Key hdr_name_key; | |
270 | const char* hdr_name = this->namepool_.add(".eh_frame_hdr", | |
cfd73a4e | 271 | false, |
3151305a ILT |
272 | &hdr_name_key); |
273 | Output_section* hdr_os = | |
274 | this->get_output_section(hdr_name, hdr_name_key, | |
275 | elfcpp::SHT_PROGBITS, | |
276 | elfcpp::SHF_ALLOC); | |
277 | ||
9025d29d | 278 | Eh_frame_hdr* hdr_posd = new Eh_frame_hdr(os); |
3151305a ILT |
279 | hdr_os->add_output_section_data(hdr_posd); |
280 | ||
281 | Output_segment* hdr_oseg = | |
282 | new Output_segment(elfcpp::PT_GNU_EH_FRAME, elfcpp::PF_R); | |
283 | this->segment_list_.push_back(hdr_oseg); | |
284 | hdr_oseg->add_output_section(hdr_os, elfcpp::PF_R); | |
285 | } | |
286 | } | |
287 | ||
288 | gold_assert(this->eh_frame_section_ == os); | |
289 | ||
290 | *off = os->add_input_section(object, shndx, name, shdr); | |
291 | } | |
292 | ||
ead1e424 ILT |
293 | // Add POSD to an output section using NAME, TYPE, and FLAGS. |
294 | ||
295 | void | |
296 | Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type, | |
297 | elfcpp::Elf_Xword flags, | |
298 | Output_section_data* posd) | |
299 | { | |
300 | // Canonicalize the name. | |
f0641a0b | 301 | Stringpool::Key name_key; |
cfd73a4e | 302 | name = this->namepool_.add(name, true, &name_key); |
ead1e424 | 303 | |
f0641a0b | 304 | Output_section* os = this->get_output_section(name, name_key, type, flags); |
ead1e424 ILT |
305 | os->add_output_section_data(posd); |
306 | } | |
307 | ||
a2fb1b05 ILT |
308 | // Map section flags to segment flags. |
309 | ||
310 | elfcpp::Elf_Word | |
311 | Layout::section_flags_to_segment(elfcpp::Elf_Xword flags) | |
312 | { | |
313 | elfcpp::Elf_Word ret = elfcpp::PF_R; | |
314 | if ((flags & elfcpp::SHF_WRITE) != 0) | |
315 | ret |= elfcpp::PF_W; | |
316 | if ((flags & elfcpp::SHF_EXECINSTR) != 0) | |
317 | ret |= elfcpp::PF_X; | |
318 | return ret; | |
319 | } | |
320 | ||
321 | // Make a new Output_section, and attach it to segments as | |
322 | // appropriate. | |
323 | ||
324 | Output_section* | |
325 | Layout::make_output_section(const char* name, elfcpp::Elf_Word type, | |
326 | elfcpp::Elf_Xword flags) | |
327 | { | |
b8e6aad9 | 328 | Output_section* os = new Output_section(name, type, flags); |
a3ad94ed | 329 | this->section_list_.push_back(os); |
a2fb1b05 ILT |
330 | |
331 | if ((flags & elfcpp::SHF_ALLOC) == 0) | |
a3ad94ed | 332 | this->unattached_section_list_.push_back(os); |
a2fb1b05 ILT |
333 | else |
334 | { | |
335 | // This output section goes into a PT_LOAD segment. | |
336 | ||
337 | elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags); | |
338 | ||
339 | // The only thing we really care about for PT_LOAD segments is | |
340 | // whether or not they are writable, so that is how we search | |
341 | // for them. People who need segments sorted on some other | |
342 | // basis will have to wait until we implement a mechanism for | |
343 | // them to describe the segments they want. | |
344 | ||
345 | Segment_list::const_iterator p; | |
346 | for (p = this->segment_list_.begin(); | |
347 | p != this->segment_list_.end(); | |
348 | ++p) | |
349 | { | |
350 | if ((*p)->type() == elfcpp::PT_LOAD | |
351 | && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W)) | |
352 | { | |
75f65a3e | 353 | (*p)->add_output_section(os, seg_flags); |
a2fb1b05 ILT |
354 | break; |
355 | } | |
356 | } | |
357 | ||
358 | if (p == this->segment_list_.end()) | |
359 | { | |
360 | Output_segment* oseg = new Output_segment(elfcpp::PT_LOAD, | |
361 | seg_flags); | |
362 | this->segment_list_.push_back(oseg); | |
75f65a3e | 363 | oseg->add_output_section(os, seg_flags); |
a2fb1b05 ILT |
364 | } |
365 | ||
366 | // If we see a loadable SHT_NOTE section, we create a PT_NOTE | |
367 | // segment. | |
368 | if (type == elfcpp::SHT_NOTE) | |
369 | { | |
370 | // See if we already have an equivalent PT_NOTE segment. | |
371 | for (p = this->segment_list_.begin(); | |
372 | p != segment_list_.end(); | |
373 | ++p) | |
374 | { | |
375 | if ((*p)->type() == elfcpp::PT_NOTE | |
376 | && (((*p)->flags() & elfcpp::PF_W) | |
377 | == (seg_flags & elfcpp::PF_W))) | |
378 | { | |
75f65a3e | 379 | (*p)->add_output_section(os, seg_flags); |
a2fb1b05 ILT |
380 | break; |
381 | } | |
382 | } | |
383 | ||
384 | if (p == this->segment_list_.end()) | |
385 | { | |
386 | Output_segment* oseg = new Output_segment(elfcpp::PT_NOTE, | |
387 | seg_flags); | |
388 | this->segment_list_.push_back(oseg); | |
75f65a3e | 389 | oseg->add_output_section(os, seg_flags); |
a2fb1b05 ILT |
390 | } |
391 | } | |
54dc6425 ILT |
392 | |
393 | // If we see a loadable SHF_TLS section, we create a PT_TLS | |
92e059d8 | 394 | // segment. There can only be one such segment. |
54dc6425 ILT |
395 | if ((flags & elfcpp::SHF_TLS) != 0) |
396 | { | |
92e059d8 | 397 | if (this->tls_segment_ == NULL) |
54dc6425 | 398 | { |
92e059d8 ILT |
399 | this->tls_segment_ = new Output_segment(elfcpp::PT_TLS, |
400 | seg_flags); | |
401 | this->segment_list_.push_back(this->tls_segment_); | |
54dc6425 | 402 | } |
92e059d8 | 403 | this->tls_segment_->add_output_section(os, seg_flags); |
54dc6425 | 404 | } |
a2fb1b05 ILT |
405 | } |
406 | ||
407 | return os; | |
408 | } | |
409 | ||
35cdfc9a ILT |
410 | // Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK |
411 | // is whether we saw a .note.GNU-stack section in the object file. | |
412 | // GNU_STACK_FLAGS is the section flags. The flags give the | |
413 | // protection required for stack memory. We record this in an | |
414 | // executable as a PT_GNU_STACK segment. If an object file does not | |
415 | // have a .note.GNU-stack segment, we must assume that it is an old | |
416 | // object. On some targets that will force an executable stack. | |
417 | ||
418 | void | |
419 | Layout::layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags) | |
420 | { | |
421 | if (!seen_gnu_stack) | |
422 | this->input_without_gnu_stack_note_ = true; | |
423 | else | |
424 | { | |
425 | this->input_with_gnu_stack_note_ = true; | |
426 | if ((gnu_stack_flags & elfcpp::SHF_EXECINSTR) != 0) | |
427 | this->input_requires_executable_stack_ = true; | |
428 | } | |
429 | } | |
430 | ||
a3ad94ed ILT |
431 | // Create the dynamic sections which are needed before we read the |
432 | // relocs. | |
433 | ||
434 | void | |
435 | Layout::create_initial_dynamic_sections(const Input_objects* input_objects, | |
436 | Symbol_table* symtab) | |
437 | { | |
436ca963 | 438 | if (parameters->doing_static_link()) |
a3ad94ed ILT |
439 | return; |
440 | ||
cfd73a4e | 441 | const char* dynamic_name = this->namepool_.add(".dynamic", false, NULL); |
a3ad94ed ILT |
442 | this->dynamic_section_ = this->make_output_section(dynamic_name, |
443 | elfcpp::SHT_DYNAMIC, | |
444 | (elfcpp::SHF_ALLOC | |
445 | | elfcpp::SHF_WRITE)); | |
446 | ||
14b31740 | 447 | symtab->define_in_output_data(input_objects->target(), "_DYNAMIC", NULL, |
a3ad94ed ILT |
448 | this->dynamic_section_, 0, 0, |
449 | elfcpp::STT_OBJECT, elfcpp::STB_LOCAL, | |
450 | elfcpp::STV_HIDDEN, 0, false, false); | |
16649710 | 451 | |
9025d29d | 452 | this->dynamic_data_ = new Output_data_dynamic(&this->dynpool_); |
16649710 ILT |
453 | |
454 | this->dynamic_section_->add_output_section_data(this->dynamic_data_); | |
a3ad94ed ILT |
455 | } |
456 | ||
bfd58944 ILT |
457 | // For each output section whose name can be represented as C symbol, |
458 | // define __start and __stop symbols for the section. This is a GNU | |
459 | // extension. | |
460 | ||
461 | void | |
462 | Layout::define_section_symbols(Symbol_table* symtab, const Target* target) | |
463 | { | |
464 | for (Section_list::const_iterator p = this->section_list_.begin(); | |
465 | p != this->section_list_.end(); | |
466 | ++p) | |
467 | { | |
468 | const char* const name = (*p)->name(); | |
469 | if (name[strspn(name, | |
470 | ("0123456789" | |
471 | "ABCDEFGHIJKLMNOPWRSTUVWXYZ" | |
472 | "abcdefghijklmnopqrstuvwxyz" | |
473 | "_"))] | |
474 | == '\0') | |
475 | { | |
476 | const std::string name_string(name); | |
477 | const std::string start_name("__start_" + name_string); | |
478 | const std::string stop_name("__stop_" + name_string); | |
479 | ||
480 | symtab->define_in_output_data(target, | |
481 | start_name.c_str(), | |
482 | NULL, // version | |
483 | *p, | |
484 | 0, // value | |
485 | 0, // symsize | |
486 | elfcpp::STT_NOTYPE, | |
487 | elfcpp::STB_GLOBAL, | |
488 | elfcpp::STV_DEFAULT, | |
489 | 0, // nonvis | |
490 | false, // offset_is_from_end | |
491 | false); // only_if_ref | |
492 | ||
493 | symtab->define_in_output_data(target, | |
494 | stop_name.c_str(), | |
495 | NULL, // version | |
496 | *p, | |
497 | 0, // value | |
498 | 0, // symsize | |
499 | elfcpp::STT_NOTYPE, | |
500 | elfcpp::STB_GLOBAL, | |
501 | elfcpp::STV_DEFAULT, | |
502 | 0, // nonvis | |
503 | true, // offset_is_from_end | |
504 | false); // only_if_ref | |
505 | } | |
506 | } | |
507 | } | |
508 | ||
75f65a3e ILT |
509 | // Find the first read-only PT_LOAD segment, creating one if |
510 | // necessary. | |
54dc6425 | 511 | |
75f65a3e ILT |
512 | Output_segment* |
513 | Layout::find_first_load_seg() | |
54dc6425 | 514 | { |
75f65a3e ILT |
515 | for (Segment_list::const_iterator p = this->segment_list_.begin(); |
516 | p != this->segment_list_.end(); | |
517 | ++p) | |
518 | { | |
519 | if ((*p)->type() == elfcpp::PT_LOAD | |
520 | && ((*p)->flags() & elfcpp::PF_R) != 0 | |
521 | && ((*p)->flags() & elfcpp::PF_W) == 0) | |
522 | return *p; | |
523 | } | |
524 | ||
525 | Output_segment* load_seg = new Output_segment(elfcpp::PT_LOAD, elfcpp::PF_R); | |
526 | this->segment_list_.push_back(load_seg); | |
527 | return load_seg; | |
54dc6425 ILT |
528 | } |
529 | ||
530 | // Finalize the layout. When this is called, we have created all the | |
531 | // output sections and all the output segments which are based on | |
532 | // input sections. We have several things to do, and we have to do | |
533 | // them in the right order, so that we get the right results correctly | |
534 | // and efficiently. | |
535 | ||
536 | // 1) Finalize the list of output segments and create the segment | |
537 | // table header. | |
538 | ||
539 | // 2) Finalize the dynamic symbol table and associated sections. | |
540 | ||
541 | // 3) Determine the final file offset of all the output segments. | |
542 | ||
543 | // 4) Determine the final file offset of all the SHF_ALLOC output | |
544 | // sections. | |
545 | ||
75f65a3e ILT |
546 | // 5) Create the symbol table sections and the section name table |
547 | // section. | |
548 | ||
549 | // 6) Finalize the symbol table: set symbol values to their final | |
54dc6425 ILT |
550 | // value and make a final determination of which symbols are going |
551 | // into the output symbol table. | |
552 | ||
54dc6425 ILT |
553 | // 7) Create the section table header. |
554 | ||
555 | // 8) Determine the final file offset of all the output sections which | |
556 | // are not SHF_ALLOC, including the section table header. | |
557 | ||
558 | // 9) Finalize the ELF file header. | |
559 | ||
75f65a3e ILT |
560 | // This function returns the size of the output file. |
561 | ||
562 | off_t | |
563 | Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab) | |
54dc6425 | 564 | { |
5a6f7e2d | 565 | Target* const target = input_objects->target(); |
dbe717ef | 566 | |
7e1edb90 | 567 | target->finalize_sections(this); |
5a6f7e2d | 568 | |
35cdfc9a ILT |
569 | this->create_gold_note(); |
570 | this->create_executable_stack_info(target); | |
4f211c8b | 571 | |
dbe717ef | 572 | Output_segment* phdr_seg = NULL; |
436ca963 | 573 | if (!parameters->doing_static_link()) |
54dc6425 | 574 | { |
dbe717ef ILT |
575 | // There was a dynamic object in the link. We need to create |
576 | // some information for the dynamic linker. | |
577 | ||
578 | // Create the PT_PHDR segment which will hold the program | |
579 | // headers. | |
580 | phdr_seg = new Output_segment(elfcpp::PT_PHDR, elfcpp::PF_R); | |
581 | this->segment_list_.push_back(phdr_seg); | |
582 | ||
14b31740 ILT |
583 | // Create the dynamic symbol table, including the hash table. |
584 | Output_section* dynstr; | |
585 | std::vector<Symbol*> dynamic_symbols; | |
586 | unsigned int local_dynamic_count; | |
587 | Versions versions; | |
588 | this->create_dynamic_symtab(target, symtab, &dynstr, | |
589 | &local_dynamic_count, &dynamic_symbols, | |
590 | &versions); | |
dbe717ef ILT |
591 | |
592 | // Create the .interp section to hold the name of the | |
593 | // interpreter, and put it in a PT_INTERP segment. | |
96f2030e ILT |
594 | if (!parameters->output_is_shared()) |
595 | this->create_interp(target); | |
a3ad94ed ILT |
596 | |
597 | // Finish the .dynamic section to hold the dynamic data, and put | |
598 | // it in a PT_DYNAMIC segment. | |
16649710 | 599 | this->finish_dynamic_section(input_objects, symtab); |
14b31740 ILT |
600 | |
601 | // We should have added everything we need to the dynamic string | |
602 | // table. | |
603 | this->dynpool_.set_string_offsets(); | |
604 | ||
605 | // Create the version sections. We can't do this until the | |
606 | // dynamic string table is complete. | |
46fe1623 | 607 | this->create_version_sections(&versions, symtab, local_dynamic_count, |
14b31740 | 608 | dynamic_symbols, dynstr); |
54dc6425 ILT |
609 | } |
610 | ||
611 | // FIXME: Handle PT_GNU_STACK. | |
612 | ||
75f65a3e ILT |
613 | Output_segment* load_seg = this->find_first_load_seg(); |
614 | ||
615 | // Lay out the segment headers. | |
75f65a3e | 616 | Output_segment_headers* segment_headers; |
9025d29d | 617 | segment_headers = new Output_segment_headers(this->segment_list_); |
75f65a3e | 618 | load_seg->add_initial_output_data(segment_headers); |
61ba1cf9 | 619 | this->special_output_list_.push_back(segment_headers); |
dbe717ef ILT |
620 | if (phdr_seg != NULL) |
621 | phdr_seg->add_initial_output_data(segment_headers); | |
75f65a3e ILT |
622 | |
623 | // Lay out the file header. | |
624 | Output_file_header* file_header; | |
9025d29d | 625 | file_header = new Output_file_header(target, symtab, segment_headers); |
75f65a3e | 626 | load_seg->add_initial_output_data(file_header); |
61ba1cf9 | 627 | this->special_output_list_.push_back(file_header); |
75f65a3e | 628 | |
ead1e424 ILT |
629 | // We set the output section indexes in set_segment_offsets and |
630 | // set_section_offsets. | |
631 | unsigned int shndx = 1; | |
632 | ||
633 | // Set the file offsets of all the segments, and all the sections | |
634 | // they contain. | |
a3ad94ed | 635 | off_t off = this->set_segment_offsets(target, load_seg, &shndx); |
75f65a3e | 636 | |
dba5a01f ILT |
637 | // Set the file offsets of all the data sections not associated with |
638 | // segments. This makes sure that debug sections have their offsets | |
639 | // before symbols are finalized. | |
86887060 | 640 | off = this->set_section_offsets(off, true); |
dba5a01f | 641 | |
75f65a3e | 642 | // Create the symbol table sections. |
9025d29d | 643 | this->create_symtab_sections(input_objects, symtab, &off); |
75f65a3e ILT |
644 | |
645 | // Create the .shstrtab section. | |
646 | Output_section* shstrtab_section = this->create_shstrtab(); | |
647 | ||
dba5a01f | 648 | // Set the file offsets of all the non-data sections not associated with |
75f65a3e | 649 | // segments. |
86887060 ILT |
650 | off = this->set_section_offsets(off, false); |
651 | ||
652 | // Now that all sections have been created, set the section indexes. | |
653 | shndx = this->set_section_indexes(shndx); | |
ead1e424 | 654 | |
75f65a3e | 655 | // Create the section table header. |
9025d29d | 656 | Output_section_headers* oshdrs = this->create_shdrs(&off); |
75f65a3e ILT |
657 | |
658 | file_header->set_section_info(oshdrs, shstrtab_section); | |
659 | ||
660 | // Now we know exactly where everything goes in the output file. | |
a3ad94ed | 661 | Output_data::layout_complete(); |
75f65a3e | 662 | |
e44fcf3b ILT |
663 | this->output_file_size_ = off; |
664 | ||
75f65a3e ILT |
665 | return off; |
666 | } | |
667 | ||
4f211c8b ILT |
668 | // Create a .note section for an executable or shared library. This |
669 | // records the version of gold used to create the binary. | |
670 | ||
671 | void | |
35cdfc9a | 672 | Layout::create_gold_note() |
4f211c8b ILT |
673 | { |
674 | if (parameters->output_is_object()) | |
675 | return; | |
676 | ||
e2305dc0 ILT |
677 | // Authorities all agree that the values in a .note field should |
678 | // be aligned on 4-byte boundaries for 32-bit binaries. However, | |
679 | // they differ on what the alignment is for 64-bit binaries. | |
680 | // The GABI says unambiguously they take 8-byte alignment: | |
681 | // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section | |
682 | // Other documentation says alignment should always be 4 bytes: | |
683 | // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format | |
684 | // GNU ld and GNU readelf both support the latter (at least as of | |
685 | // version 2.16.91), and glibc always generates the latter for | |
686 | // .note.ABI-tag (as of version 1.6), so that's the one we go with | |
687 | // here. | |
35cdfc9a | 688 | #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default. |
4f211c8b | 689 | const int size = parameters->get_size(); |
e2305dc0 ILT |
690 | #else |
691 | const int size = 32; | |
692 | #endif | |
4f211c8b ILT |
693 | |
694 | // The contents of the .note section. | |
695 | const char* name = "GNU"; | |
696 | std::string desc(std::string("gold ") + gold::get_version_string()); | |
697 | size_t namesz = strlen(name) + 1; | |
698 | size_t aligned_namesz = align_address(namesz, size / 8); | |
699 | size_t descsz = desc.length() + 1; | |
700 | size_t aligned_descsz = align_address(descsz, size / 8); | |
701 | const int note_type = 4; | |
702 | ||
703 | size_t notesz = 3 * (size / 8) + aligned_namesz + aligned_descsz; | |
704 | ||
705 | unsigned char buffer[128]; | |
706 | gold_assert(sizeof buffer >= notesz); | |
707 | memset(buffer, 0, notesz); | |
708 | ||
709 | bool is_big_endian = parameters->is_big_endian(); | |
710 | ||
711 | if (size == 32) | |
712 | { | |
713 | if (!is_big_endian) | |
714 | { | |
715 | elfcpp::Swap<32, false>::writeval(buffer, namesz); | |
716 | elfcpp::Swap<32, false>::writeval(buffer + 4, descsz); | |
717 | elfcpp::Swap<32, false>::writeval(buffer + 8, note_type); | |
718 | } | |
719 | else | |
720 | { | |
721 | elfcpp::Swap<32, true>::writeval(buffer, namesz); | |
722 | elfcpp::Swap<32, true>::writeval(buffer + 4, descsz); | |
723 | elfcpp::Swap<32, true>::writeval(buffer + 8, note_type); | |
724 | } | |
725 | } | |
726 | else if (size == 64) | |
727 | { | |
728 | if (!is_big_endian) | |
729 | { | |
730 | elfcpp::Swap<64, false>::writeval(buffer, namesz); | |
731 | elfcpp::Swap<64, false>::writeval(buffer + 8, descsz); | |
732 | elfcpp::Swap<64, false>::writeval(buffer + 16, note_type); | |
733 | } | |
734 | else | |
735 | { | |
736 | elfcpp::Swap<64, true>::writeval(buffer, namesz); | |
737 | elfcpp::Swap<64, true>::writeval(buffer + 8, descsz); | |
738 | elfcpp::Swap<64, true>::writeval(buffer + 16, note_type); | |
739 | } | |
740 | } | |
741 | else | |
742 | gold_unreachable(); | |
743 | ||
744 | memcpy(buffer + 3 * (size / 8), name, namesz); | |
745 | memcpy(buffer + 3 * (size / 8) + aligned_namesz, desc.data(), descsz); | |
746 | ||
cfd73a4e | 747 | const char* note_name = this->namepool_.add(".note", false, NULL); |
4f211c8b ILT |
748 | Output_section* os = this->make_output_section(note_name, |
749 | elfcpp::SHT_NOTE, | |
750 | 0); | |
751 | Output_section_data* posd = new Output_data_const(buffer, notesz, | |
752 | size / 8); | |
753 | os->add_output_section_data(posd); | |
754 | } | |
755 | ||
35cdfc9a ILT |
756 | // Record whether the stack should be executable. This can be set |
757 | // from the command line using the -z execstack or -z noexecstack | |
758 | // options. Otherwise, if any input file has a .note.GNU-stack | |
759 | // section with the SHF_EXECINSTR flag set, the stack should be | |
760 | // executable. Otherwise, if at least one input file a | |
761 | // .note.GNU-stack section, and some input file has no .note.GNU-stack | |
762 | // section, we use the target default for whether the stack should be | |
763 | // executable. Otherwise, we don't generate a stack note. When | |
764 | // generating a object file, we create a .note.GNU-stack section with | |
765 | // the appropriate marking. When generating an executable or shared | |
766 | // library, we create a PT_GNU_STACK segment. | |
767 | ||
768 | void | |
769 | Layout::create_executable_stack_info(const Target* target) | |
770 | { | |
771 | bool is_stack_executable; | |
772 | if (this->options_.is_execstack_set()) | |
773 | is_stack_executable = this->options_.is_stack_executable(); | |
774 | else if (!this->input_with_gnu_stack_note_) | |
775 | return; | |
776 | else | |
777 | { | |
778 | if (this->input_requires_executable_stack_) | |
779 | is_stack_executable = true; | |
780 | else if (this->input_without_gnu_stack_note_) | |
781 | is_stack_executable = target->is_default_stack_executable(); | |
782 | else | |
783 | is_stack_executable = false; | |
784 | } | |
785 | ||
786 | if (parameters->output_is_object()) | |
787 | { | |
788 | const char* name = this->namepool_.add(".note.GNU-stack", false, NULL); | |
789 | elfcpp::Elf_Xword flags = 0; | |
790 | if (is_stack_executable) | |
791 | flags |= elfcpp::SHF_EXECINSTR; | |
792 | this->make_output_section(name, elfcpp::SHT_PROGBITS, flags); | |
793 | } | |
794 | else | |
795 | { | |
796 | int flags = elfcpp::PF_R | elfcpp::PF_W; | |
797 | if (is_stack_executable) | |
798 | flags |= elfcpp::PF_X; | |
799 | Output_segment* oseg = new Output_segment(elfcpp::PT_GNU_STACK, flags); | |
800 | this->segment_list_.push_back(oseg); | |
801 | } | |
802 | } | |
803 | ||
75f65a3e ILT |
804 | // Return whether SEG1 should be before SEG2 in the output file. This |
805 | // is based entirely on the segment type and flags. When this is | |
806 | // called the segment addresses has normally not yet been set. | |
807 | ||
808 | bool | |
809 | Layout::segment_precedes(const Output_segment* seg1, | |
810 | const Output_segment* seg2) | |
811 | { | |
812 | elfcpp::Elf_Word type1 = seg1->type(); | |
813 | elfcpp::Elf_Word type2 = seg2->type(); | |
814 | ||
815 | // The single PT_PHDR segment is required to precede any loadable | |
816 | // segment. We simply make it always first. | |
817 | if (type1 == elfcpp::PT_PHDR) | |
818 | { | |
a3ad94ed | 819 | gold_assert(type2 != elfcpp::PT_PHDR); |
75f65a3e ILT |
820 | return true; |
821 | } | |
822 | if (type2 == elfcpp::PT_PHDR) | |
823 | return false; | |
824 | ||
825 | // The single PT_INTERP segment is required to precede any loadable | |
826 | // segment. We simply make it always second. | |
827 | if (type1 == elfcpp::PT_INTERP) | |
828 | { | |
a3ad94ed | 829 | gold_assert(type2 != elfcpp::PT_INTERP); |
75f65a3e ILT |
830 | return true; |
831 | } | |
832 | if (type2 == elfcpp::PT_INTERP) | |
833 | return false; | |
834 | ||
835 | // We then put PT_LOAD segments before any other segments. | |
836 | if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD) | |
837 | return true; | |
838 | if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD) | |
839 | return false; | |
840 | ||
92e059d8 ILT |
841 | // We put the PT_TLS segment last, because that is where the dynamic |
842 | // linker expects to find it (this is just for efficiency; other | |
843 | // positions would also work correctly). | |
844 | if (type1 == elfcpp::PT_TLS && type2 != elfcpp::PT_TLS) | |
845 | return false; | |
846 | if (type2 == elfcpp::PT_TLS && type1 != elfcpp::PT_TLS) | |
847 | return true; | |
848 | ||
75f65a3e ILT |
849 | const elfcpp::Elf_Word flags1 = seg1->flags(); |
850 | const elfcpp::Elf_Word flags2 = seg2->flags(); | |
851 | ||
852 | // The order of non-PT_LOAD segments is unimportant. We simply sort | |
853 | // by the numeric segment type and flags values. There should not | |
854 | // be more than one segment with the same type and flags. | |
855 | if (type1 != elfcpp::PT_LOAD) | |
856 | { | |
857 | if (type1 != type2) | |
858 | return type1 < type2; | |
a3ad94ed | 859 | gold_assert(flags1 != flags2); |
75f65a3e ILT |
860 | return flags1 < flags2; |
861 | } | |
862 | ||
863 | // We sort PT_LOAD segments based on the flags. Readonly segments | |
864 | // come before writable segments. Then executable segments come | |
865 | // before non-executable segments. Then the unlikely case of a | |
866 | // non-readable segment comes before the normal case of a readable | |
867 | // segment. If there are multiple segments with the same type and | |
868 | // flags, we require that the address be set, and we sort by | |
869 | // virtual address and then physical address. | |
870 | if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W)) | |
871 | return (flags1 & elfcpp::PF_W) == 0; | |
872 | if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X)) | |
873 | return (flags1 & elfcpp::PF_X) != 0; | |
874 | if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R)) | |
875 | return (flags1 & elfcpp::PF_R) == 0; | |
876 | ||
877 | uint64_t vaddr1 = seg1->vaddr(); | |
878 | uint64_t vaddr2 = seg2->vaddr(); | |
879 | if (vaddr1 != vaddr2) | |
880 | return vaddr1 < vaddr2; | |
881 | ||
882 | uint64_t paddr1 = seg1->paddr(); | |
883 | uint64_t paddr2 = seg2->paddr(); | |
a3ad94ed | 884 | gold_assert(paddr1 != paddr2); |
75f65a3e ILT |
885 | return paddr1 < paddr2; |
886 | } | |
887 | ||
ead1e424 ILT |
888 | // Set the file offsets of all the segments, and all the sections they |
889 | // contain. They have all been created. LOAD_SEG must be be laid out | |
890 | // first. Return the offset of the data to follow. | |
75f65a3e ILT |
891 | |
892 | off_t | |
ead1e424 ILT |
893 | Layout::set_segment_offsets(const Target* target, Output_segment* load_seg, |
894 | unsigned int *pshndx) | |
75f65a3e ILT |
895 | { |
896 | // Sort them into the final order. | |
54dc6425 ILT |
897 | std::sort(this->segment_list_.begin(), this->segment_list_.end(), |
898 | Layout::Compare_segments()); | |
899 | ||
75f65a3e ILT |
900 | // Find the PT_LOAD segments, and set their addresses and offsets |
901 | // and their section's addresses and offsets. | |
0c5e9c22 ILT |
902 | uint64_t addr; |
903 | if (options_.user_set_text_segment_address()) | |
904 | addr = options_.text_segment_address(); | |
905 | else | |
906 | addr = target->default_text_segment_address(); | |
75f65a3e ILT |
907 | off_t off = 0; |
908 | bool was_readonly = false; | |
909 | for (Segment_list::iterator p = this->segment_list_.begin(); | |
910 | p != this->segment_list_.end(); | |
911 | ++p) | |
912 | { | |
913 | if ((*p)->type() == elfcpp::PT_LOAD) | |
914 | { | |
915 | if (load_seg != NULL && load_seg != *p) | |
a3ad94ed | 916 | gold_unreachable(); |
75f65a3e ILT |
917 | load_seg = NULL; |
918 | ||
919 | // If the last segment was readonly, and this one is not, | |
920 | // then skip the address forward one page, maintaining the | |
921 | // same position within the page. This lets us store both | |
922 | // segments overlapping on a single page in the file, but | |
923 | // the loader will put them on different pages in memory. | |
924 | ||
925 | uint64_t orig_addr = addr; | |
926 | uint64_t orig_off = off; | |
927 | ||
928 | uint64_t aligned_addr = addr; | |
929 | uint64_t abi_pagesize = target->abi_pagesize(); | |
0496d5e5 ILT |
930 | |
931 | // FIXME: This should depend on the -n and -N options. | |
932 | (*p)->set_minimum_addralign(target->common_pagesize()); | |
933 | ||
75f65a3e ILT |
934 | if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0) |
935 | { | |
ead1e424 | 936 | uint64_t align = (*p)->addralign(); |
75f65a3e | 937 | |
ead1e424 | 938 | addr = align_address(addr, align); |
75f65a3e ILT |
939 | aligned_addr = addr; |
940 | if ((addr & (abi_pagesize - 1)) != 0) | |
941 | addr = addr + abi_pagesize; | |
942 | } | |
943 | ||
ead1e424 | 944 | unsigned int shndx_hold = *pshndx; |
75f65a3e | 945 | off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1)); |
ead1e424 | 946 | uint64_t new_addr = (*p)->set_section_addresses(addr, &off, pshndx); |
75f65a3e ILT |
947 | |
948 | // Now that we know the size of this segment, we may be able | |
949 | // to save a page in memory, at the cost of wasting some | |
950 | // file space, by instead aligning to the start of a new | |
951 | // page. Here we use the real machine page size rather than | |
952 | // the ABI mandated page size. | |
953 | ||
954 | if (aligned_addr != addr) | |
955 | { | |
956 | uint64_t common_pagesize = target->common_pagesize(); | |
957 | uint64_t first_off = (common_pagesize | |
958 | - (aligned_addr | |
959 | & (common_pagesize - 1))); | |
960 | uint64_t last_off = new_addr & (common_pagesize - 1); | |
961 | if (first_off > 0 | |
962 | && last_off > 0 | |
963 | && ((aligned_addr & ~ (common_pagesize - 1)) | |
964 | != (new_addr & ~ (common_pagesize - 1))) | |
965 | && first_off + last_off <= common_pagesize) | |
966 | { | |
ead1e424 ILT |
967 | *pshndx = shndx_hold; |
968 | addr = align_address(aligned_addr, common_pagesize); | |
75f65a3e | 969 | off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1)); |
ead1e424 | 970 | new_addr = (*p)->set_section_addresses(addr, &off, pshndx); |
75f65a3e ILT |
971 | } |
972 | } | |
973 | ||
974 | addr = new_addr; | |
975 | ||
976 | if (((*p)->flags() & elfcpp::PF_W) == 0) | |
977 | was_readonly = true; | |
978 | } | |
979 | } | |
980 | ||
981 | // Handle the non-PT_LOAD segments, setting their offsets from their | |
982 | // section's offsets. | |
983 | for (Segment_list::iterator p = this->segment_list_.begin(); | |
984 | p != this->segment_list_.end(); | |
985 | ++p) | |
986 | { | |
987 | if ((*p)->type() != elfcpp::PT_LOAD) | |
988 | (*p)->set_offset(); | |
989 | } | |
990 | ||
991 | return off; | |
992 | } | |
993 | ||
994 | // Set the file offset of all the sections not associated with a | |
995 | // segment. | |
996 | ||
997 | off_t | |
dba5a01f | 998 | Layout::set_section_offsets(off_t off, |
dba5a01f | 999 | bool do_bits_sections) |
75f65a3e | 1000 | { |
a3ad94ed ILT |
1001 | for (Section_list::iterator p = this->unattached_section_list_.begin(); |
1002 | p != this->unattached_section_list_.end(); | |
75f65a3e ILT |
1003 | ++p) |
1004 | { | |
dba5a01f ILT |
1005 | bool is_bits_section = ((*p)->type() == elfcpp::SHT_PROGBITS |
1006 | || (*p)->type() == elfcpp::SHT_NOBITS); | |
1007 | if (is_bits_section != do_bits_sections) | |
1008 | continue; | |
61ba1cf9 ILT |
1009 | if ((*p)->offset() != -1) |
1010 | continue; | |
ead1e424 | 1011 | off = align_address(off, (*p)->addralign()); |
75f65a3e ILT |
1012 | (*p)->set_address(0, off); |
1013 | off += (*p)->data_size(); | |
1014 | } | |
1015 | return off; | |
1016 | } | |
1017 | ||
86887060 ILT |
1018 | // Set the section indexes of all the sections not associated with a |
1019 | // segment. | |
1020 | ||
1021 | unsigned int | |
1022 | Layout::set_section_indexes(unsigned int shndx) | |
1023 | { | |
1024 | for (Section_list::iterator p = this->unattached_section_list_.begin(); | |
1025 | p != this->unattached_section_list_.end(); | |
1026 | ++p) | |
1027 | { | |
1028 | (*p)->set_out_shndx(shndx); | |
1029 | ++shndx; | |
1030 | } | |
1031 | return shndx; | |
1032 | } | |
1033 | ||
b8e6aad9 ILT |
1034 | // Create the symbol table sections. Here we also set the final |
1035 | // values of the symbols. At this point all the loadable sections are | |
1036 | // fully laid out. | |
75f65a3e ILT |
1037 | |
1038 | void | |
9025d29d | 1039 | Layout::create_symtab_sections(const Input_objects* input_objects, |
75f65a3e | 1040 | Symbol_table* symtab, |
16649710 | 1041 | off_t* poff) |
75f65a3e | 1042 | { |
61ba1cf9 ILT |
1043 | int symsize; |
1044 | unsigned int align; | |
9025d29d | 1045 | if (parameters->get_size() == 32) |
61ba1cf9 ILT |
1046 | { |
1047 | symsize = elfcpp::Elf_sizes<32>::sym_size; | |
1048 | align = 4; | |
1049 | } | |
9025d29d | 1050 | else if (parameters->get_size() == 64) |
61ba1cf9 ILT |
1051 | { |
1052 | symsize = elfcpp::Elf_sizes<64>::sym_size; | |
1053 | align = 8; | |
1054 | } | |
1055 | else | |
a3ad94ed | 1056 | gold_unreachable(); |
61ba1cf9 ILT |
1057 | |
1058 | off_t off = *poff; | |
ead1e424 | 1059 | off = align_address(off, align); |
61ba1cf9 ILT |
1060 | off_t startoff = off; |
1061 | ||
1062 | // Save space for the dummy symbol at the start of the section. We | |
1063 | // never bother to write this out--it will just be left as zero. | |
1064 | off += symsize; | |
c06b7b0b | 1065 | unsigned int local_symbol_index = 1; |
61ba1cf9 | 1066 | |
a3ad94ed ILT |
1067 | // Add STT_SECTION symbols for each Output section which needs one. |
1068 | for (Section_list::iterator p = this->section_list_.begin(); | |
1069 | p != this->section_list_.end(); | |
1070 | ++p) | |
1071 | { | |
1072 | if (!(*p)->needs_symtab_index()) | |
1073 | (*p)->set_symtab_index(-1U); | |
1074 | else | |
1075 | { | |
1076 | (*p)->set_symtab_index(local_symbol_index); | |
1077 | ++local_symbol_index; | |
1078 | off += symsize; | |
1079 | } | |
1080 | } | |
1081 | ||
f6ce93d6 ILT |
1082 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); |
1083 | p != input_objects->relobj_end(); | |
75f65a3e ILT |
1084 | ++p) |
1085 | { | |
1086 | Task_lock_obj<Object> tlo(**p); | |
c06b7b0b ILT |
1087 | unsigned int index = (*p)->finalize_local_symbols(local_symbol_index, |
1088 | off, | |
1089 | &this->sympool_); | |
1090 | off += (index - local_symbol_index) * symsize; | |
1091 | local_symbol_index = index; | |
75f65a3e ILT |
1092 | } |
1093 | ||
c06b7b0b | 1094 | unsigned int local_symcount = local_symbol_index; |
a3ad94ed | 1095 | gold_assert(local_symcount * symsize == off - startoff); |
61ba1cf9 | 1096 | |
16649710 ILT |
1097 | off_t dynoff; |
1098 | size_t dyn_global_index; | |
1099 | size_t dyncount; | |
1100 | if (this->dynsym_section_ == NULL) | |
1101 | { | |
1102 | dynoff = 0; | |
1103 | dyn_global_index = 0; | |
1104 | dyncount = 0; | |
1105 | } | |
1106 | else | |
1107 | { | |
1108 | dyn_global_index = this->dynsym_section_->info(); | |
1109 | off_t locsize = dyn_global_index * this->dynsym_section_->entsize(); | |
1110 | dynoff = this->dynsym_section_->offset() + locsize; | |
1111 | dyncount = (this->dynsym_section_->data_size() - locsize) / symsize; | |
f5c3f225 | 1112 | gold_assert(static_cast<off_t>(dyncount * symsize) |
16649710 ILT |
1113 | == this->dynsym_section_->data_size() - locsize); |
1114 | } | |
1115 | ||
1116 | off = symtab->finalize(local_symcount, off, dynoff, dyn_global_index, | |
1117 | dyncount, &this->sympool_); | |
75f65a3e | 1118 | |
9e2dcb77 ILT |
1119 | if (!parameters->strip_all()) |
1120 | { | |
1121 | this->sympool_.set_string_offsets(); | |
61ba1cf9 | 1122 | |
cfd73a4e | 1123 | const char* symtab_name = this->namepool_.add(".symtab", false, NULL); |
9e2dcb77 ILT |
1124 | Output_section* osymtab = this->make_output_section(symtab_name, |
1125 | elfcpp::SHT_SYMTAB, | |
1126 | 0); | |
1127 | this->symtab_section_ = osymtab; | |
a3ad94ed | 1128 | |
9e2dcb77 ILT |
1129 | Output_section_data* pos = new Output_data_space(off - startoff, |
1130 | align); | |
1131 | osymtab->add_output_section_data(pos); | |
61ba1cf9 | 1132 | |
cfd73a4e | 1133 | const char* strtab_name = this->namepool_.add(".strtab", false, NULL); |
9e2dcb77 ILT |
1134 | Output_section* ostrtab = this->make_output_section(strtab_name, |
1135 | elfcpp::SHT_STRTAB, | |
1136 | 0); | |
a3ad94ed | 1137 | |
9e2dcb77 ILT |
1138 | Output_section_data* pstr = new Output_data_strtab(&this->sympool_); |
1139 | ostrtab->add_output_section_data(pstr); | |
61ba1cf9 | 1140 | |
9e2dcb77 ILT |
1141 | osymtab->set_address(0, startoff); |
1142 | osymtab->set_link_section(ostrtab); | |
1143 | osymtab->set_info(local_symcount); | |
1144 | osymtab->set_entsize(symsize); | |
61ba1cf9 | 1145 | |
9e2dcb77 ILT |
1146 | *poff = off; |
1147 | } | |
75f65a3e ILT |
1148 | } |
1149 | ||
1150 | // Create the .shstrtab section, which holds the names of the | |
1151 | // sections. At the time this is called, we have created all the | |
1152 | // output sections except .shstrtab itself. | |
1153 | ||
1154 | Output_section* | |
1155 | Layout::create_shstrtab() | |
1156 | { | |
1157 | // FIXME: We don't need to create a .shstrtab section if we are | |
1158 | // stripping everything. | |
1159 | ||
cfd73a4e | 1160 | const char* name = this->namepool_.add(".shstrtab", false, NULL); |
75f65a3e | 1161 | |
61ba1cf9 ILT |
1162 | this->namepool_.set_string_offsets(); |
1163 | ||
a3ad94ed | 1164 | Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0); |
75f65a3e | 1165 | |
a3ad94ed ILT |
1166 | Output_section_data* posd = new Output_data_strtab(&this->namepool_); |
1167 | os->add_output_section_data(posd); | |
75f65a3e ILT |
1168 | |
1169 | return os; | |
1170 | } | |
1171 | ||
1172 | // Create the section headers. SIZE is 32 or 64. OFF is the file | |
1173 | // offset. | |
1174 | ||
1175 | Output_section_headers* | |
9025d29d | 1176 | Layout::create_shdrs(off_t* poff) |
75f65a3e ILT |
1177 | { |
1178 | Output_section_headers* oshdrs; | |
9025d29d | 1179 | oshdrs = new Output_section_headers(this, |
16649710 ILT |
1180 | &this->segment_list_, |
1181 | &this->unattached_section_list_, | |
61ba1cf9 | 1182 | &this->namepool_); |
ead1e424 | 1183 | off_t off = align_address(*poff, oshdrs->addralign()); |
75f65a3e | 1184 | oshdrs->set_address(0, off); |
61ba1cf9 ILT |
1185 | off += oshdrs->data_size(); |
1186 | *poff = off; | |
1187 | this->special_output_list_.push_back(oshdrs); | |
75f65a3e | 1188 | return oshdrs; |
54dc6425 ILT |
1189 | } |
1190 | ||
dbe717ef ILT |
1191 | // Create the dynamic symbol table. |
1192 | ||
1193 | void | |
14b31740 ILT |
1194 | Layout::create_dynamic_symtab(const Target* target, Symbol_table* symtab, |
1195 | Output_section **pdynstr, | |
1196 | unsigned int* plocal_dynamic_count, | |
1197 | std::vector<Symbol*>* pdynamic_symbols, | |
1198 | Versions* pversions) | |
dbe717ef | 1199 | { |
a3ad94ed ILT |
1200 | // Count all the symbols in the dynamic symbol table, and set the |
1201 | // dynamic symbol indexes. | |
dbe717ef | 1202 | |
a3ad94ed ILT |
1203 | // Skip symbol 0, which is always all zeroes. |
1204 | unsigned int index = 1; | |
dbe717ef | 1205 | |
a3ad94ed ILT |
1206 | // Add STT_SECTION symbols for each Output section which needs one. |
1207 | for (Section_list::iterator p = this->section_list_.begin(); | |
1208 | p != this->section_list_.end(); | |
1209 | ++p) | |
1210 | { | |
1211 | if (!(*p)->needs_dynsym_index()) | |
1212 | (*p)->set_dynsym_index(-1U); | |
1213 | else | |
1214 | { | |
1215 | (*p)->set_dynsym_index(index); | |
1216 | ++index; | |
1217 | } | |
1218 | } | |
1219 | ||
1220 | // FIXME: Some targets apparently require local symbols in the | |
1221 | // dynamic symbol table. Here is where we will have to count them, | |
1222 | // and set the dynamic symbol indexes, and add the names to | |
1223 | // this->dynpool_. | |
1224 | ||
1225 | unsigned int local_symcount = index; | |
14b31740 | 1226 | *plocal_dynamic_count = local_symcount; |
a3ad94ed ILT |
1227 | |
1228 | // FIXME: We have to tell set_dynsym_indexes whether the | |
1229 | // -E/--export-dynamic option was used. | |
35cdfc9a ILT |
1230 | index = symtab->set_dynsym_indexes(target, index, pdynamic_symbols, |
1231 | &this->dynpool_, pversions); | |
a3ad94ed ILT |
1232 | |
1233 | int symsize; | |
1234 | unsigned int align; | |
9025d29d | 1235 | const int size = parameters->get_size(); |
a3ad94ed ILT |
1236 | if (size == 32) |
1237 | { | |
1238 | symsize = elfcpp::Elf_sizes<32>::sym_size; | |
1239 | align = 4; | |
1240 | } | |
1241 | else if (size == 64) | |
1242 | { | |
1243 | symsize = elfcpp::Elf_sizes<64>::sym_size; | |
1244 | align = 8; | |
1245 | } | |
1246 | else | |
1247 | gold_unreachable(); | |
1248 | ||
14b31740 ILT |
1249 | // Create the dynamic symbol table section. |
1250 | ||
cfd73a4e | 1251 | const char* dynsym_name = this->namepool_.add(".dynsym", false, NULL); |
a3ad94ed ILT |
1252 | Output_section* dynsym = this->make_output_section(dynsym_name, |
1253 | elfcpp::SHT_DYNSYM, | |
1254 | elfcpp::SHF_ALLOC); | |
1255 | ||
1256 | Output_section_data* odata = new Output_data_space(index * symsize, | |
1257 | align); | |
1258 | dynsym->add_output_section_data(odata); | |
1259 | ||
1260 | dynsym->set_info(local_symcount); | |
1261 | dynsym->set_entsize(symsize); | |
1262 | dynsym->set_addralign(align); | |
1263 | ||
1264 | this->dynsym_section_ = dynsym; | |
1265 | ||
16649710 | 1266 | Output_data_dynamic* const odyn = this->dynamic_data_; |
a3ad94ed ILT |
1267 | odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym); |
1268 | odyn->add_constant(elfcpp::DT_SYMENT, symsize); | |
1269 | ||
14b31740 ILT |
1270 | // Create the dynamic string table section. |
1271 | ||
cfd73a4e | 1272 | const char* dynstr_name = this->namepool_.add(".dynstr", false, NULL); |
a3ad94ed ILT |
1273 | Output_section* dynstr = this->make_output_section(dynstr_name, |
1274 | elfcpp::SHT_STRTAB, | |
1275 | elfcpp::SHF_ALLOC); | |
1276 | ||
1277 | Output_section_data* strdata = new Output_data_strtab(&this->dynpool_); | |
1278 | dynstr->add_output_section_data(strdata); | |
1279 | ||
16649710 ILT |
1280 | dynsym->set_link_section(dynstr); |
1281 | this->dynamic_section_->set_link_section(dynstr); | |
1282 | ||
a3ad94ed ILT |
1283 | odyn->add_section_address(elfcpp::DT_STRTAB, dynstr); |
1284 | odyn->add_section_size(elfcpp::DT_STRSZ, dynstr); | |
1285 | ||
14b31740 ILT |
1286 | *pdynstr = dynstr; |
1287 | ||
1288 | // Create the hash tables. | |
1289 | ||
a3ad94ed ILT |
1290 | // FIXME: We need an option to create a GNU hash table. |
1291 | ||
1292 | unsigned char* phash; | |
1293 | unsigned int hashlen; | |
9025d29d | 1294 | Dynobj::create_elf_hash_table(*pdynamic_symbols, local_symcount, |
a3ad94ed ILT |
1295 | &phash, &hashlen); |
1296 | ||
cfd73a4e | 1297 | const char* hash_name = this->namepool_.add(".hash", false, NULL); |
a3ad94ed ILT |
1298 | Output_section* hashsec = this->make_output_section(hash_name, |
1299 | elfcpp::SHT_HASH, | |
1300 | elfcpp::SHF_ALLOC); | |
1301 | ||
1302 | Output_section_data* hashdata = new Output_data_const_buffer(phash, | |
1303 | hashlen, | |
1304 | align); | |
1305 | hashsec->add_output_section_data(hashdata); | |
1306 | ||
16649710 | 1307 | hashsec->set_link_section(dynsym); |
a3ad94ed | 1308 | hashsec->set_entsize(4); |
a3ad94ed ILT |
1309 | |
1310 | odyn->add_section_address(elfcpp::DT_HASH, hashsec); | |
dbe717ef ILT |
1311 | } |
1312 | ||
14b31740 ILT |
1313 | // Create the version sections. |
1314 | ||
1315 | void | |
9025d29d | 1316 | Layout::create_version_sections(const Versions* versions, |
46fe1623 | 1317 | const Symbol_table* symtab, |
14b31740 ILT |
1318 | unsigned int local_symcount, |
1319 | const std::vector<Symbol*>& dynamic_symbols, | |
1320 | const Output_section* dynstr) | |
1321 | { | |
1322 | if (!versions->any_defs() && !versions->any_needs()) | |
1323 | return; | |
1324 | ||
9025d29d | 1325 | if (parameters->get_size() == 32) |
14b31740 | 1326 | { |
9025d29d | 1327 | if (parameters->is_big_endian()) |
193a53d9 ILT |
1328 | { |
1329 | #ifdef HAVE_TARGET_32_BIG | |
1330 | this->sized_create_version_sections | |
1331 | SELECT_SIZE_ENDIAN_NAME(32, true)( | |
46fe1623 | 1332 | versions, symtab, local_symcount, dynamic_symbols, dynstr |
193a53d9 ILT |
1333 | SELECT_SIZE_ENDIAN(32, true)); |
1334 | #else | |
1335 | gold_unreachable(); | |
1336 | #endif | |
1337 | } | |
14b31740 | 1338 | else |
193a53d9 ILT |
1339 | { |
1340 | #ifdef HAVE_TARGET_32_LITTLE | |
1341 | this->sized_create_version_sections | |
1342 | SELECT_SIZE_ENDIAN_NAME(32, false)( | |
46fe1623 | 1343 | versions, symtab, local_symcount, dynamic_symbols, dynstr |
193a53d9 ILT |
1344 | SELECT_SIZE_ENDIAN(32, false)); |
1345 | #else | |
1346 | gold_unreachable(); | |
1347 | #endif | |
1348 | } | |
14b31740 | 1349 | } |
9025d29d | 1350 | else if (parameters->get_size() == 64) |
14b31740 | 1351 | { |
9025d29d | 1352 | if (parameters->is_big_endian()) |
193a53d9 ILT |
1353 | { |
1354 | #ifdef HAVE_TARGET_64_BIG | |
1355 | this->sized_create_version_sections | |
1356 | SELECT_SIZE_ENDIAN_NAME(64, true)( | |
46fe1623 | 1357 | versions, symtab, local_symcount, dynamic_symbols, dynstr |
193a53d9 ILT |
1358 | SELECT_SIZE_ENDIAN(64, true)); |
1359 | #else | |
1360 | gold_unreachable(); | |
1361 | #endif | |
1362 | } | |
14b31740 | 1363 | else |
193a53d9 ILT |
1364 | { |
1365 | #ifdef HAVE_TARGET_64_LITTLE | |
1366 | this->sized_create_version_sections | |
1367 | SELECT_SIZE_ENDIAN_NAME(64, false)( | |
46fe1623 | 1368 | versions, symtab, local_symcount, dynamic_symbols, dynstr |
193a53d9 ILT |
1369 | SELECT_SIZE_ENDIAN(64, false)); |
1370 | #else | |
1371 | gold_unreachable(); | |
1372 | #endif | |
1373 | } | |
14b31740 ILT |
1374 | } |
1375 | else | |
1376 | gold_unreachable(); | |
1377 | } | |
1378 | ||
1379 | // Create the version sections, sized version. | |
1380 | ||
1381 | template<int size, bool big_endian> | |
1382 | void | |
1383 | Layout::sized_create_version_sections( | |
1384 | const Versions* versions, | |
46fe1623 | 1385 | const Symbol_table* symtab, |
14b31740 ILT |
1386 | unsigned int local_symcount, |
1387 | const std::vector<Symbol*>& dynamic_symbols, | |
91da9340 ILT |
1388 | const Output_section* dynstr |
1389 | ACCEPT_SIZE_ENDIAN) | |
14b31740 | 1390 | { |
cfd73a4e | 1391 | const char* vname = this->namepool_.add(".gnu.version", false, NULL); |
14b31740 ILT |
1392 | Output_section* vsec = this->make_output_section(vname, |
1393 | elfcpp::SHT_GNU_versym, | |
1394 | elfcpp::SHF_ALLOC); | |
1395 | ||
1396 | unsigned char* vbuf; | |
1397 | unsigned int vsize; | |
91da9340 | 1398 | versions->symbol_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)( |
46fe1623 | 1399 | symtab, &this->dynpool_, local_symcount, dynamic_symbols, &vbuf, &vsize |
7e1edb90 | 1400 | SELECT_SIZE_ENDIAN(size, big_endian)); |
14b31740 ILT |
1401 | |
1402 | Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2); | |
1403 | ||
1404 | vsec->add_output_section_data(vdata); | |
1405 | vsec->set_entsize(2); | |
1406 | vsec->set_link_section(this->dynsym_section_); | |
1407 | ||
1408 | Output_data_dynamic* const odyn = this->dynamic_data_; | |
1409 | odyn->add_section_address(elfcpp::DT_VERSYM, vsec); | |
1410 | ||
1411 | if (versions->any_defs()) | |
1412 | { | |
cfd73a4e | 1413 | const char* vdname = this->namepool_.add(".gnu.version_d", false, NULL); |
14b31740 ILT |
1414 | Output_section *vdsec; |
1415 | vdsec = this->make_output_section(vdname, elfcpp::SHT_GNU_verdef, | |
1416 | elfcpp::SHF_ALLOC); | |
1417 | ||
1418 | unsigned char* vdbuf; | |
1419 | unsigned int vdsize; | |
1420 | unsigned int vdentries; | |
91da9340 ILT |
1421 | versions->def_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)( |
1422 | &this->dynpool_, &vdbuf, &vdsize, &vdentries | |
1423 | SELECT_SIZE_ENDIAN(size, big_endian)); | |
14b31740 ILT |
1424 | |
1425 | Output_section_data* vddata = new Output_data_const_buffer(vdbuf, | |
1426 | vdsize, | |
1427 | 4); | |
1428 | ||
1429 | vdsec->add_output_section_data(vddata); | |
1430 | vdsec->set_link_section(dynstr); | |
1431 | vdsec->set_info(vdentries); | |
1432 | ||
1433 | odyn->add_section_address(elfcpp::DT_VERDEF, vdsec); | |
1434 | odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries); | |
1435 | } | |
1436 | ||
1437 | if (versions->any_needs()) | |
1438 | { | |
cfd73a4e | 1439 | const char* vnname = this->namepool_.add(".gnu.version_r", false, NULL); |
14b31740 ILT |
1440 | Output_section* vnsec; |
1441 | vnsec = this->make_output_section(vnname, elfcpp::SHT_GNU_verneed, | |
1442 | elfcpp::SHF_ALLOC); | |
1443 | ||
1444 | unsigned char* vnbuf; | |
1445 | unsigned int vnsize; | |
1446 | unsigned int vnentries; | |
91da9340 ILT |
1447 | versions->need_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian) |
1448 | (&this->dynpool_, &vnbuf, &vnsize, &vnentries | |
1449 | SELECT_SIZE_ENDIAN(size, big_endian)); | |
14b31740 ILT |
1450 | |
1451 | Output_section_data* vndata = new Output_data_const_buffer(vnbuf, | |
1452 | vnsize, | |
1453 | 4); | |
1454 | ||
1455 | vnsec->add_output_section_data(vndata); | |
1456 | vnsec->set_link_section(dynstr); | |
1457 | vnsec->set_info(vnentries); | |
1458 | ||
1459 | odyn->add_section_address(elfcpp::DT_VERNEED, vnsec); | |
1460 | odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries); | |
1461 | } | |
1462 | } | |
1463 | ||
dbe717ef ILT |
1464 | // Create the .interp section and PT_INTERP segment. |
1465 | ||
1466 | void | |
1467 | Layout::create_interp(const Target* target) | |
1468 | { | |
1469 | const char* interp = this->options_.dynamic_linker(); | |
1470 | if (interp == NULL) | |
1471 | { | |
1472 | interp = target->dynamic_linker(); | |
a3ad94ed | 1473 | gold_assert(interp != NULL); |
dbe717ef ILT |
1474 | } |
1475 | ||
1476 | size_t len = strlen(interp) + 1; | |
1477 | ||
1478 | Output_section_data* odata = new Output_data_const(interp, len, 1); | |
1479 | ||
cfd73a4e | 1480 | const char* interp_name = this->namepool_.add(".interp", false, NULL); |
dbe717ef ILT |
1481 | Output_section* osec = this->make_output_section(interp_name, |
1482 | elfcpp::SHT_PROGBITS, | |
1483 | elfcpp::SHF_ALLOC); | |
1484 | osec->add_output_section_data(odata); | |
1485 | ||
1486 | Output_segment* oseg = new Output_segment(elfcpp::PT_INTERP, elfcpp::PF_R); | |
1487 | this->segment_list_.push_back(oseg); | |
1488 | oseg->add_initial_output_section(osec, elfcpp::PF_R); | |
1489 | } | |
1490 | ||
a3ad94ed ILT |
1491 | // Finish the .dynamic section and PT_DYNAMIC segment. |
1492 | ||
1493 | void | |
1494 | Layout::finish_dynamic_section(const Input_objects* input_objects, | |
16649710 | 1495 | const Symbol_table* symtab) |
a3ad94ed | 1496 | { |
a3ad94ed ILT |
1497 | Output_segment* oseg = new Output_segment(elfcpp::PT_DYNAMIC, |
1498 | elfcpp::PF_R | elfcpp::PF_W); | |
1499 | this->segment_list_.push_back(oseg); | |
1500 | oseg->add_initial_output_section(this->dynamic_section_, | |
1501 | elfcpp::PF_R | elfcpp::PF_W); | |
1502 | ||
16649710 ILT |
1503 | Output_data_dynamic* const odyn = this->dynamic_data_; |
1504 | ||
a3ad94ed ILT |
1505 | for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin(); |
1506 | p != input_objects->dynobj_end(); | |
1507 | ++p) | |
1508 | { | |
1509 | // FIXME: Handle --as-needed. | |
1510 | odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname()); | |
1511 | } | |
1512 | ||
1513 | // FIXME: Support --init and --fini. | |
1514 | Symbol* sym = symtab->lookup("_init"); | |
14b31740 | 1515 | if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj()) |
a3ad94ed ILT |
1516 | odyn->add_symbol(elfcpp::DT_INIT, sym); |
1517 | ||
1518 | sym = symtab->lookup("_fini"); | |
14b31740 | 1519 | if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj()) |
a3ad94ed ILT |
1520 | odyn->add_symbol(elfcpp::DT_FINI, sym); |
1521 | ||
1522 | // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY. | |
41f542e7 ILT |
1523 | |
1524 | // Add a DT_RPATH entry if needed. | |
1525 | const General_options::Dir_list& rpath(this->options_.rpath()); | |
1526 | if (!rpath.empty()) | |
1527 | { | |
1528 | std::string rpath_val; | |
1529 | for (General_options::Dir_list::const_iterator p = rpath.begin(); | |
1530 | p != rpath.end(); | |
1531 | ++p) | |
1532 | { | |
1533 | if (rpath_val.empty()) | |
ad2d6943 | 1534 | rpath_val = p->name(); |
41f542e7 ILT |
1535 | else |
1536 | { | |
1537 | // Eliminate duplicates. | |
1538 | General_options::Dir_list::const_iterator q; | |
1539 | for (q = rpath.begin(); q != p; ++q) | |
ad2d6943 | 1540 | if (q->name() == p->name()) |
41f542e7 ILT |
1541 | break; |
1542 | if (q == p) | |
1543 | { | |
1544 | rpath_val += ':'; | |
ad2d6943 | 1545 | rpath_val += p->name(); |
41f542e7 ILT |
1546 | } |
1547 | } | |
1548 | } | |
1549 | ||
1550 | odyn->add_string(elfcpp::DT_RPATH, rpath_val); | |
1551 | } | |
a3ad94ed ILT |
1552 | } |
1553 | ||
a2fb1b05 ILT |
1554 | // The mapping of .gnu.linkonce section names to real section names. |
1555 | ||
ead1e424 | 1556 | #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 } |
a2fb1b05 ILT |
1557 | const Layout::Linkonce_mapping Layout::linkonce_mapping[] = |
1558 | { | |
1559 | MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d". | |
1560 | MAPPING_INIT("t", ".text"), | |
1561 | MAPPING_INIT("r", ".rodata"), | |
1562 | MAPPING_INIT("d", ".data"), | |
1563 | MAPPING_INIT("b", ".bss"), | |
1564 | MAPPING_INIT("s", ".sdata"), | |
1565 | MAPPING_INIT("sb", ".sbss"), | |
1566 | MAPPING_INIT("s2", ".sdata2"), | |
1567 | MAPPING_INIT("sb2", ".sbss2"), | |
1568 | MAPPING_INIT("wi", ".debug_info"), | |
1569 | MAPPING_INIT("td", ".tdata"), | |
1570 | MAPPING_INIT("tb", ".tbss"), | |
1571 | MAPPING_INIT("lr", ".lrodata"), | |
1572 | MAPPING_INIT("l", ".ldata"), | |
1573 | MAPPING_INIT("lb", ".lbss"), | |
1574 | }; | |
1575 | #undef MAPPING_INIT | |
1576 | ||
1577 | const int Layout::linkonce_mapping_count = | |
1578 | sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]); | |
1579 | ||
1580 | // Return the name of the output section to use for a .gnu.linkonce | |
1581 | // section. This is based on the default ELF linker script of the old | |
1582 | // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo" | |
ead1e424 ILT |
1583 | // to ".text". Set *PLEN to the length of the name. *PLEN is |
1584 | // initialized to the length of NAME. | |
a2fb1b05 ILT |
1585 | |
1586 | const char* | |
ead1e424 | 1587 | Layout::linkonce_output_name(const char* name, size_t *plen) |
a2fb1b05 ILT |
1588 | { |
1589 | const char* s = name + sizeof(".gnu.linkonce") - 1; | |
1590 | if (*s != '.') | |
1591 | return name; | |
1592 | ++s; | |
1593 | const Linkonce_mapping* plm = linkonce_mapping; | |
1594 | for (int i = 0; i < linkonce_mapping_count; ++i, ++plm) | |
1595 | { | |
1596 | if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.') | |
ead1e424 ILT |
1597 | { |
1598 | *plen = plm->tolen; | |
1599 | return plm->to; | |
1600 | } | |
a2fb1b05 ILT |
1601 | } |
1602 | return name; | |
1603 | } | |
1604 | ||
ead1e424 ILT |
1605 | // Choose the output section name to use given an input section name. |
1606 | // Set *PLEN to the length of the name. *PLEN is initialized to the | |
1607 | // length of NAME. | |
1608 | ||
1609 | const char* | |
1610 | Layout::output_section_name(const char* name, size_t* plen) | |
1611 | { | |
1612 | if (Layout::is_linkonce(name)) | |
1613 | { | |
1614 | // .gnu.linkonce sections are laid out as though they were named | |
1615 | // for the sections are placed into. | |
1616 | return Layout::linkonce_output_name(name, plen); | |
1617 | } | |
1618 | ||
af4a8a83 ILT |
1619 | // gcc 4.3 generates the following sorts of section names when it |
1620 | // needs a section name specific to a function: | |
1621 | // .text.FN | |
1622 | // .rodata.FN | |
1623 | // .sdata2.FN | |
1624 | // .data.FN | |
1625 | // .data.rel.FN | |
1626 | // .data.rel.local.FN | |
1627 | // .data.rel.ro.FN | |
1628 | // .data.rel.ro.local.FN | |
1629 | // .sdata.FN | |
1630 | // .bss.FN | |
1631 | // .sbss.FN | |
1632 | // .tdata.FN | |
1633 | // .tbss.FN | |
1634 | ||
1635 | // The GNU linker maps all of those to the part before the .FN, | |
1636 | // except that .data.rel.local.FN is mapped to .data, and | |
1637 | // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections | |
1638 | // beginning with .data.rel.ro.local are grouped together. | |
1639 | ||
1640 | // For an anonymous namespace, the string FN can contain a '.'. | |
1641 | ||
1642 | // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the | |
1643 | // GNU linker maps to .rodata. | |
1644 | ||
1645 | // The .data.rel.ro sections enable a security feature triggered by | |
1646 | // the -z relro option. Section which need to be relocated at | |
1647 | // program startup time but which may be readonly after startup are | |
1648 | // grouped into .data.rel.ro. They are then put into a PT_GNU_RELRO | |
1649 | // segment. The dynamic linker will make that segment writable, | |
1650 | // perform relocations, and then make it read-only. FIXME: We do | |
1651 | // not yet implement this optimization. | |
1652 | ||
1653 | // It is hard to handle this in a principled way. | |
1654 | ||
1655 | // These are the rules we follow: | |
1656 | ||
1657 | // If the section name has no initial '.', or no dot other than an | |
1658 | // initial '.', we use the name unchanged (i.e., "mysection" and | |
1659 | // ".text" are unchanged). | |
1660 | ||
1661 | // If the name starts with ".data.rel.ro" we use ".data.rel.ro". | |
1662 | ||
1663 | // Otherwise, we drop the second '.' and everything that comes after | |
1664 | // it (i.e., ".text.XXX" becomes ".text"). | |
ead1e424 ILT |
1665 | |
1666 | const char* s = name; | |
af4a8a83 ILT |
1667 | if (*s != '.') |
1668 | return name; | |
1669 | ++s; | |
ead1e424 ILT |
1670 | const char* sdot = strchr(s, '.'); |
1671 | if (sdot == NULL) | |
1672 | return name; | |
1673 | ||
af4a8a83 ILT |
1674 | const char* const data_rel_ro = ".data.rel.ro"; |
1675 | if (strncmp(name, data_rel_ro, strlen(data_rel_ro)) == 0) | |
ead1e424 | 1676 | { |
af4a8a83 ILT |
1677 | *plen = strlen(data_rel_ro); |
1678 | return data_rel_ro; | |
ead1e424 ILT |
1679 | } |
1680 | ||
ead1e424 ILT |
1681 | *plen = sdot - name; |
1682 | return name; | |
1683 | } | |
1684 | ||
a2fb1b05 ILT |
1685 | // Record the signature of a comdat section, and return whether to |
1686 | // include it in the link. If GROUP is true, this is a regular | |
1687 | // section group. If GROUP is false, this is a group signature | |
1688 | // derived from the name of a linkonce section. We want linkonce | |
1689 | // signatures and group signatures to block each other, but we don't | |
1690 | // want a linkonce signature to block another linkonce signature. | |
1691 | ||
1692 | bool | |
1693 | Layout::add_comdat(const char* signature, bool group) | |
1694 | { | |
1695 | std::string sig(signature); | |
1696 | std::pair<Signatures::iterator, bool> ins( | |
ead1e424 | 1697 | this->signatures_.insert(std::make_pair(sig, group))); |
a2fb1b05 ILT |
1698 | |
1699 | if (ins.second) | |
1700 | { | |
1701 | // This is the first time we've seen this signature. | |
1702 | return true; | |
1703 | } | |
1704 | ||
1705 | if (ins.first->second) | |
1706 | { | |
1707 | // We've already seen a real section group with this signature. | |
1708 | return false; | |
1709 | } | |
1710 | else if (group) | |
1711 | { | |
1712 | // This is a real section group, and we've already seen a | |
a0fa0c07 | 1713 | // linkonce section with this signature. Record that we've seen |
a2fb1b05 ILT |
1714 | // a section group, and don't include this section group. |
1715 | ins.first->second = true; | |
1716 | return false; | |
1717 | } | |
1718 | else | |
1719 | { | |
1720 | // We've already seen a linkonce section and this is a linkonce | |
1721 | // section. These don't block each other--this may be the same | |
1722 | // symbol name with different section types. | |
1723 | return true; | |
1724 | } | |
1725 | } | |
1726 | ||
61ba1cf9 ILT |
1727 | // Write out data not associated with a section or the symbol table. |
1728 | ||
1729 | void | |
9025d29d | 1730 | Layout::write_data(const Symbol_table* symtab, Output_file* of) const |
61ba1cf9 | 1731 | { |
9e2dcb77 | 1732 | if (!parameters->strip_all()) |
a3ad94ed | 1733 | { |
9e2dcb77 ILT |
1734 | const Output_section* symtab_section = this->symtab_section_; |
1735 | for (Section_list::const_iterator p = this->section_list_.begin(); | |
1736 | p != this->section_list_.end(); | |
1737 | ++p) | |
a3ad94ed | 1738 | { |
9e2dcb77 ILT |
1739 | if ((*p)->needs_symtab_index()) |
1740 | { | |
1741 | gold_assert(symtab_section != NULL); | |
1742 | unsigned int index = (*p)->symtab_index(); | |
1743 | gold_assert(index > 0 && index != -1U); | |
1744 | off_t off = (symtab_section->offset() | |
1745 | + index * symtab_section->entsize()); | |
1746 | symtab->write_section_symbol(*p, of, off); | |
1747 | } | |
a3ad94ed ILT |
1748 | } |
1749 | } | |
1750 | ||
1751 | const Output_section* dynsym_section = this->dynsym_section_; | |
1752 | for (Section_list::const_iterator p = this->section_list_.begin(); | |
1753 | p != this->section_list_.end(); | |
1754 | ++p) | |
1755 | { | |
1756 | if ((*p)->needs_dynsym_index()) | |
1757 | { | |
1758 | gold_assert(dynsym_section != NULL); | |
1759 | unsigned int index = (*p)->dynsym_index(); | |
1760 | gold_assert(index > 0 && index != -1U); | |
1761 | off_t off = (dynsym_section->offset() | |
1762 | + index * dynsym_section->entsize()); | |
9025d29d | 1763 | symtab->write_section_symbol(*p, of, off); |
a3ad94ed ILT |
1764 | } |
1765 | } | |
1766 | ||
1767 | // Write out the Output_sections. Most won't have anything to | |
1768 | // write, since most of the data will come from input sections which | |
1769 | // are handled elsewhere. But some Output_sections do have | |
1770 | // Output_data. | |
1771 | for (Section_list::const_iterator p = this->section_list_.begin(); | |
1772 | p != this->section_list_.end(); | |
1773 | ++p) | |
1774 | (*p)->write(of); | |
1775 | ||
1776 | // Write out the Output_data which are not in an Output_section. | |
61ba1cf9 ILT |
1777 | for (Data_list::const_iterator p = this->special_output_list_.begin(); |
1778 | p != this->special_output_list_.end(); | |
1779 | ++p) | |
1780 | (*p)->write(of); | |
1781 | } | |
1782 | ||
1783 | // Write_data_task methods. | |
1784 | ||
1785 | // We can always run this task. | |
1786 | ||
1787 | Task::Is_runnable_type | |
1788 | Write_data_task::is_runnable(Workqueue*) | |
1789 | { | |
1790 | return IS_RUNNABLE; | |
1791 | } | |
1792 | ||
1793 | // We need to unlock FINAL_BLOCKER when finished. | |
1794 | ||
1795 | Task_locker* | |
1796 | Write_data_task::locks(Workqueue* workqueue) | |
1797 | { | |
1798 | return new Task_locker_block(*this->final_blocker_, workqueue); | |
1799 | } | |
1800 | ||
1801 | // Run the task--write out the data. | |
1802 | ||
1803 | void | |
1804 | Write_data_task::run(Workqueue*) | |
1805 | { | |
9025d29d | 1806 | this->layout_->write_data(this->symtab_, this->of_); |
61ba1cf9 ILT |
1807 | } |
1808 | ||
1809 | // Write_symbols_task methods. | |
1810 | ||
1811 | // We can always run this task. | |
1812 | ||
1813 | Task::Is_runnable_type | |
1814 | Write_symbols_task::is_runnable(Workqueue*) | |
1815 | { | |
1816 | return IS_RUNNABLE; | |
1817 | } | |
1818 | ||
1819 | // We need to unlock FINAL_BLOCKER when finished. | |
1820 | ||
1821 | Task_locker* | |
1822 | Write_symbols_task::locks(Workqueue* workqueue) | |
1823 | { | |
1824 | return new Task_locker_block(*this->final_blocker_, workqueue); | |
1825 | } | |
1826 | ||
1827 | // Run the task--write out the symbols. | |
1828 | ||
1829 | void | |
1830 | Write_symbols_task::run(Workqueue*) | |
1831 | { | |
16649710 ILT |
1832 | this->symtab_->write_globals(this->target_, this->sympool_, this->dynpool_, |
1833 | this->of_); | |
61ba1cf9 ILT |
1834 | } |
1835 | ||
92e059d8 | 1836 | // Close_task_runner methods. |
61ba1cf9 ILT |
1837 | |
1838 | // Run the task--close the file. | |
1839 | ||
1840 | void | |
92e059d8 | 1841 | Close_task_runner::run(Workqueue*) |
61ba1cf9 ILT |
1842 | { |
1843 | this->of_->close(); | |
1844 | } | |
1845 | ||
a2fb1b05 ILT |
1846 | // Instantiate the templates we need. We could use the configure |
1847 | // script to restrict this to only the ones for implemented targets. | |
1848 | ||
193a53d9 | 1849 | #ifdef HAVE_TARGET_32_LITTLE |
a2fb1b05 ILT |
1850 | template |
1851 | Output_section* | |
f6ce93d6 | 1852 | Layout::layout<32, false>(Relobj* object, unsigned int shndx, const char* name, |
a2fb1b05 | 1853 | const elfcpp::Shdr<32, false>& shdr, off_t*); |
193a53d9 | 1854 | #endif |
a2fb1b05 | 1855 | |
193a53d9 | 1856 | #ifdef HAVE_TARGET_32_BIG |
a2fb1b05 ILT |
1857 | template |
1858 | Output_section* | |
f6ce93d6 | 1859 | Layout::layout<32, true>(Relobj* object, unsigned int shndx, const char* name, |
a2fb1b05 | 1860 | const elfcpp::Shdr<32, true>& shdr, off_t*); |
193a53d9 | 1861 | #endif |
a2fb1b05 | 1862 | |
193a53d9 | 1863 | #ifdef HAVE_TARGET_64_LITTLE |
a2fb1b05 ILT |
1864 | template |
1865 | Output_section* | |
f6ce93d6 | 1866 | Layout::layout<64, false>(Relobj* object, unsigned int shndx, const char* name, |
a2fb1b05 | 1867 | const elfcpp::Shdr<64, false>& shdr, off_t*); |
193a53d9 | 1868 | #endif |
a2fb1b05 | 1869 | |
193a53d9 | 1870 | #ifdef HAVE_TARGET_64_BIG |
a2fb1b05 ILT |
1871 | template |
1872 | Output_section* | |
f6ce93d6 | 1873 | Layout::layout<64, true>(Relobj* object, unsigned int shndx, const char* name, |
a2fb1b05 | 1874 | const elfcpp::Shdr<64, true>& shdr, off_t*); |
193a53d9 | 1875 | #endif |
a2fb1b05 ILT |
1876 | |
1877 | ||
1878 | } // End namespace gold. |