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