1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-*
3 * Copyright (c) 2009-2011 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@
25 #ifndef __INPUT_FILES_H__
26 #define __INPUT_FILES_H__
28 #define HAVE_PTHREADS 1
31 #include <sys/types.h>
34 #include <sys/sysctl.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>
44 #include <mach-o/dyld.h>
57 class InputFiles
: public ld::dylib::File::DylibHandler
60 InputFiles(Options
& opts
, const char** archName
);
62 // implementation from ld::dylib::File::DylibHandler
63 virtual ld::dylib::File
* findDylib(const char* installPath
, const char* fromPath
);
65 // iterates all atoms in initial files
66 void forEachInitialAtom(ld::File::AtomHandler
&);
67 // searches libraries for name
68 bool searchLibraries(const char* name
, bool searchDylibs
, bool searchArchives
,
69 bool dataSymbolOnly
, ld::File::AtomHandler
&) const;
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
);
75 bool inferredArch() const { return _inferredArch
; }
77 // for -print_statistics
78 volatile int64_t _totalObjectSize
;
79 volatile int64_t _totalArchiveSize
;
80 volatile int32_t _totalObjectLoaded
;
81 volatile int32_t _totalArchivesLoaded
;
82 volatile int32_t _totalDylibsLoaded
;
86 void inferArchitecture(Options
& opts
, const char** archName
);
87 const char* fileArch(const uint8_t* p
, unsigned len
);
88 ld::File
* makeFile(const Options::FileInfo
& info
, bool indirectDylib
);
89 ld::File
* addDylib(ld::dylib::File
* f
, const Options::FileInfo
& info
);
90 void logTraceInfo (const char* format
, ...) const;
91 void logDylib(ld::File
*, bool indirect
);
92 void logArchive(ld::File
*) const;
93 void createIndirectDylibs();
94 void checkDylibClientRestrictions(ld::dylib::File
*);
95 void createOpaqueFileSections();
97 // for pipelined linking
98 void waitForInputFiles();
99 static void waitForInputFiles(InputFiles
*inputFiles
);
101 // for threaded input file processing
102 void parseWorkerThread();
103 static void parseWorkerThread(InputFiles
*inputFiles
);
104 void startThread(void (*threadFunc
)(InputFiles
*)) const;
106 typedef std::unordered_map
<const char*, ld::dylib::File
*, CStringHash
, CStringEquals
> InstallNameToDylib
;
108 const Options
& _options
;
109 std::vector
<ld::File
*> _inputFiles
;
110 mutable std::set
<class ld::File
*> _archiveFilesLogged
;
111 InstallNameToDylib _installPathToDylibs
;
112 std::set
<ld::dylib::File
*> _allDylibs
;
113 ld::dylib::File
* _bundleLoader
;
114 bool _allDirectDylibsLoaded
;
116 struct strcompclass
{
117 bool operator() (const char *a
, const char *b
) const { return ::strcmp(a
, b
) < 0; }
120 // for threaded input file processing
122 pthread_mutex_t _parseLock
;
123 pthread_cond_t _parseWorkReady
; // used by parse threads to block for work
124 pthread_cond_t _newFileAvailable
; // used by main thread to block for parsed input files
125 int _availableWorkers
; // number of remaining unstarted parse threads
126 int _idleWorkers
; // number of running parse threads that are idle
127 int _neededFileSlot
; // input file the resolver is currently blocked waiting for
128 int _parseCursor
; // slot to begin searching for a file to parse
129 int _availableInputFiles
; // number of input fileinfos with readyToParse==true
131 const char * _exception
; // passes an exception message from parse thread to main thread
132 int _remainingInputFiles
; // number of input files still to parse
134 ld::File::Ordinal _indirectDylibOrdinal
;
140 LibraryInfo(ld::dylib::File
* dylib
) : _lib(dylib
), _isDylib(true) {};
141 LibraryInfo(ld::archive::File
* dylib
) : _lib(dylib
), _isDylib(false) {};
143 bool isDylib() { return _isDylib
; }
144 ld::dylib::File
*dylib() { return (ld::dylib::File
*)_lib
; }
145 ld::archive::File
*archive() { return (ld::archive::File
*)_lib
; }
147 std::vector
<LibraryInfo
> _searchLibraries
;
153 #endif // __INPUT_FILES_H__