Solaris 1.x to 2.x Transition Guide
この本のみを検索
PDF 文書ファイルをダウンロードする

Compilers, Linkers, and Debuggers

15

This chapter discusses the changes to compilers, linkers, and debuggers. It contains the following sections:
Compilerspage 139
Linkerspage 146
Debuggerspage 154

Compilers

The single most significant change for developers migrating from SunOS release 4.x to the Solaris 2.5 operating environment is the unbundling of the C compiler. One of the factors that allowed the compiler to be unbundled is the dynamic kernel. The compiler is no longer required to reconfigure the kernel as devices are now automatically loaded by the kernel as needed.
An ANSI C compiler is available with the unbundled SPARCworks(TM) product. This compiler produces executables in executable and linking format (ELF), the native object format of Solaris 2.5 executables. lint and the lint libraries are also unbundled.
Source Compatibility Guide describes the differences between the C language as implemented by the SunOS release 4.x C compiler (Sun C) and as described by the ANSI Programming Language C document (ANSI C). These differences should be addressed when porting source written for the Sun C compiler to an ANSI C conforming compiler.

Compiler Option Differences

Table 15-1 compares the SunOS release 4.x Sun C and ANSI C compilers to the Solaris 2.5 SPARCompiler C++ 3.0.1. It is provided for your information, and is not intended to describe the range of operations available in default Solaris 2.5 software.

Note - Table 15-1 presents information based on the publicly available copies of the American National Standard for Information Systems - Programming Language XX3.159-1989, approved December 14, 1989.

Please note the following:
  • A "yes" in any column indicates that the option is supported by that compiler.
  • If the option has changed, the new option is listed.
  • A "no" in any column if the option is not supported by that driver
  • A "+" in the SPARCompiler C++ 3.0.1 Solaris 2.5 column indicates an option required by SVID specifications.
