diff libiberty/simple-object.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents 561a7518be6b
children 84e7813d76e9
line wrap: on
line diff
--- a/libiberty/simple-object.c	Sun Aug 21 07:07:55 2011 +0900
+++ b/libiberty/simple-object.c	Fri Oct 27 22:46:09 2017 +0900
@@ -1,5 +1,5 @@
 /* simple-object.c -- simple routines to read and write object files.
-   Copyright 2010 Free Software Foundation, Inc.
+   Copyright (C) 2010-2017 Free Software Foundation, Inc.
    Written by Ian Lance Taylor, Google.
 
 This program is free software; you can redistribute it and/or modify it
@@ -22,6 +22,7 @@
 #include "simple-object.h"
 
 #include <errno.h>
+#include <fcntl.h>
 
 #ifdef HAVE_STDLIB_H
 #include <stdlib.h>
@@ -51,7 +52,8 @@
 {
   &simple_object_elf_functions,
   &simple_object_mach_o_functions,
-  &simple_object_coff_functions
+  &simple_object_coff_functions,
+  &simple_object_xcoff_functions
 };
 
 /* Read data from a file using the simple_object error reporting
@@ -62,8 +64,6 @@
 			     unsigned char *buffer, size_t size,
 			     const char **errmsg, int *err)
 {
-  ssize_t got;
-
   if (lseek (descriptor, offset, SEEK_SET) < 0)
     {
       *errmsg = "lseek";
@@ -71,15 +71,26 @@
       return 0;
     }
 
-  got = read (descriptor, buffer, size);
-  if (got < 0)
+  do
     {
-      *errmsg = "read";
-      *err = errno;
-      return 0;
+      ssize_t got = read (descriptor, buffer, size);
+      if (got == 0)
+	break;
+      else if (got > 0)
+	{
+	  buffer += got;
+	  size -= got;
+	}
+      else if (errno != EINTR)
+	{
+	  *errmsg = "read";
+	  *err = errno;
+	  return 0;
+	}
     }
+  while (size > 0);
 
-  if ((size_t) got < size)
+  if (size > 0)
     {
       *errmsg = "file too short";
       *err = 0;
@@ -97,8 +108,6 @@
 			      const unsigned char *buffer, size_t size,
 			      const char **errmsg, int *err)
 {
-  ssize_t wrote;
-
   if (lseek (descriptor, offset, SEEK_SET) < 0)
     {
       *errmsg = "lseek";
@@ -106,15 +115,26 @@
       return 0;
     }
 
-  wrote = write (descriptor, buffer, size);
-  if (wrote < 0)
+  do
     {
-      *errmsg = "write";
-      *err = errno;
-      return 0;
+      ssize_t wrote = write (descriptor, buffer, size);
+      if (wrote == 0)
+	break;
+      else if (wrote > 0)
+	{
+	  buffer += wrote;
+	  size -= wrote;
+	}
+      else if (errno != EINTR)
+	{
+	  *errmsg = "write";
+	  *err = errno;
+	  return 0;
+	}
     }
+  while (size > 0);
 
-  if ((size_t) wrote < size)
+  if (size > 0)
     {
       *errmsg = "short write";
       *err = 0;
@@ -230,6 +250,89 @@
   return 1;
 }
 
+/* Callback to identify and rename LTO debug sections by name.
+   Returns 1 if NAME is a LTO debug section, 0 if not.  */
+
+static int
+handle_lto_debug_sections (const char **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)
+    {
+      *name = *name + sizeof (".gnu.debuglto_") - 1;
+      return 1;
+    }
+  else if (strncmp (*name, ".gnu.lto_.debug_", sizeof (".gnu.lto_.debug_") -1) == 0)
+    {
+      *name = *name + sizeof (".gnu.lto_") - 1;
+      return 1;
+    }
+  /* Copy over .note.GNU-stack section under the same name if present.  */
+  else if (strcmp (*name, ".note.GNU-stack") == 0)
+    return 1;
+  return 0;
+}
+
+/* Copy LTO debug sections.  */
+
+const char *
+simple_object_copy_lto_debug_sections (simple_object_read *sobj,
+				       const char *dest, int *err)
+{
+  const char *errmsg;
+  simple_object_write *dest_sobj;
+  simple_object_attributes *attrs;
+  int outfd;
+
+  if (! sobj->functions->copy_lto_debug_sections)
+    {
+      *err = EINVAL;
+      return "simple_object_copy_lto_debug_sections not implemented";
+    }
+
+  attrs = simple_object_fetch_attributes (sobj, &errmsg, err);
+  if (! attrs)
+    return errmsg;
+  dest_sobj = simple_object_start_write (attrs, NULL, &errmsg, err);
+  simple_object_release_attributes (attrs);
+  if (! dest_sobj)
+    return errmsg;
+
+  errmsg = sobj->functions->copy_lto_debug_sections (sobj, dest_sobj,
+						     handle_lto_debug_sections,
+						     err);
+  if (errmsg)
+    {
+      simple_object_release_write (dest_sobj);
+      return errmsg;
+    }
+
+  outfd = creat (dest, 00777);
+  if (outfd == -1)
+    {
+      *err = errno;
+      simple_object_release_write (dest_sobj);
+      return "open failed";
+    }
+
+  errmsg = simple_object_write_to_file (dest_sobj, outfd, err);
+  close (outfd);
+  if (errmsg)
+    {
+      simple_object_release_write (dest_sobj);
+      return errmsg;
+    }
+
+  simple_object_release_write (dest_sobj);
+  return NULL;
+}
+
 /* Fetch attributes.  */
 
 simple_object_attributes *
@@ -296,7 +399,7 @@
     return NULL;
   ret = XNEW (simple_object_write);
   ret->functions = attrs->functions;
-  ret->segment_name = xstrdup (segment_name);
+  ret->segment_name = segment_name ? xstrdup (segment_name) : NULL;
   ret->sections = NULL;
   ret->last_section = NULL;
   ret->data = data;