Commit | Line | Data |
---|---|---|
494e05f4 ILT |
1 | // script-sections.cc -- linker script SECTIONS for gold |
2 | ||
0d371ad3 | 3 | // Copyright 2008, 2009 Free Software Foundation, Inc. |
494e05f4 ILT |
4 | // Written by Ian Lance Taylor <iant@google.com>. |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
23 | #include "gold.h" | |
24 | ||
a445fddf ILT |
25 | #include <cstring> |
26 | #include <algorithm> | |
27 | #include <list> | |
1c4f3631 | 28 | #include <map> |
494e05f4 ILT |
29 | #include <string> |
30 | #include <vector> | |
a445fddf | 31 | #include <fnmatch.h> |
494e05f4 | 32 | |
a445fddf ILT |
33 | #include "parameters.h" |
34 | #include "object.h" | |
35 | #include "layout.h" | |
36 | #include "output.h" | |
494e05f4 ILT |
37 | #include "script-c.h" |
38 | #include "script.h" | |
39 | #include "script-sections.h" | |
40 | ||
41 | // Support for the SECTIONS clause in linker scripts. | |
42 | ||
43 | namespace gold | |
44 | { | |
45 | ||
0d371ad3 ILT |
46 | // Manage orphan sections. This is intended to be largely compatible |
47 | // with the GNU linker. The Linux kernel implicitly relies on | |
48 | // something similar to the GNU linker's orphan placement. We | |
49 | // originally used a simpler scheme here, but it caused the kernel | |
50 | // build to fail, and was also rather inefficient. | |
51 | ||
52 | class Orphan_section_placement | |
53 | { | |
54 | private: | |
55 | typedef Script_sections::Elements_iterator Elements_iterator; | |
56 | ||
57 | public: | |
58 | Orphan_section_placement(); | |
59 | ||
60 | // Handle an output section during initialization of this mapping. | |
61 | void | |
62 | output_section_init(const std::string& name, Output_section*, | |
63 | Elements_iterator location); | |
64 | ||
65 | // Initialize the last location. | |
66 | void | |
67 | last_init(Elements_iterator location); | |
68 | ||
69 | // Set *PWHERE to the address of an iterator pointing to the | |
70 | // location to use for an orphan section. Return true if the | |
71 | // iterator has a value, false otherwise. | |
72 | bool | |
73 | find_place(Output_section*, Elements_iterator** pwhere); | |
74 | ||
75 | // Return the iterator being used for sections at the very end of | |
76 | // the linker script. | |
77 | Elements_iterator | |
78 | last_place() const; | |
79 | ||
80 | private: | |
81 | // The places that we specifically recognize. This list is copied | |
82 | // from the GNU linker. | |
83 | enum Place_index | |
84 | { | |
85 | PLACE_TEXT, | |
86 | PLACE_RODATA, | |
87 | PLACE_DATA, | |
88 | PLACE_BSS, | |
89 | PLACE_REL, | |
90 | PLACE_INTERP, | |
91 | PLACE_NONALLOC, | |
92 | PLACE_LAST, | |
93 | PLACE_MAX | |
94 | }; | |
95 | ||
96 | // The information we keep for a specific place. | |
97 | struct Place | |
98 | { | |
99 | // The name of sections for this place. | |
100 | const char* name; | |
101 | // Whether we have a location for this place. | |
102 | bool have_location; | |
103 | // The iterator for this place. | |
104 | Elements_iterator location; | |
105 | }; | |
106 | ||
107 | // Initialize one place element. | |
108 | void | |
109 | initialize_place(Place_index, const char*); | |
110 | ||
111 | // The places. | |
112 | Place places_[PLACE_MAX]; | |
113 | // True if this is the first call to output_section_init. | |
114 | bool first_init_; | |
115 | }; | |
116 | ||
117 | // Initialize Orphan_section_placement. | |
118 | ||
119 | Orphan_section_placement::Orphan_section_placement() | |
120 | : first_init_(true) | |
121 | { | |
122 | this->initialize_place(PLACE_TEXT, ".text"); | |
123 | this->initialize_place(PLACE_RODATA, ".rodata"); | |
124 | this->initialize_place(PLACE_DATA, ".data"); | |
125 | this->initialize_place(PLACE_BSS, ".bss"); | |
126 | this->initialize_place(PLACE_REL, NULL); | |
127 | this->initialize_place(PLACE_INTERP, ".interp"); | |
128 | this->initialize_place(PLACE_NONALLOC, NULL); | |
129 | this->initialize_place(PLACE_LAST, NULL); | |
130 | } | |
131 | ||
132 | // Initialize one place element. | |
133 | ||
134 | void | |
135 | Orphan_section_placement::initialize_place(Place_index index, const char* name) | |
136 | { | |
137 | this->places_[index].name = name; | |
138 | this->places_[index].have_location = false; | |
139 | } | |
140 | ||
141 | // While initializing the Orphan_section_placement information, this | |
142 | // is called once for each output section named in the linker script. | |
143 | // If we found an output section during the link, it will be passed in | |
144 | // OS. | |
145 | ||
146 | void | |
147 | Orphan_section_placement::output_section_init(const std::string& name, | |
148 | Output_section* os, | |
149 | Elements_iterator location) | |
150 | { | |
151 | bool first_init = this->first_init_; | |
152 | this->first_init_ = false; | |
153 | ||
154 | for (int i = 0; i < PLACE_MAX; ++i) | |
155 | { | |
156 | if (this->places_[i].name != NULL && this->places_[i].name == name) | |
157 | { | |
158 | if (this->places_[i].have_location) | |
159 | { | |
160 | // We have already seen a section with this name. | |
161 | return; | |
162 | } | |
163 | ||
164 | this->places_[i].location = location; | |
165 | this->places_[i].have_location = true; | |
166 | ||
167 | // If we just found the .bss section, restart the search for | |
168 | // an unallocated section. This follows the GNU linker's | |
169 | // behaviour. | |
170 | if (i == PLACE_BSS) | |
171 | this->places_[PLACE_NONALLOC].have_location = false; | |
172 | ||
173 | return; | |
174 | } | |
175 | } | |
176 | ||
177 | // Relocation sections. | |
178 | if (!this->places_[PLACE_REL].have_location | |
179 | && os != NULL | |
180 | && (os->type() == elfcpp::SHT_REL || os->type() == elfcpp::SHT_RELA) | |
181 | && (os->flags() & elfcpp::SHF_ALLOC) != 0) | |
182 | { | |
183 | this->places_[PLACE_REL].location = location; | |
184 | this->places_[PLACE_REL].have_location = true; | |
185 | } | |
186 | ||
187 | // We find the location for unallocated sections by finding the | |
188 | // first debugging or comment section after the BSS section (if | |
189 | // there is one). | |
190 | if (!this->places_[PLACE_NONALLOC].have_location | |
191 | && (name == ".comment" || Layout::is_debug_info_section(name.c_str()))) | |
192 | { | |
193 | // We add orphan sections after the location in PLACES_. We | |
194 | // want to store unallocated sections before LOCATION. If this | |
195 | // is the very first section, we can't use it. | |
196 | if (!first_init) | |
197 | { | |
198 | --location; | |
199 | this->places_[PLACE_NONALLOC].location = location; | |
200 | this->places_[PLACE_NONALLOC].have_location = true; | |
201 | } | |
202 | } | |
203 | } | |
204 | ||
205 | // Initialize the last location. | |
206 | ||
207 | void | |
208 | Orphan_section_placement::last_init(Elements_iterator location) | |
209 | { | |
210 | this->places_[PLACE_LAST].location = location; | |
211 | this->places_[PLACE_LAST].have_location = true; | |
212 | } | |
213 | ||
214 | // Set *PWHERE to the address of an iterator pointing to the location | |
215 | // to use for an orphan section. Return true if the iterator has a | |
216 | // value, false otherwise. | |
217 | ||
218 | bool | |
219 | Orphan_section_placement::find_place(Output_section* os, | |
220 | Elements_iterator** pwhere) | |
221 | { | |
222 | // Figure out where OS should go. This is based on the GNU linker | |
223 | // code. FIXME: The GNU linker handles small data sections | |
224 | // specially, but we don't. | |
225 | elfcpp::Elf_Word type = os->type(); | |
226 | elfcpp::Elf_Xword flags = os->flags(); | |
227 | Place_index index; | |
228 | if ((flags & elfcpp::SHF_ALLOC) == 0 | |
229 | && !Layout::is_debug_info_section(os->name())) | |
230 | index = PLACE_NONALLOC; | |
231 | else if ((flags & elfcpp::SHF_ALLOC) == 0) | |
232 | index = PLACE_LAST; | |
233 | else if (type == elfcpp::SHT_NOTE) | |
234 | index = PLACE_INTERP; | |
235 | else if (type == elfcpp::SHT_NOBITS) | |
236 | index = PLACE_BSS; | |
237 | else if ((flags & elfcpp::SHF_WRITE) != 0) | |
238 | index = PLACE_DATA; | |
239 | else if (type == elfcpp::SHT_REL || type == elfcpp::SHT_RELA) | |
240 | index = PLACE_REL; | |
241 | else if ((flags & elfcpp::SHF_EXECINSTR) == 0) | |
242 | index = PLACE_RODATA; | |
243 | else | |
244 | index = PLACE_TEXT; | |
245 | ||
246 | // If we don't have a location yet, try to find one based on a | |
247 | // plausible ordering of sections. | |
248 | if (!this->places_[index].have_location) | |
249 | { | |
250 | Place_index follow; | |
251 | switch (index) | |
252 | { | |
253 | default: | |
254 | follow = PLACE_MAX; | |
255 | break; | |
256 | case PLACE_RODATA: | |
257 | follow = PLACE_TEXT; | |
258 | break; | |
259 | case PLACE_BSS: | |
260 | follow = PLACE_DATA; | |
261 | break; | |
262 | case PLACE_REL: | |
263 | follow = PLACE_TEXT; | |
264 | break; | |
265 | case PLACE_INTERP: | |
266 | follow = PLACE_TEXT; | |
267 | break; | |
268 | } | |
269 | if (follow != PLACE_MAX && this->places_[follow].have_location) | |
270 | { | |
271 | // Set the location of INDEX to the location of FOLLOW. The | |
272 | // location of INDEX will then be incremented by the caller, | |
273 | // so anything in INDEX will continue to be after anything | |
274 | // in FOLLOW. | |
275 | this->places_[index].location = this->places_[follow].location; | |
276 | this->places_[index].have_location = true; | |
277 | } | |
278 | } | |
279 | ||
280 | *pwhere = &this->places_[index].location; | |
281 | bool ret = this->places_[index].have_location; | |
282 | ||
283 | // The caller will set the location. | |
284 | this->places_[index].have_location = true; | |
285 | ||
286 | return ret; | |
287 | } | |
288 | ||
289 | // Return the iterator being used for sections at the very end of the | |
290 | // linker script. | |
291 | ||
292 | Orphan_section_placement::Elements_iterator | |
293 | Orphan_section_placement::last_place() const | |
294 | { | |
295 | gold_assert(this->places_[PLACE_LAST].have_location); | |
296 | return this->places_[PLACE_LAST].location; | |
297 | } | |
298 | ||
494e05f4 ILT |
299 | // An element in a SECTIONS clause. |
300 | ||
301 | class Sections_element | |
302 | { | |
303 | public: | |
304 | Sections_element() | |
305 | { } | |
306 | ||
307 | virtual ~Sections_element() | |
308 | { } | |
309 | ||
0d371ad3 ILT |
310 | // Return whether an output section is relro. |
311 | virtual bool | |
312 | is_relro() const | |
313 | { return false; } | |
314 | ||
2d924fd9 ILT |
315 | // Record that an output section is relro. |
316 | virtual void | |
317 | set_is_relro() | |
318 | { } | |
319 | ||
919ed24c ILT |
320 | // Create any required output sections. The only real |
321 | // implementation is in Output_section_definition. | |
322 | virtual void | |
323 | create_sections(Layout*) | |
324 | { } | |
325 | ||
a445fddf ILT |
326 | // Add any symbol being defined to the symbol table. |
327 | virtual void | |
328 | add_symbols_to_table(Symbol_table*) | |
329 | { } | |
330 | ||
331 | // Finalize symbols and check assertions. | |
332 | virtual void | |
77e65537 | 333 | finalize_symbols(Symbol_table*, const Layout*, uint64_t*) |
a445fddf ILT |
334 | { } |
335 | ||
336 | // Return the output section name to use for an input file name and | |
337 | // section name. This only real implementation is in | |
338 | // Output_section_definition. | |
339 | virtual const char* | |
340 | output_section_name(const char*, const char*, Output_section***) | |
341 | { return NULL; } | |
342 | ||
0d371ad3 ILT |
343 | // Initialize OSP with an output section. |
344 | virtual void | |
345 | orphan_section_init(Orphan_section_placement*, | |
346 | Script_sections::Elements_iterator) | |
347 | { } | |
a445fddf ILT |
348 | |
349 | // Set section addresses. This includes applying assignments if the | |
350 | // the expression is an absolute value. | |
351 | virtual void | |
fd247bfe | 352 | set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*) |
a445fddf ILT |
353 | { } |
354 | ||
3802b2dd ILT |
355 | // Check a constraint (ONLY_IF_RO, etc.) on an output section. If |
356 | // this section is constrained, and the input sections do not match, | |
357 | // return the constraint, and set *POSD. | |
358 | virtual Section_constraint | |
359 | check_constraint(Output_section_definition**) | |
360 | { return CONSTRAINT_NONE; } | |
361 | ||
362 | // See if this is the alternate output section for a constrained | |
363 | // output section. If it is, transfer the Output_section and return | |
364 | // true. Otherwise return false. | |
365 | virtual bool | |
366 | alternate_constraint(Output_section_definition*, Section_constraint) | |
367 | { return false; } | |
368 | ||
1c4f3631 ILT |
369 | // Get the list of segments to use for an allocated section when |
370 | // using a PHDRS clause. If this is an allocated section, return | |
2cefc357 ILT |
371 | // the Output_section, and set *PHDRS_LIST (the first parameter) to |
372 | // the list of PHDRS to which it should be attached. If the PHDRS | |
373 | // were not specified, don't change *PHDRS_LIST. When not returning | |
374 | // NULL, set *ORPHAN (the second parameter) according to whether | |
375 | // this is an orphan section--one that is not mentioned in the | |
376 | // linker script. | |
1c4f3631 | 377 | virtual Output_section* |
2cefc357 | 378 | allocate_to_segment(String_list**, bool*) |
1c4f3631 ILT |
379 | { return NULL; } |
380 | ||
8f2eb564 ILT |
381 | // Look for an output section by name and return the address, the |
382 | // load address, the alignment, and the size. This is used when an | |
383 | // expression refers to an output section which was not actually | |
384 | // created. This returns true if the section was found, false | |
385 | // otherwise. The only real definition is for | |
386 | // Output_section_definition. | |
387 | virtual bool | |
388 | get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*, | |
389 | uint64_t*) const | |
390 | { return false; } | |
391 | ||
2d924fd9 ILT |
392 | // Return the associated Output_section if there is one. |
393 | virtual Output_section* | |
394 | get_output_section() const | |
395 | { return NULL; } | |
396 | ||
a445fddf | 397 | // Print the element for debugging purposes. |
494e05f4 ILT |
398 | virtual void |
399 | print(FILE* f) const = 0; | |
400 | }; | |
401 | ||
402 | // An assignment in a SECTIONS clause outside of an output section. | |
403 | ||
404 | class Sections_element_assignment : public Sections_element | |
405 | { | |
406 | public: | |
407 | Sections_element_assignment(const char* name, size_t namelen, | |
408 | Expression* val, bool provide, bool hidden) | |
409 | : assignment_(name, namelen, val, provide, hidden) | |
410 | { } | |
411 | ||
a445fddf ILT |
412 | // Add the symbol to the symbol table. |
413 | void | |
414 | add_symbols_to_table(Symbol_table* symtab) | |
415 | { this->assignment_.add_to_table(symtab); } | |
416 | ||
417 | // Finalize the symbol. | |
418 | void | |
419 | finalize_symbols(Symbol_table* symtab, const Layout* layout, | |
77e65537 | 420 | uint64_t* dot_value) |
a445fddf | 421 | { |
77e65537 | 422 | this->assignment_.finalize_with_dot(symtab, layout, *dot_value, NULL); |
a445fddf ILT |
423 | } |
424 | ||
425 | // Set the section address. There is no section here, but if the | |
426 | // value is absolute, we set the symbol. This permits us to use | |
427 | // absolute symbols when setting dot. | |
428 | void | |
429 | set_section_addresses(Symbol_table* symtab, Layout* layout, | |
fd247bfe | 430 | uint64_t* dot_value, uint64_t*) |
a445fddf | 431 | { |
77e65537 | 432 | this->assignment_.set_if_absolute(symtab, layout, true, *dot_value); |
a445fddf ILT |
433 | } |
434 | ||
435 | // Print for debugging. | |
494e05f4 ILT |
436 | void |
437 | print(FILE* f) const | |
438 | { | |
439 | fprintf(f, " "); | |
440 | this->assignment_.print(f); | |
441 | } | |
442 | ||
443 | private: | |
444 | Symbol_assignment assignment_; | |
445 | }; | |
446 | ||
a445fddf ILT |
447 | // An assignment to the dot symbol in a SECTIONS clause outside of an |
448 | // output section. | |
449 | ||
450 | class Sections_element_dot_assignment : public Sections_element | |
451 | { | |
452 | public: | |
453 | Sections_element_dot_assignment(Expression* val) | |
454 | : val_(val) | |
455 | { } | |
456 | ||
457 | // Finalize the symbol. | |
458 | void | |
459 | finalize_symbols(Symbol_table* symtab, const Layout* layout, | |
77e65537 | 460 | uint64_t* dot_value) |
a445fddf | 461 | { |
77e65537 ILT |
462 | // We ignore the section of the result because outside of an |
463 | // output section definition the dot symbol is always considered | |
464 | // to be absolute. | |
465 | Output_section* dummy; | |
919ed24c | 466 | *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value, |
77e65537 | 467 | NULL, &dummy); |
a445fddf ILT |
468 | } |
469 | ||
470 | // Update the dot symbol while setting section addresses. | |
471 | void | |
472 | set_section_addresses(Symbol_table* symtab, Layout* layout, | |
fd247bfe | 473 | uint64_t* dot_value, uint64_t* load_address) |
a445fddf | 474 | { |
77e65537 | 475 | Output_section* dummy; |
919ed24c | 476 | *dot_value = this->val_->eval_with_dot(symtab, layout, false, *dot_value, |
77e65537 | 477 | NULL, &dummy); |
fd247bfe | 478 | *load_address = *dot_value; |
a445fddf ILT |
479 | } |
480 | ||
481 | // Print for debugging. | |
482 | void | |
483 | print(FILE* f) const | |
484 | { | |
485 | fprintf(f, " . = "); | |
486 | this->val_->print(f); | |
487 | fprintf(f, "\n"); | |
488 | } | |
489 | ||
490 | private: | |
491 | Expression* val_; | |
492 | }; | |
493 | ||
494e05f4 ILT |
494 | // An assertion in a SECTIONS clause outside of an output section. |
495 | ||
496 | class Sections_element_assertion : public Sections_element | |
497 | { | |
498 | public: | |
499 | Sections_element_assertion(Expression* check, const char* message, | |
500 | size_t messagelen) | |
501 | : assertion_(check, message, messagelen) | |
502 | { } | |
503 | ||
a445fddf ILT |
504 | // Check the assertion. |
505 | void | |
77e65537 | 506 | finalize_symbols(Symbol_table* symtab, const Layout* layout, uint64_t*) |
a445fddf ILT |
507 | { this->assertion_.check(symtab, layout); } |
508 | ||
509 | // Print for debugging. | |
494e05f4 ILT |
510 | void |
511 | print(FILE* f) const | |
512 | { | |
513 | fprintf(f, " "); | |
514 | this->assertion_.print(f); | |
515 | } | |
516 | ||
517 | private: | |
518 | Script_assertion assertion_; | |
519 | }; | |
520 | ||
521 | // An element in an output section in a SECTIONS clause. | |
522 | ||
523 | class Output_section_element | |
524 | { | |
525 | public: | |
a445fddf | 526 | // A list of input sections. |
20e6d0d6 | 527 | typedef std::list<Output_section::Simple_input_section> Input_section_list; |
a445fddf | 528 | |
494e05f4 ILT |
529 | Output_section_element() |
530 | { } | |
531 | ||
532 | virtual ~Output_section_element() | |
533 | { } | |
534 | ||
919ed24c ILT |
535 | // Return whether this element requires an output section to exist. |
536 | virtual bool | |
537 | needs_output_section() const | |
538 | { return false; } | |
539 | ||
a445fddf ILT |
540 | // Add any symbol being defined to the symbol table. |
541 | virtual void | |
542 | add_symbols_to_table(Symbol_table*) | |
543 | { } | |
544 | ||
545 | // Finalize symbols and check assertions. | |
546 | virtual void | |
77e65537 | 547 | finalize_symbols(Symbol_table*, const Layout*, uint64_t*, Output_section**) |
a445fddf ILT |
548 | { } |
549 | ||
550 | // Return whether this element matches FILE_NAME and SECTION_NAME. | |
551 | // The only real implementation is in Output_section_element_input. | |
552 | virtual bool | |
553 | match_name(const char*, const char*) const | |
554 | { return false; } | |
555 | ||
556 | // Set section addresses. This includes applying assignments if the | |
557 | // the expression is an absolute value. | |
558 | virtual void | |
559 | set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t, | |
77e65537 ILT |
560 | uint64_t*, Output_section**, std::string*, |
561 | Input_section_list*) | |
a445fddf ILT |
562 | { } |
563 | ||
564 | // Print the element for debugging purposes. | |
494e05f4 ILT |
565 | virtual void |
566 | print(FILE* f) const = 0; | |
a445fddf ILT |
567 | |
568 | protected: | |
569 | // Return a fill string that is LENGTH bytes long, filling it with | |
570 | // FILL. | |
571 | std::string | |
572 | get_fill_string(const std::string* fill, section_size_type length) const; | |
494e05f4 ILT |
573 | }; |
574 | ||
a445fddf ILT |
575 | std::string |
576 | Output_section_element::get_fill_string(const std::string* fill, | |
577 | section_size_type length) const | |
578 | { | |
579 | std::string this_fill; | |
580 | this_fill.reserve(length); | |
581 | while (this_fill.length() + fill->length() <= length) | |
582 | this_fill += *fill; | |
583 | if (this_fill.length() < length) | |
584 | this_fill.append(*fill, 0, length - this_fill.length()); | |
585 | return this_fill; | |
586 | } | |
587 | ||
494e05f4 ILT |
588 | // A symbol assignment in an output section. |
589 | ||
590 | class Output_section_element_assignment : public Output_section_element | |
591 | { | |
592 | public: | |
593 | Output_section_element_assignment(const char* name, size_t namelen, | |
594 | Expression* val, bool provide, | |
595 | bool hidden) | |
596 | : assignment_(name, namelen, val, provide, hidden) | |
597 | { } | |
598 | ||
a445fddf ILT |
599 | // Add the symbol to the symbol table. |
600 | void | |
601 | add_symbols_to_table(Symbol_table* symtab) | |
602 | { this->assignment_.add_to_table(symtab); } | |
603 | ||
604 | // Finalize the symbol. | |
605 | void | |
606 | finalize_symbols(Symbol_table* symtab, const Layout* layout, | |
77e65537 | 607 | uint64_t* dot_value, Output_section** dot_section) |
a445fddf | 608 | { |
77e65537 ILT |
609 | this->assignment_.finalize_with_dot(symtab, layout, *dot_value, |
610 | *dot_section); | |
a445fddf ILT |
611 | } |
612 | ||
613 | // Set the section address. There is no section here, but if the | |
614 | // value is absolute, we set the symbol. This permits us to use | |
615 | // absolute symbols when setting dot. | |
616 | void | |
617 | set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*, | |
77e65537 ILT |
618 | uint64_t, uint64_t* dot_value, Output_section**, |
619 | std::string*, Input_section_list*) | |
a445fddf | 620 | { |
77e65537 | 621 | this->assignment_.set_if_absolute(symtab, layout, true, *dot_value); |
a445fddf ILT |
622 | } |
623 | ||
624 | // Print for debugging. | |
494e05f4 ILT |
625 | void |
626 | print(FILE* f) const | |
627 | { | |
628 | fprintf(f, " "); | |
629 | this->assignment_.print(f); | |
630 | } | |
631 | ||
632 | private: | |
633 | Symbol_assignment assignment_; | |
634 | }; | |
635 | ||
a445fddf ILT |
636 | // An assignment to the dot symbol in an output section. |
637 | ||
638 | class Output_section_element_dot_assignment : public Output_section_element | |
639 | { | |
640 | public: | |
641 | Output_section_element_dot_assignment(Expression* val) | |
642 | : val_(val) | |
643 | { } | |
644 | ||
645 | // Finalize the symbol. | |
646 | void | |
647 | finalize_symbols(Symbol_table* symtab, const Layout* layout, | |
77e65537 | 648 | uint64_t* dot_value, Output_section** dot_section) |
a445fddf | 649 | { |
919ed24c | 650 | *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value, |
77e65537 | 651 | *dot_section, dot_section); |
a445fddf ILT |
652 | } |
653 | ||
654 | // Update the dot symbol while setting section addresses. | |
655 | void | |
656 | set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*, | |
77e65537 ILT |
657 | uint64_t, uint64_t* dot_value, Output_section**, |
658 | std::string*, Input_section_list*); | |
a445fddf ILT |
659 | |
660 | // Print for debugging. | |
661 | void | |
662 | print(FILE* f) const | |
663 | { | |
664 | fprintf(f, " . = "); | |
665 | this->val_->print(f); | |
666 | fprintf(f, "\n"); | |
667 | } | |
668 | ||
669 | private: | |
670 | Expression* val_; | |
671 | }; | |
672 | ||
673 | // Update the dot symbol while setting section addresses. | |
674 | ||
675 | void | |
676 | Output_section_element_dot_assignment::set_section_addresses( | |
677 | Symbol_table* symtab, | |
678 | Layout* layout, | |
679 | Output_section* output_section, | |
680 | uint64_t, | |
681 | uint64_t* dot_value, | |
77e65537 | 682 | Output_section** dot_section, |
a445fddf ILT |
683 | std::string* fill, |
684 | Input_section_list*) | |
685 | { | |
919ed24c ILT |
686 | uint64_t next_dot = this->val_->eval_with_dot(symtab, layout, false, |
687 | *dot_value, *dot_section, | |
688 | dot_section); | |
a445fddf ILT |
689 | if (next_dot < *dot_value) |
690 | gold_error(_("dot may not move backward")); | |
691 | if (next_dot > *dot_value && output_section != NULL) | |
692 | { | |
693 | section_size_type length = convert_to_section_size_type(next_dot | |
694 | - *dot_value); | |
695 | Output_section_data* posd; | |
696 | if (fill->empty()) | |
7d9e3d98 | 697 | posd = new Output_data_zero_fill(length, 0); |
a445fddf ILT |
698 | else |
699 | { | |
700 | std::string this_fill = this->get_fill_string(fill, length); | |
701 | posd = new Output_data_const(this_fill, 0); | |
702 | } | |
703 | output_section->add_output_section_data(posd); | |
20e6d0d6 | 704 | layout->new_output_section_data_from_script(posd); |
a445fddf ILT |
705 | } |
706 | *dot_value = next_dot; | |
707 | } | |
708 | ||
494e05f4 ILT |
709 | // An assertion in an output section. |
710 | ||
711 | class Output_section_element_assertion : public Output_section_element | |
712 | { | |
713 | public: | |
714 | Output_section_element_assertion(Expression* check, const char* message, | |
715 | size_t messagelen) | |
716 | : assertion_(check, message, messagelen) | |
717 | { } | |
718 | ||
719 | void | |
720 | print(FILE* f) const | |
721 | { | |
722 | fprintf(f, " "); | |
723 | this->assertion_.print(f); | |
724 | } | |
725 | ||
726 | private: | |
727 | Script_assertion assertion_; | |
728 | }; | |
729 | ||
77e65537 ILT |
730 | // We use a special instance of Output_section_data to handle BYTE, |
731 | // SHORT, etc. This permits forward references to symbols in the | |
732 | // expressions. | |
494e05f4 | 733 | |
77e65537 | 734 | class Output_data_expression : public Output_section_data |
494e05f4 ILT |
735 | { |
736 | public: | |
77e65537 ILT |
737 | Output_data_expression(int size, bool is_signed, Expression* val, |
738 | const Symbol_table* symtab, const Layout* layout, | |
739 | uint64_t dot_value, Output_section* dot_section) | |
20e6d0d6 | 740 | : Output_section_data(size, 0, true), |
77e65537 ILT |
741 | is_signed_(is_signed), val_(val), symtab_(symtab), |
742 | layout_(layout), dot_value_(dot_value), dot_section_(dot_section) | |
494e05f4 ILT |
743 | { } |
744 | ||
77e65537 ILT |
745 | protected: |
746 | // Write the data to the output file. | |
a445fddf | 747 | void |
77e65537 | 748 | do_write(Output_file*); |
a445fddf | 749 | |
77e65537 | 750 | // Write the data to a buffer. |
494e05f4 | 751 | void |
77e65537 | 752 | do_write_to_buffer(unsigned char*); |
494e05f4 | 753 | |
7d9e3d98 ILT |
754 | // Write to a map file. |
755 | void | |
756 | do_print_to_mapfile(Mapfile* mapfile) const | |
757 | { mapfile->print_output_data(this, _("** expression")); } | |
758 | ||
494e05f4 | 759 | private: |
a445fddf | 760 | template<bool big_endian> |
77e65537 ILT |
761 | void |
762 | endian_write_to_buffer(uint64_t, unsigned char*); | |
a445fddf | 763 | |
494e05f4 | 764 | bool is_signed_; |
494e05f4 | 765 | Expression* val_; |
77e65537 ILT |
766 | const Symbol_table* symtab_; |
767 | const Layout* layout_; | |
768 | uint64_t dot_value_; | |
769 | Output_section* dot_section_; | |
494e05f4 ILT |
770 | }; |
771 | ||
77e65537 | 772 | // Write the data element to the output file. |
a445fddf ILT |
773 | |
774 | void | |
77e65537 | 775 | Output_data_expression::do_write(Output_file* of) |
a445fddf | 776 | { |
77e65537 ILT |
777 | unsigned char* view = of->get_output_view(this->offset(), this->data_size()); |
778 | this->write_to_buffer(view); | |
779 | of->write_output_view(this->offset(), this->data_size(), view); | |
780 | } | |
a445fddf | 781 | |
77e65537 ILT |
782 | // Write the data element to a buffer. |
783 | ||
784 | void | |
785 | Output_data_expression::do_write_to_buffer(unsigned char* buf) | |
786 | { | |
787 | Output_section* dummy; | |
788 | uint64_t val = this->val_->eval_with_dot(this->symtab_, this->layout_, | |
919ed24c | 789 | true, this->dot_value_, |
77e65537 | 790 | this->dot_section_, &dummy); |
a445fddf | 791 | |
8851ecca | 792 | if (parameters->target().is_big_endian()) |
77e65537 | 793 | this->endian_write_to_buffer<true>(val, buf); |
a445fddf | 794 | else |
77e65537 | 795 | this->endian_write_to_buffer<false>(val, buf); |
a445fddf ILT |
796 | } |
797 | ||
a445fddf | 798 | template<bool big_endian> |
77e65537 ILT |
799 | void |
800 | Output_data_expression::endian_write_to_buffer(uint64_t val, | |
801 | unsigned char* buf) | |
a445fddf | 802 | { |
77e65537 | 803 | switch (this->data_size()) |
a445fddf ILT |
804 | { |
805 | case 1: | |
806 | elfcpp::Swap_unaligned<8, big_endian>::writeval(buf, val); | |
a445fddf ILT |
807 | break; |
808 | case 2: | |
809 | elfcpp::Swap_unaligned<16, big_endian>::writeval(buf, val); | |
a445fddf ILT |
810 | break; |
811 | case 4: | |
812 | elfcpp::Swap_unaligned<32, big_endian>::writeval(buf, val); | |
a445fddf ILT |
813 | break; |
814 | case 8: | |
8851ecca | 815 | if (parameters->target().get_size() == 32) |
a445fddf ILT |
816 | { |
817 | val &= 0xffffffff; | |
818 | if (this->is_signed_ && (val & 0x80000000) != 0) | |
819 | val |= 0xffffffff00000000LL; | |
820 | } | |
821 | elfcpp::Swap_unaligned<64, big_endian>::writeval(buf, val); | |
a445fddf ILT |
822 | break; |
823 | default: | |
824 | gold_unreachable(); | |
825 | } | |
77e65537 ILT |
826 | } |
827 | ||
828 | // A data item in an output section. | |
829 | ||
830 | class Output_section_element_data : public Output_section_element | |
831 | { | |
832 | public: | |
833 | Output_section_element_data(int size, bool is_signed, Expression* val) | |
834 | : size_(size), is_signed_(is_signed), val_(val) | |
835 | { } | |
836 | ||
919ed24c ILT |
837 | // If there is a data item, then we must create an output section. |
838 | bool | |
839 | needs_output_section() const | |
840 | { return true; } | |
841 | ||
77e65537 ILT |
842 | // Finalize symbols--we just need to update dot. |
843 | void | |
844 | finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value, | |
845 | Output_section**) | |
846 | { *dot_value += this->size_; } | |
847 | ||
848 | // Store the value in the section. | |
849 | void | |
850 | set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t, | |
851 | uint64_t* dot_value, Output_section**, std::string*, | |
852 | Input_section_list*); | |
853 | ||
854 | // Print for debugging. | |
855 | void | |
856 | print(FILE*) const; | |
857 | ||
858 | private: | |
859 | // The size in bytes. | |
860 | int size_; | |
861 | // Whether the value is signed. | |
862 | bool is_signed_; | |
863 | // The value. | |
864 | Expression* val_; | |
865 | }; | |
866 | ||
867 | // Store the value in the section. | |
868 | ||
869 | void | |
870 | Output_section_element_data::set_section_addresses( | |
871 | Symbol_table* symtab, | |
872 | Layout* layout, | |
873 | Output_section* os, | |
874 | uint64_t, | |
875 | uint64_t* dot_value, | |
876 | Output_section** dot_section, | |
877 | std::string*, | |
878 | Input_section_list*) | |
879 | { | |
880 | gold_assert(os != NULL); | |
20e6d0d6 DK |
881 | Output_data_expression* expression = |
882 | new Output_data_expression(this->size_, this->is_signed_, this->val_, | |
883 | symtab, layout, *dot_value, *dot_section); | |
884 | os->add_output_section_data(expression); | |
885 | layout->new_output_section_data_from_script(expression); | |
77e65537 | 886 | *dot_value += this->size_; |
a445fddf ILT |
887 | } |
888 | ||
494e05f4 ILT |
889 | // Print for debugging. |
890 | ||
891 | void | |
892 | Output_section_element_data::print(FILE* f) const | |
893 | { | |
894 | const char* s; | |
895 | switch (this->size_) | |
896 | { | |
897 | case 1: | |
898 | s = "BYTE"; | |
899 | break; | |
900 | case 2: | |
901 | s = "SHORT"; | |
902 | break; | |
903 | case 4: | |
904 | s = "LONG"; | |
905 | break; | |
906 | case 8: | |
907 | if (this->is_signed_) | |
908 | s = "SQUAD"; | |
909 | else | |
910 | s = "QUAD"; | |
911 | break; | |
912 | default: | |
913 | gold_unreachable(); | |
914 | } | |
915 | fprintf(f, " %s(", s); | |
916 | this->val_->print(f); | |
917 | fprintf(f, ")\n"); | |
918 | } | |
919 | ||
920 | // A fill value setting in an output section. | |
921 | ||
922 | class Output_section_element_fill : public Output_section_element | |
923 | { | |
924 | public: | |
925 | Output_section_element_fill(Expression* val) | |
926 | : val_(val) | |
927 | { } | |
928 | ||
a445fddf ILT |
929 | // Update the fill value while setting section addresses. |
930 | void | |
931 | set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*, | |
77e65537 ILT |
932 | uint64_t, uint64_t* dot_value, |
933 | Output_section** dot_section, | |
934 | std::string* fill, Input_section_list*) | |
a445fddf | 935 | { |
77e65537 | 936 | Output_section* fill_section; |
919ed24c | 937 | uint64_t fill_val = this->val_->eval_with_dot(symtab, layout, false, |
77e65537 ILT |
938 | *dot_value, *dot_section, |
939 | &fill_section); | |
940 | if (fill_section != NULL) | |
941 | gold_warning(_("fill value is not absolute")); | |
a445fddf ILT |
942 | // FIXME: The GNU linker supports fill values of arbitrary length. |
943 | unsigned char fill_buff[4]; | |
944 | elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val); | |
945 | fill->assign(reinterpret_cast<char*>(fill_buff), 4); | |
946 | } | |
947 | ||
948 | // Print for debugging. | |
494e05f4 ILT |
949 | void |
950 | print(FILE* f) const | |
951 | { | |
952 | fprintf(f, " FILL("); | |
953 | this->val_->print(f); | |
954 | fprintf(f, ")\n"); | |
955 | } | |
956 | ||
957 | private: | |
958 | // The new fill value. | |
959 | Expression* val_; | |
960 | }; | |
961 | ||
a445fddf ILT |
962 | // Return whether STRING contains a wildcard character. This is used |
963 | // to speed up matching. | |
964 | ||
965 | static inline bool | |
966 | is_wildcard_string(const std::string& s) | |
967 | { | |
968 | return strpbrk(s.c_str(), "?*[") != NULL; | |
969 | } | |
970 | ||
494e05f4 ILT |
971 | // An input section specification in an output section |
972 | ||
973 | class Output_section_element_input : public Output_section_element | |
974 | { | |
975 | public: | |
494e05f4 ILT |
976 | Output_section_element_input(const Input_section_spec* spec, bool keep); |
977 | ||
a445fddf ILT |
978 | // Finalize symbols--just update the value of the dot symbol. |
979 | void | |
77e65537 ILT |
980 | finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value, |
981 | Output_section** dot_section) | |
a445fddf ILT |
982 | { |
983 | *dot_value = this->final_dot_value_; | |
77e65537 | 984 | *dot_section = this->final_dot_section_; |
a445fddf ILT |
985 | } |
986 | ||
987 | // See whether we match FILE_NAME and SECTION_NAME as an input | |
988 | // section. | |
989 | bool | |
990 | match_name(const char* file_name, const char* section_name) const; | |
991 | ||
992 | // Set the section address. | |
993 | void | |
994 | set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*, | |
995 | uint64_t subalign, uint64_t* dot_value, | |
77e65537 ILT |
996 | Output_section**, std::string* fill, |
997 | Input_section_list*); | |
a445fddf ILT |
998 | |
999 | // Print for debugging. | |
494e05f4 ILT |
1000 | void |
1001 | print(FILE* f) const; | |
1002 | ||
1003 | private: | |
1004 | // An input section pattern. | |
1005 | struct Input_section_pattern | |
1006 | { | |
1007 | std::string pattern; | |
a445fddf | 1008 | bool pattern_is_wildcard; |
494e05f4 ILT |
1009 | Sort_wildcard sort; |
1010 | ||
1011 | Input_section_pattern(const char* patterna, size_t patternlena, | |
1012 | Sort_wildcard sorta) | |
a445fddf ILT |
1013 | : pattern(patterna, patternlena), |
1014 | pattern_is_wildcard(is_wildcard_string(this->pattern)), | |
1015 | sort(sorta) | |
494e05f4 ILT |
1016 | { } |
1017 | }; | |
1018 | ||
1019 | typedef std::vector<Input_section_pattern> Input_section_patterns; | |
1020 | ||
a445fddf ILT |
1021 | // Filename_exclusions is a pair of filename pattern and a bool |
1022 | // indicating whether the filename is a wildcard. | |
1023 | typedef std::vector<std::pair<std::string, bool> > Filename_exclusions; | |
1024 | ||
1025 | // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN | |
1026 | // indicates whether this is a wildcard pattern. | |
1027 | static inline bool | |
1028 | match(const char* string, const char* pattern, bool is_wildcard_pattern) | |
1029 | { | |
1030 | return (is_wildcard_pattern | |
1031 | ? fnmatch(pattern, string, 0) == 0 | |
1032 | : strcmp(string, pattern) == 0); | |
1033 | } | |
494e05f4 | 1034 | |
a445fddf ILT |
1035 | // See if we match a file name. |
1036 | bool | |
1037 | match_file_name(const char* file_name) const; | |
1038 | ||
1039 | // The file name pattern. If this is the empty string, we match all | |
1040 | // files. | |
494e05f4 | 1041 | std::string filename_pattern_; |
a445fddf ILT |
1042 | // Whether the file name pattern is a wildcard. |
1043 | bool filename_is_wildcard_; | |
494e05f4 ILT |
1044 | // How the file names should be sorted. This may only be |
1045 | // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME. | |
1046 | Sort_wildcard filename_sort_; | |
1047 | // The list of file names to exclude. | |
1048 | Filename_exclusions filename_exclusions_; | |
1049 | // The list of input section patterns. | |
1050 | Input_section_patterns input_section_patterns_; | |
1051 | // Whether to keep this section when garbage collecting. | |
1052 | bool keep_; | |
a445fddf ILT |
1053 | // The value of dot after including all matching sections. |
1054 | uint64_t final_dot_value_; | |
77e65537 ILT |
1055 | // The section where dot is defined after including all matching |
1056 | // sections. | |
1057 | Output_section* final_dot_section_; | |
494e05f4 ILT |
1058 | }; |
1059 | ||
1060 | // Construct Output_section_element_input. The parser records strings | |
1061 | // as pointers into a copy of the script file, which will go away when | |
1062 | // parsing is complete. We make sure they are in std::string objects. | |
1063 | ||
1064 | Output_section_element_input::Output_section_element_input( | |
1065 | const Input_section_spec* spec, | |
1066 | bool keep) | |
a445fddf ILT |
1067 | : filename_pattern_(), |
1068 | filename_is_wildcard_(false), | |
494e05f4 ILT |
1069 | filename_sort_(spec->file.sort), |
1070 | filename_exclusions_(), | |
1071 | input_section_patterns_(), | |
a445fddf | 1072 | keep_(keep), |
77e65537 ILT |
1073 | final_dot_value_(0), |
1074 | final_dot_section_(NULL) | |
494e05f4 | 1075 | { |
a445fddf ILT |
1076 | // The filename pattern "*" is common, and matches all files. Turn |
1077 | // it into the empty string. | |
1078 | if (spec->file.name.length != 1 || spec->file.name.value[0] != '*') | |
1079 | this->filename_pattern_.assign(spec->file.name.value, | |
1080 | spec->file.name.length); | |
1081 | this->filename_is_wildcard_ = is_wildcard_string(this->filename_pattern_); | |
1082 | ||
494e05f4 ILT |
1083 | if (spec->input_sections.exclude != NULL) |
1084 | { | |
1085 | for (String_list::const_iterator p = | |
1086 | spec->input_sections.exclude->begin(); | |
1087 | p != spec->input_sections.exclude->end(); | |
1088 | ++p) | |
a445fddf ILT |
1089 | { |
1090 | bool is_wildcard = is_wildcard_string(*p); | |
1091 | this->filename_exclusions_.push_back(std::make_pair(*p, | |
1092 | is_wildcard)); | |
1093 | } | |
494e05f4 ILT |
1094 | } |
1095 | ||
1096 | if (spec->input_sections.sections != NULL) | |
1097 | { | |
1098 | Input_section_patterns& isp(this->input_section_patterns_); | |
1099 | for (String_sort_list::const_iterator p = | |
1100 | spec->input_sections.sections->begin(); | |
1101 | p != spec->input_sections.sections->end(); | |
1102 | ++p) | |
1103 | isp.push_back(Input_section_pattern(p->name.value, p->name.length, | |
1104 | p->sort)); | |
1105 | } | |
1106 | } | |
1107 | ||
a445fddf ILT |
1108 | // See whether we match FILE_NAME. |
1109 | ||
1110 | bool | |
1111 | Output_section_element_input::match_file_name(const char* file_name) const | |
1112 | { | |
1113 | if (!this->filename_pattern_.empty()) | |
1114 | { | |
1115 | // If we were called with no filename, we refuse to match a | |
1116 | // pattern which requires a file name. | |
1117 | if (file_name == NULL) | |
1118 | return false; | |
1119 | ||
1120 | if (!match(file_name, this->filename_pattern_.c_str(), | |
1121 | this->filename_is_wildcard_)) | |
1122 | return false; | |
1123 | } | |
1124 | ||
1125 | if (file_name != NULL) | |
1126 | { | |
1127 | // Now we have to see whether FILE_NAME matches one of the | |
1128 | // exclusion patterns, if any. | |
1129 | for (Filename_exclusions::const_iterator p = | |
1130 | this->filename_exclusions_.begin(); | |
1131 | p != this->filename_exclusions_.end(); | |
1132 | ++p) | |
1133 | { | |
1134 | if (match(file_name, p->first.c_str(), p->second)) | |
1135 | return false; | |
1136 | } | |
1137 | } | |
1138 | ||
1139 | return true; | |
1140 | } | |
1141 | ||
1142 | // See whether we match FILE_NAME and SECTION_NAME. | |
1143 | ||
1144 | bool | |
1145 | Output_section_element_input::match_name(const char* file_name, | |
1146 | const char* section_name) const | |
1147 | { | |
1148 | if (!this->match_file_name(file_name)) | |
1149 | return false; | |
1150 | ||
1151 | // If there are no section name patterns, then we match. | |
1152 | if (this->input_section_patterns_.empty()) | |
1153 | return true; | |
1154 | ||
1155 | // See whether we match the section name patterns. | |
1156 | for (Input_section_patterns::const_iterator p = | |
1157 | this->input_section_patterns_.begin(); | |
1158 | p != this->input_section_patterns_.end(); | |
1159 | ++p) | |
1160 | { | |
1161 | if (match(section_name, p->pattern.c_str(), p->pattern_is_wildcard)) | |
1162 | return true; | |
1163 | } | |
1164 | ||
1165 | // We didn't match any section names, so we didn't match. | |
1166 | return false; | |
1167 | } | |
1168 | ||
1169 | // Information we use to sort the input sections. | |
1170 | ||
20e6d0d6 | 1171 | class Input_section_info |
a445fddf | 1172 | { |
20e6d0d6 DK |
1173 | public: |
1174 | Input_section_info(const Output_section::Simple_input_section& input_section) | |
1175 | : input_section_(input_section), section_name_(), | |
1176 | size_(0), addralign_(1) | |
1177 | { } | |
1178 | ||
1179 | // Return the simple input section. | |
1180 | const Output_section::Simple_input_section& | |
1181 | input_section() const | |
1182 | { return this->input_section_; } | |
1183 | ||
1184 | // Return the object. | |
1185 | Relobj* | |
1186 | relobj() const | |
1187 | { return this->input_section_.relobj(); } | |
1188 | ||
1189 | // Return the section index. | |
1190 | unsigned int | |
1191 | shndx() | |
1192 | { return this->input_section_.shndx(); } | |
1193 | ||
1194 | // Return the section name. | |
1195 | const std::string& | |
1196 | section_name() const | |
1197 | { return this->section_name_; } | |
1198 | ||
1199 | // Set the section name. | |
1200 | void | |
1201 | set_section_name(const std::string name) | |
1202 | { this->section_name_ = name; } | |
1203 | ||
1204 | // Return the section size. | |
1205 | uint64_t | |
1206 | size() const | |
1207 | { return this->size_; } | |
1208 | ||
1209 | // Set the section size. | |
1210 | void | |
1211 | set_size(uint64_t size) | |
1212 | { this->size_ = size; } | |
1213 | ||
1214 | // Return the address alignment. | |
1215 | uint64_t | |
1216 | addralign() const | |
1217 | { return this->addralign_; } | |
1218 | ||
1219 | // Set the address alignment. | |
1220 | void | |
1221 | set_addralign(uint64_t addralign) | |
1222 | { this->addralign_ = addralign; } | |
1223 | ||
1224 | private: | |
1225 | // Input section, can be a relaxed section. | |
1226 | Output_section::Simple_input_section input_section_; | |
1227 | // Name of the section. | |
1228 | std::string section_name_; | |
1229 | // Section size. | |
1230 | uint64_t size_; | |
1231 | // Address alignment. | |
1232 | uint64_t addralign_; | |
a445fddf ILT |
1233 | }; |
1234 | ||
1235 | // A class to sort the input sections. | |
1236 | ||
1237 | class Input_section_sorter | |
1238 | { | |
1239 | public: | |
1240 | Input_section_sorter(Sort_wildcard filename_sort, Sort_wildcard section_sort) | |
1241 | : filename_sort_(filename_sort), section_sort_(section_sort) | |
1242 | { } | |
1243 | ||
1244 | bool | |
1245 | operator()(const Input_section_info&, const Input_section_info&) const; | |
1246 | ||
1247 | private: | |
1248 | Sort_wildcard filename_sort_; | |
1249 | Sort_wildcard section_sort_; | |
1250 | }; | |
1251 | ||
1252 | bool | |
1253 | Input_section_sorter::operator()(const Input_section_info& isi1, | |
1254 | const Input_section_info& isi2) const | |
1255 | { | |
1256 | if (this->section_sort_ == SORT_WILDCARD_BY_NAME | |
1257 | || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT | |
1258 | || (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME | |
20e6d0d6 | 1259 | && isi1.addralign() == isi2.addralign())) |
a445fddf | 1260 | { |
20e6d0d6 DK |
1261 | if (isi1.section_name() != isi2.section_name()) |
1262 | return isi1.section_name() < isi2.section_name(); | |
a445fddf ILT |
1263 | } |
1264 | if (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT | |
1265 | || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT | |
1266 | || this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME) | |
1267 | { | |
20e6d0d6 DK |
1268 | if (isi1.addralign() != isi2.addralign()) |
1269 | return isi1.addralign() < isi2.addralign(); | |
a445fddf ILT |
1270 | } |
1271 | if (this->filename_sort_ == SORT_WILDCARD_BY_NAME) | |
1272 | { | |
20e6d0d6 DK |
1273 | if (isi1.relobj()->name() != isi2.relobj()->name()) |
1274 | return (isi1.relobj()->name() < isi2.relobj()->name()); | |
a445fddf ILT |
1275 | } |
1276 | ||
1277 | // Otherwise we leave them in the same order. | |
1278 | return false; | |
1279 | } | |
1280 | ||
1281 | // Set the section address. Look in INPUT_SECTIONS for sections which | |
1282 | // match this spec, sort them as specified, and add them to the output | |
1283 | // section. | |
1284 | ||
1285 | void | |
1286 | Output_section_element_input::set_section_addresses( | |
1287 | Symbol_table*, | |
20e6d0d6 | 1288 | Layout* layout, |
a445fddf ILT |
1289 | Output_section* output_section, |
1290 | uint64_t subalign, | |
1291 | uint64_t* dot_value, | |
77e65537 | 1292 | Output_section** dot_section, |
a445fddf ILT |
1293 | std::string* fill, |
1294 | Input_section_list* input_sections) | |
1295 | { | |
1296 | // We build a list of sections which match each | |
1297 | // Input_section_pattern. | |
1298 | ||
1299 | typedef std::vector<std::vector<Input_section_info> > Matching_sections; | |
1300 | size_t input_pattern_count = this->input_section_patterns_.size(); | |
1301 | if (input_pattern_count == 0) | |
1302 | input_pattern_count = 1; | |
1303 | Matching_sections matching_sections(input_pattern_count); | |
1304 | ||
1305 | // Look through the list of sections for this output section. Add | |
1306 | // each one which matches to one of the elements of | |
1307 | // MATCHING_SECTIONS. | |
1308 | ||
1309 | Input_section_list::iterator p = input_sections->begin(); | |
1310 | while (p != input_sections->end()) | |
1311 | { | |
20e6d0d6 DK |
1312 | Relobj* relobj = p->relobj(); |
1313 | unsigned int shndx = p->shndx(); | |
1314 | Input_section_info isi(*p); | |
1315 | ||
a445fddf ILT |
1316 | // Calling section_name and section_addralign is not very |
1317 | // efficient. | |
a445fddf ILT |
1318 | |
1319 | // Lock the object so that we can get information about the | |
1320 | // section. This is OK since we know we are single-threaded | |
1321 | // here. | |
1322 | { | |
1323 | const Task* task = reinterpret_cast<const Task*>(-1); | |
20e6d0d6 DK |
1324 | Task_lock_obj<Object> tl(task, relobj); |
1325 | ||
1326 | isi.set_section_name(relobj->section_name(shndx)); | |
1327 | if (p->is_relaxed_input_section()) | |
1328 | isi.set_size(p->relaxed_input_section()->data_size()); | |
1329 | else | |
1330 | isi.set_size(relobj->section_size(shndx)); | |
1331 | isi.set_addralign(relobj->section_addralign(shndx)); | |
a445fddf ILT |
1332 | } |
1333 | ||
20e6d0d6 | 1334 | if (!this->match_file_name(relobj->name().c_str())) |
a445fddf ILT |
1335 | ++p; |
1336 | else if (this->input_section_patterns_.empty()) | |
1337 | { | |
1338 | matching_sections[0].push_back(isi); | |
1339 | p = input_sections->erase(p); | |
1340 | } | |
1341 | else | |
1342 | { | |
1343 | size_t i; | |
1344 | for (i = 0; i < input_pattern_count; ++i) | |
1345 | { | |
1346 | const Input_section_pattern& | |
1347 | isp(this->input_section_patterns_[i]); | |
20e6d0d6 | 1348 | if (match(isi.section_name().c_str(), isp.pattern.c_str(), |
a445fddf ILT |
1349 | isp.pattern_is_wildcard)) |
1350 | break; | |
1351 | } | |
1352 | ||
1353 | if (i >= this->input_section_patterns_.size()) | |
1354 | ++p; | |
1355 | else | |
1356 | { | |
1357 | matching_sections[i].push_back(isi); | |
1358 | p = input_sections->erase(p); | |
1359 | } | |
1360 | } | |
1361 | } | |
1362 | ||
1363 | // Look through MATCHING_SECTIONS. Sort each one as specified, | |
1364 | // using a stable sort so that we get the default order when | |
1365 | // sections are otherwise equal. Add each input section to the | |
1366 | // output section. | |
1367 | ||
1368 | for (size_t i = 0; i < input_pattern_count; ++i) | |
1369 | { | |
1370 | if (matching_sections[i].empty()) | |
1371 | continue; | |
1372 | ||
1373 | gold_assert(output_section != NULL); | |
1374 | ||
1375 | const Input_section_pattern& isp(this->input_section_patterns_[i]); | |
1376 | if (isp.sort != SORT_WILDCARD_NONE | |
1377 | || this->filename_sort_ != SORT_WILDCARD_NONE) | |
1378 | std::stable_sort(matching_sections[i].begin(), | |
1379 | matching_sections[i].end(), | |
1380 | Input_section_sorter(this->filename_sort_, | |
1381 | isp.sort)); | |
1382 | ||
1383 | for (std::vector<Input_section_info>::const_iterator p = | |
1384 | matching_sections[i].begin(); | |
1385 | p != matching_sections[i].end(); | |
1386 | ++p) | |
1387 | { | |
20e6d0d6 | 1388 | uint64_t this_subalign = p->addralign(); |
a445fddf ILT |
1389 | if (this_subalign < subalign) |
1390 | this_subalign = subalign; | |
1391 | ||
1392 | uint64_t address = align_address(*dot_value, this_subalign); | |
1393 | ||
1394 | if (address > *dot_value && !fill->empty()) | |
1395 | { | |
1396 | section_size_type length = | |
1397 | convert_to_section_size_type(address - *dot_value); | |
1398 | std::string this_fill = this->get_fill_string(fill, length); | |
1399 | Output_section_data* posd = new Output_data_const(this_fill, 0); | |
1400 | output_section->add_output_section_data(posd); | |
20e6d0d6 | 1401 | layout->new_output_section_data_from_script(posd); |
a445fddf ILT |
1402 | } |
1403 | ||
20e6d0d6 DK |
1404 | output_section->add_input_section_for_script(p->input_section(), |
1405 | p->size(), | |
a445fddf ILT |
1406 | this_subalign); |
1407 | ||
20e6d0d6 | 1408 | *dot_value = address + p->size(); |
a445fddf ILT |
1409 | } |
1410 | } | |
1411 | ||
1412 | this->final_dot_value_ = *dot_value; | |
77e65537 | 1413 | this->final_dot_section_ = *dot_section; |
a445fddf ILT |
1414 | } |
1415 | ||
494e05f4 ILT |
1416 | // Print for debugging. |
1417 | ||
1418 | void | |
1419 | Output_section_element_input::print(FILE* f) const | |
1420 | { | |
1421 | fprintf(f, " "); | |
1422 | ||
1423 | if (this->keep_) | |
1424 | fprintf(f, "KEEP("); | |
1425 | ||
1426 | if (!this->filename_pattern_.empty()) | |
1427 | { | |
1428 | bool need_close_paren = false; | |
1429 | switch (this->filename_sort_) | |
1430 | { | |
1431 | case SORT_WILDCARD_NONE: | |
1432 | break; | |
1433 | case SORT_WILDCARD_BY_NAME: | |
1434 | fprintf(f, "SORT_BY_NAME("); | |
1435 | need_close_paren = true; | |
1436 | break; | |
1437 | default: | |
1438 | gold_unreachable(); | |
1439 | } | |
1440 | ||
1441 | fprintf(f, "%s", this->filename_pattern_.c_str()); | |
1442 | ||
1443 | if (need_close_paren) | |
1444 | fprintf(f, ")"); | |
1445 | } | |
1446 | ||
1447 | if (!this->input_section_patterns_.empty() | |
1448 | || !this->filename_exclusions_.empty()) | |
1449 | { | |
1450 | fprintf(f, "("); | |
1451 | ||
1452 | bool need_space = false; | |
1453 | if (!this->filename_exclusions_.empty()) | |
1454 | { | |
1455 | fprintf(f, "EXCLUDE_FILE("); | |
1456 | bool need_comma = false; | |
1457 | for (Filename_exclusions::const_iterator p = | |
1458 | this->filename_exclusions_.begin(); | |
1459 | p != this->filename_exclusions_.end(); | |
1460 | ++p) | |
1461 | { | |
1462 | if (need_comma) | |
1463 | fprintf(f, ", "); | |
a445fddf | 1464 | fprintf(f, "%s", p->first.c_str()); |
494e05f4 ILT |
1465 | need_comma = true; |
1466 | } | |
1467 | fprintf(f, ")"); | |
1468 | need_space = true; | |
1469 | } | |
1470 | ||
1471 | for (Input_section_patterns::const_iterator p = | |
1472 | this->input_section_patterns_.begin(); | |
1473 | p != this->input_section_patterns_.end(); | |
1474 | ++p) | |
1475 | { | |
1476 | if (need_space) | |
1477 | fprintf(f, " "); | |
1478 | ||
1479 | int close_parens = 0; | |
1480 | switch (p->sort) | |
1481 | { | |
1482 | case SORT_WILDCARD_NONE: | |
1483 | break; | |
1484 | case SORT_WILDCARD_BY_NAME: | |
1485 | fprintf(f, "SORT_BY_NAME("); | |
1486 | close_parens = 1; | |
1487 | break; | |
1488 | case SORT_WILDCARD_BY_ALIGNMENT: | |
1489 | fprintf(f, "SORT_BY_ALIGNMENT("); | |
1490 | close_parens = 1; | |
1491 | break; | |
1492 | case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT: | |
1493 | fprintf(f, "SORT_BY_NAME(SORT_BY_ALIGNMENT("); | |
1494 | close_parens = 2; | |
1495 | break; | |
1496 | case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME: | |
1497 | fprintf(f, "SORT_BY_ALIGNMENT(SORT_BY_NAME("); | |
1498 | close_parens = 2; | |
1499 | break; | |
1500 | default: | |
1501 | gold_unreachable(); | |
1502 | } | |
1503 | ||
1504 | fprintf(f, "%s", p->pattern.c_str()); | |
1505 | ||
1506 | for (int i = 0; i < close_parens; ++i) | |
1507 | fprintf(f, ")"); | |
1508 | ||
1509 | need_space = true; | |
1510 | } | |
1511 | ||
1512 | fprintf(f, ")"); | |
1513 | } | |
1514 | ||
1515 | if (this->keep_) | |
1516 | fprintf(f, ")"); | |
1517 | ||
1518 | fprintf(f, "\n"); | |
1519 | } | |
1520 | ||
1521 | // An output section. | |
1522 | ||
1523 | class Output_section_definition : public Sections_element | |
1524 | { | |
1525 | public: | |
a445fddf ILT |
1526 | typedef Output_section_element::Input_section_list Input_section_list; |
1527 | ||
494e05f4 ILT |
1528 | Output_section_definition(const char* name, size_t namelen, |
1529 | const Parser_output_section_header* header); | |
1530 | ||
1531 | // Finish the output section with the information in the trailer. | |
1532 | void | |
1533 | finish(const Parser_output_section_trailer* trailer); | |
1534 | ||
1535 | // Add a symbol to be defined. | |
1536 | void | |
1537 | add_symbol_assignment(const char* name, size_t length, Expression* value, | |
1538 | bool provide, bool hidden); | |
a445fddf ILT |
1539 | |
1540 | // Add an assignment to the special dot symbol. | |
1541 | void | |
1542 | add_dot_assignment(Expression* value); | |
1543 | ||
494e05f4 ILT |
1544 | // Add an assertion. |
1545 | void | |
1546 | add_assertion(Expression* check, const char* message, size_t messagelen); | |
1547 | ||
1548 | // Add a data item to the current output section. | |
1549 | void | |
1550 | add_data(int size, bool is_signed, Expression* val); | |
1551 | ||
1552 | // Add a setting for the fill value. | |
1553 | void | |
1554 | add_fill(Expression* val); | |
1555 | ||
1556 | // Add an input section specification. | |
1557 | void | |
1558 | add_input_section(const Input_section_spec* spec, bool keep); | |
1559 | ||
0d371ad3 ILT |
1560 | // Return whether the output section is relro. |
1561 | bool | |
1562 | is_relro() const | |
1563 | { return this->is_relro_; } | |
1564 | ||
2d924fd9 ILT |
1565 | // Record that the output section is relro. |
1566 | void | |
1567 | set_is_relro() | |
1568 | { this->is_relro_ = true; } | |
1569 | ||
919ed24c ILT |
1570 | // Create any required output sections. |
1571 | void | |
1572 | create_sections(Layout*); | |
1573 | ||
a445fddf ILT |
1574 | // Add any symbols being defined to the symbol table. |
1575 | void | |
1576 | add_symbols_to_table(Symbol_table* symtab); | |
1577 | ||
1578 | // Finalize symbols and check assertions. | |
1579 | void | |
77e65537 | 1580 | finalize_symbols(Symbol_table*, const Layout*, uint64_t*); |
a445fddf ILT |
1581 | |
1582 | // Return the output section name to use for an input file name and | |
1583 | // section name. | |
1584 | const char* | |
1585 | output_section_name(const char* file_name, const char* section_name, | |
1586 | Output_section***); | |
1587 | ||
0d371ad3 ILT |
1588 | // Initialize OSP with an output section. |
1589 | void | |
1590 | orphan_section_init(Orphan_section_placement* osp, | |
1591 | Script_sections::Elements_iterator p) | |
1592 | { osp->output_section_init(this->name_, this->output_section_, p); } | |
a445fddf ILT |
1593 | |
1594 | // Set the section address. | |
1595 | void | |
1596 | set_section_addresses(Symbol_table* symtab, Layout* layout, | |
fd247bfe | 1597 | uint64_t* dot_value, uint64_t* load_address); |
a445fddf | 1598 | |
3802b2dd ILT |
1599 | // Check a constraint (ONLY_IF_RO, etc.) on an output section. If |
1600 | // this section is constrained, and the input sections do not match, | |
1601 | // return the constraint, and set *POSD. | |
1602 | Section_constraint | |
1603 | check_constraint(Output_section_definition** posd); | |
1604 | ||
1605 | // See if this is the alternate output section for a constrained | |
1606 | // output section. If it is, transfer the Output_section and return | |
1607 | // true. Otherwise return false. | |
1608 | bool | |
1609 | alternate_constraint(Output_section_definition*, Section_constraint); | |
1610 | ||
1c4f3631 | 1611 | // Get the list of segments to use for an allocated section when |
2cefc357 | 1612 | // using a PHDRS clause. |
1c4f3631 | 1613 | Output_section* |
2cefc357 | 1614 | allocate_to_segment(String_list** phdrs_list, bool* orphan); |
1c4f3631 | 1615 | |
8f2eb564 ILT |
1616 | // Look for an output section by name and return the address, the |
1617 | // load address, the alignment, and the size. This is used when an | |
1618 | // expression refers to an output section which was not actually | |
1619 | // created. This returns true if the section was found, false | |
1620 | // otherwise. | |
1621 | bool | |
1622 | get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*, | |
1623 | uint64_t*) const; | |
1624 | ||
2d924fd9 ILT |
1625 | // Return the associated Output_section if there is one. |
1626 | Output_section* | |
1627 | get_output_section() const | |
1628 | { return this->output_section_; } | |
1629 | ||
494e05f4 ILT |
1630 | // Print the contents to the FILE. This is for debugging. |
1631 | void | |
1632 | print(FILE*) const; | |
1633 | ||
1634 | private: | |
1635 | typedef std::vector<Output_section_element*> Output_section_elements; | |
1636 | ||
1637 | // The output section name. | |
1638 | std::string name_; | |
1639 | // The address. This may be NULL. | |
1640 | Expression* address_; | |
1641 | // The load address. This may be NULL. | |
1642 | Expression* load_address_; | |
1643 | // The alignment. This may be NULL. | |
1644 | Expression* align_; | |
1645 | // The input section alignment. This may be NULL. | |
1646 | Expression* subalign_; | |
3802b2dd ILT |
1647 | // The constraint, if any. |
1648 | Section_constraint constraint_; | |
494e05f4 ILT |
1649 | // The fill value. This may be NULL. |
1650 | Expression* fill_; | |
1c4f3631 ILT |
1651 | // The list of segments this section should go into. This may be |
1652 | // NULL. | |
1653 | String_list* phdrs_; | |
494e05f4 ILT |
1654 | // The list of elements defining the section. |
1655 | Output_section_elements elements_; | |
a445fddf ILT |
1656 | // The Output_section created for this definition. This will be |
1657 | // NULL if none was created. | |
1658 | Output_section* output_section_; | |
8f2eb564 ILT |
1659 | // The address after it has been evaluated. |
1660 | uint64_t evaluated_address_; | |
1661 | // The load address after it has been evaluated. | |
1662 | uint64_t evaluated_load_address_; | |
1663 | // The alignment after it has been evaluated. | |
1664 | uint64_t evaluated_addralign_; | |
2d924fd9 ILT |
1665 | // The output section is relro. |
1666 | bool is_relro_; | |
494e05f4 ILT |
1667 | }; |
1668 | ||
1669 | // Constructor. | |
1670 | ||
1671 | Output_section_definition::Output_section_definition( | |
1672 | const char* name, | |
1673 | size_t namelen, | |
1674 | const Parser_output_section_header* header) | |
1675 | : name_(name, namelen), | |
1676 | address_(header->address), | |
1677 | load_address_(header->load_address), | |
1678 | align_(header->align), | |
1679 | subalign_(header->subalign), | |
3802b2dd | 1680 | constraint_(header->constraint), |
494e05f4 | 1681 | fill_(NULL), |
1c4f3631 | 1682 | phdrs_(NULL), |
a445fddf | 1683 | elements_(), |
2d924fd9 ILT |
1684 | output_section_(NULL), |
1685 | evaluated_address_(0), | |
1686 | evaluated_load_address_(0), | |
1687 | evaluated_addralign_(0), | |
1688 | is_relro_(false) | |
494e05f4 ILT |
1689 | { |
1690 | } | |
1691 | ||
1692 | // Finish an output section. | |
1693 | ||
1694 | void | |
1695 | Output_section_definition::finish(const Parser_output_section_trailer* trailer) | |
1696 | { | |
1697 | this->fill_ = trailer->fill; | |
1c4f3631 | 1698 | this->phdrs_ = trailer->phdrs; |
494e05f4 ILT |
1699 | } |
1700 | ||
1701 | // Add a symbol to be defined. | |
1702 | ||
1703 | void | |
1704 | Output_section_definition::add_symbol_assignment(const char* name, | |
1705 | size_t length, | |
1706 | Expression* value, | |
1707 | bool provide, | |
1708 | bool hidden) | |
1709 | { | |
1710 | Output_section_element* p = new Output_section_element_assignment(name, | |
1711 | length, | |
1712 | value, | |
1713 | provide, | |
1714 | hidden); | |
1715 | this->elements_.push_back(p); | |
1716 | } | |
1717 | ||
a445fddf | 1718 | // Add an assignment to the special dot symbol. |
494e05f4 ILT |
1719 | |
1720 | void | |
a445fddf ILT |
1721 | Output_section_definition::add_dot_assignment(Expression* value) |
1722 | { | |
1723 | Output_section_element* p = new Output_section_element_dot_assignment(value); | |
1724 | this->elements_.push_back(p); | |
1725 | } | |
1726 | ||
1727 | // Add an assertion. | |
1728 | ||
1729 | void | |
1730 | Output_section_definition::add_assertion(Expression* check, | |
1731 | const char* message, | |
494e05f4 ILT |
1732 | size_t messagelen) |
1733 | { | |
1734 | Output_section_element* p = new Output_section_element_assertion(check, | |
1735 | message, | |
1736 | messagelen); | |
1737 | this->elements_.push_back(p); | |
1738 | } | |
1739 | ||
1740 | // Add a data item to the current output section. | |
1741 | ||
1742 | void | |
1743 | Output_section_definition::add_data(int size, bool is_signed, Expression* val) | |
1744 | { | |
1745 | Output_section_element* p = new Output_section_element_data(size, is_signed, | |
1746 | val); | |
1747 | this->elements_.push_back(p); | |
1748 | } | |
1749 | ||
1750 | // Add a setting for the fill value. | |
1751 | ||
1752 | void | |
1753 | Output_section_definition::add_fill(Expression* val) | |
1754 | { | |
1755 | Output_section_element* p = new Output_section_element_fill(val); | |
1756 | this->elements_.push_back(p); | |
1757 | } | |
1758 | ||
1759 | // Add an input section specification. | |
1760 | ||
1761 | void | |
1762 | Output_section_definition::add_input_section(const Input_section_spec* spec, | |
1763 | bool keep) | |
1764 | { | |
1765 | Output_section_element* p = new Output_section_element_input(spec, keep); | |
1766 | this->elements_.push_back(p); | |
1767 | } | |
1768 | ||
919ed24c ILT |
1769 | // Create any required output sections. We need an output section if |
1770 | // there is a data statement here. | |
1771 | ||
1772 | void | |
1773 | Output_section_definition::create_sections(Layout* layout) | |
1774 | { | |
1775 | if (this->output_section_ != NULL) | |
1776 | return; | |
1777 | for (Output_section_elements::const_iterator p = this->elements_.begin(); | |
1778 | p != this->elements_.end(); | |
1779 | ++p) | |
1780 | { | |
1781 | if ((*p)->needs_output_section()) | |
1782 | { | |
1783 | const char* name = this->name_.c_str(); | |
1784 | this->output_section_ = layout->make_output_section_for_script(name); | |
1785 | return; | |
1786 | } | |
1787 | } | |
1788 | } | |
1789 | ||
a445fddf ILT |
1790 | // Add any symbols being defined to the symbol table. |
1791 | ||
1792 | void | |
1793 | Output_section_definition::add_symbols_to_table(Symbol_table* symtab) | |
1794 | { | |
1795 | for (Output_section_elements::iterator p = this->elements_.begin(); | |
1796 | p != this->elements_.end(); | |
1797 | ++p) | |
1798 | (*p)->add_symbols_to_table(symtab); | |
1799 | } | |
1800 | ||
1801 | // Finalize symbols and check assertions. | |
1802 | ||
1803 | void | |
1804 | Output_section_definition::finalize_symbols(Symbol_table* symtab, | |
1805 | const Layout* layout, | |
a445fddf ILT |
1806 | uint64_t* dot_value) |
1807 | { | |
1808 | if (this->output_section_ != NULL) | |
1809 | *dot_value = this->output_section_->address(); | |
1810 | else | |
1811 | { | |
1812 | uint64_t address = *dot_value; | |
1813 | if (this->address_ != NULL) | |
1814 | { | |
77e65537 | 1815 | Output_section* dummy; |
919ed24c | 1816 | address = this->address_->eval_with_dot(symtab, layout, true, |
77e65537 | 1817 | *dot_value, NULL, |
a445fddf ILT |
1818 | &dummy); |
1819 | } | |
1820 | if (this->align_ != NULL) | |
1821 | { | |
77e65537 | 1822 | Output_section* dummy; |
919ed24c | 1823 | uint64_t align = this->align_->eval_with_dot(symtab, layout, true, |
a445fddf | 1824 | *dot_value, |
77e65537 | 1825 | NULL, |
a445fddf ILT |
1826 | &dummy); |
1827 | address = align_address(address, align); | |
1828 | } | |
1829 | *dot_value = address; | |
1830 | } | |
a445fddf | 1831 | |
77e65537 | 1832 | Output_section* dot_section = this->output_section_; |
a445fddf ILT |
1833 | for (Output_section_elements::iterator p = this->elements_.begin(); |
1834 | p != this->elements_.end(); | |
1835 | ++p) | |
77e65537 | 1836 | (*p)->finalize_symbols(symtab, layout, dot_value, &dot_section); |
a445fddf ILT |
1837 | } |
1838 | ||
1839 | // Return the output section name to use for an input section name. | |
1840 | ||
1841 | const char* | |
1842 | Output_section_definition::output_section_name(const char* file_name, | |
1843 | const char* section_name, | |
1844 | Output_section*** slot) | |
1845 | { | |
1846 | // Ask each element whether it matches NAME. | |
1847 | for (Output_section_elements::const_iterator p = this->elements_.begin(); | |
1848 | p != this->elements_.end(); | |
1849 | ++p) | |
1850 | { | |
1851 | if ((*p)->match_name(file_name, section_name)) | |
1852 | { | |
1853 | // We found a match for NAME, which means that it should go | |
1854 | // into this output section. | |
1855 | *slot = &this->output_section_; | |
1856 | return this->name_.c_str(); | |
1857 | } | |
1858 | } | |
1859 | ||
1860 | // We don't know about this section name. | |
1861 | return NULL; | |
1862 | } | |
1863 | ||
a445fddf ILT |
1864 | // Set the section address. Note that the OUTPUT_SECTION_ field will |
1865 | // be NULL if no input sections were mapped to this output section. | |
1866 | // We still have to adjust dot and process symbol assignments. | |
1867 | ||
1868 | void | |
1869 | Output_section_definition::set_section_addresses(Symbol_table* symtab, | |
1870 | Layout* layout, | |
fd247bfe ILT |
1871 | uint64_t* dot_value, |
1872 | uint64_t* load_address) | |
a445fddf | 1873 | { |
a445fddf | 1874 | uint64_t address; |
77e65537 ILT |
1875 | if (this->address_ == NULL) |
1876 | address = *dot_value; | |
a445fddf ILT |
1877 | else |
1878 | { | |
77e65537 | 1879 | Output_section* dummy; |
919ed24c ILT |
1880 | address = this->address_->eval_with_dot(symtab, layout, true, |
1881 | *dot_value, NULL, &dummy); | |
a445fddf ILT |
1882 | } |
1883 | ||
1884 | uint64_t align; | |
1885 | if (this->align_ == NULL) | |
1886 | { | |
1887 | if (this->output_section_ == NULL) | |
1888 | align = 0; | |
1889 | else | |
1890 | align = this->output_section_->addralign(); | |
1891 | } | |
1892 | else | |
1893 | { | |
77e65537 | 1894 | Output_section* align_section; |
919ed24c | 1895 | align = this->align_->eval_with_dot(symtab, layout, true, *dot_value, |
77e65537 ILT |
1896 | NULL, &align_section); |
1897 | if (align_section != NULL) | |
1898 | gold_warning(_("alignment of section %s is not absolute"), | |
1899 | this->name_.c_str()); | |
a445fddf ILT |
1900 | if (this->output_section_ != NULL) |
1901 | this->output_section_->set_addralign(align); | |
1902 | } | |
1903 | ||
1904 | address = align_address(address, align); | |
1905 | ||
fd247bfe ILT |
1906 | uint64_t start_address = address; |
1907 | ||
a445fddf | 1908 | *dot_value = address; |
a445fddf ILT |
1909 | |
1910 | // The address of non-SHF_ALLOC sections is forced to zero, | |
1911 | // regardless of what the linker script wants. | |
1912 | if (this->output_section_ != NULL | |
1913 | && (this->output_section_->flags() & elfcpp::SHF_ALLOC) != 0) | |
1914 | this->output_section_->set_address(address); | |
1915 | ||
8f2eb564 ILT |
1916 | this->evaluated_address_ = address; |
1917 | this->evaluated_addralign_ = align; | |
1918 | ||
1919 | if (this->load_address_ == NULL) | |
1920 | this->evaluated_load_address_ = address; | |
1921 | else | |
a445fddf | 1922 | { |
77e65537 | 1923 | Output_section* dummy; |
55458500 | 1924 | uint64_t laddr = |
919ed24c | 1925 | this->load_address_->eval_with_dot(symtab, layout, true, *dot_value, |
77e65537 | 1926 | this->output_section_, &dummy); |
8f2eb564 | 1927 | if (this->output_section_ != NULL) |
55458500 ILT |
1928 | this->output_section_->set_load_address(laddr); |
1929 | this->evaluated_load_address_ = laddr; | |
a445fddf ILT |
1930 | } |
1931 | ||
1932 | uint64_t subalign; | |
1933 | if (this->subalign_ == NULL) | |
1934 | subalign = 0; | |
1935 | else | |
1936 | { | |
77e65537 | 1937 | Output_section* subalign_section; |
919ed24c ILT |
1938 | subalign = this->subalign_->eval_with_dot(symtab, layout, true, |
1939 | *dot_value, NULL, | |
1940 | &subalign_section); | |
77e65537 ILT |
1941 | if (subalign_section != NULL) |
1942 | gold_warning(_("subalign of section %s is not absolute"), | |
1943 | this->name_.c_str()); | |
a445fddf ILT |
1944 | } |
1945 | ||
1946 | std::string fill; | |
1947 | if (this->fill_ != NULL) | |
1948 | { | |
1949 | // FIXME: The GNU linker supports fill values of arbitrary | |
1950 | // length. | |
77e65537 | 1951 | Output_section* fill_section; |
919ed24c | 1952 | uint64_t fill_val = this->fill_->eval_with_dot(symtab, layout, true, |
a445fddf | 1953 | *dot_value, |
77e65537 ILT |
1954 | NULL, |
1955 | &fill_section); | |
1956 | if (fill_section != NULL) | |
1957 | gold_warning(_("fill of section %s is not absolute"), | |
1958 | this->name_.c_str()); | |
a445fddf ILT |
1959 | unsigned char fill_buff[4]; |
1960 | elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val); | |
1961 | fill.assign(reinterpret_cast<char*>(fill_buff), 4); | |
1962 | } | |
1963 | ||
1964 | Input_section_list input_sections; | |
1965 | if (this->output_section_ != NULL) | |
1966 | { | |
1967 | // Get the list of input sections attached to this output | |
1968 | // section. This will leave the output section with only | |
1969 | // Output_section_data entries. | |
1970 | address += this->output_section_->get_input_sections(address, | |
1971 | fill, | |
1972 | &input_sections); | |
1973 | *dot_value = address; | |
1974 | } | |
1975 | ||
77e65537 | 1976 | Output_section* dot_section = this->output_section_; |
a445fddf ILT |
1977 | for (Output_section_elements::iterator p = this->elements_.begin(); |
1978 | p != this->elements_.end(); | |
1979 | ++p) | |
1980 | (*p)->set_section_addresses(symtab, layout, this->output_section_, | |
77e65537 ILT |
1981 | subalign, dot_value, &dot_section, &fill, |
1982 | &input_sections); | |
a445fddf ILT |
1983 | |
1984 | gold_assert(input_sections.empty()); | |
fd247bfe ILT |
1985 | |
1986 | if (this->load_address_ == NULL || this->output_section_ == NULL) | |
1987 | *load_address = *dot_value; | |
1988 | else | |
1989 | *load_address = (this->output_section_->load_address() | |
1990 | + (*dot_value - start_address)); | |
2d924fd9 ILT |
1991 | |
1992 | if (this->output_section_ != NULL) | |
1993 | { | |
1994 | if (this->is_relro_) | |
1995 | this->output_section_->set_is_relro(); | |
1996 | else | |
1997 | this->output_section_->clear_is_relro(); | |
1998 | } | |
a445fddf ILT |
1999 | } |
2000 | ||
3802b2dd ILT |
2001 | // Check a constraint (ONLY_IF_RO, etc.) on an output section. If |
2002 | // this section is constrained, and the input sections do not match, | |
2003 | // return the constraint, and set *POSD. | |
2004 | ||
2005 | Section_constraint | |
2006 | Output_section_definition::check_constraint(Output_section_definition** posd) | |
2007 | { | |
2008 | switch (this->constraint_) | |
2009 | { | |
2010 | case CONSTRAINT_NONE: | |
2011 | return CONSTRAINT_NONE; | |
2012 | ||
2013 | case CONSTRAINT_ONLY_IF_RO: | |
2014 | if (this->output_section_ != NULL | |
2015 | && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0) | |
2016 | { | |
2017 | *posd = this; | |
2018 | return CONSTRAINT_ONLY_IF_RO; | |
2019 | } | |
2020 | return CONSTRAINT_NONE; | |
2021 | ||
2022 | case CONSTRAINT_ONLY_IF_RW: | |
2023 | if (this->output_section_ != NULL | |
2024 | && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0) | |
2025 | { | |
2026 | *posd = this; | |
2027 | return CONSTRAINT_ONLY_IF_RW; | |
2028 | } | |
2029 | return CONSTRAINT_NONE; | |
2030 | ||
2031 | case CONSTRAINT_SPECIAL: | |
2032 | if (this->output_section_ != NULL) | |
2033 | gold_error(_("SPECIAL constraints are not implemented")); | |
2034 | return CONSTRAINT_NONE; | |
2035 | ||
2036 | default: | |
2037 | gold_unreachable(); | |
2038 | } | |
2039 | } | |
2040 | ||
2041 | // See if this is the alternate output section for a constrained | |
2042 | // output section. If it is, transfer the Output_section and return | |
2043 | // true. Otherwise return false. | |
2044 | ||
2045 | bool | |
2046 | Output_section_definition::alternate_constraint( | |
2047 | Output_section_definition* posd, | |
2048 | Section_constraint constraint) | |
2049 | { | |
2050 | if (this->name_ != posd->name_) | |
2051 | return false; | |
2052 | ||
2053 | switch (constraint) | |
2054 | { | |
2055 | case CONSTRAINT_ONLY_IF_RO: | |
2056 | if (this->constraint_ != CONSTRAINT_ONLY_IF_RW) | |
2057 | return false; | |
2058 | break; | |
2059 | ||
2060 | case CONSTRAINT_ONLY_IF_RW: | |
2061 | if (this->constraint_ != CONSTRAINT_ONLY_IF_RO) | |
2062 | return false; | |
2063 | break; | |
2064 | ||
2065 | default: | |
2066 | gold_unreachable(); | |
2067 | } | |
2068 | ||
2069 | // We have found the alternate constraint. We just need to move | |
2070 | // over the Output_section. When constraints are used properly, | |
2071 | // THIS should not have an output_section pointer, as all the input | |
2072 | // sections should have matched the other definition. | |
2073 | ||
2074 | if (this->output_section_ != NULL) | |
2075 | gold_error(_("mismatched definition for constrained sections")); | |
2076 | ||
2077 | this->output_section_ = posd->output_section_; | |
2078 | posd->output_section_ = NULL; | |
2079 | ||
2d924fd9 ILT |
2080 | if (this->is_relro_) |
2081 | this->output_section_->set_is_relro(); | |
2082 | else | |
2083 | this->output_section_->clear_is_relro(); | |
2084 | ||
3802b2dd ILT |
2085 | return true; |
2086 | } | |
2087 | ||
1c4f3631 | 2088 | // Get the list of segments to use for an allocated section when using |
2cefc357 | 2089 | // a PHDRS clause. |
1c4f3631 ILT |
2090 | |
2091 | Output_section* | |
2cefc357 ILT |
2092 | Output_section_definition::allocate_to_segment(String_list** phdrs_list, |
2093 | bool* orphan) | |
1c4f3631 ILT |
2094 | { |
2095 | if (this->output_section_ == NULL) | |
2096 | return NULL; | |
2097 | if ((this->output_section_->flags() & elfcpp::SHF_ALLOC) == 0) | |
2098 | return NULL; | |
2cefc357 | 2099 | *orphan = false; |
1c4f3631 ILT |
2100 | if (this->phdrs_ != NULL) |
2101 | *phdrs_list = this->phdrs_; | |
2102 | return this->output_section_; | |
2103 | } | |
2104 | ||
8f2eb564 ILT |
2105 | // Look for an output section by name and return the address, the load |
2106 | // address, the alignment, and the size. This is used when an | |
2107 | // expression refers to an output section which was not actually | |
2108 | // created. This returns true if the section was found, false | |
2109 | // otherwise. | |
2110 | ||
2111 | bool | |
2112 | Output_section_definition::get_output_section_info(const char* name, | |
2113 | uint64_t* address, | |
2114 | uint64_t* load_address, | |
2115 | uint64_t* addralign, | |
2116 | uint64_t* size) const | |
2117 | { | |
2118 | if (this->name_ != name) | |
2119 | return false; | |
2120 | ||
2121 | if (this->output_section_ != NULL) | |
2122 | { | |
2123 | *address = this->output_section_->address(); | |
2124 | if (this->output_section_->has_load_address()) | |
2125 | *load_address = this->output_section_->load_address(); | |
2126 | else | |
2127 | *load_address = *address; | |
2128 | *addralign = this->output_section_->addralign(); | |
2129 | *size = this->output_section_->current_data_size(); | |
2130 | } | |
2131 | else | |
2132 | { | |
2133 | *address = this->evaluated_address_; | |
2134 | *load_address = this->evaluated_load_address_; | |
2135 | *addralign = this->evaluated_addralign_; | |
2136 | *size = 0; | |
2137 | } | |
2138 | ||
2139 | return true; | |
2140 | } | |
2141 | ||
494e05f4 ILT |
2142 | // Print for debugging. |
2143 | ||
2144 | void | |
2145 | Output_section_definition::print(FILE* f) const | |
2146 | { | |
2147 | fprintf(f, " %s ", this->name_.c_str()); | |
2148 | ||
2149 | if (this->address_ != NULL) | |
2150 | { | |
2151 | this->address_->print(f); | |
2152 | fprintf(f, " "); | |
2153 | } | |
2154 | ||
2155 | fprintf(f, ": "); | |
2156 | ||
2157 | if (this->load_address_ != NULL) | |
2158 | { | |
2159 | fprintf(f, "AT("); | |
2160 | this->load_address_->print(f); | |
2161 | fprintf(f, ") "); | |
2162 | } | |
2163 | ||
2164 | if (this->align_ != NULL) | |
2165 | { | |
2166 | fprintf(f, "ALIGN("); | |
2167 | this->align_->print(f); | |
2168 | fprintf(f, ") "); | |
2169 | } | |
2170 | ||
2171 | if (this->subalign_ != NULL) | |
2172 | { | |
2173 | fprintf(f, "SUBALIGN("); | |
2174 | this->subalign_->print(f); | |
2175 | fprintf(f, ") "); | |
2176 | } | |
2177 | ||
2178 | fprintf(f, "{\n"); | |
2179 | ||
2180 | for (Output_section_elements::const_iterator p = this->elements_.begin(); | |
2181 | p != this->elements_.end(); | |
2182 | ++p) | |
2183 | (*p)->print(f); | |
2184 | ||
2185 | fprintf(f, " }"); | |
2186 | ||
2187 | if (this->fill_ != NULL) | |
2188 | { | |
2189 | fprintf(f, " = "); | |
2190 | this->fill_->print(f); | |
2191 | } | |
2192 | ||
7d26c6cc ILT |
2193 | if (this->phdrs_ != NULL) |
2194 | { | |
2195 | for (String_list::const_iterator p = this->phdrs_->begin(); | |
2196 | p != this->phdrs_->end(); | |
2197 | ++p) | |
2198 | fprintf(f, " :%s", p->c_str()); | |
2199 | } | |
2200 | ||
494e05f4 ILT |
2201 | fprintf(f, "\n"); |
2202 | } | |
2203 | ||
a445fddf ILT |
2204 | // An output section created to hold orphaned input sections. These |
2205 | // do not actually appear in linker scripts. However, for convenience | |
2206 | // when setting the output section addresses, we put a marker to these | |
2207 | // sections in the appropriate place in the list of SECTIONS elements. | |
2208 | ||
2209 | class Orphan_output_section : public Sections_element | |
2210 | { | |
2211 | public: | |
2212 | Orphan_output_section(Output_section* os) | |
2213 | : os_(os) | |
2214 | { } | |
2215 | ||
0d371ad3 ILT |
2216 | // Return whether the orphan output section is relro. We can just |
2217 | // check the output section because we always set the flag, if | |
2218 | // needed, just after we create the Orphan_output_section. | |
a445fddf | 2219 | bool |
0d371ad3 ILT |
2220 | is_relro() const |
2221 | { return this->os_->is_relro(); } | |
2222 | ||
2223 | // Initialize OSP with an output section. This should have been | |
2224 | // done already. | |
2225 | void | |
2226 | orphan_section_init(Orphan_section_placement*, | |
2227 | Script_sections::Elements_iterator) | |
2228 | { gold_unreachable(); } | |
a445fddf ILT |
2229 | |
2230 | // Set section addresses. | |
2231 | void | |
fd247bfe | 2232 | set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*); |
a445fddf | 2233 | |
1c4f3631 | 2234 | // Get the list of segments to use for an allocated section when |
2cefc357 | 2235 | // using a PHDRS clause. |
1c4f3631 | 2236 | Output_section* |
2cefc357 | 2237 | allocate_to_segment(String_list**, bool*); |
1c4f3631 | 2238 | |
2d924fd9 ILT |
2239 | // Return the associated Output_section. |
2240 | Output_section* | |
2241 | get_output_section() const | |
2242 | { return this->os_; } | |
2243 | ||
a445fddf ILT |
2244 | // Print for debugging. |
2245 | void | |
2246 | print(FILE* f) const | |
2247 | { | |
2248 | fprintf(f, " marker for orphaned output section %s\n", | |
2249 | this->os_->name()); | |
2250 | } | |
2251 | ||
2252 | private: | |
2253 | Output_section* os_; | |
2254 | }; | |
2255 | ||
a445fddf ILT |
2256 | // Set section addresses. |
2257 | ||
2258 | void | |
2259 | Orphan_output_section::set_section_addresses(Symbol_table*, Layout*, | |
fd247bfe ILT |
2260 | uint64_t* dot_value, |
2261 | uint64_t* load_address) | |
a445fddf | 2262 | { |
20e6d0d6 | 2263 | typedef std::list<Output_section::Simple_input_section> Input_section_list; |
a445fddf | 2264 | |
fd247bfe ILT |
2265 | bool have_load_address = *load_address != *dot_value; |
2266 | ||
a445fddf ILT |
2267 | uint64_t address = *dot_value; |
2268 | address = align_address(address, this->os_->addralign()); | |
2269 | ||
2270 | if ((this->os_->flags() & elfcpp::SHF_ALLOC) != 0) | |
fd247bfe ILT |
2271 | { |
2272 | this->os_->set_address(address); | |
2273 | if (have_load_address) | |
2274 | this->os_->set_load_address(align_address(*load_address, | |
2275 | this->os_->addralign())); | |
2276 | } | |
a445fddf ILT |
2277 | |
2278 | Input_section_list input_sections; | |
2279 | address += this->os_->get_input_sections(address, "", &input_sections); | |
2280 | ||
2281 | for (Input_section_list::iterator p = input_sections.begin(); | |
2282 | p != input_sections.end(); | |
2283 | ++p) | |
2284 | { | |
2285 | uint64_t addralign; | |
2286 | uint64_t size; | |
2287 | ||
2288 | // We know what are single-threaded, so it is OK to lock the | |
2289 | // object. | |
2290 | { | |
2291 | const Task* task = reinterpret_cast<const Task*>(-1); | |
20e6d0d6 DK |
2292 | Task_lock_obj<Object> tl(task, p->relobj()); |
2293 | addralign = p->relobj()->section_addralign(p->shndx()); | |
2294 | if (p->is_relaxed_input_section()) | |
2295 | size = p->relaxed_input_section()->data_size(); | |
2296 | else | |
2297 | size = p->relobj()->section_size(p->shndx()); | |
a445fddf ILT |
2298 | } |
2299 | ||
2300 | address = align_address(address, addralign); | |
20e6d0d6 | 2301 | this->os_->add_input_section_for_script(*p, size, addralign); |
a445fddf ILT |
2302 | address += size; |
2303 | } | |
2304 | ||
fd247bfe ILT |
2305 | if (!have_load_address) |
2306 | *load_address = address; | |
2307 | else | |
2308 | *load_address += address - *dot_value; | |
2309 | ||
a445fddf ILT |
2310 | *dot_value = address; |
2311 | } | |
2312 | ||
1c4f3631 ILT |
2313 | // Get the list of segments to use for an allocated section when using |
2314 | // a PHDRS clause. If this is an allocated section, return the | |
2315 | // Output_section. We don't change the list of segments. | |
2316 | ||
2317 | Output_section* | |
2cefc357 | 2318 | Orphan_output_section::allocate_to_segment(String_list**, bool* orphan) |
1c4f3631 ILT |
2319 | { |
2320 | if ((this->os_->flags() & elfcpp::SHF_ALLOC) == 0) | |
2321 | return NULL; | |
2cefc357 | 2322 | *orphan = true; |
1c4f3631 ILT |
2323 | return this->os_; |
2324 | } | |
2325 | ||
2326 | // Class Phdrs_element. A program header from a PHDRS clause. | |
2327 | ||
2328 | class Phdrs_element | |
2329 | { | |
2330 | public: | |
2331 | Phdrs_element(const char* name, size_t namelen, unsigned int type, | |
2332 | bool includes_filehdr, bool includes_phdrs, | |
2333 | bool is_flags_valid, unsigned int flags, | |
2334 | Expression* load_address) | |
2335 | : name_(name, namelen), type_(type), includes_filehdr_(includes_filehdr), | |
2336 | includes_phdrs_(includes_phdrs), is_flags_valid_(is_flags_valid), | |
2337 | flags_(flags), load_address_(load_address), load_address_value_(0), | |
2338 | segment_(NULL) | |
2339 | { } | |
2340 | ||
2341 | // Return the name of this segment. | |
2342 | const std::string& | |
2343 | name() const | |
2344 | { return this->name_; } | |
2345 | ||
2346 | // Return the type of the segment. | |
2347 | unsigned int | |
2348 | type() const | |
2349 | { return this->type_; } | |
2350 | ||
2351 | // Whether to include the file header. | |
2352 | bool | |
2353 | includes_filehdr() const | |
2354 | { return this->includes_filehdr_; } | |
2355 | ||
2356 | // Whether to include the program headers. | |
2357 | bool | |
2358 | includes_phdrs() const | |
2359 | { return this->includes_phdrs_; } | |
2360 | ||
2361 | // Return whether there is a load address. | |
2362 | bool | |
2363 | has_load_address() const | |
2364 | { return this->load_address_ != NULL; } | |
2365 | ||
2366 | // Evaluate the load address expression if there is one. | |
2367 | void | |
2368 | eval_load_address(Symbol_table* symtab, Layout* layout) | |
2369 | { | |
2370 | if (this->load_address_ != NULL) | |
919ed24c ILT |
2371 | this->load_address_value_ = this->load_address_->eval(symtab, layout, |
2372 | true); | |
1c4f3631 ILT |
2373 | } |
2374 | ||
2375 | // Return the load address. | |
2376 | uint64_t | |
2377 | load_address() const | |
2378 | { | |
2379 | gold_assert(this->load_address_ != NULL); | |
2380 | return this->load_address_value_; | |
2381 | } | |
2382 | ||
2383 | // Create the segment. | |
2384 | Output_segment* | |
2385 | create_segment(Layout* layout) | |
2386 | { | |
2387 | this->segment_ = layout->make_output_segment(this->type_, this->flags_); | |
2388 | return this->segment_; | |
2389 | } | |
2390 | ||
2391 | // Return the segment. | |
2392 | Output_segment* | |
2393 | segment() | |
2394 | { return this->segment_; } | |
2395 | ||
20e6d0d6 DK |
2396 | // Release the segment. |
2397 | void | |
2398 | release_segment() | |
2399 | { this->segment_ = NULL; } | |
2400 | ||
1c4f3631 ILT |
2401 | // Set the segment flags if appropriate. |
2402 | void | |
2403 | set_flags_if_valid() | |
2404 | { | |
2405 | if (this->is_flags_valid_) | |
2406 | this->segment_->set_flags(this->flags_); | |
2407 | } | |
2408 | ||
7d26c6cc ILT |
2409 | // Print for debugging. |
2410 | void | |
2411 | print(FILE*) const; | |
2412 | ||
1c4f3631 ILT |
2413 | private: |
2414 | // The name used in the script. | |
2415 | std::string name_; | |
2416 | // The type of the segment (PT_LOAD, etc.). | |
2417 | unsigned int type_; | |
2418 | // Whether this segment includes the file header. | |
2419 | bool includes_filehdr_; | |
2420 | // Whether this segment includes the section headers. | |
2421 | bool includes_phdrs_; | |
2422 | // Whether the flags were explicitly specified. | |
2423 | bool is_flags_valid_; | |
2424 | // The flags for this segment (PF_R, etc.) if specified. | |
2425 | unsigned int flags_; | |
2426 | // The expression for the load address for this segment. This may | |
2427 | // be NULL. | |
2428 | Expression* load_address_; | |
2429 | // The actual load address from evaluating the expression. | |
2430 | uint64_t load_address_value_; | |
2431 | // The segment itself. | |
2432 | Output_segment* segment_; | |
2433 | }; | |
2434 | ||
7d26c6cc ILT |
2435 | // Print for debugging. |
2436 | ||
2437 | void | |
2438 | Phdrs_element::print(FILE* f) const | |
2439 | { | |
2440 | fprintf(f, " %s 0x%x", this->name_.c_str(), this->type_); | |
2441 | if (this->includes_filehdr_) | |
2442 | fprintf(f, " FILEHDR"); | |
2443 | if (this->includes_phdrs_) | |
2444 | fprintf(f, " PHDRS"); | |
2445 | if (this->is_flags_valid_) | |
2446 | fprintf(f, " FLAGS(%u)", this->flags_); | |
2447 | if (this->load_address_ != NULL) | |
2448 | { | |
2449 | fprintf(f, " AT("); | |
2450 | this->load_address_->print(f); | |
2451 | fprintf(f, ")"); | |
2452 | } | |
2453 | fprintf(f, ";\n"); | |
2454 | } | |
2455 | ||
494e05f4 ILT |
2456 | // Class Script_sections. |
2457 | ||
2458 | Script_sections::Script_sections() | |
2459 | : saw_sections_clause_(false), | |
2460 | in_sections_clause_(false), | |
2461 | sections_elements_(NULL), | |
1c4f3631 | 2462 | output_section_(NULL), |
2d924fd9 | 2463 | phdrs_elements_(NULL), |
0d371ad3 ILT |
2464 | orphan_section_placement_(NULL), |
2465 | data_segment_align_start_(), | |
2466 | saw_data_segment_align_(false), | |
2d924fd9 | 2467 | saw_relro_end_(false) |
494e05f4 ILT |
2468 | { |
2469 | } | |
2470 | ||
2471 | // Start a SECTIONS clause. | |
2472 | ||
2473 | void | |
2474 | Script_sections::start_sections() | |
2475 | { | |
2476 | gold_assert(!this->in_sections_clause_ && this->output_section_ == NULL); | |
2477 | this->saw_sections_clause_ = true; | |
2478 | this->in_sections_clause_ = true; | |
2479 | if (this->sections_elements_ == NULL) | |
2480 | this->sections_elements_ = new Sections_elements; | |
2481 | } | |
2482 | ||
2483 | // Finish a SECTIONS clause. | |
2484 | ||
2485 | void | |
2486 | Script_sections::finish_sections() | |
2487 | { | |
2488 | gold_assert(this->in_sections_clause_ && this->output_section_ == NULL); | |
2489 | this->in_sections_clause_ = false; | |
2490 | } | |
2491 | ||
2492 | // Add a symbol to be defined. | |
2493 | ||
2494 | void | |
2495 | Script_sections::add_symbol_assignment(const char* name, size_t length, | |
2496 | Expression* val, bool provide, | |
2497 | bool hidden) | |
2498 | { | |
2499 | if (this->output_section_ != NULL) | |
2500 | this->output_section_->add_symbol_assignment(name, length, val, | |
2501 | provide, hidden); | |
2502 | else | |
2503 | { | |
2504 | Sections_element* p = new Sections_element_assignment(name, length, | |
2505 | val, provide, | |
2506 | hidden); | |
2507 | this->sections_elements_->push_back(p); | |
2508 | } | |
2509 | } | |
2510 | ||
a445fddf ILT |
2511 | // Add an assignment to the special dot symbol. |
2512 | ||
2513 | void | |
2514 | Script_sections::add_dot_assignment(Expression* val) | |
2515 | { | |
2516 | if (this->output_section_ != NULL) | |
2517 | this->output_section_->add_dot_assignment(val); | |
2518 | else | |
2519 | { | |
2520 | Sections_element* p = new Sections_element_dot_assignment(val); | |
2521 | this->sections_elements_->push_back(p); | |
2522 | } | |
2523 | } | |
2524 | ||
494e05f4 ILT |
2525 | // Add an assertion. |
2526 | ||
2527 | void | |
2528 | Script_sections::add_assertion(Expression* check, const char* message, | |
2529 | size_t messagelen) | |
2530 | { | |
2531 | if (this->output_section_ != NULL) | |
2532 | this->output_section_->add_assertion(check, message, messagelen); | |
2533 | else | |
2534 | { | |
2535 | Sections_element* p = new Sections_element_assertion(check, message, | |
2536 | messagelen); | |
2537 | this->sections_elements_->push_back(p); | |
2538 | } | |
2539 | } | |
2540 | ||
2541 | // Start processing entries for an output section. | |
2542 | ||
2543 | void | |
2544 | Script_sections::start_output_section( | |
2545 | const char* name, | |
2546 | size_t namelen, | |
2547 | const Parser_output_section_header *header) | |
2548 | { | |
2549 | Output_section_definition* posd = new Output_section_definition(name, | |
2550 | namelen, | |
2551 | header); | |
2552 | this->sections_elements_->push_back(posd); | |
2553 | gold_assert(this->output_section_ == NULL); | |
2554 | this->output_section_ = posd; | |
2555 | } | |
2556 | ||
2557 | // Stop processing entries for an output section. | |
2558 | ||
2559 | void | |
2560 | Script_sections::finish_output_section( | |
2561 | const Parser_output_section_trailer* trailer) | |
2562 | { | |
2563 | gold_assert(this->output_section_ != NULL); | |
2564 | this->output_section_->finish(trailer); | |
2565 | this->output_section_ = NULL; | |
2566 | } | |
2567 | ||
2568 | // Add a data item to the current output section. | |
2569 | ||
2570 | void | |
2571 | Script_sections::add_data(int size, bool is_signed, Expression* val) | |
2572 | { | |
2573 | gold_assert(this->output_section_ != NULL); | |
2574 | this->output_section_->add_data(size, is_signed, val); | |
2575 | } | |
2576 | ||
2577 | // Add a fill value setting to the current output section. | |
2578 | ||
2579 | void | |
2580 | Script_sections::add_fill(Expression* val) | |
2581 | { | |
2582 | gold_assert(this->output_section_ != NULL); | |
2583 | this->output_section_->add_fill(val); | |
2584 | } | |
2585 | ||
2586 | // Add an input section specification to the current output section. | |
2587 | ||
2588 | void | |
2589 | Script_sections::add_input_section(const Input_section_spec* spec, bool keep) | |
2590 | { | |
2591 | gold_assert(this->output_section_ != NULL); | |
2592 | this->output_section_->add_input_section(spec, keep); | |
2593 | } | |
2594 | ||
2d924fd9 ILT |
2595 | // This is called when we see DATA_SEGMENT_ALIGN. It means that any |
2596 | // subsequent output sections may be relro. | |
2597 | ||
2598 | void | |
2599 | Script_sections::data_segment_align() | |
2600 | { | |
0d371ad3 | 2601 | if (this->saw_data_segment_align_) |
2d924fd9 | 2602 | gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script")); |
0d371ad3 ILT |
2603 | gold_assert(!this->sections_elements_->empty()); |
2604 | Sections_elements::iterator p = this->sections_elements_->end(); | |
2605 | --p; | |
2606 | this->data_segment_align_start_ = p; | |
2607 | this->saw_data_segment_align_ = true; | |
2d924fd9 ILT |
2608 | } |
2609 | ||
2610 | // This is called when we see DATA_SEGMENT_RELRO_END. It means that | |
2611 | // any output sections seen since DATA_SEGMENT_ALIGN are relro. | |
2612 | ||
2613 | void | |
2614 | Script_sections::data_segment_relro_end() | |
2615 | { | |
2616 | if (this->saw_relro_end_) | |
2617 | gold_error(_("DATA_SEGMENT_RELRO_END may only appear once " | |
2618 | "in a linker script")); | |
2619 | this->saw_relro_end_ = true; | |
2620 | ||
0d371ad3 | 2621 | if (!this->saw_data_segment_align_) |
2d924fd9 ILT |
2622 | gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN")); |
2623 | else | |
2624 | { | |
0d371ad3 ILT |
2625 | Sections_elements::iterator p = this->data_segment_align_start_; |
2626 | for (++p; p != this->sections_elements_->end(); ++p) | |
2627 | (*p)->set_is_relro(); | |
2d924fd9 ILT |
2628 | } |
2629 | } | |
2630 | ||
919ed24c ILT |
2631 | // Create any required sections. |
2632 | ||
2633 | void | |
2634 | Script_sections::create_sections(Layout* layout) | |
2635 | { | |
2636 | if (!this->saw_sections_clause_) | |
2637 | return; | |
2638 | for (Sections_elements::iterator p = this->sections_elements_->begin(); | |
2639 | p != this->sections_elements_->end(); | |
2640 | ++p) | |
2641 | (*p)->create_sections(layout); | |
2642 | } | |
2643 | ||
a445fddf ILT |
2644 | // Add any symbols we are defining to the symbol table. |
2645 | ||
2646 | void | |
2647 | Script_sections::add_symbols_to_table(Symbol_table* symtab) | |
2648 | { | |
2649 | if (!this->saw_sections_clause_) | |
2650 | return; | |
2651 | for (Sections_elements::iterator p = this->sections_elements_->begin(); | |
2652 | p != this->sections_elements_->end(); | |
2653 | ++p) | |
2654 | (*p)->add_symbols_to_table(symtab); | |
2655 | } | |
2656 | ||
2657 | // Finalize symbols and check assertions. | |
2658 | ||
2659 | void | |
2660 | Script_sections::finalize_symbols(Symbol_table* symtab, const Layout* layout) | |
2661 | { | |
2662 | if (!this->saw_sections_clause_) | |
2663 | return; | |
a445fddf ILT |
2664 | uint64_t dot_value = 0; |
2665 | for (Sections_elements::iterator p = this->sections_elements_->begin(); | |
2666 | p != this->sections_elements_->end(); | |
2667 | ++p) | |
77e65537 | 2668 | (*p)->finalize_symbols(symtab, layout, &dot_value); |
a445fddf ILT |
2669 | } |
2670 | ||
2671 | // Return the name of the output section to use for an input file name | |
2672 | // and section name. | |
2673 | ||
2674 | const char* | |
2675 | Script_sections::output_section_name(const char* file_name, | |
2676 | const char* section_name, | |
2677 | Output_section*** output_section_slot) | |
2678 | { | |
2679 | for (Sections_elements::const_iterator p = this->sections_elements_->begin(); | |
2680 | p != this->sections_elements_->end(); | |
2681 | ++p) | |
2682 | { | |
2683 | const char* ret = (*p)->output_section_name(file_name, section_name, | |
2684 | output_section_slot); | |
2685 | ||
2686 | if (ret != NULL) | |
2687 | { | |
2688 | // The special name /DISCARD/ means that the input section | |
2689 | // should be discarded. | |
2690 | if (strcmp(ret, "/DISCARD/") == 0) | |
2691 | { | |
2692 | *output_section_slot = NULL; | |
2693 | return NULL; | |
2694 | } | |
2695 | return ret; | |
2696 | } | |
2697 | } | |
2698 | ||
2699 | // If we couldn't find a mapping for the name, the output section | |
2700 | // gets the name of the input section. | |
2701 | ||
2702 | *output_section_slot = NULL; | |
2703 | ||
2704 | return section_name; | |
2705 | } | |
2706 | ||
2707 | // Place a marker for an orphan output section into the SECTIONS | |
2708 | // clause. | |
2709 | ||
2710 | void | |
2711 | Script_sections::place_orphan(Output_section* os) | |
2712 | { | |
0d371ad3 ILT |
2713 | Orphan_section_placement* osp = this->orphan_section_placement_; |
2714 | if (osp == NULL) | |
a445fddf | 2715 | { |
0d371ad3 ILT |
2716 | // Initialize the Orphan_section_placement structure. |
2717 | osp = new Orphan_section_placement(); | |
2718 | for (Sections_elements::iterator p = this->sections_elements_->begin(); | |
2719 | p != this->sections_elements_->end(); | |
2720 | ++p) | |
2721 | (*p)->orphan_section_init(osp, p); | |
2722 | gold_assert(!this->sections_elements_->empty()); | |
2723 | Sections_elements::iterator last = this->sections_elements_->end(); | |
2724 | --last; | |
2725 | osp->last_init(last); | |
2726 | this->orphan_section_placement_ = osp; | |
a445fddf ILT |
2727 | } |
2728 | ||
0d371ad3 | 2729 | Orphan_output_section* orphan = new Orphan_output_section(os); |
2d924fd9 | 2730 | |
0d371ad3 ILT |
2731 | // Look for where to put ORPHAN. |
2732 | Sections_elements::iterator* where; | |
2733 | if (osp->find_place(os, &where)) | |
2734 | { | |
2735 | if ((**where)->is_relro()) | |
2736 | os->set_is_relro(); | |
2737 | else | |
2738 | os->clear_is_relro(); | |
2739 | ||
2740 | // We want to insert ORPHAN after *WHERE, and then update *WHERE | |
2741 | // so that the next one goes after this one. | |
2742 | Sections_elements::iterator p = *where; | |
2743 | gold_assert(p != this->sections_elements_->end()); | |
2744 | ++p; | |
2745 | *where = this->sections_elements_->insert(p, orphan); | |
2746 | } | |
2d924fd9 | 2747 | else |
0d371ad3 ILT |
2748 | { |
2749 | os->clear_is_relro(); | |
2750 | // We don't have a place to put this orphan section. Put it, | |
2751 | // and all other sections like it, at the end, but before the | |
2752 | // sections which always come at the end. | |
2753 | Sections_elements::iterator last = osp->last_place(); | |
2754 | *where = this->sections_elements_->insert(last, orphan); | |
2755 | } | |
a445fddf ILT |
2756 | } |
2757 | ||
2758 | // Set the addresses of all the output sections. Walk through all the | |
2759 | // elements, tracking the dot symbol. Apply assignments which set | |
2760 | // absolute symbol values, in case they are used when setting dot. | |
2761 | // Fill in data statement values. As we find output sections, set the | |
2762 | // address, set the address of all associated input sections, and | |
2763 | // update dot. Return the segment which should hold the file header | |
2764 | // and segment headers, if any. | |
2765 | ||
2766 | Output_segment* | |
2767 | Script_sections::set_section_addresses(Symbol_table* symtab, Layout* layout) | |
2768 | { | |
2769 | gold_assert(this->saw_sections_clause_); | |
2770 | ||
3802b2dd ILT |
2771 | // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain |
2772 | // for our representation. | |
2773 | for (Sections_elements::iterator p = this->sections_elements_->begin(); | |
2774 | p != this->sections_elements_->end(); | |
2775 | ++p) | |
2776 | { | |
2777 | Output_section_definition* posd; | |
2778 | Section_constraint failed_constraint = (*p)->check_constraint(&posd); | |
2779 | if (failed_constraint != CONSTRAINT_NONE) | |
2780 | { | |
2781 | Sections_elements::iterator q; | |
2782 | for (q = this->sections_elements_->begin(); | |
2783 | q != this->sections_elements_->end(); | |
2784 | ++q) | |
2785 | { | |
2786 | if (q != p) | |
2787 | { | |
2788 | if ((*q)->alternate_constraint(posd, failed_constraint)) | |
2789 | break; | |
2790 | } | |
2791 | } | |
2792 | ||
2793 | if (q == this->sections_elements_->end()) | |
2794 | gold_error(_("no matching section constraint")); | |
2795 | } | |
2796 | } | |
2797 | ||
2d924fd9 ILT |
2798 | // Force the alignment of the first TLS section to be the maximum |
2799 | // alignment of all TLS sections. | |
2800 | Output_section* first_tls = NULL; | |
2801 | uint64_t tls_align = 0; | |
2802 | for (Sections_elements::const_iterator p = this->sections_elements_->begin(); | |
2803 | p != this->sections_elements_->end(); | |
2804 | ++p) | |
2805 | { | |
2806 | Output_section *os = (*p)->get_output_section(); | |
2807 | if (os != NULL && (os->flags() & elfcpp::SHF_TLS) != 0) | |
2808 | { | |
2809 | if (first_tls == NULL) | |
2810 | first_tls = os; | |
2811 | if (os->addralign() > tls_align) | |
2812 | tls_align = os->addralign(); | |
2813 | } | |
2814 | } | |
2815 | if (first_tls != NULL) | |
2816 | first_tls->set_addralign(tls_align); | |
2817 | ||
77e65537 | 2818 | // For a relocatable link, we implicitly set dot to zero. |
a445fddf | 2819 | uint64_t dot_value = 0; |
fd247bfe | 2820 | uint64_t load_address = 0; |
a445fddf ILT |
2821 | for (Sections_elements::iterator p = this->sections_elements_->begin(); |
2822 | p != this->sections_elements_->end(); | |
2823 | ++p) | |
fd247bfe | 2824 | (*p)->set_section_addresses(symtab, layout, &dot_value, &load_address); |
a445fddf | 2825 | |
1c4f3631 ILT |
2826 | if (this->phdrs_elements_ != NULL) |
2827 | { | |
2828 | for (Phdrs_elements::iterator p = this->phdrs_elements_->begin(); | |
2829 | p != this->phdrs_elements_->end(); | |
2830 | ++p) | |
2831 | (*p)->eval_load_address(symtab, layout); | |
2832 | } | |
2833 | ||
a445fddf ILT |
2834 | return this->create_segments(layout); |
2835 | } | |
2836 | ||
2837 | // Sort the sections in order to put them into segments. | |
2838 | ||
2839 | class Sort_output_sections | |
2840 | { | |
2841 | public: | |
2842 | bool | |
2843 | operator()(const Output_section* os1, const Output_section* os2) const; | |
2844 | }; | |
2845 | ||
2846 | bool | |
2847 | Sort_output_sections::operator()(const Output_section* os1, | |
2848 | const Output_section* os2) const | |
2849 | { | |
2850 | // Sort first by the load address. | |
2851 | uint64_t lma1 = (os1->has_load_address() | |
2852 | ? os1->load_address() | |
2853 | : os1->address()); | |
2854 | uint64_t lma2 = (os2->has_load_address() | |
2855 | ? os2->load_address() | |
2856 | : os2->address()); | |
2857 | if (lma1 != lma2) | |
2858 | return lma1 < lma2; | |
2859 | ||
2860 | // Then sort by the virtual address. | |
2861 | if (os1->address() != os2->address()) | |
2862 | return os1->address() < os2->address(); | |
2863 | ||
2864 | // Sort TLS sections to the end. | |
2865 | bool tls1 = (os1->flags() & elfcpp::SHF_TLS) != 0; | |
2866 | bool tls2 = (os2->flags() & elfcpp::SHF_TLS) != 0; | |
2867 | if (tls1 != tls2) | |
2868 | return tls2; | |
2869 | ||
2870 | // Sort PROGBITS before NOBITS. | |
2871 | if (os1->type() == elfcpp::SHT_PROGBITS && os2->type() == elfcpp::SHT_NOBITS) | |
2872 | return true; | |
2873 | if (os1->type() == elfcpp::SHT_NOBITS && os2->type() == elfcpp::SHT_PROGBITS) | |
2874 | return false; | |
2875 | ||
2876 | // Otherwise we don't care. | |
2877 | return false; | |
2878 | } | |
2879 | ||
2880 | // Return whether OS is a BSS section. This is a SHT_NOBITS section. | |
2881 | // We treat a section with the SHF_TLS flag set as taking up space | |
2882 | // even if it is SHT_NOBITS (this is true of .tbss), as we allocate | |
2883 | // space for them in the file. | |
2884 | ||
2885 | bool | |
2886 | Script_sections::is_bss_section(const Output_section* os) | |
2887 | { | |
2888 | return (os->type() == elfcpp::SHT_NOBITS | |
2889 | && (os->flags() & elfcpp::SHF_TLS) == 0); | |
2890 | } | |
2891 | ||
1c4f3631 ILT |
2892 | // Return the size taken by the file header and the program headers. |
2893 | ||
2894 | size_t | |
2895 | Script_sections::total_header_size(Layout* layout) const | |
2896 | { | |
2897 | size_t segment_count = layout->segment_count(); | |
2898 | size_t file_header_size; | |
2899 | size_t segment_headers_size; | |
8851ecca | 2900 | if (parameters->target().get_size() == 32) |
1c4f3631 ILT |
2901 | { |
2902 | file_header_size = elfcpp::Elf_sizes<32>::ehdr_size; | |
2903 | segment_headers_size = segment_count * elfcpp::Elf_sizes<32>::phdr_size; | |
2904 | } | |
8851ecca | 2905 | else if (parameters->target().get_size() == 64) |
1c4f3631 ILT |
2906 | { |
2907 | file_header_size = elfcpp::Elf_sizes<64>::ehdr_size; | |
2908 | segment_headers_size = segment_count * elfcpp::Elf_sizes<64>::phdr_size; | |
2909 | } | |
2910 | else | |
2911 | gold_unreachable(); | |
2912 | ||
2913 | return file_header_size + segment_headers_size; | |
2914 | } | |
2915 | ||
2916 | // Return the amount we have to subtract from the LMA to accomodate | |
2917 | // headers of the given size. The complication is that the file | |
2918 | // header have to be at the start of a page, as otherwise it will not | |
2919 | // be at the start of the file. | |
2920 | ||
2921 | uint64_t | |
2922 | Script_sections::header_size_adjustment(uint64_t lma, | |
2923 | size_t sizeof_headers) const | |
2924 | { | |
8851ecca | 2925 | const uint64_t abi_pagesize = parameters->target().abi_pagesize(); |
1c4f3631 ILT |
2926 | uint64_t hdr_lma = lma - sizeof_headers; |
2927 | hdr_lma &= ~(abi_pagesize - 1); | |
2928 | return lma - hdr_lma; | |
2929 | } | |
2930 | ||
a445fddf ILT |
2931 | // Create the PT_LOAD segments when using a SECTIONS clause. Returns |
2932 | // the segment which should hold the file header and segment headers, | |
2933 | // if any. | |
2934 | ||
2935 | Output_segment* | |
2936 | Script_sections::create_segments(Layout* layout) | |
2937 | { | |
2938 | gold_assert(this->saw_sections_clause_); | |
2939 | ||
8851ecca | 2940 | if (parameters->options().relocatable()) |
a445fddf ILT |
2941 | return NULL; |
2942 | ||
1c4f3631 ILT |
2943 | if (this->saw_phdrs_clause()) |
2944 | return create_segments_from_phdrs_clause(layout); | |
2945 | ||
a445fddf ILT |
2946 | Layout::Section_list sections; |
2947 | layout->get_allocated_sections(§ions); | |
2948 | ||
2949 | // Sort the sections by address. | |
2950 | std::stable_sort(sections.begin(), sections.end(), Sort_output_sections()); | |
2951 | ||
2952 | this->create_note_and_tls_segments(layout, §ions); | |
2953 | ||
2954 | // Walk through the sections adding them to PT_LOAD segments. | |
8851ecca | 2955 | const uint64_t abi_pagesize = parameters->target().abi_pagesize(); |
a445fddf ILT |
2956 | Output_segment* first_seg = NULL; |
2957 | Output_segment* current_seg = NULL; | |
2958 | bool is_current_seg_readonly = true; | |
2959 | Layout::Section_list::iterator plast = sections.end(); | |
2960 | uint64_t last_vma = 0; | |
2961 | uint64_t last_lma = 0; | |
2962 | uint64_t last_size = 0; | |
2963 | for (Layout::Section_list::iterator p = sections.begin(); | |
2964 | p != sections.end(); | |
2965 | ++p) | |
2966 | { | |
2967 | const uint64_t vma = (*p)->address(); | |
2968 | const uint64_t lma = ((*p)->has_load_address() | |
2969 | ? (*p)->load_address() | |
2970 | : vma); | |
2971 | const uint64_t size = (*p)->current_data_size(); | |
2972 | ||
2973 | bool need_new_segment; | |
2974 | if (current_seg == NULL) | |
2975 | need_new_segment = true; | |
2976 | else if (lma - vma != last_lma - last_vma) | |
2977 | { | |
2978 | // This section has a different LMA relationship than the | |
2979 | // last one; we need a new segment. | |
2980 | need_new_segment = true; | |
2981 | } | |
2982 | else if (align_address(last_lma + last_size, abi_pagesize) | |
2983 | < align_address(lma, abi_pagesize)) | |
2984 | { | |
2985 | // Putting this section in the segment would require | |
2986 | // skipping a page. | |
2987 | need_new_segment = true; | |
2988 | } | |
2989 | else if (is_bss_section(*plast) && !is_bss_section(*p)) | |
2990 | { | |
2991 | // A non-BSS section can not follow a BSS section in the | |
2992 | // same segment. | |
2993 | need_new_segment = true; | |
2994 | } | |
2995 | else if (is_current_seg_readonly | |
af6156ef ILT |
2996 | && ((*p)->flags() & elfcpp::SHF_WRITE) != 0 |
2997 | && !parameters->options().omagic()) | |
a445fddf ILT |
2998 | { |
2999 | // Don't put a writable section in the same segment as a | |
3000 | // non-writable section. | |
3001 | need_new_segment = true; | |
3002 | } | |
3003 | else | |
3004 | { | |
3005 | // Otherwise, reuse the existing segment. | |
3006 | need_new_segment = false; | |
3007 | } | |
3008 | ||
3009 | elfcpp::Elf_Word seg_flags = | |
3010 | Layout::section_flags_to_segment((*p)->flags()); | |
3011 | ||
3012 | if (need_new_segment) | |
3013 | { | |
3014 | current_seg = layout->make_output_segment(elfcpp::PT_LOAD, | |
3015 | seg_flags); | |
3016 | current_seg->set_addresses(vma, lma); | |
3017 | if (first_seg == NULL) | |
3018 | first_seg = current_seg; | |
3019 | is_current_seg_readonly = true; | |
3020 | } | |
3021 | ||
3022 | current_seg->add_output_section(*p, seg_flags); | |
3023 | ||
3024 | if (((*p)->flags() & elfcpp::SHF_WRITE) != 0) | |
3025 | is_current_seg_readonly = false; | |
3026 | ||
3027 | plast = p; | |
3028 | last_vma = vma; | |
3029 | last_lma = lma; | |
3030 | last_size = size; | |
3031 | } | |
3032 | ||
3033 | // An ELF program should work even if the program headers are not in | |
3034 | // a PT_LOAD segment. However, it appears that the Linux kernel | |
3035 | // does not set the AT_PHDR auxiliary entry in that case. It sets | |
3036 | // the load address to p_vaddr - p_offset of the first PT_LOAD | |
3037 | // segment. It then sets AT_PHDR to the load address plus the | |
3038 | // offset to the program headers, e_phoff in the file header. This | |
3039 | // fails when the program headers appear in the file before the | |
3040 | // first PT_LOAD segment. Therefore, we always create a PT_LOAD | |
3041 | // segment to hold the file header and the program headers. This is | |
3042 | // effectively what the GNU linker does, and it is slightly more | |
3043 | // efficient in any case. We try to use the first PT_LOAD segment | |
3044 | // if we can, otherwise we make a new one. | |
3045 | ||
919ed24c ILT |
3046 | if (first_seg == NULL) |
3047 | return NULL; | |
3048 | ||
3ee173de ILT |
3049 | // -n or -N mean that the program is not demand paged and there is |
3050 | // no need to put the program headers in a PT_LOAD segment. | |
3051 | if (parameters->options().nmagic() || parameters->options().omagic()) | |
3052 | return NULL; | |
3053 | ||
1c4f3631 | 3054 | size_t sizeof_headers = this->total_header_size(layout); |
3802b2dd | 3055 | |
919ed24c ILT |
3056 | uint64_t vma = first_seg->vaddr(); |
3057 | uint64_t lma = first_seg->paddr(); | |
3058 | ||
3059 | uint64_t subtract = this->header_size_adjustment(lma, sizeof_headers); | |
3060 | ||
e6188289 ILT |
3061 | if ((lma & (abi_pagesize - 1)) >= sizeof_headers) |
3062 | { | |
3063 | first_seg->set_addresses(vma - subtract, lma - subtract); | |
3064 | return first_seg; | |
3065 | } | |
3066 | ||
919ed24c ILT |
3067 | // If there is no room to squeeze in the headers, then punt. The |
3068 | // resulting executable probably won't run on GNU/Linux, but we | |
3069 | // trust that the user knows what they are doing. | |
3070 | if (lma < subtract || vma < subtract) | |
3071 | return NULL; | |
3072 | ||
a445fddf ILT |
3073 | Output_segment* load_seg = layout->make_output_segment(elfcpp::PT_LOAD, |
3074 | elfcpp::PF_R); | |
919ed24c | 3075 | load_seg->set_addresses(vma - subtract, lma - subtract); |
a445fddf ILT |
3076 | |
3077 | return load_seg; | |
3078 | } | |
3079 | ||
3080 | // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS | |
3081 | // segment if there are any SHT_TLS sections. | |
3082 | ||
3083 | void | |
3084 | Script_sections::create_note_and_tls_segments( | |
3085 | Layout* layout, | |
3086 | const Layout::Section_list* sections) | |
3087 | { | |
1c4f3631 ILT |
3088 | gold_assert(!this->saw_phdrs_clause()); |
3089 | ||
a445fddf ILT |
3090 | bool saw_tls = false; |
3091 | for (Layout::Section_list::const_iterator p = sections->begin(); | |
3092 | p != sections->end(); | |
3093 | ++p) | |
3094 | { | |
3095 | if ((*p)->type() == elfcpp::SHT_NOTE) | |
3096 | { | |
3097 | elfcpp::Elf_Word seg_flags = | |
3098 | Layout::section_flags_to_segment((*p)->flags()); | |
3099 | Output_segment* oseg = layout->make_output_segment(elfcpp::PT_NOTE, | |
3100 | seg_flags); | |
3101 | oseg->add_output_section(*p, seg_flags); | |
3102 | ||
3103 | // Incorporate any subsequent SHT_NOTE sections, in the | |
3104 | // hopes that the script is sensible. | |
3105 | Layout::Section_list::const_iterator pnext = p + 1; | |
3106 | while (pnext != sections->end() | |
3107 | && (*pnext)->type() == elfcpp::SHT_NOTE) | |
3108 | { | |
3109 | seg_flags = Layout::section_flags_to_segment((*pnext)->flags()); | |
3110 | oseg->add_output_section(*pnext, seg_flags); | |
3111 | p = pnext; | |
3112 | ++pnext; | |
3113 | } | |
3114 | } | |
3115 | ||
3116 | if (((*p)->flags() & elfcpp::SHF_TLS) != 0) | |
3117 | { | |
3118 | if (saw_tls) | |
3119 | gold_error(_("TLS sections are not adjacent")); | |
3120 | ||
3121 | elfcpp::Elf_Word seg_flags = | |
3122 | Layout::section_flags_to_segment((*p)->flags()); | |
3123 | Output_segment* oseg = layout->make_output_segment(elfcpp::PT_TLS, | |
3124 | seg_flags); | |
3125 | oseg->add_output_section(*p, seg_flags); | |
3126 | ||
3127 | Layout::Section_list::const_iterator pnext = p + 1; | |
3128 | while (pnext != sections->end() | |
3129 | && ((*pnext)->flags() & elfcpp::SHF_TLS) != 0) | |
3130 | { | |
3131 | seg_flags = Layout::section_flags_to_segment((*pnext)->flags()); | |
3132 | oseg->add_output_section(*pnext, seg_flags); | |
3133 | p = pnext; | |
3134 | ++pnext; | |
3135 | } | |
3136 | ||
3137 | saw_tls = true; | |
3138 | } | |
3139 | } | |
3140 | } | |
3141 | ||
1c4f3631 ILT |
3142 | // Add a program header. The PHDRS clause is syntactically distinct |
3143 | // from the SECTIONS clause, but we implement it with the SECTIONS | |
55458500 | 3144 | // support because PHDRS is useless if there is no SECTIONS clause. |
1c4f3631 ILT |
3145 | |
3146 | void | |
3147 | Script_sections::add_phdr(const char* name, size_t namelen, unsigned int type, | |
3148 | bool includes_filehdr, bool includes_phdrs, | |
3149 | bool is_flags_valid, unsigned int flags, | |
3150 | Expression* load_address) | |
3151 | { | |
3152 | if (this->phdrs_elements_ == NULL) | |
3153 | this->phdrs_elements_ = new Phdrs_elements(); | |
3154 | this->phdrs_elements_->push_back(new Phdrs_element(name, namelen, type, | |
3155 | includes_filehdr, | |
3156 | includes_phdrs, | |
3157 | is_flags_valid, flags, | |
3158 | load_address)); | |
3159 | } | |
3160 | ||
3802b2dd ILT |
3161 | // Return the number of segments we expect to create based on the |
3162 | // SECTIONS clause. This is used to implement SIZEOF_HEADERS. | |
3163 | ||
3164 | size_t | |
3165 | Script_sections::expected_segment_count(const Layout* layout) const | |
3166 | { | |
1c4f3631 ILT |
3167 | if (this->saw_phdrs_clause()) |
3168 | return this->phdrs_elements_->size(); | |
3169 | ||
3802b2dd ILT |
3170 | Layout::Section_list sections; |
3171 | layout->get_allocated_sections(§ions); | |
3172 | ||
3173 | // We assume that we will need two PT_LOAD segments. | |
3174 | size_t ret = 2; | |
3175 | ||
3176 | bool saw_note = false; | |
3177 | bool saw_tls = false; | |
3178 | for (Layout::Section_list::const_iterator p = sections.begin(); | |
3179 | p != sections.end(); | |
3180 | ++p) | |
3181 | { | |
3182 | if ((*p)->type() == elfcpp::SHT_NOTE) | |
3183 | { | |
3184 | // Assume that all note sections will fit into a single | |
3185 | // PT_NOTE segment. | |
3186 | if (!saw_note) | |
3187 | { | |
3188 | ++ret; | |
3189 | saw_note = true; | |
3190 | } | |
3191 | } | |
3192 | else if (((*p)->flags() & elfcpp::SHF_TLS) != 0) | |
3193 | { | |
3194 | // There can only be one PT_TLS segment. | |
3195 | if (!saw_tls) | |
3196 | { | |
3197 | ++ret; | |
3198 | saw_tls = true; | |
3199 | } | |
3200 | } | |
3201 | } | |
3202 | ||
3203 | return ret; | |
3204 | } | |
3205 | ||
1c4f3631 ILT |
3206 | // Create the segments from a PHDRS clause. Return the segment which |
3207 | // should hold the file header and program headers, if any. | |
3208 | ||
3209 | Output_segment* | |
3210 | Script_sections::create_segments_from_phdrs_clause(Layout* layout) | |
3211 | { | |
3212 | this->attach_sections_using_phdrs_clause(layout); | |
3213 | return this->set_phdrs_clause_addresses(layout); | |
3214 | } | |
3215 | ||
3216 | // Create the segments from the PHDRS clause, and put the output | |
3217 | // sections in them. | |
3218 | ||
3219 | void | |
3220 | Script_sections::attach_sections_using_phdrs_clause(Layout* layout) | |
3221 | { | |
3222 | typedef std::map<std::string, Output_segment*> Name_to_segment; | |
3223 | Name_to_segment name_to_segment; | |
3224 | for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin(); | |
3225 | p != this->phdrs_elements_->end(); | |
3226 | ++p) | |
3227 | name_to_segment[(*p)->name()] = (*p)->create_segment(layout); | |
3228 | ||
3229 | // Walk through the output sections and attach them to segments. | |
3230 | // Output sections in the script which do not list segments are | |
3231 | // attached to the same set of segments as the immediately preceding | |
3232 | // output section. | |
20e6d0d6 | 3233 | |
1c4f3631 | 3234 | String_list* phdr_names = NULL; |
20e6d0d6 | 3235 | bool load_segments_only = false; |
1c4f3631 ILT |
3236 | for (Sections_elements::const_iterator p = this->sections_elements_->begin(); |
3237 | p != this->sections_elements_->end(); | |
3238 | ++p) | |
3239 | { | |
2cefc357 | 3240 | bool orphan; |
20e6d0d6 | 3241 | String_list* old_phdr_names = phdr_names; |
2cefc357 | 3242 | Output_section* os = (*p)->allocate_to_segment(&phdr_names, &orphan); |
1c4f3631 ILT |
3243 | if (os == NULL) |
3244 | continue; | |
3245 | ||
3246 | if (phdr_names == NULL) | |
3247 | { | |
3248 | gold_error(_("allocated section not in any segment")); | |
3249 | continue; | |
3250 | } | |
3251 | ||
20e6d0d6 DK |
3252 | // We see a list of segments names. Disable PT_LOAD segment only |
3253 | // filtering. | |
3254 | if (old_phdr_names != phdr_names) | |
3255 | load_segments_only = false; | |
3256 | ||
2cefc357 ILT |
3257 | // If this is an orphan section--one that was not explicitly |
3258 | // mentioned in the linker script--then it should not inherit | |
3259 | // any segment type other than PT_LOAD. Otherwise, e.g., the | |
3260 | // PT_INTERP segment will pick up following orphan sections, | |
3261 | // which does not make sense. If this is not an orphan section, | |
3262 | // we trust the linker script. | |
3263 | if (orphan) | |
3264 | { | |
20e6d0d6 DK |
3265 | // Enable PT_LOAD segments only filtering until we see another |
3266 | // list of segment names. | |
3267 | load_segments_only = true; | |
2cefc357 ILT |
3268 | } |
3269 | ||
1c4f3631 ILT |
3270 | bool in_load_segment = false; |
3271 | for (String_list::const_iterator q = phdr_names->begin(); | |
3272 | q != phdr_names->end(); | |
3273 | ++q) | |
3274 | { | |
3275 | Name_to_segment::const_iterator r = name_to_segment.find(*q); | |
3276 | if (r == name_to_segment.end()) | |
3277 | gold_error(_("no segment %s"), q->c_str()); | |
3278 | else | |
3279 | { | |
20e6d0d6 DK |
3280 | if (load_segments_only |
3281 | && r->second->type() != elfcpp::PT_LOAD) | |
3282 | continue; | |
3283 | ||
1c4f3631 ILT |
3284 | elfcpp::Elf_Word seg_flags = |
3285 | Layout::section_flags_to_segment(os->flags()); | |
3286 | r->second->add_output_section(os, seg_flags); | |
3287 | ||
3288 | if (r->second->type() == elfcpp::PT_LOAD) | |
3289 | { | |
3290 | if (in_load_segment) | |
3291 | gold_error(_("section in two PT_LOAD segments")); | |
3292 | in_load_segment = true; | |
3293 | } | |
3294 | } | |
3295 | } | |
3296 | ||
3297 | if (!in_load_segment) | |
3298 | gold_error(_("allocated section not in any PT_LOAD segment")); | |
3299 | } | |
3300 | } | |
3301 | ||
3302 | // Set the addresses for segments created from a PHDRS clause. Return | |
3303 | // the segment which should hold the file header and program headers, | |
3304 | // if any. | |
3305 | ||
3306 | Output_segment* | |
3307 | Script_sections::set_phdrs_clause_addresses(Layout* layout) | |
3308 | { | |
3309 | Output_segment* load_seg = NULL; | |
3310 | for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin(); | |
3311 | p != this->phdrs_elements_->end(); | |
3312 | ++p) | |
3313 | { | |
3314 | // Note that we have to set the flags after adding the output | |
3315 | // sections to the segment, as adding an output segment can | |
3316 | // change the flags. | |
3317 | (*p)->set_flags_if_valid(); | |
3318 | ||
3319 | Output_segment* oseg = (*p)->segment(); | |
3320 | ||
3321 | if (oseg->type() != elfcpp::PT_LOAD) | |
3322 | { | |
3323 | // The addresses of non-PT_LOAD segments are set from the | |
3324 | // PT_LOAD segments. | |
3325 | if ((*p)->has_load_address()) | |
3326 | gold_error(_("may only specify load address for PT_LOAD segment")); | |
3327 | continue; | |
3328 | } | |
3329 | ||
3330 | // The output sections should have addresses from the SECTIONS | |
3331 | // clause. The addresses don't have to be in order, so find the | |
3332 | // one with the lowest load address. Use that to set the | |
3333 | // address of the segment. | |
3334 | ||
3335 | Output_section* osec = oseg->section_with_lowest_load_address(); | |
3336 | if (osec == NULL) | |
3337 | { | |
3338 | oseg->set_addresses(0, 0); | |
3339 | continue; | |
3340 | } | |
3341 | ||
3342 | uint64_t vma = osec->address(); | |
3343 | uint64_t lma = osec->has_load_address() ? osec->load_address() : vma; | |
3344 | ||
3345 | // Override the load address of the section with the load | |
3346 | // address specified for the segment. | |
3347 | if ((*p)->has_load_address()) | |
3348 | { | |
3349 | if (osec->has_load_address()) | |
3350 | gold_warning(_("PHDRS load address overrides " | |
3351 | "section %s load address"), | |
3352 | osec->name()); | |
3353 | ||
3354 | lma = (*p)->load_address(); | |
3355 | } | |
3356 | ||
3357 | bool headers = (*p)->includes_filehdr() && (*p)->includes_phdrs(); | |
3358 | if (!headers && ((*p)->includes_filehdr() || (*p)->includes_phdrs())) | |
3359 | { | |
3360 | // We could support this if we wanted to. | |
3361 | gold_error(_("using only one of FILEHDR and PHDRS is " | |
3362 | "not currently supported")); | |
3363 | } | |
3364 | if (headers) | |
3365 | { | |
3366 | size_t sizeof_headers = this->total_header_size(layout); | |
3367 | uint64_t subtract = this->header_size_adjustment(lma, | |
3368 | sizeof_headers); | |
3369 | if (lma >= subtract && vma >= subtract) | |
3370 | { | |
3371 | lma -= subtract; | |
3372 | vma -= subtract; | |
3373 | } | |
3374 | else | |
3375 | { | |
3376 | gold_error(_("sections loaded on first page without room " | |
3377 | "for file and program headers " | |
3378 | "are not supported")); | |
3379 | } | |
3380 | ||
3381 | if (load_seg != NULL) | |
3382 | gold_error(_("using FILEHDR and PHDRS on more than one " | |
3383 | "PT_LOAD segment is not currently supported")); | |
3384 | load_seg = oseg; | |
3385 | } | |
3386 | ||
3387 | oseg->set_addresses(vma, lma); | |
3388 | } | |
3389 | ||
3390 | return load_seg; | |
3391 | } | |
3392 | ||
3393 | // Add the file header and segment headers to non-load segments | |
3394 | // specified in the PHDRS clause. | |
3395 | ||
3396 | void | |
3397 | Script_sections::put_headers_in_phdrs(Output_data* file_header, | |
3398 | Output_data* segment_headers) | |
3399 | { | |
3400 | gold_assert(this->saw_phdrs_clause()); | |
3401 | for (Phdrs_elements::iterator p = this->phdrs_elements_->begin(); | |
3402 | p != this->phdrs_elements_->end(); | |
3403 | ++p) | |
3404 | { | |
3405 | if ((*p)->type() != elfcpp::PT_LOAD) | |
3406 | { | |
3407 | if ((*p)->includes_phdrs()) | |
3408 | (*p)->segment()->add_initial_output_data(segment_headers); | |
3409 | if ((*p)->includes_filehdr()) | |
3410 | (*p)->segment()->add_initial_output_data(file_header); | |
3411 | } | |
3412 | } | |
3413 | } | |
3414 | ||
8f2eb564 ILT |
3415 | // Look for an output section by name and return the address, the load |
3416 | // address, the alignment, and the size. This is used when an | |
3417 | // expression refers to an output section which was not actually | |
3418 | // created. This returns true if the section was found, false | |
3419 | // otherwise. | |
3420 | ||
3421 | bool | |
3422 | Script_sections::get_output_section_info(const char* name, uint64_t* address, | |
3423 | uint64_t* load_address, | |
3424 | uint64_t* addralign, | |
3425 | uint64_t* size) const | |
3426 | { | |
3427 | if (!this->saw_sections_clause_) | |
3428 | return false; | |
3429 | for (Sections_elements::const_iterator p = this->sections_elements_->begin(); | |
3430 | p != this->sections_elements_->end(); | |
3431 | ++p) | |
3432 | if ((*p)->get_output_section_info(name, address, load_address, addralign, | |
3433 | size)) | |
3434 | return true; | |
3435 | return false; | |
3436 | } | |
3437 | ||
20e6d0d6 DK |
3438 | // Release all Output_segments. This remove all pointers to all |
3439 | // Output_segments. | |
3440 | ||
3441 | void | |
3442 | Script_sections::release_segments() | |
3443 | { | |
3444 | if (this->saw_phdrs_clause()) | |
3445 | { | |
3446 | for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin(); | |
3447 | p != this->phdrs_elements_->end(); | |
3448 | ++p) | |
3449 | (*p)->release_segment(); | |
3450 | } | |
3451 | } | |
3452 | ||
494e05f4 ILT |
3453 | // Print the SECTIONS clause to F for debugging. |
3454 | ||
3455 | void | |
3456 | Script_sections::print(FILE* f) const | |
3457 | { | |
3458 | if (!this->saw_sections_clause_) | |
3459 | return; | |
3460 | ||
3461 | fprintf(f, "SECTIONS {\n"); | |
3462 | ||
3463 | for (Sections_elements::const_iterator p = this->sections_elements_->begin(); | |
3464 | p != this->sections_elements_->end(); | |
3465 | ++p) | |
3466 | (*p)->print(f); | |
3467 | ||
3468 | fprintf(f, "}\n"); | |
7d26c6cc ILT |
3469 | |
3470 | if (this->phdrs_elements_ != NULL) | |
3471 | { | |
3472 | fprintf(f, "PHDRS {\n"); | |
3473 | for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin(); | |
3474 | p != this->phdrs_elements_->end(); | |
3475 | ++p) | |
3476 | (*p)->print(f); | |
3477 | fprintf(f, "}\n"); | |
3478 | } | |
494e05f4 ILT |
3479 | } |
3480 | ||
3481 | } // End namespace gold. |