Problem
Porting an application from an earlier release of the SCO Development System, or from another development or operating system environment, appears to have introduced problems into a program.
The program may dump core, or perform incorrectly, when accessing the program's arguments (using argc and argv in main()), or when manipulating the environment (using getenv(S) or setenv(S)). The count of arguments to the program (argc) may be incorrect,
The program has been linked using the linker, ld(CP), directly.
Further investigation shows that the program's argc, argv and envp are corrupt or undefined upon entry into main().
For example, given:#include <stdio.h>main(int argc, char * argv[], char ** envp) { fprintf(stderr, "argc %d argv[0] -> \"%s\"\n", argc, argv[0] ); fprintf(stderr, "getenv(\"TERM\") = %s\n", getenv("TERM") ); }
If compiled to a binary as shown and executed, gives:
# cc -c foo.c
# ld -o foo foo.o -lc
# ./foo
Memory fault(coredump)rather than the expected:# ./foo
argc 1 argv[0] -> "./foo"
getenv("TERM") = ansi Cause
The SCO OpenServer Development System 5.0.0 linker, ld(CP), requires
different default arguments from previous releases to correctly
link the C runtime environment. The C runtime environment ensures
that argc, argv and envp are defined on entry to main().The C compiler, cc(CP), invokes the linker, ld(CP), with the appropriate arguments automatically.
Solution
To determine if the argc, argv and envp variables are undefined because the C runtime has not been correctly linked into the binary, use the C compiler, cc(CP), rather than the linker, ld(CP) to link the binary:
# cc -c foo.c
# cc -o foo foo.o -lc
# ./foo
argc 1 argv[0] -> "./foo"
getenv("TERM") = ansiThe safest solution is to use the C compiler, cc(CP), to link
programs and pass the default required flags, objects and libraries
to the linker, ld(CP). Options for specific phases or programs
in the compilation can be passed through from cc(CP). See the
cc(CP) manual page for further detail.If this is not possible, for example, if the code and compilation instructions are generated using 4GL or other automated tools, use the -i flag to cc(CP) to determine the flags, objects and libraries which should be defined for the linker, ld(CP):
# cc -i -o foo foo.o
cc: 'ld' '-Ra,XPG4PLUS,COFF' '-Y'
'P,/usr/ccs/lib:/lib:/usr/lib' '/usr/ccs/lib/crt1.o'
'/usr/ccs/lib/values-Xa.o' '-o' 'foo' 'foo.o' '-lcrypt'
'-lgen' '-lc' '/usr/ccs/lib/crtn.o' See also
cc(CP), ld(CP),environ(S), getenv(S), setenv(S)TA103851, "How can I tell what program produced a core dump?".