Dead Code Elimination With the
Solaris Operating Environment Link-editor
by Andrew Walton
(May, 2003)
We want to hear from you! Please send us your
FEEDBACK.
Abstract: This article explains how the Solaris Operating Environment (OE)
link-editor can perform
dead code and dead variable elimination when creating executables and shared
objects.
The Solaris OE link-editor, ld(1), takes one or more input files and combines
them to produce a single output file. The input files can be a combination
of relocatable object files, shared objects and archive libraries. The
output file can be a relocatable object, an executable program, or a shared
object.
The input and the output files are in executable and linking format
(ELF). ELF files are divided into a number of sections. For a description
of these sections see the Solaris
Linker and Libraries Guide. The link-editor uses two categories of
ELF sections:
-
Sections that contain program data, such as the program's instructions,
held in the
.text section, and associated data in the .data and
.bss sections.
-
Sections that contain link-editing information, such as the symbol
table
.symtab, and relocation information in the
.rela.text section.
The link-editor concatenates the program data sections of the same
type into a single section in the output file. The link-editing
sections are interpreted, and direct the link-editor to modify other sections
or create new sections.
This process of concatenation may result in an output that includes
one or more unused functions (dead code) and is larger than necessary.
Enhancements made to the link-editor in the Solaris 9 OE, 12/02 release, allow it to
perform dead code elimination by excluding program data sections
which don't contribute to the object being built. These enhancements are
included in the linker patches, making them available to earlier releases
of the Solaris OE.
A program data section can be excluded if it meets the following
criteria:
-
No other sections have any bindings (reallocations) against it.
-
It does not define any global symbols which might be needed by other dynamic
dependencies.
Unused section elimination in the link-editor is enabled with the -z
ignore option. The -z ignore applies only to the input files
that follow it, and can be cancelled with -z record.
By default, the link-editor makes all symbols global (externally visible).
In order for a symbol to be excluded its scope must be reduced to local.
This can be done using a mapfile which defines the global symbols and reduces
the scope of all other symbols to local. (See the Solaris
Linkers and Libraries Guide).
In order to eliminate individual functions, we need to direct the compiler
to place each function in its own unique text section. The -xF option,
normally used prior to using the performance analyser, will do this. This
generates sections called .text%function-name for each function.
When used with the Forte C++ 6 and Sun ONE Studio 7 C++ compilers, exception handling
must be disabled with the -noex option.
In the Sun ONE Studio 8 Compiler Collection, the C compiler can place
global data (variables with external linkage) into separate data sections,
enabling the link-editor to perform some dead variable elimination. To
direct the compiler to create separate sections for functions and global
data, use -xF=func,gbldata (or -xF=%all), -xF is
equivalent to -xF=func.
In addition to placing functions and global variables in separate sections,
the C++ compiler can also generate separate sections for local data (variables
with internal linkage) when invoked with -xF=func,gbldata,lcldata
(or -xF=%all). The restriction requiring exception handling
to be disabled when using -xF has been lifted. Using -xF=lcldata
prevents the compiler from performing some address calculation optimizations.
Minimum Solaris Linker Patches Required for Dead Code Elimination
| Solaris OE Release |
Minimum Linker Patch |
| Solaris 9 OE |
112963-03 |
| Solaris 8 OE |
109147-17 |
| Solaris 7 OE |
106950-19 |
|