OLD | NEW |
1 // output.cc -- manage the output file for gold | 1 // output.cc -- manage the output file for gold |
2 | 2 |
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc. | 3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
4 // Written by Ian Lance Taylor <iant@google.com>. | 4 // Written by Ian Lance Taylor <iant@google.com>. |
5 | 5 |
6 // This file is part of gold. | 6 // This file is part of gold. |
7 | 7 |
8 // This program is free software; you can redistribute it and/or modify | 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 | 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 | 10 // the Free Software Foundation; either version 3 of the License, or |
(...skipping 1782 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1793 found_in_sections_clause_(false), | 1793 found_in_sections_clause_(false), |
1794 has_load_address_(false), | 1794 has_load_address_(false), |
1795 info_uses_section_index_(false), | 1795 info_uses_section_index_(false), |
1796 may_sort_attached_input_sections_(false), | 1796 may_sort_attached_input_sections_(false), |
1797 must_sort_attached_input_sections_(false), | 1797 must_sort_attached_input_sections_(false), |
1798 attached_input_sections_are_sorted_(false), | 1798 attached_input_sections_are_sorted_(false), |
1799 is_relro_(false), | 1799 is_relro_(false), |
1800 is_relro_local_(false), | 1800 is_relro_local_(false), |
1801 is_small_section_(false), | 1801 is_small_section_(false), |
1802 is_large_section_(false), | 1802 is_large_section_(false), |
| 1803 is_interp_(false), |
| 1804 is_dynamic_linker_section_(false), |
| 1805 generate_code_fills_at_write_(false), |
1803 tls_offset_(0), | 1806 tls_offset_(0), |
1804 checkpoint_(NULL), | 1807 checkpoint_(NULL), |
1805 merge_section_map_(), | 1808 merge_section_map_(), |
1806 merge_section_by_properties_map_(), | 1809 merge_section_by_properties_map_(), |
1807 relaxed_input_section_map_(), | 1810 relaxed_input_section_map_(), |
1808 is_relaxed_input_section_map_valid_(true), | 1811 is_relaxed_input_section_map_valid_(true) |
1809 generate_code_fills_at_write_(false) | |
1810 { | 1812 { |
1811 // An unallocated section has no address. Forcing this means that | 1813 // An unallocated section has no address. Forcing this means that |
1812 // we don't need special treatment for symbols defined in debug | 1814 // we don't need special treatment for symbols defined in debug |
1813 // sections. | 1815 // sections. |
1814 if ((flags & elfcpp::SHF_ALLOC) == 0) | 1816 if ((flags & elfcpp::SHF_ALLOC) == 0) |
1815 this->set_address(0); | 1817 this->set_address(0); |
1816 } | 1818 } |
1817 | 1819 |
1818 Output_section::~Output_section() | 1820 Output_section::~Output_section() |
1819 { | 1821 { |
(...skipping 1218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3038 is_max_align_known_(false), | 3040 is_max_align_known_(false), |
3039 are_addresses_set_(false), | 3041 are_addresses_set_(false), |
3040 is_large_data_segment_(false) | 3042 is_large_data_segment_(false) |
3041 { | 3043 { |
3042 } | 3044 } |
3043 | 3045 |
3044 // Add an Output_section to an Output_segment. | 3046 // Add an Output_section to an Output_segment. |
3045 | 3047 |
3046 void | 3048 void |
3047 Output_segment::add_output_section(Output_section* os, | 3049 Output_segment::add_output_section(Output_section* os, |
3048 » » » » elfcpp::Elf_Word seg_flags) | 3050 » » » » elfcpp::Elf_Word seg_flags, |
| 3051 » » » » bool do_sort) |
3049 { | 3052 { |
3050 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0); | 3053 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0); |
3051 gold_assert(!this->is_max_align_known_); | 3054 gold_assert(!this->is_max_align_known_); |
3052 gold_assert(os->is_large_data_section() == this->is_large_data_segment()); | 3055 gold_assert(os->is_large_data_section() == this->is_large_data_segment()); |
| 3056 gold_assert(this->type() == elfcpp::PT_LOAD || !do_sort); |
3053 | 3057 |
3054 // Update the segment flags. | 3058 // Update the segment flags. |
3055 this->flags_ |= seg_flags; | 3059 this->flags_ |= seg_flags; |
3056 | 3060 |
3057 Output_segment::Output_data_list* pdl; | 3061 Output_segment::Output_data_list* pdl; |
3058 if (os->type() == elfcpp::SHT_NOBITS) | 3062 if (os->type() == elfcpp::SHT_NOBITS) |
3059 pdl = &this->output_bss_; | 3063 pdl = &this->output_bss_; |
3060 else | 3064 else |
3061 pdl = &this->output_data_; | 3065 pdl = &this->output_data_; |
3062 | 3066 |
3063 // So that PT_NOTE segments will work correctly, we need to ensure | |
3064 // that all SHT_NOTE sections are adjacent. This will normally | |
3065 // happen automatically, because all the SHT_NOTE input sections | |
3066 // will wind up in the same output section. However, it is possible | |
3067 // for multiple SHT_NOTE input sections to have different section | |
3068 // flags, and thus be in different output sections, but for the | |
3069 // different section flags to map into the same segment flags and | |
3070 // thus the same output segment. | |
3071 | |
3072 // Note that while there may be many input sections in an output | 3067 // Note that while there may be many input sections in an output |
3073 // section, there are normally only a few output sections in an | 3068 // section, there are normally only a few output sections in an |
3074 // output segment. This loop is expected to be fast. | 3069 // output segment. The loops below are expected to be fast. |
3075 | 3070 |
| 3071 // So that PT_NOTE segments will work correctly, we need to ensure |
| 3072 // that all SHT_NOTE sections are adjacent. |
3076 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty()) | 3073 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty()) |
3077 { | 3074 { |
3078 Output_segment::Output_data_list::iterator p = pdl->end(); | 3075 Output_segment::Output_data_list::iterator p = pdl->end(); |
3079 do | 3076 do |
3080 { | 3077 { |
3081 --p; | 3078 --p; |
3082 if ((*p)->is_section_type(elfcpp::SHT_NOTE)) | 3079 if ((*p)->is_section_type(elfcpp::SHT_NOTE)) |
3083 { | 3080 { |
3084 ++p; | 3081 ++p; |
3085 pdl->insert(p, os); | 3082 pdl->insert(p, os); |
3086 return; | 3083 return; |
3087 } | 3084 } |
3088 } | 3085 } |
3089 while (p != pdl->begin()); | 3086 while (p != pdl->begin()); |
3090 } | 3087 } |
3091 | 3088 |
3092 // Similarly, so that PT_TLS segments will work, we need to group | 3089 // Similarly, so that PT_TLS segments will work, we need to group |
3093 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special | 3090 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special |
3094 // case: we group the SHF_TLS/SHT_NOBITS sections right after the | 3091 // case: we group the SHF_TLS/SHT_NOBITS sections right after the |
3095 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS | 3092 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS |
3096 // correctly. SHF_TLS sections get added to both a PT_LOAD segment | 3093 // correctly. SHF_TLS sections get added to both a PT_LOAD segment |
3097 // and the PT_TLS segment -- we do this grouping only for the | 3094 // and the PT_TLS segment; we do this grouping only for the PT_LOAD |
3098 // PT_LOAD segment. | 3095 // segment. |
3099 if (this->type_ != elfcpp::PT_TLS | 3096 if (this->type_ != elfcpp::PT_TLS |
3100 && (os->flags() & elfcpp::SHF_TLS) != 0) | 3097 && (os->flags() & elfcpp::SHF_TLS) != 0) |
3101 { | 3098 { |
3102 pdl = &this->output_data_; | 3099 pdl = &this->output_data_; |
3103 if (!pdl->empty()) | 3100 if (!pdl->empty()) |
3104 { | 3101 { |
3105 bool nobits = os->type() == elfcpp::SHT_NOBITS; | 3102 bool nobits = os->type() == elfcpp::SHT_NOBITS; |
3106 bool sawtls = false; | 3103 bool sawtls = false; |
3107 Output_segment::Output_data_list::iterator p = pdl->end(); | 3104 Output_segment::Output_data_list::iterator p = pdl->end(); |
3108 gold_assert(p != pdl->begin()); | 3105 gold_assert(p != pdl->begin()); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3218 if ((*p)->is_section() | 3215 if ((*p)->is_section() |
3219 && (*p)->output_section()->is_large_section()) | 3216 && (*p)->output_section()->is_large_section()) |
3220 { | 3217 { |
3221 pdl->insert(p, os); | 3218 pdl->insert(p, os); |
3222 return; | 3219 return; |
3223 } | 3220 } |
3224 } | 3221 } |
3225 gold_unreachable(); | 3222 gold_unreachable(); |
3226 } | 3223 } |
3227 | 3224 |
| 3225 // We do some further output section sorting in order to make the |
| 3226 // generated program run more efficiently. We should only do this |
| 3227 // when not using a linker script, so it is controled by the DO_SORT |
| 3228 // parameter. |
| 3229 if (do_sort) |
| 3230 { |
| 3231 // FreeBSD requires the .interp section to be in the first page |
| 3232 // of the executable. That is a more efficient location anyhow |
| 3233 // for any OS, since it means that the kernel will have the data |
| 3234 // handy after it reads the program headers. |
| 3235 if (os->is_interp() && !pdl->empty()) |
| 3236 { |
| 3237 pdl->insert(pdl->begin(), os); |
| 3238 return; |
| 3239 } |
| 3240 |
| 3241 // Put loadable non-writable notes immediately after the .interp |
| 3242 // sections, so that the PT_NOTE segment is on the first page of |
| 3243 // the executable. |
| 3244 if (os->type() == elfcpp::SHT_NOTE |
| 3245 && (os->flags() & elfcpp::SHF_WRITE) == 0 |
| 3246 && !pdl->empty()) |
| 3247 { |
| 3248 Output_segment::Output_data_list::iterator p = pdl->begin(); |
| 3249 if ((*p)->is_section() && (*p)->output_section()->is_interp()) |
| 3250 ++p; |
| 3251 pdl->insert(p, os); |
| 3252 return; |
| 3253 } |
| 3254 |
| 3255 // If this section is used by the dynamic linker, and it is not |
| 3256 // writable, then put it first, after the .interp section and |
| 3257 // any loadable notes. This makes it more likely that the |
| 3258 // dynamic linker will have to read less data from the disk. |
| 3259 if (os->is_dynamic_linker_section() |
| 3260 && !pdl->empty() |
| 3261 && (os->flags() & elfcpp::SHF_WRITE) == 0) |
| 3262 { |
| 3263 bool is_reloc = (os->type() == elfcpp::SHT_REL |
| 3264 || os->type() == elfcpp::SHT_RELA); |
| 3265 Output_segment::Output_data_list::iterator p = pdl->begin(); |
| 3266 while (p != pdl->end() |
| 3267 && (*p)->is_section() |
| 3268 && ((*p)->output_section()->is_dynamic_linker_section() |
| 3269 || (*p)->output_section()->type() == elfcpp::SHT_NOTE)) |
| 3270 { |
| 3271 // Put reloc sections after the other ones. Putting the |
| 3272 // dynamic reloc sections first confuses BFD, notably |
| 3273 // objcopy and strip. |
| 3274 if (!is_reloc |
| 3275 && ((*p)->output_section()->type() == elfcpp::SHT_REL |
| 3276 || (*p)->output_section()->type() == elfcpp::SHT_RELA)) |
| 3277 break; |
| 3278 ++p; |
| 3279 } |
| 3280 pdl->insert(p, os); |
| 3281 return; |
| 3282 } |
| 3283 } |
| 3284 |
| 3285 // If there were no constraints on the output section, just add it |
| 3286 // to the end of the list. |
3228 pdl->push_back(os); | 3287 pdl->push_back(os); |
3229 } | 3288 } |
3230 | 3289 |
3231 // Remove an Output_section from this segment. It is an error if it | 3290 // Remove an Output_section from this segment. It is an error if it |
3232 // is not present. | 3291 // is not present. |
3233 | 3292 |
3234 void | 3293 void |
3235 Output_segment::remove_output_section(Output_section* os) | 3294 Output_segment::remove_output_section(Output_section* os) |
3236 { | 3295 { |
3237 // We only need this for SHT_PROGBITS. | 3296 // We only need this for SHT_PROGBITS. |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3496 // better not have moved backward. | 3555 // better not have moved backward. |
3497 if ((*p)->address() >= addr + (off - startoff)) | 3556 if ((*p)->address() >= addr + (off - startoff)) |
3498 off += (*p)->address() - (addr + (off - startoff)); | 3557 off += (*p)->address() - (addr + (off - startoff)); |
3499 else | 3558 else |
3500 { | 3559 { |
3501 if (!layout->script_options()->saw_sections_clause()) | 3560 if (!layout->script_options()->saw_sections_clause()) |
3502 gold_unreachable(); | 3561 gold_unreachable(); |
3503 else | 3562 else |
3504 { | 3563 { |
3505 Output_section* os = (*p)->output_section(); | 3564 Output_section* os = (*p)->output_section(); |
| 3565 |
| 3566 // Cast to unsigned long long to avoid format warnings. |
| 3567 unsigned long long previous_dot = |
| 3568 static_cast<unsigned long long>(addr + (off - startoff)); |
| 3569 unsigned long long dot = |
| 3570 static_cast<unsigned long long>((*p)->address()); |
| 3571 |
3506 if (os == NULL) | 3572 if (os == NULL) |
3507 gold_error(_("dot moves backward in linker script " | 3573 gold_error(_("dot moves backward in linker script " |
3508 » » » » "from 0x%llx to 0x%llx"), | 3574 » » » » "from 0x%llx to 0x%llx"), previous_dot, dot); |
3509 » » » addr + (off - startoff), (*p)->address()); | |
3510 else | 3575 else |
3511 gold_error(_("address of section '%s' moves backward " | 3576 gold_error(_("address of section '%s' moves backward " |
3512 "from 0x%llx to 0x%llx"), | 3577 "from 0x%llx to 0x%llx"), |
3513 » » » os->name(), addr + (off - startoff), | 3578 » » » os->name(), previous_dot, dot); |
3514 » » » (*p)->address()); | |
3515 } | 3579 } |
3516 } | 3580 } |
3517 (*p)->set_file_offset(off); | 3581 (*p)->set_file_offset(off); |
3518 (*p)->finalize_data_size(); | 3582 (*p)->finalize_data_size(); |
3519 } | 3583 } |
3520 | 3584 |
3521 // We want to ignore the size of a SHF_TLS or SHT_NOBITS | 3585 // We want to ignore the size of a SHF_TLS or SHT_NOBITS |
3522 // section. Such a section does not affect the size of a | 3586 // section. Such a section does not affect the size of a |
3523 // PT_LOAD segment. | 3587 // PT_LOAD segment. |
3524 if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS) | 3588 if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS) |
(...skipping 829 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4354 template | 4418 template |
4355 class Output_data_got<64, false>; | 4419 class Output_data_got<64, false>; |
4356 #endif | 4420 #endif |
4357 | 4421 |
4358 #ifdef HAVE_TARGET_64_BIG | 4422 #ifdef HAVE_TARGET_64_BIG |
4359 template | 4423 template |
4360 class Output_data_got<64, true>; | 4424 class Output_data_got<64, true>; |
4361 #endif | 4425 #endif |
4362 | 4426 |
4363 } // End namespace gold. | 4427 } // End namespace gold. |
OLD | NEW |