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