2626
2727using namespace llvm ;
2828
29- static cl::OptionCategory OptionsCategory (" Section Options" );
29+ static cl::OptionCategory ReportOptionsCategory (" Report Options" );
30+ static cl::OptionCategory CompileOptionsCategory (" Compile-style Options" );
3031
3132static cl::opt<std::string> InputFilename (cl::Positional,
3233 cl::desc (" <input bitcode file>" ),
3334 cl::init(" -" ),
3435 cl::value_desc(" filename" ),
35- cl::cat(OptionsCategory));
36+ cl::cat(ReportOptionsCategory)
37+ );
3638
3739static cl::opt<bool >
3840 FFunctionSections (" ffunction-sections" , cl::Prefix, cl::init(false ),
3941 cl::desc(" Place each function in its own section" ),
40- cl::cat(OptionsCategory ));
42+ cl::cat(CompileOptionsCategory ));
4143
4244static cl::opt<bool >
4345 FDataSections (" fdata-sections" , cl::Prefix, cl::init(false ),
4446 cl::desc(" Place each data object in its own section" ),
45- cl::cat(OptionsCategory ));
47+ cl::cat(CompileOptionsCategory ));
4648
4749static cl::opt<bool > SymbolsReport (" symbols" , cl::Prefix, cl::init(false ),
4850 cl::desc(" Report symbol names" ),
49- cl::cat(OptionsCategory ));
51+ cl::cat(ReportOptionsCategory ));
5052
5153static cl::opt<bool > SectionsReport (" sections" , cl::Prefix, cl::init(false ),
5254 cl::desc(" Report section names" ),
53- cl::cat(OptionsCategory ));
55+ cl::cat(ReportOptionsCategory ));
5456
5557static cl::opt<bool >
5658 MapReport (" map" , cl::Prefix, cl::init(false ),
5759 cl::desc(" Report mapping of symbols to sections" ),
58- cl::cat(OptionsCategory ));
60+ cl::cat(ReportOptionsCategory ));
5961
60- std::string FunctionSectionName (Function &F) {
62+ static cl::opt<bool >
63+ DefsOnly (" defs-only" , cl::Prefix, cl::init(false ),
64+ cl::desc(" Only report defined symbols, not declarations" ),
65+ cl::cat(ReportOptionsCategory));
66+
67+ static cl::opt<bool >
68+ ExternOnly (" externally-visible" , cl::Prefix, cl::init(false ),
69+ cl::desc(" Only report externally visible symbols" ),
70+ cl::cat(ReportOptionsCategory));
71+
72+
73+ static std::string FunctionSectionName (Function &F) {
6174 if (F.hasSection ()) {
6275 return F.getSection ().str ();
6376 } else {
@@ -69,7 +82,7 @@ std::string FunctionSectionName(Function &F) {
6982 }
7083}
7184
72- std::string GlobalSectionName (GlobalVariable &G) {
85+ static std::string GlobalSectionName (GlobalVariable &G) {
7386 if (G.hasSection ()) {
7487 return G.getSection ().str ();
7588 } else {
@@ -95,14 +108,22 @@ std::string GlobalSectionName(GlobalVariable &G) {
95108 }
96109}
97110
98- std::map<std::string, std::string> SymbolsToSectionsMap;
99- std::set<std::string> SectionNames;
100- std::set<std::string> SymbolNames;
111+ static std::map<std::string, std::string> SymbolsToSectionsMap;
112+ static std::set<std::string> SectionNames;
113+ static std::set<std::string> SymbolNames;
114+
115+ static bool ShouldReport (GlobalObject &G) {
116+ if (ExternOnly && !G.hasExternalLinkage ())
117+ return false ;
118+ else if (DefsOnly && G.isDeclaration ())
119+ return false ;
120+ return true ;
121+ }
101122
102123int main (int argc, char **argv) {
103124 LLVMContext Context;
104125 SMDiagnostic Err;
105- cl::HideUnrelatedOptions ({&OptionsCategory });
126+ cl::HideUnrelatedOptions ({&ReportOptionsCategory, &CompileOptionsCategory });
106127 cl::ParseCommandLineOptions (argc, argv, " LLVM LTO section tool\n " );
107128
108129 std::unique_ptr<Module> M = parseIRFile (InputFilename, Err, Context);
@@ -125,11 +146,13 @@ int main(int argc, char **argv) {
125146 };
126147
127148 for (GlobalVariable &G : M->globals ()) {
128- RecordSymbol (G.getName ().str (), GlobalSectionName (G));
149+ if (ShouldReport (G))
150+ RecordSymbol (G.getName ().str (), GlobalSectionName (G));
129151 }
130152
131153 for (Function &F : M->functions ()) {
132- RecordSymbol (F.getName ().str (), FunctionSectionName (F));
154+ if (ShouldReport (F))
155+ RecordSymbol (F.getName ().str (), FunctionSectionName (F));
133156 }
134157
135158 if (SectionsReport) {
0 commit comments