Index: gold/gold.cc =================================================================== RCS file: /cvs/src/src/gold/gold.cc,v retrieving revision 1.100 diff -u -u -p -r1.100 gold.cc --- gold/gold.cc 16 Jul 2012 19:00:18 -0000 1.100 +++ gold/gold.cc 9 Aug 2012 21:28:02 -0000 @@ -530,11 +530,13 @@ queue_middle_tasks(const General_options // Call Object::layout for the second time to determine the // output_sections for all referenced input sections. When - // --gc-sections or --icf is turned on, Object::layout is - // called twice. It is called the first time when the - // symbols are added. + // --gc-sections or --icf is turned on, or when certain input + // sections have to be mapped to unique segments, Object::layout + // is called twice. It is called the first time when symbols + // are added. if (parameters->options().gc_sections() - || parameters->options().icf_enabled()) + || parameters->options().icf_enabled() + || layout->is_unique_segment_for_sections_specified()) { for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); p != input_objects->relobj_end(); Index: gold/layout.cc =================================================================== RCS file: /cvs/src/src/gold/layout.cc,v retrieving revision 1.232 diff -u -u -p -r1.232 layout.cc --- gold/layout.cc 7 Aug 2012 13:24:47 -0000 1.232 +++ gold/layout.cc 9 Aug 2012 21:28:02 -0000 @@ -408,12 +408,14 @@ Layout::Layout(int number_of_input_files resized_signatures_(false), have_stabstr_section_(false), section_ordering_specified_(false), + unique_segment_for_sections_specified_(false), incremental_inputs_(NULL), record_output_section_data_from_script_(false), script_output_section_data_list_(), segment_states_(NULL), relaxation_debug_check_(NULL), section_order_map_(), + section_segment_map_(), input_section_position_(), input_section_glob_(), incremental_base_(NULL), @@ -1028,9 +1030,48 @@ Layout::layout(Sized_relobj_filechoose_output_section(object, name, sh_type, - shdr.get_sh_flags(), true, - ORDER_INVALID, false); + // Plugins can choose to place one or more subsets of sections in + // unique segments and this is done by mapping these section subsets + // to unique output sections. Check if this section needs to be + // remapped to a unique output section. + Section_segment_map::iterator it + = this->section_segment_map_.find(Const_section_id(object, shndx)); + if (it != this->section_segment_map_.end()) + { + // We know the name of the output section, directly call + // get_output_section here by-passing choose_output_section. + elfcpp::Elf_Xword flags = shdr.get_sh_flags(); + // Some flags in the input section should not be automatically + // copied to the output section. + flags &= ~ (elfcpp::SHF_INFO_LINK + | elfcpp::SHF_GROUP + | elfcpp::SHF_MERGE + | elfcpp::SHF_STRINGS); + + // We only clear the SHF_LINK_ORDER flag in for + // a non-relocatable link. + if (!parameters->options().relocatable()) + flags &= ~elfcpp::SHF_LINK_ORDER; + + const char* os_name = it->second->name; + Stringpool::Key name_key; + os_name = this->namepool_.add_with_length(os_name, strlen(os_name), + true, &name_key); + os = this->get_output_section(os_name, name_key, sh_type, flags, + ORDER_INVALID, false); + if (!os->is_unique_segment()) + { + os->set_is_unique_segment(); + os->set_extra_segment_flags(it->second->flags); + os->set_segment_alignment(it->second->align); + } + } + else + { + os = this->choose_output_section(object, name, sh_type, + shdr.get_sh_flags(), true, + ORDER_INVALID, false); + } if (os == NULL) return NULL; } @@ -1692,6 +1733,10 @@ Layout::attach_allocated_section_to_segm elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags); + // If this output section's segment has extra flags that need to be set, + // coming from a linker plugin, do that. + seg_flags |= os->extra_segment_flags(); + // Check for --section-start. uint64_t addr; bool is_address_set = parameters->options().section_start(os->name(), &addr); @@ -1708,8 +1753,14 @@ Layout::attach_allocated_section_to_segm p != this->segment_list_.end(); ++p) { + // No need to go through the loop if a unique segment is needed. + if (os->is_unique_segment()) + break; if ((*p)->type() != elfcpp::PT_LOAD) continue; + // If this segment is marked unique, skip. + if ((*p)->is_unique_segment()) + continue; if (!parameters->options().omagic() && ((*p)->flags() & elfcpp::PF_W) != (seg_flags & elfcpp::PF_W)) continue; @@ -1742,7 +1793,8 @@ Layout::attach_allocated_section_to_segm break; } - if (p == this->segment_list_.end()) + if (p == this->segment_list_.end() + || os->is_unique_segment()) { Output_segment* oseg = this->make_output_segment(elfcpp::PT_LOAD, seg_flags); @@ -1751,6 +1803,14 @@ Layout::attach_allocated_section_to_segm oseg->add_output_section_to_load(this, os, seg_flags); if (is_address_set) oseg->set_addresses(addr, addr); + // Check if segment should be marked unique. For segments marked + // unique by linker plugins, set the new alignment if specified. + if (os->is_unique_segment()) + { + oseg->set_is_unique_segment(); + if (os->segment_alignment()) + oseg->set_minimum_p_align(os->segment_alignment()); + } } // If we see a loadable SHT_NOTE section, we create a PT_NOTE @@ -3095,9 +3155,11 @@ Layout::segment_precedes(const Output_se // We shouldn't get here--we shouldn't create segments which we // can't distinguish. Unless of course we are using a weird linker - // script or overlapping --section-start options. + // script or overlapping --section-start options. We could also get + // here if plugins want unique segments for subsets of sections. gold_assert(this->script_options_->saw_phdrs_clause() - || parameters->options().any_section_start()); + || parameters->options().any_section_start() + || this->is_unique_segment_for_sections_specified()); return false; } Index: gold/layout.h =================================================================== RCS file: /cvs/src/src/gold/layout.h,v retrieving revision 1.103 diff -u -u -p -r1.103 layout.h --- gold/layout.h 2 May 2012 21:37:23 -0000 1.103 +++ gold/layout.h 9 Aug 2012 21:28:02 -0000 @@ -528,6 +528,29 @@ class Layout get_section_order_map() { return &this->section_order_map_; } + // Struct to store segment info when mapping some input sections to + // unique segments using linker plugins. Mapping an input section to + // a unique segment is done by first placing such input sections in + // unique output sections and then mapping the output section to a + // unique segment. NAME is the name of the output section. FLAGS + // and ALIGN are the extra flags and alignment of the segment. + typedef struct + { + // Identifier for the Segment. ELF Segments dont have names. + const char* name; + // Segment flags. + uint64_t flags; + uint64_t align; + } Unique_segment_info; + + // Mapping from input section to segment. + typedef std::map + Section_segment_map; + + Section_segment_map& + get_section_segment_map() + { return this->section_segment_map_; } + bool is_section_ordering_specified() { return this->section_ordering_specified_; } @@ -536,6 +559,14 @@ class Layout set_section_ordering_specified() { this->section_ordering_specified_ = true; } + bool + is_unique_segment_for_sections_specified() const + { return this->unique_segment_for_sections_specified_; } + + void + set_unique_segment_for_sections_specified() + { this->unique_segment_for_sections_specified_ = true; } + // For incremental updates, allocate a block of memory from the // free list. Find a block starting at or after MINOFF. off_t @@ -1331,6 +1362,9 @@ class Layout // True if the input sections in the output sections should be sorted // as specified in a section ordering file. bool section_ordering_specified_; + // True if some input sections need to be mapped to a unique segment, + // after being mapped to a unique Output_section. + bool unique_segment_for_sections_specified_; // In incremental build, holds information check the inputs and build the // .gnu_incremental_inputs section. Incremental_inputs* incremental_inputs_; @@ -1345,6 +1379,11 @@ class Layout // Plugins specify section_ordering using this map. This is set in // update_section_order in plugin.cc std::map section_order_map_; + // This maps an input section to a unique segment. This is done by first + // placing such input sections in unique output sections and then mapping + // the output section to a unique segment. Unique_segment_info stores + // any additional flags and alignment of the new segment. + Section_segment_map section_segment_map_; // Hash a pattern to its position in the section ordering file. Unordered_map input_section_position_; // Vector of glob only patterns in the section_ordering file. Index: gold/object.cc =================================================================== RCS file: /cvs/src/src/gold/object.cc,v retrieving revision 1.155 diff -u -u -p -r1.155 object.cc --- gold/object.cc 2 May 2012 21:37:23 -0000 1.155 +++ gold/object.cc 9 Aug 2012 21:28:03 -0000 @@ -1164,15 +1164,19 @@ Sized_relobj_file::lay // whether they should be included in the link. If they should, we // pass them to the Layout object, which will return an output section // and an offset. -// During garbage collection (--gc-sections) and identical code folding -// (--icf), this function is called twice. When it is called the first -// time, it is for setting up some sections as roots to a work-list for -// --gc-sections and to do comdat processing. Actual layout happens the -// second time around after all the relevant sections have been determined. -// The first time, is_worklist_ready or is_icf_ready is false. It is then -// set to true after the garbage collection worklist or identical code -// folding is processed and the relevant sections to be kept are -// determined. Then, this function is called again to layout the sections. +// This function is called twice sometimes, two passes, when mapping +// of input sections to output sections must be delayed. +// This is true for the following : +// * Garbage collection (--gc-sections): Some input sections will be +// discarded and hence the assignment must wait until the second pass. +// In the first pass, it is for setting up some sections as roots to +// a work-list for --gc-sections and to do comdat processing. +// * Identical Code Folding (--icf=): Some input sections +// will be folded and hence the assignment must wait. +// * Using plugins to map some sections to unique segments: Mapping +// some sections to unique segments requires mapping them to unique +// output sections too. This can be done via plugins now and this +// information is not available in the first pass. template void @@ -1181,26 +1185,44 @@ Sized_relobj_file::do_ Read_symbols_data* sd) { const unsigned int shnum = this->shnum(); - bool is_gc_pass_one = ((parameters->options().gc_sections() - && !symtab->gc()->is_worklist_ready()) - || (parameters->options().icf_enabled() - && !symtab->icf()->is_icf_ready())); - - bool is_gc_pass_two = ((parameters->options().gc_sections() - && symtab->gc()->is_worklist_ready()) - || (parameters->options().icf_enabled() - && symtab->icf()->is_icf_ready())); - bool is_gc_or_icf = (parameters->options().gc_sections() - || parameters->options().icf_enabled()); + /* Should this function be called twice? */ + bool is_two_pass = (parameters->options().gc_sections() + || parameters->options().icf_enabled() + || layout->is_unique_segment_for_sections_specified()); + + /* Only one of is_pass_one and is_pass_two is true. Both are false when + a two-pass approach is not needed. */ + bool is_pass_one = false; + bool is_pass_two = false; - // Both is_gc_pass_one and is_gc_pass_two should not be true. - gold_assert(!(is_gc_pass_one && is_gc_pass_two)); + Symbols_data* gc_sd = NULL; + /* Check if do_layout needs to be two-pass. If so, find out which pass + should happen. In the first pass, the data in sd is saved to be used + later in the second pass. */ + if (is_two_pass) + { + gc_sd = this->get_symbols_data(); + if (gc_sd == NULL) + { + gold_assert (sd != NULL); + is_pass_one = true; + } + else + { + if (parameters->options().gc_sections()) + gold_assert(symtab->gc()->is_worklist_ready()); + if (parameters->options().icf_enabled()) + gold_assert(symtab->icf()->is_icf_ready()); + is_pass_two = true; + } + } + if (shnum == 0) return; - Symbols_data* gc_sd = NULL; - if (is_gc_pass_one) + + if (is_pass_one) { // During garbage collection save the symbols data to use it when // re-entering this function. @@ -1208,10 +1230,6 @@ Sized_relobj_file::do_ this->copy_symbols_data(gc_sd, sd, This::shdr_size * shnum); this->set_symbols_data(gc_sd); } - else if (is_gc_pass_two) - { - gc_sd = this->get_symbols_data(); - } const unsigned char* section_headers_data = NULL; section_size_type section_names_size; @@ -1220,7 +1238,7 @@ Sized_relobj_file::do_ const unsigned char* symbol_names_data = NULL; section_size_type symbol_names_size; - if (is_gc_or_icf) + if (is_two_pass) { section_headers_data = gc_sd->section_headers_data; section_names_size = gc_sd->section_names_size; @@ -1246,7 +1264,7 @@ Sized_relobj_file::do_ const unsigned char* pshdrs; // Get the section names. - const unsigned char* pnamesu = (is_gc_or_icf) + const unsigned char* pnamesu = (is_two_pass) ? gc_sd->section_names_data : sd->section_names->data(); @@ -1298,7 +1316,7 @@ Sized_relobj_file::do_ Output_sections& out_sections(this->output_sections()); std::vector
& out_section_offsets(this->section_offsets()); - if (!is_gc_pass_two) + if (!is_pass_two) { out_sections.resize(shnum); out_section_offsets.resize(shnum); @@ -1308,7 +1326,7 @@ Sized_relobj_file::do_ // do here. if (this->input_file()->just_symbols()) { - if (!is_gc_pass_two) + if (!is_pass_two) { delete sd->section_headers; sd->section_headers = NULL; @@ -1360,7 +1378,7 @@ Sized_relobj_file::do_ const char* name = pnames + shdr.get_sh_name(); - if (!is_gc_pass_two) + if (!is_pass_two) { if (this->handle_gnu_warning_section(name, i, symtab)) { @@ -1434,7 +1452,7 @@ Sized_relobj_file::do_ } } - if (is_gc_pass_one && parameters->options().gc_sections()) + if (is_pass_one && parameters->options().gc_sections()) { if (this->is_section_name_included(name) || shdr.get_sh_type() == elfcpp::SHT_INIT_ARRAY @@ -1479,7 +1497,7 @@ Sized_relobj_file::do_ && strcmp(name, ".eh_frame") == 0 && this->check_eh_frame_flags(&shdr)) { - if (is_gc_pass_one) + if (is_pass_one) { out_sections[i] = reinterpret_cast(1); out_section_offsets[i] = invalid_address; @@ -1494,7 +1512,7 @@ Sized_relobj_file::do_ continue; } - if (is_gc_pass_two && parameters->options().gc_sections()) + if (is_pass_two && parameters->options().gc_sections()) { // This is executed during the second pass of garbage // collection. do_layout has been called before and some @@ -1519,7 +1537,7 @@ Sized_relobj_file::do_ } } - if (is_gc_pass_two && parameters->options().icf_enabled()) + if (is_pass_two && parameters->options().icf_enabled()) { if (out_sections[i] == NULL) { @@ -1553,7 +1571,7 @@ Sized_relobj_file::do_ // should_defer_layout should be false. if (should_defer_layout && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC)) { - gold_assert(!is_gc_pass_two); + gold_assert(!is_pass_two); this->deferred_layout_.push_back(Deferred_layout(i, name, pshdrs, reloc_shndx[i], @@ -1568,11 +1586,11 @@ Sized_relobj_file::do_ // During gc_pass_two if a section that was previously deferred is // found, do not layout the section as layout_deferred_sections will // do it later from gold.cc. - if (is_gc_pass_two + if (is_pass_two && (out_sections[i] == reinterpret_cast(2))) continue; - if (is_gc_pass_one) + if (is_pass_one) { // This is during garbage collection. The out_sections are // assigned in the second call to this function. @@ -1603,7 +1621,7 @@ Sized_relobj_file::do_ } } - if (!is_gc_pass_two) + if (!is_pass_two) layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags, this); // When doing a relocatable link handle the reloc sections at the @@ -1612,7 +1630,7 @@ Sized_relobj_file::do_ if (emit_relocs) this->size_relocatable_relocs(); - gold_assert(!(is_gc_or_icf) || reloc_sections.empty()); + gold_assert(!(is_two_pass) || reloc_sections.empty()); for (std::vector::const_iterator p = reloc_sections.begin(); p != reloc_sections.end(); @@ -1659,7 +1677,7 @@ Sized_relobj_file::do_ } // Handle the .eh_frame sections at the end. - gold_assert(!is_gc_pass_one || eh_frame_sections.empty()); + gold_assert(!is_pass_one || eh_frame_sections.empty()); for (std::vector::const_iterator p = eh_frame_sections.begin(); p != eh_frame_sections.end(); ++p) @@ -1682,7 +1700,7 @@ Sized_relobj_file::do_ // When building a .gdb_index section, scan the .debug_info and // .debug_types sections. - gold_assert(!is_gc_pass_one + gold_assert(!is_pass_one || (debug_info_sections.empty() && debug_types_sections.empty())); for (std::vector::const_iterator p = debug_info_sections.begin(); @@ -1703,7 +1721,7 @@ Sized_relobj_file::do_ i, reloc_shndx[i], reloc_type[i]); } - if (is_gc_pass_two) + if (is_pass_two) { delete[] gc_sd->section_headers_data; delete[] gc_sd->section_names_data; Index: gold/output.cc =================================================================== RCS file: /cvs/src/src/gold/output.cc,v retrieving revision 1.167 diff -u -u -p -r1.167 output.cc --- gold/output.cc 6 Jun 2012 22:12:47 -0000 1.167 +++ gold/output.cc 9 Aug 2012 21:28:03 -0000 @@ -2245,7 +2245,10 @@ Output_section::Output_section(const cha always_keeps_input_sections_(false), has_fixed_layout_(false), is_patch_space_allowed_(false), + is_unique_segment_(false), tls_offset_(0), + extra_segment_flags_(0), + segment_alignment_(0), checkpoint_(NULL), lookup_maps_(new Output_section_lookup_maps), free_list_(), @@ -3979,7 +3982,8 @@ Output_segment::Output_segment(elfcpp::E flags_(flags), is_max_align_known_(false), are_addresses_set_(false), - is_large_data_segment_(false) + is_large_data_segment_(false), + is_unique_segment_(false) { // The ELF ABI specifies that a PT_TLS segment always has PF_R as // the flags. Index: gold/output.h =================================================================== RCS file: /cvs/src/src/gold/output.h,v retrieving revision 1.135 diff -u -u -p -r1.135 output.h --- gold/output.h 10 Jul 2012 14:54:29 -0000 1.135 +++ gold/output.h 9 Aug 2012 21:28:03 -0000 @@ -3249,6 +3249,29 @@ class Output_section : public Output_dat requires_postprocessing() const { return this->requires_postprocessing_; } + bool + is_unique_segment() const + { return this->is_unique_segment_; } + + void + set_is_unique_segment() + { this->is_unique_segment_ = true; } + + uint64_t extra_segment_flags() const + { return extra_segment_flags_; } + + void + set_extra_segment_flags(uint64_t flags) + { extra_segment_flags_ = flags; } + + uint64_t segment_alignment() const + { return segment_alignment_; } + + void + set_segment_alignment(uint64_t align) + { segment_alignment_ = align; } + + // If a section requires postprocessing, return the buffer to use. unsigned char* postprocessing_buffer() const @@ -4200,9 +4223,17 @@ class Output_section : public Output_dat bool has_fixed_layout_ : 1; // True if we can add patch space to this section. bool is_patch_space_allowed_ : 1; + // True if this output section goes into a unique segment. + bool is_unique_segment_ : 1; // For SHT_TLS sections, the offset of this section relative to the base // of the TLS segment. uint64_t tls_offset_; + // Additional segment flags, specified via linker plugin, when mapping some + // input sections to unique segments. + uint64_t extra_segment_flags_; + // Segment alignment specified via linker plugin, when mapping some + // input sections to unique segments. + uint64_t segment_alignment_; // Saved checkpoint. Checkpoint_output_section* checkpoint_; // Fast lookup maps for merged and relaxed input sections. @@ -4278,6 +4309,16 @@ class Output_segment set_is_large_data_segment() { this->is_large_data_segment_ = true; } + bool + is_unique_segment() const + { return is_unique_segment_; } + + // Mark segment as unique, happens when linker plugins request that + // certain input sections be mapped to unique segments. + void + set_is_unique_segment() + { this->is_unique_segment_ = true; } + // Return the maximum alignment of the Output_data. uint64_t maximum_alignment(); @@ -4488,6 +4529,8 @@ class Output_segment bool are_addresses_set_ : 1; // Whether this segment holds large data sections. bool is_large_data_segment_ : 1; + // Whether this was marked as a unique segment via a linker plugin. + bool is_unique_segment_ : 1; }; // This class represents the output file. Index: gold/plugin.cc =================================================================== RCS file: /cvs/src/src/gold/plugin.cc,v retrieving revision 1.54 diff -u -u -p -r1.54 plugin.cc --- gold/plugin.cc 12 Jun 2012 22:52:41 -0000 1.54 +++ gold/plugin.cc 9 Aug 2012 21:28:03 -0000 @@ -115,6 +115,15 @@ update_section_order(const struct ld_plu static enum ld_plugin_status allow_section_ordering(); +static enum ld_plugin_status +allow_unique_segment_for_sections(); + +static enum ld_plugin_status +unique_segment_for_sections(const char* segment_name, + uint64_t flags, + uint64_t align, + const struct ld_plugin_section *section_list, + unsigned int num_sections); }; #endif // ENABLE_PLUGINS @@ -159,7 +168,7 @@ Plugin::load() sscanf(ver, "%d.%d", &major, &minor); // Allocate and populate a transfer vector. - const int tv_fixed_size = 24; + const int tv_fixed_size = 26; int tv_size = this->args_.size() + tv_fixed_size; ld_plugin_tv* tv = new ld_plugin_tv[tv_size]; @@ -273,6 +282,15 @@ Plugin::load() tv[i].tv_u.tv_allow_section_ordering = allow_section_ordering; ++i; + tv[i].tv_tag = LDPT_ALLOW_UNIQUE_SEGMENT_FOR_SECTIONS; + tv[i].tv_u.tv_allow_unique_segment_for_sections + = allow_unique_segment_for_sections; + + ++i; + tv[i].tv_tag = LDPT_UNIQUE_SEGMENT_FOR_SECTIONS; + tv[i].tv_u.tv_unique_segment_for_sections = unique_segment_for_sections; + + ++i; tv[i].tv_tag = LDPT_NULL; tv[i].tv_u.tv_val = 0; @@ -1685,6 +1703,68 @@ allow_section_ordering() return LDPS_OK; } +// Let the linker know that a subset of sections could be mapped +// to a unique segment. + +static enum ld_plugin_status +allow_unique_segment_for_sections() +{ + gold_assert(parameters->options().has_plugins()); + Layout* layout = parameters->options().plugins()->layout(); + layout->set_unique_segment_for_sections_specified(); + return LDPS_OK; +} + +// This function should map the list of sections specified in the +// SECTION_LIST to a unique segment. ELF segments do not have names +// and the NAME is used to identify Output Section which should contain +// the list of sections. This Output Section will then be mapped to +// a unique segment. FLAGS is used to specify if any additional segment +// flags need to be set. For instance, a specific segment flag can be +// set to identify this segment. Unsetting segment flags is not possible. +// ALIGN specifies the alignment of the segment. + +static enum ld_plugin_status +unique_segment_for_sections(const char* segment_name, + uint64_t flags, + uint64_t align, + const struct ld_plugin_section* section_list, + unsigned int num_sections) +{ + gold_assert(parameters->options().has_plugins()); + + if (num_sections == 0) + return LDPS_OK; + + if (section_list == NULL) + return LDPS_ERR; + + Layout* layout = parameters->options().plugins()->layout(); + gold_assert (layout != NULL); + + Layout::Section_segment_map& section_segment_map + = layout->get_section_segment_map(); + + Layout::Unique_segment_info* s = static_cast + (malloc(sizeof(Layout::Unique_segment_info))); + s->name = segment_name; + s->flags = flags; + s->align = align; + + for (unsigned int i = 0; i < num_sections; ++i) + { + Object* obj = parameters->options().plugins()->get_elf_object( + section_list[i].handle); + if (obj == NULL) + return LDPS_BAD_HANDLE; + unsigned int shndx = section_list[i].shndx; + Const_section_id secn_id(obj, shndx); + section_segment_map[secn_id] = s; + } + + return LDPS_OK; +} + #endif // ENABLE_PLUGINS // Allocate a Pluginobj object of the appropriate size and endianness. Index: gold/testsuite/Makefile.am =================================================================== RCS file: /cvs/src/src/gold/testsuite/Makefile.am,v retrieving revision 1.194 diff -u -u -p -r1.194 Makefile.am --- gold/testsuite/Makefile.am 19 Jul 2012 00:19:34 -0000 1.194 +++ gold/testsuite/Makefile.am 9 Aug 2012 21:28:03 -0000 @@ -1519,13 +1519,15 @@ unused.c: @cp /dev/null $@ check_SCRIPTS += plugin_final_layout.sh -check_DATA += plugin_final_layout.stdout +check_DATA += plugin_final_layout.stdout plugin_final_layout.readelf.stdout plugin_final_layout.o: plugin_final_layout.cc $(CXXCOMPILE) -O0 -c -ffunction-sections -fdata-sections -g -o $@ $< plugin_final_layout: plugin_final_layout.o plugin_section_order.so gcctestdir/ld $(CXXLINK) -Bgcctestdir/ -Wl,--plugin,"./plugin_section_order.so" plugin_final_layout.o plugin_final_layout.stdout: plugin_final_layout $(TEST_NM) -n plugin_final_layout > plugin_final_layout.stdout +plugin_final_layout.readelf.stdout: plugin_final_layout + $(TEST_READELF) -l plugin_final_layout > plugin_final_layout.readelf.stdout plugin_section_order.so: plugin_section_order.o $(LINK) -Bgcctestdir/ -shared plugin_section_order.o Index: gold/testsuite/Makefile.in =================================================================== RCS file: /cvs/src/src/gold/testsuite/Makefile.in,v retrieving revision 1.204 diff -u -u -p -r1.204 Makefile.in --- gold/testsuite/Makefile.in 19 Jul 2012 00:19:35 -0000 1.204 +++ gold/testsuite/Makefile.in 9 Aug 2012 21:28:04 -0000 @@ -329,7 +329,8 @@ check_PROGRAMS = $(am__EXEEXT_1) $(am__E @GCC_TRUE@@NATIVE_LINKER_TRUE@@PLUGINS_TRUE@ plugin_test_7.err \ @GCC_TRUE@@NATIVE_LINKER_TRUE@@PLUGINS_TRUE@ plugin_test_7.syms \ @GCC_TRUE@@NATIVE_LINKER_TRUE@@PLUGINS_TRUE@ plugin_test_9.err \ -@GCC_TRUE@@NATIVE_LINKER_TRUE@@PLUGINS_TRUE@ plugin_final_layout.stdout +@GCC_TRUE@@NATIVE_LINKER_TRUE@@PLUGINS_TRUE@ plugin_final_layout.stdout \ +@GCC_TRUE@@NATIVE_LINKER_TRUE@@PLUGINS_TRUE@ plugin_final_layout.readelf.stdout # Make a copy of two_file_test_1.o, which does not define the symbol _Z4t16av. @GCC_TRUE@@NATIVE_LINKER_TRUE@@PLUGINS_TRUE@am__append_36 = \ @GCC_TRUE@@NATIVE_LINKER_TRUE@@PLUGINS_TRUE@ plugin_test_1.err \ @@ -4867,6 +4868,8 @@ uninstall-am: @GCC_TRUE@@NATIVE_LINKER_TRUE@@PLUGINS_TRUE@ $(CXXLINK) -Bgcctestdir/ -Wl,--plugin,"./plugin_section_order.so" plugin_final_layout.o @GCC_TRUE@@NATIVE_LINKER_TRUE@@PLUGINS_TRUE@plugin_final_layout.stdout: plugin_final_layout @GCC_TRUE@@NATIVE_LINKER_TRUE@@PLUGINS_TRUE@ $(TEST_NM) -n plugin_final_layout > plugin_final_layout.stdout +@GCC_TRUE@@NATIVE_LINKER_TRUE@@PLUGINS_TRUE@plugin_final_layout.readelf.stdout: plugin_final_layout +@GCC_TRUE@@NATIVE_LINKER_TRUE@@PLUGINS_TRUE@ $(TEST_READELF) -l plugin_final_layout > plugin_final_layout.readelf.stdout @GCC_TRUE@@NATIVE_LINKER_TRUE@@PLUGINS_TRUE@plugin_section_order.so: plugin_section_order.o @GCC_TRUE@@NATIVE_LINKER_TRUE@@PLUGINS_TRUE@ $(LINK) -Bgcctestdir/ -shared plugin_section_order.o Index: gold/testsuite/plugin_final_layout.sh =================================================================== RCS file: /cvs/src/src/gold/testsuite/plugin_final_layout.sh,v retrieving revision 1.1 diff -u -u -p -r1.1 plugin_final_layout.sh --- gold/testsuite/plugin_final_layout.sh 29 Sep 2011 23:45:57 -0000 1.1 +++ gold/testsuite/plugin_final_layout.sh 9 Aug 2012 21:28:04 -0000 @@ -56,5 +56,35 @@ END { }" $1 } +# With readelf -l, an ELF Section to Segment mapping is printed as : +############################################## +# Section to Segment mapping: +# Segment Sections... +# ... +# 0x .text.plugin_created_unique +# ... +############################################## +# Check of .text.plugin_created_unique is the only section in the segment. +check_unique_segment() +{ + awk " +BEGIN { saw_section = 0; saw_unique = 0; } +/$2/ { saw_section = 1; } +/[ ]*0[0-9][ ]*$2[ ]*\$/ { saw_unique = 1; } +END { + if (!saw_section) + { + printf \"Section $2 not seen in output\\n\"; + exit 1; + } + else if (!saw_unique) + { + printf \"Unique segment not seen for: $2\\n\"; + exit 1; + } + }" $1 +} + check plugin_final_layout.stdout "_Z3foov" "_Z3barv" check plugin_final_layout.stdout "_Z3barv" "_Z3bazv" +check_unique_segment plugin_final_layout.readelf.stdout ".text.plugin_created_unique" Index: gold/testsuite/plugin_section_order.c =================================================================== RCS file: /cvs/src/src/gold/testsuite/plugin_section_order.c,v retrieving revision 1.1 diff -u -u -p -r1.1 plugin_section_order.c --- gold/testsuite/plugin_section_order.c 29 Sep 2011 23:45:57 -0000 1.1 +++ gold/testsuite/plugin_section_order.c 9 Aug 2012 21:28:04 -0000 @@ -36,6 +36,9 @@ static ld_plugin_get_input_section_name static ld_plugin_get_input_section_contents get_input_section_contents = NULL; static ld_plugin_update_section_order update_section_order = NULL; static ld_plugin_allow_section_ordering allow_section_ordering = NULL; +static ld_plugin_allow_unique_segment_for_sections + allow_unique_segment_for_sections = NULL; +static ld_plugin_unique_segment_for_sections unique_segment_for_sections = NULL; enum ld_plugin_status onload(struct ld_plugin_tv *tv); enum ld_plugin_status claim_file_hook(const struct ld_plugin_input_file *file, @@ -52,11 +55,13 @@ onload(struct ld_plugin_tv *tv) switch (entry->tv_tag) { case LDPT_REGISTER_CLAIM_FILE_HOOK: - assert((*entry->tv_u.tv_register_claim_file) (claim_file_hook) == LDPS_OK); + assert((*entry->tv_u.tv_register_claim_file) (claim_file_hook) + == LDPS_OK); break; case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: - assert((*entry->tv_u.tv_register_all_symbols_read) (all_symbols_read_hook) - == LDPS_OK); + assert((*entry->tv_u.tv_register_all_symbols_read) + (all_symbols_read_hook) + == LDPS_OK); break; case LDPT_GET_INPUT_SECTION_COUNT: get_input_section_count = *entry->tv_u.tv_get_input_section_count; @@ -68,7 +73,8 @@ onload(struct ld_plugin_tv *tv) get_input_section_name = *entry->tv_u.tv_get_input_section_name; break; case LDPT_GET_INPUT_SECTION_CONTENTS: - get_input_section_contents = *entry->tv_u.tv_get_input_section_contents; + get_input_section_contents + = *entry->tv_u.tv_get_input_section_contents; break; case LDPT_UPDATE_SECTION_ORDER: update_section_order = *entry->tv_u.tv_update_section_order; @@ -76,6 +82,13 @@ onload(struct ld_plugin_tv *tv) case LDPT_ALLOW_SECTION_ORDERING: allow_section_ordering = *entry->tv_u.tv_allow_section_ordering; break; + case LDPT_ALLOW_UNIQUE_SEGMENT_FOR_SECTIONS: + allow_unique_segment_for_sections + = *entry->tv_u.tv_allow_unique_segment_for_sections; + case LDPT_UNIQUE_SEGMENT_FOR_SECTIONS: + unique_segment_for_sections + = *entry->tv_u.tv_unique_segment_for_sections; + break; default: break; } @@ -86,7 +99,9 @@ onload(struct ld_plugin_tv *tv) || get_input_section_name == NULL || get_input_section_contents == NULL || update_section_order == NULL - || allow_section_ordering == NULL) + || allow_section_ordering == NULL + || allow_unique_segment_for_sections == NULL + || unique_segment_for_sections == NULL) { fprintf(stderr, "Some interfaces are missing\n"); return LDPS_ERR; @@ -117,6 +132,9 @@ claim_file_hook(const struct ld_plugin_i { /* Inform the linker to prepare for section reordering. */ (*allow_section_ordering)(); + /* Inform the linker to prepare to map some sections to unique + segments. */ + (*allow_unique_segment_for_sections)(); is_ordering_specified = 1; } @@ -160,8 +178,11 @@ enum ld_plugin_status all_symbols_read_hook(void) { if (num_entries == 3) - update_section_order(section_list, num_entries); + { + update_section_order(section_list, num_entries); + unique_segment_for_sections (".text.plugin_created_unique", 0, 0x1000, + section_list, num_entries); + } return LDPS_OK; } - Index: include/plugin-api.h =================================================================== RCS file: /cvs/src/src/include/plugin-api.h,v retrieving revision 1.19 diff -u -u -p -r1.19 plugin-api.h --- include/plugin-api.h 12 Jun 2012 22:50:44 -0000 1.19 +++ include/plugin-api.h 9 Aug 2012 21:28:04 -0000 @@ -318,6 +318,31 @@ typedef enum ld_plugin_status (*ld_plugin_allow_section_ordering) (void); +/* The linker's interface for specifying that a subset of sections is + to be mapped to a unique segment. This should be invoked before + unique_segment_for_sections, preferably in the claim_file handler. */ + +typedef +enum ld_plugin_status +(*ld_plugin_allow_unique_segment_for_sections) (void); + +/* The linker's interface for specifying that a specific set of sections + must be mapped to a unique segment. ELF segments do not have names + and the NAME is used as an identifier only. FLAGS is used to specify + if any additional segment flags need to be set. For instance, a + specific segment flag can be set to identify this segment. Unsetting + segment flags that would be set by default is not possible. The + parameter SEGMENT_ALIGNMENT when non-zero will override the default. */ + +typedef +enum ld_plugin_status +(*ld_plugin_unique_segment_for_sections) ( + const char* segment_name, + uint64_t segment_flags, + uint64_t segment_alignment, + const struct ld_plugin_section * section_list, + unsigned int num_sections); + enum ld_plugin_level { LDPL_INFO, @@ -355,7 +380,9 @@ enum ld_plugin_tag LDPT_GET_INPUT_SECTION_CONTENTS, LDPT_UPDATE_SECTION_ORDER, LDPT_ALLOW_SECTION_ORDERING, - LDPT_GET_SYMBOLS_V2 + LDPT_GET_SYMBOLS_V2, + LDPT_ALLOW_UNIQUE_SEGMENT_FOR_SECTIONS, + LDPT_UNIQUE_SEGMENT_FOR_SECTIONS }; /* The plugin transfer vector. */ @@ -385,6 +412,8 @@ struct ld_plugin_tv ld_plugin_get_input_section_contents tv_get_input_section_contents; ld_plugin_update_section_order tv_update_section_order; ld_plugin_allow_section_ordering tv_allow_section_ordering; + ld_plugin_allow_unique_segment_for_sections tv_allow_unique_segment_for_sections; + ld_plugin_unique_segment_for_sections tv_unique_segment_for_sections; } tv_u; };