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