Subject: [PATCH] Improving HP-UX support

From: "Osipov, Michael" <[email-masked]>
Date: Thu, 9 May 2019 12:50:16 +0200

Hi folks,

please find a patch attached to properly compile and link GNU coreutils 
8.31 on HP-UX IA64. As of now, it does not compile out of the box.

The patch includes these changes:

* basenc.c: HP aCC does not allow anonymous unions inside structs, I 
have named it otherwise the compilation fails
* blake2/blake2.h: __attribute__((packed)) does not exist in HP aCC, use 
#pragma pack
* copy.c, mkfifo.c: disable SELinux code if it is not available
* system.h: use __attribute(x) on a GNU-style compiler only
* configure.ac, local.mk: use HP aCC-style linker options for libstdbuf

Compile instructions:
> export PREFIX=/opt/ports/coreutils
> export LIBDIR=$PREFIX/lib/hpux32
> export CONFIGURE="./configure --prefix=$PREFIX --libdir=$LIBDIR"
> export CPPFLAGS="-I$PREFIX/include -D_INCLUDE_STDC__SOURCE_199901"
> export LDFLAGS="-L$LIBDIR"
> autoreconf -fi
> FORCE_UNSAFE_CONFIGURE=1 gl_cv_have_include_next=no ac_cv_func_getacl=no ac_cv_func_aclsort=no ac_cv_header_sys_bitypes_h=no $CONFIGURE
> gmake install

The patch isn't perfect, some issues need to be discussion because I 
don't know yet how to solve them:
* copy.c, mkfifo.c: for some reason the code compiles w/o changes 
although SELinux is not available
* I have disabled the compilation of b2sum completely because gmake 
repeatedly quits with:
> gmake[2]: *** No rule to make target '/var/tmp/AAA000546.i', needed by 'src/b2sum-md5sum.o'.  Stop.
> gmake[2]: Leaving directory '/tmp/system-compile/gnu/coreutils-8.31'
> gmake[1]: *** [Makefile:12647: all-recursive] Error 1
> gmake[1]: Leaving directory '/tmp/system-compile/gnu/coreutils-8.31'
> gmake: *** [Makefile:6827: all] Error 2
* The configuration of PIC and shared library is for HP-UX now. At best 
autotools would determine that with some default m4 file.

Note: I am not subscribed to this list.

Regards,

Michael

diff -ur src/basenc.c src/basenc.c
--- src/basenc.c	2019-02-11 02:51:47 +0000
+++ src/basenc.c	2019-05-08 15:08:50 +0000
@@ -247,7 +247,7 @@
     struct base16_decode_context b16ctx;
     struct base2_decode_context  b2ctx;
     struct z85_decode_context    z85ctx;
-  };
+  } bdcontext;
   char *inbuf;
   size_t bufsize;
 };
@@ -271,7 +271,7 @@
 static void
 base64_decode_ctx_init_wrapper (struct base_decode_context *ctx)
 {
-  base64_decode_ctx_init (&ctx->b64ctx);
+  base64_decode_ctx_init (&ctx->bdcontext.b64ctx);
 }
 
 static bool
@@ -279,8 +279,8 @@
                            const char *restrict in, size_t inlen,
                            char *restrict out, size_t *outlen)
 {
-  bool b = base64_decode_ctx (&ctx->b64ctx, in, inlen, out, outlen);
-  ctx->i = ctx->b64ctx.i;
+  bool b = base64_decode_ctx (&ctx->bdcontext.b64ctx, in, inlen, out, outlen);
+  ctx->i = ctx->bdcontext.b64ctx.i;
   return b;
 }
 
@@ -329,7 +329,7 @@
 static void
 base64url_decode_ctx_init_wrapper (struct base_decode_context *ctx)
 {
-  base64_decode_ctx_init (&ctx->b64ctx);
+  base64_decode_ctx_init (&ctx->bdcontext.b64ctx);
   init_inbuf (ctx);
 }
 
@@ -359,8 +359,8 @@
       ++p;
     }
 
