Path: blob/main/contrib/llvm-project/llvm/lib/ObjCopy/COFF/COFFObject.cpp
35269 views
//===- COFFObject.cpp -----------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "COFFObject.h"9#include "llvm/ADT/DenseSet.h"10#include <algorithm>1112namespace llvm {13namespace objcopy {14namespace coff {1516using namespace object;1718void Object::addSymbols(ArrayRef<Symbol> NewSymbols) {19for (Symbol S : NewSymbols) {20S.UniqueId = NextSymbolUniqueId++;21Symbols.emplace_back(S);22}23updateSymbols();24}2526void Object::updateSymbols() {27SymbolMap = DenseMap<size_t, Symbol *>(Symbols.size());28for (Symbol &Sym : Symbols)29SymbolMap[Sym.UniqueId] = &Sym;30}3132const Symbol *Object::findSymbol(size_t UniqueId) const {33return SymbolMap.lookup(UniqueId);34}3536Error Object::removeSymbols(37function_ref<Expected<bool>(const Symbol &)> ToRemove) {38Error Errs = Error::success();39llvm::erase_if(Symbols, [ToRemove, &Errs](const Symbol &Sym) {40Expected<bool> ShouldRemove = ToRemove(Sym);41if (!ShouldRemove) {42Errs = joinErrors(std::move(Errs), ShouldRemove.takeError());43return false;44}45return *ShouldRemove;46});4748updateSymbols();49return Errs;50}5152Error Object::markSymbols() {53for (Symbol &Sym : Symbols)54Sym.Referenced = false;55for (const Section &Sec : Sections) {56for (const Relocation &R : Sec.Relocs) {57auto It = SymbolMap.find(R.Target);58if (It == SymbolMap.end())59return createStringError(object_error::invalid_symbol_index,60"relocation target %zu not found", R.Target);61It->second->Referenced = true;62}63}64return Error::success();65}6667void Object::addSections(ArrayRef<Section> NewSections) {68for (Section S : NewSections) {69S.UniqueId = NextSectionUniqueId++;70Sections.emplace_back(S);71}72updateSections();73}7475void Object::updateSections() {76SectionMap = DenseMap<ssize_t, Section *>(Sections.size());77size_t Index = 1;78for (Section &S : Sections) {79SectionMap[S.UniqueId] = &S;80S.Index = Index++;81}82}8384const Section *Object::findSection(ssize_t UniqueId) const {85return SectionMap.lookup(UniqueId);86}8788void Object::removeSections(function_ref<bool(const Section &)> ToRemove) {89DenseSet<ssize_t> AssociatedSections;90auto RemoveAssociated = [&AssociatedSections](const Section &Sec) {91return AssociatedSections.contains(Sec.UniqueId);92};93do {94DenseSet<ssize_t> RemovedSections;95llvm::erase_if(Sections, [ToRemove, &RemovedSections](const Section &Sec) {96bool Remove = ToRemove(Sec);97if (Remove)98RemovedSections.insert(Sec.UniqueId);99return Remove;100});101// Remove all symbols referring to the removed sections.102AssociatedSections.clear();103llvm::erase_if(104Symbols, [&RemovedSections, &AssociatedSections](const Symbol &Sym) {105// If there are sections that are associative to a removed106// section,107// remove those as well as nothing will include them (and we can't108// leave them dangling).109if (RemovedSections.contains(Sym.AssociativeComdatTargetSectionId))110AssociatedSections.insert(Sym.TargetSectionId);111return RemovedSections.contains(Sym.TargetSectionId);112});113ToRemove = RemoveAssociated;114} while (!AssociatedSections.empty());115updateSections();116updateSymbols();117}118119void Object::truncateSections(function_ref<bool(const Section &)> ToTruncate) {120for (Section &Sec : Sections) {121if (ToTruncate(Sec)) {122Sec.clearContents();123Sec.Relocs.clear();124Sec.Header.SizeOfRawData = 0;125}126}127}128129} // end namespace coff130} // end namespace objcopy131} // end namespace llvm132133134