1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2005-2007 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
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
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.
22 * @APPLE_LICENSE_HEADER_END@
30 #include <mach/machine.h>
33 #include <ext/hash_set>
35 #include "ObjectFile.h"
37 void throwf (const char* format
, ...) __attribute__ ((noreturn
));
39 class DynamicLibraryOptions
42 DynamicLibraryOptions() : fWeakImport(false), fReExport(false), fBundleLoader(false) {}
50 // The public interface to the Options class is the abstract representation of what work the linker
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).
61 Options(int argc
, const char* argv
[]);
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 UUIDMode
{ kUUIDNone
, kUUIDRandom
, kUUIDContent
};
74 enum LocalSymbolHandling
{ kLocalSymbolsAll
, kLocalSymbolsNone
, kLocalSymbolsSelectiveInclude
, kLocalSymbolsSelectiveExclude
};
80 DynamicLibraryOptions options
;
84 const char* segmentName
;
85 const char* sectionName
;
91 struct SectionAlignment
{
92 const char* segmentName
;
93 const char* sectionName
;
97 struct OrderedSymbol
{
98 const char* symbolName
;
99 const char* objectFileName
;
102 struct SegmentStart
{
107 struct SegmentProtect
{
113 struct DylibOverride
{
114 const char* installName
;
115 const char* useInstead
;
119 const ObjectFile::ReaderOptions
& readerOptions();
120 const char* getOutputFilePath();
121 std::vector
<FileInfo
>& getInputFiles();
123 cpu_type_t
architecture();
124 OutputKind
outputKind();
127 bool fullyLoadArchives();
128 NameSpace
nameSpace();
129 const char* installPath(); // only for kDynamicLibrary
130 uint32_t currentVersion(); // only for kDynamicLibrary
131 uint32_t compatibilityVersion(); // only for kDynamicLibrary
132 const char* entryName(); // only for kDynamicExecutable or kStaticExecutable
133 const char* executablePath();
134 uint64_t baseAddress();
135 bool keepPrivateExterns(); // only for kObjectFile
136 bool interposable(); // only for kDynamicLibrary
137 bool needsModuleTable(); // only for kDynamicLibrary
138 bool hasExportRestrictList();
139 bool allGlobalsAreDeadStripRoots();
140 bool shouldExport(const char*);
141 bool ignoreOtherArchInputFiles();
142 bool forceCpuSubtypeAll();
144 bool traceArchives();
145 DeadStripMode
deadStrip();
146 UndefinedTreatment
undefinedTreatment();
147 ObjectFile::ReaderOptions::VersionMin
macosxVersionMin();
148 bool messagesPrefixedWithArchitecture();
149 Treatment
picTreatment();
150 WeakReferenceMismatchTreatment
weakReferenceMismatchTreatment();
151 const char* umbrellaName();
152 std::vector
<const char*>& allowableClients();
153 const char* clientName();
154 const char* initFunctionName(); // only for kDynamicLibrary
155 const char* dotOutputFile();
156 uint64_t zeroPageSize();
157 bool hasCustomStack();
158 uint64_t customStackSize();
159 uint64_t customStackAddr();
160 bool hasExecutableStack();
161 std::vector
<const char*>& initialUndefines();
162 bool printWhyLive(const char* name
);
163 uint32_t minimumHeaderPad();
164 bool maxMminimumHeaderPad() { return fMaxMinimumHeaderPad
; }
165 std::vector
<ExtraSection
>& extraSections();
166 std::vector
<SectionAlignment
>& sectionAlignments();
167 CommonsMode
commonsMode();
169 bool keepRelocations();
170 FileInfo
findFile(const char* path
);
171 UUIDMode
getUUIDMode() { return fUUIDMode
; }
173 bool pauseAtEnd() { return fPause
; }
174 bool printStatistics() { return fStatistics
; }
175 bool printArchPrefix() { return fMessagesPrefixedWithArchitecture
; }
176 void gotoClassicLinker(int argc
, const char* argv
[]);
177 bool sharedRegionEligible() { return fSharedRegionEligible
; }
178 bool printOrderFileStatistics() { return fPrintOrderFileStatistics
; }
179 const char* dTraceScriptName() { return fDtraceScriptName
; }
180 bool dTrace() { return (fDtraceScriptName
!= NULL
); }
181 std::vector
<OrderedSymbol
>& orderedSymbols() { return fOrderedSymbols
; }
182 bool splitSeg() { return fSplitSegs
; }
183 uint64_t baseWritableAddress() { return fBaseWritableAddress
; }
184 std::vector
<SegmentStart
>& customSegmentAddresses() { return fCustomSegmentAddresses
; }
185 std::vector
<SegmentProtect
>& customSegmentProtections() { return fCustomSegmentProtections
; }
186 bool saveTempFiles() { return fSaveTempFiles
; }
187 const std::vector
<const char*>& rpaths() { return fRPaths
; }
188 bool readOnlyx86Stubs() { return fReadOnlyx86Stubs
; }
189 std::vector
<DylibOverride
>& dylibOverrides() { return fDylibOverrides
; }
190 const char* generatedMapPath() { return fMapPath
; }
191 bool positionIndependentExecutable() { return fPositionIndependentExecutable
; }
192 Options::FileInfo
findFileUsingPaths(const char* path
);
193 bool deadStripDylibs() { return fDeadStripDylibs
; }
194 bool allowedUndefined(const char* name
) { return ( fAllowedUndefined
.find(name
) != fAllowedUndefined
.end() ); }
195 bool someAllowedUndefines() { return (fAllowedUndefined
.size() != 0); }
196 LocalSymbolHandling
localSymbolHandling() { return fLocalSymbolHandling
; }
197 bool keepLocalSymbol(const char* symbolName
);
198 bool emitWarnings() { return !fSuppressWarnings
; }
204 bool operator()(const char* left
, const char* right
) const { return (strcmp(left
, right
) == 0); }
207 typedef __gnu_cxx::hash_set
<const char*, __gnu_cxx::hash
<const char*>, CStringEquals
> NameSet
;
208 enum ExportMode
{ kExportDefault
, kExportSome
, kDontExportSome
};
209 enum LibrarySearchMode
{ kSearchDylibAndArchiveInEachDir
, kSearchAllDirsForDylibsThenAllDirsForArchives
};
211 class SetWithWildcards
{
213 void insert(const char*);
214 bool contains(const char*);
215 NameSet::iterator
regularBegin() { return fRegular
.begin(); }
216 NameSet::iterator
regularEnd() { return fRegular
.end(); }
218 bool hasWildCards(const char*);
219 bool wildCardMatch(const char* pattern
, const char* candidate
);
220 bool inCharRange(const char*& range
, unsigned char c
);
223 std::vector
<const char*> fWildCard
;
227 void parse(int argc
, const char* argv
[]);
228 void checkIllegalOptionCombinations();
229 void buildSearchPaths(int argc
, const char* argv
[]);
230 void parseArch(const char* architecture
);
231 FileInfo
findLibrary(const char* rootName
);
232 FileInfo
findFramework(const char* frameworkName
);
233 FileInfo
findFramework(const char* rootName
, const char* suffix
);
234 bool checkForFile(const char* format
, const char* dir
, const char* rootName
,
236 uint32_t parseVersionNumber(const char*);
237 void parseSectionOrderFile(const char* segment
, const char* section
, const char* path
);
238 void parseOrderFile(const char* path
, bool cstring
);
239 void addSection(const char* segment
, const char* section
, const char* path
);
240 void addSubLibrary(const char* name
);
241 void loadFileList(const char* fileOfPaths
);
242 uint64_t parseAddress(const char* addr
);
243 void loadExportFile(const char* fileOfExports
, const char* option
, SetWithWildcards
& set
);
244 void parseAliasFile(const char* fileOfAliases
);
245 void parsePreCommandLineEnvironmentSettings();
246 void parsePostCommandLineEnvironmentSettings();
247 void setUndefinedTreatment(const char* treatment
);
248 void setVersionMin(const char* version
);
249 void setWeakReferenceMismatchTreatment(const char* treatment
);
250 void addDylibOverride(const char* paths
);
251 void addSectionAlignment(const char* segment
, const char* section
, const char* alignment
);
252 CommonsMode
parseCommonsTreatment(const char* mode
);
253 Treatment
parseTreatment(const char* treatment
);
254 void reconfigureDefaults();
255 void checkForClassic(int argc
, const char* argv
[]);
256 void parseSegAddrTable(const char* segAddrPath
, const char* installPath
);
257 void addLibrary(const FileInfo
& info
);
258 void warnObsolete(const char* arg
);
259 uint32_t parseProtection(const char* prot
);
262 ObjectFile::ReaderOptions fReaderOptions
;
263 const char* fOutputFile
;
264 std::vector
<Options::FileInfo
> fInputFiles
;
265 cpu_type_t fArchitecture
;
266 OutputKind fOutputKind
;
269 bool fKeepPrivateExterns
;
271 bool fNeedsModuleTable
;
272 bool fIgnoreOtherArchFiles
;
273 bool fForceSubtypeAll
;
274 DeadStripMode fDeadStrip
;
275 NameSpace fNameSpace
;
276 uint32_t fDylibCompatVersion
;
277 uint32_t fDylibCurrentVersion
;
278 const char* fDylibInstallName
;
279 const char* fFinalName
;
280 const char* fEntryName
;
281 uint64_t fBaseAddress
;
282 uint64_t fBaseWritableAddress
;
284 SetWithWildcards fExportSymbols
;
285 SetWithWildcards fDontExportSymbols
;
286 ExportMode fExportMode
;
287 LibrarySearchMode fLibrarySearchMode
;
288 UndefinedTreatment fUndefinedTreatment
;
289 bool fMessagesPrefixedWithArchitecture
;
290 WeakReferenceMismatchTreatment fWeakReferenceMismatchTreatment
;
291 std::vector
<const char*> fSubUmbellas
;
292 std::vector
<const char*> fSubLibraries
;
293 std::vector
<const char*> fAllowableClients
;
294 std::vector
<const char*> fRPaths
;
295 const char* fClientName
;
296 const char* fUmbrellaName
;
297 const char* fInitFunctionName
;
298 const char* fDotOutputFile
;
299 const char* fExecutablePath
;
300 const char* fBundleLoader
;
301 const char* fDtraceScriptName
;
302 const char* fSegAddrTablePath
;
303 const char* fMapPath
;
304 uint64_t fZeroPageSize
;
307 bool fExecutableStack
;
308 uint32_t fMinimumHeaderPad
;
309 CommonsMode fCommonsMode
;
311 SetWithWildcards fLocalSymbolsIncluded
;
312 SetWithWildcards fLocalSymbolsExcluded
;
313 LocalSymbolHandling fLocalSymbolHandling
;
316 bool fKeepRelocations
;
318 bool fTraceDylibSearching
;
322 bool fSharedRegionEligible
;
323 bool fPrintOrderFileStatistics
;
324 bool fReadOnlyx86Stubs
;
325 bool fPositionIndependentExecutable
;
326 bool fMaxMinimumHeaderPad
;
327 bool fDeadStripDylibs
;
328 bool fSuppressWarnings
;
329 std::vector
<const char*> fInitialUndefines
;
330 NameSet fAllowedUndefined
;
332 std::vector
<ExtraSection
> fExtraSections
;
333 std::vector
<SectionAlignment
> fSectionAlignments
;
334 std::vector
<OrderedSymbol
> fOrderedSymbols
;
335 std::vector
<SegmentStart
> fCustomSegmentAddresses
;
336 std::vector
<SegmentProtect
> fCustomSegmentProtections
;
337 std::vector
<DylibOverride
> fDylibOverrides
;
339 std::vector
<const char*> fLibrarySearchPaths
;
340 std::vector
<const char*> fFrameworkSearchPaths
;
341 std::vector
<const char*> fSDKPaths
;
347 #endif // __OPTIONS__