-  bool b = base64_decode_ctx (&ctx->b64ctx, ctx->inbuf, inlen, out, outlen);
-  ctx->i = ctx->b64ctx.i;
+  bool b = base64_decode_ctx (&ctx->bdcontext.b64ctx, ctx->inbuf, inlen, out, outlen);
+  ctx->i = ctx->bdcontext.b64ctx.i;
 
   return b;
 }
@@ -376,7 +376,7 @@
 static void
 base32_decode_ctx_init_wrapper (struct base_decode_context *ctx)
 {
-  base32_decode_ctx_init (&ctx->b32ctx);
+  base32_decode_ctx_init (&ctx->bdcontext.b32ctx);
 }
 
 static bool
@@ -384,8 +384,8 @@
                            const char *restrict in, size_t inlen,
                            char *restrict out, size_t *outlen)
 {
-  bool b = base32_decode_ctx (&ctx->b32ctx, in, inlen, out, outlen);
-  ctx->i = ctx->b32ctx.i;
+  bool b = base32_decode_ctx (&ctx->bdcontext.b32ctx, in, inlen, out, outlen);
+  ctx->i = ctx->bdcontext.b32ctx.i;
   return b;
 }
 
@@ -457,7 +457,7 @@
 static void
 base32hex_decode_ctx_init_wrapper (struct base_decode_context *ctx)
 {
-  base32_decode_ctx_init (&ctx->b32ctx);
+  base32_decode_ctx_init (&ctx->bdcontext.b32ctx);
   init_inbuf (ctx);
 }
 
@@ -481,8 +481,8 @@
       ++in;
     }
 
-  bool b = base32_decode_ctx (&ctx->b32ctx, ctx->inbuf, inlen, out, outlen);
-  ctx->i = ctx->b32ctx.i;
+  bool b = base32_decode_ctx (&ctx->bdcontext.b32ctx, ctx->inbuf, inlen, out, outlen);
+  ctx->i = ctx->bdcontext.b32ctx.i;
 
   return b;
 }
@@ -519,7 +519,7 @@
 base16_decode_ctx_init (struct base_decode_context *ctx)
 {
   init_inbuf (ctx);
-  ctx->b16ctx.have_nibble = false;
+  ctx->bdcontext.b16ctx.have_nibble = false;
   ctx->i = 1;
 }
 
@@ -538,7 +538,7 @@
      if there is a dangling high nibble - we are missing the low nibble,
      so return false - indicating an invalid input.  */
   if (inlen == 0)
-    return !ctx->b16ctx.have_nibble;
+    return !ctx->bdcontext.b16ctx.have_nibble;
 
   while (inlen--)
     {
@@ -557,18 +557,18 @@
 
       ++in;
 
-      if (ctx->b16ctx.have_nibble)
+      if (ctx->bdcontext.b16ctx.have_nibble)
         {
           /* have both nibbles, write octet */
-          *out++ = (ctx->b16ctx.nibble<<4) + nib;
+          *out++ = (ctx->bdcontext.b16ctx.nibble<<4) + nib;
           ++(*outlen);
         }
       else
         {
           /* Store higher nibble until next one arrives */
-          ctx->b16ctx.nibble = nib;
+          ctx->bdcontext.b16ctx.nibble = nib;
         }
-      ctx->b16ctx.have_nibble = !ctx->b16ctx.have_nibble;
+      ctx->bdcontext.b16ctx.have_nibble = !ctx->bdcontext.b16ctx.have_nibble;
     }
   return true;
 }
@@ -657,20 +657,20 @@
 z85_decode_ctx_init (struct base_decode_context *ctx)
 {
   init_inbuf (ctx);
-  ctx->z85ctx.i = 0;
+  ctx->bdcontext.z85ctx.i = 0;
   ctx->i = 1;
 }

 # define Z85_LO_CTX_TO_32BIT_VAL(ctx) \
-  (((ctx)->z85ctx.octets[1] * 85 * 85 * 85) +   \
-   ((ctx)->z85ctx.octets[2] * 85 * 85) +	    \
-   ((ctx)->z85ctx.octets[3] * 85) +		    \
-   ((ctx)->z85ctx.octets[4]))
+  (((ctx)->bdcontext.z85ctx.octets[1] * 85 * 85 * 85) +   \
+   ((ctx)->bdcontext.z85ctx.octets[2] * 85 * 85) +	    \
+   ((ctx)->bdcontext.z85ctx.octets[3] * 85) +		    \
+   ((ctx)->bdcontext.z85ctx.octets[4]))

 # define Z85_HI_CTX_TO_32BIT_VAL(ctx) \
