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