public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Martin Liška" <mliska@suse.cz>
To: Jakub Jelinek <jakub@redhat.com>
Cc: "H.J. Lu" <hjl.tools@gmail.com>,
	Richard Biener <richard.guenther@gmail.com>,
	GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] Check endianess detection.
Date: Tue, 24 Mar 2020 11:32:32 +0100	[thread overview]
Message-ID: <b5a25c37-d29c-b636-04ef-eba959ad495b@suse.cz> (raw)
In-Reply-To: <20200324091805.GQ2156@tucnak>

[-- Attachment #1: Type: text/plain, Size: 2454 bytes --]

On 3/24/20 10:18 AM, Jakub Jelinek wrote:
> Hi!
> 
> So, assuming I'm on glibc with GCC 4.5 on powerpc64-linux.
> 
> On Tue, Mar 24, 2020 at 09:49:42AM +0100, Martin Liška wrote:
>> +/* Detect endianess based on __BYTE_ORDER__ macro.  */
>> +#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
>> +    defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_PDP_ENDIAN__)
>> +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
>> +#define PLUGIN_LITTLE_ENDIAN 1
>> +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
>> +#define PLUGIN_BIG_ENDIAN 1
>> +#elif __BYTE_ORDER__ == __ORDER_PDP_ENDIAN__
>> +#define PLUGIN_PDP_ENDIAN 1
>> +#endif
>> +#else
>> +/* Older GCC releases (<4.6.0) can make detection from glibc macros.  */
>> +#if defined(__GLIBC__) || defined(__GNU_LIBRARY__) || defined(__ANDROID__)
>> +#include <endian.h>
>> +#if __BYTE_ORDER == __LITTLE_ENDIAN
>> +#define PLUGIN_LITTLE_ENDIAN 1
>> +#elif __BYTE_ORDER == __BIG_ENDIAN
>> +#define PLUGIN_BIG_ENDIAN 1
>> +#endif
> 
> This will definePLUGIN_BIG_ENDIAN.
> 
>> +#endif
>> +/* Include all necessary header files based on target.  */
>> +#if defined(__SVR4) && defined(__sun)
>> +#include <sys/byteorder.h>
>> +#endif
>> +#if defined(__FreeBSD__) || defined(__NetBSD__) || \
>> +    defined(__DragonFly__) || defined(__minix)
>> +#include <sys/endian.h>
>> +#endif
>> +#if defined(__OpenBSD__)
>> +#include <machine/endian.h>
>> +#endif
> 
> The above headers will not be included.
> 
>> +/* Detect endianess based on _BYTE_ORDER.  */
>> +#if _BYTE_ORDER == _LITTLE_ENDIAN
> 
> So most likely _BYTE_ORDER and _LITTLE_ENDIAN macros will not be defined.
> Which means this will be handled as #if 0 == 0 and override the
>> +#define PLUGIN_LITTLE_ENDIAN 1
> 
> will define also PLUGIN_LITTLE_ENDIAN.

Ok, for being sure, I've wrapped all equality comparison with corresponding
check of the LHS is defined.

> 
>> +#elif _BYTE_ORDER == _BIG_ENDIAN
>> +#define PLUGIN_BIG_ENDIAN 1
>> +#endif
>> +/* Detect based on _WIN32.  */
>> +#if defined(_WIN32)
>> +#define PLUGIN_LITTLE_ENDIAN 1
>> +#endif
>> +/* Fallback to __BIG_ENDIAN__ and __LITTLE_ENDIAN__ */
>> +#ifdef __LITTLE_ENDIAN__
>> +#define PLUGIN_LITTLE_ENDIAN 1
>> +#endif
>> +#ifdef __BIG_ENDIAN__
>> +#define PLUGIN_BIG_ENDIAN 1
>> +#endif
>> +#endif
> 
> and the above isn't really a fallback, because it isn't guarded with
> PLUGIN_*_ENDIAN not being defined yet.

This comment has been also updated.

Martin

> 
> 	Jakub
> 


