1 //===- GeneralOptions.h ---------------------------------------------------===//
2 //
3 //                     The MCLinker Project
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #ifndef MCLD_GENERALOPTIONS_H_
10 #define MCLD_GENERALOPTIONS_H_
11 #include "mcld/Config/Config.h"
12 #include "mcld/Support/RealPath.h"
13 #include "mcld/Support/FileSystem.h"
14 #include <string>
15 #include <vector>
16 #include <set>
17 
18 namespace mcld {
19 
20 class Input;
21 class ZOption;
22 
23 /** \class GeneralOptions
24  *  \brief GeneralOptions collects the options that not be one of the
25  *     - input files
26  *     - attribute of input files
27  */
28 class GeneralOptions {
29  public:
30   enum class StripSymbolMode {
31     KeepAllSymbols,
32     StripTemporaries,
33     StripLocals,
34     StripAllSymbols
35   };
36 
37   enum class HashStyle : uint8_t {
38     Unknown = 0x0,
39     SystemV = 0x1,
40     GNU = 0x2,
41     Both = 0x3
42   };
43 
44   enum class ICF {
45     Unknown,
46     None,
47     All,
48     Safe
49   };
50 
51   typedef std::vector<std::string> RpathList;
52   typedef RpathList::iterator rpath_iterator;
53   typedef RpathList::const_iterator const_rpath_iterator;
54 
55   typedef std::vector<std::string> ScriptList;
56   typedef ScriptList::iterator script_iterator;
57   typedef ScriptList::const_iterator const_script_iterator;
58 
59   typedef std::vector<std::string> AuxiliaryList;
60   typedef AuxiliaryList::iterator aux_iterator;
61   typedef AuxiliaryList::const_iterator const_aux_iterator;
62 
63   typedef std::vector<std::string> UndefSymList;
64   typedef UndefSymList::iterator undef_sym_iterator;
65   typedef UndefSymList::const_iterator const_undef_sym_iterator;
66 
67   typedef std::set<std::string> ExcludeLIBS;
68 
69  public:
70   GeneralOptions();
71   ~GeneralOptions();
72 
73   /// trace
74   void setTrace(bool pEnableTrace = true) { m_bTrace = pEnableTrace; }
75 
trace()76   bool trace() const { return m_bTrace; }
77 
78   void setBsymbolic(bool pBsymbolic = true) { m_Bsymbolic = pBsymbolic; }
79 
Bsymbolic()80   bool Bsymbolic() const { return m_Bsymbolic; }
81 
82   void setPIE(bool pPIE = true) { m_bPIE = pPIE; }
83 
isPIE()84   bool isPIE() const { return m_bPIE; }
85 
86   void setBgroup(bool pBgroup = true) { m_Bgroup = pBgroup; }
87 
Bgroup()88   bool Bgroup() const { return m_Bgroup; }
89 
setDyld(const std::string & pDyld)90   void setDyld(const std::string& pDyld) { m_Dyld = pDyld; }
91 
dyld()92   const std::string& dyld() const { return m_Dyld; }
93 
hasDyld()94   bool hasDyld() const { return !m_Dyld.empty(); }
95 
96   void setSOName(const std::string& pName);
97 
soname()98   const std::string& soname() const { return m_SOName; }
99 
100   void setVerbose(int8_t pVerbose = -1) { m_Verbose = pVerbose; }
101 
verbose()102   int8_t verbose() const { return m_Verbose; }
103 
setMaxErrorNum(int16_t pNum)104   void setMaxErrorNum(int16_t pNum) { m_MaxErrorNum = pNum; }
105 
maxErrorNum()106   int16_t maxErrorNum() const { return m_MaxErrorNum; }
107 
setMaxWarnNum(int16_t pNum)108   void setMaxWarnNum(int16_t pNum) { m_MaxWarnNum = pNum; }
109 
maxWarnNum()110   int16_t maxWarnNum() const { return m_MaxWarnNum; }
111 
112   void setColor(bool pEnabled = true) { m_bColor = pEnabled; }
113 
color()114   bool color() const { return m_bColor; }
115 
116   void setNoUndefined(bool pEnable = true) {
117     m_NoUndefined = (pEnable ? YES : NO);
118   }
119 
setNumSpareDTags(uint32_t pNum)120   void setNumSpareDTags(uint32_t pNum) {
121     m_NumSpareDTags = pNum;
122   }
123 
getNumSpareDTags()124   unsigned getNumSpareDTags() const { return m_NumSpareDTags; }
125 
126   void setMulDefs(bool pEnable = true) { m_MulDefs = (pEnable ? YES : NO); }
127 
128   void setEhFrameHdr(bool pEnable = true) { m_bCreateEhFrameHdr = pEnable; }
129 
130   ///  -----  the -z options  -----  ///
131   void addZOption(const mcld::ZOption& pOption);
132 
hasCombReloc()133   bool hasCombReloc() const { return m_bCombReloc; }
134 
hasNoUndefined()135   bool hasNoUndefined() const { return (Unknown != m_NoUndefined); }
136 
isNoUndefined()137   bool isNoUndefined() const { return (YES == m_NoUndefined); }
138 
hasStackSet()139   bool hasStackSet() const { return (Unknown != m_ExecStack); }
140 
hasExecStack()141   bool hasExecStack() const { return (YES == m_ExecStack); }
142 
hasInitFirst()143   bool hasInitFirst() const { return m_bInitFirst; }
144 
hasInterPose()145   bool hasInterPose() const { return m_bInterPose; }
146 
hasLoadFltr()147   bool hasLoadFltr() const { return m_bLoadFltr; }
148 
hasMulDefs()149   bool hasMulDefs() const { return (Unknown != m_MulDefs); }
150 
isMulDefs()151   bool isMulDefs() const { return (YES == m_MulDefs); }
152 
hasNoCopyReloc()153   bool hasNoCopyReloc() const { return m_bNoCopyReloc; }
154 
hasNoDefaultLib()155   bool hasNoDefaultLib() const { return m_bNoDefaultLib; }
156 
hasNoDelete()157   bool hasNoDelete() const { return m_bNoDelete; }
158 
hasNoDLOpen()159   bool hasNoDLOpen() const { return m_bNoDLOpen; }
160 
hasNoDump()161   bool hasNoDump() const { return m_bNoDump; }
162 
hasRelro()163   bool hasRelro() const { return m_bRelro; }
164 
hasNow()165   bool hasNow() const { return m_bNow; }
166 
hasOrigin()167   bool hasOrigin() const { return m_bOrigin; }
168 
commPageSize()169   uint64_t commPageSize() const { return m_CommPageSize; }
170 
maxPageSize()171   uint64_t maxPageSize() const { return m_MaxPageSize; }
172 
hasEhFrameHdr()173   bool hasEhFrameHdr() const { return m_bCreateEhFrameHdr; }
174 
175   // -n, --nmagic
176   void setNMagic(bool pMagic = true) { m_bNMagic = pMagic; }
177 
nmagic()178   bool nmagic() const { return m_bNMagic; }
179 
180   // -N, --omagic
181   void setOMagic(bool pMagic = true) { m_bOMagic = pMagic; }
182 
omagic()183   bool omagic() const { return m_bOMagic; }
184 
185   // -S, --strip-debug
186   void setStripDebug(bool pStripDebug = true) { m_bStripDebug = pStripDebug; }
187 
stripDebug()188   bool stripDebug() const { return m_bStripDebug; }
189 
190   // -E, --export-dynamic
191   void setExportDynamic(bool pExportDynamic = true) {
192     m_bExportDynamic = pExportDynamic;
193   }
194 
exportDynamic()195   bool exportDynamic() const { return m_bExportDynamic; }
196 
197   // --warn-shared-textrel
198   void setWarnSharedTextrel(bool pWarnSharedTextrel = true) {
199     m_bWarnSharedTextrel = pWarnSharedTextrel;
200   }
201 
warnSharedTextrel()202   bool warnSharedTextrel() const { return m_bWarnSharedTextrel; }
203 
204   void setBinaryInput(bool pBinaryInput = true) {
205     m_bBinaryInput = pBinaryInput;
206   }
207 
isBinaryInput()208   bool isBinaryInput() const { return m_bBinaryInput; }
209 
210   void setDefineCommon(bool pEnable = true) { m_bDefineCommon = pEnable; }
211 
isDefineCommon()212   bool isDefineCommon() const { return m_bDefineCommon; }
213 
214   void setFatalWarnings(bool pEnable = true) { m_bFatalWarnings = pEnable; }
215 
isFatalWarnings()216   bool isFatalWarnings() const { return m_bFatalWarnings; }
217 
getStripSymbolMode()218   StripSymbolMode getStripSymbolMode() const { return m_StripSymbols; }
219 
setStripSymbols(StripSymbolMode pMode)220   void setStripSymbols(StripSymbolMode pMode) { m_StripSymbols = pMode; }
221 
222   void setNewDTags(bool pEnable = true) { m_bNewDTags = pEnable; }
223 
hasNewDTags()224   bool hasNewDTags() const { return m_bNewDTags; }
225 
226   void setNoStdlib(bool pEnable = true) { m_bNoStdlib = pEnable; }
227 
nostdlib()228   bool nostdlib() const { return m_bNoStdlib; }
229 
230   // -M, --print-map
231   void setPrintMap(bool pEnable = true) { m_bPrintMap = pEnable; }
232 
printMap()233   bool printMap() const { return m_bPrintMap; }
234 
235   void setWarnMismatch(bool pEnable = true) { m_bWarnMismatch = pEnable; }
236 
warnMismatch()237   bool warnMismatch() const { return m_bWarnMismatch; }
238 
239   // --gc-sections
240   void setGCSections(bool pEnable = true) { m_bGCSections = pEnable; }
241 
GCSections()242   bool GCSections() const { return m_bGCSections; }
243 
244   // --print-gc-sections
245   void setPrintGCSections(bool pEnable = true) { m_bPrintGCSections = pEnable; }
246 
getPrintGCSections()247   bool getPrintGCSections() const { return m_bPrintGCSections; }
248 
249   // --ld-generated-unwind-info
250   void setGenUnwindInfo(bool pEnable = true) { m_bGenUnwindInfo = pEnable; }
251 
genUnwindInfo()252   bool genUnwindInfo() const { return m_bGenUnwindInfo; }
253 
getHashStyle()254   HashStyle getHashStyle() const { return m_HashStyle; }
255 
hasGNUHash()256   bool hasGNUHash() const {
257     return m_HashStyle == HashStyle::GNU || m_HashStyle == HashStyle::Both;
258   }
259 
hasSysVHash()260   bool hasSysVHash() const {
261     return m_HashStyle == HashStyle::SystemV || m_HashStyle == HashStyle::Both;
262   }
263 
setHashStyle(HashStyle pStyle)264   void setHashStyle(HashStyle pStyle) { m_HashStyle = pStyle; }
265 
getICFMode()266   ICF getICFMode() const { return m_ICF; }
267 
setICFMode(ICF pMode)268   void setICFMode(ICF pMode) { m_ICF = pMode; }
269 
getICFIterations()270   size_t getICFIterations() const { return m_ICFIterations; }
271 
setICFIterations(size_t pNum)272   void setICFIterations(size_t pNum) { m_ICFIterations = pNum; }
273 
printICFSections()274   bool printICFSections() const { return m_bPrintICFSections; }
275 
276   void setPrintICFSections(bool pPrintICFSections = true) {
277     m_bPrintICFSections = pPrintICFSections;
278   }
279 
280   // -----  link-in rpath  ----- //
getRpathList()281   const RpathList& getRpathList() const { return m_RpathList; }
getRpathList()282   RpathList& getRpathList() { return m_RpathList; }
283 
rpath_begin()284   const_rpath_iterator rpath_begin() const { return m_RpathList.begin(); }
rpath_begin()285   rpath_iterator rpath_begin() { return m_RpathList.begin(); }
rpath_end()286   const_rpath_iterator rpath_end() const { return m_RpathList.end(); }
rpath_end()287   rpath_iterator rpath_end() { return m_RpathList.end(); }
288 
289   // -----  link-in script  ----- //
getScriptList()290   const ScriptList& getScriptList() const { return m_ScriptList; }
getScriptList()291   ScriptList& getScriptList() { return m_ScriptList; }
292 
script_begin()293   const_script_iterator script_begin() const { return m_ScriptList.begin(); }
script_begin()294   script_iterator script_begin() { return m_ScriptList.begin(); }
script_end()295   const_script_iterator script_end() const { return m_ScriptList.end(); }
script_end()296   script_iterator script_end() { return m_ScriptList.end(); }
297 
298   // -----  -u/--undefined, undefined symbols ----- //
getUndefSymList()299   const UndefSymList& getUndefSymList() const { return m_UndefSymList; }
getUndefSymList()300   UndefSymList& getUndefSymList() { return m_UndefSymList; }
301 
undef_sym_begin()302   const_undef_sym_iterator undef_sym_begin() const {
303     return m_UndefSymList.begin();
304   }
undef_sym_begin()305   undef_sym_iterator undef_sym_begin() { return m_UndefSymList.begin(); }
306 
undef_sym_end()307   const_undef_sym_iterator undef_sym_end() const {
308     return m_UndefSymList.end();
309   }
undef_sym_end()310   undef_sym_iterator undef_sym_end() { return m_UndefSymList.end(); }
311 
312   // -----  filter and auxiliary filter  ----- //
setFilter(const std::string & pFilter)313   void setFilter(const std::string& pFilter) { m_Filter = pFilter; }
314 
filter()315   const std::string& filter() const { return m_Filter; }
316 
hasFilter()317   bool hasFilter() const { return !m_Filter.empty(); }
318 
getAuxiliaryList()319   const AuxiliaryList& getAuxiliaryList() const { return m_AuxiliaryList; }
getAuxiliaryList()320   AuxiliaryList& getAuxiliaryList() { return m_AuxiliaryList; }
321 
aux_begin()322   const_aux_iterator aux_begin() const { return m_AuxiliaryList.begin(); }
aux_begin()323   aux_iterator aux_begin() { return m_AuxiliaryList.begin(); }
aux_end()324   const_aux_iterator aux_end() const { return m_AuxiliaryList.end(); }
aux_end()325   aux_iterator aux_end() { return m_AuxiliaryList.end(); }
326 
327   // -----  exclude libs  ----- //
excludeLIBS()328   ExcludeLIBS& excludeLIBS() { return m_ExcludeLIBS; }
329 
330   bool isInExcludeLIBS(const Input& pInput) const;
331 
getVersionString()332   const char* getVersionString() const { return PACKAGE_NAME " " MCLD_VERSION; }
333 
334  private:
335   enum status { YES, NO, Unknown };
336 
337  private:
338   std::string m_DefaultLDScript;
339   std::string m_Dyld;
340   std::string m_SOName;
341   int8_t m_Verbose;          // --verbose[=0,1,2]
342   uint16_t m_MaxErrorNum;    // --error-limit=N
343   uint16_t m_MaxWarnNum;     // --warning-limit=N
344   unsigned m_NumSpareDTags;  // --spare-dynamic-tags
345   status m_ExecStack;        // execstack, noexecstack
346   status m_NoUndefined;      // defs, --no-undefined
347   status m_MulDefs;          // muldefs, --allow-multiple-definition
348   uint64_t m_CommPageSize;   // common-page-size=value
349   uint64_t m_MaxPageSize;    // max-page-size=value
350   bool m_bCombReloc : 1;     // combreloc, nocombreloc
351   bool m_bInitFirst : 1;     // initfirst
352   bool m_bInterPose : 1;     // interpose
353   bool m_bLoadFltr : 1;      // loadfltr
354   bool m_bNoCopyReloc : 1;   // nocopyreloc
355   bool m_bNoDefaultLib : 1;  // nodefaultlib
356   bool m_bNoDelete : 1;      // nodelete
357   bool m_bNoDLOpen : 1;      // nodlopen
358   bool m_bNoDump : 1;        // nodump
359   bool m_bRelro : 1;         // relro, norelro
360   bool m_bNow : 1;           // lazy, now
361   bool m_bOrigin : 1;        // origin
362   bool m_bTrace : 1;         // --trace
363   bool m_Bsymbolic : 1;      // --Bsymbolic
364   bool m_Bgroup : 1;
365   bool m_bPIE : 1;
366   bool m_bColor : 1;              // --color[=true,false,auto]
367   bool m_bCreateEhFrameHdr : 1;   // --eh-frame-hdr
368   bool m_bNMagic : 1;             // -n, --nmagic
369   bool m_bOMagic : 1;             // -N, --omagic
370   bool m_bStripDebug : 1;         // -S, --strip-debug
371   bool m_bExportDynamic : 1;      // -E, --export-dynamic
372   bool m_bWarnSharedTextrel : 1;  // --warn-shared-textrel
373   bool m_bBinaryInput : 1;        // -b [input-format], --format=[input-format]
374   bool m_bDefineCommon : 1;       // -d, -dc, -dp
375   bool m_bFatalWarnings : 1;      // --fatal-warnings
376   bool m_bNewDTags : 1;           // --enable-new-dtags
377   bool m_bNoStdlib : 1;           // -nostdlib
378   bool m_bPrintMap : 1;           // --print-map
379   bool m_bWarnMismatch : 1;       // --no-warn-mismatch
380   bool m_bGCSections : 1;         // --gc-sections
381   bool m_bPrintGCSections : 1;    // --print-gc-sections
382   bool m_bGenUnwindInfo : 1;      // --ld-generated-unwind-info
383   bool m_bPrintICFSections : 1;   // --print-icf-sections
384   ICF m_ICF;
385   size_t m_ICFIterations;
386   StripSymbolMode m_StripSymbols;
387   RpathList m_RpathList;
388   ScriptList m_ScriptList;
389   UndefSymList m_UndefSymList;  // -u [symbol], --undefined [symbol]
390   HashStyle m_HashStyle;
391   std::string m_Filter;
392   AuxiliaryList m_AuxiliaryList;
393   ExcludeLIBS m_ExcludeLIBS;
394 };
395 
396 }  // namespace mcld
397 
398 #endif  // MCLD_GENERALOPTIONS_H_
399