Table 15-1
Option or FlagSun C (SunOS release 4.x)ANSI C (SunOS release 4.x)SPARCompiler C++ 3.0.1 (Solaris 2.5)Description
-A symbolNoYesYescpp predicate assertion
-aYesYes-xaCounts # basic block executions
-assertxYesYes-zSpecifies link time assertion
-alignYesYesNoPage aligns (ld)
-BXYesYes-d[y|n]Specifies binding type (only for libraries in SunOS release 5.5)
-bnzeroYesYesNoGenerates nonzero AR
-CYesYes+Yescpp comments left in
-cYesYes+YesProduces .o file
-cg87YesYesNoSets fp option to -cg87
-cg89YesYesNoSets fp option to -cg89
Table 15-1 (Continued)
Option or FlagSun C (SunOS release 4.x)ANSI C (SunOS release 4.x)SPARCompiler C++ 3.0.1 (Solaris 2.5)Description
-dryrunYesYes-#Shows commands constructed by driver
-DxYesYes+YesDefines cpp symbol x
-dYesYesNoForces definition of common (ld)
-dalignYesYesYesAssumes doubles are doubleword aligned
-dlYesYesNoGenerates long data segment refs
-d[y|n]-BX-BXYesDynamics linking [yes|no]
-EYesYes+YesRuns source through cpp
-eNoNoYesEntry point for ld
-F-O*-O*+NoOptimization directives
-fNoNoNoFloating-point support
-fastYesYesYesOptions for best performance
-fsingleYesNoNoFloats are single precision
-fsingle2YesNoNoPasses float as float not double
-fnonstdYesYesYesNon-standard float option
-fstoreYesNoNoForces writes on store
-GNoNoYesCreates shared library, not available with the -dn option
-gYesYes+YesGenerates info for dbx
-goYesNoNoGenerates info for adb
-HYesYesYesPrints paths of included files
-h nameNoNoYesUses name as internal identifier; soname passed to linker
-helpYesYes-flagsLists options
-IxYesYes+YesAdds x to include path
-Jsun3NoNoGenerates long offset for switch|case
Table 15-1 (Continued)
Option or FlagSun C (SunOS release 4.x)ANSI C (SunOS release 4.x)SPARCompiler C++ 3.0.1 (Solaris 2.5)Description
-KPIC-PIC-PICYesPosition independent code
-Kpic-pic-picYesPIC with short offsets
-KminabiNoNoYesABI compliant code
-libmilYesYes-xlibmilPasses libm.il as part of -fast
-lxYesYesYesReads object library (for ld)
-LxYesYesYesAdds x to ld library path
-MYesYesNoCollects dependencies
-M mapfileYesNoYesPasses mapfile to the linker
-misalignYesYesYesHandles misaligned Sun-4 data
-NYesNoNoDoes not make shared
-nYesNoNoMakes shared
-nativeYesYesNoUses appropriate -cg option
-nolibmilYesYes-xnolibmilDoesn't pass libm.il with -fast
-noc2YesYesNoDoesn't do peephole optimization
-o fileYesYes+YesSets name of output file
-O[1,2,3,4]YesYes-x0[1,2,3,4]Generates optimized code
-OYesYes+YesGenerates optimized code
-PYesYes+YesRuns source through cpp, output to .i
-PICYesNo-KPICGenerates pic code with long offset
-pYesYes+YesCollects data for prof
-picYesYes-Kpicpic code with short offset
-pipeYesNoNoUses pipes instead of temp files
-pgYesYes-xpgCollects data for gprof
-purecrossYesNoNoDoesn't have slash in VROOT
-Qdir xYesYes-Y*Looks for compiler passes in x
Table 15-1 (Continued)
Option or FlagSun C (SunOS release 4.x)ANSI C (SunOS release 4.x)SPARCompiler C++ 3.0.1 (Solaris 2.5)Description
-Qpath xYesYes-Y*Same as -Qdir
-QnNoNoYesDoesn't add version stamp info
-QyNoNoYesAdds version stamp info
-qdir xYesYes-Y*Looks for compiler passes in x
-qp-p-p+YesCollects data for prof
-qlNoNo+YesCollects data for lprof
-qpath xYesYes-Y*Same as -Qdir
-Qoption cpp xYesYesNoPasses option x on to program cpp
-Qoption iropt xYesYesNoPasses option x on to program iropt
-Qoption cg xYesYesNoPasses option x on to program cg
-Qoption inline xYesYesNoPasses option x on to program inline
-Qoption as xYesYesNoPasses option x on to program as
-Qoption asS xYesYesNoPasses option x on to program asS
-Qoption ld xYesYesNoPasses option x on to program ld
-qoption cpp xYesYesNoPasses option x on to program cpp
-qoption ccom xYesYesNoPasses option x on to program ccom
-qoption lint1 xYesYesNoPasses option x on to program lint1
-qoption iropt xYesYesNoPasses option x on to program iropt
-qoption cg xYesYesNoPasses option x on to program cg
-qoption inline xYesYesNoPasses option x on to program inline
-qoption cat xYesYesNoPasses option x on to program cat
-qoption c2 xYesYesNoPasses option x on to program c2
-qoption as xYesYesNoPasses option x on to program as
Table 15-1 (Continued)
Option or FlagSun C (SunOS release 4.x)ANSI C (SunOS release 4.x)SPARCompiler C++ 3.0.1 (Solaris 2.5)Description
-qoption asS xYesYesNoPasses option x on to program asS
-qoption ld xYesYesNoPasses option x on to program ld
-Qproduce .oYesYesNoProduces type .o file (Object file)
-Qproduce .sYesYesNoProduces type .s file (Assembler source)
-Qproduce .cYesYesNoProduces type .c file (C source)
-Qproduce .iYesYesNoProduces type .i file (C source after cpp)
-qproduce .oYesYesNoProduces type .o file (Object file)
-qproduce .sYesYesNoProduces type .s file (Assembler source)
-qproduce .cYesYesNoProduces type .c file (C source)
-qproduce .iYesYesNoProduces type .i file (C source after cpp)
-rYesYesYesMakes relocatable; pass to linker
-RYesYesNoMerges data into text segment
-RNoNoYesSpecifies search directories for the run-time linker
-SYesYes+YesProduces .s file only
-sYesYesYesStrips (4.1); pass to linker
-sbYesYes-xsbCollects information for code browser
-strconstNoYes-xstrconstPlaces string literals in read-only text segment
-sun2YesNoNoGenerates code for a Sun-2 system
-sun3xYesNoNoGenerates code for a Sun-3ax system
-sun386YesNoNoGenerates code for a Sun386i
-sun3YesNoNoGenerates code for a Sun-3 system
Table 15-1 (Continued)
Option or FlagSun C (SunOS release 4.x)ANSI C (SunOS release 4.x)SPARCompiler C++ 3.0.1 (Solaris 2.5)Description
-sun4cYesNoNoGenerates code for a Sun-4c system
-sun4YesNoNoGenerates code for a Sun-4 system
-target xYesNoNoSets target architecture to x
-temp=dirYesYesNoSet directory for temps to dir
-timeYesYesNoReports the execution times
-uYesYesYesEnters symbol arg as undef (ld)
-UxYesYes+YesUndefines cpp symbol x
-vYesYes-#Verbose mode
-vNoNoYesStrict semantic checking
-VYesYes+YesReports versions of programs
-WNoNo+YesArguments to other components
-wYesYesYesDoes not print warnings
-X[t,a,c,s]NoYesYesCompatibility options
-YNoNo+YesChanges pathname to components
-yxYesNoNoTraces symbol
-z-assert-assertYesTurns on asserts in linker
-#-v-vYesVerbose mode
EOFNoNoYesFile argument
The C compiler accepts the types of filename arguments shown in Table 15-2.
Table 15-2
SuffixFile Type
.aObject library
.ilIn-line expansion file
.oObject file
.soShared object
Table 15-2 (Continued)
SuffixFile Type
.sAssembler source
.SAssembler source for cpp
.cC source
.iC source after cpp
"file.X=.Y" will read the file "file.X" but treat it as if it had suffix "Y"

