1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2006-2010 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 __LTO_READER_H__
26 #define __LTO_READER_H__
29 #include <sys/param.h>
30 #include <sys/fcntl.h>
34 #include <mach-o/dyld.h>
37 #include <unordered_set>
38 #include <unordered_map>
40 #include "MachOFileAbstraction.hpp"
41 #include "Architectures.hpp"
43 #include "macho_relocatable_file.h"
46 // #defines are a work around for <rdar://problem/8760268>
47 #define __STDC_LIMIT_MACROS 1
48 #define __STDC_CONSTANT_MACROS 1
49 #include "llvm-c/lto.h"
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.
61 class InternalAtom
: public ld::Atom
64 InternalAtom(class File
& f
);
65 // overrides of ld::Atom
66 virtual ld::File
* file() const { return &_file
; }
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()]; }
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
,
78 ld::Fixup::kindNone
, false, strdup(nm
))); }
82 mutable std::vector
<ld::Fixup
> _undefs
;
89 class File
: public ld::relocatable::File
92 File(const char* path
, time_t mTime
, ld::File::Ordinal ordinal
,
93 const uint8_t* content
, uint32_t contentLength
, cpu_type_t arch
);
96 // overrides of ld::File
97 virtual bool forEachAtom(ld::File::AtomHandler
&) const;
98 virtual bool justInTimeforEachAtom(const char* name
, ld::File::AtomHandler
&) const
100 virtual uint32_t cpuSubType() const { return _cpuSubType
; }
102 // overrides of ld::relocatable::File
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; }
109 virtual LinkerOptionsList
* linkerOptions() const { return NULL
; }
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
)
118 _debugInfoPath
= pth
;
119 _debugInfoModTime
= modTime
;
120 _cpuSubType
= subtype
;}
122 static bool sSupportsLocalContext
;
123 static bool sHasTriedLocalContext
;
124 bool mergeIntoGenerator(lto_code_gen_t generator
, bool useSetModule
);
127 friend class InternalAtom
;
130 cpu_type_t _architecture
;
131 class InternalAtom _internalAtom
;
132 class Atom
* _atomArray
;
133 uint32_t _atomArrayCount
;
134 lto_module_t _module
;
136 const uint8_t* _content
;
137 uint32_t _contentLength
;
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
;
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.
153 class Atom
: public ld::Atom
156 Atom(File
& f
, const char* name
, ld::Atom::Scope s
,
157 ld::Atom::Definition d
, ld::Atom::Combine c
, ld::Atom::Alignment a
, bool ah
);
159 // overrides of ld::Atom
160 virtual ld::File
* file() const { return &_file
; }
161 virtual const char* translationUnitSource() const
162 { return (_compiledAtom
? _compiledAtom
->translationUnitSource() : NULL
); }
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
); }
187 const ld::Atom
* compiledAtom() { return _compiledAtom
; }
188 void setCompiledAtom(const ld::Atom
& atom
);
194 const ld::Atom
* _compiledAtom
;
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
,
209 time_t modTime
, ld::File::Ordinal ordinal
, cpu_type_t architecture
, cpu_subtype_t subarch
,
210 bool logAllFiles
, bool verboseOptimizationHints
);
211 static bool libLTOisLoaded() { return (::lto_get_version() != NULL
); }
212 static bool optimize( const std::vector
<const ld::Atom
*>& allAtoms
,
214 const OptimizeOptions
& options
,
215 ld::File::AtomHandler
& handler
,
216 std::vector
<const ld::Atom
*>& newAtoms
,
217 std::vector
<const char*>& additionalUndefines
);
219 static const char* ltoVersion() { return ::lto_get_version(); }
222 static const char* tripletPrefixForArch(cpu_type_t arch
);
223 static ld::relocatable::File
* parseMachOFile(const uint8_t* p
, size_t len
, const OptimizeOptions
& options
);
224 #if LTO_API_VERSION >= 7
225 static void ltoDiagnosticHandler(lto_codegen_diagnostic_severity_t
, const char*, void*);
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
;
231 class AtomSyncer
: public ld::File::AtomHandler
{
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
&) { }
240 const OptimizeOptions
& _options
;
241 std::vector
<const char*>& _additionalUndefines
;
242 std::vector
<const ld::Atom
*>& _newAtoms
;
243 CStringToAtom _llvmAtoms
;
244 CStringToAtom _deadllvmAtoms
;
247 static std::vector
<File
*> _s_files
;
250 std::vector
<File
*> Parser::_s_files
;
253 bool Parser::validFile(const uint8_t* fileContent
, uint64_t fileLength
, cpu_type_t architecture
, cpu_subtype_t subarch
)
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
);
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
);
270 const char* Parser::fileKind(const uint8_t* p
, uint64_t fileLength
)
272 if ( (p
[0] == 0xDE) && (p
[1] == 0xC0) && (p
[2] == 0x17) && (p
[3] == 0x0B) ) {
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
) {
277 if ( ::lto_module_is_object_file_in_memory_for_target(p
, fileLength
, t
->llvmTriplePrefix
) )
285 return "unknown bitcode architecture";
290 File
* Parser::parse(const uint8_t* fileContent
, uint64_t fileLength
, const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
291 cpu_type_t architecture
, cpu_subtype_t subarch
, bool logAllFiles
, bool verboseOptimizationHints
)
293 File
* f
= new File(path
, modTime
, ordinal
, fileContent
, fileLength
, architecture
);
294 _s_files
.push_back(f
);
296 printf("%s\n", path
);
301 ld::relocatable::File
* Parser::parseMachOFile(const uint8_t* p
, size_t len
, const OptimizeOptions
& options
)
303 mach_o::relocatable::ParserOptions objOpts
;
304 objOpts
.architecture
= options
.arch
;
305 objOpts
.objSubtypeMustMatch
= false;
306 objOpts
.logAllFiles
= false;
307 objOpts
.warnUnwindConversionProblems
= options
.needsUnwindInfoSection
;
308 objOpts
.keepDwarfUnwind
= options
.keepDwarfUnwind
;
309 objOpts
.forceDwarfConversion
= false;
310 objOpts
.neverConvertDwarf
= false;
311 objOpts
.verboseOptimizationHints
= options
.verboseOptimizationHints
;
312 objOpts
.armUsesZeroCostExceptions
= options
.armUsesZeroCostExceptions
;
313 objOpts
.simulator
= options
.simulator
;
314 objOpts
.ignoreMismatchPlatform
= options
.ignoreMismatchPlatform
;
315 objOpts
.platform
= options
.platform
;
317 objOpts
.srcKind
= ld::relocatable::File::kSourceLTO
;
318 objOpts
.treateBitcodeAsData
= false;
319 objOpts
.usingBitcode
= options
.bitcodeBundle
;
320 objOpts
.maxDefaultCommonAlignment
= options
.maxDefaultCommonAlignment
;
322 // mach-o parsing is done in-memory, but need path for debug notes
323 const char* path
= "/tmp/lto.o";
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
;
332 ld::relocatable::File
* result
= mach_o::relocatable::parse(p
, len
, path
, modTime
, ld::File::Ordinal::LTOOrdinal(), objOpts
);
333 if ( result
!= NULL
)
335 throw "LLVM LTO, file is not of required architecture";
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),
342 _atomArray(NULL
), _atomArrayCount(0), _module(NULL
), _path(pth
),
343 _content(content
), _contentLength(contentLength
), _debugInfoPath(pth
),
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)
348 const bool log
= false;
350 // create llvm module
351 #if LTO_API_VERSION >= 11
352 if ( sSupportsLocalContext
|| !sHasTriedLocalContext
) {
353 _module
= ::lto_module_create_in_local_context(content
, contentLength
, pth
);
355 if ( !sHasTriedLocalContext
) {
356 sHasTriedLocalContext
= true;
357 sSupportsLocalContext
= (_module
!= NULL
);
359 if ( (_module
== NULL
) && !sSupportsLocalContext
)
361 #if LTO_API_VERSION >= 9
362 _module
= ::lto_module_create_from_memory_with_path(content
, contentLength
, pth
);
363 if ( _module
== NULL
&& !sSupportsLocalContext
)
365 _module
= ::lto_module_create_from_memory(content
, contentLength
);
366 if ( _module
== NULL
)
367 throwf("could not parse object file %s: '%s', using libLTO version '%s'", pth
, ::lto_get_error_message(), ::lto_get_version());
369 if ( log
) fprintf(stderr
, "bitcode file: %s\n", pth
);
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
);
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) )
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
;
390 case LTO_SYMBOL_DEFINITION_TENTATIVE
:
391 def
= ld::Atom::definitionTentative
;
393 case LTO_SYMBOL_DEFINITION_WEAK
:
394 def
= ld::Atom::definitionRegular
;
395 combine
= ld::Atom::combineByName
;
397 case LTO_SYMBOL_DEFINITION_UNDEFINED
:
398 case LTO_SYMBOL_DEFINITION_WEAKUNDEF
:
399 def
= ld::Atom::definitionProxy
;
402 throwf("unknown definition kind for symbol %s in bitcode file %s", name
, pth
);
405 // make LLVM atoms for definitions and a reference for undefines
406 if ( def
!= ld::Atom::definitionProxy
) {
407 ld::Atom::Scope scope
;
408 bool autohide
= false;
409 switch ( attr
& LTO_SYMBOL_SCOPE_MASK
) {
410 case LTO_SYMBOL_SCOPE_INTERNAL
:
411 scope
= ld::Atom::scopeTranslationUnit
;
413 case LTO_SYMBOL_SCOPE_HIDDEN
:
414 scope
= ld::Atom::scopeLinkageUnit
;
416 case LTO_SYMBOL_SCOPE_DEFAULT
:
417 scope
= ld::Atom::scopeGlobal
;
419 #if LTO_API_VERSION >= 4
420 case LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN
:
421 scope
= ld::Atom::scopeGlobal
;
426 throwf("unknown scope for symbol %s in bitcode file %s", name
, pth
);
428 // only make atoms for non-internal symbols
429 if ( scope
== ld::Atom::scopeTranslationUnit
)
431 uint8_t alignment
= (attr
& LTO_SYMBOL_ALIGNMENT_MASK
);
432 // make Atom using placement new operator
433 new (&_atomArray
[_atomArrayCount
++]) Atom(*this, name
, scope
, def
, combine
, alignment
, autohide
);
434 if ( scope
!= ld::Atom::scopeTranslationUnit
)
435 _internalAtom
.addReference(name
);
436 if ( log
) fprintf(stderr
, "\t0x%08X %s\n", attr
, name
);
439 // add to list of external references
440 _internalAtom
.addReference(name
);
441 if ( log
) fprintf(stderr
, "\t%s (undefined)\n", name
);
445 #if LTO_API_VERSION >= 11
446 if ( sSupportsLocalContext
)
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
,
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());
467 assert(_module
&& "Expected module to stick around");
468 #if LTO_API_VERSION >= 13
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
);
476 if ( ::lto_codegen_add_module(generator
, _module
) )
479 // <rdar://problem/15471128> linker should release module as soon as possible
486 if ( _module
!= NULL
)
487 ::lto_module_dispose(_module
);
491 bool File::forEachAtom(ld::File::AtomHandler
& handler
) const
493 handler
.doAtom(_internalAtom
);
494 for(uint32_t i
=0; i
< _atomArrayCount
; ++i
) {
495 handler
.doAtom(_atomArray
[i
]);
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)),
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
)
509 : ld::Atom(f
._section
, d
, c
, s
, ld::Atom::typeLTOtemporary
,
510 ld::Atom::symbolTableIn
, false, false, false, a
),
511 _file(f
), _name(strdup(nm
)), _compiledAtom(NULL
)
517 void Atom::setCompiledAtom(const ld::Atom
& atom
)
519 // set delegate so virtual methods go to it
520 _compiledAtom
= &atom
;
522 //fprintf(stderr, "setting lto atom %p to delegate to mach-o atom %p (%s)\n", this, &atom, atom.name());
524 // update fields in ld::Atom to match newly constructed mach-o atom
525 (const_cast<Atom
*>(this))->setAttributesFromAtom(atom
);
530 // <rdar://problem/12379604> The order that files are merged must match command line order
531 struct CommandLineOrderFileSorter
533 bool operator()(File
* left
, File
* right
)
535 return ( left
->ordinal() < right
->ordinal() );
540 #if LTO_API_VERSION >= 7
541 void Parser::ltoDiagnosticHandler(lto_codegen_diagnostic_severity_t severity
, const char* message
, void*)
543 switch ( severity
) {
544 #if LTO_API_VERSION >= 10
546 fprintf(stderr
, "ld: LTO remark: %s\n", message
);
551 warning("%s", message
);
554 throwf("%s", message
);
559 bool Parser::optimize( const std::vector
<const ld::Atom
*>& allAtoms
,
561 const OptimizeOptions
& options
,
562 ld::File::AtomHandler
& handler
,
563 std::vector
<const ld::Atom
*>& newAtoms
,
564 std::vector
<const char*>& additionalUndefines
)
566 const bool logMustPreserve
= false;
567 const bool logExtraOptions
= false;
568 const bool logBitcodeFiles
= false;
569 const bool logAtomsBeforeSync
= false;
571 // exit quickly if nothing to do
572 if ( _s_files
.size() == 0 )
575 // print out LTO version string if -v was used
576 if ( options
.verbose
)
577 fprintf(stderr
, "%s\n", ::lto_get_version());
579 // create optimizer and add each Reader
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();
586 generator
= ::lto_codegen_create();
587 #if LTO_API_VERSION >= 7
588 lto_codegen_set_diagnostic_handler(generator
, ltoDiagnosticHandler
, NULL
);
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
;
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);
601 for (std::vector
<File
*>::iterator it
=_s_files
.begin(); it
!= _s_files
.end(); ++it
) {
603 assert(f
->ordinal() > lastOrdinal
);
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
) )
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();
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
);
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
);
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;
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());
643 case ld::Fixup::bindingsIndirectlyBound
:
644 target
= state
.indirectBindingTable
[fit
->u
.bindingIndex
];
645 if ( (target
!= NULL
) && (target
->contentType() == ld::Atom::typeLTOtemporary
) )
646 nonLLVMRefs
.insert(target
->name());
652 else if ( atom
->scope() >= ld::Atom::scopeLinkageUnit
) {
653 llvmAtoms
[atom
->name()] = (Atom
*)atom
;
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());
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
;
676 for (std::vector
<File
*>::iterator it
=_s_files
.begin(); it
!= _s_files
.end(); ++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
;
689 else if ( options
.linkerDeadStripping
&& !llvmAtom
->live() ) {
690 const char* name
= llvmAtom
->name();
691 deadllvmAtoms
[name
] = (Atom
*)llvmAtom
;
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.
704 if ( (atom
->scope() == ld::Atom::scopeGlobal
) && options
.preserveAllGlobals
) {
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
);
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
);
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
);
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
);
725 // special case running ld -r on all bitcode files to produce another bitcode file (instead of mach-o)
726 if ( options
.relocatable
&& !hasNonllvmAtoms
) {
727 #if LTO_API_VERSION >= 15
728 ::lto_codegen_set_should_embed_uselists(generator
, false);
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
734 warning("could not produce merged bitcode file");
737 // set code-gen model
738 lto_codegen_model model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC
;
739 if ( options
.mainExecutable
) {
740 if ( options
.staticExecutable
) {
741 // x86_64 "static" or any "-static -pie" is really dynamic code model
742 if ( (options
.arch
== CPU_TYPE_X86_64
) || options
.pie
)
743 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC
;
745 model
= LTO_CODEGEN_PIC_MODEL_STATIC
;
749 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC
;
751 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC
;
755 if ( options
.allowTextRelocs
)
756 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC
;
758 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC
;
760 if ( ::lto_codegen_set_pic_model(generator
, model
) )
761 throwf("could not create set codegen model: %s", lto_get_error_message());
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");
768 #if LTO_API_VERSION >= 15
769 ::lto_codegen_set_should_embed_uselists(generator
, true);
771 ::lto_codegen_write_merged_modules(generator
, tempBitcodePath
);
774 #if LTO_API_VERSION >= 3
775 // find assembler next to linker
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
);
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)
797 size_t machOFileLen
= 0;
798 const uint8_t* machOFile
= NULL
;
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);
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());
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);
817 ::lto_codegen_write_merged_modules(generator
, tempOptBitcodePath
);
818 if ( options
.bitcodeBundle
)
819 state
.ltoBitcodePath
= tempOptBitcodePath
;
822 // run code generator
823 machOFile
= (uint8_t*)::lto_codegen_compile_optimized(generator
, &machOFileLen
);
825 if ( machOFile
== NULL
)
826 throwf("could not do LTO codegen: '%s', using libLTO version '%s'", ::lto_get_error_message(), ::lto_get_version());
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);
841 ::lto_codegen_write_merged_modules(generator
, tempOptBitcodePath
);
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);
852 ::write(fd
, machOFile
, machOFileLen
);
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);
861 ::write(fd
, machOFile
, machOFileLen
);
865 warning("could not write LTO temp file '%s', errno=%d", options
.tmpObjectFilePath
, errno
);
869 // parse generated mach-o file into a MachOReader
870 ld::relocatable::File
* machoFile
= parseMachOFile(machOFile
, machOFileLen
, options
);
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
;
877 Atom
* atom
= it
->second
;
878 fprintf(stderr
, "\t%p\t%s\n", atom
, name
);
880 fprintf(stderr
, "deadllvmAtoms:\n");
881 for (CStringToAtom::iterator it
= deadllvmAtoms
.begin(); it
!= deadllvmAtoms
.end(); ++it
) {
882 const char* name
= it
->first
;
883 Atom
* atom
= it
->second
;
884 fprintf(stderr
, "\t%p\t%s\n", atom
, name
);
887 AtomSyncer
syncer(additionalUndefines
, newAtoms
, llvmAtoms
, deadllvmAtoms
, options
);
888 machoFile
->forEachAtom(syncer
);
890 // Remove InternalAtoms from ld
891 for (std::vector
<File
*>::iterator it
=_s_files
.begin(); it
!= _s_files
.end(); ++it
) {
892 (*it
)->internalAtom().setCoalescedAway();
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();
903 // notify about file level attributes
904 handler
.doFile(*machoFile
);
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());
916 void Parser::AtomSyncer::doAtom(const ld::Atom
& machoAtom
)
918 static const bool log
= false;
919 static const ld::Atom
* lastProxiedAtom
= NULL
;
920 static const ld::File
* lastProxiedFile
= NULL
;
921 // update proxy atoms to point to real atoms and find new atoms
922 const char* name
= machoAtom
.name();
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());
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
);
939 _newAtoms
.push_back(&machoAtom
);
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());
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());
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
);
956 if (log
) fprintf(stderr
, "AtomSyncer, mach-o atom %p is totally new (name=%s)\n", &machoAtom
, machoAtom
.name());
960 // adjust fixups to go through proxy atoms
961 if (log
) fprintf(stderr
, " adjusting fixups in atom: %s\n", machoAtom
.name());
962 for (ld::Fixup::iterator fit
=machoAtom
.fixupsBegin(); fit
!= machoAtom
.fixupsEnd(); ++fit
) {
963 switch ( fit
->binding
) {
964 case ld::Fixup::bindingNone
:
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
);
970 if (log
) fprintf(stderr
, " adding by-name symbol %s\n", fit
->u
.name
);
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.
977 const char* targetName
= fit
->u
.target
->name();
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
);
985 // <rdar://problem/12859831> Don't unbind follow-on reference into by-name reference
986 if ( (_deadllvmAtoms
.find(targetName
) != _deadllvmAtoms
.end()) && (fit
->kind
!= ld::Fixup::kindNoneFollowOn
) && (fit
->u
.target
->scope() != ld::Atom::scopeTranslationUnit
) ) {
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
;
993 //fprintf(stderr, " direct ref to: %s (scope=%d)\n", fit->u.target->name(), fit->u.target->scope());
995 case ld::Fixup::bindingByContentBound
:
996 //fprintf(stderr, " direct by content to: %s\n", fit->u.target->name());
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);
1008 static pthread_mutex_t lto_lock
;
1010 Mutex() { pthread_mutex_lock(<o_lock
); }
1011 ~Mutex() { pthread_mutex_unlock(<o_lock
); }
1013 pthread_mutex_t
Mutex::lto_lock
= PTHREAD_MUTEX_INITIALIZER
;
1014 bool File::sSupportsLocalContext
= false;
1015 bool File::sHasTriedLocalContext
= false;
1018 // Used by archive reader to see if member is an llvm bitcode file
1020 bool isObjectFile(const uint8_t* fileContent
, uint64_t fileLength
, cpu_type_t architecture
, cpu_subtype_t subarch
)
1023 return Parser::validFile(fileContent
, fileLength
, architecture
, subarch
);
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
)
1032 if ( Parser::validFile(fileContent
, fileLength
, architecture
, subarch
) )
1033 return Parser::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, architecture
, subarch
, logAllFiles
, verboseOptimizationHints
);
1039 // main function used by linker to instantiate ld::Files
1041 ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
1042 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
1043 cpu_type_t architecture
, cpu_subtype_t subarch
, bool logAllFiles
,
1044 bool verboseOptimizationHints
)
1046 // do light weight check before acquiring lock
1047 if ( fileLength
< 4 )
1049 if ( (fileContent
[0] != 0xDE) || (fileContent
[1] != 0xC0) || (fileContent
[2] != 0x17) || (fileContent
[3] != 0x0B) )
1052 // Note: Once lto_module_create_in_local_context() and friends are thread safe
1053 // this lock can be removed.
1055 return parseImpl(fileContent
, fileLength
, path
, modTime
, ordinal
,
1056 architecture
, subarch
, logAllFiles
,
1057 verboseOptimizationHints
);
1061 // used by "ld -v" to report version of libLTO.dylib being used
1063 const char* version()
1066 return ::lto_get_version();
1071 // used by ld for error reporting
1073 bool libLTOisLoaded()
1076 return (::lto_get_version() != NULL
);
1080 // used by ld for error reporting
1082 const char* archName(const uint8_t* fileContent
, uint64_t fileLength
)
1085 return Parser::fileKind(fileContent
, fileLength
);
1089 // used by ld for doing link time optimization
1091 bool optimize( const std::vector
<const ld::Atom
*>& allAtoms
,
1092 ld::Internal
& state
,
1093 const OptimizeOptions
& options
,
1094 ld::File::AtomHandler
& handler
,
1095 std::vector
<const ld::Atom
*>& newAtoms
,
1096 std::vector
<const char*>& additionalUndefines
)
1099 return Parser::optimize(allAtoms
, state
, options
, handler
, newAtoms
, additionalUndefines
);