[21/21] Fix gold linker build issues for mingw

Message ID 20250402121759.1962001-22-jovan.dmitrovic@htecgroup.com
State New
Headers
Series Integrate MIPS-Specific Support |

Commit Message

Jovan Dmitrovic April 2, 2025, 12:18 p.m. UTC
  From: Faraz Shahbazker <fshahbazker@wavecomp.com>

Release branch only.

These functions are not defined as of mingw-w64 v6.0.0, so we
work around them as follows:
mkdtemp: simulate using tempnam and mkdir
link: omit usage and fall back to file copy

NOTE: use of tempnam is a security risk. Find a better solution!!

Cherry-picked cf1e966
from https://github.com/MIPS/binutils-gdb

Signed-off-by: Faraz Shahbazker <fshahbazker@wavecomp.com>
Signed-off-by: Milica Matic <milica.matic@htecgroup.com>
---
 gold/plugin.cc | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
  

Comments

Andreas Schwab April 2, 2025, 1:19 p.m. UTC | #1
On Apr 02 2025, Jovan Dmitrovic wrote:

> diff --git a/gold/plugin.cc b/gold/plugin.cc
> index 89f2e3bb182..545c0d96e16 100644
> --- a/gold/plugin.cc
> +++ b/gold/plugin.cc
> @@ -509,8 +509,12 @@ Plugin_recorder::init()
>    // copies of replacement files.
>    char dir_template[] = "gold-recording-XXXXXX";
>  #ifdef HAVE_MKDTEMP
> -  if (mkdtemp(dir_template) == NULL)
> -    return false;
> +  #ifdef __MINGW32__
> +    if (mkdir (tmpnam(dir_template)) == 0)
> +  #else
> +    if (mkdtemp(dir_template) == NULL)
> +  #endif
> +  return false;

Why is HAVE_MKDTEMP defined if it's not available?

> @@ -574,7 +578,7 @@ link_or_copy_file(const char* inname, const char* outname)
>  {
>    static char buf[4096];
>  
> -#ifdef HAVE_LINK
> +#if defined(HAVE_LINK) && !defined(__MINGW32__)

Why is HAVE_LINK defined if it's not available?
  

Patch

diff --git a/gold/plugin.cc b/gold/plugin.cc
index 89f2e3bb182..545c0d96e16 100644
--- a/gold/plugin.cc
+++ b/gold/plugin.cc
@@ -509,8 +509,12 @@  Plugin_recorder::init()
   // copies of replacement files.
   char dir_template[] = "gold-recording-XXXXXX";
 #ifdef HAVE_MKDTEMP
-  if (mkdtemp(dir_template) == NULL)
-    return false;
+  #ifdef __MINGW32__
+    if (mkdir (tmpnam(dir_template)) == 0)
+  #else
+    if (mkdtemp(dir_template) == NULL)
+  #endif
+  return false;
 #else
   if (mktemp(dir_template) == NULL)
     return false;
@@ -574,7 +578,7 @@  link_or_copy_file(const char* inname, const char* outname)
 {
   static char buf[4096];
 
-#ifdef HAVE_LINK
+#if defined(HAVE_LINK) && !defined(__MINGW32__)
   if (::link(inname, outname) == 0)
     return true;
 #endif