2323#include " llvm/Demangle/Demangle.h"
2424#include " llvm/IR/Function.h"
2525#include " llvm/IR/LLVMContext.h"
26+ #include " llvm/MC/TargetRegistry.h"
2627#include " llvm/Object/Archive.h"
2728#include " llvm/Object/COFF.h"
2829#include " llvm/Object/COFFImportFile.h"
@@ -110,6 +111,7 @@ static bool PrintSize;
110111static bool Quiet;
111112static bool ReverseSort;
112113static bool SpecialSyms;
114+ static bool SyntheticSyms;
113115static bool SizeSort;
114116static bool UndefinedOnly;
115117static bool WithoutAliases;
@@ -1783,6 +1785,55 @@ getDynamicSyms(SymbolicFile &Obj) {
17831785 return E->getDynamicSymbolIterators ();
17841786}
17851787
1788+ // Returns false if there is error found or true otherwise.
1789+ static bool getPltSyms (SymbolicFile &Obj, std::vector<NMSymbol> &SymbolList) {
1790+ const auto *ELFObj = dyn_cast<ELFObjectFileBase>(&Obj);
1791+ if (!ELFObj)
1792+ return true ;
1793+
1794+ std::string Err;
1795+ Triple TT;
1796+ TT.setArch (ELFObj->getArch ());
1797+ TT.setOS (ELFObj->getOS ());
1798+ const Target *TheTarget = TargetRegistry::lookupTarget (TT, Err);
1799+ if (!TheTarget) {
1800+ error (" unable to find target for " + Obj.getFileName () + " : " + Err);
1801+ return false ;
1802+ }
1803+
1804+ MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo (
1805+ TT.getTriple (), ELFObj->tryGetCPUName ().value_or (" " ).str (), " " );
1806+ if (!STI) {
1807+ error (" unable to create subtarget info for " + Obj.getFileName () + " : " +
1808+ Err);
1809+ return false ;
1810+ }
1811+
1812+ for (auto Plt : ELFObj->getPltEntries (*STI)) {
1813+ if (Plt.Symbol ) {
1814+ SymbolRef Symbol (*Plt.Symbol , ELFObj);
1815+ if (Expected<StringRef> NameOrErr = Symbol.getName ()) {
1816+ if (!NameOrErr->empty ()) {
1817+ NMSymbol S = {};
1818+ S.Address = Plt.Address ;
1819+ S.Name = NameOrErr->str () + " @plt" ;
1820+ S.TypeChar = ' T' ;
1821+ S.SectionName = Plt.Section ;
1822+ SymbolList.push_back (S);
1823+ }
1824+ } else {
1825+ consumeError (NameOrErr.takeError ());
1826+ }
1827+ } else {
1828+ WithColor::warning (errs (), ToolName)
1829+ << " PLT entry at 0x" + Twine::utohexstr (Plt.Address )
1830+ << " references an invalid symbol" ;
1831+ }
1832+ }
1833+
1834+ return true ;
1835+ }
1836+
17861837// Returns false if there is error found or true otherwise.
17871838static bool getSymbolNamesFromObject (SymbolicFile &Obj,
17881839 std::vector<NMSymbol> &SymbolList) {
@@ -1807,6 +1858,12 @@ static bool getSymbolNamesFromObject(SymbolicFile &Obj,
18071858 << toString (VersionsOrErr.takeError ()) << " \n " ;
18081859 }
18091860 }
1861+
1862+ if (SyntheticSyms) {
1863+ if (!getPltSyms (Obj, SymbolList))
1864+ return false ;
1865+ }
1866+
18101867 // If a "-s segname sectname" option was specified and this is a Mach-O
18111868 // file get the section number for that section in this object file.
18121869 unsigned int Nsect = 0 ;
@@ -2474,6 +2531,7 @@ int llvm_nm_main(int argc, char **argv, const llvm::ToolContext &) {
24742531 " (hexadecimal)" );
24752532 SizeSort = Args.hasArg (OPT_size_sort);
24762533 SpecialSyms = Args.hasArg (OPT_special_syms);
2534+ SyntheticSyms = Args.hasArg (OPT_synthetic_syms);
24772535 UndefinedOnly = Args.hasArg (OPT_undefined_only);
24782536 WithoutAliases = Args.hasArg (OPT_without_aliases);
24792537
0 commit comments