]>
Commit | Line | Data |
---|---|---|
a645023d A |
1 | /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-* |
2 | * | |
afe874b1 | 3 | * Copyright (c) 2009-2011 Apple Inc. All rights reserved. |
a645023d A |
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 __INPUT_FILES_H__ | |
26 | #define __INPUT_FILES_H__ | |
27 | ||
ebf6f434 A |
28 | #define HAVE_PTHREADS 1 |
29 | ||
a645023d A |
30 | #include <stdlib.h> |
31 | #include <sys/types.h> | |
32 | #include <sys/stat.h> | |
33 | #include <sys/mman.h> | |
34 | #include <sys/sysctl.h> | |
35 | #include <fcntl.h> | |
36 | #include <errno.h> | |
37 | #include <limits.h> | |
38 | #include <unistd.h> | |
39 | #include <mach/mach_time.h> | |
40 | #include <mach/vm_statistics.h> | |
41 | #include <mach/mach_init.h> | |
42 | #include <mach/mach_host.h> | |
43 | #include <dlfcn.h> | |
44 | #include <mach-o/dyld.h> | |
ebf6f434 A |
45 | #if HAVE_PTHREADS |
46 | #include <pthread.h> | |
47 | #endif | |
a645023d A |
48 | |
49 | #include <vector> | |
50 | ||
51 | #include "Options.h" | |
52 | #include "ld.hpp" | |
53 | ||
54 | namespace ld { | |
55 | namespace tool { | |
56 | ||
57 | class InputFiles : public ld::dylib::File::DylibHandler | |
58 | { | |
59 | public: | |
60 | InputFiles(Options& opts, const char** archName); | |
61 | ||
62 | // implementation from ld::dylib::File::DylibHandler | |
63 | virtual ld::dylib::File* findDylib(const char* installPath, const char* fromPath); | |
64 | ||
65 | // iterates all atoms in initial files | |
f80fe69f | 66 | void forEachInitialAtom(ld::File::AtomHandler&, ld::Internal& state); |
a645023d | 67 | // searches libraries for name |
afe874b1 A |
68 | bool searchLibraries(const char* name, bool searchDylibs, bool searchArchives, |
69 | bool dataSymbolOnly, ld::File::AtomHandler&) const; | |
a645023d A |
70 | // see if any linked dylibs export a weak def of symbol |
71 | bool searchWeakDefInDylib(const char* name) const; | |
72 | // copy dylibs to link with in command line order | |
73 | void dylibs(ld::Internal& state); | |
74 | ||
75 | bool inferredArch() const { return _inferredArch; } | |
76 | ||
599556ff | 77 | void addLinkerOptionLibraries(ld::Internal& state, ld::File::AtomHandler& handler); |
9543cb2f A |
78 | void createIndirectDylibs(); |
79 | ||
a645023d | 80 | // for -print_statistics |
ebf6f434 A |
81 | volatile int64_t _totalObjectSize; |
82 | volatile int64_t _totalArchiveSize; | |
83 | volatile int32_t _totalObjectLoaded; | |
84 | volatile int32_t _totalArchivesLoaded; | |
85 | volatile int32_t _totalDylibsLoaded; | |
a645023d A |
86 | |
87 | ||
88 | private: | |
89 | void inferArchitecture(Options& opts, const char** archName); | |
90 | const char* fileArch(const uint8_t* p, unsigned len); | |
afe874b1 | 91 | ld::File* makeFile(const Options::FileInfo& info, bool indirectDylib); |
ebf6f434 | 92 | ld::File* addDylib(ld::dylib::File* f, const Options::FileInfo& info); |
a645023d A |
93 | void logTraceInfo (const char* format, ...) const; |
94 | void logDylib(ld::File*, bool indirect); | |
95 | void logArchive(ld::File*) const; | |
f80fe69f | 96 | void markExplicitlyLinkedDylibs(); |
a645023d A |
97 | void checkDylibClientRestrictions(ld::dylib::File*); |
98 | void createOpaqueFileSections(); | |
f80fe69f | 99 | bool libraryAlreadyLoaded(const char* path); |
ec29ba20 A |
100 | bool frameworkAlreadyLoaded(const char* path, const char* frameworkName); |
101 | ||
ebf6f434 | 102 | // for pipelined linking |
f80fe69f | 103 | void waitForInputFiles(); |
ebf6f434 A |
104 | static void waitForInputFiles(InputFiles *inputFiles); |
105 | ||
106 | // for threaded input file processing | |
107 | void parseWorkerThread(); | |
108 | static void parseWorkerThread(InputFiles *inputFiles); | |
109 | void startThread(void (*threadFunc)(InputFiles *)) const; | |
a645023d | 110 | |
d425e388 | 111 | typedef std::unordered_map<const char*, ld::dylib::File*, CStringHash, CStringEquals> InstallNameToDylib; |
a645023d A |
112 | |
113 | const Options& _options; | |
114 | std::vector<ld::File*> _inputFiles; | |
115 | mutable std::set<class ld::File*> _archiveFilesLogged; | |
116 | InstallNameToDylib _installPathToDylibs; | |
117 | std::set<ld::dylib::File*> _allDylibs; | |
118 | ld::dylib::File* _bundleLoader; | |
a645023d | 119 | bool _inferredArch; |
ebf6f434 A |
120 | struct strcompclass { |
121 | bool operator() (const char *a, const char *b) const { return ::strcmp(a, b) < 0; } | |
122 | }; | |
123 | ||
124 | // for threaded input file processing | |
125 | #if HAVE_PTHREADS | |
126 | pthread_mutex_t _parseLock; | |
127 | pthread_cond_t _parseWorkReady; // used by parse threads to block for work | |
128 | pthread_cond_t _newFileAvailable; // used by main thread to block for parsed input files | |
129 | int _availableWorkers; // number of remaining unstarted parse threads | |
130 | int _idleWorkers; // number of running parse threads that are idle | |
131 | int _neededFileSlot; // input file the resolver is currently blocked waiting for | |
132 | int _parseCursor; // slot to begin searching for a file to parse | |
133 | int _availableInputFiles; // number of input fileinfos with readyToParse==true | |
134 | #endif | |
135 | const char * _exception; // passes an exception message from parse thread to main thread | |
136 | int _remainingInputFiles; // number of input files still to parse | |
137 | ||
138 | ld::File::Ordinal _indirectDylibOrdinal; | |
f80fe69f | 139 | ld::File::Ordinal _linkerOptionOrdinal; |
ebf6f434 A |
140 | |
141 | class LibraryInfo { | |
142 | ld::File* _lib; | |
143 | bool _isDylib; | |
144 | public: | |
145 | LibraryInfo(ld::dylib::File* dylib) : _lib(dylib), _isDylib(true) {}; | |
146 | LibraryInfo(ld::archive::File* dylib) : _lib(dylib), _isDylib(false) {}; | |
147 | ||
ec29ba20 A |
148 | bool isDylib() const { return _isDylib; } |
149 | ld::dylib::File *dylib() const { return (ld::dylib::File*)_lib; } | |
150 | ld::archive::File *archive() const { return (ld::archive::File*)_lib; } | |
ebf6f434 A |
151 | }; |
152 | std::vector<LibraryInfo> _searchLibraries; | |
a645023d A |
153 | }; |
154 | ||
155 | } // namespace tool | |
156 | } // namespace ld | |
157 | ||
158 | #endif // __INPUT_FILES_H__ |