]> git.saurik.com Git - apple/ld64.git/blob - src/Options.h
c8ef861235e9cbb4c8f833523eb7956594fe28fc
[apple/ld64.git] / src / Options.h
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2005 Apple Computer, 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 void throwf (const char* format, ...) __attribute__ ((noreturn));
38
39 class DynamicLibraryOptions
40 {
41 public:
42 DynamicLibraryOptions() : fWeakImport(false), fReExport(false), fInstallPathOverride(NULL) {}
43
44 bool fWeakImport;
45 bool fReExport;
46 const char* fInstallPathOverride;
47 };
48
49 //
50 // The public interface to the Options class is the abstract representation of what work the linker
51 // should do.
52 //
53 // This abstraction layer will make it easier to support a future where the linker is a shared library
54 // invoked directly from Xcode. The target settings in Xcode would be used to directly construct an Options
55 // object (without building a command line which is then parsed).
56 //
57 //
58 class Options
59 {
60 public:
61 Options(int argc, const char* argv[]);
62 ~Options();
63
64 enum OutputKind { kDynamicExecutable, kStaticExecutable, kDynamicLibrary, kDynamicBundle, kObjectFile, kDyld };
65 enum NameSpace { kTwoLevelNameSpace, kFlatNameSpace, kForceFlatNameSpace };
66 // Standard treatment for many options.
67 enum Treatment { kError, kWarning, kSuppress, kNULL, kInvalid };
68 enum UndefinedTreatment { kUndefinedError, kUndefinedWarning, kUndefinedSuppress, kUndefinedDynamicLookup };
69 enum WeakReferenceMismatchTreatment { kWeakReferenceMismatchError, kWeakReferenceMismatchWeak,
70 kWeakReferenceMismatchNonWeak };
71 enum CommonsMode { kCommonsIgnoreDylibs, kCommonsOverriddenByDylibs, kCommonsConflictsDylibsError };
72 enum DeadStripMode { kDeadStripOff, kDeadStripOn, kDeadStripOnPlusUnusedInits };
73 enum VersionMin { k10_1, k10_2, k10_3, k10_4, k10_5 };
74
75 struct FileInfo {
76 const char* path;
77 uint64_t fileLen;
78 time_t modTime;
79 DynamicLibraryOptions options;
80 };
81
82 struct ExtraSection {
83 const char* segmentName;
84 const char* sectionName;
85 const char* path;
86 const uint8_t* data;
87 uint64_t dataLen;
88 };
89
90 struct SectionAlignment {
91 const char* segmentName;
92 const char* sectionName;
93 uint8_t alignment;
94 };
95
96 const ObjectFile::ReaderOptions& readerOptions();
97 const char* getOutputFilePath();
98 std::vector<FileInfo>& getInputFiles();
99
100 cpu_type_t architecture();
101 OutputKind outputKind();
102 bool stripLocalSymbols();
103 bool prebind();
104 bool bindAtLoad();
105 bool fullyLoadArchives();
106 NameSpace nameSpace();
107 const char* installPath(); // only for kDynamicLibrary
108 uint32_t currentVersion(); // only for kDynamicLibrary
109 uint32_t compatibilityVersion(); // only for kDynamicLibrary
110 const char* entryName(); // only for kDynamicExecutable or kStaticExecutable
111 const char* executablePath();
112 uint64_t baseAddress();
113 bool keepPrivateExterns(); // only for kObjectFile
114 bool interposable(); // only for kDynamicLibrary
115 bool hasExportRestrictList();
116 bool shouldExport(const char*);
117 bool ignoreOtherArchInputFiles();
118 bool forceCpuSubtypeAll();
119 bool traceDylibs();
120 bool traceArchives();
121 DeadStripMode deadStrip();
122 UndefinedTreatment undefinedTreatment();
123 VersionMin macosxVersionMin();
124 bool messagesPrefixedWithArchitecture();
125 Treatment picTreatment();
126 WeakReferenceMismatchTreatment weakReferenceMismatchTreatment();
127 Treatment multipleDefinitionsInDylibs();
128 Treatment overridingDefinitionInDependentDylib();
129 bool warnOnMultipleDefinitionsInObjectFiles();
130 const char* umbrellaName();
131 std::vector<const char*>& allowableClients();
132 const char* clientName();
133 const char* initFunctionName(); // only for kDynamicLibrary
134 const char* dotOutputFile();
135 uint64_t zeroPageSize();
136 bool hasCustomStack();
137 uint64_t customStackSize();
138 uint64_t customStackAddr();
139 bool hasExecutableStack();
140 std::vector<const char*>& initialUndefines();
141 uint32_t minimumHeaderPad();
142 std::vector<ExtraSection>& extraSections();
143 std::vector<SectionAlignment>& sectionAlignments();
144 CommonsMode commonsMode();
145 bool warnCommons();
146 bool keepRelocations();
147 std::vector<const char*>& traceSymbols();
148 FileInfo findFile(const char* path);
149 bool emitUUID();
150 bool warnStabs();
151 bool pauseAtEnd() { return fPause; }
152 bool printStatistics() { return fStatistics; }
153
154 private:
155 class CStringEquals
156 {
157 public:
158 bool operator()(const char* left, const char* right) const { return (strcmp(left, right) == 0); }
159 };
160
161 typedef __gnu_cxx::hash_set<const char*, __gnu_cxx::hash<const char*>, CStringEquals> NameSet;
162 enum ExportMode { kExportDefault, kExportSome, kDontExportSome };
163 enum LibrarySearchMode { kSearchDylibAndArchiveInEachDir, kSearchAllDirsForDylibsThenAllDirsForArchives };
164
165 void parse(int argc, const char* argv[]);
166 void checkIllegalOptionCombinations();
167 void buildSearchPaths(int argc, const char* argv[]);
168 void parseArch(const char* architecture);
169 FileInfo findLibrary(const char* rootName);
170 FileInfo findFramework(const char* rootName);
171 bool checkForFile(const char* format, const char* dir, const char* rootName,
172 FileInfo& result);
173 uint32_t parseVersionNumber(const char*);
174 void parseSectionOrderFile(const char* segment, const char* section, const char* path);
175 void addSection(const char* segment, const char* section, const char* path);
176 void addSubLibrary(const char* name);
177 void loadFileList(const char* fileOfPaths);
178 uint64_t parseAddress(const char* addr);
179 void loadExportFile(const char* fileOfExports, const char* option, NameSet& set);
180 void parsePreCommandLineEnvironmentSettings();
181 void parsePostCommandLineEnvironmentSettings();
182 void setUndefinedTreatment(const char* treatment);
183 void setVersionMin(const char* version);
184 void setWeakReferenceMismatchTreatment(const char* treatment);
185 void setDylibInstallNameOverride(const char* paths);
186 void addSectionAlignment(const char* segment, const char* section, const char* alignment);
187 CommonsMode parseCommonsTreatment(const char* mode);
188 Treatment parseTreatment(const char* treatment);
189
190
191
192 ObjectFile::ReaderOptions fReaderOptions;
193 const char* fOutputFile;
194 std::vector<Options::FileInfo> fInputFiles;
195 cpu_type_t fArchitecture;
196 OutputKind fOutputKind;
197 bool fPrebind;
198 bool fBindAtLoad;
199 bool fStripLocalSymbols;
200 bool fKeepPrivateExterns;
201 bool fInterposable;
202 bool fIgnoreOtherArchFiles;
203 bool fForceSubtypeAll;
204 DeadStripMode fDeadStrip;
205 VersionMin fVersionMin;
206 NameSpace fNameSpace;
207 uint32_t fDylibCompatVersion;
208 uint32_t fDylibCurrentVersion;
209 const char* fDylibInstallName;
210 const char* fEntryName;
211 uint64_t fBaseAddress;
212 NameSet fExportSymbols;
213 NameSet fDontExportSymbols;
214 ExportMode fExportMode;
215 LibrarySearchMode fLibrarySearchMode;
216 UndefinedTreatment fUndefinedTreatment;
217 bool fMessagesPrefixedWithArchitecture;
218 Treatment fPICTreatment;
219 WeakReferenceMismatchTreatment fWeakReferenceMismatchTreatment;
220 Treatment fMultiplyDefinedDynamic;
221 Treatment fMultiplyDefinedUnused;
222 bool fWarnOnMultiplyDefined;
223 std::vector<const char*> fSubUmbellas;
224 std::vector<const char*> fSubLibraries;
225 std::vector<const char*> fAllowableClients;
226 const char* fClientName;
227 const char* fUmbrellaName;
228 const char* fInitFunctionName;
229 const char* fDotOutputFile;
230 const char* fExecutablePath;
231 uint64_t fZeroPageSize;
232 uint64_t fStackSize;
233 uint64_t fStackAddr;
234 bool fExecutableStack;
235 uint32_t fMinimumHeaderPad;
236 CommonsMode fCommonsMode;
237 bool fWarnCommons;
238 bool fVerbose;
239 bool fKeepRelocations;
240 bool fEmitUUID;
241 bool fWarnStabs;
242 bool fTraceDylibSearching;
243 bool fPause;
244 bool fStatistics;
245 bool fPrintOptions;
246 std::vector<const char*> fInitialUndefines;
247 std::vector<const char*> fTraceSymbols;
248 unsigned long fLimitUndefinedSymbols;
249 std::vector<ExtraSection> fExtraSections;
250 std::vector<SectionAlignment> fSectionAlignments;
251
252 std::vector<const char*> fLibrarySearchPaths;
253 std::vector<const char*> fFrameworkSearchPaths;
254 std::vector<const char*> fSDKPaths;
255 bool fAllowStackExecute;
256
257 };
258
259
260
261
262 #endif // __OPTIONS__