GarbageCollection.h revision f33f6de54db174aa679a4b6d1e040d37e95541c0
1//===- GarbageCollection.h ------------------------------------------------===//
2//
3//                     The MCLinker Project
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#ifndef MCLD_LD_GARBAGECOLLECTION_H
10#define MCLD_LD_GARBAGECOLLECTION_H
11#ifdef ENABLE_UNITTEST
12#include <gtest.h>
13#endif
14
15#include <map>
16#include <set>
17#include <vector>
18
19namespace mcld {
20
21class LDSection;
22class LinkerConfig;
23class Module;
24class TargetLDBackend;
25
26/** \class GarbageCollection
27 *  \brief Implementation of garbage collection for --gc-section.
28 *  @ref GNU gold, gc.
29 */
30class GarbageCollection
31{
32public:
33  typedef std::set<const LDSection*> SectionListTy;
34  typedef std::vector<const LDSection*> SectionVecTy;
35
36  /** \class SectionReachedListMap
37   *  \brief Map the section to the list of sections which it can reach directly
38   */
39  class SectionReachedListMap
40  {
41  public:
42    SectionReachedListMap() {}
43
44    /// addReference - add a reference from pFrom to pTo
45    void addReference(const LDSection& pFrom, const LDSection& pTo);
46
47    /// getReachedList - get the list of sections which can be reached by
48    /// pSection, create one if the list has not existed
49    SectionListTy& getReachedList(const LDSection& pSection);
50
51    /// findReachedList - find the list of sections which can be reached by
52    /// pSection, return NULL if the list not exists
53    SectionListTy* findReachedList(const LDSection& pSection);
54
55  private:
56    typedef std::map<const LDSection*, SectionListTy> ReachedSectionsTy;
57
58  private:
59    /// m_ReachedSections - map a section to the reachable sections list
60    ReachedSectionsTy m_ReachedSections;
61  };
62
63public:
64  GarbageCollection(const LinkerConfig& pConfig,
65                    const TargetLDBackend& pBackend,
66                    Module& pModule);
67  ~GarbageCollection();
68
69  /// run - do garbage collection
70  bool run();
71
72private:
73  void setUpReachedSections();
74  void findReferencedSections(SectionVecTy& pEntry);
75  void getEntrySections(SectionVecTy& pEntry);
76  void stripSections();
77
78private:
79  /// m_SectionReachedListMap - map the section to the list of sections which it
80  /// can reach directly
81  SectionReachedListMap m_SectionReachedListMap;
82
83  /// m_ReferencedSections - a list of sections which can be reached from entry
84  SectionListTy m_ReferencedSections;
85
86  const LinkerConfig& m_Config;
87  const TargetLDBackend& m_Backend;
88  Module& m_Module;
89};
90
91} // namespace of mcld
92
93#endif
94
95