Linkers

There are several changes to the link editor, ld(1), in this release. The most important change is its ability to handle the new ELF native file format.

Note - The recommended method for building libraries and executables is through the compiler driver rather than by invoking the linker directly. The compiler automatically supplies several files needed by the linker.

Link Editor Option Differences

Some options have been renamed in the new linker, some have remained the same, and others are no longer needed. Table 15-3 compares the SunOS release 4.x ld command to the Solaris 2.5 ld command.
The sections following Table 15-3 explain how certain linking tasks are affected by the option differences.
Table 15-3 ld
SunOS release 4.x OptionSolaris 2.5 ReplacementNotes
-align datum-M mapfileUses mapfiles and distinct sections
-assert definitionsdefault
-assert nodefinitions-znodefsIssues a fatal error instead of a warning
-assert nosymbolic-zdefsIssues a fatal error instead of a warning
-assert pure-text-ztextIssues a fatal error instead of a warning
Table 15-3 ld(Continued)
SunOS release 4.x OptionSolaris 2.5 ReplacementNotes
-A nameNo replacementdlopen(3X) and dlclose(3X) can approximate this behavior
-Bdynamic-BdynamicApplies only to the inclusion of shared libraries; use -dy (the default) to build dynamically linked executables. See "Building Executables" on page 148.
-Bnosymbolic-zdefs
-Bstatic-dn & -BstaticUsed with the -dn option to completely eliminate the dynamic linker, the -dn option must be specified; use -Bstatic in dynamic mode to include archive libraries. (Used as a toggle. See "Building Executables" on page 148.)
-Bsymbolic-BsymbolicAlso gets -assert nosymbolic with this option
-d -dc -dpDefaultUse -b option in SVR4 to turn off
-D hex-M mapfilemapfiles contain different mechanisms to accomplish desired effect
-e entry-e entry
no -e-GCreates a shared object
-lx[.v]-lxOnly major number versioning of shared libraries is currently supported
-Ldir-Ldirdir not recorded in executable; use -R option instead.
-M-m
-nDefaultSVR4 executable format compresses disk image as -n
-NNo replacement
-o name-o name
-pDefaultCan override with -M mapfile
-r-r
Table 15-3 ld(Continued)
SunOS release 4.x OptionSolaris 2.5 ReplacementNotes
-SNo replacement
-s-s
-tno replacement
-T hex-M mapfilemapfiles contain different mechanisms to accomplish desired effect
-Tdata hex-M mapfilemapfiles contain different mechanisms to accomplish desired effect
-u name-u name
-xNo replacement
-XNo replacement
-ysymNo replacement
-zdefaultSVR4 executable format demand pages as -z

Building Shared Libraries

The procedure for building shared libraries has changed in the Solaris 2.5 operating environment. The -G option is now required to build a shared library. In SunOS release 4.x, the linker would infer that a shared library was being built by the absense of the -e option. As shared libraries may have entry points, this option can no longer be used.

Building Executables

The -Bdynamic and -Bstatic options are still available, but their behavior is different. These options now refer to library inclusions; not to the executable binding. Executable binding is set exclusively with the new -dy and -dn options in the Solaris 2.5 software. The
-dy option is the default. It is
required to create a dynamically linked executable. The -dn option is required to create a statically linked executable.
The -Bdynamic and -Bstatic options apply only when using the -dy option. -Bdynamic tells the link editor to include shared libraries, while -Bstatic tells it to include archive libraries. These options act as a toggle governing subsequent -l arguments until the next -Bdynamic or -Bstatic option is encountered.
The following examples show SunOS release 4.x and Solaris 2.5 commands that can be used to create similar executables.

  sunos4.1% ld -Bstatic test.o -lx                        # uses libx.a and creates a static executable  
  sunos5.x% cc -dn test.o -lx                             # uses libx.a and creates a static executable  
  sunos4.1% ld -Bdynamic test.o -lx                       # uses libx.so and creates a dynamic  
                                                          # executable  
  sunos5.x% cc test.o -lx                                 # uses libx.so and creates a dynamic  
                                                          # executable  
  sunos4.1% ld -Bdynamic test.o -Bstatic -lx              # uses libx.a and creates a dynamic  
                                                          # executable  
  sunos5.x% cc test.o -Bstatic -lx                        # uses libx.a and creates a dynamic  
                                                          # executable  

