xref: /dflybsd-src/contrib/binutils-2.34/gold/gc.cc (revision b52ef7118d1621abed722c5bbbd542210290ecef)
1*fae548d3Szrj // gc.cc -- garbage collection of unused sections
2*fae548d3Szrj 
3*fae548d3Szrj // Copyright (C) 2009-2020 Free Software Foundation, Inc.
4*fae548d3Szrj // Written by Sriraman Tallam <tmsriram@google.com>.
5*fae548d3Szrj 
6*fae548d3Szrj // This file is part of gold.
7*fae548d3Szrj 
8*fae548d3Szrj // This program is free software; you can redistribute it and/or modify
9*fae548d3Szrj // it under the terms of the GNU General Public License as published by
10*fae548d3Szrj // the Free Software Foundation; either version 3 of the License, or
11*fae548d3Szrj // (at your option) any later version.
12*fae548d3Szrj 
13*fae548d3Szrj // This program is distributed in the hope that it will be useful,
14*fae548d3Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*fae548d3Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*fae548d3Szrj // GNU General Public License for more details.
17*fae548d3Szrj 
18*fae548d3Szrj // You should have received a copy of the GNU General Public License
19*fae548d3Szrj // along with this program; if not, write to the Free Software
20*fae548d3Szrj // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21*fae548d3Szrj // MA 02110-1301, USA.
22*fae548d3Szrj 
23*fae548d3Szrj 
24*fae548d3Szrj #include "gold.h"
25*fae548d3Szrj #include "object.h"
26*fae548d3Szrj #include "gc.h"
27*fae548d3Szrj #include "symtab.h"
28*fae548d3Szrj 
29*fae548d3Szrj namespace gold
30*fae548d3Szrj {
31*fae548d3Szrj 
32*fae548d3Szrj // Garbage collection uses a worklist style algorithm to determine the
33*fae548d3Szrj // transitive closure of all referenced sections.
34*fae548d3Szrj void
do_transitive_closure()35*fae548d3Szrj Garbage_collection::do_transitive_closure()
36*fae548d3Szrj {
37*fae548d3Szrj   while (!this->worklist().empty())
38*fae548d3Szrj     {
39*fae548d3Szrj       // Add elements from the work list to the referenced list
40*fae548d3Szrj       // one by one.
41*fae548d3Szrj       Section_id entry = this->worklist().back();
42*fae548d3Szrj       this->worklist().pop_back();
43*fae548d3Szrj       if (!this->referenced_list().insert(entry).second)
44*fae548d3Szrj         continue;
45*fae548d3Szrj       Garbage_collection::Section_ref::iterator find_it =
46*fae548d3Szrj                 this->section_reloc_map().find(entry);
47*fae548d3Szrj       if (find_it == this->section_reloc_map().end())
48*fae548d3Szrj           continue;
49*fae548d3Szrj       const Garbage_collection::Sections_reachable &v = find_it->second;
50*fae548d3Szrj       // Scan the vector of references for each work_list entry.
51*fae548d3Szrj       for (Garbage_collection::Sections_reachable::const_iterator it_v =
52*fae548d3Szrj                v.begin();
53*fae548d3Szrj            it_v != v.end();
54*fae548d3Szrj            ++it_v)
55*fae548d3Szrj         {
56*fae548d3Szrj           // Do not add already processed sections to the work_list.
57*fae548d3Szrj           if (this->referenced_list().find(*it_v)
58*fae548d3Szrj               == this->referenced_list().end())
59*fae548d3Szrj             {
60*fae548d3Szrj               this->worklist().push_back(*it_v);
61*fae548d3Szrj             }
62*fae548d3Szrj         }
63*fae548d3Szrj     }
64*fae548d3Szrj   this->worklist_ready();
65*fae548d3Szrj }
66*fae548d3Szrj 
67*fae548d3Szrj } // End namespace gold.
68*fae548d3Szrj 
69