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