{'fraction_non_alphanumeric': 0.019068458893404187, 'fraction_numerical': 0.005939356048765239, 'mean_word_length': 4.663716814159292, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Safe ICF: Pointer Safe and Unwinding aware Identical Code Folding in the Gold Linker

We have found that large C++ applications and shared libraries tend to have many functions whose code is identical with another function. As much as 10% of the code could theoretically be eliminated by merging such identical functions into a single copy. This optimization, Identical Code Folding (ICF), has been implemented in the gold [4] linker. At link time, ICF detects functions with identical object code and merges them into a single copy. ICF can be unsafe, however, as it can change the run-time behaviour of code that relies on each function having a unique address. To address this, ICF can be used in a safe mode where it identifies and folds functions whose addresses are guaranteed not to have been used in comparison operations. Further, profiling and debugging binaries with merged functions can be confusing, as the PC values of merged functions cannot be always disambiguated to point to the correct function. To address this, we propose a new call table format for the DWARF debugging information to allow tools like the debugger and profiler to disambiguate PC values of merged functions correctly by examining the call chain. Detailed experiments on the x86 platform show that ICF can reduce the text size of a selection of Google binaries, whose average text size is 64 MB, by about 6%. Also, the code size savings of ICF with the safe option is almost as good as the code savings obtained without the safe option. Further, experiments also show that the run-time performance of the optimized binaries on the x86 platform does not change. 1 Motivation We have found that identical code is particularly common in C++ programs with heavy use of templates. Figure 1 shows an example. Here, the template class Foo is instantiated with different types and each of them gets a separate copy of the getElement function. However, since the size of the different pointer types is the same, the bodies of the different getElement functions are identical and can be merged into one instance. Using ICF on this example results in these functions being merged into one function and the output of nm shows the various getElement functions mapped to the same address. The rest of the paper is organized as follows. Section 2 discusses the Identical Code Folding algorithm. Section 3 talks about the extensions to ICF to make it safe for pointer comparisons. Section 4 is about debugging binaries with folded functions and Section 5 contains the experimental data showing the effectiveness of ICF in reducing the code size of binaries. Section 6 talks about merging identical data members and Section 7 concludes the paper. 2 The Identical Code Folding Algorithm In this section, we discuss how ICF detects identical functions. In order for the linker to perform ICF, the compiler must place each function in a separate section, which can be done in the GCC compiler with the flag -ffunction-sections. Now, let us define identical functions. From the point of view of the linker, a function has a text section and relocations that are applied to the text section. Two function sections are identical if and