Skip to content

Commit

Permalink
More fixes for systems that require -lm (re: 1afa007, 4edfa45)
Browse files Browse the repository at this point in the history
As of 1afa007, the build fails on at least Arch and Kali Linux.

Analysis: same. The fix is just not complete yet. The -lm probe
programs in INIT (m*.c, compiled/linked via mkreq-maplib.sh in
INIT/Mamfile, but never run) are also written such that their
results are known at compile time, causing modern compilers to
optimise out the probe calls (on newer gcc, happens even with
-O0 (!)). This commit adds the same fix to them: rand() calls to
rule out optimisation.

Thanks to Marc Wilson (@posguy99) for the report.
Resolves: #716
  • Loading branch information
McDutchie committed Feb 2, 2024
1 parent dcb7d99 commit 81f226a
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 16 deletions.
23 changes: 17 additions & 6 deletions src/cmd/INIT/m.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* *
* This software is part of the ast package *
* Copyright (c) 1994-2011 AT&T Intellectual Property *
* Copyright (c) 2020-2023 Contributors to ksh 93u+m *
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 2.0 *
* *
Expand All @@ -17,16 +17,27 @@
***********************************************************************/
/*
* -lm test #1
*
* This program is compiled and linked by mkreq-maplib.sh via INIT/Mamfile
* but never actually run. It is only used to check if linking succeeds
* without or with -lm.
*
* For that test to work correctly, we must work around compiler optimization.
* The rand() call is to stop the result from being considered known at
* compile time, which would cause modern compilers to optimize out the probe
* calls, which would in turn cause linking to succeed where it shouldn't.
*/

#ifndef sin
#include <stdlib.h>
#include <math.h>
#endif

int
main(void)
{
sin(0.0);
fmod(100.234, 11.0);
return 0;
double f = (double)rand();
int r = 0;

r |= sin(f) != 0.0;
r |= fmod(f, 11.0) != 0.0;
return r;
}
14 changes: 12 additions & 2 deletions src/cmd/INIT/m2.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* *
* This software is part of the ast package *
* Copyright (c) 1994-2011 AT&T Intellectual Property *
* Copyright (c) 2020-2023 Contributors to ksh 93u+m *
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 2.0 *
* *
Expand All @@ -17,14 +17,24 @@
***********************************************************************/
/*
* -lm test #2
*
* This program is compiled and linked by mkreq-maplib.sh via INIT/Mamfile
* but never actually run. It is only used to check if linking succeeds
* without or with -lm.
*
* For that test to work correctly, we must work around compiler optimization.
* The rand() call is to stop the result from being considered known at
* compile time, which would cause modern compilers to optimize out the probe
* calls, which would in turn cause linking to succeed where it shouldn't.
*/

#include <stdlib.h>
#include <math.h>

int
main(void)
{
double value = 0;
double value = (double)rand();
int exp = 0;
int r = 0;

Expand Down
14 changes: 12 additions & 2 deletions src/cmd/INIT/m3.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* *
* This software is part of the ast package *
* Copyright (c) 1994-2011 AT&T Intellectual Property *
* Copyright (c) 2020-2023 Contributors to ksh 93u+m *
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 2.0 *
* *
Expand All @@ -17,14 +17,24 @@
***********************************************************************/
/*
* -lm test #3
*
* This program is compiled and linked by mkreq-maplib.sh via INIT/Mamfile
* but never actually run. It is only used to check if linking succeeds
* without or with -lm.
*
* For that test to work correctly, we must work around compiler optimization.
* The rand() call is to stop the result from being considered known at
* compile time, which would cause modern compilers to optimize out the probe
* calls, which would in turn cause linking to succeed where it shouldn't.
*/

#include <stdlib.h>
#include <math.h>

int
main(void)
{
long double value = 0;
long double value = (long double)rand();
int exp = 0;
int r = 0;

Expand Down
14 changes: 12 additions & 2 deletions src/cmd/INIT/m4.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* *
* This software is part of the ast package *
* Copyright (c) 1994-2011 AT&T Intellectual Property *
* Copyright (c) 2020-2023 Contributors to ksh 93u+m *
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 2.0 *
* *
Expand All @@ -17,14 +17,24 @@
***********************************************************************/
/*
* -lm test #4
*
* This program is compiled and linked by mkreq-maplib.sh via INIT/Mamfile
* but never actually run. It is only used to check if linking succeeds
* without or with -lm.
*
* For that test to work correctly, we must work around compiler optimization.
* The rand() call is to stop the result from being considered known at
* compile time, which would cause modern compilers to optimize out the probe
* calls, which would in turn cause linking to succeed where it shouldn't.
*/

#include <stdlib.h>
#include <math.h>

int
main(void)
{
double value = 0;
double value = (double)rand();

return isnan(value);
}
14 changes: 12 additions & 2 deletions src/cmd/INIT/m5.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* *
* This software is part of the ast package *
* Copyright (c) 1994-2011 AT&T Intellectual Property *
* Copyright (c) 2020-2023 Contributors to ksh 93u+m *
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 2.0 *
* *
Expand All @@ -17,14 +17,24 @@
***********************************************************************/
/*
* -lm test #5
*
* This program is compiled and linked by mkreq-maplib.sh via INIT/Mamfile
* but never actually run. It is only used to check if linking succeeds
* without or with -lm.
*
* For that test to work correctly, we must work around compiler optimization.
* The rand() call is to stop the result from being considered known at
* compile time, which would cause modern compilers to optimize out the probe
* calls, which would in turn cause linking to succeed where it shouldn't.
*/

#include <stdlib.h>
#include <math.h>

int
main(void)
{
long double value = 0;
long double value = (long double)rand();

return isnanl(value);
}
14 changes: 12 additions & 2 deletions src/cmd/INIT/m6.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* *
* This software is part of the ast package *
* Copyright (c) 1994-2011 AT&T Intellectual Property *
* Copyright (c) 2020-2023 Contributors to ksh 93u+m *
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 2.0 *
* *
Expand All @@ -17,16 +17,26 @@
***********************************************************************/
/*
* -lm test #6
*
* This program is compiled and linked by mkreq-maplib.sh via INIT/Mamfile
* but never actually run. It is only used to check if linking succeeds
* without or with -lm.
*
* For that test to work correctly, we must work around compiler optimization.
* The rand() call is to stop the result from being considered known at
* compile time, which would cause modern compilers to optimize out the probe
* calls, which would in turn cause linking to succeed where it shouldn't.
*/

#define _ISOC99_SOURCE 1

#include <stdlib.h>
#include <math.h>

int
main(void)
{
double value = -0.0;
double value = -((double)rand());

return !signbit(value);
}

0 comments on commit 81f226a

Please sign in to comment.