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