summaryrefslogtreecommitdiff
path: root/lib/LD/IdenticalCodeFolding.cpp
blob: a899bb06582fc4e76816759d16a20dd2bfb397de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//===- IndenticalCodeFolding.cpp ------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "mcld/LD/IdenticalCodeFolding.h"

#include "mcld/GeneralOptions.h"
#include "mcld/Module.h"
#include "mcld/Fragment/RegionFragment.h"
#include "mcld/LD/LDContext.h"
#include "mcld/LD/LDSection.h"
#include "mcld/LD/RelocData.h"
#include "mcld/LD/Relocator.h"
#include "mcld/LD/ResolveInfo.h"
#include "mcld/LD/SectionData.h"
#include "mcld/LinkerConfig.h"
#include "mcld/MC/Input.h"
#include "mcld/Support/Demangle.h"
#include "mcld/Support/MsgHandling.h"
#include "mcld/Target/GNULDBackend.h"

#include <llvm/ADT/StringRef.h>
#include <llvm/Support/Casting.h>
#include <llvm/Support/Format.h>

#include <cassert>
#include <map>
#include <set>

#include <zlib.h>

namespace mcld {

static bool isSymCtorOrDtor(const ResolveInfo& pSym) {
  // We can always fold ctors and dtors since accessing function pointer in C++
  // is forbidden.
  llvm::StringRef name(pSym.name(), pSym.nameSize());
  if (!name.startswith("_ZZ") && !name.startswith("_ZN")) {
    return false;
  }
  return isCtorOrDtor(pSym.name(), pSym.nameSize());
}

IdenticalCodeFolding::IdenticalCodeFolding(const LinkerConfig& pConfig,
                                           const TargetLDBackend& pBackend,
                                           Module& pModule)
    : m_Config(pConfig), m_Backend(pBackend), m_Module(pModule) {
}

void IdenticalCodeFolding::foldIdenticalCode() {
  // 1. Find folding candidates.
  FoldingCandidates candidate_list;
  findCandidates(candidate_list);

  // 2. Initialize constant section content
  for (size_t i = 0; i < candidate_list.size(); ++i) {
    candidate_list[i].initConstantContent(m_Backend, m_KeptSections);
  }

  // 3. Find identical code until convergence
  bool converged = false;
  size_t iterations = 0;
  while (!converged && (iterations < m_Config.options().getICFIterations())) {
    converged = matchCandidates(candidate_list);
    ++iterations;
  }
  if (m_Config.options().printICFSections()) {
    debug(diag::debug_icf_iterations) << iterations;
  }

  // 4. Fold the identical code
  typedef std::set<Input*> FoldedObjects;
  FoldedObjects folded_objs;
  KeptSections::iterator kept, keptEnd = m_KeptSections.end();
  size_t index = 0;
  for (kept = m_KeptSections.begin(); kept != keptEnd; ++kept, ++index) {
    LDSection* sect = (*kept).first;
    Input* obj = (*kept).second.first;
    size_t kept_index = (*kept).second.second;
    if (index != kept_index) {
      sect->setKind(LDFileFormat::Folded);
      folded_objs.insert(obj);

      if (m_Config.options().printICFSections()) {
        KeptSections::iterator it = m_KeptSections.begin() + kept_index;
        LDSection* kept_sect = (*it).first;
        Input* kept_obj = (*it).second.first;
        debug(diag::debug_icf_folded_section) << sect->name() << obj->name()
                                              << kept_sect->name()
                                              << kept_obj->name();
      }
    }
  }

  // Adjust the fragment reference of the folded symbols.
  FoldedObjects::iterator fobj, fobjEnd = folded_objs.end();
  for (fobj = folded_objs.begin(); fobj != fobjEnd; ++fobj) {
    LDContext::sym_iterator sym, symEnd = (*fobj)->context()->symTabEnd();
    for (sym = (*fobj)->context()->symTabBegin(); sym != symEnd; ++sym) {
      if ((*sym)->hasFragRef() && ((*sym)->type() == ResolveInfo::Function)) {
        LDSymbol* out_sym = (*sym)->resolveInfo()->outSymbol();
        FragmentRef* frag_ref = out_sym->fragRef();
        LDSection* sect = &(frag_ref->frag()->getParent()->getSection());
        if (sect->kind() == LDFileFormat::Folded) {
          size_t kept_index = m_KeptSections[sect].second;
          LDSection* kept_sect = (*(m_KeptSections.begin() + kept_index)).first;
          frag_ref->assign(kept_sect->getSectionData()->front(),
                           frag_ref->offset());
        }
      }
    }  // for each symbol
  }    // for each folded object
}

void IdenticalCodeFolding::findCandidates(FoldingCandidates& pCandidateList) {
  Module::obj_iterator obj, objEnd = m_Module.obj_end();
  for (obj = m_Module.obj_begin(); obj != objEnd; ++obj) {
    std::set<const LDSection*> funcptr_access_set;
    typedef std::map<LDSection*, LDSection*> CandidateMap;
    CandidateMap candidate_map;
    LDContext::sect_iterator sect, sectEnd = (*obj)->context()->sectEnd();
    for (sect = (*obj)->context()->sectBegin(); sect != sectEnd; ++sect) {
      switch ((*sect)->kind()) {
        case LDFileFormat::TEXT: {
          candidate_map.insert(std::make_pair(*sect, nullptr));
          break;
        }
        case LDFileFormat::Relocation: {
          LDSection* target = (*sect)->getLink();
          if (target->kind() == LDFileFormat::TEXT) {
            candidate_map[target] = *sect;
          }

          // Safe icf
          if (m_Config.options().getICFMode() == GeneralOptions::ICF::Safe) {
            RelocData::iterator rel, relEnd = (*sect)->getRelocData()->end();
            for (rel = (*sect)->getRelocData()->begin(); rel != relEnd; ++rel) {
              LDSymbol* sym = rel->symInfo()->outSymbol();
              if (sym->hasFragRef() && (sym->type() == ResolveInfo::Function)) {
                const LDSection* def =
                    &sym->fragRef()->frag()->getParent()->getSection();
                if (!isSymCtorOrDtor(*rel->symInfo()) &&
                    m_Backend.mayHaveUnsafeFunctionPointerAccess(*target) &&
                    m_Backend.getRelocator()
                        ->mayHaveFunctionPointerAccess(*rel)) {
                  funcptr_access_set.insert(def);
                }
              }
            }  // for each reloc
          }

          break;
        }
        default: {
          // skip
          break;
        }
      }  // end of switch
    }    // for each section

    CandidateMap::iterator candidate, candidateEnd = candidate_map.end();
    for (candidate = candidate_map.begin(); candidate != candidateEnd;
         ++candidate) {
      if ((m_Config.options().getICFMode() == GeneralOptions::ICF::All) ||
          (funcptr_access_set.count(candidate->first) == 0)) {
        size_t index = m_KeptSections.size();
        m_KeptSections[candidate->first] = ObjectAndId(*obj, index);
        pCandidateList.push_back(
            FoldingCandidate(candidate->first, candidate->second, *obj));
      }
    }  // for each possible candidate
  }  // for each obj
}

bool IdenticalCodeFolding::matchCandidates(FoldingCandidates& pCandidateList) {
  typedef std::multimap<uint32_t, size_t> ChecksumMap;
  ChecksumMap checksum_map;
  std::vector<std::string> contents(pCandidateList.size());
  bool converged = true;

  for (size_t index = 0; index < pCandidateList.size(); ++index) {
    contents[index] = pCandidateList[index].getContentWithVariables(
        m_Backend, m_KeptSections);
    uint32_t checksum = ::crc32(0xFFFFFFFF,
                                (const uint8_t*)contents[index].c_str(),
                                contents[index].length());

    size_t count = checksum_map.count(checksum);
    if (count == 0) {
      checksum_map.insert(std::make_pair(checksum, index));
    } else {
      std::pair<ChecksumMap::iterator, ChecksumMap::iterator> ret =
          checksum_map.equal_range(checksum);
      for (ChecksumMap::iterator it = ret.first; it != ret.second; ++it) {
        size_t kept_index = (*it).second;
        if (contents[index].compare(contents[kept_index]) == 0) {
          m_KeptSections[pCandidateList[index].sect].second = kept_index;
          converged = false;
          break;
        }
      }
    }
  }

  return converged;
}

void IdenticalCodeFolding::FoldingCandidate::initConstantContent(
    const TargetLDBackend& pBackend,
    const IdenticalCodeFolding::KeptSections& pKeptSections) {
  // Get the static content from text.
  assert(sect != NULL && sect->hasSectionData());
  SectionData::const_iterator frag, fragEnd = sect->getSectionData()->end();
  for (frag = sect->getSectionData()->begin(); frag != fragEnd; ++frag) {
    switch (frag->getKind()) {
      case Fragment::Region: {
        const RegionFragment& region = llvm::cast<RegionFragment>(*frag);
        content.append(region.getRegion().begin(), region.size());
        break;
      }
      default: {
        // FIXME: Currently we only take care of RegionFragment.
        break;
      }
    }
  }

  // Get the static content from relocs.
  if (reloc_sect != NULL && reloc_sect->hasRelocData()) {
    for (Relocation& rel : *reloc_sect->getRelocData()) {
      llvm::format_object<Relocation::Type,
                          Relocation::Address,
                          Relocation::Address,
                          Relocation::Address> rel_info("%x%llx%llx%llx",
                                                        rel.type(),
                                                        rel.symValue(),
                                                        rel.addend(),
                                                        rel.place());
      char rel_str[48];
      rel_info.print(rel_str, sizeof(rel_str));
      content.append(rel_str);

      // Handle the recursive call.
      LDSymbol* sym = rel.symInfo()->outSymbol();
      if ((sym->type() == ResolveInfo::Function) && sym->hasFragRef()) {
        LDSection* def = &sym->fragRef()->frag()->getParent()->getSection();
        if (def == sect) {
          continue;
        }
      }

      if (!pBackend.isSymbolPreemptible(*rel.symInfo()) && sym->hasFragRef() &&
          (pKeptSections.find(
               &sym->fragRef()->frag()->getParent()->getSection()) !=
           pKeptSections.end())) {
        // Mark this reloc as a variable.
        variable_relocs.push_back(&rel);
      } else {
        // TODO: Support inlining merge sections if possible (target-dependent).
        if ((sym->binding() == ResolveInfo::Local) ||
            (sym->binding() == ResolveInfo::Absolute)) {
          // ABS or Local symbols.
          content.append(sym->name()).append(obj->name()).append(
              obj->path().native());
        } else {
          content.append(sym->name());
        }
      }
    }
  }
}

std::string IdenticalCodeFolding::FoldingCandidate::getContentWithVariables(
    const TargetLDBackend& pBackend,
    const IdenticalCodeFolding::KeptSections& pKeptSections) {
  std::string result(content);
  // Compute the variable content from relocs.
  std::vector<Relocation*>::const_iterator rel, relEnd = variable_relocs.end();
  for (rel = variable_relocs.begin(); rel != relEnd; ++rel) {
    LDSymbol* sym = (*rel)->symInfo()->outSymbol();
    LDSection* def = &sym->fragRef()->frag()->getParent()->getSection();
    // Use the kept section index.
    KeptSections::const_iterator it = pKeptSections.find(def);
    llvm::format_object<size_t> kept_info("%x", (*it).second.second);
    char kept_str[8];
    kept_info.print(kept_str, sizeof(kept_str));
    result.append(kept_str);
  }

  return result;
}

}  // namespace mcld