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 76 bool trace() const { return m_bTrace; } 77 78 void setBsymbolic(bool pBsymbolic = true) { m_Bsymbolic = pBsymbolic; } 79 80 bool Bsymbolic() const { return m_Bsymbolic; } 81 82 void setPIE(bool pPIE = true) { m_bPIE = pPIE; } 83 84 bool isPIE() const { return m_bPIE; } 85 86 void setBgroup(bool pBgroup = true) { m_Bgroup = pBgroup; } 87 88 bool Bgroup() const { return m_Bgroup; } 89 90 void setDyld(const std::string& pDyld) { m_Dyld = pDyld; } 91 92 const std::string& dyld() const { return m_Dyld; } 93 94 bool hasDyld() const { return !m_Dyld.empty(); } 95 96 void setSOName(const std::string& pName); 97 98 const std::string& soname() const { return m_SOName; } 99 100 void setVerbose(int8_t pVerbose = -1) { m_Verbose = pVerbose; } 101 102 int8_t verbose() const { return m_Verbose; } 103 104 void setMaxErrorNum(int16_t pNum) { m_MaxErrorNum = pNum; } 105 106 int16_t maxErrorNum() const { return m_MaxErrorNum; } 107 108 void setMaxWarnNum(int16_t pNum) { m_MaxWarnNum = pNum; } 109 110 int16_t maxWarnNum() const { return m_MaxWarnNum; } 111 112 void setColor(bool pEnabled = true) { m_bColor = pEnabled; } 113 114 bool color() const { return m_bColor; } 115 116 void setNoUndefined(bool pEnable = true) { 117 m_NoUndefined = (pEnable ? YES : NO); 118 } 119 120 void setNumSpareDTags(uint32_t pNum) { 121 m_NumSpareDTags = pNum; 122 } 123 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 133 bool hasCombReloc() const { return m_bCombReloc; } 134 135 bool hasNoUndefined() const { return (Unknown != m_NoUndefined); } 136 137 bool isNoUndefined() const { return (YES == m_NoUndefined); } 138 139 bool hasStackSet() const { return (Unknown != m_ExecStack); } 140 141 bool hasExecStack() const { return (YES == m_ExecStack); } 142 143 bool hasInitFirst() const { return m_bInitFirst; } 144 145 bool hasInterPose() const { return m_bInterPose; } 146 147 bool hasLoadFltr() const { return m_bLoadFltr; } 148 149 bool hasMulDefs() const { return (Unknown != m_MulDefs); } 150 151 bool isMulDefs() const { return (YES == m_MulDefs); } 152 153 bool hasNoCopyReloc() const { return m_bNoCopyReloc; } 154 155 bool hasNoDefaultLib() const { return m_bNoDefaultLib; } 156 157 bool hasNoDelete() const { return m_bNoDelete; } 158 159 bool hasNoDLOpen() const { return m_bNoDLOpen; } 160 161 bool hasNoDump() const { return m_bNoDump; } 162 163 bool hasRelro() const { return m_bRelro; } 164 165 bool hasNow() const { return m_bNow; } 166 167 bool hasOrigin() const { return m_bOrigin; } 168 169 uint64_t commPageSize() const { return m_CommPageSize; } 170 171 uint64_t maxPageSize() const { return m_MaxPageSize; } 172 173 bool hasEhFrameHdr() const { return m_bCreateEhFrameHdr; } 174 175 // -n, --nmagic 176 void setNMagic(bool pMagic = true) { m_bNMagic = pMagic; } 177 178 bool nmagic() const { return m_bNMagic; } 179 180 // -N, --omagic 181 void setOMagic(bool pMagic = true) { m_bOMagic = pMagic; } 182 183 bool omagic() const { return m_bOMagic; } 184 185 // -S, --strip-debug 186 void setStripDebug(bool pStripDebug = true) { m_bStripDebug = pStripDebug; } 187 188 bool stripDebug() const { return m_bStripDebug; } 189 190 // -E, --export-dynamic 191 void setExportDynamic(bool pExportDynamic = true) { 192 m_bExportDynamic = pExportDynamic; 193 } 194 195 bool exportDynamic() const { return m_bExportDynamic; } 196 197 // --warn-shared-textrel 198 void setWarnSharedTextrel(bool pWarnSharedTextrel = true) { 199 m_bWarnSharedTextrel = pWarnSharedTextrel; 200 } 201 202 bool warnSharedTextrel() const { return m_bWarnSharedTextrel; } 203 204 void setBinaryInput(bool pBinaryInput = true) { 205 m_bBinaryInput = pBinaryInput; 206 } 207 208 bool isBinaryInput() const { return m_bBinaryInput; } 209 210 void setDefineCommon(bool pEnable = true) { m_bDefineCommon = pEnable; } 211 212 bool isDefineCommon() const { return m_bDefineCommon; } 213 214 void setFatalWarnings(bool pEnable = true) { m_bFatalWarnings = pEnable; } 215 216 bool isFatalWarnings() const { return m_bFatalWarnings; } 217 218 StripSymbolMode getStripSymbolMode() const { return m_StripSymbols; } 219 220 void setStripSymbols(StripSymbolMode pMode) { m_StripSymbols = pMode; } 221 222 void setNewDTags(bool pEnable = true) { m_bNewDTags = pEnable; } 223 224 bool hasNewDTags() const { return m_bNewDTags; } 225 226 void setNoStdlib(bool pEnable = true) { m_bNoStdlib = pEnable; } 227 228 bool nostdlib() const { return m_bNoStdlib; } 229 230 // -M, --print-map 231 void setPrintMap(bool pEnable = true) { m_bPrintMap = pEnable; } 232 233 bool printMap() const { return m_bPrintMap; } 234 235 void setWarnMismatch(bool pEnable = true) { m_bWarnMismatch = pEnable; } 236 237 bool warnMismatch() const { return m_bWarnMismatch; } 238 239 // --gc-sections 240 void setGCSections(bool pEnable = true) { m_bGCSections = pEnable; } 241 242 bool GCSections() const { return m_bGCSections; } 243 244 // --print-gc-sections 245 void setPrintGCSections(bool pEnable = true) { m_bPrintGCSections = pEnable; } 246 247 bool getPrintGCSections() const { return m_bPrintGCSections; } 248 249 // --ld-generated-unwind-info 250 void setGenUnwindInfo(bool pEnable = true) { m_bGenUnwindInfo = pEnable; } 251 252 bool genUnwindInfo() const { return m_bGenUnwindInfo; } 253 254 HashStyle getHashStyle() const { return m_HashStyle; } 255 256 bool hasGNUHash() const { 257 return m_HashStyle == HashStyle::GNU || m_HashStyle == HashStyle::Both; 258 } 259 260 bool hasSysVHash() const { 261 return m_HashStyle == HashStyle::SystemV || m_HashStyle == HashStyle::Both; 262 } 263 264 void setHashStyle(HashStyle pStyle) { m_HashStyle = pStyle; } 265 266 ICF getICFMode() const { return m_ICF; } 267 268 void setICFMode(ICF pMode) { m_ICF = pMode; } 269 270 size_t getICFIterations() const { return m_ICFIterations; } 271 272 void setICFIterations(size_t pNum) { m_ICFIterations = pNum; } 273 274 bool printICFSections() const { return m_bPrintICFSections; } 275 276 void setPrintICFSections(bool pPrintICFSections = true) { 277 m_bPrintICFSections = pPrintICFSections; 278 } 279 280 // ----- link-in rpath ----- // 281 const RpathList& getRpathList() const { return m_RpathList; } 282 RpathList& getRpathList() { return m_RpathList; } 283 284 const_rpath_iterator rpath_begin() const { return m_RpathList.begin(); } 285 rpath_iterator rpath_begin() { return m_RpathList.begin(); } 286 const_rpath_iterator rpath_end() const { return m_RpathList.end(); } 287 rpath_iterator rpath_end() { return m_RpathList.end(); } 288 289 // ----- link-in script ----- // 290 const ScriptList& getScriptList() const { return m_ScriptList; } 291 ScriptList& getScriptList() { return m_ScriptList; } 292 293 const_script_iterator script_begin() const { return m_ScriptList.begin(); } 294 script_iterator script_begin() { return m_ScriptList.begin(); } 295 const_script_iterator script_end() const { return m_ScriptList.end(); } 296 script_iterator script_end() { return m_ScriptList.end(); } 297 298 // ----- -u/--undefined, undefined symbols ----- // 299 const UndefSymList& getUndefSymList() const { return m_UndefSymList; } 300 UndefSymList& getUndefSymList() { return m_UndefSymList; } 301 302 const_undef_sym_iterator undef_sym_begin() const { 303 return m_UndefSymList.begin(); 304 } 305 undef_sym_iterator undef_sym_begin() { return m_UndefSymList.begin(); } 306 307 const_undef_sym_iterator undef_sym_end() const { 308 return m_UndefSymList.end(); 309 } 310 undef_sym_iterator undef_sym_end() { return m_UndefSymList.end(); } 311 312 // ----- filter and auxiliary filter ----- // 313 void setFilter(const std::string& pFilter) { m_Filter = pFilter; } 314 315 const std::string& filter() const { return m_Filter; } 316 317 bool hasFilter() const { return !m_Filter.empty(); } 318 319 const AuxiliaryList& getAuxiliaryList() const { return m_AuxiliaryList; } 320 AuxiliaryList& getAuxiliaryList() { return m_AuxiliaryList; } 321 322 const_aux_iterator aux_begin() const { return m_AuxiliaryList.begin(); } 323 aux_iterator aux_begin() { return m_AuxiliaryList.begin(); } 324 const_aux_iterator aux_end() const { return m_AuxiliaryList.end(); } 325 aux_iterator aux_end() { return m_AuxiliaryList.end(); } 326 327 // ----- exclude libs ----- // 328 ExcludeLIBS& excludeLIBS() { return m_ExcludeLIBS; } 329 330 bool isInExcludeLIBS(const Input& pInput) const; 331 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