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