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