Specifying Library Search Paths

In SunOS release 4.x, directories specified with the -L option were searched at link time and the information retained for use at execution time. This behavior is now divided between the -L and -R options. The -L option specifies the directories to search at link time; the new -R option tells the linker the search paths to be retained for use at run time. See "Search Path Rules," in the next section for more information.
As with the -Bdynamic and -Bstatic options, the position of the -L option has significance; it applies only to the subsequent -l options.

Search Path Rules

The dynamic linker and the run time linker determine their search paths through a different algorithm from that used by the SunOS release 4.x linker.
Table 15-4 through Table 15-6 compare the search paths for the dynamic linker and the run time linker for SunOS release 4.x and the Solaris 2.5 operating environment. Notice that in the latter, the search path for the link editor and the runtime linker are affected by the LD_LIBRARY_PATH setting.
Table 15-4

Library Search Path
Link Editor-L, LD_LIBRARY_PATH, /usr/lib, /usr/local/lib
Runtime LinkerLD_LIBRARY_PATH, -L, /usr/lib, /usr/local/lib
Table 15-5 LD_LIBRARY_PATHdirlist1

Library Search Path
LD_LIBRARY_PATH=dirlist1
Link Editor-L, dirlist1, /usr/ccs/lib, /usr/lib
Runtime Linkerdirlist1, -R, /usr/lib
Table 15-6 LD_LIBRARY_PATHdirlist1dirlist2

Library Search Path
LD_LIBRARY_PATH=dirlist1; dirlist2
Link Editordirlist1,-L, dirlist2, /usr/ccs/lib, /usr/lib
Runtime Linkerdirlist1, dirlist2, -R, /usr/lib

Version Numbering

SunOS release 4.x supported both major and minor version numbers on shared libraries. The Solaris 2.5 operating environment supports only the major version number. For binary compatibility support, major and minor version
numbers are recognized on SunOS release 4.x shared libraries. These libraries are required to retain the same major and minor version number they had in SunOS release 4.x.
Table 15-7 shows versions of SunOS release 4.x and Solaris 2.5 shared libraries.
Table 15-7
SunOS release 4.xSolaris 2.5
libc.so.1.7libc.so.1
libdl.so.1.0libdl.so.1
In SunOS release 4.x system software, when the -l option was specified, the build environment linker searched for a library with both major and minor numbers. For example, if -ldl was specified, the library, libdl.so.1.0 was linked. In Solaris 2.5, even though major numbers are still supported, the default behavior of the link editor is to ignore version numbers. Using the previous example, the build environment link editor now searches for libdl.so and a symbolic link points to a specific version file.
The recording of a dependency in a dynamic executable or shared object is, by default, the filename of the associated shared object as it is referenced by the link-editor. To provide a more consistent means of specifying dependencies, shared objects can record within themselves the filename by which they should be referenced at runtime.This is specified with the -h option when linking the library file.
Symbolic links have been created for most libraries in this release. Build any new shared libraries with major numbers, then create a symbolic link to the version of the library that is used most often. .

Examples

A new utility, dump(1) (See "Backing Up and Restoring Files" on page 101), makes it easier to debug object files or to check the static and dynamic linking. The dump -L option displays the information needed by the runtime linker that is contained in the executable. This information is contained in the dynamic section of an ELF file. The RPATH entry displays search paths specified by the -R option to ld.
The following example:
  • Builds a shared library, libx.so.1, using libx.o
  • Creates a link from libx.so.1 to libx.so
  • Shows dump output, including the SONAME field, which stores the information passed with the -h option.

  examples% cc -G -o libx.so.1 -h libx.so.1 libx.o  
  
  examples% cp libx.so.1 /mylibs  
  
  examples% ln -s /mylibs/libx.so.1 /mylibs/libx.so  
  
  examples% dump -Lv libx.so.1  
  
  libx.so.1:  
  
    **** DYNAMIC SECTION INFORMATION ****  
  .dynamic :  
  [INDEX] Tag      Value  
  [1]     INIT     0x3b8  
  [2]     FINI     0x3f4  
  [3]     SONAME   libx.so.1  
  [4]     HASH     0x94  
  [5]     STRTAB   0x33c  
  [6]     SYMTAB   0x14c  
  [7]     STRSZ    0x62  
  [8]     SYMENT   0x10  
  [9]     PLTGOT   0x10404  
  [10]    PLTSZ    0xc  
  [11]    PLTREL   0x7  
  [12]    JMPREL   0x3ac  
  [13]    RELA     0x3a0  
  [14]    RELASZ   0x18  
  [15]    RELAENT  0xc  

