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