Technical article 100785

Programs compiled under OpenServer 5.0.0 have corrupt argv, argc and envp.

OpenServer 5.0.0

Written · Last revised

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") = ansi
The 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?".

From the Xinuos technical article database, record 100785. Reproduced with permission. The original is at www.sco.com/ta/100785, which is reachable but blocked from search engines. Xinuos marks this article against a retired product line. That refers to the product’s lifecycle, not to whether the advice still works — plenty of it still applies to running systems.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.