-  ((ctx)->z85ctx.octets[0] * 85 * 85 * 85 * 85 )
+  ((ctx)->bdcontext.z85ctx.octets[0] * 85 * 85 * 85 * 85 )
 
 /*
  0 -  9:  0 1 2 3 4 5 6 7 8 9
@@ -713,7 +713,7 @@
      so return false - indicating an invalid input.  */
   if (inlen == 0)
     {
-      if (ctx->z85ctx.i > 0)
+      if (ctx->bdcontext.z85ctx.i > 0)
         {
           /* Z85 variant does not allow padding - input must
              be a multiple of 5 - so return error.  */
@@ -744,8 +744,8 @@
 
       ++in;
 
-      ctx->z85ctx.octets[ctx->z85ctx.i++] = c;
-      if (ctx->z85ctx.i == 5)
+      ctx->bdcontext.z85ctx.octets[ctx->bdcontext.z85ctx.i++] = c;
+      if (ctx->bdcontext.z85ctx.i == 5)
         {
           /* decode the lowest 4 octets, then check for overflows.  */
           unsigned int val = Z85_LO_CTX_TO_32BIT_VAL (ctx);
@@ -761,8 +761,8 @@
              '%' (decoded to 82) in the highest octet can fit in unsigned int
              if the other 4 octets decode to a small enough value.
           */
-          if ((ctx->z85ctx.octets[0] == 84 || ctx->z85ctx.octets[0] == 83) \
-              || (ctx->z85ctx.octets[0] == 82 \
+          if ((ctx->bdcontext.z85ctx.octets[0] == 84 || ctx->bdcontext.z85ctx.octets[0] == 83) \
+              || (ctx->bdcontext.z85ctx.octets[0] == 82 \
                   && (val > 0xFFFFFFFF - 82*85*85*85*85U)))
             return false;
 
@@ -776,10 +776,10 @@
 
           *outlen += 4;
 
-          ctx->z85ctx.i = 0;
+          ctx->bdcontext.z85ctx.i = 0;
         }
     }
-  ctx->i = ctx->z85ctx.i;
+  ctx->i = ctx->bdcontext.z85ctx.i;
   return true;
 }
 
@@ -838,7 +838,7 @@
 base2_decode_ctx_init (struct base_decode_context *ctx)
 {
   init_inbuf (ctx);
-  ctx->b2ctx.octet = 0;
+  ctx->bdcontext.b2ctx.octet = 0;
   ctx->i = 0;
 }
 
@@ -870,13 +870,13 @@
         return false;
 
       bool bit = (*in == '1');
-      ctx->b2ctx.octet |= bit << ctx->i;
+      ctx->bdcontext.b2ctx.octet |= bit << ctx->i;
       ++ctx->i;
 
       if (ctx->i==8)
         {
-          *out++ = ctx->b2ctx.octet ;
-          ctx->b2ctx.octet = 0;
+          *out++ = ctx->bdcontext.b2ctx.octet ;
+          ctx->bdcontext.b2ctx.octet = 0;
           ++*outlen;
           ctx->i = 0;
         }
@@ -917,12 +917,12 @@
       if (ctx->i == 0)
         ctx->i = 8;
       --ctx->i;
-      ctx->b2ctx.octet |= bit << ctx->i;
+      ctx->bdcontext.b2ctx.octet |= bit << ctx->i;
 
       if (ctx->i==0)
         {
-          *out++ = ctx->b2ctx.octet ;
-          ctx->b2ctx.octet = 0;
+          *out++ = ctx->bdcontext.b2ctx.octet ;
+          ctx->bdcontext.b2ctx.octet = 0;
           ++*outlen;
           ctx->i = 0;
         }
diff -ur src/blake2/blake2.h src/blake2/blake2.h
--- src/blake2/blake2.h	2018-05-14 06:20:24 +0000
+++ src/blake2/blake2.h	2019-05-08 15:08:42 +0000
@@ -21,8 +21,12 @@
 #if defined(_MSC_VER)
 #define BLAKE2_PACKED(x) __pragma(pack(push, 1)) x __pragma(pack(pop))
 #else
+#ifdef __hpux
+#define BLAKE2_PACKED(x) x
+#else
 #define BLAKE2_PACKED(x) x __attribute__((packed))
 #endif
+#endif
 
 #if defined(__cplusplus)
 extern "C" {
@@ -86,6 +90,9 @@
     size_t        outlen;
   } blake2bp_state;
 
+#ifdef __hpux
+#pragma pack 1
+#endif
 
   BLAKE2_PACKED(struct blake2s_param__
   {
@@ -102,9 +109,15 @@
     uint8_t  salt[BLAKE2S_SALTBYTES]; /* 24 */
     uint8_t  personal[BLAKE2S_PERSONALBYTES];  /* 32 */
   });
+#ifdef __hpux
+#pragma pack
+#endif
 
   typedef struct blake2s_param__ blake2s_param;
 
+#ifdef __hpux
+#pragma pack 1
+#endif
   BLAKE2_PACKED(struct blake2b_param__
   {
     uint8_t  digest_length; /* 1 */
@@ -120,6 +133,9 @@
     uint8_t  salt[BLAKE2B_SALTBYTES]; /* 48 */
     uint8_t  personal[BLAKE2B_PERSONALBYTES];  /* 64 */
   });
+#ifdef __hpux
+#pragma pack
+#endif
 
   typedef struct blake2b_param__ blake2b_param;
 
diff -ur src/copy.c src/copy.c
--- src/copy.c	2019-01-05 11:36:22 +0000
+++ src/copy.c	2019-05-08 17:25:22 +0000
@@ -21,7 +21,9 @@
 #include <assert.h>
 #include <sys/ioctl.h>
 #include <sys/types.h>
+#if HAVE_SELINUX_SELINUX_H
 #include <selinux/selinux.h>
+#endif /* HAVE_SELINUX_SELINUX_H */
 
 #if HAVE_HURD_H
 # include <hurd.h>
@@ -893,6 +895,7 @@
 set_process_security_ctx (char const *src_name, char const *dst_name,
                           mode_t mode, bool new_dst, const struct cp_options *x)
 {
+#if HAVE_SELINUX_SELINUX_H
   if (x->preserve_security_context)
     {
       /* Set the default context for the process to match the source.  */
@@ -940,6 +943,7 @@
                  quoteaf (dst_name));
         }
     }
+#endif /* HAVE_SELINUX_SELINUX_H */
 
   return true;
 }
