> Tobias <[email protected]> wrote:
>
>> I am searching a constant to check if I am compiling on an
(Open)Solaris
>> system with x86 architecture. Apple defines __APPLE__ to check for Mac
OS X, WIN32 can be used for Windows etc. Is there something for
(Open)Solaris? I could not find any information on this.
>> thanks for your help
>
> Just call:
>
> cc -# t.c
>
> and check the parameters of the called programs.....
>
> /opt/SUNWspro/prod/bin/acomp -xldscope=global -i t.c -y-fbe
> -y/oroot/opt/SUNWspro/prod/bin/fbe -y-xarch=generic -y-o -yt.o
-y-verbose
> -y-xthreadvar=no%dynamic -y-comdat -xdbggen=no%stabs+dwarf2+usedonly
-xdbggen=incl -y-s -m32 -fparam_ir -Qy -D__SunOS_5_11 -D__SUNPRO_C=0x590
-D__SVR4 -D__sun -D__SunOS -D__unix -D__i386 -D__BUILTIN_VA_ARG_INCR
-D__C99FEATURES__ -Xa -D__PRAGMA_REDEFINE_EXTNAME -Dunix -Dsun -Di386
-D__RESTRICT -xc99=%all,no%lib -D__FLT_EVAL_METHOD__=-1
> -I/oroot/opt/SUNWspro/prod/include/cc "-g/oroot/opt/SUNWspro/prod/bin/cc
-c "
> -fsimple=0 -D__SUN_PREFETCH -destination_ir=yabe
>
> People usually check for -Dsun and -D__SVR4
>
> Jörg
[ Please pardon the pedantic long winded email. I got carried away. ]
Also, if you are a standards(5) compliant focused person then you may want
to watch for _POSIX_SOURCE. From the "POSIX Programmers Guide, Writing
Portable UNIX Programs", Donald Lewine, page 25 we see :
/* Feature test switches */
This section should define the _POSIX_SOURCE macro to enable
the POSIX symbols and disable all unspecified symbols.
That can have some pretty wide sweeping effects on how a binary is
compiled and linked.
Also, in standards(5) we see mention of various OpenGroup documents and
specs that Solaris revs from 2.5.1 through 10 are compliant with. There is
no mention of OpenSolaris or Solaris Nevada so therefore I do not know
where either of those stand.
Should one wish some degree of compliance with SUSv3 then there is a PATH
to set :
From standards(5) in OpenSolaris snv_111b
POSIX.1-2001, SUSv3
1. /usr/xpg6/bin
2. /usr/xpg4/bin
3. /usr/ccs/bin
4. /usr/bin
5. directory containing binaries for your compiler
6. other directories containing binaries needed by
the application
I do not know what degree of compliance there is in OpenSolaris snv_111b
but my PATH and env looks like so when I compile things :
# env | sort
EDITOR=/usr/xpg4/bin/vi
HOME=/export/home/dclarke
HZ=
LANG=C
LC_ALL=C
LC_MESSAGES=en_US.UTF-8
LOGNAME=dclarke
MAIL=/var/mail/dclarke
MANPATH=/usr/share/man:/usr/X11/share/man
OLDPWD=/opt
PAGER=/usr/xpg4/bin/more
PATH=/usr/xpg6/bin:/usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin:/usr/sbin:/bin:/sbin:/usr/X11/bin:/usr/dt/bin:/usr/openwin/bin:/opt/schily/bin:/opt/csw/bin
PWD=/export/home/dclarke/build
SHELL=/bin/ksh93
SHLVL=1
TERM=xterm
VISUAL=/usr/xpg4/bin/vi
_=/usr/xpg4/bin/env
To be honest, I know that there are a bucket load of #define's in
<unistd.h> and also in the implementation specific <sys/unistd.h> that
speak to various specs and standards that one may comply with. If one is
careful you can end up with code that compiles *everywhere* and runs
exactly as expected on all systems that are compliant with a SUSv3 spec or
_XPG6.
I do know that a default install of OpenSolaris from media will fail to
compile a simple hello.c because there are no SUNWhea headers provided :
$ uname -a
SunOS aequitas 5.11 snv_111b i86pc i386 i86pc
$ cat -n hello.c
1 #include <stdio.h>
2 int main(int argc, char *argv[])
3 {
4 printf ( "Hello World!\n" );
5 return (0);
6 }
7
$ cc -\# -H -s -c -o hello.s hello.c
### Note: NLSPATH =
/opt/SUNWspro/prod/bin/../lib/locale/%L/LC_MESSAGES/%N.cat:/opt/SUNWspro/prod/bin/../../lib/locale/%L/LC_MESSAGES/%N.cat
### command line files and options (expanded):
### -c -H -s hello.c -o hello.s
/opt/SUNWspro/prod/bin/acomp -xldscope=global -i hello.c -y-fbe
-y/opt/SUNWspro/prod/bin/fbe -y-xarch=generic -y-o -yhello.s -y-verbose
-y-xthreadvar=no%dynamic -y-comdat -xdbggen=no%stabs+dwarf2+usedonly -H
-m32 -fparam_ir -Qy -D__SunOS_5_11 -D__SUNPRO_C=0x590 -D__SVR4 -D__sun
-D__SunOS -D__unix -D__i386 -D__BUILTIN_VA_ARG_INCR -D__C99FEATURES__ -Xa
-D__PRAGMA_REDEFINE_EXTNAME -Dunix -Dsun -Di386 -D__RESTRICT
-xc99=%all,no%lib -D__FLT_EVAL_METHOD__=-1 -I/opt/SUNWspro/prod/include/cc
"-g/opt/SUNWspro/prod/bin/cc -H -s -c " -fsimple=0 -D__SUN_PREFETCH
-destination_ir=yabe
"hello.c", line 1: cannot find include file: <stdio.h>
"hello.c", line 4: warning: implicit function declaration: printf
cc: acomp failed for hello.c
Thats easy to fix however :
# pkg refresh
# pkg list -av SUNWhea
FMRI STATE
UFIX
pkg:/[email protected],5.11-0.111:20090508T160738Z known
----
# pkg install -v [email protected],5.11-0.111:20090508T160738Z
Creating Plan / Before evaluation:
UNEVALUATED:
+pkg:/[email protected],5.11-0.111:20090508T160738Z
After evaluation:
None -> pkg:/[email protected],5.11-0.111:20090508T160738Z
Actuators:
None
DOWNLOAD PKGS FILES XFER (MB)
SUNWhea 0/1 430/1441 1.00/3.43
SUNWhea 0/1 825/1441 2.00/3.43
Completed 1/1 1441/1441 3.43/3.43
PHASE ACTIONS
Install Phase 1546/1546
#
You will also need SUNWarc :
# pkg list -av SUNWarc
FMRI STATE
UFIX
pkg:/[email protected],5.11-0.111:20090508T153223Z known
----
#
# pkg install -v [email protected],5.11-0.111:20090508T153223Z
Creating Plan - Before evaluation:
UNEVALUATED:
+pkg:/[email protected],5.11-0.111:20090508T153223Z
After evaluation:
None -> pkg:/[email protected],5.11-0.111:20090508T153223Z
Actuators:
None
DOWNLOAD PKGS FILES XFER (MB)
Completed 1/1 290/290 1.47/1.47
PHASE ACTIONS
Install Phase 487/487
#
Then things just work :
c89 -\# -H -Xc -m32 -xarch=386 -nofstore -Qn -xbuiltin=%none -xnolibmil
-xnolibmopt -Kpic -xildoff -xregs=no%frameptr -xstrconst -D_TS_ERRNO
-D_POSIX_SOURCE -o hello hello.c
### Note: NLSPATH =
/opt/SUNWspro/prod/bin/../lib/locale/%L/LC_MESSAGES/%N.cat:/opt/SUNWspro/prod/bin/../../lib/locale/%L/LC_MESSAGES/%N.cat
### command line files and options (expanded):
### -D_TS_ERRNO -D_POSIX_SOURCE -errwarn=E_CANT_ACCESS_INCLUDE_DIR
-nofstore -H -Kpic -Xc -xarch=386 -m32 -features=no%typeof
-xregs=no%frameptr -xstrconst hello.c -o hello
/opt/SUNWspro/prod/bin/acomp -xldscope=global -i hello.c -y-fbe
-y/opt/SUNWspro/prod/bin/fbe -y-pic -y-xarch=386 -y-o -yhello.o -y-verbose
-y-xthreadvar=dynamic -y-comdat -xdbggen=no%stabs+dwarf2+usedonly -e
rrwarn=E_CANT_ACCESS_INCLUDE_DIR -xwarn_include_dir=permission -H
-strconst -D_TS_ERRNO -D_POSIX_SOURCE -xdbggen=incl -y-s -2k -m32
-fparam_ir -D__SunOS_5_11 -D__SUNPRO_C=0x590 -D__SVR4 -D__sun -D__SunOS
-D__unix -D__i386 -D__BUILTIN_VA_ARG_INCR -Xc -D__PRAGMA_REDEFINE_EXTNAME
-xc99=%none -features=no%typeof -I/usr/xpg4/include
-I/opt/SUNWspro/prod/include/cc "-g/opt/SUNWspro/prod/bin/c89 -H -Xc -m32
-xarch=386 -nofstore -Qn -xbuiltin=%none -xnolibmil -xnolibmopt -Kpic
-xildoff -xregs=no%frameptr -xstrconst -D_TS_ERRNO -D_POSIX_SOURCE -c "
-fsimple=0 -D__SUN_PREFETCH -destination_ir=yabe
/usr/include/stdio.h
/usr/include/sys/feature_tests.h
/usr/include/sys/ccompile.h
/usr/include/sys/isa_defs.h
/usr/include/iso/stdio_iso.h
/usr/include/sys/va_list.h
/usr/include/stdio_tag.h
/usr/include/stdio_impl.h
/usr/include/iso/stdio_c99.h
/opt/SUNWspro/prod/bin/fbe -s -o hello.o -warn=%none -Qy /tmp/yabeAAAdeaqYi
rm /tmp/yabeAAAdeaqYi
### Note: LD_LIBRARY_PATH = <null>
### Note: LD_RUN_PATH = <null>
/usr/ccs/bin/ld -L/usr/xpg4/lib -R/usr/xpg4/lib
/opt/SUNWspro/prod/lib/crti.o /opt/SUNWspro/prod/lib/crt1.o
/opt/SUNWspro/prod/lib/values-xc.o /opt/SUNWspro/prod/lib/values-xpg4.o -o
hello hello.o -Y "P,/opt/SUNWspro/prod/lib:/usr/ccs/lib:/lib:/usr/lib" -lc
-Bdynamic -ldl /opt/SUNWspro/prod/lib/crtn.o
$
Well what can you now dig out of that binary ? I mean information about
how it was built and what tools were used ?
$ elfdump hello | grep NEED
[0] NEEDED 0x165 libc.so.1
[1] NEEDED 0x18b libdl.so.1
[17] VERNEED 0x80507f4
[18] VERNEEDNUM 0x1
That tells you what shared objects are needed.
$ elfdump hello | grep PATH
[4] RUNPATH 0x196 /usr/xpg4/lib
[5] RPATH 0x196 /usr/xpg4/lib
That tells you where the runtime linker is going to look for those needed
libs. If not found there then it will default to /usr/lib and you can play
with LD_FLAGS/LD_OPTIONS to see more details on that.
You can set this in your environment :
LD_OPTIONS=\-R/somepath/lib/\ \-L/somepage/lib
export LD_OPTIONS
Studio will stuff that RUNPATH/RPATH data into the resultant binary and
now the runtime linker will look in that path first for libs. That can be
dangerous and can result in a real security breach if someone gives you a
binary with their own custom libs that they hacked. Even more scary is
something call $ORIGIN for a place to look for runtime dynamic libs. Feel
free to look into it with Daryl Gove's book "Solaris Application
Programming" and please see
http://www.blastwave.org/dclarke/blog/?q=node/138
Some information about the tools used can be found with :
$ mcs -p hello
hello:
%Z%%M% %I% %E% SMI
%Z%%M% %I% %E% SMI
%Z%%M% %I% %E% SMI
%Z%%M% %I% %E% SMI
%Z%%M% %I% %E% SMI
%Z%%M% %I% %E% SMI
%Z%%M% %I% %E% SMI
as: Sun Compiler Common 12 SunOS_i386 Patch 126498-11 2008/10/06
whoops !
I should set the compiler flag -Qy such that information is placed in
there.
If you really want to look into the ELF Header format there are a bucket
load of flags but one thing will jump out at you and that is the ei_class
and e_machine data :
$ elfdump hello | head -12
ELF Header
ei_magic: { 0x7f, E, L, F }
ei_class: ELFCLASS32 ei_data: ELFDATA2LSB
e_machine: EM_386 e_version: EV_CURRENT
e_type: ET_EXEC
e_flags: 0
e_entry: 0x8050940 e_ehsize: 52 e_shstrndx: 31
e_shoff: 0x17d4 e_shentsize: 40 e_shnum: 32
e_phoff: 0x34 e_phentsize: 32 e_phnum: 6
The smart thing to do is use :
$ elfdump -d hello
Dynamic Section: .dynamic
index tag value
[0] NEEDED 0x165 libc.so.1
[1] NEEDED 0x18b libdl.so.1
[2] INIT 0x8050a94
[3] FINI 0x8050ab0
[4] RUNPATH 0x196 /usr/xpg4/lib
[5] RPATH 0x196 /usr/xpg4/lib
[6] HASH 0x8050118
[7] STRTAB 0x8050450
[8] STRSZ 0x3a4
[9] SYMTAB 0x80502a0
[10] SYMENT 0x10
[11] SUNW_SYMTAB 0x8050200
[12] SUNW_SYMSZ 0x250
[13] SUNW_SORTENT 0x4
[14] SUNW_SYMSORT 0x805085c
[15] SUNW_SYMSORTSZ 0x3c
[16] CHECKSUM 0xaa25
[17] VERNEED 0x80507f4
[18] VERNEEDNUM 0x1
[19] PLTRELSZ 0x30
[20] PLTREL 0x11
[21] JMPREL 0x80508a0
[22] REL 0x8050898
[23] RELSZ 0x38
[24] RELENT 0x8
[25] DEBUG 0
[26] FEATURE_1 0x1 [ PARINIT ]
[27] SUNW_CAP 0x8050108
[28] FLAGS 0 0
[29] FLAGS_1 0 0
[30] SUNW_STRPAD 0x200
[31] SUNW_LDMACH 0x3 EM_386
[32] PLTGOT 0x8060ae0
[33-43] NULL 0
I think SUNW_LDMACH, CHECKSUM and DEBUG are all interesting.
Let's get mcs -p to work for us by using -Qy :
$ mcs -p hello
hello:
%Z%%M% %I% %E% SMI
%Z%%M% %I% %E% SMI
%Z%%M% %I% %E% SMI
%Z%%M% %I% %E% SMI
%Z%%M% %I% %E% SMI
%Z%%M% %I% %E% SMI
%Z%%M% %I% %E% SMI
acomp: Sun C 5.9 SunOS_i386 Patch 124868-07 2008/10/07
as: Sun Compiler Common 12 SunOS_i386 Patch 126498-11 2008/10/06
ld: Software Generation Utilities - Solaris Link Editors: 5.11-1.1652
Well we get a funky list of %foobar% stuff and also we know the assembler,
the linker and the compiler was all Sun Studio 12.
You won't get information like that from GCC.
You can get a recent GCC thus ( as root ):
# pkgadd -d http://blastwave.network.com/csw/pkgutil_`/sbin/uname -p`.pkg
# rm /opt/csw/etc/pkgutil.conf
# /bin/printf "progress=dot:mega\n" > $HOME/.wgetrc
# /opt/csw/bin/pkgutil --catalog
# /opt/csw/bin/pkgutil --install gcc4 bash
Parsing catalog, may take a while...
New packages: CSWcommon-1.4.6,REV=2008.04.28
CSWgcc4corert-4.3.4,REV=2009.09.29 CSWgcc4g++rt-4.3.4,REV=2009.11.23
CSWstl4-12.0,REV=2007.05.03 CSWiconv-1.13.1,REV=2009.07.01
CSWlibgmp-4.3.1,REV=2009.12.04 CSWzlib-1.2.3,REV=2007.05.12
CSWggettext-0.17,REV=2009.05.27 CSWlibmpfr-2.4.2,REV=2009.12.04
CSWbinutils-2.19.1,REV=2009.02.21 CSWgcc4-4.3.4,REV=2009.09.29
Total size: 127.5 MB
11 packages to fetch. Do you want to continue? [Y,n]
Just hit enter and watch. You may get asked a question or two, hit "y" and
enter.
At the end run the mkheaders script that comes with GCC :
# /opt/csw/gcc4/libexec/gcc/i386-pc-solaris2.10/4.3.4/install-tools/mkheaders
What you get is a 64-bit aware GCC 4.3.4 but despite all the manhours that
went into giving you that package it won't hold a candle to Studio 12. It
just won't. Sorry.
Try to compile that same hello.c and then go looking for information about
the tools used.
$ gcc -v -m32 -std=c89 -D_TS_ERRNO -D_POSIX_SOURCE -o hello_i386 hello.c
Using built-in specs.
Target: i386-pc-solaris2.10
Configured with: /export/medusa/dclarke/build/GCC/gcc-4.3.4/configure
--build=i386-pc-solaris2.10 --with-gnu-as --with-as=/opt/csw/bin/gas
--without-gnu-ld --with-ld=/usr/ccs/bin/ld --with-cpu-32=i386
--with-cpu-64=opteron --with-arch-32=i386 --with-arch-64=opteron
--enable-stage1-languages=c --enable-nls --with-libiconv-prefix=/opt/csw
--enable-threads=posix --prefix=/opt/csw/gcc4 --with-local-prefix=/opt/csw
--enable-shared --enable-multilib --with-included-gettext
--with-system-zlib --with-gmp=/opt/csw --with-mpfr=/opt/csw
--enable-languages=c,c++,objc,fortran --enable-bootstrap
Thread model: posix
gcc version 4.3.4 (GCC)
COLLECT_GCC_OPTIONS='-v' '-m32' '-std=c89' '-D_TS_ERRNO' '-D_POSIX_SOURCE'
'-o' 'hello_i386' '-mtune=generic'
/opt/csw/gcc4/libexec/gcc/i386-pc-solaris2.10/4.3.4/cc1 -quiet -v
-D_TS_ERRNO -D_POSIX_SOURCE hello.c -quiet -dumpbase hello.c -m32
-mtune=generic -auxbase hello -std=c89 -version -o /var/tmp//ccnL4gaH.s
ignoring nonexistent directory
"/opt/csw/gcc4/lib/gcc/i386-pc-solaris2.10/4.3.4/../../../../i386-pc-solaris2.10/include"
#include "..." search starts here:
#include <...> search starts here:
/opt/csw/include
/opt/csw/gcc4/include
/opt/csw/gcc4/lib/gcc/i386-pc-solaris2.10/4.3.4/include
/opt/csw/gcc4/lib/gcc/i386-pc-solaris2.10/4.3.4/include-fixed
/usr/include
End of search list.
GNU C (GCC) version 4.3.4 (i386-pc-solaris2.10)
compiled by GNU C version 4.3.4, GMP version 4.3.1, MPFR version
2.4.2-rc1.
warning: MPFR header version 2.4.2-rc1 differs from library version 2.4.2.
GGC heuristics: --param ggc-min-expand=95 --param ggc-min-heapsize=122687
Compiler executable checksum: 9ba7f205b9cefa6eae48c8f511c89b44
COLLECT_GCC_OPTIONS='-v' '-m32' '-std=c89' '-D_TS_ERRNO' '-D_POSIX_SOURCE'
'-o' 'hello_i386' '-mtune=generic'
/opt/csw/bin/gas -v -V -Qy --32 -s -o /var/tmp//cci73xzz.o
/var/tmp//ccnL4gaH.s
GNU assembler version 2.19.1 (i386-pc-solaris2.8) using BFD version (GNU
Binutils) 2.19.1
COMPILER_PATH=/opt/csw/gcc4/libexec/gcc/i386-pc-solaris2.10/4.3.4/:/opt/csw/gcc4/libexec/gcc/i386-pc-solaris2.10/4.3.4/:/opt/csw/gcc4/libexec/gcc/i386-pc-solaris2.10/:/opt/csw/gcc4/lib/gcc/i386-pc-solaris2.10/4.3.4/:/opt/csw/gcc4/lib/gcc/i386-pc-solaris2.10/:/usr/ccs/bin/
LIBRARY_PATH=/opt/csw/gcc4/lib/gcc/i386-pc-solaris2.10/4.3.4/:/opt/csw/gcc4/lib/gcc/i386-pc-solaris2.10/4.3.4/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-m32' '-std=c89' '-D_TS_ERRNO' '-D_POSIX_SOURCE'
'-o' 'hello_i386' '-mtune=generic'
/opt/csw/gcc4/libexec/gcc/i386-pc-solaris2.10/4.3.4/collect2 -V -Y
P,/usr/ccs/lib:/usr/lib -Qy -o hello_i386 /usr/lib/crt1.o /usr/lib/crti.o
/usr/lib/values-Xa.o
/opt/csw/gcc4/lib/gcc/i386-pc-solaris2.10/4.3.4/crtbegin.o
-L/opt/csw/gcc4/lib/gcc/i386-pc-solaris2.10/4.3.4
-L/opt/csw/gcc4/lib/gcc/i386-pc-solaris2.10/4.3.4/../../..
/var/tmp//cci73xzz.o -lgcc -lgcc_eh -lc -lgcc -lgcc_eh
/opt/csw/gcc4/lib/gcc/i386-pc-solaris2.10/4.3.4/crtend.o /usr/lib/crtn.o
ld: Software Generation Utilities - Solaris Link Editors: 5.11-1.1652
$ file hello_i386
hello_i386: ELF 32-bit LSB executable 80386 Version 1 [FPU], dynamically
linked, not stripped, no debugging information available
$ ldd hello_i386
libc.so.1 => /lib/libc.so.1
libm.so.2 => /lib/libm.so.2
$ elfdump -d hello_i386
Dynamic Section: .dynamic
index tag value
[0] NEEDED 0x1e6 libc.so.1
[1] INIT 0x8050ce0
[2] FINI 0x8050d00
[3] HASH 0x8050118
[4] STRTAB 0x80504c8
[5] STRSZ 0x40c
[6] SYMTAB 0x80502d8
[7] SYMENT 0x10
[8] SUNW_SYMTAB 0x8050218
[9] SUNW_SYMSZ 0x2b0
[10] SUNW_SORTENT 0x4
[11] SUNW_SYMSORT 0x8050944
[12] SUNW_SYMSORTSZ 0x3c
[13] CHECKSUM 0x5335
[14] VERNEED 0x80508d4
[15] VERNEEDNUM 0x1
[16] PLTRELSZ 0x40
[17] PLTREL 0x11
[18] JMPREL 0x80509a8
[19] REL 0x8050980
[20] RELSZ 0x68
[21] RELENT 0x8
[22] DEBUG 0
[23] FEATURE_1 0x1 [ PARINIT ]
[24] SUNW_CAP 0x8050108
[25] FLAGS 0 0
[26] FLAGS_1 0 0
[27] SUNW_STRPAD 0x200
[28] SUNW_LDMACH 0x3 EM_386
[29] PLTGOT 0x8060d2c
[30-40] NULL 0
$
$ mcs -p hello_i386
hello_i386:
@(#)SunOS 5.11 snv_111a November 2008
@(#)SunOS 5.11 snv_111a November 2008
@(#)SunOS 5.11 snv_111a November 2008
GCC: (GNU) 4.3.4
GCC: (GNU) 4.3.4
GCC: (GNU) 4.3.4
@(#)SunOS 5.11 snv_111a November 2008
ld: Software Generation Utilities - Solaris Link Editors: 5.11-1.1652
$ uname -a
SunOS aequitas 5.11 snv_111b i86pc i386 i86pc
Also try this just to see the effect :
$ LD_OPTIONS=\-R\$HOME/lib/\ \-L\$HOME/lib
$ export LD_OPTIONS
$ gcc -m32 -std=c89 -D_TS_ERRNO -D_POSIX_SOURCE -o hello_i386 hello.c
$ elfdump hello_i386 | grep PATH
[3] RUNPATH 0x20c $HOME/lib/
[4] RPATH 0x20c $HOME/lib/
I don't want to get into a gcc versus Studio thread but there is a pile of
information available to the user via a few tools. The best is Daryl
Gove's book.
--
Dennis Clarke
[email protected] <- Email related to the open source Solaris
[email protected] <- Email related to open source for Solaris
_______________________________________________
opensolaris-code mailing list
[email protected]
http://mail.opensolaris.org/mailman/listinfo/opensolaris-code