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