GarbageCollection.h revision 37b74a387bb3993387029859c2d9d051c41c724e
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
12#include <map>
13#include <set>
14#include <vector>
15
16namespace mcld {
17
18class LDSection;
19class LinkerConfig;
20class Module;
21class TargetLDBackend;
22
23/** \class GarbageCollection
24 *  \brief Implementation of garbage collection for --gc-section.
25 */
26class GarbageCollection {
27 public:
28  typedef std::set<const LDSection*> SectionListTy;
29  typedef std::vector<const LDSection*> SectionVecTy;
30
31  /** \class SectionReachedListMap
32   *  \brief Map the section to the list of sections which it can reach directly
33   */
34  class SectionReachedListMap {
35   public:
36    SectionReachedListMap() {}
37
38    /// addReference - add a reference from pFrom to pTo
39    void addReference(const LDSection& pFrom, const LDSection& pTo);
40
41    /// getReachedList - get the list of sections which can be reached by
42    /// pSection, create one if the list has not existed
43    SectionListTy& getReachedList(const LDSection& pSection);
44
45    /// findReachedList - find the list of sections which can be reached by
46    /// pSection, return NULL if the list not exists
47    SectionListTy* findReachedList(const LDSection& pSection);
48
49   private:
50    typedef std::map<const LDSection*, SectionListTy> ReachedSectionsTy;
51
52   private:
53    /// m_ReachedSections - map a section to the reachable sections list
54    ReachedSectionsTy m_ReachedSections;
55  };
56
57 public:
58  GarbageCollection(const LinkerConfig& pConfig,
59                    const TargetLDBackend& pBackend,
60                    Module& pModule);
61  ~GarbageCollection();
62
63  /// run - do garbage collection
64  bool run();
65
66 private:
67  void setUpReachedSections();
68  void findReferencedSections(SectionVecTy& pEntry);
69  void getEntrySections(SectionVecTy& pEntry);
70  void stripSections();
71
72 private:
73  /// m_SectionReachedListMap - map the section to the list of sections which it
74  /// can reach directly
75  SectionReachedListMap m_SectionReachedListMap;
76
77  /// m_ReferencedSections - a list of sections which can be reached from entry
78  SectionListTy m_ReferencedSections;
79
80  const LinkerConfig& m_Config;
81  const TargetLDBackend& m_Backend;
82  Module& m_Module;
83};
84
85}  // namespace mcld
86
87#endif  // MCLD_LD_GARBAGECOLLECTION_H_
88