//===- TapiFile.cpp -------------------------------------------------------===////// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.// See https://llvm.org/LICENSE.txt for license information.// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception////===----------------------------------------------------------------------===////// This file defines the Text-based Dynamcic Library Stub format.////===----------------------------------------------------------------------===//#include"llvm/Object/TapiFile.h"#include"llvm/ADT/StringRef.h"#include"llvm/Object/Error.h"#include"llvm/Support/MemoryBuffer.h"usingnamespacellvm;usingnamespaceMachO;usingnamespaceobject;staticconstexprStringLiteralObjC1ClassNamePrefix=".objc_class_name_";staticconstexprStringLiteralObjC2ClassNamePrefix="_OBJC_CLASS_$_";staticconstexprStringLiteralObjC2MetaClassNamePrefix="_OBJC_METACLASS_$_";staticconstexprStringLiteralObjC2EHTypePrefix="_OBJC_EHTYPE_$_";staticconstexprStringLiteralObjC2IVarPrefix="_OBJC_IVAR_$_";staticuint32_tgetFlags(constSymbol*Sym){uint32_tFlags=BasicSymbolRef::SF_Global;if(Sym->isUndefined())Flags|=BasicSymbolRef::SF_Undefined;elseFlags|=BasicSymbolRef::SF_Exported;if(Sym->isWeakDefined()||Sym->isWeakReferenced())Flags|=BasicSymbolRef::SF_Weak;returnFlags;}TapiFile::TapiFile(MemoryBufferRefSource,constInterfaceFile&interface,ArchitectureArch):SymbolicFile(ID_TapiFile,Source){for(constauto*Symbol:interface.symbols()){if(!Symbol->getArchitectures().has(Arch))continue;autoPlatform=interface.getPlatform();switch(Symbol->getKind()){caseSymbolKind::GlobalSymbol:Symbols.emplace_back(StringRef(),Symbol->getName(),getFlags(Symbol));break;caseSymbolKind::ObjectiveCClass:if(Platform==PlatformKind::macOS&&Arch==AK_i386){Symbols.emplace_back(ObjC1ClassNamePrefix,Symbol->getName(),getFlags(Symbol));}else{Symbols.emplace_back(ObjC2ClassNamePrefix,Symbol->getName(),getFlags(Symbol));Symbols.emplace_back(ObjC2MetaClassNamePrefix,Symbol->getName(),getFlags(Symbol));}break;caseSymbolKind::ObjectiveCClassEHType:Symbols.emplace_back(ObjC2EHTypePrefix,Symbol->getName(),getFlags(Symbol));break;caseSymbolKind::ObjectiveCInstanceVariable:Symbols.emplace_back(ObjC2IVarPrefix,Symbol->getName(),getFlags(Symbol));break;}}}TapiFile::~TapiFile()=default;voidTapiFile::moveSymbolNext(DataRefImpl&DRI)const{constauto*Sym=reinterpret_cast<constSymbol*>(DRI.p);DRI.p=reinterpret_cast<uintptr_t>(++Sym);}ErrorTapiFile::printSymbolName(raw_ostream&OS,DataRefImplDRI)const{constauto*Sym=reinterpret_cast<constSymbol*>(DRI.p);OS<<Sym->Prefix<<Sym->Name;returnError::success();}uint32_tTapiFile::getSymbolFlags(DataRefImplDRI)const{constauto*Sym=reinterpret_cast<constSymbol*>(DRI.p);returnSym->Flags;}basic_symbol_iteratorTapiFile::symbol_begin()const{DataRefImplDRI;DRI.p=reinterpret_cast<uintptr_t>(&*Symbols.begin());returnBasicSymbolRef{DRI,this};}basic_symbol_iteratorTapiFile::symbol_end()const{DataRefImplDRI;DRI.p=reinterpret_cast<uintptr_t>(&*Symbols.end());returnBasicSymbolRef{DRI,this};}