Commit | Line | Data |
---|---|---|
a2fb1b05 ILT |
1 | // layout.cc -- lay out output file sections for gold |
2 | ||
3 | #include "gold.h" | |
4 | ||
a2fb1b05 | 5 | #include <cstring> |
54dc6425 | 6 | #include <algorithm> |
a2fb1b05 ILT |
7 | #include <iostream> |
8 | #include <utility> | |
9 | ||
10 | #include "output.h" | |
f6ce93d6 | 11 | #include "symtab.h" |
a3ad94ed | 12 | #include "dynobj.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) |
a3ad94ed | 42 | : options_(options), namepool_(), sympool_(), dynpool_(), signatures_(), |
61ba1cf9 | 43 | section_name_map_(), segment_list_(), section_list_(), |
a3ad94ed | 44 | unattached_section_list_(), special_output_list_(), |
14b31740 ILT |
45 | tls_segment_(NULL), symtab_section_(NULL), |
46 | dynsym_section_(NULL), dynamic_section_(NULL), dynamic_data_(NULL) | |
54dc6425 ILT |
47 | { |
48 | // Make space for more than enough segments for a typical file. | |
49 | // This is just for efficiency--it's OK if we wind up needing more. | |
a3ad94ed ILT |
50 | this->segment_list_.reserve(12); |
51 | ||
52 | // We expect three unattached Output_data objects: the file header, | |
53 | // the segment headers, and the section headers. | |
54 | this->special_output_list_.reserve(3); | |
54dc6425 ILT |
55 | } |
56 | ||
a2fb1b05 ILT |
57 | // Hash a key we use to look up an output section mapping. |
58 | ||
59 | size_t | |
60 | Layout::Hash_key::operator()(const Layout::Key& k) const | |
61 | { | |
f0641a0b | 62 | return k.first + k.second.first + k.second.second; |
a2fb1b05 ILT |
63 | } |
64 | ||
65 | // Whether to include this section in the link. | |
66 | ||
67 | template<int size, bool big_endian> | |
68 | bool | |
69 | Layout::include_section(Object*, const char*, | |
70 | const elfcpp::Shdr<size, big_endian>& shdr) | |
71 | { | |
72 | // Some section types are never linked. Some are only linked when | |
73 | // doing a relocateable link. | |
74 | switch (shdr.get_sh_type()) | |
75 | { | |
76 | case elfcpp::SHT_NULL: | |
77 | case elfcpp::SHT_SYMTAB: | |
78 | case elfcpp::SHT_DYNSYM: | |
79 | case elfcpp::SHT_STRTAB: | |
80 | case elfcpp::SHT_HASH: | |
81 | case elfcpp::SHT_DYNAMIC: | |
82 | case elfcpp::SHT_SYMTAB_SHNDX: | |
83 | return false; | |
84 | ||
85 | case elfcpp::SHT_RELA: | |
86 | case elfcpp::SHT_REL: | |
87 | case elfcpp::SHT_GROUP: | |
88 | return this->options_.is_relocatable(); | |
89 | ||
90 | default: | |
91 | // FIXME: Handle stripping debug sections here. | |
92 | return true; | |
93 | } | |
94 | } | |
95 | ||
ead1e424 | 96 | // Return an output section named NAME, or NULL if there is none. |
a2fb1b05 | 97 | |
a2fb1b05 | 98 | Output_section* |
ead1e424 | 99 | Layout::find_output_section(const char* name) const |
a2fb1b05 | 100 | { |
ead1e424 ILT |
101 | for (Section_name_map::const_iterator p = this->section_name_map_.begin(); |
102 | p != this->section_name_map_.end(); | |
103 | ++p) | |
f0641a0b | 104 | if (strcmp(p->second->name(), name) == 0) |
ead1e424 ILT |
105 | return p->second; |
106 | return NULL; | |
107 | } | |
a2fb1b05 | 108 | |
ead1e424 ILT |
109 | // Return an output segment of type TYPE, with segment flags SET set |
110 | // and segment flags CLEAR clear. Return NULL if there is none. | |
a2fb1b05 | 111 | |
ead1e424 ILT |
112 | Output_segment* |
113 | Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set, | |
114 | elfcpp::Elf_Word clear) const | |
115 | { | |
116 | for (Segment_list::const_iterator p = this->segment_list_.begin(); | |
117 | p != this->segment_list_.end(); | |
118 | ++p) | |
119 | if (static_cast<elfcpp::PT>((*p)->type()) == type | |
120 | && ((*p)->flags() & set) == set | |
121 | && ((*p)->flags() & clear) == 0) | |
122 | return *p; | |
123 | return NULL; | |
124 | } | |
a2fb1b05 | 125 | |
ead1e424 ILT |
126 | // Return the output section to use for section NAME with type TYPE |
127 | // and section flags FLAGS. | |
a2fb1b05 | 128 | |
ead1e424 | 129 | Output_section* |
f0641a0b ILT |
130 | Layout::get_output_section(const char* name, Stringpool::Key name_key, |
131 | elfcpp::Elf_Word type, elfcpp::Elf_Xword flags) | |
ead1e424 ILT |
132 | { |
133 | // We should ignore some flags. | |
134 | flags &= ~ (elfcpp::SHF_INFO_LINK | |
135 | | elfcpp::SHF_LINK_ORDER | |
b8e6aad9 ILT |
136 | | elfcpp::SHF_GROUP |
137 | | elfcpp::SHF_MERGE | |
138 | | elfcpp::SHF_STRINGS); | |
a2fb1b05 | 139 | |
f0641a0b | 140 | const Key key(name_key, std::make_pair(type, flags)); |
a2fb1b05 ILT |
141 | const std::pair<Key, Output_section*> v(key, NULL); |
142 | std::pair<Section_name_map::iterator, bool> ins( | |
143 | this->section_name_map_.insert(v)); | |
144 | ||
a2fb1b05 | 145 | if (!ins.second) |
ead1e424 | 146 | return ins.first->second; |
a2fb1b05 ILT |
147 | else |
148 | { | |
149 | // This is the first time we've seen this name/type/flags | |
150 | // combination. | |
ead1e424 | 151 | Output_section* os = this->make_output_section(name, type, flags); |
a2fb1b05 | 152 | ins.first->second = os; |
ead1e424 | 153 | return os; |
a2fb1b05 | 154 | } |
ead1e424 ILT |
155 | } |
156 | ||
157 | // Return the output section to use for input section SHNDX, with name | |
158 | // NAME, with header HEADER, from object OBJECT. Set *OFF to the | |
159 | // offset of this input section without the output section. | |
160 | ||
161 | template<int size, bool big_endian> | |
162 | Output_section* | |
f6ce93d6 | 163 | Layout::layout(Relobj* object, unsigned int shndx, const char* name, |
ead1e424 ILT |
164 | const elfcpp::Shdr<size, big_endian>& shdr, off_t* off) |
165 | { | |
166 | if (!this->include_section(object, name, shdr)) | |
167 | return NULL; | |
168 | ||
169 | // If we are not doing a relocateable link, choose the name to use | |
170 | // for the output section. | |
171 | size_t len = strlen(name); | |
172 | if (!this->options_.is_relocatable()) | |
173 | name = Layout::output_section_name(name, &len); | |
174 | ||
175 | // FIXME: Handle SHF_OS_NONCONFORMING here. | |
176 | ||
177 | // Canonicalize the section name. | |
f0641a0b ILT |
178 | Stringpool::Key name_key; |
179 | name = this->namepool_.add(name, len, &name_key); | |
ead1e424 ILT |
180 | |
181 | // Find the output section. The output section is selected based on | |
182 | // the section name, type, and flags. | |
f0641a0b ILT |
183 | Output_section* os = this->get_output_section(name, name_key, |
184 | shdr.get_sh_type(), | |
ead1e424 | 185 | shdr.get_sh_flags()); |
a2fb1b05 ILT |
186 | |
187 | // FIXME: Handle SHF_LINK_ORDER somewhere. | |
188 | ||
ead1e424 | 189 | *off = os->add_input_section(object, shndx, name, shdr); |
a2fb1b05 ILT |
190 | |
191 | return os; | |
192 | } | |
193 | ||
ead1e424 ILT |
194 | // Add POSD to an output section using NAME, TYPE, and FLAGS. |
195 | ||
196 | void | |
197 | Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type, | |
198 | elfcpp::Elf_Xword flags, | |
199 | Output_section_data* posd) | |
200 | { | |
201 | // Canonicalize the name. | |
f0641a0b ILT |
202 | Stringpool::Key name_key; |
203 | name = this->namepool_.add(name, &name_key); | |
ead1e424 | 204 | |
f0641a0b | 205 | Output_section* os = this->get_output_section(name, name_key, type, flags); |
ead1e424 ILT |
206 | os->add_output_section_data(posd); |
207 | } | |
208 | ||
a2fb1b05 ILT |
209 | // Map section flags to segment flags. |
210 | ||
211 | elfcpp::Elf_Word | |
212 | Layout::section_flags_to_segment(elfcpp::Elf_Xword flags) | |
213 | { | |
214 | elfcpp::Elf_Word ret = elfcpp::PF_R; | |
215 | if ((flags & elfcpp::SHF_WRITE) != 0) | |
216 | ret |= elfcpp::PF_W; | |
217 | if ((flags & elfcpp::SHF_EXECINSTR) != 0) | |
218 | ret |= elfcpp::PF_X; | |
219 | return ret; | |
220 | } | |
221 | ||
222 | // Make a new Output_section, and attach it to segments as | |
223 | // appropriate. | |
224 | ||
225 | Output_section* | |
226 | Layout::make_output_section(const char* name, elfcpp::Elf_Word type, | |
227 | elfcpp::Elf_Xword flags) | |
228 | { | |
b8e6aad9 | 229 | Output_section* os = new Output_section(name, type, flags); |
a3ad94ed | 230 | this->section_list_.push_back(os); |
a2fb1b05 ILT |
231 | |
232 | if ((flags & elfcpp::SHF_ALLOC) == 0) | |
a3ad94ed | 233 | this->unattached_section_list_.push_back(os); |
a2fb1b05 ILT |
234 | else |
235 | { | |
236 | // This output section goes into a PT_LOAD segment. | |
237 | ||
238 | elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags); | |
239 | ||
240 | // The only thing we really care about for PT_LOAD segments is | |
241 | // whether or not they are writable, so that is how we search | |
242 | // for them. People who need segments sorted on some other | |
243 | // basis will have to wait until we implement a mechanism for | |
244 | // them to describe the segments they want. | |
245 | ||
246 | Segment_list::const_iterator p; | |
247 | for (p = this->segment_list_.begin(); | |
248 | p != this->segment_list_.end(); | |
249 | ++p) | |
250 | { | |
251 | if ((*p)->type() == elfcpp::PT_LOAD | |
252 | && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W)) | |
253 | { | |
75f65a3e | 254 | (*p)->add_output_section(os, seg_flags); |
a2fb1b05 ILT |
255 | break; |
256 | } | |
257 | } | |
258 | ||
259 | if (p == this->segment_list_.end()) | |
260 | { | |
261 | Output_segment* oseg = new Output_segment(elfcpp::PT_LOAD, | |
262 | seg_flags); | |
263 | this->segment_list_.push_back(oseg); | |
75f65a3e | 264 | oseg->add_output_section(os, seg_flags); |
a2fb1b05 ILT |
265 | } |
266 | ||
267 | // If we see a loadable SHT_NOTE section, we create a PT_NOTE | |
268 | // segment. | |
269 | if (type == elfcpp::SHT_NOTE) | |
270 | { | |
271 | // See if we already have an equivalent PT_NOTE segment. | |
272 | for (p = this->segment_list_.begin(); | |
273 | p != segment_list_.end(); | |
274 | ++p) | |
275 | { | |
276 | if ((*p)->type() == elfcpp::PT_NOTE | |
277 | && (((*p)->flags() & elfcpp::PF_W) | |
278 | == (seg_flags & elfcpp::PF_W))) | |
279 | { | |
75f65a3e | 280 | (*p)->add_output_section(os, seg_flags); |
a2fb1b05 ILT |
281 | break; |
282 | } | |
283 | } | |
284 | ||
285 | if (p == this->segment_list_.end()) | |
286 | { | |
287 | Output_segment* oseg = new Output_segment(elfcpp::PT_NOTE, | |
288 | seg_flags); | |
289 | this->segment_list_.push_back(oseg); | |
75f65a3e | 290 | oseg->add_output_section(os, seg_flags); |
a2fb1b05 ILT |
291 | } |
292 | } | |
54dc6425 ILT |
293 | |
294 | // If we see a loadable SHF_TLS section, we create a PT_TLS | |
92e059d8 | 295 | // segment. There can only be one such segment. |
54dc6425 ILT |
296 | if ((flags & elfcpp::SHF_TLS) != 0) |
297 | { | |
92e059d8 | 298 | if (this->tls_segment_ == NULL) |
54dc6425 | 299 | { |
92e059d8 ILT |
300 | this->tls_segment_ = new Output_segment(elfcpp::PT_TLS, |
301 | seg_flags); | |
302 | this->segment_list_.push_back(this->tls_segment_); | |
54dc6425 | 303 | } |
92e059d8 | 304 | this->tls_segment_->add_output_section(os, seg_flags); |
54dc6425 | 305 | } |
a2fb1b05 ILT |
306 | } |
307 | ||
308 | return os; | |
309 | } | |
310 | ||
a3ad94ed ILT |
311 | // Create the dynamic sections which are needed before we read the |
312 | // relocs. | |
313 | ||
314 | void | |
315 | Layout::create_initial_dynamic_sections(const Input_objects* input_objects, | |
316 | Symbol_table* symtab) | |
317 | { | |
318 | if (!input_objects->any_dynamic()) | |
319 | return; | |
320 | ||
321 | const char* dynamic_name = this->namepool_.add(".dynamic", NULL); | |
322 | this->dynamic_section_ = this->make_output_section(dynamic_name, | |
323 | elfcpp::SHT_DYNAMIC, | |
324 | (elfcpp::SHF_ALLOC | |
325 | | elfcpp::SHF_WRITE)); | |
326 | ||
14b31740 | 327 | symtab->define_in_output_data(input_objects->target(), "_DYNAMIC", NULL, |
a3ad94ed ILT |
328 | this->dynamic_section_, 0, 0, |
329 | elfcpp::STT_OBJECT, elfcpp::STB_LOCAL, | |
330 | elfcpp::STV_HIDDEN, 0, false, false); | |
16649710 ILT |
331 | |
332 | this->dynamic_data_ = new Output_data_dynamic(input_objects->target(), | |
333 | &this->dynpool_); | |
334 | ||
335 | this->dynamic_section_->add_output_section_data(this->dynamic_data_); | |
a3ad94ed ILT |
336 | } |
337 | ||
75f65a3e ILT |
338 | // Find the first read-only PT_LOAD segment, creating one if |
339 | // necessary. | |
54dc6425 | 340 | |
75f65a3e ILT |
341 | Output_segment* |
342 | Layout::find_first_load_seg() | |
54dc6425 | 343 | { |
75f65a3e ILT |
344 | for (Segment_list::const_iterator p = this->segment_list_.begin(); |
345 | p != this->segment_list_.end(); | |
346 | ++p) | |
347 | { | |
348 | if ((*p)->type() == elfcpp::PT_LOAD | |
349 | && ((*p)->flags() & elfcpp::PF_R) != 0 | |
350 | && ((*p)->flags() & elfcpp::PF_W) == 0) | |
351 | return *p; | |
352 | } | |
353 | ||
354 | Output_segment* load_seg = new Output_segment(elfcpp::PT_LOAD, elfcpp::PF_R); | |
355 | this->segment_list_.push_back(load_seg); | |
356 | return load_seg; | |
54dc6425 ILT |
357 | } |
358 | ||
359 | // Finalize the layout. When this is called, we have created all the | |
360 | // output sections and all the output segments which are based on | |
361 | // input sections. We have several things to do, and we have to do | |
362 | // them in the right order, so that we get the right results correctly | |
363 | // and efficiently. | |
364 | ||
365 | // 1) Finalize the list of output segments and create the segment | |
366 | // table header. | |
367 | ||
368 | // 2) Finalize the dynamic symbol table and associated sections. | |
369 | ||
370 | // 3) Determine the final file offset of all the output segments. | |
371 | ||
372 | // 4) Determine the final file offset of all the SHF_ALLOC output | |
373 | // sections. | |
374 | ||
75f65a3e ILT |
375 | // 5) Create the symbol table sections and the section name table |
376 | // section. | |
377 | ||
378 | // 6) Finalize the symbol table: set symbol values to their final | |
54dc6425 ILT |
379 | // value and make a final determination of which symbols are going |
380 | // into the output symbol table. | |
381 | ||
54dc6425 ILT |
382 | // 7) Create the section table header. |
383 | ||
384 | // 8) Determine the final file offset of all the output sections which | |
385 | // are not SHF_ALLOC, including the section table header. | |
386 | ||
387 | // 9) Finalize the ELF file header. | |
388 | ||
75f65a3e ILT |
389 | // This function returns the size of the output file. |
390 | ||
391 | off_t | |
392 | Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab) | |
54dc6425 | 393 | { |
5a6f7e2d | 394 | Target* const target = input_objects->target(); |
a3ad94ed | 395 | const int size = target->get_size(); |
dbe717ef | 396 | |
16649710 | 397 | target->finalize_sections(&this->options_, this); |
5a6f7e2d | 398 | |
dbe717ef | 399 | Output_segment* phdr_seg = NULL; |
54dc6425 ILT |
400 | if (input_objects->any_dynamic()) |
401 | { | |
dbe717ef ILT |
402 | // There was a dynamic object in the link. We need to create |
403 | // some information for the dynamic linker. | |
404 | ||
405 | // Create the PT_PHDR segment which will hold the program | |
406 | // headers. | |
407 | phdr_seg = new Output_segment(elfcpp::PT_PHDR, elfcpp::PF_R); | |
408 | this->segment_list_.push_back(phdr_seg); | |
409 | ||
14b31740 ILT |
410 | // Create the dynamic symbol table, including the hash table. |
411 | Output_section* dynstr; | |
412 | std::vector<Symbol*> dynamic_symbols; | |
413 | unsigned int local_dynamic_count; | |
414 | Versions versions; | |
415 | this->create_dynamic_symtab(target, symtab, &dynstr, | |
416 | &local_dynamic_count, &dynamic_symbols, | |
417 | &versions); | |
dbe717ef ILT |
418 | |
419 | // Create the .interp section to hold the name of the | |
420 | // interpreter, and put it in a PT_INTERP segment. | |
a3ad94ed ILT |
421 | this->create_interp(target); |
422 | ||
423 | // Finish the .dynamic section to hold the dynamic data, and put | |
424 | // it in a PT_DYNAMIC segment. | |
16649710 | 425 | this->finish_dynamic_section(input_objects, symtab); |
14b31740 ILT |
426 | |
427 | // We should have added everything we need to the dynamic string | |
428 | // table. | |
429 | this->dynpool_.set_string_offsets(); | |
430 | ||
431 | // Create the version sections. We can't do this until the | |
432 | // dynamic string table is complete. | |
433 | this->create_version_sections(target, &versions, local_dynamic_count, | |
434 | dynamic_symbols, dynstr); | |
54dc6425 ILT |
435 | } |
436 | ||
437 | // FIXME: Handle PT_GNU_STACK. | |
438 | ||
75f65a3e ILT |
439 | Output_segment* load_seg = this->find_first_load_seg(); |
440 | ||
441 | // Lay out the segment headers. | |
a3ad94ed | 442 | bool big_endian = target->is_big_endian(); |
75f65a3e | 443 | Output_segment_headers* segment_headers; |
61ba1cf9 ILT |
444 | segment_headers = new Output_segment_headers(size, big_endian, |
445 | this->segment_list_); | |
75f65a3e | 446 | load_seg->add_initial_output_data(segment_headers); |
61ba1cf9 | 447 | this->special_output_list_.push_back(segment_headers); |
dbe717ef ILT |
448 | if (phdr_seg != NULL) |
449 | phdr_seg->add_initial_output_data(segment_headers); | |
75f65a3e ILT |
450 | |
451 | // Lay out the file header. | |
452 | Output_file_header* file_header; | |
453 | file_header = new Output_file_header(size, | |
61ba1cf9 | 454 | big_endian, |
75f65a3e | 455 | this->options_, |
a3ad94ed | 456 | target, |
75f65a3e ILT |
457 | symtab, |
458 | segment_headers); | |
459 | load_seg->add_initial_output_data(file_header); | |
61ba1cf9 | 460 | this->special_output_list_.push_back(file_header); |
75f65a3e | 461 | |
ead1e424 ILT |
462 | // We set the output section indexes in set_segment_offsets and |
463 | // set_section_offsets. | |
464 | unsigned int shndx = 1; | |
465 | ||
466 | // Set the file offsets of all the segments, and all the sections | |
467 | // they contain. | |
a3ad94ed | 468 | off_t off = this->set_segment_offsets(target, load_seg, &shndx); |
75f65a3e ILT |
469 | |
470 | // Create the symbol table sections. | |
16649710 | 471 | this->create_symtab_sections(size, input_objects, symtab, &off); |
75f65a3e ILT |
472 | |
473 | // Create the .shstrtab section. | |
474 | Output_section* shstrtab_section = this->create_shstrtab(); | |
475 | ||
476 | // Set the file offsets of all the sections not associated with | |
477 | // segments. | |
ead1e424 ILT |
478 | off = this->set_section_offsets(off, &shndx); |
479 | ||
75f65a3e | 480 | // Create the section table header. |
61ba1cf9 | 481 | Output_section_headers* oshdrs = this->create_shdrs(size, big_endian, &off); |
75f65a3e ILT |
482 | |
483 | file_header->set_section_info(oshdrs, shstrtab_section); | |
484 | ||
485 | // Now we know exactly where everything goes in the output file. | |
a3ad94ed | 486 | Output_data::layout_complete(); |
75f65a3e ILT |
487 | |
488 | return off; | |
489 | } | |
490 | ||
491 | // Return whether SEG1 should be before SEG2 in the output file. This | |
492 | // is based entirely on the segment type and flags. When this is | |
493 | // called the segment addresses has normally not yet been set. | |
494 | ||
495 | bool | |
496 | Layout::segment_precedes(const Output_segment* seg1, | |
497 | const Output_segment* seg2) | |
498 | { | |
499 | elfcpp::Elf_Word type1 = seg1->type(); | |
500 | elfcpp::Elf_Word type2 = seg2->type(); | |
501 | ||
502 | // The single PT_PHDR segment is required to precede any loadable | |
503 | // segment. We simply make it always first. | |
504 | if (type1 == elfcpp::PT_PHDR) | |
505 | { | |
a3ad94ed | 506 | gold_assert(type2 != elfcpp::PT_PHDR); |
75f65a3e ILT |
507 | return true; |
508 | } | |
509 | if (type2 == elfcpp::PT_PHDR) | |
510 | return false; | |
511 | ||
512 | // The single PT_INTERP segment is required to precede any loadable | |
513 | // segment. We simply make it always second. | |
514 | if (type1 == elfcpp::PT_INTERP) | |
515 | { | |
a3ad94ed | 516 | gold_assert(type2 != elfcpp::PT_INTERP); |
75f65a3e ILT |
517 | return true; |
518 | } | |
519 | if (type2 == elfcpp::PT_INTERP) | |
520 | return false; | |
521 | ||
522 | // We then put PT_LOAD segments before any other segments. | |
523 | if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD) | |
524 | return true; | |
525 | if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD) | |
526 | return false; | |
527 | ||
92e059d8 ILT |
528 | // We put the PT_TLS segment last, because that is where the dynamic |
529 | // linker expects to find it (this is just for efficiency; other | |
530 | // positions would also work correctly). | |
531 | if (type1 == elfcpp::PT_TLS && type2 != elfcpp::PT_TLS) | |
532 | return false; | |
533 | if (type2 == elfcpp::PT_TLS && type1 != elfcpp::PT_TLS) | |
534 | return true; | |
535 | ||
75f65a3e ILT |
536 | const elfcpp::Elf_Word flags1 = seg1->flags(); |
537 | const elfcpp::Elf_Word flags2 = seg2->flags(); | |
538 | ||
539 | // The order of non-PT_LOAD segments is unimportant. We simply sort | |
540 | // by the numeric segment type and flags values. There should not | |
541 | // be more than one segment with the same type and flags. | |
542 | if (type1 != elfcpp::PT_LOAD) | |
543 | { | |
544 | if (type1 != type2) | |
545 | return type1 < type2; | |
a3ad94ed | 546 | gold_assert(flags1 != flags2); |
75f65a3e ILT |
547 | return flags1 < flags2; |
548 | } | |
549 | ||
550 | // We sort PT_LOAD segments based on the flags. Readonly segments | |
551 | // come before writable segments. Then executable segments come | |
552 | // before non-executable segments. Then the unlikely case of a | |
553 | // non-readable segment comes before the normal case of a readable | |
554 | // segment. If there are multiple segments with the same type and | |
555 | // flags, we require that the address be set, and we sort by | |
556 | // virtual address and then physical address. | |
557 | if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W)) | |
558 | return (flags1 & elfcpp::PF_W) == 0; | |
559 | if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X)) | |
560 | return (flags1 & elfcpp::PF_X) != 0; | |
561 | if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R)) | |
562 | return (flags1 & elfcpp::PF_R) == 0; | |
563 | ||
564 | uint64_t vaddr1 = seg1->vaddr(); | |
565 | uint64_t vaddr2 = seg2->vaddr(); | |
566 | if (vaddr1 != vaddr2) | |
567 | return vaddr1 < vaddr2; | |
568 | ||
569 | uint64_t paddr1 = seg1->paddr(); | |
570 | uint64_t paddr2 = seg2->paddr(); | |
a3ad94ed | 571 | gold_assert(paddr1 != paddr2); |
75f65a3e ILT |
572 | return paddr1 < paddr2; |
573 | } | |
574 | ||
ead1e424 ILT |
575 | // Set the file offsets of all the segments, and all the sections they |
576 | // contain. They have all been created. LOAD_SEG must be be laid out | |
577 | // first. Return the offset of the data to follow. | |
75f65a3e ILT |
578 | |
579 | off_t | |
ead1e424 ILT |
580 | Layout::set_segment_offsets(const Target* target, Output_segment* load_seg, |
581 | unsigned int *pshndx) | |
75f65a3e ILT |
582 | { |
583 | // Sort them into the final order. | |
54dc6425 ILT |
584 | std::sort(this->segment_list_.begin(), this->segment_list_.end(), |
585 | Layout::Compare_segments()); | |
586 | ||
75f65a3e ILT |
587 | // Find the PT_LOAD segments, and set their addresses and offsets |
588 | // and their section's addresses and offsets. | |
589 | uint64_t addr = target->text_segment_address(); | |
590 | off_t off = 0; | |
591 | bool was_readonly = false; | |
592 | for (Segment_list::iterator p = this->segment_list_.begin(); | |
593 | p != this->segment_list_.end(); | |
594 | ++p) | |
595 | { | |
596 | if ((*p)->type() == elfcpp::PT_LOAD) | |
597 | { | |
598 | if (load_seg != NULL && load_seg != *p) | |
a3ad94ed | 599 | gold_unreachable(); |
75f65a3e ILT |
600 | load_seg = NULL; |
601 | ||
602 | // If the last segment was readonly, and this one is not, | |
603 | // then skip the address forward one page, maintaining the | |
604 | // same position within the page. This lets us store both | |
605 | // segments overlapping on a single page in the file, but | |
606 | // the loader will put them on different pages in memory. | |
607 | ||
608 | uint64_t orig_addr = addr; | |
609 | uint64_t orig_off = off; | |
610 | ||
611 | uint64_t aligned_addr = addr; | |
612 | uint64_t abi_pagesize = target->abi_pagesize(); | |
0496d5e5 ILT |
613 | |
614 | // FIXME: This should depend on the -n and -N options. | |
615 | (*p)->set_minimum_addralign(target->common_pagesize()); | |
616 | ||
75f65a3e ILT |
617 | if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0) |
618 | { | |
ead1e424 | 619 | uint64_t align = (*p)->addralign(); |
75f65a3e | 620 | |
ead1e424 | 621 | addr = align_address(addr, align); |
75f65a3e ILT |
622 | aligned_addr = addr; |
623 | if ((addr & (abi_pagesize - 1)) != 0) | |
624 | addr = addr + abi_pagesize; | |
625 | } | |
626 | ||
ead1e424 | 627 | unsigned int shndx_hold = *pshndx; |
75f65a3e | 628 | off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1)); |
ead1e424 | 629 | uint64_t new_addr = (*p)->set_section_addresses(addr, &off, pshndx); |
75f65a3e ILT |
630 | |
631 | // Now that we know the size of this segment, we may be able | |
632 | // to save a page in memory, at the cost of wasting some | |
633 | // file space, by instead aligning to the start of a new | |
634 | // page. Here we use the real machine page size rather than | |
635 | // the ABI mandated page size. | |
636 | ||
637 | if (aligned_addr != addr) | |
638 | { | |
639 | uint64_t common_pagesize = target->common_pagesize(); | |
640 | uint64_t first_off = (common_pagesize | |
641 | - (aligned_addr | |
642 | & (common_pagesize - 1))); | |
643 | uint64_t last_off = new_addr & (common_pagesize - 1); | |
644 | if (first_off > 0 | |
645 | && last_off > 0 | |
646 | && ((aligned_addr & ~ (common_pagesize - 1)) | |
647 | != (new_addr & ~ (common_pagesize - 1))) | |
648 | && first_off + last_off <= common_pagesize) | |
649 | { | |
ead1e424 ILT |
650 | *pshndx = shndx_hold; |
651 | addr = align_address(aligned_addr, common_pagesize); | |
75f65a3e | 652 | off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1)); |
ead1e424 | 653 | new_addr = (*p)->set_section_addresses(addr, &off, pshndx); |
75f65a3e ILT |
654 | } |
655 | } | |
656 | ||
657 | addr = new_addr; | |
658 | ||
659 | if (((*p)->flags() & elfcpp::PF_W) == 0) | |
660 | was_readonly = true; | |
661 | } | |
662 | } | |
663 | ||
664 | // Handle the non-PT_LOAD segments, setting their offsets from their | |
665 | // section's offsets. | |
666 | for (Segment_list::iterator p = this->segment_list_.begin(); | |
667 | p != this->segment_list_.end(); | |
668 | ++p) | |
669 | { | |
670 | if ((*p)->type() != elfcpp::PT_LOAD) | |
671 | (*p)->set_offset(); | |
672 | } | |
673 | ||
674 | return off; | |
675 | } | |
676 | ||
677 | // Set the file offset of all the sections not associated with a | |
678 | // segment. | |
679 | ||
680 | off_t | |
ead1e424 | 681 | Layout::set_section_offsets(off_t off, unsigned int* pshndx) |
75f65a3e | 682 | { |
a3ad94ed ILT |
683 | for (Section_list::iterator p = this->unattached_section_list_.begin(); |
684 | p != this->unattached_section_list_.end(); | |
75f65a3e ILT |
685 | ++p) |
686 | { | |
ead1e424 ILT |
687 | (*p)->set_out_shndx(*pshndx); |
688 | ++*pshndx; | |
61ba1cf9 ILT |
689 | if ((*p)->offset() != -1) |
690 | continue; | |
ead1e424 | 691 | off = align_address(off, (*p)->addralign()); |
75f65a3e ILT |
692 | (*p)->set_address(0, off); |
693 | off += (*p)->data_size(); | |
694 | } | |
695 | return off; | |
696 | } | |
697 | ||
b8e6aad9 ILT |
698 | // Create the symbol table sections. Here we also set the final |
699 | // values of the symbols. At this point all the loadable sections are | |
700 | // fully laid out. | |
75f65a3e ILT |
701 | |
702 | void | |
61ba1cf9 | 703 | Layout::create_symtab_sections(int size, const Input_objects* input_objects, |
75f65a3e | 704 | Symbol_table* symtab, |
16649710 | 705 | off_t* poff) |
75f65a3e | 706 | { |
61ba1cf9 ILT |
707 | int symsize; |
708 | unsigned int align; | |
709 | if (size == 32) | |
710 | { | |
711 | symsize = elfcpp::Elf_sizes<32>::sym_size; | |
712 | align = 4; | |
713 | } | |
714 | else if (size == 64) | |
715 | { | |
716 | symsize = elfcpp::Elf_sizes<64>::sym_size; | |
717 | align = 8; | |
718 | } | |
719 | else | |
a3ad94ed | 720 | gold_unreachable(); |
61ba1cf9 ILT |
721 | |
722 | off_t off = *poff; | |
ead1e424 | 723 | off = align_address(off, align); |
61ba1cf9 ILT |
724 | off_t startoff = off; |
725 | ||
726 | // Save space for the dummy symbol at the start of the section. We | |
727 | // never bother to write this out--it will just be left as zero. | |
728 | off += symsize; | |
c06b7b0b | 729 | unsigned int local_symbol_index = 1; |
61ba1cf9 | 730 | |
a3ad94ed ILT |
731 | // Add STT_SECTION symbols for each Output section which needs one. |
732 | for (Section_list::iterator p = this->section_list_.begin(); | |
733 | p != this->section_list_.end(); | |
734 | ++p) | |
735 | { | |
736 | if (!(*p)->needs_symtab_index()) | |
737 | (*p)->set_symtab_index(-1U); | |
738 | else | |
739 | { | |
740 | (*p)->set_symtab_index(local_symbol_index); | |
741 | ++local_symbol_index; | |
742 | off += symsize; | |
743 | } | |
744 | } | |
745 | ||
f6ce93d6 ILT |
746 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); |
747 | p != input_objects->relobj_end(); | |
75f65a3e ILT |
748 | ++p) |
749 | { | |
750 | Task_lock_obj<Object> tlo(**p); | |
c06b7b0b ILT |
751 | unsigned int index = (*p)->finalize_local_symbols(local_symbol_index, |
752 | off, | |
753 | &this->sympool_); | |
754 | off += (index - local_symbol_index) * symsize; | |
755 | local_symbol_index = index; | |
75f65a3e ILT |
756 | } |
757 | ||
c06b7b0b | 758 | unsigned int local_symcount = local_symbol_index; |
a3ad94ed | 759 | gold_assert(local_symcount * symsize == off - startoff); |
61ba1cf9 | 760 | |
16649710 ILT |
761 | off_t dynoff; |
762 | size_t dyn_global_index; | |
763 | size_t dyncount; | |
764 | if (this->dynsym_section_ == NULL) | |
765 | { | |
766 | dynoff = 0; | |
767 | dyn_global_index = 0; | |
768 | dyncount = 0; | |
769 | } | |
770 | else | |
771 | { | |
772 | dyn_global_index = this->dynsym_section_->info(); | |
773 | off_t locsize = dyn_global_index * this->dynsym_section_->entsize(); | |
774 | dynoff = this->dynsym_section_->offset() + locsize; | |
775 | dyncount = (this->dynsym_section_->data_size() - locsize) / symsize; | |
776 | gold_assert(dyncount * symsize | |
777 | == this->dynsym_section_->data_size() - locsize); | |
778 | } | |
779 | ||
780 | off = symtab->finalize(local_symcount, off, dynoff, dyn_global_index, | |
781 | dyncount, &this->sympool_); | |
75f65a3e | 782 | |
61ba1cf9 ILT |
783 | this->sympool_.set_string_offsets(); |
784 | ||
f0641a0b | 785 | const char* symtab_name = this->namepool_.add(".symtab", NULL); |
a3ad94ed ILT |
786 | Output_section* osymtab = this->make_output_section(symtab_name, |
787 | elfcpp::SHT_SYMTAB, | |
788 | 0); | |
789 | this->symtab_section_ = osymtab; | |
790 | ||
791 | Output_section_data* pos = new Output_data_space(off - startoff, | |
792 | align); | |
793 | osymtab->add_output_section_data(pos); | |
61ba1cf9 | 794 | |
f0641a0b | 795 | const char* strtab_name = this->namepool_.add(".strtab", NULL); |
a3ad94ed ILT |
796 | Output_section* ostrtab = this->make_output_section(strtab_name, |
797 | elfcpp::SHT_STRTAB, | |
798 | 0); | |
799 | ||
800 | Output_section_data* pstr = new Output_data_strtab(&this->sympool_); | |
801 | ostrtab->add_output_section_data(pstr); | |
61ba1cf9 ILT |
802 | |
803 | osymtab->set_address(0, startoff); | |
16649710 | 804 | osymtab->set_link_section(ostrtab); |
61ba1cf9 ILT |
805 | osymtab->set_info(local_symcount); |
806 | osymtab->set_entsize(symsize); | |
61ba1cf9 ILT |
807 | |
808 | *poff = off; | |
75f65a3e ILT |
809 | } |
810 | ||
811 | // Create the .shstrtab section, which holds the names of the | |
812 | // sections. At the time this is called, we have created all the | |
813 | // output sections except .shstrtab itself. | |
814 | ||
815 | Output_section* | |
816 | Layout::create_shstrtab() | |
817 | { | |
818 | // FIXME: We don't need to create a .shstrtab section if we are | |
819 | // stripping everything. | |
820 | ||
f0641a0b | 821 | const char* name = this->namepool_.add(".shstrtab", NULL); |
75f65a3e | 822 | |
61ba1cf9 ILT |
823 | this->namepool_.set_string_offsets(); |
824 | ||
a3ad94ed | 825 | Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0); |
75f65a3e | 826 | |
a3ad94ed ILT |
827 | Output_section_data* posd = new Output_data_strtab(&this->namepool_); |
828 | os->add_output_section_data(posd); | |
75f65a3e ILT |
829 | |
830 | return os; | |
831 | } | |
832 | ||
833 | // Create the section headers. SIZE is 32 or 64. OFF is the file | |
834 | // offset. | |
835 | ||
836 | Output_section_headers* | |
61ba1cf9 | 837 | Layout::create_shdrs(int size, bool big_endian, off_t* poff) |
75f65a3e ILT |
838 | { |
839 | Output_section_headers* oshdrs; | |
16649710 ILT |
840 | oshdrs = new Output_section_headers(size, big_endian, this, |
841 | &this->segment_list_, | |
842 | &this->unattached_section_list_, | |
61ba1cf9 | 843 | &this->namepool_); |
ead1e424 | 844 | off_t off = align_address(*poff, oshdrs->addralign()); |
75f65a3e | 845 | oshdrs->set_address(0, off); |
61ba1cf9 ILT |
846 | off += oshdrs->data_size(); |
847 | *poff = off; | |
848 | this->special_output_list_.push_back(oshdrs); | |
75f65a3e | 849 | return oshdrs; |
54dc6425 ILT |
850 | } |
851 | ||
dbe717ef ILT |
852 | // Create the dynamic symbol table. |
853 | ||
854 | void | |
14b31740 ILT |
855 | Layout::create_dynamic_symtab(const Target* target, Symbol_table* symtab, |
856 | Output_section **pdynstr, | |
857 | unsigned int* plocal_dynamic_count, | |
858 | std::vector<Symbol*>* pdynamic_symbols, | |
859 | Versions* pversions) | |
dbe717ef | 860 | { |
a3ad94ed ILT |
861 | // Count all the symbols in the dynamic symbol table, and set the |
862 | // dynamic symbol indexes. | |
dbe717ef | 863 | |
a3ad94ed ILT |
864 | // Skip symbol 0, which is always all zeroes. |
865 | unsigned int index = 1; | |
dbe717ef | 866 | |
a3ad94ed ILT |
867 | // Add STT_SECTION symbols for each Output section which needs one. |
868 | for (Section_list::iterator p = this->section_list_.begin(); | |
869 | p != this->section_list_.end(); | |
870 | ++p) | |
871 | { | |
872 | if (!(*p)->needs_dynsym_index()) | |
873 | (*p)->set_dynsym_index(-1U); | |
874 | else | |
875 | { | |
876 | (*p)->set_dynsym_index(index); | |
877 | ++index; | |
878 | } | |
879 | } | |
880 | ||
881 | // FIXME: Some targets apparently require local symbols in the | |
882 | // dynamic symbol table. Here is where we will have to count them, | |
883 | // and set the dynamic symbol indexes, and add the names to | |
884 | // this->dynpool_. | |
885 | ||
886 | unsigned int local_symcount = index; | |
14b31740 | 887 | *plocal_dynamic_count = local_symcount; |
a3ad94ed ILT |
888 | |
889 | // FIXME: We have to tell set_dynsym_indexes whether the | |
890 | // -E/--export-dynamic option was used. | |
14b31740 ILT |
891 | index = symtab->set_dynsym_indexes(&this->options_, target, index, |
892 | pdynamic_symbols, &this->dynpool_, | |
893 | pversions); | |
a3ad94ed ILT |
894 | |
895 | int symsize; | |
896 | unsigned int align; | |
897 | const int size = target->get_size(); | |
898 | if (size == 32) | |
899 | { | |
900 | symsize = elfcpp::Elf_sizes<32>::sym_size; | |
901 | align = 4; | |
902 | } | |
903 | else if (size == 64) | |
904 | { | |
905 | symsize = elfcpp::Elf_sizes<64>::sym_size; | |
906 | align = 8; | |
907 | } | |
908 | else | |
909 | gold_unreachable(); | |
910 | ||
14b31740 ILT |
911 | // Create the dynamic symbol table section. |
912 | ||
a3ad94ed ILT |
913 | const char* dynsym_name = this->namepool_.add(".dynsym", NULL); |
914 | Output_section* dynsym = this->make_output_section(dynsym_name, | |
915 | elfcpp::SHT_DYNSYM, | |
916 | elfcpp::SHF_ALLOC); | |
917 | ||
918 | Output_section_data* odata = new Output_data_space(index * symsize, | |
919 | align); | |
920 | dynsym->add_output_section_data(odata); | |
921 | ||
922 | dynsym->set_info(local_symcount); | |
923 | dynsym->set_entsize(symsize); | |
924 | dynsym->set_addralign(align); | |
925 | ||
926 | this->dynsym_section_ = dynsym; | |
927 | ||
16649710 | 928 | Output_data_dynamic* const odyn = this->dynamic_data_; |
a3ad94ed ILT |
929 | odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym); |
930 | odyn->add_constant(elfcpp::DT_SYMENT, symsize); | |
931 | ||
14b31740 ILT |
932 | // Create the dynamic string table section. |
933 | ||
a3ad94ed ILT |
934 | const char* dynstr_name = this->namepool_.add(".dynstr", NULL); |
935 | Output_section* dynstr = this->make_output_section(dynstr_name, | |
936 | elfcpp::SHT_STRTAB, | |
937 | elfcpp::SHF_ALLOC); | |
938 | ||
939 | Output_section_data* strdata = new Output_data_strtab(&this->dynpool_); | |
940 | dynstr->add_output_section_data(strdata); | |
941 | ||
16649710 ILT |
942 | dynsym->set_link_section(dynstr); |
943 | this->dynamic_section_->set_link_section(dynstr); | |
944 | ||
a3ad94ed ILT |
945 | odyn->add_section_address(elfcpp::DT_STRTAB, dynstr); |
946 | odyn->add_section_size(elfcpp::DT_STRSZ, dynstr); | |
947 | ||
14b31740 ILT |
948 | *pdynstr = dynstr; |
949 | ||
950 | // Create the hash tables. | |
951 | ||
a3ad94ed ILT |
952 | // FIXME: We need an option to create a GNU hash table. |
953 | ||
954 | unsigned char* phash; | |
955 | unsigned int hashlen; | |
14b31740 | 956 | Dynobj::create_elf_hash_table(target, *pdynamic_symbols, local_symcount, |
a3ad94ed ILT |
957 | &phash, &hashlen); |
958 | ||
959 | const char* hash_name = this->namepool_.add(".hash", NULL); | |
960 | Output_section* hashsec = this->make_output_section(hash_name, | |
961 | elfcpp::SHT_HASH, | |
962 | elfcpp::SHF_ALLOC); | |
963 | ||
964 | Output_section_data* hashdata = new Output_data_const_buffer(phash, | |
965 | hashlen, | |
966 | align); | |
967 | hashsec->add_output_section_data(hashdata); | |
968 | ||
16649710 | 969 | hashsec->set_link_section(dynsym); |
a3ad94ed | 970 | hashsec->set_entsize(4); |
a3ad94ed ILT |
971 | |
972 | odyn->add_section_address(elfcpp::DT_HASH, hashsec); | |
dbe717ef ILT |
973 | } |
974 | ||
14b31740 ILT |
975 | // Create the version sections. |
976 | ||
977 | void | |
978 | Layout::create_version_sections(const Target* target, const Versions* versions, | |
979 | unsigned int local_symcount, | |
980 | const std::vector<Symbol*>& dynamic_symbols, | |
981 | const Output_section* dynstr) | |
982 | { | |
983 | if (!versions->any_defs() && !versions->any_needs()) | |
984 | return; | |
985 | ||
986 | if (target->get_size() == 32) | |
987 | { | |
988 | if (target->is_big_endian()) | |
91da9340 ILT |
989 | this->sized_create_version_sections SELECT_SIZE_ENDIAN_NAME(32, true)( |
990 | versions, local_symcount, dynamic_symbols, dynstr | |
991 | SELECT_SIZE_ENDIAN(32, true)); | |
14b31740 | 992 | else |
91da9340 ILT |
993 | this->sized_create_version_sections SELECT_SIZE_ENDIAN_NAME(32, false)( |
994 | versions, local_symcount, dynamic_symbols, dynstr | |
995 | SELECT_SIZE_ENDIAN(32, false)); | |
14b31740 ILT |
996 | } |
997 | else if (target->get_size() == 64) | |
998 | { | |
999 | if (target->is_big_endian()) | |
91da9340 ILT |
1000 | this->sized_create_version_sections SELECT_SIZE_ENDIAN_NAME(64, true)( |
1001 | versions, local_symcount, dynamic_symbols, dynstr | |
1002 | SELECT_SIZE_ENDIAN(64, true)); | |
14b31740 | 1003 | else |
91da9340 ILT |
1004 | this->sized_create_version_sections SELECT_SIZE_ENDIAN_NAME(64, false)( |
1005 | versions, local_symcount, dynamic_symbols, dynstr | |
1006 | SELECT_SIZE_ENDIAN(64, false)); | |
14b31740 ILT |
1007 | } |
1008 | else | |
1009 | gold_unreachable(); | |
1010 | } | |
1011 | ||
1012 | // Create the version sections, sized version. | |
1013 | ||
1014 | template<int size, bool big_endian> | |
1015 | void | |
1016 | Layout::sized_create_version_sections( | |
1017 | const Versions* versions, | |
1018 | unsigned int local_symcount, | |
1019 | const std::vector<Symbol*>& dynamic_symbols, | |
91da9340 ILT |
1020 | const Output_section* dynstr |
1021 | ACCEPT_SIZE_ENDIAN) | |
14b31740 ILT |
1022 | { |
1023 | const char* vname = this->namepool_.add(".gnu.version", NULL); | |
1024 | Output_section* vsec = this->make_output_section(vname, | |
1025 | elfcpp::SHT_GNU_versym, | |
1026 | elfcpp::SHF_ALLOC); | |
1027 | ||
1028 | unsigned char* vbuf; | |
1029 | unsigned int vsize; | |
91da9340 ILT |
1030 | versions->symbol_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)( |
1031 | &this->dynpool_, local_symcount, dynamic_symbols, &vbuf, &vsize | |
1032 | SELECT_SIZE_ENDIAN(size, big_endian)); | |
14b31740 ILT |
1033 | |
1034 | Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2); | |
1035 | ||
1036 | vsec->add_output_section_data(vdata); | |
1037 | vsec->set_entsize(2); | |
1038 | vsec->set_link_section(this->dynsym_section_); | |
1039 | ||
1040 | Output_data_dynamic* const odyn = this->dynamic_data_; | |
1041 | odyn->add_section_address(elfcpp::DT_VERSYM, vsec); | |
1042 | ||
1043 | if (versions->any_defs()) | |
1044 | { | |
1045 | const char* vdname = this->namepool_.add(".gnu.version_d", NULL); | |
1046 | Output_section *vdsec; | |
1047 | vdsec = this->make_output_section(vdname, elfcpp::SHT_GNU_verdef, | |
1048 | elfcpp::SHF_ALLOC); | |
1049 | ||
1050 | unsigned char* vdbuf; | |
1051 | unsigned int vdsize; | |
1052 | unsigned int vdentries; | |
91da9340 ILT |
1053 | versions->def_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)( |
1054 | &this->dynpool_, &vdbuf, &vdsize, &vdentries | |
1055 | SELECT_SIZE_ENDIAN(size, big_endian)); | |
14b31740 ILT |
1056 | |
1057 | Output_section_data* vddata = new Output_data_const_buffer(vdbuf, | |
1058 | vdsize, | |
1059 | 4); | |
1060 | ||
1061 | vdsec->add_output_section_data(vddata); | |
1062 | vdsec->set_link_section(dynstr); | |
1063 | vdsec->set_info(vdentries); | |
1064 | ||
1065 | odyn->add_section_address(elfcpp::DT_VERDEF, vdsec); | |
1066 | odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries); | |
1067 | } | |
1068 | ||
1069 | if (versions->any_needs()) | |
1070 | { | |
1071 | const char* vnname = this->namepool_.add(".gnu.version_r", NULL); | |
1072 | Output_section* vnsec; | |
1073 | vnsec = this->make_output_section(vnname, elfcpp::SHT_GNU_verneed, | |
1074 | elfcpp::SHF_ALLOC); | |
1075 | ||
1076 | unsigned char* vnbuf; | |
1077 | unsigned int vnsize; | |
1078 | unsigned int vnentries; | |
91da9340 ILT |
1079 | versions->need_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian) |
1080 | (&this->dynpool_, &vnbuf, &vnsize, &vnentries | |
1081 | SELECT_SIZE_ENDIAN(size, big_endian)); | |
14b31740 ILT |
1082 | |
1083 | Output_section_data* vndata = new Output_data_const_buffer(vnbuf, | |
1084 | vnsize, | |
1085 | 4); | |
1086 | ||
1087 | vnsec->add_output_section_data(vndata); | |
1088 | vnsec->set_link_section(dynstr); | |
1089 | vnsec->set_info(vnentries); | |
1090 | ||
1091 | odyn->add_section_address(elfcpp::DT_VERNEED, vnsec); | |
1092 | odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries); | |
1093 | } | |
1094 | } | |
1095 | ||
dbe717ef ILT |
1096 | // Create the .interp section and PT_INTERP segment. |
1097 | ||
1098 | void | |
1099 | Layout::create_interp(const Target* target) | |
1100 | { | |
1101 | const char* interp = this->options_.dynamic_linker(); | |
1102 | if (interp == NULL) | |
1103 | { | |
1104 | interp = target->dynamic_linker(); | |
a3ad94ed | 1105 | gold_assert(interp != NULL); |
dbe717ef ILT |
1106 | } |
1107 | ||
1108 | size_t len = strlen(interp) + 1; | |
1109 | ||
1110 | Output_section_data* odata = new Output_data_const(interp, len, 1); | |
1111 | ||
1112 | const char* interp_name = this->namepool_.add(".interp", NULL); | |
1113 | Output_section* osec = this->make_output_section(interp_name, | |
1114 | elfcpp::SHT_PROGBITS, | |
1115 | elfcpp::SHF_ALLOC); | |
1116 | osec->add_output_section_data(odata); | |
1117 | ||
1118 | Output_segment* oseg = new Output_segment(elfcpp::PT_INTERP, elfcpp::PF_R); | |
1119 | this->segment_list_.push_back(oseg); | |
1120 | oseg->add_initial_output_section(osec, elfcpp::PF_R); | |
1121 | } | |
1122 | ||
a3ad94ed ILT |
1123 | // Finish the .dynamic section and PT_DYNAMIC segment. |
1124 | ||
1125 | void | |
1126 | Layout::finish_dynamic_section(const Input_objects* input_objects, | |
16649710 | 1127 | const Symbol_table* symtab) |
a3ad94ed | 1128 | { |
a3ad94ed ILT |
1129 | Output_segment* oseg = new Output_segment(elfcpp::PT_DYNAMIC, |
1130 | elfcpp::PF_R | elfcpp::PF_W); | |
1131 | this->segment_list_.push_back(oseg); | |
1132 | oseg->add_initial_output_section(this->dynamic_section_, | |
1133 | elfcpp::PF_R | elfcpp::PF_W); | |
1134 | ||
16649710 ILT |
1135 | Output_data_dynamic* const odyn = this->dynamic_data_; |
1136 | ||
a3ad94ed ILT |
1137 | for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin(); |
1138 | p != input_objects->dynobj_end(); | |
1139 | ++p) | |
1140 | { | |
1141 | // FIXME: Handle --as-needed. | |
1142 | odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname()); | |
1143 | } | |
1144 | ||
1145 | // FIXME: Support --init and --fini. | |
1146 | Symbol* sym = symtab->lookup("_init"); | |
14b31740 | 1147 | if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj()) |
a3ad94ed ILT |
1148 | odyn->add_symbol(elfcpp::DT_INIT, sym); |
1149 | ||
1150 | sym = symtab->lookup("_fini"); | |
14b31740 | 1151 | if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj()) |
a3ad94ed ILT |
1152 | odyn->add_symbol(elfcpp::DT_FINI, sym); |
1153 | ||
1154 | // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY. | |
41f542e7 ILT |
1155 | |
1156 | // Add a DT_RPATH entry if needed. | |
1157 | const General_options::Dir_list& rpath(this->options_.rpath()); | |
1158 | if (!rpath.empty()) | |
1159 | { | |
1160 | std::string rpath_val; | |
1161 | for (General_options::Dir_list::const_iterator p = rpath.begin(); | |
1162 | p != rpath.end(); | |
1163 | ++p) | |
1164 | { | |
1165 | if (rpath_val.empty()) | |
1166 | rpath_val = *p; | |
1167 | else | |
1168 | { | |
1169 | // Eliminate duplicates. | |
1170 | General_options::Dir_list::const_iterator q; | |
1171 | for (q = rpath.begin(); q != p; ++q) | |
1172 | if (strcmp(*q, *p) == 0) | |
1173 | break; | |
1174 | if (q == p) | |
1175 | { | |
1176 | rpath_val += ':'; | |
1177 | rpath_val += *p; | |
1178 | } | |
1179 | } | |
1180 | } | |
1181 | ||
1182 | odyn->add_string(elfcpp::DT_RPATH, rpath_val); | |
1183 | } | |
a3ad94ed ILT |
1184 | } |
1185 | ||
a2fb1b05 ILT |
1186 | // The mapping of .gnu.linkonce section names to real section names. |
1187 | ||
ead1e424 | 1188 | #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 } |
a2fb1b05 ILT |
1189 | const Layout::Linkonce_mapping Layout::linkonce_mapping[] = |
1190 | { | |
1191 | MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d". | |
1192 | MAPPING_INIT("t", ".text"), | |
1193 | MAPPING_INIT("r", ".rodata"), | |
1194 | MAPPING_INIT("d", ".data"), | |
1195 | MAPPING_INIT("b", ".bss"), | |
1196 | MAPPING_INIT("s", ".sdata"), | |
1197 | MAPPING_INIT("sb", ".sbss"), | |
1198 | MAPPING_INIT("s2", ".sdata2"), | |
1199 | MAPPING_INIT("sb2", ".sbss2"), | |
1200 | MAPPING_INIT("wi", ".debug_info"), | |
1201 | MAPPING_INIT("td", ".tdata"), | |
1202 | MAPPING_INIT("tb", ".tbss"), | |
1203 | MAPPING_INIT("lr", ".lrodata"), | |
1204 | MAPPING_INIT("l", ".ldata"), | |
1205 | MAPPING_INIT("lb", ".lbss"), | |
1206 | }; | |
1207 | #undef MAPPING_INIT | |
1208 | ||
1209 | const int Layout::linkonce_mapping_count = | |
1210 | sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]); | |
1211 | ||
1212 | // Return the name of the output section to use for a .gnu.linkonce | |
1213 | // section. This is based on the default ELF linker script of the old | |
1214 | // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo" | |
ead1e424 ILT |
1215 | // to ".text". Set *PLEN to the length of the name. *PLEN is |
1216 | // initialized to the length of NAME. | |
a2fb1b05 ILT |
1217 | |
1218 | const char* | |
ead1e424 | 1219 | Layout::linkonce_output_name(const char* name, size_t *plen) |
a2fb1b05 ILT |
1220 | { |
1221 | const char* s = name + sizeof(".gnu.linkonce") - 1; | |
1222 | if (*s != '.') | |
1223 | return name; | |
1224 | ++s; | |
1225 | const Linkonce_mapping* plm = linkonce_mapping; | |
1226 | for (int i = 0; i < linkonce_mapping_count; ++i, ++plm) | |
1227 | { | |
1228 | if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.') | |
ead1e424 ILT |
1229 | { |
1230 | *plen = plm->tolen; | |
1231 | return plm->to; | |
1232 | } | |
a2fb1b05 ILT |
1233 | } |
1234 | return name; | |
1235 | } | |
1236 | ||
ead1e424 ILT |
1237 | // Choose the output section name to use given an input section name. |
1238 | // Set *PLEN to the length of the name. *PLEN is initialized to the | |
1239 | // length of NAME. | |
1240 | ||
1241 | const char* | |
1242 | Layout::output_section_name(const char* name, size_t* plen) | |
1243 | { | |
1244 | if (Layout::is_linkonce(name)) | |
1245 | { | |
1246 | // .gnu.linkonce sections are laid out as though they were named | |
1247 | // for the sections are placed into. | |
1248 | return Layout::linkonce_output_name(name, plen); | |
1249 | } | |
1250 | ||
1251 | // If the section name has no '.', or only an initial '.', we use | |
1252 | // the name unchanged (i.e., ".text" is unchanged). | |
1253 | ||
1254 | // Otherwise, if the section name does not include ".rel", we drop | |
1255 | // the last '.' and everything that follows (i.e., ".text.XXX" | |
1256 | // becomes ".text"). | |
1257 | ||
1258 | // Otherwise, if the section name has zero or one '.' after the | |
1259 | // ".rel", we use the name unchanged (i.e., ".rel.text" is | |
1260 | // unchanged). | |
1261 | ||
1262 | // Otherwise, we drop the last '.' and everything that follows | |
1263 | // (i.e., ".rel.text.XXX" becomes ".rel.text"). | |
1264 | ||
1265 | const char* s = name; | |
1266 | if (*s == '.') | |
1267 | ++s; | |
1268 | const char* sdot = strchr(s, '.'); | |
1269 | if (sdot == NULL) | |
1270 | return name; | |
1271 | ||
1272 | const char* srel = strstr(s, ".rel"); | |
1273 | if (srel == NULL) | |
1274 | { | |
1275 | *plen = sdot - name; | |
1276 | return name; | |
1277 | } | |
1278 | ||
1279 | sdot = strchr(srel + 1, '.'); | |
1280 | if (sdot == NULL) | |
1281 | return name; | |
1282 | sdot = strchr(sdot + 1, '.'); | |
1283 | if (sdot == NULL) | |
1284 | return name; | |
1285 | ||
1286 | *plen = sdot - name; | |
1287 | return name; | |
1288 | } | |
1289 | ||
a2fb1b05 ILT |
1290 | // Record the signature of a comdat section, and return whether to |
1291 | // include it in the link. If GROUP is true, this is a regular | |
1292 | // section group. If GROUP is false, this is a group signature | |
1293 | // derived from the name of a linkonce section. We want linkonce | |
1294 | // signatures and group signatures to block each other, but we don't | |
1295 | // want a linkonce signature to block another linkonce signature. | |
1296 | ||
1297 | bool | |
1298 | Layout::add_comdat(const char* signature, bool group) | |
1299 | { | |
1300 | std::string sig(signature); | |
1301 | std::pair<Signatures::iterator, bool> ins( | |
ead1e424 | 1302 | this->signatures_.insert(std::make_pair(sig, group))); |
a2fb1b05 ILT |
1303 | |
1304 | if (ins.second) | |
1305 | { | |
1306 | // This is the first time we've seen this signature. | |
1307 | return true; | |
1308 | } | |
1309 | ||
1310 | if (ins.first->second) | |
1311 | { | |
1312 | // We've already seen a real section group with this signature. | |
1313 | return false; | |
1314 | } | |
1315 | else if (group) | |
1316 | { | |
1317 | // This is a real section group, and we've already seen a | |
1318 | // linkonce section with tihs signature. Record that we've seen | |
1319 | // a section group, and don't include this section group. | |
1320 | ins.first->second = true; | |
1321 | return false; | |
1322 | } | |
1323 | else | |
1324 | { | |
1325 | // We've already seen a linkonce section and this is a linkonce | |
1326 | // section. These don't block each other--this may be the same | |
1327 | // symbol name with different section types. | |
1328 | return true; | |
1329 | } | |
1330 | } | |
1331 | ||
61ba1cf9 ILT |
1332 | // Write out data not associated with a section or the symbol table. |
1333 | ||
1334 | void | |
a3ad94ed ILT |
1335 | Layout::write_data(const Symbol_table* symtab, const Target* target, |
1336 | Output_file* of) const | |
61ba1cf9 | 1337 | { |
a3ad94ed ILT |
1338 | const Output_section* symtab_section = this->symtab_section_; |
1339 | for (Section_list::const_iterator p = this->section_list_.begin(); | |
1340 | p != this->section_list_.end(); | |
1341 | ++p) | |
1342 | { | |
1343 | if ((*p)->needs_symtab_index()) | |
1344 | { | |
1345 | gold_assert(symtab_section != NULL); | |
1346 | unsigned int index = (*p)->symtab_index(); | |
1347 | gold_assert(index > 0 && index != -1U); | |
1348 | off_t off = (symtab_section->offset() | |
1349 | + index * symtab_section->entsize()); | |
1350 | symtab->write_section_symbol(target, *p, of, off); | |
1351 | } | |
1352 | } | |
1353 | ||
1354 | const Output_section* dynsym_section = this->dynsym_section_; | |
1355 | for (Section_list::const_iterator p = this->section_list_.begin(); | |
1356 | p != this->section_list_.end(); | |
1357 | ++p) | |
1358 | { | |
1359 | if ((*p)->needs_dynsym_index()) | |
1360 | { | |
1361 | gold_assert(dynsym_section != NULL); | |
1362 | unsigned int index = (*p)->dynsym_index(); | |
1363 | gold_assert(index > 0 && index != -1U); | |
1364 | off_t off = (dynsym_section->offset() | |
1365 | + index * dynsym_section->entsize()); | |
1366 | symtab->write_section_symbol(target, *p, of, off); | |
1367 | } | |
1368 | } | |
1369 | ||
1370 | // Write out the Output_sections. Most won't have anything to | |
1371 | // write, since most of the data will come from input sections which | |
1372 | // are handled elsewhere. But some Output_sections do have | |
1373 | // Output_data. | |
1374 | for (Section_list::const_iterator p = this->section_list_.begin(); | |
1375 | p != this->section_list_.end(); | |
1376 | ++p) | |
1377 | (*p)->write(of); | |
1378 | ||
1379 | // Write out the Output_data which are not in an Output_section. | |
61ba1cf9 ILT |
1380 | for (Data_list::const_iterator p = this->special_output_list_.begin(); |
1381 | p != this->special_output_list_.end(); | |
1382 | ++p) | |
1383 | (*p)->write(of); | |
1384 | } | |
1385 | ||
1386 | // Write_data_task methods. | |
1387 | ||
1388 | // We can always run this task. | |
1389 | ||
1390 | Task::Is_runnable_type | |
1391 | Write_data_task::is_runnable(Workqueue*) | |
1392 | { | |
1393 | return IS_RUNNABLE; | |
1394 | } | |
1395 | ||
1396 | // We need to unlock FINAL_BLOCKER when finished. | |
1397 | ||
1398 | Task_locker* | |
1399 | Write_data_task::locks(Workqueue* workqueue) | |
1400 | { | |
1401 | return new Task_locker_block(*this->final_blocker_, workqueue); | |
1402 | } | |
1403 | ||
1404 | // Run the task--write out the data. | |
1405 | ||
1406 | void | |
1407 | Write_data_task::run(Workqueue*) | |
1408 | { | |
a3ad94ed | 1409 | this->layout_->write_data(this->symtab_, this->target_, this->of_); |
61ba1cf9 ILT |
1410 | } |
1411 | ||
1412 | // Write_symbols_task methods. | |
1413 | ||
1414 | // We can always run this task. | |
1415 | ||
1416 | Task::Is_runnable_type | |
1417 | Write_symbols_task::is_runnable(Workqueue*) | |
1418 | { | |
1419 | return IS_RUNNABLE; | |
1420 | } | |
1421 | ||
1422 | // We need to unlock FINAL_BLOCKER when finished. | |
1423 | ||
1424 | Task_locker* | |
1425 | Write_symbols_task::locks(Workqueue* workqueue) | |
1426 | { | |
1427 | return new Task_locker_block(*this->final_blocker_, workqueue); | |
1428 | } | |
1429 | ||
1430 | // Run the task--write out the symbols. | |
1431 | ||
1432 | void | |
1433 | Write_symbols_task::run(Workqueue*) | |
1434 | { | |
16649710 ILT |
1435 | this->symtab_->write_globals(this->target_, this->sympool_, this->dynpool_, |
1436 | this->of_); | |
61ba1cf9 ILT |
1437 | } |
1438 | ||
92e059d8 | 1439 | // Close_task_runner methods. |
61ba1cf9 ILT |
1440 | |
1441 | // Run the task--close the file. | |
1442 | ||
1443 | void | |
92e059d8 | 1444 | Close_task_runner::run(Workqueue*) |
61ba1cf9 ILT |
1445 | { |
1446 | this->of_->close(); | |
1447 | } | |
1448 | ||
a2fb1b05 ILT |
1449 | // Instantiate the templates we need. We could use the configure |
1450 | // script to restrict this to only the ones for implemented targets. | |
1451 | ||
1452 | template | |
1453 | Output_section* | |
f6ce93d6 | 1454 | Layout::layout<32, false>(Relobj* object, unsigned int shndx, const char* name, |
a2fb1b05 ILT |
1455 | const elfcpp::Shdr<32, false>& shdr, off_t*); |
1456 | ||
1457 | template | |
1458 | Output_section* | |
f6ce93d6 | 1459 | Layout::layout<32, true>(Relobj* object, unsigned int shndx, const char* name, |
a2fb1b05 ILT |
1460 | const elfcpp::Shdr<32, true>& shdr, off_t*); |
1461 | ||
1462 | template | |
1463 | Output_section* | |
f6ce93d6 | 1464 | Layout::layout<64, false>(Relobj* object, unsigned int shndx, const char* name, |
a2fb1b05 ILT |
1465 | const elfcpp::Shdr<64, false>& shdr, off_t*); |
1466 | ||
1467 | template | |
1468 | Output_section* | |
f6ce93d6 | 1469 | Layout::layout<64, true>(Relobj* object, unsigned int shndx, const char* name, |
a2fb1b05 ILT |
1470 | const elfcpp::Shdr<64, true>& shdr, off_t*); |
1471 | ||
1472 | ||
1473 | } // End namespace gold. |