]>
Commit | Line | Data |
---|---|---|
a645023d A |
1 | /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- |
2 | * | |
3 | * Copyright (c) 2006-2010 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 __LTO_READER_H__ | |
26 | #define __LTO_READER_H__ | |
27 | ||
28 | #include <stdlib.h> | |
29 | #include <sys/param.h> | |
30 | #include <sys/fcntl.h> | |
31 | #include <sys/stat.h> | |
32 | #include <errno.h> | |
d425e388 | 33 | #include <pthread.h> |
a645023d A |
34 | #include <mach-o/dyld.h> |
35 | #include <vector> | |
eaf282aa | 36 | #include <map> |
d425e388 A |
37 | #include <unordered_set> |
38 | #include <unordered_map> | |
a645023d A |
39 | |
40 | #include "MachOFileAbstraction.hpp" | |
41 | #include "Architectures.hpp" | |
42 | #include "ld.hpp" | |
43 | #include "macho_relocatable_file.h" | |
44 | #include "lto_file.h" | |
45 | ||
afe874b1 A |
46 | // #defines are a work around for <rdar://problem/8760268> |
47 | #define __STDC_LIMIT_MACROS 1 | |
48 | #define __STDC_CONSTANT_MACROS 1 | |
a645023d A |
49 | #include "llvm-c/lto.h" |
50 | ||
a645023d A |
51 | namespace lto { |
52 | ||
53 | ||
54 | // | |
55 | // ld64 only tracks non-internal symbols from an llvm bitcode file. | |
56 | // We model this by having an InternalAtom which represent all internal functions and data. | |
57 | // All non-interal symbols from a bitcode file are represented by an Atom | |
58 | // and each Atom has a reference to the InternalAtom. The InternalAtom | |
59 | // also has references to each symbol external to the bitcode file. | |
60 | // | |
61 | class InternalAtom : public ld::Atom | |
62 | { | |
63 | public: | |
64 | InternalAtom(class File& f); | |
65 | // overrides of ld::Atom | |
66 | virtual ld::File* file() const { return &_file; } | |
a645023d A |
67 | virtual const char* name() const { return "import-atom"; } |
68 | virtual uint64_t size() const { return 0; } | |
69 | virtual uint64_t objectAddress() const { return 0; } | |
70 | virtual void copyRawContent(uint8_t buffer[]) const { } | |
71 | virtual void setScope(Scope) { } | |
72 | virtual ld::Fixup::iterator fixupsBegin() const { return &_undefs[0]; } | |
73 | virtual ld::Fixup::iterator fixupsEnd() const { return &_undefs[_undefs.size()]; } | |
74 | ||
75 | // for adding references to symbols outside bitcode file | |
76 | void addReference(const char* nm) | |
77 | { _undefs.push_back(ld::Fixup(0, ld::Fixup::k1of1, | |
599556ff | 78 | ld::Fixup::kindNone, false, strdup(nm))); } |
a645023d A |
79 | private: |
80 | ||
81 | ld::File& _file; | |
82 | mutable std::vector<ld::Fixup> _undefs; | |
83 | }; | |
84 | ||
85 | ||
86 | // | |
87 | // LLVM bitcode file | |
88 | // | |
89 | class File : public ld::relocatable::File | |
90 | { | |
91 | public: | |
b1f7435d A |
92 | File(const char* path, time_t mTime, ld::File::Ordinal ordinal, |
93 | const uint8_t* content, uint32_t contentLength, cpu_type_t arch); | |
a645023d A |
94 | virtual ~File(); |
95 | ||
96 | // overrides of ld::File | |
97 | virtual bool forEachAtom(ld::File::AtomHandler&) const; | |
98 | virtual bool justInTimeforEachAtom(const char* name, ld::File::AtomHandler&) const | |
99 | { return false; } | |
100 | virtual uint32_t cpuSubType() const { return _cpuSubType; } | |
101 | ||
102 | // overrides of ld::relocatable::File | |
a645023d A |
103 | virtual DebugInfoKind debugInfo() const { return _debugInfo; } |
104 | virtual const char* debugInfoPath() const { return _debugInfoPath; } | |
105 | virtual time_t debugInfoModificationTime() const | |
106 | { return _debugInfoModTime; } | |
107 | virtual const std::vector<ld::relocatable::File::Stab>* stabs() const { return NULL; } | |
108 | virtual bool canScatterAtoms() const { return true; } | |
f80fe69f A |
109 | virtual LinkerOptionsList* linkerOptions() const { return NULL; } |
110 | ||
a645023d | 111 | |
599556ff | 112 | void release(); |
a645023d A |
113 | lto_module_t module() { return _module; } |
114 | class InternalAtom& internalAtom() { return _internalAtom; } | |
115 | void setDebugInfo(ld::relocatable::File::DebugInfoKind k, | |
116 | const char* pth, time_t modTime, uint32_t subtype) | |
117 | { _debugInfo = k; | |
118 | _debugInfoPath = pth; | |
119 | _debugInfoModTime = modTime; | |
120 | _cpuSubType = subtype;} | |
121 | ||
eaf282aa A |
122 | static bool sSupportsLocalContext; |
123 | static bool sHasTriedLocalContext; | |
124 | bool mergeIntoGenerator(lto_code_gen_t generator, bool useSetModule); | |
a645023d A |
125 | private: |
126 | friend class Atom; | |
127 | friend class InternalAtom; | |
128 | friend class Parser; | |
129 | ||
130 | cpu_type_t _architecture; | |
131 | class InternalAtom _internalAtom; | |
132 | class Atom* _atomArray; | |
133 | uint32_t _atomArrayCount; | |
134 | lto_module_t _module; | |
eaf282aa A |
135 | const char* _path; |
136 | const uint8_t* _content; | |
137 | uint32_t _contentLength; | |
a645023d A |
138 | const char* _debugInfoPath; |
139 | time_t _debugInfoModTime; | |
140 | ld::Section _section; | |
141 | ld::Fixup _fixupToInternal; | |
142 | ld::relocatable::File::DebugInfoKind _debugInfo; | |
143 | uint32_t _cpuSubType; | |
144 | }; | |
145 | ||
146 | // | |
147 | // Atom acts as a proxy Atom for the symbols that are exported by LLVM bitcode file. Initially, | |
148 | // Reader creates Atoms to allow linker proceed with usual symbol resolution phase. After | |
149 | // optimization is performed, real Atoms are created for these symobls. However these real Atoms | |
150 | // are not inserted into global symbol table. Atom holds real Atom and forwards appropriate | |
151 | // methods to real atom. | |
152 | // | |
153 | class Atom : public ld::Atom | |
154 | { | |
155 | public: | |
156 | Atom(File& f, const char* name, ld::Atom::Scope s, | |
b2fa67a8 | 157 | ld::Atom::Definition d, ld::Atom::Combine c, ld::Atom::Alignment a, bool ah); |
a645023d A |
158 | |
159 | // overrides of ld::Atom | |
160 | virtual ld::File* file() const { return &_file; } | |
b1f7435d A |
161 | virtual const char* translationUnitSource() const |
162 | { return (_compiledAtom ? _compiledAtom->translationUnitSource() : NULL); } | |
a645023d A |
163 | virtual const char* name() const { return _name; } |
164 | virtual uint64_t size() const { return (_compiledAtom ? _compiledAtom->size() : 0); } | |
165 | virtual uint64_t objectAddress() const { return (_compiledAtom ? _compiledAtom->objectAddress() : 0); } | |
166 | virtual void copyRawContent(uint8_t buffer[]) const | |
167 | { if (_compiledAtom) _compiledAtom->copyRawContent(buffer); } | |
168 | virtual const uint8_t* rawContentPointer() const | |
169 | { return (_compiledAtom ? _compiledAtom->rawContentPointer() : NULL); } | |
170 | virtual unsigned long contentHash(const class ld::IndirectBindingTable& ibt) const | |
171 | { return (_compiledAtom ? _compiledAtom->contentHash(ibt) : 0); } | |
172 | virtual bool canCoalesceWith(const ld::Atom& rhs, const class ld::IndirectBindingTable& ibt) const | |
173 | { return (_compiledAtom ? _compiledAtom->canCoalesceWith(rhs,ibt) : false); } | |
174 | virtual ld::Fixup::iterator fixupsBegin() const | |
175 | { return (_compiledAtom ? _compiledAtom->fixupsBegin() : (ld::Fixup*)&_file._fixupToInternal); } | |
176 | virtual ld::Fixup::iterator fixupsEnd() const | |
177 | { return (_compiledAtom ? _compiledAtom->fixupsEnd() : &((ld::Fixup*)&_file._fixupToInternal)[1]); } | |
178 | virtual ld::Atom::UnwindInfo::iterator beginUnwind() const | |
179 | { return (_compiledAtom ? _compiledAtom->beginUnwind() : NULL); } | |
180 | virtual ld::Atom::UnwindInfo::iterator endUnwind() const | |
181 | { return (_compiledAtom ? _compiledAtom->endUnwind() : NULL); } | |
182 | virtual ld::Atom::LineInfo::iterator beginLineInfo() const | |
183 | { return (_compiledAtom ? _compiledAtom->beginLineInfo() : NULL); } | |
184 | virtual ld::Atom::LineInfo::iterator endLineInfo() const | |
185 | { return (_compiledAtom ? _compiledAtom->endLineInfo() : NULL); } | |
186 | ||
187 | const ld::Atom* compiledAtom() { return _compiledAtom; } | |
188 | void setCompiledAtom(const ld::Atom& atom); | |
189 | ||
190 | private: | |
191 | ||
192 | File& _file; | |
193 | const char* _name; | |
194 | const ld::Atom* _compiledAtom; | |
195 | }; | |
196 | ||
197 | ||
198 | ||
199 | ||
200 | ||
201 | ||
202 | ||
203 | class Parser | |
204 | { | |
205 | public: | |
206 | static bool validFile(const uint8_t* fileContent, uint64_t fileLength, cpu_type_t architecture, cpu_subtype_t subarch); | |
207 | static const char* fileKind(const uint8_t* fileContent, uint64_t fileLength); | |
208 | static File* parse(const uint8_t* fileContent, uint64_t fileLength, const char* path, | |
9543cb2f A |
209 | time_t modTime, ld::File::Ordinal ordinal, cpu_type_t architecture, cpu_subtype_t subarch, |
210 | bool logAllFiles, bool verboseOptimizationHints); | |
a645023d A |
211 | static bool libLTOisLoaded() { return (::lto_get_version() != NULL); } |
212 | static bool optimize( const std::vector<const ld::Atom*>& allAtoms, | |
213 | ld::Internal& state, | |
a645023d A |
214 | const OptimizeOptions& options, |
215 | ld::File::AtomHandler& handler, | |
216 | std::vector<const ld::Atom*>& newAtoms, | |
217 | std::vector<const char*>& additionalUndefines); | |
218 | ||
219 | static const char* ltoVersion() { return ::lto_get_version(); } | |
220 | ||
221 | private: | |
222 | static const char* tripletPrefixForArch(cpu_type_t arch); | |
ebf6f434 | 223 | static ld::relocatable::File* parseMachOFile(const uint8_t* p, size_t len, const OptimizeOptions& options); |
9543cb2f A |
224 | #if LTO_API_VERSION >= 7 |
225 | static void ltoDiagnosticHandler(lto_codegen_diagnostic_severity_t, const char*, void*); | |
226 | #endif | |
a645023d | 227 | |
d425e388 A |
228 | typedef std::unordered_set<const char*, ld::CStringHash, ld::CStringEquals> CStringSet; |
229 | typedef std::unordered_map<const char*, Atom*, ld::CStringHash, ld::CStringEquals> CStringToAtom; | |
a645023d A |
230 | |
231 | class AtomSyncer : public ld::File::AtomHandler { | |
232 | public: | |
233 | AtomSyncer(std::vector<const char*>& a, std::vector<const ld::Atom*>&na, | |
234 | CStringToAtom la, CStringToAtom dla, const OptimizeOptions& options) : | |
235 | _options(options), _additionalUndefines(a), _newAtoms(na), _llvmAtoms(la), _deadllvmAtoms(dla) { } | |
236 | virtual void doAtom(const class ld::Atom&); | |
237 | virtual void doFile(const class ld::File&) { } | |
238 | ||
599556ff | 239 | |
a645023d A |
240 | const OptimizeOptions& _options; |
241 | std::vector<const char*>& _additionalUndefines; | |
242 | std::vector<const ld::Atom*>& _newAtoms; | |
243 | CStringToAtom _llvmAtoms; | |
244 | CStringToAtom _deadllvmAtoms; | |
245 | }; | |
246 | ||
247 | static std::vector<File*> _s_files; | |
248 | }; | |
249 | ||
250 | std::vector<File*> Parser::_s_files; | |
251 | ||
252 | ||
253 | bool Parser::validFile(const uint8_t* fileContent, uint64_t fileLength, cpu_type_t architecture, cpu_subtype_t subarch) | |
254 | { | |
ebf6f434 A |
255 | for (const ArchInfo* t=archInfoArray; t->archName != NULL; ++t) { |
256 | if ( (architecture == t->cpuType) && (!(t->isSubType) || (subarch == t->cpuSubType)) ) { | |
257 | bool result = ::lto_module_is_object_file_in_memory_for_target(fileContent, fileLength, t->llvmTriplePrefix); | |
258 | if ( !result ) { | |
259 | // <rdar://problem/8434487> LTO only supports thumbv7 not armv7 | |
260 | if ( t->llvmTriplePrefixAlt[0] != '\0' ) { | |
261 | result = ::lto_module_is_object_file_in_memory_for_target(fileContent, fileLength, t->llvmTriplePrefixAlt); | |
262 | } | |
a645023d | 263 | } |
ebf6f434 A |
264 | return result; |
265 | } | |
a645023d A |
266 | } |
267 | return false; | |
268 | } | |
269 | ||
270 | const char* Parser::fileKind(const uint8_t* p, uint64_t fileLength) | |
271 | { | |
272 | if ( (p[0] == 0xDE) && (p[1] == 0xC0) && (p[2] == 0x17) && (p[3] == 0x0B) ) { | |
ebf6f434 A |
273 | cpu_type_t arch = LittleEndian::get32(*((uint32_t*)(&p[16]))); |
274 | for (const ArchInfo* t=archInfoArray; t->archName != NULL; ++t) { | |
275 | if ( arch == t->cpuType ) { | |
276 | if ( t->isSubType ) { | |
afe874b1 | 277 | if ( ::lto_module_is_object_file_in_memory_for_target(p, fileLength, t->llvmTriplePrefix) ) |
ebf6f434 A |
278 | return t->archName; |
279 | } | |
280 | else { | |
281 | return t->archName; | |
afe874b1 | 282 | } |
ebf6f434 | 283 | } |
a645023d A |
284 | } |
285 | return "unknown bitcode architecture"; | |
286 | } | |
287 | return NULL; | |
288 | } | |
289 | ||
b1f7435d | 290 | File* Parser::parse(const uint8_t* fileContent, uint64_t fileLength, const char* path, time_t modTime, ld::File::Ordinal ordinal, |
9543cb2f | 291 | cpu_type_t architecture, cpu_subtype_t subarch, bool logAllFiles, bool verboseOptimizationHints) |
a645023d | 292 | { |
b1f7435d | 293 | File* f = new File(path, modTime, ordinal, fileContent, fileLength, architecture); |
a645023d A |
294 | _s_files.push_back(f); |
295 | if ( logAllFiles ) | |
296 | printf("%s\n", path); | |
297 | return f; | |
298 | } | |
299 | ||
300 | ||
ebf6f434 | 301 | ld::relocatable::File* Parser::parseMachOFile(const uint8_t* p, size_t len, const OptimizeOptions& options) |
a645023d A |
302 | { |
303 | mach_o::relocatable::ParserOptions objOpts; | |
304 | objOpts.architecture = options.arch; | |
305 | objOpts.objSubtypeMustMatch = false; | |
306 | objOpts.logAllFiles = false; | |
f80fe69f A |
307 | objOpts.warnUnwindConversionProblems = options.needsUnwindInfoSection; |
308 | objOpts.keepDwarfUnwind = options.keepDwarfUnwind; | |
309 | objOpts.forceDwarfConversion = false; | |
9543cb2f A |
310 | objOpts.neverConvertDwarf = false; |
311 | objOpts.verboseOptimizationHints = options.verboseOptimizationHints; | |
ba348e21 | 312 | objOpts.armUsesZeroCostExceptions = options.armUsesZeroCostExceptions; |
eaf282aa A |
313 | objOpts.simulator = options.simulator; |
314 | objOpts.ignoreMismatchPlatform = options.ignoreMismatchPlatform; | |
315 | objOpts.platform = options.platform; | |
a645023d | 316 | objOpts.subType = 0; |
eaf282aa | 317 | objOpts.srcKind = ld::relocatable::File::kSourceLTO; |
dd9e569f A |
318 | objOpts.treateBitcodeAsData = false; |
319 | objOpts.usingBitcode = options.bitcodeBundle; | |
ec29ba20 A |
320 | objOpts.maxDefaultCommonAlignment = options.maxDefaultCommonAlignment; |
321 | ||
a645023d A |
322 | // mach-o parsing is done in-memory, but need path for debug notes |
323 | const char* path = "/tmp/lto.o"; | |
324 | time_t modTime = 0; | |
325 | if ( options.tmpObjectFilePath != NULL ) { | |
326 | path = options.tmpObjectFilePath; | |
327 | struct stat statBuffer; | |
328 | if ( stat(options.tmpObjectFilePath, &statBuffer) == 0 ) | |
329 | modTime = statBuffer.st_mtime; | |
330 | } | |
331 | ||
ebf6f434 | 332 | ld::relocatable::File* result = mach_o::relocatable::parse(p, len, path, modTime, ld::File::Ordinal::LTOOrdinal(), objOpts); |
a645023d A |
333 | if ( result != NULL ) |
334 | return result; | |
335 | throw "LLVM LTO, file is not of required architecture"; | |
336 | } | |
337 | ||
338 | ||
339 | ||
b1f7435d A |
340 | File::File(const char* pth, time_t mTime, ld::File::Ordinal ordinal, const uint8_t* content, uint32_t contentLength, cpu_type_t arch) |
341 | : ld::relocatable::File(pth,mTime,ordinal), _architecture(arch), _internalAtom(*this), | |
eaf282aa A |
342 | _atomArray(NULL), _atomArrayCount(0), _module(NULL), _path(pth), |
343 | _content(content), _contentLength(contentLength), _debugInfoPath(pth), | |
a645023d A |
344 | _section("__TEXT_", "__tmp_lto", ld::Section::typeTempLTO), |
345 | _fixupToInternal(0, ld::Fixup::k1of1, ld::Fixup::kindNone, &_internalAtom), | |
346 | _debugInfo(ld::relocatable::File::kDebugInfoNone), _cpuSubType(0) | |
347 | { | |
348 | const bool log = false; | |
349 | ||
350 | // create llvm module | |
eaf282aa A |
351 | #if LTO_API_VERSION >= 11 |
352 | if ( sSupportsLocalContext || !sHasTriedLocalContext ) { | |
353 | _module = ::lto_module_create_in_local_context(content, contentLength, pth); | |
354 | } | |
355 | if ( !sHasTriedLocalContext ) { | |
356 | sHasTriedLocalContext = true; | |
357 | sSupportsLocalContext = (_module != NULL); | |
358 | } | |
359 | if ( (_module == NULL) && !sSupportsLocalContext ) | |
360 | #endif | |
599556ff A |
361 | #if LTO_API_VERSION >= 9 |
362 | _module = ::lto_module_create_from_memory_with_path(content, contentLength, pth); | |
eaf282aa | 363 | if ( _module == NULL && !sSupportsLocalContext ) |
599556ff | 364 | #endif |
a645023d A |
365 | _module = ::lto_module_create_from_memory(content, contentLength); |
366 | if ( _module == NULL ) | |
d425e388 | 367 | throwf("could not parse object file %s: '%s', using libLTO version '%s'", pth, ::lto_get_error_message(), ::lto_get_version()); |
a645023d A |
368 | |
369 | if ( log ) fprintf(stderr, "bitcode file: %s\n", pth); | |
370 | ||
371 | // create atom for each global symbol in module | |
372 | uint32_t count = ::lto_module_get_num_symbols(_module); | |
373 | _atomArray = (Atom*)malloc(sizeof(Atom)*count); | |
374 | for (uint32_t i=0; i < count; ++i) { | |
375 | const char* name = ::lto_module_get_symbol_name(_module, i); | |
376 | lto_symbol_attributes attr = lto_module_get_symbol_attribute(_module, i); | |
377 | ||
378 | // <rdar://problem/6378110> LTO doesn't like dtrace symbols | |
379 | // ignore dtrace static probes for now | |
380 | // later when codegen is done and a mach-o file is produces the probes will be processed | |
381 | if ( (strncmp(name, "___dtrace_probe$", 16) == 0) || (strncmp(name, "___dtrace_isenabled$", 20) == 0) ) | |
382 | continue; | |
383 | ||
384 | ld::Atom::Definition def; | |
385 | ld::Atom::Combine combine = ld::Atom::combineNever; | |
386 | switch ( attr & LTO_SYMBOL_DEFINITION_MASK ) { | |
387 | case LTO_SYMBOL_DEFINITION_REGULAR: | |
388 | def = ld::Atom::definitionRegular; | |
389 | break; | |
390 | case LTO_SYMBOL_DEFINITION_TENTATIVE: | |
391 | def = ld::Atom::definitionTentative; | |
392 | break; | |
393 | case LTO_SYMBOL_DEFINITION_WEAK: | |
394 | def = ld::Atom::definitionRegular; | |
395 | combine = ld::Atom::combineByName; | |
396 | break; | |
397 | case LTO_SYMBOL_DEFINITION_UNDEFINED: | |
398 | case LTO_SYMBOL_DEFINITION_WEAKUNDEF: | |
399 | def = ld::Atom::definitionProxy; | |
400 | break; | |
401 | default: | |
402 | throwf("unknown definition kind for symbol %s in bitcode file %s", name, pth); | |
403 | } | |
404 | ||
405 | // make LLVM atoms for definitions and a reference for undefines | |
406 | if ( def != ld::Atom::definitionProxy ) { | |
407 | ld::Atom::Scope scope; | |
b2fa67a8 | 408 | bool autohide = false; |
a645023d A |
409 | switch ( attr & LTO_SYMBOL_SCOPE_MASK) { |
410 | case LTO_SYMBOL_SCOPE_INTERNAL: | |
411 | scope = ld::Atom::scopeTranslationUnit; | |
412 | break; | |
413 | case LTO_SYMBOL_SCOPE_HIDDEN: | |
414 | scope = ld::Atom::scopeLinkageUnit; | |
415 | break; | |
416 | case LTO_SYMBOL_SCOPE_DEFAULT: | |
417 | scope = ld::Atom::scopeGlobal; | |
418 | break; | |
b2fa67a8 A |
419 | #if LTO_API_VERSION >= 4 |
420 | case LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN: | |
421 | scope = ld::Atom::scopeGlobal; | |
422 | autohide = true; | |
423 | break; | |
424 | #endif | |
a645023d A |
425 | default: |
426 | throwf("unknown scope for symbol %s in bitcode file %s", name, pth); | |
427 | } | |
428 | // only make atoms for non-internal symbols | |
429 | if ( scope == ld::Atom::scopeTranslationUnit ) | |
430 | continue; | |
431 | uint8_t alignment = (attr & LTO_SYMBOL_ALIGNMENT_MASK); | |
432 | // make Atom using placement new operator | |
b2fa67a8 | 433 | new (&_atomArray[_atomArrayCount++]) Atom(*this, name, scope, def, combine, alignment, autohide); |
ebf6f434 | 434 | if ( scope != ld::Atom::scopeTranslationUnit ) |
a645023d A |
435 | _internalAtom.addReference(name); |
436 | if ( log ) fprintf(stderr, "\t0x%08X %s\n", attr, name); | |
437 | } | |
438 | else { | |
439 | // add to list of external references | |
440 | _internalAtom.addReference(name); | |
441 | if ( log ) fprintf(stderr, "\t%s (undefined)\n", name); | |
442 | } | |
443 | } | |
eaf282aa A |
444 | |
445 | #if LTO_API_VERSION >= 11 | |
446 | if ( sSupportsLocalContext ) | |
447 | this->release(); | |
448 | #endif | |
a645023d A |
449 | } |
450 | ||
451 | File::~File() | |
599556ff A |
452 | { |
453 | this->release(); | |
454 | } | |
455 | ||
eaf282aa A |
456 | bool File::mergeIntoGenerator(lto_code_gen_t generator, bool useSetModule) { |
457 | #if LTO_API_VERSION >= 11 | |
458 | if ( sSupportsLocalContext ) { | |
459 | assert(!_module && "Expected module to be disposed"); | |
460 | _module = ::lto_module_create_in_codegen_context(_content, _contentLength, | |
461 | _path, generator); | |
462 | if ( _module == NULL ) | |
463 | throwf("could not reparse object file %s: '%s', using libLTO version '%s'", | |
464 | _path, ::lto_get_error_message(), ::lto_get_version()); | |
465 | } | |
466 | #endif | |
467 | assert(_module && "Expected module to stick around"); | |
468 | #if LTO_API_VERSION >= 13 | |
469 | if (useSetModule) { | |
470 | // lto_codegen_set_module will transfer ownership of the module to LTO code generator, | |
471 | // so we don't need to release the module here. | |
472 | ::lto_codegen_set_module(generator, _module); | |
473 | return false; | |
474 | } | |
475 | #endif | |
476 | if ( ::lto_codegen_add_module(generator, _module) ) | |
477 | return true; | |
478 | ||
479 | // <rdar://problem/15471128> linker should release module as soon as possible | |
480 | this->release(); | |
481 | return false; | |
482 | } | |
483 | ||
599556ff | 484 | void File::release() |
a645023d A |
485 | { |
486 | if ( _module != NULL ) | |
487 | ::lto_module_dispose(_module); | |
599556ff | 488 | _module = NULL; |
a645023d A |
489 | } |
490 | ||
491 | bool File::forEachAtom(ld::File::AtomHandler& handler) const | |
492 | { | |
493 | handler.doAtom(_internalAtom); | |
494 | for(uint32_t i=0; i < _atomArrayCount; ++i) { | |
495 | handler.doAtom(_atomArray[i]); | |
496 | } | |
497 | return true; | |
498 | } | |
499 | ||
500 | InternalAtom::InternalAtom(File& f) | |
501 | : ld::Atom(f._section, ld::Atom::definitionRegular, ld::Atom::combineNever, ld::Atom::scopeTranslationUnit, | |
502 | ld::Atom::typeLTOtemporary, ld::Atom::symbolTableNotIn, true, false, false, ld::Atom::Alignment(0)), | |
503 | _file(f) | |
504 | { | |
505 | } | |
506 | ||
b2fa67a8 A |
507 | Atom::Atom(File& f, const char* nm, ld::Atom::Scope s, ld::Atom::Definition d, ld::Atom::Combine c, |
508 | ld::Atom::Alignment a, bool ah) | |
a645023d A |
509 | : ld::Atom(f._section, d, c, s, ld::Atom::typeLTOtemporary, |
510 | ld::Atom::symbolTableIn, false, false, false, a), | |
599556ff | 511 | _file(f), _name(strdup(nm)), _compiledAtom(NULL) |
a645023d | 512 | { |
b2fa67a8 A |
513 | if ( ah ) |
514 | this->setAutoHide(); | |
a645023d A |
515 | } |
516 | ||
517 | void Atom::setCompiledAtom(const ld::Atom& atom) | |
518 | { | |
519 | // set delegate so virtual methods go to it | |
520 | _compiledAtom = &atom; | |
521 | ||
522 | //fprintf(stderr, "setting lto atom %p to delegate to mach-o atom %p (%s)\n", this, &atom, atom.name()); | |
523 | ||
524 | // update fields in ld::Atom to match newly constructed mach-o atom | |
525 | (const_cast<Atom*>(this))->setAttributesFromAtom(atom); | |
526 | } | |
527 | ||
528 | ||
529 | ||
f80fe69f A |
530 | // <rdar://problem/12379604> The order that files are merged must match command line order |
531 | struct CommandLineOrderFileSorter | |
532 | { | |
533 | bool operator()(File* left, File* right) | |
534 | { | |
535 | return ( left->ordinal() < right->ordinal() ); | |
536 | } | |
537 | }; | |
538 | ||
539 | ||
9543cb2f A |
540 | #if LTO_API_VERSION >= 7 |
541 | void Parser::ltoDiagnosticHandler(lto_codegen_diagnostic_severity_t severity, const char* message, void*) | |
542 | { | |
543 | switch ( severity ) { | |
599556ff A |
544 | #if LTO_API_VERSION >= 10 |
545 | case LTO_DS_REMARK: | |
eaf282aa A |
546 | fprintf(stderr, "ld: LTO remark: %s\n", message); |
547 | break; | |
599556ff | 548 | #endif |
9543cb2f A |
549 | case LTO_DS_NOTE: |
550 | case LTO_DS_WARNING: | |
551 | warning("%s", message); | |
552 | break; | |
553 | case LTO_DS_ERROR: | |
554 | throwf("%s", message); | |
555 | } | |
556 | } | |
557 | #endif | |
558 | ||
a645023d A |
559 | bool Parser::optimize( const std::vector<const ld::Atom*>& allAtoms, |
560 | ld::Internal& state, | |
a645023d A |
561 | const OptimizeOptions& options, |
562 | ld::File::AtomHandler& handler, | |
563 | std::vector<const ld::Atom*>& newAtoms, | |
564 | std::vector<const char*>& additionalUndefines) | |
565 | { | |
566 | const bool logMustPreserve = false; | |
567 | const bool logExtraOptions = false; | |
568 | const bool logBitcodeFiles = false; | |
569 | const bool logAtomsBeforeSync = false; | |
570 | ||
571 | // exit quickly if nothing to do | |
572 | if ( _s_files.size() == 0 ) | |
573 | return false; | |
574 | ||
575 | // print out LTO version string if -v was used | |
576 | if ( options.verbose ) | |
d425e388 | 577 | fprintf(stderr, "%s\n", ::lto_get_version()); |
a645023d A |
578 | |
579 | // create optimizer and add each Reader | |
eaf282aa A |
580 | lto_code_gen_t generator = NULL; |
581 | #if LTO_API_VERSION >= 11 | |
582 | if ( File::sSupportsLocalContext ) | |
583 | generator = ::lto_codegen_create_in_local_context(); | |
584 | else | |
585 | #endif | |
586 | generator = ::lto_codegen_create(); | |
9543cb2f A |
587 | #if LTO_API_VERSION >= 7 |
588 | lto_codegen_set_diagnostic_handler(generator, ltoDiagnosticHandler, NULL); | |
589 | #endif | |
590 | ||
f80fe69f A |
591 | // <rdar://problem/12379604> The order that files are merged must match command line order |
592 | std::sort(_s_files.begin(), _s_files.end(), CommandLineOrderFileSorter()); | |
593 | ld::File::Ordinal lastOrdinal; | |
eaf282aa A |
594 | |
595 | // When flto_codegen_only is on and we have a single .bc file, use lto_codegen_set_module instead of | |
596 | // lto_codegen_add_module, to make sure the the destination module will be the same as the input .bc file. | |
597 | bool useSetModule = false; | |
598 | #if LTO_API_VERSION >= 13 | |
599 | useSetModule = (_s_files.size() == 1) && options.ltoCodegenOnly && (::lto_api_version() >= 13); | |
600 | #endif | |
a645023d | 601 | for (std::vector<File*>::iterator it=_s_files.begin(); it != _s_files.end(); ++it) { |
f80fe69f A |
602 | File* f = *it; |
603 | assert(f->ordinal() > lastOrdinal); | |
eaf282aa A |
604 | if ( logBitcodeFiles && !useSetModule) fprintf(stderr, "lto_codegen_add_module(%s)\n", f->path()); |
605 | if ( logBitcodeFiles && useSetModule) fprintf(stderr, "lto_codegen_set_module(%s)\n", f->path()); | |
606 | if ( f->mergeIntoGenerator(generator, useSetModule) ) | |
f80fe69f A |
607 | throwf("lto: could not merge in %s because '%s', using libLTO version '%s'", f->path(), ::lto_get_error_message(), ::lto_get_version()); |
608 | lastOrdinal = f->ordinal(); | |
a645023d A |
609 | } |
610 | ||
611 | // add any -mllvm command line options | |
612 | for (std::vector<const char*>::const_iterator it=options.llvmOptions->begin(); it != options.llvmOptions->end(); ++it) { | |
613 | if ( logExtraOptions ) fprintf(stderr, "passing option to llvm: %s\n", *it); | |
614 | ::lto_codegen_debug_options(generator, *it); | |
615 | } | |
616 | ||
f80fe69f A |
617 | // <rdar://problem/13687397> Need a way for LTO to get cpu variants (until that info is in bitcode) |
618 | if ( options.mcpu != NULL ) | |
619 | ::lto_codegen_set_cpu(generator, options.mcpu); | |
620 | ||
a645023d A |
621 | // The atom graph uses directed edges (references). Collect all references where |
622 | // originating atom is not part of any LTO Reader. This allows optimizer to optimize an | |
623 | // external (i.e. not originated from same .o file) reference if all originating atoms are also | |
624 | // defined in llvm bitcode file. | |
625 | CStringSet nonLLVMRefs; | |
626 | CStringToAtom llvmAtoms; | |
627 | bool hasNonllvmAtoms = false; | |
628 | for (std::vector<const ld::Atom*>::const_iterator it = allAtoms.begin(); it != allAtoms.end(); ++it) { | |
629 | const ld::Atom* atom = *it; | |
630 | // only look at references that come from an atom that is not an llvm atom | |
631 | if ( atom->contentType() != ld::Atom::typeLTOtemporary ) { | |
632 | if ( (atom->section().type() != ld::Section::typeMachHeader) && (atom->definition() != ld::Atom::definitionProxy) ) { | |
633 | hasNonllvmAtoms = true; | |
634 | } | |
635 | const ld::Atom* target; | |
636 | for (ld::Fixup::iterator fit=atom->fixupsBegin(); fit != atom->fixupsEnd(); ++fit) { | |
637 | switch ( fit->binding ) { | |
638 | case ld::Fixup::bindingDirectlyBound: | |
639 | // that reference an llvm atom | |
640 | if ( fit->u.target->contentType() == ld::Atom::typeLTOtemporary ) | |
641 | nonLLVMRefs.insert(fit->u.target->name()); | |
642 | break; | |
643 | case ld::Fixup::bindingsIndirectlyBound: | |
644 | target = state.indirectBindingTable[fit->u.bindingIndex]; | |
9543cb2f | 645 | if ( (target != NULL) && (target->contentType() == ld::Atom::typeLTOtemporary) ) |
a645023d A |
646 | nonLLVMRefs.insert(target->name()); |
647 | default: | |
648 | break; | |
649 | } | |
650 | } | |
651 | } | |
599556ff | 652 | else if ( atom->scope() >= ld::Atom::scopeLinkageUnit ) { |
a645023d A |
653 | llvmAtoms[atom->name()] = (Atom*)atom; |
654 | } | |
655 | } | |
656 | // if entry point is in a llvm bitcode file, it must be preserved by LTO | |
657 | if ( state.entryPoint!= NULL ) { | |
658 | if ( state.entryPoint->contentType() == ld::Atom::typeLTOtemporary ) | |
659 | nonLLVMRefs.insert(state.entryPoint->name()); | |
660 | } | |
661 | ||
662 | // deadAtoms are the atoms that the linker coalesced. For instance weak or tentative definitions | |
663 | // overriden by another atom. If any of these deadAtoms are llvm atoms and they were replaced | |
664 | // with a mach-o atom, we need to tell the lto engine to preserve (not optimize away) its dead | |
665 | // atom so that the linker can replace it with the mach-o one later. | |
666 | CStringToAtom deadllvmAtoms; | |
667 | for (std::vector<const ld::Atom*>::const_iterator it = allAtoms.begin(); it != allAtoms.end(); ++it) { | |
668 | const ld::Atom* atom = *it; | |
669 | if ( atom->coalescedAway() && (atom->contentType() == ld::Atom::typeLTOtemporary) ) { | |
670 | const char* name = atom->name(); | |
671 | if ( logMustPreserve ) fprintf(stderr, "lto_codegen_add_must_preserve_symbol(%s) because linker coalesce away and replace with a mach-o atom\n", name); | |
672 | ::lto_codegen_add_must_preserve_symbol(generator, name); | |
673 | deadllvmAtoms[name] = (Atom*)atom; | |
674 | } | |
675 | } | |
676 | for (std::vector<File*>::iterator it=_s_files.begin(); it != _s_files.end(); ++it) { | |
677 | File* file = *it; | |
678 | for(uint32_t i=0; i < file->_atomArrayCount; ++i) { | |
679 | Atom* llvmAtom = &file->_atomArray[i]; | |
680 | if ( llvmAtom->coalescedAway() ) { | |
681 | const char* name = llvmAtom->name(); | |
682 | if ( deadllvmAtoms.find(name) == deadllvmAtoms.end() ) { | |
683 | if ( logMustPreserve ) | |
684 | fprintf(stderr, "lto_codegen_add_must_preserve_symbol(%s) because linker coalesce away and replace with a mach-o atom\n", name); | |
685 | ::lto_codegen_add_must_preserve_symbol(generator, name); | |
686 | deadllvmAtoms[name] = (Atom*)llvmAtom; | |
687 | } | |
688 | } | |
689 | else if ( options.linkerDeadStripping && !llvmAtom->live() ) { | |
690 | const char* name = llvmAtom->name(); | |
691 | deadllvmAtoms[name] = (Atom*)llvmAtom; | |
692 | } | |
693 | } | |
694 | } | |
695 | ||
696 | // tell code generator about symbols that must be preserved | |
697 | for (CStringToAtom::iterator it = llvmAtoms.begin(); it != llvmAtoms.end(); ++it) { | |
698 | const char* name = it->first; | |
699 | Atom* atom = it->second; | |
700 | // Include llvm Symbol in export list if it meets one of following two conditions | |
701 | // 1 - atom scope is global (and not linkage unit). | |
702 | // 2 - included in nonLLVMRefs set. | |
703 | // If a symbol is not listed in exportList then LTO is free to optimize it away. | |
ebf6f434 | 704 | if ( (atom->scope() == ld::Atom::scopeGlobal) && options.preserveAllGlobals ) { |
a645023d A |
705 | if ( logMustPreserve ) fprintf(stderr, "lto_codegen_add_must_preserve_symbol(%s) because global symbol\n", name); |
706 | ::lto_codegen_add_must_preserve_symbol(generator, name); | |
707 | } | |
708 | else if ( nonLLVMRefs.find(name) != nonLLVMRefs.end() ) { | |
709 | if ( logMustPreserve ) fprintf(stderr, "lto_codegen_add_must_preserve_symbol(%s) because referenced by a mach-o atom\n", name); | |
710 | ::lto_codegen_add_must_preserve_symbol(generator, name); | |
711 | } | |
599556ff A |
712 | else if ( options.relocatable && hasNonllvmAtoms ) { |
713 | // <rdar://problem/14334895> ld -r mode but merging in some mach-o files, so need to keep libLTO from optimizing away anything | |
714 | if ( logMustPreserve ) fprintf(stderr, "lto_codegen_add_must_preserve_symbol(%s) because -r mode disable LTO dead stripping\n", name); | |
715 | ::lto_codegen_add_must_preserve_symbol(generator, name); | |
716 | } | |
a645023d A |
717 | } |
718 | ||
599556ff A |
719 | // <rdar://problem/16165191> tell code generator to preserve initial undefines |
720 | for( std::vector<const char*>::const_iterator it=options.initialUndefines->begin(); it != options.initialUndefines->end(); ++it) { | |
721 | if ( logMustPreserve ) fprintf(stderr, "lto_codegen_add_must_preserve_symbol(%s) because it is an initial undefine\n", *it); | |
722 | ::lto_codegen_add_must_preserve_symbol(generator, *it); | |
723 | } | |
724 | ||
a645023d A |
725 | // special case running ld -r on all bitcode files to produce another bitcode file (instead of mach-o) |
726 | if ( options.relocatable && !hasNonllvmAtoms ) { | |
eaf282aa A |
727 | #if LTO_API_VERSION >= 15 |
728 | ::lto_codegen_set_should_embed_uselists(generator, false); | |
729 | #endif | |
a645023d A |
730 | if ( ! ::lto_codegen_write_merged_modules(generator, options.outputFilePath) ) { |
731 | // HACK, no good way to tell linker we are all done, so just quit | |
732 | exit(0); | |
733 | } | |
734 | warning("could not produce merged bitcode file"); | |
735 | } | |
736 | ||
737 | // set code-gen model | |
738 | lto_codegen_model model = LTO_CODEGEN_PIC_MODEL_DYNAMIC; | |
739 | if ( options.mainExecutable ) { | |
740 | if ( options.staticExecutable ) { | |
d425e388 A |
741 | // x86_64 "static" or any "-static -pie" is really dynamic code model |
742 | if ( (options.arch == CPU_TYPE_X86_64) || options.pie ) | |
a645023d A |
743 | model = LTO_CODEGEN_PIC_MODEL_DYNAMIC; |
744 | else | |
745 | model = LTO_CODEGEN_PIC_MODEL_STATIC; | |
746 | } | |
747 | else { | |
748 | if ( options.pie ) | |
749 | model = LTO_CODEGEN_PIC_MODEL_DYNAMIC; | |
750 | else | |
751 | model = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC; | |
752 | } | |
753 | } | |
754 | else { | |
755 | if ( options.allowTextRelocs ) | |
756 | model = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC; | |
757 | else | |
758 | model = LTO_CODEGEN_PIC_MODEL_DYNAMIC; | |
759 | } | |
760 | if ( ::lto_codegen_set_pic_model(generator, model) ) | |
761 | throwf("could not create set codegen model: %s", lto_get_error_message()); | |
762 | ||
763 | // if requested, save off merged bitcode file | |
764 | if ( options.saveTemps ) { | |
765 | char tempBitcodePath[MAXPATHLEN]; | |
766 | strcpy(tempBitcodePath, options.outputFilePath); | |
767 | strcat(tempBitcodePath, ".lto.bc"); | |
eaf282aa A |
768 | #if LTO_API_VERSION >= 15 |
769 | ::lto_codegen_set_should_embed_uselists(generator, true); | |
770 | #endif | |
a645023d A |
771 | ::lto_codegen_write_merged_modules(generator, tempBitcodePath); |
772 | } | |
773 | ||
774 | #if LTO_API_VERSION >= 3 | |
775 | // find assembler next to linker | |
776 | char path[PATH_MAX]; | |
777 | uint32_t bufSize = PATH_MAX; | |
778 | if ( _NSGetExecutablePath(path, &bufSize) != -1 ) { | |
779 | char* lastSlash = strrchr(path, '/'); | |
780 | if ( lastSlash != NULL ) { | |
781 | strcpy(lastSlash+1, "as"); | |
782 | struct stat statInfo; | |
783 | if ( stat(path, &statInfo) == 0 ) | |
784 | ::lto_codegen_set_assembler_path(generator, path); | |
785 | } | |
786 | } | |
787 | #endif | |
eaf282aa A |
788 | |
789 | // When lto API version is greater than or equal to 12, we use lto_codegen_optimize and lto_codegen_compile_optimized | |
790 | // instead of lto_codegen_compile, and we save the merged bitcode file in between. | |
791 | bool useSplitAPI = false; | |
792 | #if LTO_API_VERSION >= 12 | |
793 | if ( ::lto_api_version() >= 12) | |
794 | useSplitAPI = true; | |
795 | #endif | |
796 | ||
797 | size_t machOFileLen = 0; | |
798 | const uint8_t* machOFile = NULL; | |
799 | if ( useSplitAPI) { | |
800 | #if LTO_API_VERSION >= 12 | |
801 | #if LTO_API_VERSION >= 14 | |
802 | if ( ::lto_api_version() >= 14 && options.ltoCodegenOnly) | |
803 | lto_codegen_set_should_internalize(generator, false); | |
804 | #endif | |
805 | // run optimizer | |
806 | if ( !options.ltoCodegenOnly && ::lto_codegen_optimize(generator) ) | |
807 | throwf("could not do LTO optimization: '%s', using libLTO version '%s'", ::lto_get_error_message(), ::lto_get_version()); | |
808 | ||
809 | if ( options.saveTemps || options.bitcodeBundle ) { | |
810 | // save off merged bitcode file | |
811 | char tempOptBitcodePath[MAXPATHLEN]; | |
812 | strcpy(tempOptBitcodePath, options.outputFilePath); | |
813 | strcat(tempOptBitcodePath, ".lto.opt.bc"); | |
814 | #if LTO_API_VERSION >= 15 | |
815 | ::lto_codegen_set_should_embed_uselists(generator, true); | |
816 | #endif | |
817 | ::lto_codegen_write_merged_modules(generator, tempOptBitcodePath); | |
818 | if ( options.bitcodeBundle ) | |
819 | state.ltoBitcodePath = tempOptBitcodePath; | |
820 | } | |
821 | ||
822 | // run code generator | |
823 | machOFile = (uint8_t*)::lto_codegen_compile_optimized(generator, &machOFileLen); | |
824 | #endif | |
825 | if ( machOFile == NULL ) | |
826 | throwf("could not do LTO codegen: '%s', using libLTO version '%s'", ::lto_get_error_message(), ::lto_get_version()); | |
827 | } | |
828 | else { | |
829 | // run optimizer and code generator | |
830 | machOFile = (uint8_t*)::lto_codegen_compile(generator, &machOFileLen); | |
831 | if ( machOFile == NULL ) | |
832 | throwf("could not do LTO codegen: '%s', using libLTO version '%s'", ::lto_get_error_message(), ::lto_get_version()); | |
833 | if ( options.saveTemps ) { | |
834 | // save off merged bitcode file | |
835 | char tempOptBitcodePath[MAXPATHLEN]; | |
836 | strcpy(tempOptBitcodePath, options.outputFilePath); | |
837 | strcat(tempOptBitcodePath, ".lto.opt.bc"); | |
838 | #if LTO_API_VERSION >= 15 | |
839 | ::lto_codegen_set_should_embed_uselists(generator, true); | |
840 | #endif | |
841 | ::lto_codegen_write_merged_modules(generator, tempOptBitcodePath); | |
842 | } | |
843 | } | |
a645023d A |
844 | |
845 | // if requested, save off temp mach-o file | |
846 | if ( options.saveTemps ) { | |
847 | char tempMachoPath[MAXPATHLEN]; | |
848 | strcpy(tempMachoPath, options.outputFilePath); | |
849 | strcat(tempMachoPath, ".lto.o"); | |
850 | int fd = ::open(tempMachoPath, O_CREAT | O_WRONLY | O_TRUNC, 0666); | |
851 | if ( fd != -1) { | |
852 | ::write(fd, machOFile, machOFileLen); | |
853 | ::close(fd); | |
854 | } | |
a645023d A |
855 | } |
856 | ||
857 | // if needed, save temp mach-o file to specific location | |
858 | if ( options.tmpObjectFilePath != NULL ) { | |
859 | int fd = ::open(options.tmpObjectFilePath, O_CREAT | O_WRONLY | O_TRUNC, 0666); | |
860 | if ( fd != -1) { | |
861 | ::write(fd, machOFile, machOFileLen); | |
862 | ::close(fd); | |
863 | } | |
864 | else { | |
865 | warning("could not write LTO temp file '%s', errno=%d", options.tmpObjectFilePath, errno); | |
866 | } | |
867 | } | |
868 | ||
869 | // parse generated mach-o file into a MachOReader | |
ebf6f434 | 870 | ld::relocatable::File* machoFile = parseMachOFile(machOFile, machOFileLen, options); |
a645023d A |
871 | |
872 | // sync generated mach-o atoms with existing atoms ld knows about | |
873 | if ( logAtomsBeforeSync ) { | |
874 | fprintf(stderr, "llvmAtoms:\n"); | |
875 | for (CStringToAtom::iterator it = llvmAtoms.begin(); it != llvmAtoms.end(); ++it) { | |
876 | const char* name = it->first; | |
599556ff A |
877 | Atom* atom = it->second; |
878 | fprintf(stderr, "\t%p\t%s\n", atom, name); | |
a645023d A |
879 | } |
880 | fprintf(stderr, "deadllvmAtoms:\n"); | |
881 | for (CStringToAtom::iterator it = deadllvmAtoms.begin(); it != deadllvmAtoms.end(); ++it) { | |
882 | const char* name = it->first; | |
599556ff A |
883 | Atom* atom = it->second; |
884 | fprintf(stderr, "\t%p\t%s\n", atom, name); | |
a645023d A |
885 | } |
886 | } | |
887 | AtomSyncer syncer(additionalUndefines, newAtoms, llvmAtoms, deadllvmAtoms, options); | |
888 | machoFile->forEachAtom(syncer); | |
889 | ||
890 | // Remove InternalAtoms from ld | |
891 | for (std::vector<File*>::iterator it=_s_files.begin(); it != _s_files.end(); ++it) { | |
892 | (*it)->internalAtom().setCoalescedAway(); | |
893 | } | |
894 | // Remove Atoms from ld if code generator optimized them away | |
895 | for (CStringToAtom::iterator li = llvmAtoms.begin(), le = llvmAtoms.end(); li != le; ++li) { | |
896 | // check if setRealAtom() called on this Atom | |
897 | if ( li->second->compiledAtom() == NULL ) { | |
898 | //fprintf(stderr, "llvm optimized away %p %s\n", li->second, li->second->name()); | |
899 | li->second->setCoalescedAway(); | |
900 | } | |
901 | } | |
902 | ||
903 | // notify about file level attributes | |
904 | handler.doFile(*machoFile); | |
905 | ||
906 | // if final mach-o file has debug info, update original bitcode files to match | |
907 | for (std::vector<File*>::iterator it=_s_files.begin(); it != _s_files.end(); ++it) { | |
908 | (*it)->setDebugInfo(machoFile->debugInfo(), machoFile->path(), | |
909 | machoFile->modificationTime(), machoFile->cpuSubType()); | |
910 | } | |
911 | ||
912 | return true; | |
913 | } | |
914 | ||
915 | ||
916 | void Parser::AtomSyncer::doAtom(const ld::Atom& machoAtom) | |
917 | { | |
599556ff | 918 | static const bool log = false; |
9543cb2f A |
919 | static const ld::Atom* lastProxiedAtom = NULL; |
920 | static const ld::File* lastProxiedFile = NULL; | |
a645023d A |
921 | // update proxy atoms to point to real atoms and find new atoms |
922 | const char* name = machoAtom.name(); | |
599556ff A |
923 | CStringToAtom::iterator pos = _llvmAtoms.find(name); |
924 | if ( pos != _llvmAtoms.end() ) { | |
925 | // turn Atom into a proxy for this mach-o atom | |
926 | pos->second->setCompiledAtom(machoAtom); | |
927 | lastProxiedAtom = &machoAtom; | |
928 | lastProxiedFile = pos->second->file(); | |
929 | if (log) fprintf(stderr, "AtomSyncer, mach-o atom %p synced to lto atom %p (name=%s)\n", &machoAtom, pos->second, machoAtom.name()); | |
930 | } | |
931 | else { | |
932 | // an atom of this name was not in the allAtoms list the linker gave us | |
933 | if ( _deadllvmAtoms.find(name) != _deadllvmAtoms.end() ) { | |
934 | // this corresponding to an atom that the linker coalesced away or marked not-live | |
935 | if ( _options.linkerDeadStripping ) { | |
936 | // llvm seems to want this atom and -dead_strip is enabled, so it will be deleted if not needed, so add back | |
937 | Atom* llvmAtom = _deadllvmAtoms[name]; | |
938 | llvmAtom->setCompiledAtom(machoAtom); | |
a645023d | 939 | _newAtoms.push_back(&machoAtom); |
599556ff | 940 | if (log) fprintf(stderr, "AtomSyncer, mach-o atom %p matches dead lto atom %p but adding back (name=%s)\n", &machoAtom, llvmAtom, machoAtom.name()); |
a645023d | 941 | } |
599556ff A |
942 | else { |
943 | // Don't pass it back as a new atom | |
944 | if (log) fprintf(stderr, "AtomSyncer, mach-o atom %p matches dead lto atom %p (name=%s)\n", &machoAtom, _deadllvmAtoms[name], machoAtom.name()); | |
945 | } | |
a645023d | 946 | } |
599556ff A |
947 | else |
948 | { | |
949 | // this is something new that lto conjured up, tell ld its new | |
950 | _newAtoms.push_back(&machoAtom); | |
951 | // <rdar://problem/15469363> if new static atom in same section as previous non-static atom, assign to same file as previous | |
952 | if ( (lastProxiedAtom != NULL) && (lastProxiedAtom->section() == machoAtom.section()) ) { | |
953 | ld::Atom* ma = const_cast<ld::Atom*>(&machoAtom); | |
954 | ma->setFile(lastProxiedFile); | |
955 | } | |
956 | if (log) fprintf(stderr, "AtomSyncer, mach-o atom %p is totally new (name=%s)\n", &machoAtom, machoAtom.name()); | |
9543cb2f | 957 | } |
a645023d A |
958 | } |
959 | ||
960 | // adjust fixups to go through proxy atoms | |
599556ff | 961 | if (log) fprintf(stderr, " adjusting fixups in atom: %s\n", machoAtom.name()); |
a645023d A |
962 | for (ld::Fixup::iterator fit=machoAtom.fixupsBegin(); fit != machoAtom.fixupsEnd(); ++fit) { |
963 | switch ( fit->binding ) { | |
964 | case ld::Fixup::bindingNone: | |
965 | break; | |
966 | case ld::Fixup::bindingByNameUnbound: | |
967 | // don't know if this target has been seen by linker before or if it is new | |
968 | // be conservative and tell linker it is new | |
969 | _additionalUndefines.push_back(fit->u.name); | |
599556ff | 970 | if (log) fprintf(stderr, " adding by-name symbol %s\n", fit->u.name); |
a645023d A |
971 | break; |
972 | case ld::Fixup::bindingDirectlyBound: | |
973 | // If mach-o atom is referencing another mach-o atom then | |
974 | // reference is not going through Atom proxy. Fix it here to ensure that all | |
975 | // llvm symbol references always go through Atom proxy. | |
599556ff | 976 | { |
a645023d | 977 | const char* targetName = fit->u.target->name(); |
599556ff A |
978 | CStringToAtom::iterator post = _llvmAtoms.find(targetName); |
979 | if ( post != _llvmAtoms.end() ) { | |
980 | const ld::Atom* t = post->second; | |
981 | if (log) fprintf(stderr, " updating direct reference to %p to be ref to %p: %s\n", fit->u.target, t, targetName); | |
982 | fit->u.target = t; | |
a645023d A |
983 | } |
984 | else { | |
f80fe69f | 985 | // <rdar://problem/12859831> Don't unbind follow-on reference into by-name reference |
eaf282aa | 986 | if ( (_deadllvmAtoms.find(targetName) != _deadllvmAtoms.end()) && (fit->kind != ld::Fixup::kindNoneFollowOn) && (fit->u.target->scope() != ld::Atom::scopeTranslationUnit) ) { |
a645023d A |
987 | // target was coalesed away and replace by mach-o atom from a non llvm .o file |
988 | fit->binding = ld::Fixup::bindingByNameUnbound; | |
989 | fit->u.name = targetName; | |
990 | } | |
991 | } | |
992 | } | |
993 | //fprintf(stderr, " direct ref to: %s (scope=%d)\n", fit->u.target->name(), fit->u.target->scope()); | |
994 | break; | |
995 | case ld::Fixup::bindingByContentBound: | |
996 | //fprintf(stderr, " direct by content to: %s\n", fit->u.target->name()); | |
997 | break; | |
998 | case ld::Fixup::bindingsIndirectlyBound: | |
999 | assert(0 && "indirect binding found in initial mach-o file?"); | |
1000 | //fprintf(stderr, " indirect by content to: %u\n", fit->u.bindingIndex); | |
1001 | break; | |
1002 | } | |
1003 | } | |
1004 | ||
1005 | } | |
1006 | ||
ebf6f434 A |
1007 | class Mutex { |
1008 | static pthread_mutex_t lto_lock; | |
1009 | public: | |
1010 | Mutex() { pthread_mutex_lock(<o_lock); } | |
1011 | ~Mutex() { pthread_mutex_unlock(<o_lock); } | |
1012 | }; | |
1013 | pthread_mutex_t Mutex::lto_lock = PTHREAD_MUTEX_INITIALIZER; | |
eaf282aa A |
1014 | bool File::sSupportsLocalContext = false; |
1015 | bool File::sHasTriedLocalContext = false; | |
a645023d A |
1016 | |
1017 | // | |
1018 | // Used by archive reader to see if member is an llvm bitcode file | |
1019 | // | |
1020 | bool isObjectFile(const uint8_t* fileContent, uint64_t fileLength, cpu_type_t architecture, cpu_subtype_t subarch) | |
1021 | { | |
ebf6f434 | 1022 | Mutex lock; |
a645023d A |
1023 | return Parser::validFile(fileContent, fileLength, architecture, subarch); |
1024 | } | |
1025 | ||
eaf282aa A |
1026 | static ld::relocatable::File *parseImpl( |
1027 | const uint8_t *fileContent, uint64_t fileLength, const char *path, | |
1028 | time_t modTime, ld::File::Ordinal ordinal, cpu_type_t architecture, | |
1029 | cpu_subtype_t subarch, bool logAllFiles, | |
1030 | bool verboseOptimizationHints) | |
1031 | { | |
1032 | if ( Parser::validFile(fileContent, fileLength, architecture, subarch) ) | |
1033 | return Parser::parse(fileContent, fileLength, path, modTime, ordinal, architecture, subarch, logAllFiles, verboseOptimizationHints); | |
1034 | else | |
1035 | return NULL; | |
1036 | } | |
1037 | ||
a645023d A |
1038 | // |
1039 | // main function used by linker to instantiate ld::Files | |
1040 | // | |
1041 | ld::relocatable::File* parse(const uint8_t* fileContent, uint64_t fileLength, | |
b1f7435d | 1042 | const char* path, time_t modTime, ld::File::Ordinal ordinal, |
9543cb2f A |
1043 | cpu_type_t architecture, cpu_subtype_t subarch, bool logAllFiles, |
1044 | bool verboseOptimizationHints) | |
a645023d | 1045 | { |
ec29ba20 A |
1046 | // do light weight check before acquiring lock |
1047 | if ( fileLength < 4 ) | |
1048 | return NULL; | |
1049 | if ( (fileContent[0] != 0xDE) || (fileContent[1] != 0xC0) || (fileContent[2] != 0x17) || (fileContent[3] != 0x0B) ) | |
1050 | return NULL; | |
1051 | ||
eaf282aa A |
1052 | // Note: Once lto_module_create_in_local_context() and friends are thread safe |
1053 | // this lock can be removed. | |
ebf6f434 | 1054 | Mutex lock; |
eaf282aa A |
1055 | return parseImpl(fileContent, fileLength, path, modTime, ordinal, |
1056 | architecture, subarch, logAllFiles, | |
1057 | verboseOptimizationHints); | |
a645023d A |
1058 | } |
1059 | ||
1060 | // | |
1061 | // used by "ld -v" to report version of libLTO.dylib being used | |
1062 | // | |
1063 | const char* version() | |
1064 | { | |
ebf6f434 | 1065 | Mutex lock; |
a645023d A |
1066 | return ::lto_get_version(); |
1067 | } | |
1068 | ||
1069 | ||
1070 | // | |
1071 | // used by ld for error reporting | |
1072 | // | |
1073 | bool libLTOisLoaded() | |
1074 | { | |
ebf6f434 | 1075 | Mutex lock; |
a645023d A |
1076 | return (::lto_get_version() != NULL); |
1077 | } | |
1078 | ||
1079 | // | |
1080 | // used by ld for error reporting | |
1081 | // | |
1082 | const char* archName(const uint8_t* fileContent, uint64_t fileLength) | |
1083 | { | |
ebf6f434 | 1084 | Mutex lock; |
a645023d A |
1085 | return Parser::fileKind(fileContent, fileLength); |
1086 | } | |
1087 | ||
1088 | // | |
1089 | // used by ld for doing link time optimization | |
1090 | // | |
1091 | bool optimize( const std::vector<const ld::Atom*>& allAtoms, | |
1092 | ld::Internal& state, | |
a645023d A |
1093 | const OptimizeOptions& options, |
1094 | ld::File::AtomHandler& handler, | |
1095 | std::vector<const ld::Atom*>& newAtoms, | |
1096 | std::vector<const char*>& additionalUndefines) | |
1097 | { | |
ebf6f434 A |
1098 | Mutex lock; |
1099 | return Parser::optimize(allAtoms, state, options, handler, newAtoms, additionalUndefines); | |
a645023d A |
1100 | } |
1101 | ||
1102 | ||
1103 | ||
1104 | }; // namespace lto | |
1105 | ||
1106 | ||
1107 | #endif | |
1108 |