[-- Attachment #2: 0001-Improve-endianess-detection.patch --]
[-- Type: text/x-patch, Size: 2934 bytes --]

From 82b8731f304c734353c34ddaf1b1265341a90882 Mon Sep 17 00:00:00 2001
From: Martin Liska <mliska@suse.cz>
Date: Tue, 24 Mar 2020 09:12:50 +0100
Subject: [PATCH] Improve endianess detection.

include/ChangeLog:

2020-03-24  Martin Liska  <mliska@suse.cz>

	PR lto/94249
	* plugin-api.h: Add more robust endianess detection.
---
 include/plugin-api.h | 65 ++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 63 insertions(+), 2 deletions(-)

diff --git a/include/plugin-api.h b/include/plugin-api.h
index 673f136ce68..864d2bf91ac 100644
--- a/include/plugin-api.h
+++ b/include/plugin-api.h
@@ -37,6 +37,60 @@
 #error cannot find uint64_t type
 #endif
 
+/* Detect endianess based on __BYTE_ORDER__ macro.  */
+#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
+    defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_PDP_ENDIAN__)
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define PLUGIN_LITTLE_ENDIAN 1
+#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+#define PLUGIN_BIG_ENDIAN 1
+#elif __BYTE_ORDER__ == __ORDER_PDP_ENDIAN__
+#define PLUGIN_PDP_ENDIAN 1
+#endif
+#else
+/* Older GCC releases (<4.6.0) can make detection from glibc macros.  */
+#if defined(__GLIBC__) || defined(__GNU_LIBRARY__) || defined(__ANDROID__)
+#include <endian.h>
+#ifdef _BYTE_ORDER
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+#define PLUGIN_LITTLE_ENDIAN 1
+#elif __BYTE_ORDER == __BIG_ENDIAN
+#define PLUGIN_BIG_ENDIAN 1
+#endif
+#endif
+#endif
+/* Include all necessary header files based on target.  */
+#if defined(__SVR4) && defined(__sun)
+#include <sys/byteorder.h>
+#endif
+#if defined(__FreeBSD__) || defined(__NetBSD__) || \
+    defined(__DragonFly__) || defined(__minix)
+#include <sys/endian.h>
+#endif
+#if defined(__OpenBSD__)
+#include <machine/endian.h>
+#endif
+/* Detect endianess based on _BYTE_ORDER.  */
+#ifdef _BYTE_ORDER
+#if _BYTE_ORDER == _LITTLE_ENDIAN
+#define PLUGIN_LITTLE_ENDIAN 1
+#elif _BYTE_ORDER == _BIG_ENDIAN
+#define PLUGIN_BIG_ENDIAN 1
+#endif
+#endif
+/* Detect based on _WIN32.  */
+#if defined(_WIN32)
+#define PLUGIN_LITTLE_ENDIAN 1
+#endif
+/* Detect based on __BIG_ENDIAN__ and __LITTLE_ENDIAN__ */
+#ifdef __LITTLE_ENDIAN__
+#define PLUGIN_LITTLE_ENDIAN 1
+#endif
+#ifdef __BIG_ENDIAN__
+#define PLUGIN_BIG_ENDIAN 1
+#endif
+#endif
+
 #ifdef __cplusplus
 extern "C"
 {
@@ -89,16 +143,23 @@ struct ld_plugin_symbol
   char *version;
   /* This is for compatibility with older ABIs.  The older ABI defined
      only 'def' field.  */
-#ifdef __BIG_ENDIAN__
+#if PLUGIN_BIG_ENDIAN == 1
   char unused;
   char section_kind;
   char symbol_type;
   char def;
-#else
+#elif PLUGIN_LITTLE_ENDIAN == 1
   char def;
   char symbol_type;
   char section_kind;
   char unused;
+#elif PLUGIN_PDP_ENDIAN == 1
+  char symbol_type;
+  char def;
+  char unused;
+  char section_kind;
+#else
+#error "Could not detect architecture endianess"
 #endif
   int visibility;
   uint64_t size;
-- 
2.25.1


  reply	other threads:[~2020-03-24 10:32 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-23  9:25 Martin Liška
2020-03-23  9:43 ` Jakub Jelinek
2020-03-23 10:00   ` Martin Liška
2020-03-23 10:10     ` Jakub Jelinek
2020-03-23 10:28       ` Martin Liška
2020-03-23 10:35         ` Jakub Jelinek
2020-03-23 12:43           ` Martin Liška
2020-03-23 15:06             ` Martin Liška
2020-03-23 15:39               ` Richard Biener
2020-03-23 16:06                 ` H.J. Lu
2020-03-23 17:17                   ` Martin Liška
2020-03-23 17:40                     ` H.J. Lu
2020-03-24  8:19                       ` Martin Liška
2020-03-24  8:31                         ` Jakub Jelinek
2020-03-24  8:49                           ` Martin Liška
2020-03-24  9:18                             ` Jakub Jelinek
2020-03-24 10:32                               ` Martin Liška [this message]
2020-03-24 10:39                                 ` Jakub Jelinek
2020-03-31 13:27                                 ` [PATCH] PR lto/94249: Correct endianness detection with the __BYTE_ORDER macro Maciej W. Rozycki
2020-04-01  5:01                                   ` Hans-Peter Nilsson
2020-04-01  7:43                                     ` Martin Liška
2020-04-01 23:57                                       ` Hans-Peter Nilsson
2020-04-01  7:17                                   ` Richard Biener
2020-04-01  7:41                                   ` Martin Liška
2020-04-01  9:55                                     ` Maciej W. Rozycki
2020-04-01 10:01                                       ` Martin Liška
2020-04-01 15:59                                         ` Maciej W. Rozycki
2020-04-01 16:54                                           ` Martin Liška
2020-04-01 17:28                                             ` Maciej W. Rozycki
2020-04-01 10:04                                     ` Maciej W. Rozycki
2020-04-01 10:09                                       ` Martin Liška
2020-03-23 15:16             ` [PATCH] Check endianess detection Richard Biener

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b5a25c37-d29c-b636-04ef-eba959ad495b@suse.cz \
    --to=mliska@suse.cz \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=hjl.tools@gmail.com \
    --cc=jakub@redhat.com \
    --cc=richard.guenther@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).