[PATCH] inttypes.h: imaxabs(3): Implement as a macro
Alejandro Colomar
alx.manpages@gmail.com
Tue Sep 13 15:34:38 GMT 2022
Hi Florian,
On 9/13/22 17:18, Alex Colomar wrote:
> Functions using the [u]intmax_t types have a problem: ABI.
>
> The [u]intmax_t types were designed so that users could blindly
> use them to work with whatever is the biggest size for a given
> architecture. But this is problematic to implement. These days,
> ABI stability is very important, and programs compiled a specific
> version of glibc should continue working with newer glibc
> releases. Of course, that means that future glibc versions can't
> change the types of functions, or the functions would be
> incompatible.
>
> So, we cannot really change the (linker) signature of functions;
> not even of those using [u]intmax_t.
>
> A solution to that problem is to use macros, and not real
> functions. That way, the program really links against whatever
> function works with the compile-time maximum width type, be it
> long or long long, and the program will be bonded to the ABI of
> long or long long, but will know nothing about intmax_t.
>
> We still need a linker symbol with the name imaxabs(), for old
> programs linked against old glibc versions, and also for
> compatibility with an interpretation of the ISO C standard that
> says that imaxabs(3) is a function. But the standard knows
> nothing about ABI or a linker, so a macro that evaluates to a
> differently-named function is not a straight violation, but rather
> stepping through the line without crossing it, AFAIK.
>
> This implementation with C11 _Generic() is one that I used for the
> new _Generic(3) manual page. It allows treating the macro as a
> function, and to take pointers to it. And it allows to more
> easily understand the definition of the macro, without needing
> to parse the ifdefs. It makes it easier to parse with tools like
> grepc(1):
>
> $ grepc -k __PRI64_PREFIX inttypes.h
> inttypes.h:44:# define __PRI64_PREFIX "l"
> inttypes.h:47:# define __PRI64_PREFIX "ll"
> $ grepc -k imaxabs inttypes.h
> inttypes.h:290:#define imaxabs _Generic(INTMAX_C(0), long: labs, long long: llabs)
>
> If glibc can't use C11 features in public headers, then usual
> ifdefs could be used.
>
> Cc: Florian Weimer <fweimer@redhat.com>
> Cc: JeanHeyd Meneide <wg14@soasis.org>
> Signed-off-by: Alex Colomar <alx.manpages@gmail.com>
> ---
My plan, if this is welcome, is to get rid of most traces of [u]intmax_t
in the ABI, so I'd continue with similar patches for other functions and
symbols.
Cheers,
Alex
--
<http://www.alejandro-colomar.es/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <https://sourceware.org/pipermail/libc-alpha/attachments/20220913/a1f44c4e/attachment.sig>
More information about the Libc-alpha
mailing list