Commit | Line | Data |
---|---|---|
494e05f4 ILT |
1 | // script-sections.cc -- linker script SECTIONS for gold |
2 | ||
3 | // Copyright 2008 Free Software Foundation, Inc. | |
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> | |
494e05f4 ILT |
28 | #include <string> |
29 | #include <vector> | |
a445fddf | 30 | #include <fnmatch.h> |
494e05f4 | 31 | |
a445fddf ILT |
32 | #include "parameters.h" |
33 | #include "object.h" | |
34 | #include "layout.h" | |
35 | #include "output.h" | |
494e05f4 ILT |
36 | #include "script-c.h" |
37 | #include "script.h" | |
38 | #include "script-sections.h" | |
39 | ||
40 | // Support for the SECTIONS clause in linker scripts. | |
41 | ||
42 | namespace gold | |
43 | { | |
44 | ||
45 | // An element in a SECTIONS clause. | |
46 | ||
47 | class Sections_element | |
48 | { | |
49 | public: | |
50 | Sections_element() | |
51 | { } | |
52 | ||
53 | virtual ~Sections_element() | |
54 | { } | |
55 | ||
a445fddf ILT |
56 | // Add any symbol being defined to the symbol table. |
57 | virtual void | |
58 | add_symbols_to_table(Symbol_table*) | |
59 | { } | |
60 | ||
61 | // Finalize symbols and check assertions. | |
62 | virtual void | |
63 | finalize_symbols(Symbol_table*, const Layout*, bool*, uint64_t*) | |
64 | { } | |
65 | ||
66 | // Return the output section name to use for an input file name and | |
67 | // section name. This only real implementation is in | |
68 | // Output_section_definition. | |
69 | virtual const char* | |
70 | output_section_name(const char*, const char*, Output_section***) | |
71 | { return NULL; } | |
72 | ||
73 | // Return whether to place an orphan output section after this | |
74 | // element. | |
75 | virtual bool | |
76 | place_orphan_here(const Output_section *, bool*) const | |
77 | { return false; } | |
78 | ||
79 | // Set section addresses. This includes applying assignments if the | |
80 | // the expression is an absolute value. | |
81 | virtual void | |
82 | set_section_addresses(Symbol_table*, Layout*, bool*, uint64_t*) | |
83 | { } | |
84 | ||
85 | // Print the element for debugging purposes. | |
494e05f4 ILT |
86 | virtual void |
87 | print(FILE* f) const = 0; | |
88 | }; | |
89 | ||
90 | // An assignment in a SECTIONS clause outside of an output section. | |
91 | ||
92 | class Sections_element_assignment : public Sections_element | |
93 | { | |
94 | public: | |
95 | Sections_element_assignment(const char* name, size_t namelen, | |
96 | Expression* val, bool provide, bool hidden) | |
97 | : assignment_(name, namelen, val, provide, hidden) | |
98 | { } | |
99 | ||
a445fddf ILT |
100 | // Add the symbol to the symbol table. |
101 | void | |
102 | add_symbols_to_table(Symbol_table* symtab) | |
103 | { this->assignment_.add_to_table(symtab); } | |
104 | ||
105 | // Finalize the symbol. | |
106 | void | |
107 | finalize_symbols(Symbol_table* symtab, const Layout* layout, | |
108 | bool* dot_has_value, uint64_t* dot_value) | |
109 | { | |
110 | this->assignment_.finalize_with_dot(symtab, layout, *dot_has_value, | |
111 | *dot_value); | |
112 | } | |
113 | ||
114 | // Set the section address. There is no section here, but if the | |
115 | // value is absolute, we set the symbol. This permits us to use | |
116 | // absolute symbols when setting dot. | |
117 | void | |
118 | set_section_addresses(Symbol_table* symtab, Layout* layout, | |
119 | bool* dot_has_value, uint64_t* dot_value) | |
120 | { | |
121 | this->assignment_.set_if_absolute(symtab, layout, true, *dot_has_value, | |
122 | *dot_value); | |
123 | } | |
124 | ||
125 | // Print for debugging. | |
494e05f4 ILT |
126 | void |
127 | print(FILE* f) const | |
128 | { | |
129 | fprintf(f, " "); | |
130 | this->assignment_.print(f); | |
131 | } | |
132 | ||
133 | private: | |
134 | Symbol_assignment assignment_; | |
135 | }; | |
136 | ||
a445fddf ILT |
137 | // An assignment to the dot symbol in a SECTIONS clause outside of an |
138 | // output section. | |
139 | ||
140 | class Sections_element_dot_assignment : public Sections_element | |
141 | { | |
142 | public: | |
143 | Sections_element_dot_assignment(Expression* val) | |
144 | : val_(val) | |
145 | { } | |
146 | ||
147 | // Finalize the symbol. | |
148 | void | |
149 | finalize_symbols(Symbol_table* symtab, const Layout* layout, | |
150 | bool* dot_has_value, uint64_t* dot_value) | |
151 | { | |
152 | bool dummy; | |
153 | *dot_value = this->val_->eval_with_dot(symtab, layout, *dot_has_value, | |
154 | *dot_value, &dummy); | |
155 | *dot_has_value = true; | |
156 | } | |
157 | ||
158 | // Update the dot symbol while setting section addresses. | |
159 | void | |
160 | set_section_addresses(Symbol_table* symtab, Layout* layout, | |
161 | bool* dot_has_value, uint64_t* dot_value) | |
162 | { | |
163 | bool is_absolute; | |
164 | *dot_value = this->val_->eval_with_dot(symtab, layout, *dot_has_value, | |
165 | *dot_value, &is_absolute); | |
166 | if (!is_absolute) | |
167 | gold_error(_("dot set to non-absolute value")); | |
168 | *dot_has_value = true; | |
169 | } | |
170 | ||
171 | // Print for debugging. | |
172 | void | |
173 | print(FILE* f) const | |
174 | { | |
175 | fprintf(f, " . = "); | |
176 | this->val_->print(f); | |
177 | fprintf(f, "\n"); | |
178 | } | |
179 | ||
180 | private: | |
181 | Expression* val_; | |
182 | }; | |
183 | ||
494e05f4 ILT |
184 | // An assertion in a SECTIONS clause outside of an output section. |
185 | ||
186 | class Sections_element_assertion : public Sections_element | |
187 | { | |
188 | public: | |
189 | Sections_element_assertion(Expression* check, const char* message, | |
190 | size_t messagelen) | |
191 | : assertion_(check, message, messagelen) | |
192 | { } | |
193 | ||
a445fddf ILT |
194 | // Check the assertion. |
195 | void | |
196 | finalize_symbols(Symbol_table* symtab, const Layout* layout, bool*, | |
197 | uint64_t*) | |
198 | { this->assertion_.check(symtab, layout); } | |
199 | ||
200 | // Print for debugging. | |
494e05f4 ILT |
201 | void |
202 | print(FILE* f) const | |
203 | { | |
204 | fprintf(f, " "); | |
205 | this->assertion_.print(f); | |
206 | } | |
207 | ||
208 | private: | |
209 | Script_assertion assertion_; | |
210 | }; | |
211 | ||
212 | // An element in an output section in a SECTIONS clause. | |
213 | ||
214 | class Output_section_element | |
215 | { | |
216 | public: | |
a445fddf ILT |
217 | // A list of input sections. |
218 | typedef std::list<std::pair<Relobj*, unsigned int> > Input_section_list; | |
219 | ||
494e05f4 ILT |
220 | Output_section_element() |
221 | { } | |
222 | ||
223 | virtual ~Output_section_element() | |
224 | { } | |
225 | ||
a445fddf ILT |
226 | // Add any symbol being defined to the symbol table. |
227 | virtual void | |
228 | add_symbols_to_table(Symbol_table*) | |
229 | { } | |
230 | ||
231 | // Finalize symbols and check assertions. | |
232 | virtual void | |
233 | finalize_symbols(Symbol_table*, const Layout*, bool*, uint64_t*) | |
234 | { } | |
235 | ||
236 | // Return whether this element matches FILE_NAME and SECTION_NAME. | |
237 | // The only real implementation is in Output_section_element_input. | |
238 | virtual bool | |
239 | match_name(const char*, const char*) const | |
240 | { return false; } | |
241 | ||
242 | // Set section addresses. This includes applying assignments if the | |
243 | // the expression is an absolute value. | |
244 | virtual void | |
245 | set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t, | |
246 | uint64_t*, std::string*, Input_section_list*) | |
247 | { } | |
248 | ||
249 | // Print the element for debugging purposes. | |
494e05f4 ILT |
250 | virtual void |
251 | print(FILE* f) const = 0; | |
a445fddf ILT |
252 | |
253 | protected: | |
254 | // Return a fill string that is LENGTH bytes long, filling it with | |
255 | // FILL. | |
256 | std::string | |
257 | get_fill_string(const std::string* fill, section_size_type length) const; | |
494e05f4 ILT |
258 | }; |
259 | ||
a445fddf ILT |
260 | std::string |
261 | Output_section_element::get_fill_string(const std::string* fill, | |
262 | section_size_type length) const | |
263 | { | |
264 | std::string this_fill; | |
265 | this_fill.reserve(length); | |
266 | while (this_fill.length() + fill->length() <= length) | |
267 | this_fill += *fill; | |
268 | if (this_fill.length() < length) | |
269 | this_fill.append(*fill, 0, length - this_fill.length()); | |
270 | return this_fill; | |
271 | } | |
272 | ||
494e05f4 ILT |
273 | // A symbol assignment in an output section. |
274 | ||
275 | class Output_section_element_assignment : public Output_section_element | |
276 | { | |
277 | public: | |
278 | Output_section_element_assignment(const char* name, size_t namelen, | |
279 | Expression* val, bool provide, | |
280 | bool hidden) | |
281 | : assignment_(name, namelen, val, provide, hidden) | |
282 | { } | |
283 | ||
a445fddf ILT |
284 | // Add the symbol to the symbol table. |
285 | void | |
286 | add_symbols_to_table(Symbol_table* symtab) | |
287 | { this->assignment_.add_to_table(symtab); } | |
288 | ||
289 | // Finalize the symbol. | |
290 | void | |
291 | finalize_symbols(Symbol_table* symtab, const Layout* layout, | |
292 | bool* dot_has_value, uint64_t* dot_value) | |
293 | { | |
294 | this->assignment_.finalize_with_dot(symtab, layout, *dot_has_value, | |
295 | *dot_value); | |
296 | } | |
297 | ||
298 | // Set the section address. There is no section here, but if the | |
299 | // value is absolute, we set the symbol. This permits us to use | |
300 | // absolute symbols when setting dot. | |
301 | void | |
302 | set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*, | |
303 | uint64_t, uint64_t* dot_value, std::string*, | |
304 | Input_section_list*) | |
305 | { | |
306 | this->assignment_.set_if_absolute(symtab, layout, true, true, *dot_value); | |
307 | } | |
308 | ||
309 | // Print for debugging. | |
494e05f4 ILT |
310 | void |
311 | print(FILE* f) const | |
312 | { | |
313 | fprintf(f, " "); | |
314 | this->assignment_.print(f); | |
315 | } | |
316 | ||
317 | private: | |
318 | Symbol_assignment assignment_; | |
319 | }; | |
320 | ||
a445fddf ILT |
321 | // An assignment to the dot symbol in an output section. |
322 | ||
323 | class Output_section_element_dot_assignment : public Output_section_element | |
324 | { | |
325 | public: | |
326 | Output_section_element_dot_assignment(Expression* val) | |
327 | : val_(val) | |
328 | { } | |
329 | ||
330 | // Finalize the symbol. | |
331 | void | |
332 | finalize_symbols(Symbol_table* symtab, const Layout* layout, | |
333 | bool* dot_has_value, uint64_t* dot_value) | |
334 | { | |
335 | bool dummy; | |
336 | *dot_value = this->val_->eval_with_dot(symtab, layout, *dot_has_value, | |
337 | *dot_value, &dummy); | |
338 | *dot_has_value = true; | |
339 | } | |
340 | ||
341 | // Update the dot symbol while setting section addresses. | |
342 | void | |
343 | set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*, | |
344 | uint64_t, uint64_t* dot_value, std::string*, | |
345 | Input_section_list*); | |
346 | ||
347 | // Print for debugging. | |
348 | void | |
349 | print(FILE* f) const | |
350 | { | |
351 | fprintf(f, " . = "); | |
352 | this->val_->print(f); | |
353 | fprintf(f, "\n"); | |
354 | } | |
355 | ||
356 | private: | |
357 | Expression* val_; | |
358 | }; | |
359 | ||
360 | // Update the dot symbol while setting section addresses. | |
361 | ||
362 | void | |
363 | Output_section_element_dot_assignment::set_section_addresses( | |
364 | Symbol_table* symtab, | |
365 | Layout* layout, | |
366 | Output_section* output_section, | |
367 | uint64_t, | |
368 | uint64_t* dot_value, | |
369 | std::string* fill, | |
370 | Input_section_list*) | |
371 | { | |
372 | bool is_absolute; | |
373 | uint64_t next_dot = this->val_->eval_with_dot(symtab, layout, true, | |
374 | *dot_value, &is_absolute); | |
375 | if (!is_absolute) | |
376 | gold_error(_("dot set to non-absolute value")); | |
377 | if (next_dot < *dot_value) | |
378 | gold_error(_("dot may not move backward")); | |
379 | if (next_dot > *dot_value && output_section != NULL) | |
380 | { | |
381 | section_size_type length = convert_to_section_size_type(next_dot | |
382 | - *dot_value); | |
383 | Output_section_data* posd; | |
384 | if (fill->empty()) | |
385 | posd = new Output_data_fixed_space(length, 0); | |
386 | else | |
387 | { | |
388 | std::string this_fill = this->get_fill_string(fill, length); | |
389 | posd = new Output_data_const(this_fill, 0); | |
390 | } | |
391 | output_section->add_output_section_data(posd); | |
392 | } | |
393 | *dot_value = next_dot; | |
394 | } | |
395 | ||
494e05f4 ILT |
396 | // An assertion in an output section. |
397 | ||
398 | class Output_section_element_assertion : public Output_section_element | |
399 | { | |
400 | public: | |
401 | Output_section_element_assertion(Expression* check, const char* message, | |
402 | size_t messagelen) | |
403 | : assertion_(check, message, messagelen) | |
404 | { } | |
405 | ||
406 | void | |
407 | print(FILE* f) const | |
408 | { | |
409 | fprintf(f, " "); | |
410 | this->assertion_.print(f); | |
411 | } | |
412 | ||
413 | private: | |
414 | Script_assertion assertion_; | |
415 | }; | |
416 | ||
417 | // A data item in an output section. | |
418 | ||
419 | class Output_section_element_data : public Output_section_element | |
420 | { | |
421 | public: | |
422 | Output_section_element_data(int size, bool is_signed, Expression* val) | |
423 | : size_(size), is_signed_(is_signed), val_(val) | |
424 | { } | |
425 | ||
a445fddf ILT |
426 | // Finalize symbols--we just need to update dot. |
427 | void | |
428 | finalize_symbols(Symbol_table*, const Layout*, bool*, uint64_t* dot_value) | |
429 | { *dot_value += this->size_; } | |
430 | ||
431 | // Store the value in the section. | |
432 | void | |
433 | set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t, | |
434 | uint64_t* dot_value, std::string*, | |
435 | Input_section_list*); | |
436 | ||
437 | // Print for debugging. | |
494e05f4 ILT |
438 | void |
439 | print(FILE*) const; | |
440 | ||
441 | private: | |
a445fddf ILT |
442 | template<bool big_endian> |
443 | std::string | |
444 | set_fill_string(uint64_t); | |
445 | ||
494e05f4 ILT |
446 | // The size in bytes. |
447 | int size_; | |
448 | // Whether the value is signed. | |
449 | bool is_signed_; | |
450 | // The value. | |
451 | Expression* val_; | |
452 | }; | |
453 | ||
a445fddf ILT |
454 | // Store the value in the section. |
455 | ||
456 | void | |
457 | Output_section_element_data::set_section_addresses(Symbol_table* symtab, | |
458 | Layout* layout, | |
459 | Output_section* os, | |
460 | uint64_t, | |
461 | uint64_t* dot_value, | |
462 | std::string*, | |
463 | Input_section_list*) | |
464 | { | |
465 | gold_assert(os != NULL); | |
466 | ||
467 | bool is_absolute; | |
468 | uint64_t val = this->val_->eval_with_dot(symtab, layout, true, *dot_value, | |
469 | &is_absolute); | |
470 | if (!is_absolute) | |
471 | gold_error(_("data directive with non-absolute value")); | |
472 | ||
473 | std::string fill; | |
474 | if (parameters->is_big_endian()) | |
475 | fill = this->set_fill_string<true>(val); | |
476 | else | |
477 | fill = this->set_fill_string<false>(val); | |
478 | ||
479 | os->add_output_section_data(new Output_data_const(fill, 0)); | |
480 | ||
481 | *dot_value += this->size_; | |
482 | } | |
483 | ||
484 | // Get the value to store in a std::string. | |
485 | ||
486 | template<bool big_endian> | |
487 | std::string | |
488 | Output_section_element_data::set_fill_string(uint64_t val) | |
489 | { | |
490 | std::string ret; | |
491 | unsigned char buf[8]; | |
492 | switch (this->size_) | |
493 | { | |
494 | case 1: | |
495 | elfcpp::Swap_unaligned<8, big_endian>::writeval(buf, val); | |
496 | ret.assign(reinterpret_cast<char*>(buf), 1); | |
497 | break; | |
498 | case 2: | |
499 | elfcpp::Swap_unaligned<16, big_endian>::writeval(buf, val); | |
500 | ret.assign(reinterpret_cast<char*>(buf), 2); | |
501 | break; | |
502 | case 4: | |
503 | elfcpp::Swap_unaligned<32, big_endian>::writeval(buf, val); | |
504 | ret.assign(reinterpret_cast<char*>(buf), 4); | |
505 | break; | |
506 | case 8: | |
507 | if (parameters->get_size() == 32) | |
508 | { | |
509 | val &= 0xffffffff; | |
510 | if (this->is_signed_ && (val & 0x80000000) != 0) | |
511 | val |= 0xffffffff00000000LL; | |
512 | } | |
513 | elfcpp::Swap_unaligned<64, big_endian>::writeval(buf, val); | |
514 | ret.assign(reinterpret_cast<char*>(buf), 8); | |
515 | break; | |
516 | default: | |
517 | gold_unreachable(); | |
518 | } | |
519 | return ret; | |
520 | } | |
521 | ||
494e05f4 ILT |
522 | // Print for debugging. |
523 | ||
524 | void | |
525 | Output_section_element_data::print(FILE* f) const | |
526 | { | |
527 | const char* s; | |
528 | switch (this->size_) | |
529 | { | |
530 | case 1: | |
531 | s = "BYTE"; | |
532 | break; | |
533 | case 2: | |
534 | s = "SHORT"; | |
535 | break; | |
536 | case 4: | |
537 | s = "LONG"; | |
538 | break; | |
539 | case 8: | |
540 | if (this->is_signed_) | |
541 | s = "SQUAD"; | |
542 | else | |
543 | s = "QUAD"; | |
544 | break; | |
545 | default: | |
546 | gold_unreachable(); | |
547 | } | |
548 | fprintf(f, " %s(", s); | |
549 | this->val_->print(f); | |
550 | fprintf(f, ")\n"); | |
551 | } | |
552 | ||
553 | // A fill value setting in an output section. | |
554 | ||
555 | class Output_section_element_fill : public Output_section_element | |
556 | { | |
557 | public: | |
558 | Output_section_element_fill(Expression* val) | |
559 | : val_(val) | |
560 | { } | |
561 | ||
a445fddf ILT |
562 | // Update the fill value while setting section addresses. |
563 | void | |
564 | set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*, | |
565 | uint64_t, uint64_t* dot_value, std::string* fill, | |
566 | Input_section_list*) | |
567 | { | |
568 | bool is_absolute; | |
569 | uint64_t fill_val = this->val_->eval_with_dot(symtab, layout, true, | |
570 | *dot_value, | |
571 | &is_absolute); | |
572 | if (!is_absolute) | |
573 | gold_error(_("fill set to non-absolute value")); | |
574 | // FIXME: The GNU linker supports fill values of arbitrary length. | |
575 | unsigned char fill_buff[4]; | |
576 | elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val); | |
577 | fill->assign(reinterpret_cast<char*>(fill_buff), 4); | |
578 | } | |
579 | ||
580 | // Print for debugging. | |
494e05f4 ILT |
581 | void |
582 | print(FILE* f) const | |
583 | { | |
584 | fprintf(f, " FILL("); | |
585 | this->val_->print(f); | |
586 | fprintf(f, ")\n"); | |
587 | } | |
588 | ||
589 | private: | |
590 | // The new fill value. | |
591 | Expression* val_; | |
592 | }; | |
593 | ||
a445fddf ILT |
594 | // Return whether STRING contains a wildcard character. This is used |
595 | // to speed up matching. | |
596 | ||
597 | static inline bool | |
598 | is_wildcard_string(const std::string& s) | |
599 | { | |
600 | return strpbrk(s.c_str(), "?*[") != NULL; | |
601 | } | |
602 | ||
494e05f4 ILT |
603 | // An input section specification in an output section |
604 | ||
605 | class Output_section_element_input : public Output_section_element | |
606 | { | |
607 | public: | |
494e05f4 ILT |
608 | Output_section_element_input(const Input_section_spec* spec, bool keep); |
609 | ||
a445fddf ILT |
610 | // Finalize symbols--just update the value of the dot symbol. |
611 | void | |
612 | finalize_symbols(Symbol_table*, const Layout*, bool* dot_has_value, | |
613 | uint64_t* dot_value) | |
614 | { | |
615 | *dot_value = this->final_dot_value_; | |
616 | *dot_has_value = true; | |
617 | } | |
618 | ||
619 | // See whether we match FILE_NAME and SECTION_NAME as an input | |
620 | // section. | |
621 | bool | |
622 | match_name(const char* file_name, const char* section_name) const; | |
623 | ||
624 | // Set the section address. | |
625 | void | |
626 | set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*, | |
627 | uint64_t subalign, uint64_t* dot_value, | |
628 | std::string* fill, Input_section_list*); | |
629 | ||
630 | // Print for debugging. | |
494e05f4 ILT |
631 | void |
632 | print(FILE* f) const; | |
633 | ||
634 | private: | |
635 | // An input section pattern. | |
636 | struct Input_section_pattern | |
637 | { | |
638 | std::string pattern; | |
a445fddf | 639 | bool pattern_is_wildcard; |
494e05f4 ILT |
640 | Sort_wildcard sort; |
641 | ||
642 | Input_section_pattern(const char* patterna, size_t patternlena, | |
643 | Sort_wildcard sorta) | |
a445fddf ILT |
644 | : pattern(patterna, patternlena), |
645 | pattern_is_wildcard(is_wildcard_string(this->pattern)), | |
646 | sort(sorta) | |
494e05f4 ILT |
647 | { } |
648 | }; | |
649 | ||
650 | typedef std::vector<Input_section_pattern> Input_section_patterns; | |
651 | ||
a445fddf ILT |
652 | // Filename_exclusions is a pair of filename pattern and a bool |
653 | // indicating whether the filename is a wildcard. | |
654 | typedef std::vector<std::pair<std::string, bool> > Filename_exclusions; | |
655 | ||
656 | // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN | |
657 | // indicates whether this is a wildcard pattern. | |
658 | static inline bool | |
659 | match(const char* string, const char* pattern, bool is_wildcard_pattern) | |
660 | { | |
661 | return (is_wildcard_pattern | |
662 | ? fnmatch(pattern, string, 0) == 0 | |
663 | : strcmp(string, pattern) == 0); | |
664 | } | |
494e05f4 | 665 | |
a445fddf ILT |
666 | // See if we match a file name. |
667 | bool | |
668 | match_file_name(const char* file_name) const; | |
669 | ||
670 | // The file name pattern. If this is the empty string, we match all | |
671 | // files. | |
494e05f4 | 672 | std::string filename_pattern_; |
a445fddf ILT |
673 | // Whether the file name pattern is a wildcard. |
674 | bool filename_is_wildcard_; | |
494e05f4 ILT |
675 | // How the file names should be sorted. This may only be |
676 | // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME. | |
677 | Sort_wildcard filename_sort_; | |
678 | // The list of file names to exclude. | |
679 | Filename_exclusions filename_exclusions_; | |
680 | // The list of input section patterns. | |
681 | Input_section_patterns input_section_patterns_; | |
682 | // Whether to keep this section when garbage collecting. | |
683 | bool keep_; | |
a445fddf ILT |
684 | // The value of dot after including all matching sections. |
685 | uint64_t final_dot_value_; | |
494e05f4 ILT |
686 | }; |
687 | ||
688 | // Construct Output_section_element_input. The parser records strings | |
689 | // as pointers into a copy of the script file, which will go away when | |
690 | // parsing is complete. We make sure they are in std::string objects. | |
691 | ||
692 | Output_section_element_input::Output_section_element_input( | |
693 | const Input_section_spec* spec, | |
694 | bool keep) | |
a445fddf ILT |
695 | : filename_pattern_(), |
696 | filename_is_wildcard_(false), | |
494e05f4 ILT |
697 | filename_sort_(spec->file.sort), |
698 | filename_exclusions_(), | |
699 | input_section_patterns_(), | |
a445fddf ILT |
700 | keep_(keep), |
701 | final_dot_value_(0) | |
494e05f4 | 702 | { |
a445fddf ILT |
703 | // The filename pattern "*" is common, and matches all files. Turn |
704 | // it into the empty string. | |
705 | if (spec->file.name.length != 1 || spec->file.name.value[0] != '*') | |
706 | this->filename_pattern_.assign(spec->file.name.value, | |
707 | spec->file.name.length); | |
708 | this->filename_is_wildcard_ = is_wildcard_string(this->filename_pattern_); | |
709 | ||
494e05f4 ILT |
710 | if (spec->input_sections.exclude != NULL) |
711 | { | |
712 | for (String_list::const_iterator p = | |
713 | spec->input_sections.exclude->begin(); | |
714 | p != spec->input_sections.exclude->end(); | |
715 | ++p) | |
a445fddf ILT |
716 | { |
717 | bool is_wildcard = is_wildcard_string(*p); | |
718 | this->filename_exclusions_.push_back(std::make_pair(*p, | |
719 | is_wildcard)); | |
720 | } | |
494e05f4 ILT |
721 | } |
722 | ||
723 | if (spec->input_sections.sections != NULL) | |
724 | { | |
725 | Input_section_patterns& isp(this->input_section_patterns_); | |
726 | for (String_sort_list::const_iterator p = | |
727 | spec->input_sections.sections->begin(); | |
728 | p != spec->input_sections.sections->end(); | |
729 | ++p) | |
730 | isp.push_back(Input_section_pattern(p->name.value, p->name.length, | |
731 | p->sort)); | |
732 | } | |
733 | } | |
734 | ||
a445fddf ILT |
735 | // See whether we match FILE_NAME. |
736 | ||
737 | bool | |
738 | Output_section_element_input::match_file_name(const char* file_name) const | |
739 | { | |
740 | if (!this->filename_pattern_.empty()) | |
741 | { | |
742 | // If we were called with no filename, we refuse to match a | |
743 | // pattern which requires a file name. | |
744 | if (file_name == NULL) | |
745 | return false; | |
746 | ||
747 | if (!match(file_name, this->filename_pattern_.c_str(), | |
748 | this->filename_is_wildcard_)) | |
749 | return false; | |
750 | } | |
751 | ||
752 | if (file_name != NULL) | |
753 | { | |
754 | // Now we have to see whether FILE_NAME matches one of the | |
755 | // exclusion patterns, if any. | |
756 | for (Filename_exclusions::const_iterator p = | |
757 | this->filename_exclusions_.begin(); | |
758 | p != this->filename_exclusions_.end(); | |
759 | ++p) | |
760 | { | |
761 | if (match(file_name, p->first.c_str(), p->second)) | |
762 | return false; | |
763 | } | |
764 | } | |
765 | ||
766 | return true; | |
767 | } | |
768 | ||
769 | // See whether we match FILE_NAME and SECTION_NAME. | |
770 | ||
771 | bool | |
772 | Output_section_element_input::match_name(const char* file_name, | |
773 | const char* section_name) const | |
774 | { | |
775 | if (!this->match_file_name(file_name)) | |
776 | return false; | |
777 | ||
778 | // If there are no section name patterns, then we match. | |
779 | if (this->input_section_patterns_.empty()) | |
780 | return true; | |
781 | ||
782 | // See whether we match the section name patterns. | |
783 | for (Input_section_patterns::const_iterator p = | |
784 | this->input_section_patterns_.begin(); | |
785 | p != this->input_section_patterns_.end(); | |
786 | ++p) | |
787 | { | |
788 | if (match(section_name, p->pattern.c_str(), p->pattern_is_wildcard)) | |
789 | return true; | |
790 | } | |
791 | ||
792 | // We didn't match any section names, so we didn't match. | |
793 | return false; | |
794 | } | |
795 | ||
796 | // Information we use to sort the input sections. | |
797 | ||
798 | struct Input_section_info | |
799 | { | |
800 | Relobj* relobj; | |
801 | unsigned int shndx; | |
802 | std::string section_name; | |
803 | uint64_t size; | |
804 | uint64_t addralign; | |
805 | }; | |
806 | ||
807 | // A class to sort the input sections. | |
808 | ||
809 | class Input_section_sorter | |
810 | { | |
811 | public: | |
812 | Input_section_sorter(Sort_wildcard filename_sort, Sort_wildcard section_sort) | |
813 | : filename_sort_(filename_sort), section_sort_(section_sort) | |
814 | { } | |
815 | ||
816 | bool | |
817 | operator()(const Input_section_info&, const Input_section_info&) const; | |
818 | ||
819 | private: | |
820 | Sort_wildcard filename_sort_; | |
821 | Sort_wildcard section_sort_; | |
822 | }; | |
823 | ||
824 | bool | |
825 | Input_section_sorter::operator()(const Input_section_info& isi1, | |
826 | const Input_section_info& isi2) const | |
827 | { | |
828 | if (this->section_sort_ == SORT_WILDCARD_BY_NAME | |
829 | || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT | |
830 | || (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME | |
831 | && isi1.addralign == isi2.addralign)) | |
832 | { | |
833 | if (isi1.section_name != isi2.section_name) | |
834 | return isi1.section_name < isi2.section_name; | |
835 | } | |
836 | if (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT | |
837 | || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT | |
838 | || this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME) | |
839 | { | |
840 | if (isi1.addralign != isi2.addralign) | |
841 | return isi1.addralign < isi2.addralign; | |
842 | } | |
843 | if (this->filename_sort_ == SORT_WILDCARD_BY_NAME) | |
844 | { | |
845 | if (isi1.relobj->name() != isi2.relobj->name()) | |
846 | return isi1.relobj->name() < isi2.relobj->name(); | |
847 | } | |
848 | ||
849 | // Otherwise we leave them in the same order. | |
850 | return false; | |
851 | } | |
852 | ||
853 | // Set the section address. Look in INPUT_SECTIONS for sections which | |
854 | // match this spec, sort them as specified, and add them to the output | |
855 | // section. | |
856 | ||
857 | void | |
858 | Output_section_element_input::set_section_addresses( | |
859 | Symbol_table*, | |
860 | Layout*, | |
861 | Output_section* output_section, | |
862 | uint64_t subalign, | |
863 | uint64_t* dot_value, | |
864 | std::string* fill, | |
865 | Input_section_list* input_sections) | |
866 | { | |
867 | // We build a list of sections which match each | |
868 | // Input_section_pattern. | |
869 | ||
870 | typedef std::vector<std::vector<Input_section_info> > Matching_sections; | |
871 | size_t input_pattern_count = this->input_section_patterns_.size(); | |
872 | if (input_pattern_count == 0) | |
873 | input_pattern_count = 1; | |
874 | Matching_sections matching_sections(input_pattern_count); | |
875 | ||
876 | // Look through the list of sections for this output section. Add | |
877 | // each one which matches to one of the elements of | |
878 | // MATCHING_SECTIONS. | |
879 | ||
880 | Input_section_list::iterator p = input_sections->begin(); | |
881 | while (p != input_sections->end()) | |
882 | { | |
883 | // Calling section_name and section_addralign is not very | |
884 | // efficient. | |
885 | Input_section_info isi; | |
886 | isi.relobj = p->first; | |
887 | isi.shndx = p->second; | |
888 | ||
889 | // Lock the object so that we can get information about the | |
890 | // section. This is OK since we know we are single-threaded | |
891 | // here. | |
892 | { | |
893 | const Task* task = reinterpret_cast<const Task*>(-1); | |
894 | Task_lock_obj<Object> tl(task, p->first); | |
895 | ||
896 | isi.section_name = p->first->section_name(p->second); | |
897 | isi.size = p->first->section_size(p->second); | |
898 | isi.addralign = p->first->section_addralign(p->second); | |
899 | } | |
900 | ||
901 | if (!this->match_file_name(isi.relobj->name().c_str())) | |
902 | ++p; | |
903 | else if (this->input_section_patterns_.empty()) | |
904 | { | |
905 | matching_sections[0].push_back(isi); | |
906 | p = input_sections->erase(p); | |
907 | } | |
908 | else | |
909 | { | |
910 | size_t i; | |
911 | for (i = 0; i < input_pattern_count; ++i) | |
912 | { | |
913 | const Input_section_pattern& | |
914 | isp(this->input_section_patterns_[i]); | |
915 | if (match(isi.section_name.c_str(), isp.pattern.c_str(), | |
916 | isp.pattern_is_wildcard)) | |
917 | break; | |
918 | } | |
919 | ||
920 | if (i >= this->input_section_patterns_.size()) | |
921 | ++p; | |
922 | else | |
923 | { | |
924 | matching_sections[i].push_back(isi); | |
925 | p = input_sections->erase(p); | |
926 | } | |
927 | } | |
928 | } | |
929 | ||
930 | // Look through MATCHING_SECTIONS. Sort each one as specified, | |
931 | // using a stable sort so that we get the default order when | |
932 | // sections are otherwise equal. Add each input section to the | |
933 | // output section. | |
934 | ||
935 | for (size_t i = 0; i < input_pattern_count; ++i) | |
936 | { | |
937 | if (matching_sections[i].empty()) | |
938 | continue; | |
939 | ||
940 | gold_assert(output_section != NULL); | |
941 | ||
942 | const Input_section_pattern& isp(this->input_section_patterns_[i]); | |
943 | if (isp.sort != SORT_WILDCARD_NONE | |
944 | || this->filename_sort_ != SORT_WILDCARD_NONE) | |
945 | std::stable_sort(matching_sections[i].begin(), | |
946 | matching_sections[i].end(), | |
947 | Input_section_sorter(this->filename_sort_, | |
948 | isp.sort)); | |
949 | ||
950 | for (std::vector<Input_section_info>::const_iterator p = | |
951 | matching_sections[i].begin(); | |
952 | p != matching_sections[i].end(); | |
953 | ++p) | |
954 | { | |
955 | uint64_t this_subalign = p->addralign; | |
956 | if (this_subalign < subalign) | |
957 | this_subalign = subalign; | |
958 | ||
959 | uint64_t address = align_address(*dot_value, this_subalign); | |
960 | ||
961 | if (address > *dot_value && !fill->empty()) | |
962 | { | |
963 | section_size_type length = | |
964 | convert_to_section_size_type(address - *dot_value); | |
965 | std::string this_fill = this->get_fill_string(fill, length); | |
966 | Output_section_data* posd = new Output_data_const(this_fill, 0); | |
967 | output_section->add_output_section_data(posd); | |
968 | } | |
969 | ||
970 | output_section->add_input_section_for_script(p->relobj, | |
971 | p->shndx, | |
972 | p->size, | |
973 | this_subalign); | |
974 | ||
975 | *dot_value = address + p->size; | |
976 | } | |
977 | } | |
978 | ||
979 | this->final_dot_value_ = *dot_value; | |
980 | } | |
981 | ||
494e05f4 ILT |
982 | // Print for debugging. |
983 | ||
984 | void | |
985 | Output_section_element_input::print(FILE* f) const | |
986 | { | |
987 | fprintf(f, " "); | |
988 | ||
989 | if (this->keep_) | |
990 | fprintf(f, "KEEP("); | |
991 | ||
992 | if (!this->filename_pattern_.empty()) | |
993 | { | |
994 | bool need_close_paren = false; | |
995 | switch (this->filename_sort_) | |
996 | { | |
997 | case SORT_WILDCARD_NONE: | |
998 | break; | |
999 | case SORT_WILDCARD_BY_NAME: | |
1000 | fprintf(f, "SORT_BY_NAME("); | |
1001 | need_close_paren = true; | |
1002 | break; | |
1003 | default: | |
1004 | gold_unreachable(); | |
1005 | } | |
1006 | ||
1007 | fprintf(f, "%s", this->filename_pattern_.c_str()); | |
1008 | ||
1009 | if (need_close_paren) | |
1010 | fprintf(f, ")"); | |
1011 | } | |
1012 | ||
1013 | if (!this->input_section_patterns_.empty() | |
1014 | || !this->filename_exclusions_.empty()) | |
1015 | { | |
1016 | fprintf(f, "("); | |
1017 | ||
1018 | bool need_space = false; | |
1019 | if (!this->filename_exclusions_.empty()) | |
1020 | { | |
1021 | fprintf(f, "EXCLUDE_FILE("); | |
1022 | bool need_comma = false; | |
1023 | for (Filename_exclusions::const_iterator p = | |
1024 | this->filename_exclusions_.begin(); | |
1025 | p != this->filename_exclusions_.end(); | |
1026 | ++p) | |
1027 | { | |
1028 | if (need_comma) | |
1029 | fprintf(f, ", "); | |
a445fddf | 1030 | fprintf(f, "%s", p->first.c_str()); |
494e05f4 ILT |
1031 | need_comma = true; |
1032 | } | |
1033 | fprintf(f, ")"); | |
1034 | need_space = true; | |
1035 | } | |
1036 | ||
1037 | for (Input_section_patterns::const_iterator p = | |
1038 | this->input_section_patterns_.begin(); | |
1039 | p != this->input_section_patterns_.end(); | |
1040 | ++p) | |
1041 | { | |
1042 | if (need_space) | |
1043 | fprintf(f, " "); | |
1044 | ||
1045 | int close_parens = 0; | |
1046 | switch (p->sort) | |
1047 | { | |
1048 | case SORT_WILDCARD_NONE: | |
1049 | break; | |
1050 | case SORT_WILDCARD_BY_NAME: | |
1051 | fprintf(f, "SORT_BY_NAME("); | |
1052 | close_parens = 1; | |
1053 | break; | |
1054 | case SORT_WILDCARD_BY_ALIGNMENT: | |
1055 | fprintf(f, "SORT_BY_ALIGNMENT("); | |
1056 | close_parens = 1; | |
1057 | break; | |
1058 | case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT: | |
1059 | fprintf(f, "SORT_BY_NAME(SORT_BY_ALIGNMENT("); | |
1060 | close_parens = 2; | |
1061 | break; | |
1062 | case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME: | |
1063 | fprintf(f, "SORT_BY_ALIGNMENT(SORT_BY_NAME("); | |
1064 | close_parens = 2; | |
1065 | break; | |
1066 | default: | |
1067 | gold_unreachable(); | |
1068 | } | |
1069 | ||
1070 | fprintf(f, "%s", p->pattern.c_str()); | |
1071 | ||
1072 | for (int i = 0; i < close_parens; ++i) | |
1073 | fprintf(f, ")"); | |
1074 | ||
1075 | need_space = true; | |
1076 | } | |
1077 | ||
1078 | fprintf(f, ")"); | |
1079 | } | |
1080 | ||
1081 | if (this->keep_) | |
1082 | fprintf(f, ")"); | |
1083 | ||
1084 | fprintf(f, "\n"); | |
1085 | } | |
1086 | ||
1087 | // An output section. | |
1088 | ||
1089 | class Output_section_definition : public Sections_element | |
1090 | { | |
1091 | public: | |
a445fddf ILT |
1092 | typedef Output_section_element::Input_section_list Input_section_list; |
1093 | ||
494e05f4 ILT |
1094 | Output_section_definition(const char* name, size_t namelen, |
1095 | const Parser_output_section_header* header); | |
1096 | ||
1097 | // Finish the output section with the information in the trailer. | |
1098 | void | |
1099 | finish(const Parser_output_section_trailer* trailer); | |
1100 | ||
1101 | // Add a symbol to be defined. | |
1102 | void | |
1103 | add_symbol_assignment(const char* name, size_t length, Expression* value, | |
1104 | bool provide, bool hidden); | |
a445fddf ILT |
1105 | |
1106 | // Add an assignment to the special dot symbol. | |
1107 | void | |
1108 | add_dot_assignment(Expression* value); | |
1109 | ||
494e05f4 ILT |
1110 | // Add an assertion. |
1111 | void | |
1112 | add_assertion(Expression* check, const char* message, size_t messagelen); | |
1113 | ||
1114 | // Add a data item to the current output section. | |
1115 | void | |
1116 | add_data(int size, bool is_signed, Expression* val); | |
1117 | ||
1118 | // Add a setting for the fill value. | |
1119 | void | |
1120 | add_fill(Expression* val); | |
1121 | ||
1122 | // Add an input section specification. | |
1123 | void | |
1124 | add_input_section(const Input_section_spec* spec, bool keep); | |
1125 | ||
a445fddf ILT |
1126 | // Add any symbols being defined to the symbol table. |
1127 | void | |
1128 | add_symbols_to_table(Symbol_table* symtab); | |
1129 | ||
1130 | // Finalize symbols and check assertions. | |
1131 | void | |
1132 | finalize_symbols(Symbol_table*, const Layout*, bool*, uint64_t*); | |
1133 | ||
1134 | // Return the output section name to use for an input file name and | |
1135 | // section name. | |
1136 | const char* | |
1137 | output_section_name(const char* file_name, const char* section_name, | |
1138 | Output_section***); | |
1139 | ||
1140 | // Return whether to place an orphan section after this one. | |
1141 | bool | |
1142 | place_orphan_here(const Output_section *os, bool* exact) const; | |
1143 | ||
1144 | // Set the section address. | |
1145 | void | |
1146 | set_section_addresses(Symbol_table* symtab, Layout* layout, | |
1147 | bool* dot_has_value, uint64_t* dot_value); | |
1148 | ||
494e05f4 ILT |
1149 | // Print the contents to the FILE. This is for debugging. |
1150 | void | |
1151 | print(FILE*) const; | |
1152 | ||
1153 | private: | |
1154 | typedef std::vector<Output_section_element*> Output_section_elements; | |
1155 | ||
1156 | // The output section name. | |
1157 | std::string name_; | |
1158 | // The address. This may be NULL. | |
1159 | Expression* address_; | |
1160 | // The load address. This may be NULL. | |
1161 | Expression* load_address_; | |
1162 | // The alignment. This may be NULL. | |
1163 | Expression* align_; | |
1164 | // The input section alignment. This may be NULL. | |
1165 | Expression* subalign_; | |
1166 | // The fill value. This may be NULL. | |
1167 | Expression* fill_; | |
1168 | // The list of elements defining the section. | |
1169 | Output_section_elements elements_; | |
a445fddf ILT |
1170 | // The Output_section created for this definition. This will be |
1171 | // NULL if none was created. | |
1172 | Output_section* output_section_; | |
494e05f4 ILT |
1173 | }; |
1174 | ||
1175 | // Constructor. | |
1176 | ||
1177 | Output_section_definition::Output_section_definition( | |
1178 | const char* name, | |
1179 | size_t namelen, | |
1180 | const Parser_output_section_header* header) | |
1181 | : name_(name, namelen), | |
1182 | address_(header->address), | |
1183 | load_address_(header->load_address), | |
1184 | align_(header->align), | |
1185 | subalign_(header->subalign), | |
1186 | fill_(NULL), | |
a445fddf ILT |
1187 | elements_(), |
1188 | output_section_(NULL) | |
494e05f4 ILT |
1189 | { |
1190 | } | |
1191 | ||
1192 | // Finish an output section. | |
1193 | ||
1194 | void | |
1195 | Output_section_definition::finish(const Parser_output_section_trailer* trailer) | |
1196 | { | |
1197 | this->fill_ = trailer->fill; | |
1198 | } | |
1199 | ||
1200 | // Add a symbol to be defined. | |
1201 | ||
1202 | void | |
1203 | Output_section_definition::add_symbol_assignment(const char* name, | |
1204 | size_t length, | |
1205 | Expression* value, | |
1206 | bool provide, | |
1207 | bool hidden) | |
1208 | { | |
1209 | Output_section_element* p = new Output_section_element_assignment(name, | |
1210 | length, | |
1211 | value, | |
1212 | provide, | |
1213 | hidden); | |
1214 | this->elements_.push_back(p); | |
1215 | } | |
1216 | ||
a445fddf | 1217 | // Add an assignment to the special dot symbol. |
494e05f4 ILT |
1218 | |
1219 | void | |
a445fddf ILT |
1220 | Output_section_definition::add_dot_assignment(Expression* value) |
1221 | { | |
1222 | Output_section_element* p = new Output_section_element_dot_assignment(value); | |
1223 | this->elements_.push_back(p); | |
1224 | } | |
1225 | ||
1226 | // Add an assertion. | |
1227 | ||
1228 | void | |
1229 | Output_section_definition::add_assertion(Expression* check, | |
1230 | const char* message, | |
494e05f4 ILT |
1231 | size_t messagelen) |
1232 | { | |
1233 | Output_section_element* p = new Output_section_element_assertion(check, | |
1234 | message, | |
1235 | messagelen); | |
1236 | this->elements_.push_back(p); | |
1237 | } | |
1238 | ||
1239 | // Add a data item to the current output section. | |
1240 | ||
1241 | void | |
1242 | Output_section_definition::add_data(int size, bool is_signed, Expression* val) | |
1243 | { | |
1244 | Output_section_element* p = new Output_section_element_data(size, is_signed, | |
1245 | val); | |
1246 | this->elements_.push_back(p); | |
1247 | } | |
1248 | ||
1249 | // Add a setting for the fill value. | |
1250 | ||
1251 | void | |
1252 | Output_section_definition::add_fill(Expression* val) | |
1253 | { | |
1254 | Output_section_element* p = new Output_section_element_fill(val); | |
1255 | this->elements_.push_back(p); | |
1256 | } | |
1257 | ||
1258 | // Add an input section specification. | |
1259 | ||
1260 | void | |
1261 | Output_section_definition::add_input_section(const Input_section_spec* spec, | |
1262 | bool keep) | |
1263 | { | |
1264 | Output_section_element* p = new Output_section_element_input(spec, keep); | |
1265 | this->elements_.push_back(p); | |
1266 | } | |
1267 | ||
a445fddf ILT |
1268 | // Add any symbols being defined to the symbol table. |
1269 | ||
1270 | void | |
1271 | Output_section_definition::add_symbols_to_table(Symbol_table* symtab) | |
1272 | { | |
1273 | for (Output_section_elements::iterator p = this->elements_.begin(); | |
1274 | p != this->elements_.end(); | |
1275 | ++p) | |
1276 | (*p)->add_symbols_to_table(symtab); | |
1277 | } | |
1278 | ||
1279 | // Finalize symbols and check assertions. | |
1280 | ||
1281 | void | |
1282 | Output_section_definition::finalize_symbols(Symbol_table* symtab, | |
1283 | const Layout* layout, | |
1284 | bool* dot_has_value, | |
1285 | uint64_t* dot_value) | |
1286 | { | |
1287 | if (this->output_section_ != NULL) | |
1288 | *dot_value = this->output_section_->address(); | |
1289 | else | |
1290 | { | |
1291 | uint64_t address = *dot_value; | |
1292 | if (this->address_ != NULL) | |
1293 | { | |
1294 | bool dummy; | |
1295 | address = this->address_->eval_with_dot(symtab, layout, | |
1296 | *dot_has_value, *dot_value, | |
1297 | &dummy); | |
1298 | } | |
1299 | if (this->align_ != NULL) | |
1300 | { | |
1301 | bool dummy; | |
1302 | uint64_t align = this->align_->eval_with_dot(symtab, layout, | |
1303 | *dot_has_value, | |
1304 | *dot_value, | |
1305 | &dummy); | |
1306 | address = align_address(address, align); | |
1307 | } | |
1308 | *dot_value = address; | |
1309 | } | |
1310 | *dot_has_value = true; | |
1311 | ||
1312 | for (Output_section_elements::iterator p = this->elements_.begin(); | |
1313 | p != this->elements_.end(); | |
1314 | ++p) | |
1315 | (*p)->finalize_symbols(symtab, layout, dot_has_value, dot_value); | |
1316 | } | |
1317 | ||
1318 | // Return the output section name to use for an input section name. | |
1319 | ||
1320 | const char* | |
1321 | Output_section_definition::output_section_name(const char* file_name, | |
1322 | const char* section_name, | |
1323 | Output_section*** slot) | |
1324 | { | |
1325 | // Ask each element whether it matches NAME. | |
1326 | for (Output_section_elements::const_iterator p = this->elements_.begin(); | |
1327 | p != this->elements_.end(); | |
1328 | ++p) | |
1329 | { | |
1330 | if ((*p)->match_name(file_name, section_name)) | |
1331 | { | |
1332 | // We found a match for NAME, which means that it should go | |
1333 | // into this output section. | |
1334 | *slot = &this->output_section_; | |
1335 | return this->name_.c_str(); | |
1336 | } | |
1337 | } | |
1338 | ||
1339 | // We don't know about this section name. | |
1340 | return NULL; | |
1341 | } | |
1342 | ||
1343 | // Return whether to place an orphan output section after this | |
1344 | // section. | |
1345 | ||
1346 | bool | |
1347 | Output_section_definition::place_orphan_here(const Output_section *os, | |
1348 | bool* exact) const | |
1349 | { | |
1350 | // Check for the simple case first. | |
1351 | if (this->output_section_ != NULL | |
1352 | && this->output_section_->type() == os->type() | |
1353 | && this->output_section_->flags() == os->flags()) | |
1354 | { | |
1355 | *exact = true; | |
1356 | return true; | |
1357 | } | |
1358 | ||
1359 | // Otherwise use some heuristics. | |
1360 | ||
1361 | if ((os->flags() & elfcpp::SHF_ALLOC) == 0) | |
1362 | return false; | |
1363 | ||
1364 | if (os->type() == elfcpp::SHT_NOBITS) | |
1365 | { | |
1366 | if (this->output_section_ != NULL | |
1367 | && this->output_section_->type() == elfcpp::SHT_NOBITS) | |
1368 | return true; | |
1369 | if (this->name_ == ".bss") | |
1370 | return true; | |
1371 | } | |
1372 | else if (os->type() == elfcpp::SHT_NOTE) | |
1373 | { | |
1374 | if (this->output_section_ != NULL | |
1375 | && this->output_section_->type() == elfcpp::SHT_NOTE) | |
1376 | return true; | |
1377 | if (this->name_ == ".interp" | |
1378 | || this->name_.compare(0, 5, ".note") == 0) | |
1379 | return true; | |
1380 | } | |
1381 | else if (os->type() == elfcpp::SHT_REL || os->type() == elfcpp::SHT_RELA) | |
1382 | { | |
1383 | if (this->output_section_ != NULL | |
1384 | && (this->output_section_->type() == elfcpp::SHT_REL | |
1385 | || this->output_section_->type() == elfcpp::SHT_RELA)) | |
1386 | return true; | |
1387 | if (this->name_.compare(0, 4, ".rel") == 0) | |
1388 | return true; | |
1389 | } | |
1390 | else if (os->type() == elfcpp::SHT_PROGBITS | |
1391 | && (os->flags() & elfcpp::SHF_WRITE) != 0) | |
1392 | { | |
1393 | if (this->output_section_ != NULL | |
1394 | && this->output_section_->type() == elfcpp::SHT_PROGBITS | |
1395 | && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0) | |
1396 | return true; | |
1397 | if (this->name_ == ".data") | |
1398 | return true; | |
1399 | } | |
1400 | else if (os->type() == elfcpp::SHT_PROGBITS | |
1401 | && (os->flags() & elfcpp::SHF_EXECINSTR) != 0) | |
1402 | { | |
1403 | if (this->output_section_ != NULL | |
1404 | && this->output_section_->type() == elfcpp::SHT_PROGBITS | |
1405 | && (this->output_section_->flags() & elfcpp::SHF_EXECINSTR) != 0) | |
1406 | return true; | |
1407 | if (this->name_ == ".text") | |
1408 | return true; | |
1409 | } | |
1410 | else if (os->type() == elfcpp::SHT_PROGBITS) | |
1411 | { | |
1412 | if (this->output_section_ != NULL | |
1413 | && this->output_section_->type() == elfcpp::SHT_PROGBITS | |
1414 | && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0 | |
1415 | && (this->output_section_->flags() & elfcpp::SHF_EXECINSTR) == 0) | |
1416 | return true; | |
1417 | if (this->name_ == ".rodata") | |
1418 | return true; | |
1419 | } | |
1420 | ||
1421 | return false; | |
1422 | } | |
1423 | ||
1424 | // Set the section address. Note that the OUTPUT_SECTION_ field will | |
1425 | // be NULL if no input sections were mapped to this output section. | |
1426 | // We still have to adjust dot and process symbol assignments. | |
1427 | ||
1428 | void | |
1429 | Output_section_definition::set_section_addresses(Symbol_table* symtab, | |
1430 | Layout* layout, | |
1431 | bool* dot_has_value, | |
1432 | uint64_t* dot_value) | |
1433 | { | |
1434 | bool is_absolute; | |
1435 | uint64_t address; | |
1436 | if (this->address_ != NULL) | |
1437 | { | |
1438 | address = this->address_->eval_with_dot(symtab, layout, *dot_has_value, | |
1439 | *dot_value, &is_absolute); | |
1440 | if (!is_absolute) | |
1441 | gold_error(_("address of section %s is not absolute"), | |
1442 | this->name_.c_str()); | |
1443 | } | |
1444 | else | |
1445 | { | |
1446 | if (!*dot_has_value) | |
1447 | gold_error(_("no address given for section %s"), | |
1448 | this->name_.c_str()); | |
1449 | address = *dot_value; | |
1450 | } | |
1451 | ||
1452 | uint64_t align; | |
1453 | if (this->align_ == NULL) | |
1454 | { | |
1455 | if (this->output_section_ == NULL) | |
1456 | align = 0; | |
1457 | else | |
1458 | align = this->output_section_->addralign(); | |
1459 | } | |
1460 | else | |
1461 | { | |
1462 | align = this->align_->eval_with_dot(symtab, layout, *dot_has_value, | |
1463 | *dot_value, &is_absolute); | |
1464 | if (!is_absolute) | |
1465 | gold_error(_("alignment of section %s is not absolute"), | |
1466 | this->name_.c_str()); | |
1467 | if (this->output_section_ != NULL) | |
1468 | this->output_section_->set_addralign(align); | |
1469 | } | |
1470 | ||
1471 | address = align_address(address, align); | |
1472 | ||
1473 | *dot_value = address; | |
1474 | *dot_has_value = true; | |
1475 | ||
1476 | // The address of non-SHF_ALLOC sections is forced to zero, | |
1477 | // regardless of what the linker script wants. | |
1478 | if (this->output_section_ != NULL | |
1479 | && (this->output_section_->flags() & elfcpp::SHF_ALLOC) != 0) | |
1480 | this->output_section_->set_address(address); | |
1481 | ||
1482 | if (this->load_address_ != NULL && this->output_section_ != NULL) | |
1483 | { | |
1484 | uint64_t load_address = | |
1485 | this->load_address_->eval_with_dot(symtab, layout, *dot_has_value, | |
1486 | *dot_value, &is_absolute); | |
1487 | if (!is_absolute) | |
1488 | gold_error(_("load address of section %s is not absolute"), | |
1489 | this->name_.c_str()); | |
1490 | this->output_section_->set_load_address(load_address); | |
1491 | } | |
1492 | ||
1493 | uint64_t subalign; | |
1494 | if (this->subalign_ == NULL) | |
1495 | subalign = 0; | |
1496 | else | |
1497 | { | |
1498 | subalign = this->subalign_->eval_with_dot(symtab, layout, *dot_has_value, | |
1499 | *dot_value, &is_absolute); | |
1500 | if (!is_absolute) | |
1501 | gold_error(_("subalign of section %s is not absolute"), | |
1502 | this->name_.c_str()); | |
1503 | } | |
1504 | ||
1505 | std::string fill; | |
1506 | if (this->fill_ != NULL) | |
1507 | { | |
1508 | // FIXME: The GNU linker supports fill values of arbitrary | |
1509 | // length. | |
1510 | uint64_t fill_val = this->fill_->eval_with_dot(symtab, layout, | |
1511 | *dot_has_value, | |
1512 | *dot_value, | |
1513 | &is_absolute); | |
1514 | if (!is_absolute) | |
1515 | gold_error(_("fill of section %s is not absolute"), | |
1516 | this->name_.c_str()); | |
1517 | unsigned char fill_buff[4]; | |
1518 | elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val); | |
1519 | fill.assign(reinterpret_cast<char*>(fill_buff), 4); | |
1520 | } | |
1521 | ||
1522 | Input_section_list input_sections; | |
1523 | if (this->output_section_ != NULL) | |
1524 | { | |
1525 | // Get the list of input sections attached to this output | |
1526 | // section. This will leave the output section with only | |
1527 | // Output_section_data entries. | |
1528 | address += this->output_section_->get_input_sections(address, | |
1529 | fill, | |
1530 | &input_sections); | |
1531 | *dot_value = address; | |
1532 | } | |
1533 | ||
1534 | for (Output_section_elements::iterator p = this->elements_.begin(); | |
1535 | p != this->elements_.end(); | |
1536 | ++p) | |
1537 | (*p)->set_section_addresses(symtab, layout, this->output_section_, | |
1538 | subalign, dot_value, &fill, &input_sections); | |
1539 | ||
1540 | gold_assert(input_sections.empty()); | |
1541 | } | |
1542 | ||
494e05f4 ILT |
1543 | // Print for debugging. |
1544 | ||
1545 | void | |
1546 | Output_section_definition::print(FILE* f) const | |
1547 | { | |
1548 | fprintf(f, " %s ", this->name_.c_str()); | |
1549 | ||
1550 | if (this->address_ != NULL) | |
1551 | { | |
1552 | this->address_->print(f); | |
1553 | fprintf(f, " "); | |
1554 | } | |
1555 | ||
1556 | fprintf(f, ": "); | |
1557 | ||
1558 | if (this->load_address_ != NULL) | |
1559 | { | |
1560 | fprintf(f, "AT("); | |
1561 | this->load_address_->print(f); | |
1562 | fprintf(f, ") "); | |
1563 | } | |
1564 | ||
1565 | if (this->align_ != NULL) | |
1566 | { | |
1567 | fprintf(f, "ALIGN("); | |
1568 | this->align_->print(f); | |
1569 | fprintf(f, ") "); | |
1570 | } | |
1571 | ||
1572 | if (this->subalign_ != NULL) | |
1573 | { | |
1574 | fprintf(f, "SUBALIGN("); | |
1575 | this->subalign_->print(f); | |
1576 | fprintf(f, ") "); | |
1577 | } | |
1578 | ||
1579 | fprintf(f, "{\n"); | |
1580 | ||
1581 | for (Output_section_elements::const_iterator p = this->elements_.begin(); | |
1582 | p != this->elements_.end(); | |
1583 | ++p) | |
1584 | (*p)->print(f); | |
1585 | ||
1586 | fprintf(f, " }"); | |
1587 | ||
1588 | if (this->fill_ != NULL) | |
1589 | { | |
1590 | fprintf(f, " = "); | |
1591 | this->fill_->print(f); | |
1592 | } | |
1593 | ||
1594 | fprintf(f, "\n"); | |
1595 | } | |
1596 | ||
a445fddf ILT |
1597 | // An output section created to hold orphaned input sections. These |
1598 | // do not actually appear in linker scripts. However, for convenience | |
1599 | // when setting the output section addresses, we put a marker to these | |
1600 | // sections in the appropriate place in the list of SECTIONS elements. | |
1601 | ||
1602 | class Orphan_output_section : public Sections_element | |
1603 | { | |
1604 | public: | |
1605 | Orphan_output_section(Output_section* os) | |
1606 | : os_(os) | |
1607 | { } | |
1608 | ||
1609 | // Return whether to place an orphan section after this one. | |
1610 | bool | |
1611 | place_orphan_here(const Output_section *os, bool* exact) const; | |
1612 | ||
1613 | // Set section addresses. | |
1614 | void | |
1615 | set_section_addresses(Symbol_table*, Layout*, bool*, uint64_t*); | |
1616 | ||
1617 | // Print for debugging. | |
1618 | void | |
1619 | print(FILE* f) const | |
1620 | { | |
1621 | fprintf(f, " marker for orphaned output section %s\n", | |
1622 | this->os_->name()); | |
1623 | } | |
1624 | ||
1625 | private: | |
1626 | Output_section* os_; | |
1627 | }; | |
1628 | ||
1629 | // Whether to place another orphan section after this one. | |
1630 | ||
1631 | bool | |
1632 | Orphan_output_section::place_orphan_here(const Output_section* os, | |
1633 | bool* exact) const | |
1634 | { | |
1635 | if (this->os_->type() == os->type() | |
1636 | && this->os_->flags() == os->flags()) | |
1637 | { | |
1638 | *exact = true; | |
1639 | return true; | |
1640 | } | |
1641 | return false; | |
1642 | } | |
1643 | ||
1644 | // Set section addresses. | |
1645 | ||
1646 | void | |
1647 | Orphan_output_section::set_section_addresses(Symbol_table*, Layout*, | |
1648 | bool* dot_has_value, | |
1649 | uint64_t* dot_value) | |
1650 | { | |
1651 | typedef std::list<std::pair<Relobj*, unsigned int> > Input_section_list; | |
1652 | ||
1653 | if (!*dot_has_value) | |
1654 | gold_error(_("no address for orphan section %s"), this->os_->name()); | |
1655 | ||
1656 | uint64_t address = *dot_value; | |
1657 | address = align_address(address, this->os_->addralign()); | |
1658 | ||
1659 | if ((this->os_->flags() & elfcpp::SHF_ALLOC) != 0) | |
1660 | this->os_->set_address(address); | |
1661 | ||
1662 | Input_section_list input_sections; | |
1663 | address += this->os_->get_input_sections(address, "", &input_sections); | |
1664 | ||
1665 | for (Input_section_list::iterator p = input_sections.begin(); | |
1666 | p != input_sections.end(); | |
1667 | ++p) | |
1668 | { | |
1669 | uint64_t addralign; | |
1670 | uint64_t size; | |
1671 | ||
1672 | // We know what are single-threaded, so it is OK to lock the | |
1673 | // object. | |
1674 | { | |
1675 | const Task* task = reinterpret_cast<const Task*>(-1); | |
1676 | Task_lock_obj<Object> tl(task, p->first); | |
1677 | addralign = p->first->section_addralign(p->second); | |
1678 | size = p->first->section_size(p->second); | |
1679 | } | |
1680 | ||
1681 | address = align_address(address, addralign); | |
1682 | this->os_->add_input_section_for_script(p->first, p->second, size, 0); | |
1683 | address += size; | |
1684 | } | |
1685 | ||
1686 | *dot_value = address; | |
1687 | } | |
1688 | ||
494e05f4 ILT |
1689 | // Class Script_sections. |
1690 | ||
1691 | Script_sections::Script_sections() | |
1692 | : saw_sections_clause_(false), | |
1693 | in_sections_clause_(false), | |
1694 | sections_elements_(NULL), | |
1695 | output_section_(NULL) | |
1696 | { | |
1697 | } | |
1698 | ||
1699 | // Start a SECTIONS clause. | |
1700 | ||
1701 | void | |
1702 | Script_sections::start_sections() | |
1703 | { | |
1704 | gold_assert(!this->in_sections_clause_ && this->output_section_ == NULL); | |
1705 | this->saw_sections_clause_ = true; | |
1706 | this->in_sections_clause_ = true; | |
1707 | if (this->sections_elements_ == NULL) | |
1708 | this->sections_elements_ = new Sections_elements; | |
1709 | } | |
1710 | ||
1711 | // Finish a SECTIONS clause. | |
1712 | ||
1713 | void | |
1714 | Script_sections::finish_sections() | |
1715 | { | |
1716 | gold_assert(this->in_sections_clause_ && this->output_section_ == NULL); | |
1717 | this->in_sections_clause_ = false; | |
1718 | } | |
1719 | ||
1720 | // Add a symbol to be defined. | |
1721 | ||
1722 | void | |
1723 | Script_sections::add_symbol_assignment(const char* name, size_t length, | |
1724 | Expression* val, bool provide, | |
1725 | bool hidden) | |
1726 | { | |
1727 | if (this->output_section_ != NULL) | |
1728 | this->output_section_->add_symbol_assignment(name, length, val, | |
1729 | provide, hidden); | |
1730 | else | |
1731 | { | |
1732 | Sections_element* p = new Sections_element_assignment(name, length, | |
1733 | val, provide, | |
1734 | hidden); | |
1735 | this->sections_elements_->push_back(p); | |
1736 | } | |
1737 | } | |
1738 | ||
a445fddf ILT |
1739 | // Add an assignment to the special dot symbol. |
1740 | ||
1741 | void | |
1742 | Script_sections::add_dot_assignment(Expression* val) | |
1743 | { | |
1744 | if (this->output_section_ != NULL) | |
1745 | this->output_section_->add_dot_assignment(val); | |
1746 | else | |
1747 | { | |
1748 | Sections_element* p = new Sections_element_dot_assignment(val); | |
1749 | this->sections_elements_->push_back(p); | |
1750 | } | |
1751 | } | |
1752 | ||
494e05f4 ILT |
1753 | // Add an assertion. |
1754 | ||
1755 | void | |
1756 | Script_sections::add_assertion(Expression* check, const char* message, | |
1757 | size_t messagelen) | |
1758 | { | |
1759 | if (this->output_section_ != NULL) | |
1760 | this->output_section_->add_assertion(check, message, messagelen); | |
1761 | else | |
1762 | { | |
1763 | Sections_element* p = new Sections_element_assertion(check, message, | |
1764 | messagelen); | |
1765 | this->sections_elements_->push_back(p); | |
1766 | } | |
1767 | } | |
1768 | ||
1769 | // Start processing entries for an output section. | |
1770 | ||
1771 | void | |
1772 | Script_sections::start_output_section( | |
1773 | const char* name, | |
1774 | size_t namelen, | |
1775 | const Parser_output_section_header *header) | |
1776 | { | |
1777 | Output_section_definition* posd = new Output_section_definition(name, | |
1778 | namelen, | |
1779 | header); | |
1780 | this->sections_elements_->push_back(posd); | |
1781 | gold_assert(this->output_section_ == NULL); | |
1782 | this->output_section_ = posd; | |
1783 | } | |
1784 | ||
1785 | // Stop processing entries for an output section. | |
1786 | ||
1787 | void | |
1788 | Script_sections::finish_output_section( | |
1789 | const Parser_output_section_trailer* trailer) | |
1790 | { | |
1791 | gold_assert(this->output_section_ != NULL); | |
1792 | this->output_section_->finish(trailer); | |
1793 | this->output_section_ = NULL; | |
1794 | } | |
1795 | ||
1796 | // Add a data item to the current output section. | |
1797 | ||
1798 | void | |
1799 | Script_sections::add_data(int size, bool is_signed, Expression* val) | |
1800 | { | |
1801 | gold_assert(this->output_section_ != NULL); | |
1802 | this->output_section_->add_data(size, is_signed, val); | |
1803 | } | |
1804 | ||
1805 | // Add a fill value setting to the current output section. | |
1806 | ||
1807 | void | |
1808 | Script_sections::add_fill(Expression* val) | |
1809 | { | |
1810 | gold_assert(this->output_section_ != NULL); | |
1811 | this->output_section_->add_fill(val); | |
1812 | } | |
1813 | ||
1814 | // Add an input section specification to the current output section. | |
1815 | ||
1816 | void | |
1817 | Script_sections::add_input_section(const Input_section_spec* spec, bool keep) | |
1818 | { | |
1819 | gold_assert(this->output_section_ != NULL); | |
1820 | this->output_section_->add_input_section(spec, keep); | |
1821 | } | |
1822 | ||
a445fddf ILT |
1823 | // Add any symbols we are defining to the symbol table. |
1824 | ||
1825 | void | |
1826 | Script_sections::add_symbols_to_table(Symbol_table* symtab) | |
1827 | { | |
1828 | if (!this->saw_sections_clause_) | |
1829 | return; | |
1830 | for (Sections_elements::iterator p = this->sections_elements_->begin(); | |
1831 | p != this->sections_elements_->end(); | |
1832 | ++p) | |
1833 | (*p)->add_symbols_to_table(symtab); | |
1834 | } | |
1835 | ||
1836 | // Finalize symbols and check assertions. | |
1837 | ||
1838 | void | |
1839 | Script_sections::finalize_symbols(Symbol_table* symtab, const Layout* layout) | |
1840 | { | |
1841 | if (!this->saw_sections_clause_) | |
1842 | return; | |
1843 | bool dot_has_value = false; | |
1844 | uint64_t dot_value = 0; | |
1845 | for (Sections_elements::iterator p = this->sections_elements_->begin(); | |
1846 | p != this->sections_elements_->end(); | |
1847 | ++p) | |
1848 | (*p)->finalize_symbols(symtab, layout, &dot_has_value, &dot_value); | |
1849 | } | |
1850 | ||
1851 | // Return the name of the output section to use for an input file name | |
1852 | // and section name. | |
1853 | ||
1854 | const char* | |
1855 | Script_sections::output_section_name(const char* file_name, | |
1856 | const char* section_name, | |
1857 | Output_section*** output_section_slot) | |
1858 | { | |
1859 | for (Sections_elements::const_iterator p = this->sections_elements_->begin(); | |
1860 | p != this->sections_elements_->end(); | |
1861 | ++p) | |
1862 | { | |
1863 | const char* ret = (*p)->output_section_name(file_name, section_name, | |
1864 | output_section_slot); | |
1865 | ||
1866 | if (ret != NULL) | |
1867 | { | |
1868 | // The special name /DISCARD/ means that the input section | |
1869 | // should be discarded. | |
1870 | if (strcmp(ret, "/DISCARD/") == 0) | |
1871 | { | |
1872 | *output_section_slot = NULL; | |
1873 | return NULL; | |
1874 | } | |
1875 | return ret; | |
1876 | } | |
1877 | } | |
1878 | ||
1879 | // If we couldn't find a mapping for the name, the output section | |
1880 | // gets the name of the input section. | |
1881 | ||
1882 | *output_section_slot = NULL; | |
1883 | ||
1884 | return section_name; | |
1885 | } | |
1886 | ||
1887 | // Place a marker for an orphan output section into the SECTIONS | |
1888 | // clause. | |
1889 | ||
1890 | void | |
1891 | Script_sections::place_orphan(Output_section* os) | |
1892 | { | |
1893 | // Look for an output section definition which matches the output | |
1894 | // section. Put a marker after that section. | |
1895 | Sections_elements::iterator place = this->sections_elements_->end(); | |
1896 | for (Sections_elements::iterator p = this->sections_elements_->begin(); | |
1897 | p != this->sections_elements_->end(); | |
1898 | ++p) | |
1899 | { | |
1900 | bool exact; | |
1901 | if ((*p)->place_orphan_here(os, &exact)) | |
1902 | { | |
1903 | place = p; | |
1904 | if (exact) | |
1905 | break; | |
1906 | } | |
1907 | } | |
1908 | ||
1909 | // The insert function puts the new element before the iterator. | |
1910 | if (place != this->sections_elements_->end()) | |
1911 | ++place; | |
1912 | ||
1913 | this->sections_elements_->insert(place, new Orphan_output_section(os)); | |
1914 | } | |
1915 | ||
1916 | // Set the addresses of all the output sections. Walk through all the | |
1917 | // elements, tracking the dot symbol. Apply assignments which set | |
1918 | // absolute symbol values, in case they are used when setting dot. | |
1919 | // Fill in data statement values. As we find output sections, set the | |
1920 | // address, set the address of all associated input sections, and | |
1921 | // update dot. Return the segment which should hold the file header | |
1922 | // and segment headers, if any. | |
1923 | ||
1924 | Output_segment* | |
1925 | Script_sections::set_section_addresses(Symbol_table* symtab, Layout* layout) | |
1926 | { | |
1927 | gold_assert(this->saw_sections_clause_); | |
1928 | ||
1929 | bool dot_has_value = false; | |
1930 | uint64_t dot_value = 0; | |
1931 | for (Sections_elements::iterator p = this->sections_elements_->begin(); | |
1932 | p != this->sections_elements_->end(); | |
1933 | ++p) | |
1934 | (*p)->set_section_addresses(symtab, layout, &dot_has_value, &dot_value); | |
1935 | ||
1936 | return this->create_segments(layout); | |
1937 | } | |
1938 | ||
1939 | // Sort the sections in order to put them into segments. | |
1940 | ||
1941 | class Sort_output_sections | |
1942 | { | |
1943 | public: | |
1944 | bool | |
1945 | operator()(const Output_section* os1, const Output_section* os2) const; | |
1946 | }; | |
1947 | ||
1948 | bool | |
1949 | Sort_output_sections::operator()(const Output_section* os1, | |
1950 | const Output_section* os2) const | |
1951 | { | |
1952 | // Sort first by the load address. | |
1953 | uint64_t lma1 = (os1->has_load_address() | |
1954 | ? os1->load_address() | |
1955 | : os1->address()); | |
1956 | uint64_t lma2 = (os2->has_load_address() | |
1957 | ? os2->load_address() | |
1958 | : os2->address()); | |
1959 | if (lma1 != lma2) | |
1960 | return lma1 < lma2; | |
1961 | ||
1962 | // Then sort by the virtual address. | |
1963 | if (os1->address() != os2->address()) | |
1964 | return os1->address() < os2->address(); | |
1965 | ||
1966 | // Sort TLS sections to the end. | |
1967 | bool tls1 = (os1->flags() & elfcpp::SHF_TLS) != 0; | |
1968 | bool tls2 = (os2->flags() & elfcpp::SHF_TLS) != 0; | |
1969 | if (tls1 != tls2) | |
1970 | return tls2; | |
1971 | ||
1972 | // Sort PROGBITS before NOBITS. | |
1973 | if (os1->type() == elfcpp::SHT_PROGBITS && os2->type() == elfcpp::SHT_NOBITS) | |
1974 | return true; | |
1975 | if (os1->type() == elfcpp::SHT_NOBITS && os2->type() == elfcpp::SHT_PROGBITS) | |
1976 | return false; | |
1977 | ||
1978 | // Otherwise we don't care. | |
1979 | return false; | |
1980 | } | |
1981 | ||
1982 | // Return whether OS is a BSS section. This is a SHT_NOBITS section. | |
1983 | // We treat a section with the SHF_TLS flag set as taking up space | |
1984 | // even if it is SHT_NOBITS (this is true of .tbss), as we allocate | |
1985 | // space for them in the file. | |
1986 | ||
1987 | bool | |
1988 | Script_sections::is_bss_section(const Output_section* os) | |
1989 | { | |
1990 | return (os->type() == elfcpp::SHT_NOBITS | |
1991 | && (os->flags() & elfcpp::SHF_TLS) == 0); | |
1992 | } | |
1993 | ||
1994 | // Create the PT_LOAD segments when using a SECTIONS clause. Returns | |
1995 | // the segment which should hold the file header and segment headers, | |
1996 | // if any. | |
1997 | ||
1998 | Output_segment* | |
1999 | Script_sections::create_segments(Layout* layout) | |
2000 | { | |
2001 | gold_assert(this->saw_sections_clause_); | |
2002 | ||
2003 | if (parameters->output_is_object()) | |
2004 | return NULL; | |
2005 | ||
2006 | Layout::Section_list sections; | |
2007 | layout->get_allocated_sections(§ions); | |
2008 | ||
2009 | // Sort the sections by address. | |
2010 | std::stable_sort(sections.begin(), sections.end(), Sort_output_sections()); | |
2011 | ||
2012 | this->create_note_and_tls_segments(layout, §ions); | |
2013 | ||
2014 | // Walk through the sections adding them to PT_LOAD segments. | |
2015 | const uint64_t abi_pagesize = parameters->target()->abi_pagesize(); | |
2016 | Output_segment* first_seg = NULL; | |
2017 | Output_segment* current_seg = NULL; | |
2018 | bool is_current_seg_readonly = true; | |
2019 | Layout::Section_list::iterator plast = sections.end(); | |
2020 | uint64_t last_vma = 0; | |
2021 | uint64_t last_lma = 0; | |
2022 | uint64_t last_size = 0; | |
2023 | for (Layout::Section_list::iterator p = sections.begin(); | |
2024 | p != sections.end(); | |
2025 | ++p) | |
2026 | { | |
2027 | const uint64_t vma = (*p)->address(); | |
2028 | const uint64_t lma = ((*p)->has_load_address() | |
2029 | ? (*p)->load_address() | |
2030 | : vma); | |
2031 | const uint64_t size = (*p)->current_data_size(); | |
2032 | ||
2033 | bool need_new_segment; | |
2034 | if (current_seg == NULL) | |
2035 | need_new_segment = true; | |
2036 | else if (lma - vma != last_lma - last_vma) | |
2037 | { | |
2038 | // This section has a different LMA relationship than the | |
2039 | // last one; we need a new segment. | |
2040 | need_new_segment = true; | |
2041 | } | |
2042 | else if (align_address(last_lma + last_size, abi_pagesize) | |
2043 | < align_address(lma, abi_pagesize)) | |
2044 | { | |
2045 | // Putting this section in the segment would require | |
2046 | // skipping a page. | |
2047 | need_new_segment = true; | |
2048 | } | |
2049 | else if (is_bss_section(*plast) && !is_bss_section(*p)) | |
2050 | { | |
2051 | // A non-BSS section can not follow a BSS section in the | |
2052 | // same segment. | |
2053 | need_new_segment = true; | |
2054 | } | |
2055 | else if (is_current_seg_readonly | |
2056 | && ((*p)->flags() & elfcpp::SHF_WRITE) != 0) | |
2057 | { | |
2058 | // Don't put a writable section in the same segment as a | |
2059 | // non-writable section. | |
2060 | need_new_segment = true; | |
2061 | } | |
2062 | else | |
2063 | { | |
2064 | // Otherwise, reuse the existing segment. | |
2065 | need_new_segment = false; | |
2066 | } | |
2067 | ||
2068 | elfcpp::Elf_Word seg_flags = | |
2069 | Layout::section_flags_to_segment((*p)->flags()); | |
2070 | ||
2071 | if (need_new_segment) | |
2072 | { | |
2073 | current_seg = layout->make_output_segment(elfcpp::PT_LOAD, | |
2074 | seg_flags); | |
2075 | current_seg->set_addresses(vma, lma); | |
2076 | if (first_seg == NULL) | |
2077 | first_seg = current_seg; | |
2078 | is_current_seg_readonly = true; | |
2079 | } | |
2080 | ||
2081 | current_seg->add_output_section(*p, seg_flags); | |
2082 | ||
2083 | if (((*p)->flags() & elfcpp::SHF_WRITE) != 0) | |
2084 | is_current_seg_readonly = false; | |
2085 | ||
2086 | plast = p; | |
2087 | last_vma = vma; | |
2088 | last_lma = lma; | |
2089 | last_size = size; | |
2090 | } | |
2091 | ||
2092 | // An ELF program should work even if the program headers are not in | |
2093 | // a PT_LOAD segment. However, it appears that the Linux kernel | |
2094 | // does not set the AT_PHDR auxiliary entry in that case. It sets | |
2095 | // the load address to p_vaddr - p_offset of the first PT_LOAD | |
2096 | // segment. It then sets AT_PHDR to the load address plus the | |
2097 | // offset to the program headers, e_phoff in the file header. This | |
2098 | // fails when the program headers appear in the file before the | |
2099 | // first PT_LOAD segment. Therefore, we always create a PT_LOAD | |
2100 | // segment to hold the file header and the program headers. This is | |
2101 | // effectively what the GNU linker does, and it is slightly more | |
2102 | // efficient in any case. We try to use the first PT_LOAD segment | |
2103 | // if we can, otherwise we make a new one. | |
2104 | ||
2105 | size_t segment_count = layout->segment_count(); | |
2106 | size_t file_header_size; | |
2107 | size_t segment_headers_size; | |
2108 | if (parameters->get_size() == 32) | |
2109 | { | |
2110 | file_header_size = elfcpp::Elf_sizes<32>::ehdr_size; | |
2111 | segment_headers_size = segment_count * elfcpp::Elf_sizes<32>::phdr_size; | |
2112 | } | |
2113 | else if (parameters->get_size() == 64) | |
2114 | { | |
2115 | file_header_size = elfcpp::Elf_sizes<64>::ehdr_size; | |
2116 | segment_headers_size = segment_count * elfcpp::Elf_sizes<64>::phdr_size; | |
2117 | } | |
2118 | else | |
2119 | gold_unreachable(); | |
2120 | ||
2121 | if (first_seg != NULL | |
2122 | && ((first_seg->paddr() & (abi_pagesize - 1)) | |
2123 | >= file_header_size + segment_headers_size)) | |
2124 | return first_seg; | |
2125 | ||
2126 | Output_segment* load_seg = layout->make_output_segment(elfcpp::PT_LOAD, | |
2127 | elfcpp::PF_R); | |
2128 | if (first_seg == NULL) | |
2129 | load_seg->set_addresses(0, 0); | |
2130 | else | |
2131 | { | |
2132 | uint64_t vma = first_seg->vaddr(); | |
2133 | uint64_t lma = first_seg->paddr(); | |
2134 | ||
2135 | if (lma >= file_header_size + segment_headers_size | |
2136 | && lma >= abi_pagesize) | |
2137 | { | |
2138 | // We want a segment with the same relationship between VMA | |
2139 | // and LMA, but with enough room for the headers. | |
2140 | uint64_t size_for_page = align_address((file_header_size | |
2141 | + segment_headers_size), | |
2142 | abi_pagesize); | |
2143 | load_seg->set_addresses(vma - size_for_page, lma - size_for_page); | |
2144 | } | |
2145 | else | |
2146 | { | |
2147 | // We could handle this case by create the file header | |
2148 | // outside of any PT_LOAD segment, and creating a new | |
2149 | // PT_LOAD segment after the others to hold the segment | |
2150 | // headers. | |
2151 | gold_error(_("sections loaded on first page without room for " | |
2152 | "file and program headers are not supported")); | |
2153 | } | |
2154 | } | |
2155 | ||
2156 | return load_seg; | |
2157 | } | |
2158 | ||
2159 | // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS | |
2160 | // segment if there are any SHT_TLS sections. | |
2161 | ||
2162 | void | |
2163 | Script_sections::create_note_and_tls_segments( | |
2164 | Layout* layout, | |
2165 | const Layout::Section_list* sections) | |
2166 | { | |
2167 | bool saw_tls = false; | |
2168 | for (Layout::Section_list::const_iterator p = sections->begin(); | |
2169 | p != sections->end(); | |
2170 | ++p) | |
2171 | { | |
2172 | if ((*p)->type() == elfcpp::SHT_NOTE) | |
2173 | { | |
2174 | elfcpp::Elf_Word seg_flags = | |
2175 | Layout::section_flags_to_segment((*p)->flags()); | |
2176 | Output_segment* oseg = layout->make_output_segment(elfcpp::PT_NOTE, | |
2177 | seg_flags); | |
2178 | oseg->add_output_section(*p, seg_flags); | |
2179 | ||
2180 | // Incorporate any subsequent SHT_NOTE sections, in the | |
2181 | // hopes that the script is sensible. | |
2182 | Layout::Section_list::const_iterator pnext = p + 1; | |
2183 | while (pnext != sections->end() | |
2184 | && (*pnext)->type() == elfcpp::SHT_NOTE) | |
2185 | { | |
2186 | seg_flags = Layout::section_flags_to_segment((*pnext)->flags()); | |
2187 | oseg->add_output_section(*pnext, seg_flags); | |
2188 | p = pnext; | |
2189 | ++pnext; | |
2190 | } | |
2191 | } | |
2192 | ||
2193 | if (((*p)->flags() & elfcpp::SHF_TLS) != 0) | |
2194 | { | |
2195 | if (saw_tls) | |
2196 | gold_error(_("TLS sections are not adjacent")); | |
2197 | ||
2198 | elfcpp::Elf_Word seg_flags = | |
2199 | Layout::section_flags_to_segment((*p)->flags()); | |
2200 | Output_segment* oseg = layout->make_output_segment(elfcpp::PT_TLS, | |
2201 | seg_flags); | |
2202 | oseg->add_output_section(*p, seg_flags); | |
2203 | ||
2204 | Layout::Section_list::const_iterator pnext = p + 1; | |
2205 | while (pnext != sections->end() | |
2206 | && ((*pnext)->flags() & elfcpp::SHF_TLS) != 0) | |
2207 | { | |
2208 | seg_flags = Layout::section_flags_to_segment((*pnext)->flags()); | |
2209 | oseg->add_output_section(*pnext, seg_flags); | |
2210 | p = pnext; | |
2211 | ++pnext; | |
2212 | } | |
2213 | ||
2214 | saw_tls = true; | |
2215 | } | |
2216 | } | |
2217 | } | |
2218 | ||
494e05f4 ILT |
2219 | // Print the SECTIONS clause to F for debugging. |
2220 | ||
2221 | void | |
2222 | Script_sections::print(FILE* f) const | |
2223 | { | |
2224 | if (!this->saw_sections_clause_) | |
2225 | return; | |
2226 | ||
2227 | fprintf(f, "SECTIONS {\n"); | |
2228 | ||
2229 | for (Sections_elements::const_iterator p = this->sections_elements_->begin(); | |
2230 | p != this->sections_elements_->end(); | |
2231 | ++p) | |
2232 | (*p)->print(f); | |
2233 | ||
2234 | fprintf(f, "}\n"); | |
2235 | } | |
2236 | ||
2237 | } // End namespace gold. |