]>
Commit | Line | Data |
---|---|---|
a2fb1b05 ILT |
1 | // layout.cc -- lay out output file sections for gold |
2 | ||
3 | #include "gold.h" | |
4 | ||
5 | #include <cassert> | |
6 | #include <cstring> | |
54dc6425 | 7 | #include <algorithm> |
a2fb1b05 ILT |
8 | #include <iostream> |
9 | #include <utility> | |
10 | ||
11 | #include "output.h" | |
f6ce93d6 | 12 | #include "symtab.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) |
ead1e424 | 42 | : options_(options), namepool_(), sympool_(), signatures_(), |
61ba1cf9 | 43 | section_name_map_(), segment_list_(), section_list_(), |
92e059d8 | 44 | special_output_list_(), tls_segment_(NULL) |
54dc6425 ILT |
45 | { |
46 | // Make space for more than enough segments for a typical file. | |
47 | // This is just for efficiency--it's OK if we wind up needing more. | |
48 | segment_list_.reserve(12); | |
49 | } | |
50 | ||
a2fb1b05 ILT |
51 | // Hash a key we use to look up an output section mapping. |
52 | ||
53 | size_t | |
54 | Layout::Hash_key::operator()(const Layout::Key& k) const | |
55 | { | |
f0641a0b | 56 | return k.first + k.second.first + k.second.second; |
a2fb1b05 ILT |
57 | } |
58 | ||
59 | // Whether to include this section in the link. | |
60 | ||
61 | template<int size, bool big_endian> | |
62 | bool | |
63 | Layout::include_section(Object*, const char*, | |
64 | const elfcpp::Shdr<size, big_endian>& shdr) | |
65 | { | |
66 | // Some section types are never linked. Some are only linked when | |
67 | // doing a relocateable link. | |
68 | switch (shdr.get_sh_type()) | |
69 | { | |
70 | case elfcpp::SHT_NULL: | |
71 | case elfcpp::SHT_SYMTAB: | |
72 | case elfcpp::SHT_DYNSYM: | |
73 | case elfcpp::SHT_STRTAB: | |
74 | case elfcpp::SHT_HASH: | |
75 | case elfcpp::SHT_DYNAMIC: | |
76 | case elfcpp::SHT_SYMTAB_SHNDX: | |
77 | return false; | |
78 | ||
79 | case elfcpp::SHT_RELA: | |
80 | case elfcpp::SHT_REL: | |
81 | case elfcpp::SHT_GROUP: | |
82 | return this->options_.is_relocatable(); | |
83 | ||
84 | default: | |
85 | // FIXME: Handle stripping debug sections here. | |
86 | return true; | |
87 | } | |
88 | } | |
89 | ||
ead1e424 | 90 | // Return an output section named NAME, or NULL if there is none. |
a2fb1b05 | 91 | |
a2fb1b05 | 92 | Output_section* |
ead1e424 | 93 | Layout::find_output_section(const char* name) const |
a2fb1b05 | 94 | { |
ead1e424 ILT |
95 | for (Section_name_map::const_iterator p = this->section_name_map_.begin(); |
96 | p != this->section_name_map_.end(); | |
97 | ++p) | |
f0641a0b | 98 | if (strcmp(p->second->name(), name) == 0) |
ead1e424 ILT |
99 | return p->second; |
100 | return NULL; | |
101 | } | |
a2fb1b05 | 102 | |
ead1e424 ILT |
103 | // Return an output segment of type TYPE, with segment flags SET set |
104 | // and segment flags CLEAR clear. Return NULL if there is none. | |
a2fb1b05 | 105 | |
ead1e424 ILT |
106 | Output_segment* |
107 | Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set, | |
108 | elfcpp::Elf_Word clear) const | |
109 | { | |
110 | for (Segment_list::const_iterator p = this->segment_list_.begin(); | |
111 | p != this->segment_list_.end(); | |
112 | ++p) | |
113 | if (static_cast<elfcpp::PT>((*p)->type()) == type | |
114 | && ((*p)->flags() & set) == set | |
115 | && ((*p)->flags() & clear) == 0) | |
116 | return *p; | |
117 | return NULL; | |
118 | } | |
a2fb1b05 | 119 | |
ead1e424 ILT |
120 | // Return the output section to use for section NAME with type TYPE |
121 | // and section flags FLAGS. | |
a2fb1b05 | 122 | |
ead1e424 | 123 | Output_section* |
f0641a0b ILT |
124 | Layout::get_output_section(const char* name, Stringpool::Key name_key, |
125 | elfcpp::Elf_Word type, elfcpp::Elf_Xword flags) | |
ead1e424 ILT |
126 | { |
127 | // We should ignore some flags. | |
128 | flags &= ~ (elfcpp::SHF_INFO_LINK | |
129 | | elfcpp::SHF_LINK_ORDER | |
130 | | elfcpp::SHF_GROUP); | |
a2fb1b05 | 131 | |
f0641a0b | 132 | const Key key(name_key, std::make_pair(type, flags)); |
a2fb1b05 ILT |
133 | const std::pair<Key, Output_section*> v(key, NULL); |
134 | std::pair<Section_name_map::iterator, bool> ins( | |
135 | this->section_name_map_.insert(v)); | |
136 | ||
a2fb1b05 | 137 | if (!ins.second) |
ead1e424 | 138 | return ins.first->second; |
a2fb1b05 ILT |
139 | else |
140 | { | |
141 | // This is the first time we've seen this name/type/flags | |
142 | // combination. | |
ead1e424 | 143 | Output_section* os = this->make_output_section(name, type, flags); |
a2fb1b05 | 144 | ins.first->second = os; |
ead1e424 | 145 | return os; |
a2fb1b05 | 146 | } |
ead1e424 ILT |
147 | } |
148 | ||
149 | // Return the output section to use for input section SHNDX, with name | |
150 | // NAME, with header HEADER, from object OBJECT. Set *OFF to the | |
151 | // offset of this input section without the output section. | |
152 | ||
153 | template<int size, bool big_endian> | |
154 | Output_section* | |
f6ce93d6 | 155 | Layout::layout(Relobj* object, unsigned int shndx, const char* name, |
ead1e424 ILT |
156 | const elfcpp::Shdr<size, big_endian>& shdr, off_t* off) |
157 | { | |
158 | if (!this->include_section(object, name, shdr)) | |
159 | return NULL; | |
160 | ||
161 | // If we are not doing a relocateable link, choose the name to use | |
162 | // for the output section. | |
163 | size_t len = strlen(name); | |
164 | if (!this->options_.is_relocatable()) | |
165 | name = Layout::output_section_name(name, &len); | |
166 | ||
167 | // FIXME: Handle SHF_OS_NONCONFORMING here. | |
168 | ||
169 | // Canonicalize the section name. | |
f0641a0b ILT |
170 | Stringpool::Key name_key; |
171 | name = this->namepool_.add(name, len, &name_key); | |
ead1e424 ILT |
172 | |
173 | // Find the output section. The output section is selected based on | |
174 | // the section name, type, and flags. | |
f0641a0b ILT |
175 | Output_section* os = this->get_output_section(name, name_key, |
176 | shdr.get_sh_type(), | |
ead1e424 | 177 | shdr.get_sh_flags()); |
a2fb1b05 ILT |
178 | |
179 | // FIXME: Handle SHF_LINK_ORDER somewhere. | |
180 | ||
ead1e424 | 181 | *off = os->add_input_section(object, shndx, name, shdr); |
a2fb1b05 ILT |
182 | |
183 | return os; | |
184 | } | |
185 | ||
ead1e424 ILT |
186 | // Add POSD to an output section using NAME, TYPE, and FLAGS. |
187 | ||
188 | void | |
189 | Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type, | |
190 | elfcpp::Elf_Xword flags, | |
191 | Output_section_data* posd) | |
192 | { | |
193 | // Canonicalize the name. | |
f0641a0b ILT |
194 | Stringpool::Key name_key; |
195 | name = this->namepool_.add(name, &name_key); | |
ead1e424 | 196 | |
f0641a0b | 197 | Output_section* os = this->get_output_section(name, name_key, type, flags); |
ead1e424 ILT |
198 | os->add_output_section_data(posd); |
199 | } | |
200 | ||
a2fb1b05 ILT |
201 | // Map section flags to segment flags. |
202 | ||
203 | elfcpp::Elf_Word | |
204 | Layout::section_flags_to_segment(elfcpp::Elf_Xword flags) | |
205 | { | |
206 | elfcpp::Elf_Word ret = elfcpp::PF_R; | |
207 | if ((flags & elfcpp::SHF_WRITE) != 0) | |
208 | ret |= elfcpp::PF_W; | |
209 | if ((flags & elfcpp::SHF_EXECINSTR) != 0) | |
210 | ret |= elfcpp::PF_X; | |
211 | return ret; | |
212 | } | |
213 | ||
214 | // Make a new Output_section, and attach it to segments as | |
215 | // appropriate. | |
216 | ||
217 | Output_section* | |
218 | Layout::make_output_section(const char* name, elfcpp::Elf_Word type, | |
219 | elfcpp::Elf_Xword flags) | |
220 | { | |
ead1e424 | 221 | Output_section* os = new Output_section(name, type, flags, true); |
a2fb1b05 ILT |
222 | |
223 | if ((flags & elfcpp::SHF_ALLOC) == 0) | |
224 | this->section_list_.push_back(os); | |
225 | else | |
226 | { | |
227 | // This output section goes into a PT_LOAD segment. | |
228 | ||
229 | elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags); | |
230 | ||
231 | // The only thing we really care about for PT_LOAD segments is | |
232 | // whether or not they are writable, so that is how we search | |
233 | // for them. People who need segments sorted on some other | |
234 | // basis will have to wait until we implement a mechanism for | |
235 | // them to describe the segments they want. | |
236 | ||
237 | Segment_list::const_iterator p; | |
238 | for (p = this->segment_list_.begin(); | |
239 | p != this->segment_list_.end(); | |
240 | ++p) | |
241 | { | |
242 | if ((*p)->type() == elfcpp::PT_LOAD | |
243 | && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W)) | |
244 | { | |
75f65a3e | 245 | (*p)->add_output_section(os, seg_flags); |
a2fb1b05 ILT |
246 | break; |
247 | } | |
248 | } | |
249 | ||
250 | if (p == this->segment_list_.end()) | |
251 | { | |
252 | Output_segment* oseg = new Output_segment(elfcpp::PT_LOAD, | |
253 | seg_flags); | |
254 | this->segment_list_.push_back(oseg); | |
75f65a3e | 255 | oseg->add_output_section(os, seg_flags); |
a2fb1b05 ILT |
256 | } |
257 | ||
258 | // If we see a loadable SHT_NOTE section, we create a PT_NOTE | |
259 | // segment. | |
260 | if (type == elfcpp::SHT_NOTE) | |
261 | { | |
262 | // See if we already have an equivalent PT_NOTE segment. | |
263 | for (p = this->segment_list_.begin(); | |
264 | p != segment_list_.end(); | |
265 | ++p) | |
266 | { | |
267 | if ((*p)->type() == elfcpp::PT_NOTE | |
268 | && (((*p)->flags() & elfcpp::PF_W) | |
269 | == (seg_flags & elfcpp::PF_W))) | |
270 | { | |
75f65a3e | 271 | (*p)->add_output_section(os, seg_flags); |
a2fb1b05 ILT |
272 | break; |
273 | } | |
274 | } | |
275 | ||
276 | if (p == this->segment_list_.end()) | |
277 | { | |
278 | Output_segment* oseg = new Output_segment(elfcpp::PT_NOTE, | |
279 | seg_flags); | |
280 | this->segment_list_.push_back(oseg); | |
75f65a3e | 281 | oseg->add_output_section(os, seg_flags); |
a2fb1b05 ILT |
282 | } |
283 | } | |
54dc6425 ILT |
284 | |
285 | // If we see a loadable SHF_TLS section, we create a PT_TLS | |
92e059d8 | 286 | // segment. There can only be one such segment. |
54dc6425 ILT |
287 | if ((flags & elfcpp::SHF_TLS) != 0) |
288 | { | |
92e059d8 | 289 | if (this->tls_segment_ == NULL) |
54dc6425 | 290 | { |
92e059d8 ILT |
291 | this->tls_segment_ = new Output_segment(elfcpp::PT_TLS, |
292 | seg_flags); | |
293 | this->segment_list_.push_back(this->tls_segment_); | |
54dc6425 | 294 | } |
92e059d8 | 295 | this->tls_segment_->add_output_section(os, seg_flags); |
54dc6425 | 296 | } |
a2fb1b05 ILT |
297 | } |
298 | ||
299 | return os; | |
300 | } | |
301 | ||
75f65a3e ILT |
302 | // Find the first read-only PT_LOAD segment, creating one if |
303 | // necessary. | |
54dc6425 | 304 | |
75f65a3e ILT |
305 | Output_segment* |
306 | Layout::find_first_load_seg() | |
54dc6425 | 307 | { |
75f65a3e ILT |
308 | for (Segment_list::const_iterator p = this->segment_list_.begin(); |
309 | p != this->segment_list_.end(); | |
310 | ++p) | |
311 | { | |
312 | if ((*p)->type() == elfcpp::PT_LOAD | |
313 | && ((*p)->flags() & elfcpp::PF_R) != 0 | |
314 | && ((*p)->flags() & elfcpp::PF_W) == 0) | |
315 | return *p; | |
316 | } | |
317 | ||
318 | Output_segment* load_seg = new Output_segment(elfcpp::PT_LOAD, elfcpp::PF_R); | |
319 | this->segment_list_.push_back(load_seg); | |
320 | return load_seg; | |
54dc6425 ILT |
321 | } |
322 | ||
323 | // Finalize the layout. When this is called, we have created all the | |
324 | // output sections and all the output segments which are based on | |
325 | // input sections. We have several things to do, and we have to do | |
326 | // them in the right order, so that we get the right results correctly | |
327 | // and efficiently. | |
328 | ||
329 | // 1) Finalize the list of output segments and create the segment | |
330 | // table header. | |
331 | ||
332 | // 2) Finalize the dynamic symbol table and associated sections. | |
333 | ||
334 | // 3) Determine the final file offset of all the output segments. | |
335 | ||
336 | // 4) Determine the final file offset of all the SHF_ALLOC output | |
337 | // sections. | |
338 | ||
75f65a3e ILT |
339 | // 5) Create the symbol table sections and the section name table |
340 | // section. | |
341 | ||
342 | // 6) Finalize the symbol table: set symbol values to their final | |
54dc6425 ILT |
343 | // value and make a final determination of which symbols are going |
344 | // into the output symbol table. | |
345 | ||
54dc6425 ILT |
346 | // 7) Create the section table header. |
347 | ||
348 | // 8) Determine the final file offset of all the output sections which | |
349 | // are not SHF_ALLOC, including the section table header. | |
350 | ||
351 | // 9) Finalize the ELF file header. | |
352 | ||
75f65a3e ILT |
353 | // This function returns the size of the output file. |
354 | ||
355 | off_t | |
356 | Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab) | |
54dc6425 | 357 | { |
dbe717ef ILT |
358 | const int size = input_objects->target()->get_size(); |
359 | ||
360 | Output_segment* phdr_seg = NULL; | |
54dc6425 ILT |
361 | if (input_objects->any_dynamic()) |
362 | { | |
dbe717ef ILT |
363 | // There was a dynamic object in the link. We need to create |
364 | // some information for the dynamic linker. | |
365 | ||
366 | // Create the PT_PHDR segment which will hold the program | |
367 | // headers. | |
368 | phdr_seg = new Output_segment(elfcpp::PT_PHDR, elfcpp::PF_R); | |
369 | this->segment_list_.push_back(phdr_seg); | |
370 | ||
371 | // Create the dynamic symbol table, including the hash table, | |
372 | // the dynamic relocations, and the version sections. | |
373 | this->create_dynamic_symtab(size, symtab); | |
374 | ||
375 | // Create the .dynamic section to hold the dynamic data, and put | |
376 | // it in a PT_DYNAMIC segment. | |
377 | this->create_dynamic_section(); | |
378 | ||
379 | // Create the .interp section to hold the name of the | |
380 | // interpreter, and put it in a PT_INTERP segment. | |
381 | this->create_interp(input_objects->target()); | |
54dc6425 ILT |
382 | } |
383 | ||
384 | // FIXME: Handle PT_GNU_STACK. | |
385 | ||
75f65a3e ILT |
386 | Output_segment* load_seg = this->find_first_load_seg(); |
387 | ||
388 | // Lay out the segment headers. | |
61ba1cf9 | 389 | bool big_endian = input_objects->target()->is_big_endian(); |
75f65a3e | 390 | Output_segment_headers* segment_headers; |
61ba1cf9 ILT |
391 | segment_headers = new Output_segment_headers(size, big_endian, |
392 | this->segment_list_); | |
75f65a3e | 393 | load_seg->add_initial_output_data(segment_headers); |
61ba1cf9 | 394 | this->special_output_list_.push_back(segment_headers); |
dbe717ef ILT |
395 | if (phdr_seg != NULL) |
396 | phdr_seg->add_initial_output_data(segment_headers); | |
75f65a3e ILT |
397 | |
398 | // Lay out the file header. | |
399 | Output_file_header* file_header; | |
400 | file_header = new Output_file_header(size, | |
61ba1cf9 | 401 | big_endian, |
75f65a3e ILT |
402 | this->options_, |
403 | input_objects->target(), | |
404 | symtab, | |
405 | segment_headers); | |
406 | load_seg->add_initial_output_data(file_header); | |
61ba1cf9 | 407 | this->special_output_list_.push_back(file_header); |
75f65a3e | 408 | |
ead1e424 ILT |
409 | // We set the output section indexes in set_segment_offsets and |
410 | // set_section_offsets. | |
411 | unsigned int shndx = 1; | |
412 | ||
413 | // Set the file offsets of all the segments, and all the sections | |
414 | // they contain. | |
415 | off_t off = this->set_segment_offsets(input_objects->target(), load_seg, | |
416 | &shndx); | |
75f65a3e ILT |
417 | |
418 | // Create the symbol table sections. | |
419 | // FIXME: We don't need to do this if we are stripping symbols. | |
420 | Output_section* osymtab; | |
421 | Output_section* ostrtab; | |
61ba1cf9 ILT |
422 | this->create_symtab_sections(size, input_objects, symtab, &off, |
423 | &osymtab, &ostrtab); | |
75f65a3e ILT |
424 | |
425 | // Create the .shstrtab section. | |
426 | Output_section* shstrtab_section = this->create_shstrtab(); | |
427 | ||
428 | // Set the file offsets of all the sections not associated with | |
429 | // segments. | |
ead1e424 ILT |
430 | off = this->set_section_offsets(off, &shndx); |
431 | ||
432 | // Now the section index of OSTRTAB is set. | |
433 | osymtab->set_link(ostrtab->out_shndx()); | |
75f65a3e ILT |
434 | |
435 | // Create the section table header. | |
61ba1cf9 | 436 | Output_section_headers* oshdrs = this->create_shdrs(size, big_endian, &off); |
75f65a3e ILT |
437 | |
438 | file_header->set_section_info(oshdrs, shstrtab_section); | |
439 | ||
440 | // Now we know exactly where everything goes in the output file. | |
441 | ||
442 | return off; | |
443 | } | |
444 | ||
445 | // Return whether SEG1 should be before SEG2 in the output file. This | |
446 | // is based entirely on the segment type and flags. When this is | |
447 | // called the segment addresses has normally not yet been set. | |
448 | ||
449 | bool | |
450 | Layout::segment_precedes(const Output_segment* seg1, | |
451 | const Output_segment* seg2) | |
452 | { | |
453 | elfcpp::Elf_Word type1 = seg1->type(); | |
454 | elfcpp::Elf_Word type2 = seg2->type(); | |
455 | ||
456 | // The single PT_PHDR segment is required to precede any loadable | |
457 | // segment. We simply make it always first. | |
458 | if (type1 == elfcpp::PT_PHDR) | |
459 | { | |
460 | assert(type2 != elfcpp::PT_PHDR); | |
461 | return true; | |
462 | } | |
463 | if (type2 == elfcpp::PT_PHDR) | |
464 | return false; | |
465 | ||
466 | // The single PT_INTERP segment is required to precede any loadable | |
467 | // segment. We simply make it always second. | |
468 | if (type1 == elfcpp::PT_INTERP) | |
469 | { | |
470 | assert(type2 != elfcpp::PT_INTERP); | |
471 | return true; | |
472 | } | |
473 | if (type2 == elfcpp::PT_INTERP) | |
474 | return false; | |
475 | ||
476 | // We then put PT_LOAD segments before any other segments. | |
477 | if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD) | |
478 | return true; | |
479 | if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD) | |
480 | return false; | |
481 | ||
92e059d8 ILT |
482 | // We put the PT_TLS segment last, because that is where the dynamic |
483 | // linker expects to find it (this is just for efficiency; other | |
484 | // positions would also work correctly). | |
485 | if (type1 == elfcpp::PT_TLS && type2 != elfcpp::PT_TLS) | |
486 | return false; | |
487 | if (type2 == elfcpp::PT_TLS && type1 != elfcpp::PT_TLS) | |
488 | return true; | |
489 | ||
75f65a3e ILT |
490 | const elfcpp::Elf_Word flags1 = seg1->flags(); |
491 | const elfcpp::Elf_Word flags2 = seg2->flags(); | |
492 | ||
493 | // The order of non-PT_LOAD segments is unimportant. We simply sort | |
494 | // by the numeric segment type and flags values. There should not | |
495 | // be more than one segment with the same type and flags. | |
496 | if (type1 != elfcpp::PT_LOAD) | |
497 | { | |
498 | if (type1 != type2) | |
499 | return type1 < type2; | |
500 | assert(flags1 != flags2); | |
501 | return flags1 < flags2; | |
502 | } | |
503 | ||
504 | // We sort PT_LOAD segments based on the flags. Readonly segments | |
505 | // come before writable segments. Then executable segments come | |
506 | // before non-executable segments. Then the unlikely case of a | |
507 | // non-readable segment comes before the normal case of a readable | |
508 | // segment. If there are multiple segments with the same type and | |
509 | // flags, we require that the address be set, and we sort by | |
510 | // virtual address and then physical address. | |
511 | if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W)) | |
512 | return (flags1 & elfcpp::PF_W) == 0; | |
513 | if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X)) | |
514 | return (flags1 & elfcpp::PF_X) != 0; | |
515 | if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R)) | |
516 | return (flags1 & elfcpp::PF_R) == 0; | |
517 | ||
518 | uint64_t vaddr1 = seg1->vaddr(); | |
519 | uint64_t vaddr2 = seg2->vaddr(); | |
520 | if (vaddr1 != vaddr2) | |
521 | return vaddr1 < vaddr2; | |
522 | ||
523 | uint64_t paddr1 = seg1->paddr(); | |
524 | uint64_t paddr2 = seg2->paddr(); | |
525 | assert(paddr1 != paddr2); | |
526 | return paddr1 < paddr2; | |
527 | } | |
528 | ||
ead1e424 ILT |
529 | // Set the file offsets of all the segments, and all the sections they |
530 | // contain. They have all been created. LOAD_SEG must be be laid out | |
531 | // first. Return the offset of the data to follow. | |
75f65a3e ILT |
532 | |
533 | off_t | |
ead1e424 ILT |
534 | Layout::set_segment_offsets(const Target* target, Output_segment* load_seg, |
535 | unsigned int *pshndx) | |
75f65a3e ILT |
536 | { |
537 | // Sort them into the final order. | |
54dc6425 ILT |
538 | std::sort(this->segment_list_.begin(), this->segment_list_.end(), |
539 | Layout::Compare_segments()); | |
540 | ||
75f65a3e ILT |
541 | // Find the PT_LOAD segments, and set their addresses and offsets |
542 | // and their section's addresses and offsets. | |
543 | uint64_t addr = target->text_segment_address(); | |
544 | off_t off = 0; | |
545 | bool was_readonly = false; | |
546 | for (Segment_list::iterator p = this->segment_list_.begin(); | |
547 | p != this->segment_list_.end(); | |
548 | ++p) | |
549 | { | |
550 | if ((*p)->type() == elfcpp::PT_LOAD) | |
551 | { | |
552 | if (load_seg != NULL && load_seg != *p) | |
553 | abort(); | |
554 | load_seg = NULL; | |
555 | ||
556 | // If the last segment was readonly, and this one is not, | |
557 | // then skip the address forward one page, maintaining the | |
558 | // same position within the page. This lets us store both | |
559 | // segments overlapping on a single page in the file, but | |
560 | // the loader will put them on different pages in memory. | |
561 | ||
562 | uint64_t orig_addr = addr; | |
563 | uint64_t orig_off = off; | |
564 | ||
565 | uint64_t aligned_addr = addr; | |
566 | uint64_t abi_pagesize = target->abi_pagesize(); | |
567 | if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0) | |
568 | { | |
ead1e424 | 569 | uint64_t align = (*p)->addralign(); |
75f65a3e | 570 | |
ead1e424 | 571 | addr = align_address(addr, align); |
75f65a3e ILT |
572 | aligned_addr = addr; |
573 | if ((addr & (abi_pagesize - 1)) != 0) | |
574 | addr = addr + abi_pagesize; | |
575 | } | |
576 | ||
ead1e424 | 577 | unsigned int shndx_hold = *pshndx; |
75f65a3e | 578 | off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1)); |
ead1e424 | 579 | uint64_t new_addr = (*p)->set_section_addresses(addr, &off, pshndx); |
75f65a3e ILT |
580 | |
581 | // Now that we know the size of this segment, we may be able | |
582 | // to save a page in memory, at the cost of wasting some | |
583 | // file space, by instead aligning to the start of a new | |
584 | // page. Here we use the real machine page size rather than | |
585 | // the ABI mandated page size. | |
586 | ||
587 | if (aligned_addr != addr) | |
588 | { | |
589 | uint64_t common_pagesize = target->common_pagesize(); | |
590 | uint64_t first_off = (common_pagesize | |
591 | - (aligned_addr | |
592 | & (common_pagesize - 1))); | |
593 | uint64_t last_off = new_addr & (common_pagesize - 1); | |
594 | if (first_off > 0 | |
595 | && last_off > 0 | |
596 | && ((aligned_addr & ~ (common_pagesize - 1)) | |
597 | != (new_addr & ~ (common_pagesize - 1))) | |
598 | && first_off + last_off <= common_pagesize) | |
599 | { | |
ead1e424 ILT |
600 | *pshndx = shndx_hold; |
601 | addr = align_address(aligned_addr, common_pagesize); | |
75f65a3e | 602 | off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1)); |
ead1e424 | 603 | new_addr = (*p)->set_section_addresses(addr, &off, pshndx); |
75f65a3e ILT |
604 | } |
605 | } | |
606 | ||
607 | addr = new_addr; | |
608 | ||
609 | if (((*p)->flags() & elfcpp::PF_W) == 0) | |
610 | was_readonly = true; | |
611 | } | |
612 | } | |
613 | ||
614 | // Handle the non-PT_LOAD segments, setting their offsets from their | |
615 | // section's offsets. | |
616 | for (Segment_list::iterator p = this->segment_list_.begin(); | |
617 | p != this->segment_list_.end(); | |
618 | ++p) | |
619 | { | |
620 | if ((*p)->type() != elfcpp::PT_LOAD) | |
621 | (*p)->set_offset(); | |
622 | } | |
623 | ||
624 | return off; | |
625 | } | |
626 | ||
627 | // Set the file offset of all the sections not associated with a | |
628 | // segment. | |
629 | ||
630 | off_t | |
ead1e424 | 631 | Layout::set_section_offsets(off_t off, unsigned int* pshndx) |
75f65a3e ILT |
632 | { |
633 | for (Layout::Section_list::iterator p = this->section_list_.begin(); | |
634 | p != this->section_list_.end(); | |
635 | ++p) | |
636 | { | |
ead1e424 ILT |
637 | (*p)->set_out_shndx(*pshndx); |
638 | ++*pshndx; | |
61ba1cf9 ILT |
639 | if ((*p)->offset() != -1) |
640 | continue; | |
ead1e424 | 641 | off = align_address(off, (*p)->addralign()); |
75f65a3e ILT |
642 | (*p)->set_address(0, off); |
643 | off += (*p)->data_size(); | |
644 | } | |
645 | return off; | |
646 | } | |
647 | ||
648 | // Create the symbol table sections. | |
649 | ||
650 | void | |
61ba1cf9 | 651 | Layout::create_symtab_sections(int size, const Input_objects* input_objects, |
75f65a3e | 652 | Symbol_table* symtab, |
61ba1cf9 | 653 | off_t* poff, |
75f65a3e ILT |
654 | Output_section** posymtab, |
655 | Output_section** postrtab) | |
656 | { | |
61ba1cf9 ILT |
657 | int symsize; |
658 | unsigned int align; | |
659 | if (size == 32) | |
660 | { | |
661 | symsize = elfcpp::Elf_sizes<32>::sym_size; | |
662 | align = 4; | |
663 | } | |
664 | else if (size == 64) | |
665 | { | |
666 | symsize = elfcpp::Elf_sizes<64>::sym_size; | |
667 | align = 8; | |
668 | } | |
669 | else | |
670 | abort(); | |
671 | ||
672 | off_t off = *poff; | |
ead1e424 | 673 | off = align_address(off, align); |
61ba1cf9 ILT |
674 | off_t startoff = off; |
675 | ||
676 | // Save space for the dummy symbol at the start of the section. We | |
677 | // never bother to write this out--it will just be left as zero. | |
678 | off += symsize; | |
679 | ||
f6ce93d6 ILT |
680 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); |
681 | p != input_objects->relobj_end(); | |
75f65a3e ILT |
682 | ++p) |
683 | { | |
684 | Task_lock_obj<Object> tlo(**p); | |
685 | off = (*p)->finalize_local_symbols(off, &this->sympool_); | |
686 | } | |
687 | ||
61ba1cf9 ILT |
688 | unsigned int local_symcount = (off - startoff) / symsize; |
689 | assert(local_symcount * symsize == off - startoff); | |
690 | ||
75f65a3e ILT |
691 | off = symtab->finalize(off, &this->sympool_); |
692 | ||
61ba1cf9 ILT |
693 | this->sympool_.set_string_offsets(); |
694 | ||
f0641a0b | 695 | const char* symtab_name = this->namepool_.add(".symtab", NULL); |
61ba1cf9 | 696 | Output_section* osymtab = new Output_section_symtab(symtab_name, |
ead1e424 | 697 | off - startoff); |
61ba1cf9 ILT |
698 | this->section_list_.push_back(osymtab); |
699 | ||
f0641a0b | 700 | const char* strtab_name = this->namepool_.add(".strtab", NULL); |
61ba1cf9 | 701 | Output_section *ostrtab = new Output_section_strtab(strtab_name, |
ead1e424 | 702 | &this->sympool_); |
61ba1cf9 ILT |
703 | this->section_list_.push_back(ostrtab); |
704 | this->special_output_list_.push_back(ostrtab); | |
705 | ||
706 | osymtab->set_address(0, startoff); | |
61ba1cf9 ILT |
707 | osymtab->set_info(local_symcount); |
708 | osymtab->set_entsize(symsize); | |
709 | osymtab->set_addralign(align); | |
710 | ||
711 | *poff = off; | |
712 | *posymtab = osymtab; | |
713 | *postrtab = ostrtab; | |
75f65a3e ILT |
714 | } |
715 | ||
716 | // Create the .shstrtab section, which holds the names of the | |
717 | // sections. At the time this is called, we have created all the | |
718 | // output sections except .shstrtab itself. | |
719 | ||
720 | Output_section* | |
721 | Layout::create_shstrtab() | |
722 | { | |
723 | // FIXME: We don't need to create a .shstrtab section if we are | |
724 | // stripping everything. | |
725 | ||
f0641a0b | 726 | const char* name = this->namepool_.add(".shstrtab", NULL); |
75f65a3e | 727 | |
61ba1cf9 ILT |
728 | this->namepool_.set_string_offsets(); |
729 | ||
ead1e424 | 730 | Output_section* os = new Output_section_strtab(name, &this->namepool_); |
75f65a3e ILT |
731 | |
732 | this->section_list_.push_back(os); | |
61ba1cf9 | 733 | this->special_output_list_.push_back(os); |
75f65a3e ILT |
734 | |
735 | return os; | |
736 | } | |
737 | ||
738 | // Create the section headers. SIZE is 32 or 64. OFF is the file | |
739 | // offset. | |
740 | ||
741 | Output_section_headers* | |
61ba1cf9 | 742 | Layout::create_shdrs(int size, bool big_endian, off_t* poff) |
75f65a3e ILT |
743 | { |
744 | Output_section_headers* oshdrs; | |
61ba1cf9 ILT |
745 | oshdrs = new Output_section_headers(size, big_endian, this->segment_list_, |
746 | this->section_list_, | |
747 | &this->namepool_); | |
ead1e424 | 748 | off_t off = align_address(*poff, oshdrs->addralign()); |
75f65a3e | 749 | oshdrs->set_address(0, off); |
61ba1cf9 ILT |
750 | off += oshdrs->data_size(); |
751 | *poff = off; | |
752 | this->special_output_list_.push_back(oshdrs); | |
75f65a3e | 753 | return oshdrs; |
54dc6425 ILT |
754 | } |
755 | ||
dbe717ef ILT |
756 | // Create the dynamic symbol table. |
757 | ||
758 | void | |
759 | Layout::create_dynamic_symtab(int, Symbol_table*) | |
760 | { | |
761 | abort(); | |
762 | } | |
763 | ||
764 | // Create the .dynamic section and PT_DYNAMIC segment. | |
765 | ||
766 | void | |
767 | Layout::create_dynamic_section() | |
768 | { | |
769 | abort(); | |
770 | } | |
771 | ||
772 | // Create the .interp section and PT_INTERP segment. | |
773 | ||
774 | void | |
775 | Layout::create_interp(const Target* target) | |
776 | { | |
777 | const char* interp = this->options_.dynamic_linker(); | |
778 | if (interp == NULL) | |
779 | { | |
780 | interp = target->dynamic_linker(); | |
781 | assert(interp != NULL); | |
782 | } | |
783 | ||
784 | size_t len = strlen(interp) + 1; | |
785 | ||
786 | Output_section_data* odata = new Output_data_const(interp, len, 1); | |
787 | ||
788 | const char* interp_name = this->namepool_.add(".interp", NULL); | |
789 | Output_section* osec = this->make_output_section(interp_name, | |
790 | elfcpp::SHT_PROGBITS, | |
791 | elfcpp::SHF_ALLOC); | |
792 | osec->add_output_section_data(odata); | |
793 | ||
794 | Output_segment* oseg = new Output_segment(elfcpp::PT_INTERP, elfcpp::PF_R); | |
795 | this->segment_list_.push_back(oseg); | |
796 | oseg->add_initial_output_section(osec, elfcpp::PF_R); | |
797 | } | |
798 | ||
a2fb1b05 ILT |
799 | // The mapping of .gnu.linkonce section names to real section names. |
800 | ||
ead1e424 | 801 | #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 } |
a2fb1b05 ILT |
802 | const Layout::Linkonce_mapping Layout::linkonce_mapping[] = |
803 | { | |
804 | MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d". | |
805 | MAPPING_INIT("t", ".text"), | |
806 | MAPPING_INIT("r", ".rodata"), | |
807 | MAPPING_INIT("d", ".data"), | |
808 | MAPPING_INIT("b", ".bss"), | |
809 | MAPPING_INIT("s", ".sdata"), | |
810 | MAPPING_INIT("sb", ".sbss"), | |
811 | MAPPING_INIT("s2", ".sdata2"), | |
812 | MAPPING_INIT("sb2", ".sbss2"), | |
813 | MAPPING_INIT("wi", ".debug_info"), | |
814 | MAPPING_INIT("td", ".tdata"), | |
815 | MAPPING_INIT("tb", ".tbss"), | |
816 | MAPPING_INIT("lr", ".lrodata"), | |
817 | MAPPING_INIT("l", ".ldata"), | |
818 | MAPPING_INIT("lb", ".lbss"), | |
819 | }; | |
820 | #undef MAPPING_INIT | |
821 | ||
822 | const int Layout::linkonce_mapping_count = | |
823 | sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]); | |
824 | ||
825 | // Return the name of the output section to use for a .gnu.linkonce | |
826 | // section. This is based on the default ELF linker script of the old | |
827 | // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo" | |
ead1e424 ILT |
828 | // to ".text". Set *PLEN to the length of the name. *PLEN is |
829 | // initialized to the length of NAME. | |
a2fb1b05 ILT |
830 | |
831 | const char* | |
ead1e424 | 832 | Layout::linkonce_output_name(const char* name, size_t *plen) |
a2fb1b05 ILT |
833 | { |
834 | const char* s = name + sizeof(".gnu.linkonce") - 1; | |
835 | if (*s != '.') | |
836 | return name; | |
837 | ++s; | |
838 | const Linkonce_mapping* plm = linkonce_mapping; | |
839 | for (int i = 0; i < linkonce_mapping_count; ++i, ++plm) | |
840 | { | |
841 | if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.') | |
ead1e424 ILT |
842 | { |
843 | *plen = plm->tolen; | |
844 | return plm->to; | |
845 | } | |
a2fb1b05 ILT |
846 | } |
847 | return name; | |
848 | } | |
849 | ||
ead1e424 ILT |
850 | // Choose the output section name to use given an input section name. |
851 | // Set *PLEN to the length of the name. *PLEN is initialized to the | |
852 | // length of NAME. | |
853 | ||
854 | const char* | |
855 | Layout::output_section_name(const char* name, size_t* plen) | |
856 | { | |
857 | if (Layout::is_linkonce(name)) | |
858 | { | |
859 | // .gnu.linkonce sections are laid out as though they were named | |
860 | // for the sections are placed into. | |
861 | return Layout::linkonce_output_name(name, plen); | |
862 | } | |
863 | ||
864 | // If the section name has no '.', or only an initial '.', we use | |
865 | // the name unchanged (i.e., ".text" is unchanged). | |
866 | ||
867 | // Otherwise, if the section name does not include ".rel", we drop | |
868 | // the last '.' and everything that follows (i.e., ".text.XXX" | |
869 | // becomes ".text"). | |
870 | ||
871 | // Otherwise, if the section name has zero or one '.' after the | |
872 | // ".rel", we use the name unchanged (i.e., ".rel.text" is | |
873 | // unchanged). | |
874 | ||
875 | // Otherwise, we drop the last '.' and everything that follows | |
876 | // (i.e., ".rel.text.XXX" becomes ".rel.text"). | |
877 | ||
878 | const char* s = name; | |
879 | if (*s == '.') | |
880 | ++s; | |
881 | const char* sdot = strchr(s, '.'); | |
882 | if (sdot == NULL) | |
883 | return name; | |
884 | ||
885 | const char* srel = strstr(s, ".rel"); | |
886 | if (srel == NULL) | |
887 | { | |
888 | *plen = sdot - name; | |
889 | return name; | |
890 | } | |
891 | ||
892 | sdot = strchr(srel + 1, '.'); | |
893 | if (sdot == NULL) | |
894 | return name; | |
895 | sdot = strchr(sdot + 1, '.'); | |
896 | if (sdot == NULL) | |
897 | return name; | |
898 | ||
899 | *plen = sdot - name; | |
900 | return name; | |
901 | } | |
902 | ||
a2fb1b05 ILT |
903 | // Record the signature of a comdat section, and return whether to |
904 | // include it in the link. If GROUP is true, this is a regular | |
905 | // section group. If GROUP is false, this is a group signature | |
906 | // derived from the name of a linkonce section. We want linkonce | |
907 | // signatures and group signatures to block each other, but we don't | |
908 | // want a linkonce signature to block another linkonce signature. | |
909 | ||
910 | bool | |
911 | Layout::add_comdat(const char* signature, bool group) | |
912 | { | |
913 | std::string sig(signature); | |
914 | std::pair<Signatures::iterator, bool> ins( | |
ead1e424 | 915 | this->signatures_.insert(std::make_pair(sig, group))); |
a2fb1b05 ILT |
916 | |
917 | if (ins.second) | |
918 | { | |
919 | // This is the first time we've seen this signature. | |
920 | return true; | |
921 | } | |
922 | ||
923 | if (ins.first->second) | |
924 | { | |
925 | // We've already seen a real section group with this signature. | |
926 | return false; | |
927 | } | |
928 | else if (group) | |
929 | { | |
930 | // This is a real section group, and we've already seen a | |
931 | // linkonce section with tihs signature. Record that we've seen | |
932 | // a section group, and don't include this section group. | |
933 | ins.first->second = true; | |
934 | return false; | |
935 | } | |
936 | else | |
937 | { | |
938 | // We've already seen a linkonce section and this is a linkonce | |
939 | // section. These don't block each other--this may be the same | |
940 | // symbol name with different section types. | |
941 | return true; | |
942 | } | |
943 | } | |
944 | ||
61ba1cf9 ILT |
945 | // Write out data not associated with a section or the symbol table. |
946 | ||
947 | void | |
948 | Layout::write_data(Output_file* of) const | |
949 | { | |
950 | for (Data_list::const_iterator p = this->special_output_list_.begin(); | |
951 | p != this->special_output_list_.end(); | |
952 | ++p) | |
953 | (*p)->write(of); | |
954 | } | |
955 | ||
956 | // Write_data_task methods. | |
957 | ||
958 | // We can always run this task. | |
959 | ||
960 | Task::Is_runnable_type | |
961 | Write_data_task::is_runnable(Workqueue*) | |
962 | { | |
963 | return IS_RUNNABLE; | |
964 | } | |
965 | ||
966 | // We need to unlock FINAL_BLOCKER when finished. | |
967 | ||
968 | Task_locker* | |
969 | Write_data_task::locks(Workqueue* workqueue) | |
970 | { | |
971 | return new Task_locker_block(*this->final_blocker_, workqueue); | |
972 | } | |
973 | ||
974 | // Run the task--write out the data. | |
975 | ||
976 | void | |
977 | Write_data_task::run(Workqueue*) | |
978 | { | |
979 | this->layout_->write_data(this->of_); | |
980 | } | |
981 | ||
982 | // Write_symbols_task methods. | |
983 | ||
984 | // We can always run this task. | |
985 | ||
986 | Task::Is_runnable_type | |
987 | Write_symbols_task::is_runnable(Workqueue*) | |
988 | { | |
989 | return IS_RUNNABLE; | |
990 | } | |
991 | ||
992 | // We need to unlock FINAL_BLOCKER when finished. | |
993 | ||
994 | Task_locker* | |
995 | Write_symbols_task::locks(Workqueue* workqueue) | |
996 | { | |
997 | return new Task_locker_block(*this->final_blocker_, workqueue); | |
998 | } | |
999 | ||
1000 | // Run the task--write out the symbols. | |
1001 | ||
1002 | void | |
1003 | Write_symbols_task::run(Workqueue*) | |
1004 | { | |
1005 | this->symtab_->write_globals(this->target_, this->sympool_, this->of_); | |
1006 | } | |
1007 | ||
92e059d8 | 1008 | // Close_task_runner methods. |
61ba1cf9 ILT |
1009 | |
1010 | // Run the task--close the file. | |
1011 | ||
1012 | void | |
92e059d8 | 1013 | Close_task_runner::run(Workqueue*) |
61ba1cf9 ILT |
1014 | { |
1015 | this->of_->close(); | |
1016 | } | |
1017 | ||
a2fb1b05 ILT |
1018 | // Instantiate the templates we need. We could use the configure |
1019 | // script to restrict this to only the ones for implemented targets. | |
1020 | ||
1021 | template | |
1022 | Output_section* | |
f6ce93d6 | 1023 | Layout::layout<32, false>(Relobj* object, unsigned int shndx, const char* name, |
a2fb1b05 ILT |
1024 | const elfcpp::Shdr<32, false>& shdr, off_t*); |
1025 | ||
1026 | template | |
1027 | Output_section* | |
f6ce93d6 | 1028 | Layout::layout<32, true>(Relobj* object, unsigned int shndx, const char* name, |
a2fb1b05 ILT |
1029 | const elfcpp::Shdr<32, true>& shdr, off_t*); |
1030 | ||
1031 | template | |
1032 | Output_section* | |
f6ce93d6 | 1033 | Layout::layout<64, false>(Relobj* object, unsigned int shndx, const char* name, |
a2fb1b05 ILT |
1034 | const elfcpp::Shdr<64, false>& shdr, off_t*); |
1035 | ||
1036 | template | |
1037 | Output_section* | |
f6ce93d6 | 1038 | Layout::layout<64, true>(Relobj* object, unsigned int shndx, const char* name, |
a2fb1b05 ILT |
1039 | const elfcpp::Shdr<64, true>& shdr, off_t*); |
1040 | ||
1041 | ||
1042 | } // End namespace gold. |