The function unique_segment_for_section (which is part of the public
plugin API) stores the passed in segment name pointer without creating
a copy of the string. This requires callers to make sure that the
passed-in name has a sufficiently long life time. This is not the
expected behavior and puts more burden on the caller (who needs to be
aware this fact).

The attached patch (2 modified lines) changes the type of
`Unique_segment_info::name` from `const char*` to `std::string` to fix
this issue.


```
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)
```



https://sourceware.org/bugzilla/show_bug.cgi?id=22065
diff --git a/gold/layout.cc b/gold/layout.cc
index 5f25faea55..22f5ffbe53 100644
--- a/gold/layout.cc
+++ b/gold/layout.cc
@@ -1198,7 +1198,7 @@ Layout::layout(Sized_relobj_file<size, big_endian>* object, unsigned int shndx,
 	  elfcpp::Elf_Xword flags
 	    = this->get_output_section_flags(shdr.get_sh_flags());
 
-	  const char* os_name = it->second->name;
+	  const char* os_name = it->second->name.c_str();
 	  Stringpool::Key name_key;
 	  os_name = this->namepool_.add(os_name, true, &name_key);
 	  os = this->get_output_section(os_name, name_key, sh_type, flags,
diff --git a/gold/layout.h b/gold/layout.h
index 15ee924678..a5e331b942 100644
--- a/gold/layout.h
+++ b/gold/layout.h
@@ -541,7 +541,7 @@ class Layout
   {
     // Identifier for the segment.  ELF segments don't have names.  This
     // is used as the name of the output section mapped to the segment.
-    const char* name;
+    std::string name;
     // Additional segment flags.
     uint64_t flags;
     // Segment alignment.
_______________________________________________
bug-binutils mailing list
bug-binutils@gnu.org
https://lists.gnu.org/mailman/listinfo/bug-binutils

Reply via email to