Linker and Libraries Guide

Collapse Multiply-Defined Data

Data can be reduced by collapsing multiply-defined data. A program with multiple occurrences of the same error messages can be better off by defining one global datum, and have all other instances reference this. For example:


const char * Errmsg = "prog: error encountered: %d";

foo()
{
    ......
    (void) fprintf(stderr, Errmsg, error);
    ......

The main candidates for this sort of data reduction are strings. String usage in a shared object can be investigated using strings(1). For example:


$ strings -10 libfoo.so.1 | sort | uniq -c | sort -rn 

will generate a sorted list of the data strings within the file libfoo.so.1. Each entry in the list is prefixed with the number of occurrences of the string.