]> git.saurik.com Git - apple/ld64.git/blame - src/ld/Options.h
ld64-97.17.tar.gz
[apple/ld64.git] / src / ld / Options.h
CommitLineData
d696c285 1/* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
6e880c60 2 *
a61fdf0a 3 * Copyright (c) 2005-2007 Apple Inc. All rights reserved.
c2646906
A
4 *
5 * @APPLE_LICENSE_HEADER_START@
d696c285 6 *
c2646906
A
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * file.
d696c285 13 *
c2646906
A
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
d696c285 21 *
c2646906
A
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25#ifndef __OPTIONS__
26#define __OPTIONS__
27
28
29#include <stdint.h>
30#include <mach/machine.h>
31
c2646906
A
32#include <vector>
33#include <ext/hash_set>
55e3d2f6 34#include <ext/hash_map>
c2646906
A
35
36#include "ObjectFile.h"
37
fb24a050
A
38extern void throwf (const char* format, ...) __attribute__ ((noreturn,format(printf, 1, 2)));
39extern void warning(const char* format, ...) __attribute__((format(printf, 1, 2)));
c2646906 40
55e3d2f6 41class LibraryOptions
c2646906
A
42{
43public:
55e3d2f6
A
44 LibraryOptions() : fWeakImport(false), fReExport(false), fBundleLoader(false), fLazyLoad(false), fForceLoad(false) {}
45 // for dynamic libraries
c2646906
A
46 bool fWeakImport;
47 bool fReExport;
69a49097 48 bool fBundleLoader;
2f2f92e4 49 bool fLazyLoad;
55e3d2f6
A
50 // for static libraries
51 bool fForceLoad;
c2646906
A
52};
53
55e3d2f6
A
54
55
d696c285
A
56//
57// The public interface to the Options class is the abstract representation of what work the linker
58// should do.
59//
60// This abstraction layer will make it easier to support a future where the linker is a shared library
61// invoked directly from Xcode. The target settings in Xcode would be used to directly construct an Options
62// object (without building a command line which is then parsed).
63//
64//
c2646906
A
65class Options
66{
67public:
d696c285
A
68 Options(int argc, const char* argv[]);
69 ~Options();
c2646906 70
55e3d2f6 71 enum OutputKind { kDynamicExecutable, kStaticExecutable, kDynamicLibrary, kDynamicBundle, kObjectFile, kDyld, kPreload, kKextBundle };
c2646906 72 enum NameSpace { kTwoLevelNameSpace, kFlatNameSpace, kForceFlatNameSpace };
d696c285
A
73 // Standard treatment for many options.
74 enum Treatment { kError, kWarning, kSuppress, kNULL, kInvalid };
c2646906 75 enum UndefinedTreatment { kUndefinedError, kUndefinedWarning, kUndefinedSuppress, kUndefinedDynamicLookup };
d696c285
A
76 enum WeakReferenceMismatchTreatment { kWeakReferenceMismatchError, kWeakReferenceMismatchWeak,
77 kWeakReferenceMismatchNonWeak };
c2646906 78 enum CommonsMode { kCommonsIgnoreDylibs, kCommonsOverriddenByDylibs, kCommonsConflictsDylibsError };
d696c285 79 enum DeadStripMode { kDeadStripOff, kDeadStripOn, kDeadStripOnPlusUnusedInits };
a61fdf0a
A
80 enum UUIDMode { kUUIDNone, kUUIDRandom, kUUIDContent };
81 enum LocalSymbolHandling { kLocalSymbolsAll, kLocalSymbolsNone, kLocalSymbolsSelectiveInclude, kLocalSymbolsSelectiveExclude };
c2646906
A
82
83 struct FileInfo {
84 const char* path;
85 uint64_t fileLen;
d696c285 86 time_t modTime;
55e3d2f6 87 LibraryOptions options;
c2646906 88 };
d696c285 89
c2646906
A
90 struct ExtraSection {
91 const char* segmentName;
92 const char* sectionName;
93 const char* path;
94 const uint8_t* data;
95 uint64_t dataLen;
96 };
d696c285 97
c2646906
A
98 struct SectionAlignment {
99 const char* segmentName;
100 const char* sectionName;
101 uint8_t alignment;
102 };
103
a61fdf0a
A
104 struct OrderedSymbol {
105 const char* symbolName;
106 const char* objectFileName;
107 };
108
109 struct SegmentStart {
110 const char* name;
111 uint64_t address;
112 };
113
55e3d2f6
A
114 struct SegmentSize {
115 const char* name;
116 uint64_t size;
117 };
118
a61fdf0a
A
119 struct SegmentProtect {
120 const char* name;
121 uint32_t max;
122 uint32_t init;
123 };
124
125 struct DylibOverride {
126 const char* installName;
127 const char* useInstead;
128 };
129
130
d696c285
A
131 const ObjectFile::ReaderOptions& readerOptions();
132 const char* getOutputFilePath();
133 std::vector<FileInfo>& getInputFiles();
134
2f2f92e4
A
135 cpu_type_t architecture() { return fArchitecture; }
136 bool preferSubArchitecture() { return fHasPreferredSubType; }
137 cpu_subtype_t subArchitecture() { return fSubArchitecture; }
55e3d2f6 138 bool allowSubArchitectureMismatches() { return fAllowCpuSubtypeMismatches; }
c2646906 139 OutputKind outputKind();
d696c285 140 bool prebind();
c2646906
A
141 bool bindAtLoad();
142 bool fullyLoadArchives();
143 NameSpace nameSpace();
144 const char* installPath(); // only for kDynamicLibrary
145 uint32_t currentVersion(); // only for kDynamicLibrary
146 uint32_t compatibilityVersion(); // only for kDynamicLibrary
147 const char* entryName(); // only for kDynamicExecutable or kStaticExecutable
d696c285 148 const char* executablePath();
c2646906 149 uint64_t baseAddress();
2f2f92e4
A
150 bool keepPrivateExterns(); // only for kObjectFile
151 bool needsModuleTable(); // only for kDynamicLibrary
152 bool interposable(const char* name);
55e3d2f6
A
153 bool hasExportRestrictList(); // -exported_symbol or -unexported_symbol
154 bool hasExportMaskList(); // just -exported_symbol
2f2f92e4 155 bool hasWildCardExportRestrictList();
69a49097 156 bool allGlobalsAreDeadStripRoots();
c2646906
A
157 bool shouldExport(const char*);
158 bool ignoreOtherArchInputFiles();
159 bool forceCpuSubtypeAll();
160 bool traceDylibs();
161 bool traceArchives();
d696c285 162 DeadStripMode deadStrip();
c2646906 163 UndefinedTreatment undefinedTreatment();
55e3d2f6
A
164 ObjectFile::ReaderOptions::MacVersionMin macosxVersionMin() { return fReaderOptions.fMacVersionMin; }
165 ObjectFile::ReaderOptions::IPhoneVersionMin iphoneOSVersionMin() { return fReaderOptions.fIPhoneVersionMin; }
166 bool minOS(ObjectFile::ReaderOptions::MacVersionMin mac, ObjectFile::ReaderOptions::IPhoneVersionMin iPhoneOS);
c2646906 167 bool messagesPrefixedWithArchitecture();
d696c285 168 Treatment picTreatment();
c2646906
A
169 WeakReferenceMismatchTreatment weakReferenceMismatchTreatment();
170 const char* umbrellaName();
d696c285
A
171 std::vector<const char*>& allowableClients();
172 const char* clientName();
c2646906 173 const char* initFunctionName(); // only for kDynamicLibrary
d696c285 174 const char* dotOutputFile();
c2646906
A
175 uint64_t zeroPageSize();
176 bool hasCustomStack();
177 uint64_t customStackSize();
178 uint64_t customStackAddr();
d696c285 179 bool hasExecutableStack();
c2646906 180 std::vector<const char*>& initialUndefines();
69a49097 181 bool printWhyLive(const char* name);
c2646906 182 uint32_t minimumHeaderPad();
55e3d2f6 183 uint64_t segmentAlignment() { return fSegmentAlignment; }
a61fdf0a 184 bool maxMminimumHeaderPad() { return fMaxMinimumHeaderPad; }
c2646906
A
185 std::vector<ExtraSection>& extraSections();
186 std::vector<SectionAlignment>& sectionAlignments();
187 CommonsMode commonsMode();
188 bool warnCommons();
d696c285 189 bool keepRelocations();
6e880c60 190 FileInfo findFile(const char* path);
a61fdf0a 191 UUIDMode getUUIDMode() { return fUUIDMode; }
d696c285
A
192 bool warnStabs();
193 bool pauseAtEnd() { return fPause; }
194 bool printStatistics() { return fStatistics; }
69a49097 195 bool printArchPrefix() { return fMessagesPrefixedWithArchitecture; }
a61fdf0a
A
196 void gotoClassicLinker(int argc, const char* argv[]);
197 bool sharedRegionEligible() { return fSharedRegionEligible; }
198 bool printOrderFileStatistics() { return fPrintOrderFileStatistics; }
199 const char* dTraceScriptName() { return fDtraceScriptName; }
200 bool dTrace() { return (fDtraceScriptName != NULL); }
201 std::vector<OrderedSymbol>& orderedSymbols() { return fOrderedSymbols; }
202 bool splitSeg() { return fSplitSegs; }
203 uint64_t baseWritableAddress() { return fBaseWritableAddress; }
204 std::vector<SegmentStart>& customSegmentAddresses() { return fCustomSegmentAddresses; }
55e3d2f6 205 std::vector<SegmentSize>& customSegmentSizes() { return fCustomSegmentSizes; }
a61fdf0a
A
206 std::vector<SegmentProtect>& customSegmentProtections() { return fCustomSegmentProtections; }
207 bool saveTempFiles() { return fSaveTempFiles; }
208 const std::vector<const char*>& rpaths() { return fRPaths; }
209 bool readOnlyx86Stubs() { return fReadOnlyx86Stubs; }
210 std::vector<DylibOverride>& dylibOverrides() { return fDylibOverrides; }
211 const char* generatedMapPath() { return fMapPath; }
212 bool positionIndependentExecutable() { return fPositionIndependentExecutable; }
213 Options::FileInfo findFileUsingPaths(const char* path);
214 bool deadStripDylibs() { return fDeadStripDylibs; }
215 bool allowedUndefined(const char* name) { return ( fAllowedUndefined.find(name) != fAllowedUndefined.end() ); }
216 bool someAllowedUndefines() { return (fAllowedUndefined.size() != 0); }
217 LocalSymbolHandling localSymbolHandling() { return fLocalSymbolHandling; }
218 bool keepLocalSymbol(const char* symbolName);
2f2f92e4
A
219 bool allowTextRelocs() { return fAllowTextRelocs; }
220 bool warnAboutTextRelocs() { return fWarnTextRelocs; }
221 bool usingLazyDylibLinking() { return fUsingLazyDylibLinking; }
222 bool verbose() { return fVerbose; }
223 bool makeEncryptable() { return fEncryptable; }
55e3d2f6
A
224 bool needsUnwindInfoSection() { return fReaderOptions.fAddCompactUnwindEncoding; }
225 std::vector<const char*>& llvmOptions() { return fLLVMOptions; }
226 bool makeClassicDyldInfo() { return fMakeClassicDyldInfo; }
227 bool makeCompressedDyldInfo() { return fMakeCompressedDyldInfo; }
228 bool hasExportedSymbolOrder();
229 bool exportedSymbolOrder(const char* sym, unsigned int* order);
230 bool orderData() { return fOrderData; }
231 bool errorOnOtherArchFiles() { return fErrorOnOtherArchFiles; }
232 bool markAutoDeadStripDylib() { return fMarkDeadStrippableDylib; }
233 bool removeEHLabels() { return fReaderOptions.fNoEHLabels; }
fb24a050 234 bool useSimplifiedDylibReExports() { return fUseSimplifiedDylibReExports; }
d9246299 235 bool objCABIVersion2POverride() { return fObjCABIVersion2POverride; }
9d2e0767 236
c2646906
A
237private:
238 class CStringEquals
239 {
240 public:
241 bool operator()(const char* left, const char* right) const { return (strcmp(left, right) == 0); }
242 };
55e3d2f6 243 typedef __gnu_cxx::hash_map<const char*, unsigned int, __gnu_cxx::hash<const char*>, CStringEquals> NameToOrder;
c2646906
A
244 typedef __gnu_cxx::hash_set<const char*, __gnu_cxx::hash<const char*>, CStringEquals> NameSet;
245 enum ExportMode { kExportDefault, kExportSome, kDontExportSome };
246 enum LibrarySearchMode { kSearchDylibAndArchiveInEachDir, kSearchAllDirsForDylibsThenAllDirsForArchives };
2f2f92e4 247 enum InterposeMode { kInterposeNone, kInterposeAllExternal, kInterposeSome };
c2646906 248
a61fdf0a
A
249 class SetWithWildcards {
250 public:
251 void insert(const char*);
252 bool contains(const char*);
2f2f92e4 253 bool hasWildCards() { return !fWildCard.empty(); }
a61fdf0a
A
254 NameSet::iterator regularBegin() { return fRegular.begin(); }
255 NameSet::iterator regularEnd() { return fRegular.end(); }
256 private:
2f2f92e4 257 static bool hasWildCards(const char*);
a61fdf0a
A
258 bool wildCardMatch(const char* pattern, const char* candidate);
259 bool inCharRange(const char*& range, unsigned char c);
260
261 NameSet fRegular;
262 std::vector<const char*> fWildCard;
263 };
264
265
c2646906
A
266 void parse(int argc, const char* argv[]);
267 void checkIllegalOptionCombinations();
268 void buildSearchPaths(int argc, const char* argv[]);
269 void parseArch(const char* architecture);
2f2f92e4 270 FileInfo findLibrary(const char* rootName, bool dylibsOnly=false);
74cfe461
A
271 FileInfo findFramework(const char* frameworkName);
272 FileInfo findFramework(const char* rootName, const char* suffix);
d696c285
A
273 bool checkForFile(const char* format, const char* dir, const char* rootName,
274 FileInfo& result);
c2646906
A
275 uint32_t parseVersionNumber(const char*);
276 void parseSectionOrderFile(const char* segment, const char* section, const char* path);
a61fdf0a 277 void parseOrderFile(const char* path, bool cstring);
c2646906
A
278 void addSection(const char* segment, const char* section, const char* path);
279 void addSubLibrary(const char* name);
280 void loadFileList(const char* fileOfPaths);
281 uint64_t parseAddress(const char* addr);
a61fdf0a
A
282 void loadExportFile(const char* fileOfExports, const char* option, SetWithWildcards& set);
283 void parseAliasFile(const char* fileOfAliases);
c2646906
A
284 void parsePreCommandLineEnvironmentSettings();
285 void parsePostCommandLineEnvironmentSettings();
286 void setUndefinedTreatment(const char* treatment);
2f2f92e4
A
287 void setMacOSXVersionMin(const char* version);
288 void setIPhoneVersionMin(const char* version);
c2646906 289 void setWeakReferenceMismatchTreatment(const char* treatment);
a61fdf0a 290 void addDylibOverride(const char* paths);
c2646906
A
291 void addSectionAlignment(const char* segment, const char* section, const char* alignment);
292 CommonsMode parseCommonsTreatment(const char* mode);
d696c285 293 Treatment parseTreatment(const char* treatment);
69a49097 294 void reconfigureDefaults();
a61fdf0a
A
295 void checkForClassic(int argc, const char* argv[]);
296 void parseSegAddrTable(const char* segAddrPath, const char* installPath);
297 void addLibrary(const FileInfo& info);
298 void warnObsolete(const char* arg);
299 uint32_t parseProtection(const char* prot);
55e3d2f6 300 void loadSymbolOrderFile(const char* fileOfExports, NameToOrder& orderMapping);
d696c285
A
301
302
c2646906
A
303 ObjectFile::ReaderOptions fReaderOptions;
304 const char* fOutputFile;
305 std::vector<Options::FileInfo> fInputFiles;
306 cpu_type_t fArchitecture;
2f2f92e4 307 cpu_subtype_t fSubArchitecture;
c2646906 308 OutputKind fOutputKind;
2f2f92e4 309 bool fHasPreferredSubType;
d696c285 310 bool fPrebind;
c2646906 311 bool fBindAtLoad;
c2646906 312 bool fKeepPrivateExterns;
a61fdf0a 313 bool fNeedsModuleTable;
c2646906 314 bool fIgnoreOtherArchFiles;
55e3d2f6 315 bool fErrorOnOtherArchFiles;
c2646906 316 bool fForceSubtypeAll;
2f2f92e4 317 InterposeMode fInterposeMode;
d696c285 318 DeadStripMode fDeadStrip;
c2646906
A
319 NameSpace fNameSpace;
320 uint32_t fDylibCompatVersion;
321 uint32_t fDylibCurrentVersion;
322 const char* fDylibInstallName;
a61fdf0a 323 const char* fFinalName;
c2646906
A
324 const char* fEntryName;
325 uint64_t fBaseAddress;
a61fdf0a
A
326 uint64_t fBaseWritableAddress;
327 bool fSplitSegs;
328 SetWithWildcards fExportSymbols;
329 SetWithWildcards fDontExportSymbols;
2f2f92e4 330 SetWithWildcards fInterposeList;
55e3d2f6 331 NameToOrder fExportSymbolsOrder;
c2646906
A
332 ExportMode fExportMode;
333 LibrarySearchMode fLibrarySearchMode;
334 UndefinedTreatment fUndefinedTreatment;
335 bool fMessagesPrefixedWithArchitecture;
c2646906
A
336 WeakReferenceMismatchTreatment fWeakReferenceMismatchTreatment;
337 std::vector<const char*> fSubUmbellas;
338 std::vector<const char*> fSubLibraries;
d696c285 339 std::vector<const char*> fAllowableClients;
a61fdf0a 340 std::vector<const char*> fRPaths;
d696c285 341 const char* fClientName;
c2646906
A
342 const char* fUmbrellaName;
343 const char* fInitFunctionName;
d696c285
A
344 const char* fDotOutputFile;
345 const char* fExecutablePath;
69a49097 346 const char* fBundleLoader;
a61fdf0a
A
347 const char* fDtraceScriptName;
348 const char* fSegAddrTablePath;
349 const char* fMapPath;
c2646906
A
350 uint64_t fZeroPageSize;
351 uint64_t fStackSize;
352 uint64_t fStackAddr;
d696c285 353 bool fExecutableStack;
c2646906 354 uint32_t fMinimumHeaderPad;
55e3d2f6 355 uint64_t fSegmentAlignment;
c2646906 356 CommonsMode fCommonsMode;
a61fdf0a
A
357 UUIDMode fUUIDMode;
358 SetWithWildcards fLocalSymbolsIncluded;
359 SetWithWildcards fLocalSymbolsExcluded;
360 LocalSymbolHandling fLocalSymbolHandling;
c2646906 361 bool fWarnCommons;
6e880c60 362 bool fVerbose;
d696c285 363 bool fKeepRelocations;
d696c285
A
364 bool fWarnStabs;
365 bool fTraceDylibSearching;
366 bool fPause;
367 bool fStatistics;
368 bool fPrintOptions;
a61fdf0a
A
369 bool fSharedRegionEligible;
370 bool fPrintOrderFileStatistics;
371 bool fReadOnlyx86Stubs;
372 bool fPositionIndependentExecutable;
d9246299 373 bool fDisablePositionIndependentExecutable;
a61fdf0a
A
374 bool fMaxMinimumHeaderPad;
375 bool fDeadStripDylibs;
2f2f92e4
A
376 bool fAllowTextRelocs;
377 bool fWarnTextRelocs;
378 bool fUsingLazyDylibLinking;
379 bool fEncryptable;
55e3d2f6
A
380 bool fOrderData;
381 bool fMarkDeadStrippableDylib;
382 bool fMakeClassicDyldInfo;
383 bool fMakeCompressedDyldInfo;
384 bool fNoEHLabels;
385 bool fAllowCpuSubtypeMismatches;
fb24a050 386 bool fUseSimplifiedDylibReExports;
d9246299 387 bool fObjCABIVersion2POverride;
c2646906 388 std::vector<const char*> fInitialUndefines;
a61fdf0a 389 NameSet fAllowedUndefined;
69a49097 390 NameSet fWhyLive;
c2646906
A
391 std::vector<ExtraSection> fExtraSections;
392 std::vector<SectionAlignment> fSectionAlignments;
a61fdf0a
A
393 std::vector<OrderedSymbol> fOrderedSymbols;
394 std::vector<SegmentStart> fCustomSegmentAddresses;
55e3d2f6 395 std::vector<SegmentSize> fCustomSegmentSizes;
a61fdf0a
A
396 std::vector<SegmentProtect> fCustomSegmentProtections;
397 std::vector<DylibOverride> fDylibOverrides;
55e3d2f6 398 std::vector<const char*> fLLVMOptions;
c2646906
A
399 std::vector<const char*> fLibrarySearchPaths;
400 std::vector<const char*> fFrameworkSearchPaths;
6e880c60 401 std::vector<const char*> fSDKPaths;
a61fdf0a 402 bool fSaveTempFiles;
c2646906
A
403};
404
405
406
c2646906 407#endif // __OPTIONS__