Memory leaks and ineffective bounds checking in wasm_scan
[deliverable/binutils-gdb.git] / libiberty / simple-object.c
index 5b95fb2d56daf6c5cabcf1bdfde4228c273d069c..b00c265128c9dbca6984cea556af010df99f5427 100644 (file)
@@ -1,5 +1,5 @@
 /* simple-object.c -- simple routines to read and write object files.
-   Copyright (C) 2010-2018 Free Software Foundation, Inc.
+   Copyright (C) 2010-2019 Free Software Foundation, Inc.
    Written by Ian Lance Taylor, Google.
 
 This program is free software; you can redistribute it and/or modify it
@@ -44,6 +44,10 @@ Boston, MA 02110-1301, USA.  */
 #define SEEK_SET 0
 #endif
 
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
 #include "simple-object-common.h"
 
 /* The known object file formats.  */
@@ -251,39 +255,74 @@ simple_object_find_section (simple_object_read *sobj, const char *name,
 }
 
 /* Callback to identify and rename LTO debug sections by name.
-   Returns 1 if NAME is a LTO debug section, 0 if not.  */
+   Returns non-NULL if NAME is a LTO debug section, NULL if not.
+   If RENAME is true it will rename LTO debug sections to non-LTO
+   ones.  */
 
-static int
-handle_lto_debug_sections (const char **name)
+static char *
+handle_lto_debug_sections (const char *name, int rename)
 {
+  char *newname = rename ? XCNEWVEC (char, strlen (name) + 1)
+                        : xstrdup (name);
+
   /* ???  So we can't use .gnu.lto_ prefixed sections as the assembler
      complains about bogus section flags.  Which means we need to arrange
      for that to be fixed or .gnu.debuglto_ marked as SHF_EXCLUDE (to make
      fat lto object tooling work for the fat part).  */
-  /* ???  For now this handles both .gnu.lto_ and .gnu.debuglto_ prefixed
-     sections.  */
-  /* Copy LTO debug sections and rename them to their non-LTO name.  */
-  if (strncmp (*name, ".gnu.debuglto_", sizeof (".gnu.debuglto_") - 1) == 0)
+  /* Also include corresponding reloc sections.  */
+  if (strncmp (name, ".rela", sizeof (".rela") - 1) == 0)
     {
-      *name = *name + sizeof (".gnu.debuglto_") - 1;
-      return 1;
+      if (rename)
+        strncpy (newname, name, sizeof (".rela") - 1);
+      name += sizeof (".rela") - 1;
     }
-  else if (strncmp (*name, ".gnu.lto_.debug_", sizeof (".gnu.lto_.debug_") -1) == 0)
+  else if (strncmp (name, ".rel", sizeof (".rel") - 1) == 0)
     {
-      *name = *name + sizeof (".gnu.lto_") - 1;
-      return 1;
+      if (rename)
+        strncpy (newname, name, sizeof (".rel") - 1);
+      name += sizeof (".rel") - 1;
     }
+  /* ???  For now this handles both .gnu.lto_ and .gnu.debuglto_ prefixed
+     sections.  */
+  /* Copy LTO debug sections and rename them to their non-LTO name.  */
+  if (strncmp (name, ".gnu.debuglto_", sizeof (".gnu.debuglto_") - 1) == 0)
+    return rename ? strcat (newname, name + sizeof (".gnu.debuglto_") - 1) : newname;
+  else if (strncmp (name, ".gnu.lto_.debug_",
+                   sizeof (".gnu.lto_.debug_") -1) == 0)
+    return rename ? strcat (newname, name + sizeof (".gnu.lto_") - 1) : newname;
   /* Copy over .note.GNU-stack section under the same name if present.  */
-  else if (strcmp (*name, ".note.GNU-stack") == 0)
-    return 1;
-  return 0;
+  else if (strcmp (name, ".note.GNU-stack") == 0)
+    return strcpy (newname, name);
+  /* Copy over .comment section under the same name if present.  Solaris
+     ld uses them to relax its checking of ELF gABI access rules for
+     COMDAT sections in objects produced by GCC.  */
+  else if (strcmp (name, ".comment") == 0)
+    return strcpy (newname, name);
+  free (newname);
+  return NULL;
+}
+
+/* Wrapper for handle_lto_debug_sections.  */
+
+static char *
+handle_lto_debug_sections_rename (const char *name)
+{
+  return handle_lto_debug_sections (name, 1);
+}
+
+/* Wrapper for handle_lto_debug_sections.  */
+
+static char *
+handle_lto_debug_sections_norename (const char *name)
+{
+  return handle_lto_debug_sections (name, 0);
 }
 
 /* Copy LTO debug sections.  */
 
 const char *
 simple_object_copy_lto_debug_sections (simple_object_read *sobj,
-                                      const char *dest, int *err)
+                                      const char *dest, int *err, int rename)
 {
   const char *errmsg;
   simple_object_write *dest_sobj;
@@ -304,16 +343,17 @@ simple_object_copy_lto_debug_sections (simple_object_read *sobj,
   if (! dest_sobj)
     return errmsg;
 
-  errmsg = sobj->functions->copy_lto_debug_sections (sobj, dest_sobj,
-                                                    handle_lto_debug_sections,
-                                                    err);
+  errmsg = sobj->functions->copy_lto_debug_sections
+                (sobj, dest_sobj,
+                 rename ? handle_lto_debug_sections_rename
+                        : handle_lto_debug_sections_norename,  err);
   if (errmsg)
     {
       simple_object_release_write (dest_sobj);
       return errmsg;
     }
 
-  outfd = creat (dest, 00777);
+  outfd = open (dest, O_CREAT|O_WRONLY|O_TRUNC|O_BINARY, 00777);
   if (outfd == -1)
     {
       *err = errno;
This page took 0.026159 seconds and 4 git commands to generate.