Checking if a system is ELF

Started by J.M.over 26 years ago6 messages
#1J.M.
darcy@druid.net

Todd Vierling (tv@pobox.com) suggested the following test for ELFness.
Seems pretty portable to me.

Thus spake Todd Vierling (tv@pobox.com)

On NetBSD, the following will do it. This may even be platform independednt
if "grep -q" is replaced by "grep >/dev/null 2>&1".

if echo __ELF__ | ${CC} -E - | grep -q __ELF__; then
... a.out action ...
else
... ELF action ...
fi

-- 
D'Arcy J.M. Cain <darcy@{druid|vex}.net>   |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 424 2871     (DoD#0082)    (eNTP)   |  what's for dinner.
#2Ross J. Reedstrom
reedstrm@wallace.ece.rice.edu
In reply to: J.M. (#1)
Re: [HACKERS] Checking if a system is ELF

On Tue, Jul 27, 1999 at 05:58:33PM -0400, D'Arcy J.M. Cain wrote:

Todd Vierling (tv@pobox.com) suggested the following test for ELFness.
Seems pretty portable to me.

Thus spake Todd Vierling (tv@pobox.com)

On NetBSD, the following will do it. This may even be platform independednt
if "grep -q" is replaced by "grep >/dev/null 2>&1".

if echo __ELF__ | ${CC} -E - | grep -q __ELF__; then
... a.out action ...
else
... ELF action ...
fi

Uh, two problems:

One, it assumes ${CC} is really gcc - the native SGI MIPS compiler doesn't
like - for compiling stdin. Second, my linux box seems to #define __ELF__
as 1. I'm not sure if that is gcc version related, or platform.

tatabox% echo __ELF__ | cc -E -
cc ERROR parsing -: unknown flag
cc ERROR: no source or object file given
tatabox% cc -version
MIPSpro Compilers: Version 7.2.1.3m

tatabox% echo __ELF__ | gcc -E -
# 1 ""
__ELF__
tatabox% gcc -v
Reading specs from /usr/site/egcs-1.1.2/lib/gcc-lib/mips-sgi-irix6.5/egcs-2.91.66/specs
gcc version egcs-2.91.66 19990314 (egcs-1.1.2 release)

wallace$ echo __ELF__ | gcc -E -
# 1 ""
1
wallace$ gcc -v
Reading specs from /usr/lib/gcc-lib/i486-linux/2.7.2.3/specs
gcc version 2.7.2.3

Ross
--
Ross J. Reedstrom, Ph.D., <reedstrm@rice.edu>
NSBRI Research Scientist/Programmer
Computer and Information Technology Institute
Rice University, 6100 S. Main St., Houston, TX 77005

#3J.M.
darcy@druid.net
In reply to: Ross J. Reedstrom (#2)
Re: [HACKERS] Checking if a system is ELF

Thus spake Ross J. Reedstrom

Uh, two problems:

One, it assumes ${CC} is really gcc - the native SGI MIPS compiler doesn't
like - for compiling stdin.

OK, so we do the test for specific ports like NetBSD. That would be better
than no test at all. Perhaps we can also add an option to config to force
it if we can't tell automatically.

Second, my linux box seems to #define __ELF__
as 1. I'm not sure if that is gcc version related, or platform.

Well, that's sort of the idea assuming that your Linux box is ELF. Here
is the output from two systems. Druid is a.out and smaug is ELF.

[darcy@druid:work/trends] $ echo __ELF__ | gcc -E -
# 1 ""
__ELF__

[db@smaug:/usr/db] $ echo __ELF__ | gcc -E -
# 1 ""
1

So grep will find "__ELF__" in the output on druid proving that it is an
a.out system. On smaug, __ELF__ is defined as "1" so grep fails to find
the string "__ELF__" proving it to be an ELF system.

tatabox% echo __ELF__ | cc -E -
cc ERROR parsing -: unknown flag
cc ERROR: no source or object file given

Is there any way to compile stdin? If so then all we need to do is make
the command a variable and special case it for some ports.

tatabox% cc -version
MIPSpro Compilers: Version 7.2.1.3m

tatabox% echo __ELF__ | gcc -E -
# 1 ""
__ELF__

This implies that tatabox is not an ELF system. Is that accurate?

wallace$ echo __ELF__ | gcc -E -
# 1 ""
1

And this says that wallace is. Correct or no?

-- 
D'Arcy J.M. Cain <darcy@{druid|vex}.net>   |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 424 2871     (DoD#0082)    (eNTP)   |  what's for dinner.
#4Ross J. Reedstrom
reedstrm@wallace.ece.rice.edu
In reply to: J.M. (#3)
Re: [HACKERS] Checking if a system is ELF

On Tue, Jul 27, 1999 at 08:20:31PM -0400, D'Arcy J.M. Cain wrote:

Thus spake Ross J. Reedstrom

tatabox% echo __ELF__ | gcc -E -
# 1 ""
__ELF__

This implies that tatabox is not an ELF system. Is that accurate?

wallace$ echo __ELF__ | gcc -E -
# 1 ""
1

And this says that wallace is. Correct or no?

Quite correct. I had the logic backwords from the original suggestion.
I can only plead pre-dinner hunger - lack of glucose to the brain ;-)

Don't know about feeding the native cc on stdin. Probably have to
create a temp file and compile that.

Ross

--
Ross J. Reedstrom, Ph.D., <reedstrm@rice.edu>
NSBRI Research Scientist/Programmer
Computer and Information Technology Institute
Rice University, 6100 S. Main St., Houston, TX 77005

#5Tom Lane
tgl@sss.pgh.pa.us
In reply to: Ross J. Reedstrom (#4)
Re: [HACKERS] Checking if a system is ELF

"D'Arcy" "J.M." Cain <darcy@druid.net> writes:

So grep will find "__ELF__" in the output on druid proving that it is an
a.out system. On smaug, __ELF__ is defined as "1" so grep fails to find
the string "__ELF__" proving it to be an ELF system.

Seems to me that this is a test for __ELF__ being defined, but not for
exactly what it is defined as. Mightn't a non-ELF system define it as 0?

Also, I think there are prefab test macros in Autoconf for checking
whether a #define symbol exists ... you shouldn't have to do anything
as grotty as writing out an explicit test program ...

regards, tom lane

#6J.M.
darcy@druid.net
In reply to: Tom Lane (#5)
Re: [HACKERS] Checking if a system is ELF

Thus spake Tom Lane

"D'Arcy" "J.M." Cain <darcy@druid.net> writes:

So grep will find "__ELF__" in the output on druid proving that it is an
a.out system. On smaug, __ELF__ is defined as "1" so grep fails to find
the string "__ELF__" proving it to be an ELF system.

Seems to me that this is a test for __ELF__ being defined, but not for
exactly what it is defined as. Mightn't a non-ELF system define it as 0?

Hard to imagine. Pre-ELF systems wouldn't know about it one way or
another. However, it is certainly a theoretical possibility.

Also, I think there are prefab test macros in Autoconf for checking
whether a #define symbol exists ... you shouldn't have to do anything
as grotty as writing out an explicit test program ...

This is why I didn't send in diffs. I don't know enough about autoconf.
Is anyone looking at this discussion planning to incorporate something?

-- 
D'Arcy J.M. Cain <darcy@{druid|vex}.net>   |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 424 2871     (DoD#0082)    (eNTP)   |  what's for dinner.