Compile error on the aarch64 platform: Missing asm/hwcap.h

Started by 高增琦about 2 months ago10 messages
#1高增琦
pgf00a@gmail.com

Hi,

Compile error on CentOS Linux release 7.6.1810 (AltArch) with the message
“‘HWCAP_CRC32’ undeclared”.

Relevant code from discussion:
/messages/by-id/4496616.iHFcN1HehY@portable-bastien
It mentions: "It doesn't look like I need to include <asm/hwcap.h> from
Bastien's
original message, because <sys/auxv.h> pulls in <bits/hwcap.h>
already."

However, the bits/hwcap.h file included with glibc in CentOS 7.6 lacks
these symbol definitions (the file is nearly empty). This file appears to
be an internal glibc dependency, so it's preferable to directly use
asm/hwcap.h for the required symbol definitions.

After modifying the code to add `include <asm/hwcap.h>`, the compilation
succeeded.

--
GaoZengqi
pgf00a@gmail.com
zengqigao@gmail.com

#2John Naylor
johncnaylorls@gmail.com
In reply to: 高增琦 (#1)
Re: Compile error on the aarch64 platform: Missing asm/hwcap.h

On Fri, Nov 14, 2025 at 11:02 AM 高增琦 <pgf00a@gmail.com> wrote:

Compile error on CentOS Linux release 7.6.1810 (AltArch) with the message “‘HWCAP_CRC32’ undeclared”.

We still have at least one CentOS 7 aarch64 machine that gets regular
testing (not sure of the reason, since it's been EOL for a year and a
half IIUC), and it builds fine:

https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=arowana&amp;dt=2025-11-04%2004%3A56%3A57

After modifying the code to add `include <asm/hwcap.h>`, the compilation succeeded.

We already have the following, so I'm not sure what you mean (or even
what architecture you're running on):

#if defined(HAVE_ELF_AUX_INFO) || defined(HAVE_GETAUXVAL)
#include <sys/auxv.h>
#if defined(__linux__) && !defined(__aarch64__) && !defined(HWCAP2_CRC32)
#include <asm/hwcap.h>
#endif
#endif

--
John Naylor
Amazon Web Services

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: John Naylor (#2)
Re: Compile error on the aarch64 platform: Missing asm/hwcap.h

John Naylor <johncnaylorls@gmail.com> writes:

We already have the following, so I'm not sure what you mean (or even
what architecture you're running on):

#if defined(HAVE_ELF_AUX_INFO) || defined(HAVE_GETAUXVAL)
#include <sys/auxv.h>
#if defined(__linux__) && !defined(__aarch64__) && !defined(HWCAP2_CRC32)
#include <asm/hwcap.h>
#endif
#endif

I wonder if the "&& !defined(__aarch64__)" bit needs to be removed.
But yeah, the real question is what is the difference between the
OP's machine and everyplace else where this code works fine ...

regards, tom lane

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Tom Lane (#3)
Re: Compile error on the aarch64 platform: Missing asm/hwcap.h

I wrote:

I wonder if the "&& !defined(__aarch64__)" bit needs to be removed.

No, that idea is off-track. Looking at the following code, the
HWCAP2_CRC32 flag is only relevant on ARM32, so we don't need to
worry about obtaining a definition for it on aarch64 (and we'd likely
not find one anyway). The OP is seemingly building on ARM32, else
it shouldn't try to reference this symbol.

I checked an old ARM32 Red Hat installation (Fedora 30, so it's
not as old as RHEL7, but it's the oldest image I've got) and
verified that <asm/hwcap.h> exists, and it defines HWCAP2_CRC32,
and our current code builds fine on it.

But yeah, the real question is what is the difference between the
OP's machine and everyplace else where this code works fine ...

After staring at the code for awhile, it appears to me that the
only way to explain this report is that "defined(__linux__)"
isn't true on the OP's machine. Which is quite hard to believe,
and if it's the case then it's not something to fix on our side.

regards, tom lane

#5Steven Niu
niushiji@gmail.com
In reply to: John Naylor (#2)
Re: Compile error on the aarch64 platform: Missing asm/hwcap.h

Hi, John, Thomas,

IvorySQL team found the same build issue on our building machine when we built IvorySQL code which is based on PG 18.0.

pg_crc32c_armv8_choose.c:58:32:error:'HWCAP CRC32' undeclared(first use in this function)
58 | return (getauxval(AT_HWCAP) & HWCAP_CRC32) != θ;
| ^~~~~~~~~~~

Then we tried to build source code of PG 18.1 and got the same failure result.

I made some investigation to test the following code in src/port/pg_crc32c_armv8_choose.c

+#if defined(HAVE_ELF_AUX_INFO) || defined(HAVE_GETAUXVAL)
+#include <sys/auxv.h>
+#if defined(__linux__) && !defined(__aarch64__) && !defined(HWCAP2_CRC32)
+#include <asm/hwcap.h>
+#endif

// 1. Test defined(HAVE_ELF_AUX_INFO) || defined(HAVE_GETAUXVAL)
// Got #warning "CONDITION IS FALSE"
echo '#if defined(HAVE_ELF_AUX_INFO) || defined(HAVE_GETAUXVAL)
#warning "CONDITION IS TRUE"
#else
#warning "CONDITION IS FALSE"
#endif' | gcc -E -x c - -o -

// 2. Test defined(__linux__)
// Got true
echo '#if defined(__linux__)
#warning "CONDITION IS TRUE"
#else
#warning "CONDITION IS FALSE"
#endif' | gcc -E -x c - -o -

// 3. Test !defined(__aarch64__)
// Got false
echo '#if !defined(__aarch64__)
#warning "CONDITION IS TRUE"
#else
#warning "CONDITION IS FALSE"
#endif' | gcc -E -x c - -o -

// Test !defined(HWCAP2_CRC32)
// Got true
echo '#if !defined(HWCAP2_CRC32)
#warning "CONDITION IS TRUE"
#else
#warning "CONDITION IS FALSE"
#endif' | gcc -E -x c - -o -

As a result, hwcap.h has no chance to be included in the building path.

So, my question is, why we exclude __aarch64__ in the second #if condition?

Thanks,
Steven

________________________________
From: John Naylor <johncnaylorls@gmail.com>
Sent: Friday, November 14, 2025 13:06
To: 高增琦 <pgf00a@gmail.com>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Subject: Re: Compile error on the aarch64 platform: Missing asm/hwcap.h

On Fri, Nov 14, 2025 at 11:02 AM 高增琦 <pgf00a@gmail.com> wrote:

Compile error on CentOS Linux release 7.6.1810 (AltArch) with the message “‘HWCAP_CRC32’ undeclared”.

We still have at least one CentOS 7 aarch64 machine that gets regular
testing (not sure of the reason, since it's been EOL for a year and a
half IIUC), and it builds fine:

https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=arowana&amp;dt=2025-11-04%2004%3A56%3A57

After modifying the code to add `include <asm/hwcap.h>`, the compilation succeeded.

We already have the following, so I'm not sure what you mean (or even
what architecture you're running on):

#if defined(HAVE_ELF_AUX_INFO) || defined(HAVE_GETAUXVAL)
#include <sys/auxv.h>
#if defined(__linux__) && !defined(__aarch64__) && !defined(HWCAP2_CRC32)
#include <asm/hwcap.h>
#endif
#endif

--
John Naylor
Amazon Web Services

#6高增琦
pgf00a@gmail.com
In reply to: Steven Niu (#5)
Re: Compile error on the aarch64 platform: Missing asm/hwcap.h

The build farm success because the configure result: checking which CRC-32C
implementation to use... slicing-by-8

My test machine's configure result: checking which CRC-32C implementation
to use... ARMv8 CRC instructions with runtime check

More info about the machine:
uname -a:
Linux xxx 4.18.0-348.20.1.el7.aarch64 #1 SMP Wed Apr 13 20:57:50 UTC 2022
aarch64 aarch64 aarch64 GNU/Linux

cat /usr/include/bits/hwcap.h
/* Defines for bits in AT_HWCAP.
Copyright (C) 2012 Free Software Foundation, Inc.
This file is part of the GNU C Library.

The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/&gt;. */

#ifndef _SYS_AUXV_H
# error "Never include <bits/hwcap.h> directly; use <sys/auxv.h> instead."
#endif

/* No bits defined for this architecture. */

rpm -qa | grep glibc
glibc-headers-2.17-326.el7_9.aarch64
glibc-common-2.17-326.el7_9.aarch64
glibc-devel-2.17-326.el7_9.aarch64
glibc-2.17-326.el7_9.aarch64

/etc/redhat-release
CentOS Linux release 7.6.1810 (AltArch)

both __linux__ and __aarch64__ is defined

May be is better to always include "asm/hwcap.h"?
or check HWCAP_CRC32 for pg_crc32c_armv8_choose.c and HWCAP_SVE
for pg_popcount_aarch64.c

Steven Niu <niushiji@gmail.com> 于2025年11月17日周一 11:56写道:

Hi, John, Thomas,

IvorySQL team found the same build issue on our building machine when we
built IvorySQL code which is based on PG 18.0.

pg_crc32c_armv8_choose.c:58:32:error:'HWCAP CRC32' undeclared(first use in
this function)
58 | return (getauxval(AT_HWCAP) & HWCAP_CRC32) != θ;
|
^~~~~~~~~~~

Then we tried to build source code of PG 18.1 and got the same failure
result.

I made some investigation to test the following code in
src/port/pg_crc32c_armv8_choose.c

+#if defined(HAVE_ELF_AUX_INFO) || defined(HAVE_GETAUXVAL)
+#include <sys/auxv.h>
+#if defined(__linux__) && !defined(__aarch64__) && !defined(HWCAP2_CRC32)
+#include <asm/hwcap.h>
+#endif

// 1. Test defined(HAVE_ELF_AUX_INFO) || defined(HAVE_GETAUXVAL)
// Got #warning "CONDITION IS FALSE"
echo '#if defined(HAVE_ELF_AUX_INFO) || defined(HAVE_GETAUXVAL)
#warning "CONDITION IS TRUE"
#else
#warning "CONDITION IS FALSE"
#endif' | gcc -E -x c - -o -

// 2. Test defined(__linux__)
// Got true
echo '#if defined(__linux__)
#warning "CONDITION IS TRUE"
#else
#warning "CONDITION IS FALSE"
#endif' | gcc -E -x c - -o -

// 3. Test !defined(__aarch64__)
// Got false
echo '#if !defined(__aarch64__)
#warning "CONDITION IS TRUE"
#else
#warning "CONDITION IS FALSE"
#endif' | gcc -E -x c - -o -

// Test !defined(HWCAP2_CRC32)
// Got true
echo '#if !defined(HWCAP2_CRC32)
#warning "CONDITION IS TRUE"
#else
#warning "CONDITION IS FALSE"
#endif' | gcc -E -x c - -o -

As a result, hwcap.h has no chance to be included in the building path.

So, my question is, why we exclude __aarch64__ in the second #if
condition?

Thanks,
Steven

------------------------------
*From:* John Naylor <johncnaylorls@gmail.com>
*Sent:* Friday, November 14, 2025 13:06
*To:* 高增琦 <pgf00a@gmail.com>
*Cc:* PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
*Subject:* Re: Compile error on the aarch64 platform: Missing asm/hwcap.h

On Fri, Nov 14, 2025 at 11:02 AM 高增琦 <pgf00a@gmail.com> wrote:

Compile error on CentOS Linux release 7.6.1810 (AltArch) with the

message “‘HWCAP_CRC32’ undeclared”.

We still have at least one CentOS 7 aarch64 machine that gets regular
testing (not sure of the reason, since it's been EOL for a year and a
half IIUC), and it builds fine:

https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=arowana&amp;dt=2025-11-04%2004%3A56%3A57

After modifying the code to add `include <asm/hwcap.h>`, the compilation

succeeded.

We already have the following, so I'm not sure what you mean (or even
what architecture you're running on):

#if defined(HAVE_ELF_AUX_INFO) || defined(HAVE_GETAUXVAL)
#include <sys/auxv.h>
#if defined(__linux__) && !defined(__aarch64__) && !defined(HWCAP2_CRC32)
#include <asm/hwcap.h>
#endif
#endif

--
John Naylor
Amazon Web Services

--
GaoZengqi
pgf00a@gmail.com
zengqigao@gmail.com

#7Tom Lane
tgl@sss.pgh.pa.us
In reply to: Steven Niu (#5)
Re: Compile error on the aarch64 platform: Missing asm/hwcap.h

Steven Niu <niushiji@gmail.com> writes:

IvorySQL team found the same build issue on our building machine when we built IvorySQL code which is based on PG 18.0.

pg_crc32c_armv8_choose.c:58:32:error:'HWCAP CRC32' undeclared(first use in this function)
58 | return (getauxval(AT_HWCAP) & HWCAP_CRC32) != θ;
| ^~~~~~~~~~~

Bleah ... I confess to having misread the initial message as being a
complaint about HWCAP2_CRC32 not HWCAP_CRC32. -ENOCAFFEINE I guess.

So what we've got here is that somewhere along the line glibc decided
that sys/auxv.h should duplicate the HWCAPxxx macros from the kernel's
header. On recent (and even not so recent) aarch64 Fedora,
/usr/include/bits/hwcap.h has

/* The following must match the kernel's <asm/hwcap.h> and update the
list together with sysdeps/unix/sysv/linux/aarch64/dl-procinfo.c. */
#define HWCAP_FP (1 << 0)
...

but that is not the case if you go back as far as RHEL7.

A minimal fix might be to change

 #if defined(HAVE_ELF_AUX_INFO) || defined(HAVE_GETAUXVAL)
 #include <sys/auxv.h>
-#if defined(__linux__) && !defined(__aarch64__) && !defined(HWCAP2_CRC32)
+#if defined(__linux__)
 #include <asm/hwcap.h>
 #endif
 #endif

but that risks compiler warnings if there are any macro discrepancies
at all between bits/hwcap.h and asm/hwcap.h. I'm inclined to think
it's better to do something like

+#if defined(__linux__) && (defined(__aarch64__) ? !defined(HWCAP_CRC32) : !defined(HWCAP2_CRC32))

or perhaps that's too unreadable and we should break it out into
multiple #if's.

regards, tom lane

#8Tom Lane
tgl@sss.pgh.pa.us
In reply to: Tom Lane (#7)
Re: Compile error on the aarch64 platform: Missing asm/hwcap.h

I wrote:

... I'm inclined to think
it's better to do something like

+#if defined(__linux__) && (defined(__aarch64__) ? !defined(HWCAP_CRC32) : !defined(HWCAP2_CRC32))

or perhaps that's too unreadable and we should break it out into
multiple #if's.

I tried it as a nest of #ifdef's and decided that was none too
readable either, so pushed it as above. Thanks for the report!

regards, tom lane

#9高增琦
pgf00a@gmail.com
In reply to: Tom Lane (#8)
Re: Compile error on the aarch64 platform: Missing asm/hwcap.h

Thanks for the quick fix.

There's another one (the last one) in the previous email that needs fixing:

HWCAP_SVE for pg_popcount_aarch64.c

😅

Tom Lane <tgl@sss.pgh.pa.us> 于2025年11月18日周二 04:31写道:

I wrote:

... I'm inclined to think
it's better to do something like

+#if defined(__linux__) && (defined(__aarch64__) ? !defined(HWCAP_CRC32)

: !defined(HWCAP2_CRC32))

or perhaps that's too unreadable and we should break it out into
multiple #if's.

I tried it as a nest of #ifdef's and decided that was none too
readable either, so pushed it as above. Thanks for the report!

regards, tom lane

--
GaoZengqi
pgf00a@gmail.com
zengqigao@gmail.com

#10Tom Lane
tgl@sss.pgh.pa.us
In reply to: 高增琦 (#9)
Re: Compile error on the aarch64 platform: Missing asm/hwcap.h

=?UTF-8?B?6auY5aKe55Cm?= <pgf00a@gmail.com> writes:

There's another one (the last one) in the previous email that needs fixing:
HWCAP_SVE for pg_popcount_aarch64.c

Sorry, you didn't make clear enough that you were parenthetically
reporting a totally independent bug ...

regards, tom lane