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