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