@@ -1066,6 +1070,7 @@
       dest_desc = open (dst_name, open_flags);
       dest_errno = errno;
 
+#if HAVE_SELINUX_SELINUX_H
       /* When using cp --preserve=context to copy to an existing destination,
          reset the context as per the default context, which has already been
          set according to the src.
@@ -1086,6 +1091,7 @@
                 }
             }
         }
+#endif /* HAVE_SELINUX_SELINUX_H */
 
       if (dest_desc < 0 && x->unlink_dest_after_failed_open)
         {
@@ -1768,9 +1774,11 @@
 static void
 restore_default_fscreatecon_or_die (void)
 {
+#if HAVE_SELINUX_SELINUX_H
   if (setfscreatecon (NULL) != 0)
     die (EXIT_FAILURE, errno,
          _("failed to restore the default file creation context"));
+#endif /* HAVE_SELINUX_SELINUX_H */
 }
 
 /* Create a hard link DST_NAME to SRC_NAME, honoring the REPLACE, VERBOSE and
@@ -2483,12 +2491,14 @@
 
   delayed_ok = true;
 
+#if HAVE_SELINUX_SELINUX_H
   /* If required, set the default security context for new files.
      Also for existing files this is used as a reference
      when copying the context with --preserve=context.
      FIXME: Do we need to consider dst_mode_bits here?  */
   if (! set_process_security_ctx (src_name, dst_name, src_mode, new_dst, x))
     return false;
+#endif /* HAVE_SELINUX_SELINUX_H */
 
   if (S_ISDIR (src_mode))
     {
diff -ur src/local.mk src/local.mk
--- src/local.mk	2019-03-04 09:36:33 +0000
+++ src/local.mk	2019-05-08 15:40:50 +0000
@@ -431,8 +431,8 @@
 # Note libstdbuf is only compiled if GCC is available
 # (as per the check in configure.ac), so these flags should be available.
 # libtool is probably required to relax this dependency.
-src_libstdbuf_so_LDFLAGS = -shared
-src_libstdbuf_so_CFLAGS = -fPIC $(AM_CFLAGS)
+src_libstdbuf_so_LDFLAGS = -b
+src_libstdbuf_so_CFLAGS = +z $(AM_CFLAGS)
 
 BUILT_SOURCES += src/coreutils.h
 if SINGLE_BINARY
diff -ur src/mkfifo.c src/mkfifo.c
--- src/mkfifo.c	2019-01-05 11:36:22 +0000
+++ src/mkfifo.c	2019-05-08 17:25:37 +0000
@@ -20,7 +20,9 @@
 #include <stdio.h>
 #include <getopt.h>
 #include <sys/types.h>
+#if HAVE_SELINUX_SELINUX_H
 #include <selinux/selinux.h>
+#endif /* HAVE_SELINUX_SELINUX_H */
 
 #include "system.h"
 #include "die.h"
@@ -80,7 +82,9 @@
   char const *specified_mode = NULL;
   int exit_status = EXIT_SUCCESS;
   int optc;
+#if HAVE_SELINUX_SELINUX_H
   char const *scontext = NULL;
+#endif /* HAVE_SELINUX_SELINUX_H */
   bool set_security_context = false;
 
   initialize_main (&argc, &argv);
@@ -98,6 +102,7 @@
         case 'm':
           specified_mode = optarg;
           break;
+#if HAVE_SELINUX_SELINUX_H
         case 'Z':
           if (is_smack_enabled ())
             {
@@ -118,6 +123,7 @@
                        "it requires an SELinux/SMACK-enabled kernel"));
             }
           break;
