Add --orphan-handling option.
authorCary Coutant <ccoutant@gmail.com>
Tue, 13 Dec 2016 21:01:13 +0000 (13:01 -0800)
committerCary Coutant <ccoutant@gmail.com>
Tue, 13 Dec 2016 21:01:26 +0000 (13:01 -0800)
gold/
PR gold/20749
* options.h (--orphan-handling): New option.
(General_options::Orphan_handling): New enum.
(General_options::orphan_handling_enum): New method.
(General_options::set_orphan_handling_enum): New method.
(General_options::orphan_handling_enum_): New data member.
* options.cc (General_options::General_options): Initialize new member.
(General_options::finalize): Convert --orphan-handling argument to enum.
* script-sections.cc (Script_sections::output_section_name): Check it.

gold/ChangeLog
gold/options.cc
gold/options.h
gold/script-sections.cc

index ac5fce8cd5fe57dc018e3719aba903c4af3492bc..b40f7b8e20a5ffb7e9236930f73747a2ffbdeb59 100644 (file)
@@ -1,3 +1,15 @@
+2016-12-13  Cary Coutant  <ccoutant@gmail.com>
+
+       PR gold/20749
+       * options.h (--orphan-handling): New option.
+       (General_options::Orphan_handling): New enum.
+       (General_options::orphan_handling_enum): New method.
+       (General_options::set_orphan_handling_enum): New method.
+       (General_options::orphan_handling_enum_): New data member.
+       * options.cc (General_options::General_options): Initialize new member.
+       (General_options::finalize): Convert --orphan-handling argument to enum.
+       * script-sections.cc (Script_sections::output_section_name): Check it.
+
 2016-12-13  Cary Coutant  <ccoutant@gmail.com>
 
        PR gold/20522
index 276be39e47016bff3637859dab9ecc3f40ec3668..1650910326cf8b5227887bfc8e0b365f42504fad 100644 (file)
@@ -998,7 +998,8 @@ General_options::General_options()
     section_starts_(),
     fix_v4bx_(FIX_V4BX_NONE),
     endianness_(ENDIANNESS_NOT_SET),
-    discard_locals_(DISCARD_SEC_MERGE)
+    discard_locals_(DISCARD_SEC_MERGE),
+    orphan_handling_enum_(ORPHAN_PLACE)
 {
   // Turn off option registration once construction is complete.
   gold::options::ready_to_register = false;
@@ -1157,6 +1158,19 @@ General_options::finalize()
       this->set_do_demangle(getenv("COLLECT_NO_DEMANGLE") == NULL);
     }
 
+  // Parse the --orphan-handling argument.
+  if (this->user_set_orphan_handling())
+    {
+      if (strcmp(this->orphan_handling(), "place") == 0)
+        this->set_orphan_handling_enum(ORPHAN_PLACE);
+      else if (strcmp(this->orphan_handling(), "discard") == 0)
+        this->set_orphan_handling_enum(ORPHAN_DISCARD);
+      else if (strcmp(this->orphan_handling(), "warn") == 0)
+        this->set_orphan_handling_enum(ORPHAN_WARN);
+      else if (strcmp(this->orphan_handling(), "error") == 0)
+        this->set_orphan_handling_enum(ORPHAN_ERROR);
+    }
+
   // -M is equivalent to "-Map -".
   if (this->print_map() && !this->user_set_Map())
     {
index 75b2f091ad0289436b79cc0264035eb9e3a2cda2..ebe08df55ac9a95031d9473b8d15d5e3a54a24ea 100644 (file)
@@ -1072,6 +1072,10 @@ class General_options
   DEFINE_uint(optimize, options::EXACTLY_ONE_DASH, 'O', 0,
              N_("Optimize output file size"), N_("LEVEL"));
 
+  DEFINE_enum(orphan_handling, options::TWO_DASHES, '\0', "place",
+             N_("Orphan section handling"), N_("[place,discard,warn,error]"),
+             {"place", "discard", "warn", "error"});
+
   // p
 
   DEFINE_bool(p, options::ONE_DASH, 'p', false,
@@ -1684,6 +1688,22 @@ class General_options
   discard_sec_merge() const
   { return this->discard_locals_ == DISCARD_SEC_MERGE; }
 
+  enum Orphan_handling
+  {
+    // Place orphan sections normally (default).
+    ORPHAN_PLACE,
+    // Discard all orphan sections.
+    ORPHAN_DISCARD,
+    // Warn when placing orphan sections.
+    ORPHAN_WARN,
+    // Issue error for orphan sections.
+    ORPHAN_ERROR
+  };
+
+  Orphan_handling
+  orphan_handling_enum() const
+  { return this->orphan_handling_enum_; }
+
  private:
   // Don't copy this structure.
   General_options(const General_options&);
@@ -1739,6 +1759,10 @@ class General_options
   set_static(bool value)
   { static_ = value; }
 
+  void
+  set_orphan_handling_enum(Orphan_handling value)
+  { this->orphan_handling_enum_ = value; }
+
   // These are called by finalize() to set up the search-path correctly.
   void
   add_to_library_path_with_sysroot(const std::string& arg)
@@ -1804,6 +1828,8 @@ class General_options
   Discard_locals discard_locals_;
   // Stack of saved options for --push-state/--pop-state.
   std::vector<Position_dependent_options*> options_stack_;
+  // Orphan handling option, decoded to an enum value.
+  Orphan_handling orphan_handling_enum_;
 };
 
 // The position-dependent options.  We use this to store the state of
index 90ec8d4a504d873e10b9165ea3f5c104d57981f9..ffd4666999ba76e0f1799d05d2d2f463ea318840 100644 (file)
@@ -3592,13 +3592,37 @@ Script_sections::output_section_name(
        }
     }
 
-  // If we couldn't find a mapping for the name, the output section
-  // gets the name of the input section.
-
+  // We have an orphan section.
   *output_section_slot = NULL;
   *psection_type = Script_sections::ST_NONE;
   *keep = false;
 
+  General_options::Orphan_handling orphan_handling =
+      parameters->options().orphan_handling_enum();
+  if (orphan_handling == General_options::ORPHAN_DISCARD)
+    return NULL;
+  if (orphan_handling == General_options::ORPHAN_ERROR)
+    {
+      if (file_name == NULL)
+       gold_error(_("unplaced orphan section '%s'"), section_name);
+      else
+       gold_error(_("unplaced orphan section '%s' from '%s'"),
+                  section_name, file_name);
+      return NULL;
+    }
+  if (orphan_handling == General_options::ORPHAN_WARN)
+    {
+      if (file_name == NULL)
+       gold_warning(_("orphan section '%s' is being placed in section '%s'"),
+                    section_name, section_name);
+      else
+       gold_warning(_("orphan section '%s' from '%s' is being placed "
+                      "in section '%s'"),
+                    section_name, file_name, section_name);
+    }
+
+  // If we couldn't find a mapping for the name, the output section
+  // gets the name of the input section.
   return section_name;
 }