If a library needs other dynamic libraries, they should be specified along with an RPATH, as the next example shows.
The next example compiles prog.c, dynamically linking libx.so (as built in the previous example), and specifies that the binary retain the current directory information for execution. This example shows the output of dump from the
compiled program, prog.c. Here, the information stored in the SONAME field of the previous example is shown as NEEDED by prog. When prog is run, it will use libx.so.1, even if libx.so is linked to a different version.

  examples% cc -o prog prog.c -L/mylibs -R/mylibs -lx  
  
  example% dump -Lv prog  
  
  prog:  
  
    **** DYNAMIC SECTION INFORMATION ****  
  .dynamic :  
  [INDEX]   Tag   Value  
  [1]  NEEDED   libx.so.1  
  [2]  NEEDED   libc.so.1  
  [3]  INIT     0x1b1ac  
  [4]  FINI     0x1b248  
  [5]  RPATH    /mylibs  
  [6]  HASH     0x100e8  
  [7]  STRTAB   0x17f90  
  [8]  SYMTAB   0x12be0  
  [9]  STRSZ    0x31e1  
  [10] SYMENT   0x10  
  [11] DEBUG    0x0  
  [12] PLTGOT   0x2b25c  
  [13] PLTSZ    0x30  
  [14] PLTREL   0x7  
  [15] JMPREL   0x1b180  
  [16] RELA     0x1b174  
  [17] RELASZ   0x3c  
  [18] RELAENT  0xc  

Debuggers

This section describes changes to debugging tools.

dbx and dbxtool

These tools are no longer available with default system software. Enhanced versions of these tools are available as the SPARCworks Debugger, an unbundled product.

adb and kadb

These tools are available in the Solaris 2.5 operating environment. They offer the same capabilities as the tools in SunOS release 4.x. kadb has been enhanced to recognize multiple processors. The processor ID is displayed in the kadb prompt. In the following examples, it is 0.
To make kernel debugging under the Solaris 2.5 operating environment easier:
  • Enable savecore (uncomment the savecore lines in the /etc/init.d/sysetup file)
  • Boot under kadb (type $c when the system crashes)
  • Use adb and crash

kadb Macros

The following kadb macros are particularly useful with the new multithreaded kernel:
thread Displays the current thread. The current thread pointer is in SPARC global register g7.

  kadb[0]: <g7$<thread  

threadlist Shows the stack traces of all the kernel threads in the system. This can be a long list.

  kadb[0]: $<threadlist  

mutex
Shows you the address of the owning thread, given a mutex. This example
uses the global unsafe driver mutex.


  kadb[0]: unsafe_driver$<mutex  

moddebug


  kadb[0]: moddebug/W 0x80000000  

Allows you to watch module loading. See the end of <sys/modctl.h> for legal values for moddebug for debugging purposes only.

Debugging a Live Kernel

Use the following command to debug a live kernel.

  # adb -k /dev/ksyms /dev/mem  

/dev/ksyms is a pseudo device that contains the complete name list of the running kernel.

truss Command

truss is a new utility, provided to trace system calls performed, signals received, and machine faults incurred. truss offers several significant improvements over the SunOS release 4.x trace(1) command: including the ability to follow forked processes and the ability to deal with multithreaded processes.
The following example shows a summary of traced calls for the date command. With the -c option, truss does not display the trace line-by-line. Instead, it counts the system calls, signals, and faults, and displays a summary.

  example% truss -c date  
  Fri Sep 18 14:31:30 PDT 1992  
  syscall      seconds   calls  errors  
  _exit            .00       1  
  read             .00       7  
  write            .00       1  
  open             .03      12  
  close            .00      12  
  time             .00       1  
  brk              .01       4  
  lseek            .00       1  
  fstat            .00       4  
  ioctl            .00       1  
  execve           .00       1  
  mmap             .01      17  
  munmap           .00       8  
                  ----     ---    ---  
  sys totals:      .05      70      0  
  usr time:        .03  
  elapsed:         .28  

See the truss(1) man page in man Pages(1): User Commands for complete details on all truss options.. In Solaris 2.5 there are a number of other new debugging tools which are based on proc(4) such as pmap(1).