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