]> git.saurik.com Git - apple/ld64.git/blob - FireOpal/src/Options.h
ld64-85.2.1.tar.gz
[apple/ld64.git] / FireOpal / src / Options.h
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2005-2007 Apple Inc. All rights reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
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.
13 *
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.
21 *
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
32 #include <vector>
33 #include <ext/hash_set>
34
35 #include "ObjectFile.h"
36
37 extern void throwf (const char* format, ...) __attribute__ ((noreturn));
38 extern void warning(const char* format, ...);
39
40 class DynamicLibraryOptions
41 {
42 public:
43 DynamicLibraryOptions() : fWeakImport(false), fReExport(false), fBundleLoader(false), fLazyLoad(false) {}
44
45 bool fWeakImport;
46 bool fReExport;
47 bool fBundleLoader;
48 bool fLazyLoad;
49 };
50
51 //
52 // The public interface to the Options class is the abstract representation of what work the linker
53 // should do.
54 //
55 // This abstraction layer will make it easier to support a future where the linker is a shared library
56 // invoked directly from Xcode. The target settings in Xcode would be used to directly construct an Options
57 // object (without building a command line which is then parsed).
58 //
59 //
60 class Options
61 {
62 public:
63 Options(int argc, const char* argv[]);
64 ~Options();
65
66 enum OutputKind { kDynamicExecutable, kStaticExecutable, kDynamicLibrary, kDynamicBundle, kObjectFile, kDyld };
67 enum NameSpace { kTwoLevelNameSpace, kFlatNameSpace, kForceFlatNameSpace };
68 // Standard treatment for many options.
69 enum Treatment { kError, kWarning, kSuppress, kNULL, kInvalid };
70 enum UndefinedTreatment { kUndefinedError, kUndefinedWarning, kUndefinedSuppress, kUndefinedDynamicLookup };
71 enum WeakReferenceMismatchTreatment { kWeakReferenceMismatchError, kWeakReferenceMismatchWeak,
72 kWeakReferenceMismatchNonWeak };
73 enum CommonsMode { kCommonsIgnoreDylibs, kCommonsOverriddenByDylibs, kCommonsConflictsDylibsError };
74 enum DeadStripMode { kDeadStripOff, kDeadStripOn, kDeadStripOnPlusUnusedInits };
75 enum UUIDMode { kUUIDNone, kUUIDRandom, kUUIDContent };
76 enum LocalSymbolHandling { kLocalSymbolsAll, kLocalSymbolsNone, kLocalSymbolsSelectiveInclude, kLocalSymbolsSelectiveExclude };
77
78 struct FileInfo {
79 const char* path;
80 uint64_t fileLen;
81 time_t modTime;
82 DynamicLibraryOptions options;
83 };
84
85 struct ExtraSection {
86 const char* segmentName;
87 const char* sectionName;
88 const char* path;
89 const uint8_t* data;
90 uint64_t dataLen;
91 };
92
93 struct SectionAlignment {
94 const char* segmentName;
95 const char* sectionName;
96 uint8_t alignment;
97 };
98
99 struct OrderedSymbol {
100 const char* symbolName;
101 const char* objectFileName;
102 };
103
104 struct SegmentStart {
105 const char* name;
106 uint64_t address;
107 };
108
109 struct SegmentProtect {
110 const char* name;
111 uint32_t max;
112 uint32_t init;
113 };
114
115 struct DylibOverride {
116 const char* installName;
117 const char* useInstead;
118 };
119
120
121 const ObjectFile::ReaderOptions& readerOptions();
122 const char* getOutputFilePath();
123 std::vector<FileInfo>& getInputFiles();
124
125 cpu_type_t architecture() { return fArchitecture; }
126 bool preferSubArchitecture() { return fHasPreferredSubType; }
127 cpu_subtype_t subArchitecture() { return fSubArchitecture; }
128 OutputKind outputKind();
129 bool prebind();
130 bool bindAtLoad();
131 bool fullyLoadArchives();
132 NameSpace nameSpace();
133 const char* installPath(); // only for kDynamicLibrary
134 uint32_t currentVersion(); // only for kDynamicLibrary
135 uint32_t compatibilityVersion(); // only for kDynamicLibrary
136 const char* entryName(); // only for kDynamicExecutable or kStaticExecutable
137 const char* executablePath();
138 uint64_t baseAddress();
139 bool keepPrivateExterns(); // only for kObjectFile
140 bool needsModuleTable(); // only for kDynamicLibrary
141 bool interposable(const char* name);
142 bool hasExportRestrictList();
143 bool hasExportMaskList();
144 bool hasWildCardExportRestrictList();
145 bool allGlobalsAreDeadStripRoots();
146 bool shouldExport(const char*);
147 bool ignoreOtherArchInputFiles();
148 bool forceCpuSubtypeAll();
149 bool traceDylibs();
150 bool traceArchives();
151 DeadStripMode deadStrip();
152 UndefinedTreatment undefinedTreatment();
153 ObjectFile::ReaderOptions::VersionMin macosxVersionMin();
154 bool messagesPrefixedWithArchitecture();
155 Treatment picTreatment();
156 WeakReferenceMismatchTreatment weakReferenceMismatchTreatment();
157 const char* umbrellaName();
158 std::vector<const char*>& allowableClients();
159 const char* clientName();
160 const char* initFunctionName(); // only for kDynamicLibrary
161 const char* dotOutputFile();
162 uint64_t zeroPageSize();
163 bool hasCustomStack();
164 uint64_t customStackSize();
165 uint64_t customStackAddr();
166 bool hasExecutableStack();
167 std::vector<const char*>& initialUndefines();
168 bool printWhyLive(const char* name);
169 uint32_t minimumHeaderPad();
170 bool maxMminimumHeaderPad() { return fMaxMinimumHeaderPad; }
171 std::vector<ExtraSection>& extraSections();
172 std::vector<SectionAlignment>& sectionAlignments();
173 CommonsMode commonsMode();
174 bool warnCommons();
175 bool keepRelocations();
176 FileInfo findFile(const char* path);
177 UUIDMode getUUIDMode() { return fUUIDMode; }
178 bool warnStabs();
179 bool pauseAtEnd() { return fPause; }
180 bool printStatistics() { return fStatistics; }
181 bool printArchPrefix() { return fMessagesPrefixedWithArchitecture; }
182 void gotoClassicLinker(int argc, const char* argv[]);
183 bool sharedRegionEligible() { return fSharedRegionEligible; }
184 bool printOrderFileStatistics() { return fPrintOrderFileStatistics; }
185 const char* dTraceScriptName() { return fDtraceScriptName; }
186 bool dTrace() { return (fDtraceScriptName != NULL); }
187 std::vector<OrderedSymbol>& orderedSymbols() { return fOrderedSymbols; }
188 bool splitSeg() { return fSplitSegs; }
189 uint64_t baseWritableAddress() { return fBaseWritableAddress; }
190 std::vector<SegmentStart>& customSegmentAddresses() { return fCustomSegmentAddresses; }
191 std::vector<SegmentProtect>& customSegmentProtections() { return fCustomSegmentProtections; }
192 bool saveTempFiles() { return fSaveTempFiles; }
193 const std::vector<const char*>& rpaths() { return fRPaths; }
194 bool readOnlyx86Stubs() { return fReadOnlyx86Stubs; }
195 bool slowx86Stubs() { return fReaderOptions.fSlowx86Stubs; }
196 std::vector<DylibOverride>& dylibOverrides() { return fDylibOverrides; }
197 const char* generatedMapPath() { return fMapPath; }
198 bool positionIndependentExecutable() { return fPositionIndependentExecutable; }
199 Options::FileInfo findFileUsingPaths(const char* path);
200 bool deadStripDylibs() { return fDeadStripDylibs; }
201 bool allowedUndefined(const char* name) { return ( fAllowedUndefined.find(name) != fAllowedUndefined.end() ); }
202 bool someAllowedUndefines() { return (fAllowedUndefined.size() != 0); }
203 LocalSymbolHandling localSymbolHandling() { return fLocalSymbolHandling; }
204 bool keepLocalSymbol(const char* symbolName);
205 bool allowTextRelocs() { return fAllowTextRelocs; }
206 bool warnAboutTextRelocs() { return fWarnTextRelocs; }
207 bool usingLazyDylibLinking() { return fUsingLazyDylibLinking; }
208 bool verbose() { return fVerbose; }
209 bool makeEncryptable() { return fEncryptable; }
210 std::vector<const char*>& llvmOptions() { return fLLVMOptions; }
211
212 private:
213 class CStringEquals
214 {
215 public:
216 bool operator()(const char* left, const char* right) const { return (strcmp(left, right) == 0); }
217 };
218
219 typedef __gnu_cxx::hash_set<const char*, __gnu_cxx::hash<const char*>, CStringEquals> NameSet;
220 enum ExportMode { kExportDefault, kExportSome, kDontExportSome };
221 enum LibrarySearchMode { kSearchDylibAndArchiveInEachDir, kSearchAllDirsForDylibsThenAllDirsForArchives };
222 enum InterposeMode { kInterposeNone, kInterposeAllExternal, kInterposeSome };
223
224 class SetWithWildcards {
225 public:
226 void insert(const char*);
227 bool contains(const char*);
228 bool hasWildCards() { return !fWildCard.empty(); }
229 NameSet::iterator regularBegin() { return fRegular.begin(); }
230 NameSet::iterator regularEnd() { return fRegular.end(); }
231 private:
232 static bool hasWildCards(const char*);
233 bool wildCardMatch(const char* pattern, const char* candidate);
234 bool inCharRange(const char*& range, unsigned char c);
235
236 NameSet fRegular;
237 std::vector<const char*> fWildCard;
238 };
239
240
241 void parse(int argc, const char* argv[]);
242 void checkIllegalOptionCombinations();
243 void buildSearchPaths(int argc, const char* argv[]);
244 void parseArch(const char* architecture);
245 FileInfo findLibrary(const char* rootName, bool dylibsOnly=false);
246 FileInfo findFramework(const char* frameworkName);
247 FileInfo findFramework(const char* rootName, const char* suffix);
248 bool checkForFile(const char* format, const char* dir, const char* rootName,
249 FileInfo& result);
250 uint32_t parseVersionNumber(const char*);
251 void parseSectionOrderFile(const char* segment, const char* section, const char* path);
252 void parseOrderFile(const char* path, bool cstring);
253 void addSection(const char* segment, const char* section, const char* path);
254 void addSubLibrary(const char* name);
255 void loadFileList(const char* fileOfPaths);
256 uint64_t parseAddress(const char* addr);
257 void loadExportFile(const char* fileOfExports, const char* option, SetWithWildcards& set);
258 void parseAliasFile(const char* fileOfAliases);
259 void parsePreCommandLineEnvironmentSettings();
260 void parsePostCommandLineEnvironmentSettings();
261 void setUndefinedTreatment(const char* treatment);
262 void setMacOSXVersionMin(const char* version);
263 void setIPhoneVersionMin(const char* version);
264 void setWeakReferenceMismatchTreatment(const char* treatment);
265 void addDylibOverride(const char* paths);
266 void addSectionAlignment(const char* segment, const char* section, const char* alignment);
267 CommonsMode parseCommonsTreatment(const char* mode);
268 Treatment parseTreatment(const char* treatment);
269 void reconfigureDefaults();
270 void checkForClassic(int argc, const char* argv[]);
271 void parseSegAddrTable(const char* segAddrPath, const char* installPath);
272 void addLibrary(const FileInfo& info);
273 void warnObsolete(const char* arg);
274 uint32_t parseProtection(const char* prot);
275
276
277 ObjectFile::ReaderOptions fReaderOptions;
278 const char* fOutputFile;
279 std::vector<Options::FileInfo> fInputFiles;
280 cpu_type_t fArchitecture;
281 cpu_subtype_t fSubArchitecture;
282 OutputKind fOutputKind;
283 bool fHasPreferredSubType;
284 bool fPrebind;
285 bool fBindAtLoad;
286 bool fKeepPrivateExterns;
287 bool fNeedsModuleTable;
288 bool fIgnoreOtherArchFiles;
289 bool fForceSubtypeAll;
290 InterposeMode fInterposeMode;
291 DeadStripMode fDeadStrip;
292 NameSpace fNameSpace;
293 uint32_t fDylibCompatVersion;
294 uint32_t fDylibCurrentVersion;
295 const char* fDylibInstallName;
296 const char* fFinalName;
297 const char* fEntryName;
298 uint64_t fBaseAddress;
299 uint64_t fBaseWritableAddress;
300 bool fSplitSegs;
301 SetWithWildcards fExportSymbols;
302 SetWithWildcards fDontExportSymbols;
303 SetWithWildcards fInterposeList;
304 ExportMode fExportMode;
305 LibrarySearchMode fLibrarySearchMode;
306 UndefinedTreatment fUndefinedTreatment;
307 bool fMessagesPrefixedWithArchitecture;
308 WeakReferenceMismatchTreatment fWeakReferenceMismatchTreatment;
309 std::vector<const char*> fSubUmbellas;
310 std::vector<const char*> fSubLibraries;
311 std::vector<const char*> fAllowableClients;
312 std::vector<const char*> fRPaths;
313 const char* fClientName;
314 const char* fUmbrellaName;
315 const char* fInitFunctionName;
316 const char* fDotOutputFile;
317 const char* fExecutablePath;
318 const char* fBundleLoader;
319 const char* fDtraceScriptName;
320 const char* fSegAddrTablePath;
321 const char* fMapPath;
322 uint64_t fZeroPageSize;
323 uint64_t fStackSize;
324 uint64_t fStackAddr;
325 bool fExecutableStack;
326 uint32_t fMinimumHeaderPad;
327 CommonsMode fCommonsMode;
328 UUIDMode fUUIDMode;
329 SetWithWildcards fLocalSymbolsIncluded;
330 SetWithWildcards fLocalSymbolsExcluded;
331 LocalSymbolHandling fLocalSymbolHandling;
332 bool fWarnCommons;
333 bool fVerbose;
334 bool fKeepRelocations;
335 bool fWarnStabs;
336 bool fTraceDylibSearching;
337 bool fPause;
338 bool fStatistics;
339 bool fPrintOptions;
340 bool fSharedRegionEligible;
341 bool fPrintOrderFileStatistics;
342 bool fReadOnlyx86Stubs;
343 bool fPositionIndependentExecutable;
344 bool fMaxMinimumHeaderPad;
345 bool fDeadStripDylibs;
346 bool fAllowTextRelocs;
347 bool fWarnTextRelocs;
348 bool fUsingLazyDylibLinking;
349 bool fEncryptable;
350 std::vector<const char*> fInitialUndefines;
351 NameSet fAllowedUndefined;
352 NameSet fWhyLive;
353 std::vector<ExtraSection> fExtraSections;
354 std::vector<SectionAlignment> fSectionAlignments;
355 std::vector<OrderedSymbol> fOrderedSymbols;
356 std::vector<SegmentStart> fCustomSegmentAddresses;
357 std::vector<SegmentProtect> fCustomSegmentProtections;
358 std::vector<DylibOverride> fDylibOverrides;
359 std::vector<const char*> fLLVMOptions;
360 std::vector<const char*> fLibrarySearchPaths;
361 std::vector<const char*> fFrameworkSearchPaths;
362 std::vector<const char*> fSDKPaths;
363 bool fSaveTempFiles;
364 };
365
366
367
368 #endif // __OPTIONS__