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