+#endif /* HAVE_SELINUX_SELINUX_H */
         case_GETOPT_HELP_CHAR;
         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
         default:
@@ -131,6 +137,7 @@
       usage (EXIT_FAILURE);
     }
 
+#if HAVE_SELINUX_SELINUX_H
   if (scontext)
     {
       int ret = 0;
@@ -144,6 +151,7 @@
              _("failed to set default file creation context to %s"),
              quote (scontext));
     }
+#endif /* HAVE_SELINUX_SELINUX_H */
 
   newmode = MODE_RW_UGO;
   if (specified_mode)
diff -ur src/system.h src/system.h
--- src/system.h	2019-01-05 11:36:22 +0000
+++ src/system.h	2019-05-08 15:19:29 +0000
@@ -422,6 +422,10 @@
 # endif
 #endif
 
+#ifndef __GNUC__
+#define __attribute(x) /* empty */
+#endif
+
 #ifndef ATTRIBUTE_NORETURN
 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
 #endif
--- configure.ac	2019-03-04 08:40:55 +0000
+++ configure.ac	2019-05-08 15:40:41 +0000
@@ -481,8 +481,8 @@
 gl_WARN_ADD([-errwarn], [CFLAGS])
 # Put this message here, after gl_WARN_ADD's chatter.
 AC_MSG_CHECKING([whether this system supports stdbuf])
-CFLAGS="-fPIC $CFLAGS"
-LDFLAGS="-shared $LDFLAGS"
+CFLAGS="+z $CFLAGS"
+LDFLAGS="-b $LDFLAGS"
 stdbuf_supported=no
 # Note we only LINK here rather than RUN to support cross compilation
 AC_LINK_IFELSE(
--- build-aux/gen-lists-of-programs.sh	2019-05-08 18:07:21 +0000
+++ build-aux/gen-lists-of-programs.sh	2019-05-08 16:27:21 +0000
@@ -42,7 +42,6 @@
 # be buildable without problems on any target system.
 normal_progs='
     [
-    b2sum
     base64
     base32
     basenc