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
;
319 // mach-o parsing is done in-memory, but need path for debug notes
320 const char* path
= "/tmp/lto.o";
322 if ( options
.tmpObjectFilePath
!= NULL
) {
323 path
= options
.tmpObjectFilePath
;
324 struct stat statBuffer
;
325 if ( stat(options
.tmpObjectFilePath
, &statBuffer
) == 0 )
326 modTime
= statBuffer
.st_mtime
;
329 ld::relocatable::File
* result
= mach_o::relocatable::parse(p
, len
, path
, modTime
, ld::File::Ordinal::LTOOrdinal(), objOpts
);
330 if ( result
!= NULL
)
332 throw "LLVM LTO, file is not of required architecture";
337 File::File(const char* pth
, time_t mTime
, ld::File::Ordinal ordinal
, const uint8_t* content
, uint32_t contentLength
, cpu_type_t arch
)
338 : ld::relocatable::File(pth
,mTime
,ordinal
), _architecture(arch
), _internalAtom(*this),
339 _atomArray(NULL
), _atomArrayCount(0), _module(NULL
), _path(pth
),
340 _content(content
), _contentLength(contentLength
), _debugInfoPath(pth
),
341 _section("__TEXT_", "__tmp_lto", ld::Section::typeTempLTO
),
342 _fixupToInternal(0, ld::Fixup::k1of1
, ld::Fixup::kindNone
, &_internalAtom
),
343 _debugInfo(ld::relocatable::File::kDebugInfoNone
), _cpuSubType(0)
345 const bool log
= false;
347 // create llvm module
348 #if LTO_API_VERSION >= 11
349 if ( sSupportsLocalContext
|| !sHasTriedLocalContext
) {
350 _module
= ::lto_module_create_in_local_context(content
, contentLength
, pth
);
352 if ( !sHasTriedLocalContext
) {
353 sHasTriedLocalContext
= true;
354 sSupportsLocalContext
= (_module
!= NULL
);
356 if ( (_module
== NULL
) && !sSupportsLocalContext
)
358 #if LTO_API_VERSION >= 9
359 _module
= ::lto_module_create_from_memory_with_path(content
, contentLength
, pth
);
360 if ( _module
== NULL
&& !sSupportsLocalContext
)
362 _module
= ::lto_module_create_from_memory(content
, contentLength
);
363 if ( _module
== NULL
)
364 throwf("could not parse object file %s: '%s', using libLTO version '%s'", pth
, ::lto_get_error_message(), ::lto_get_version());
366 if ( log
) fprintf(stderr
, "bitcode file: %s\n", pth
);
368 // create atom for each global symbol in module
369 uint32_t count
= ::lto_module_get_num_symbols(_module
);
370 _atomArray
= (Atom
*)malloc(sizeof(Atom
)*count
);
371 for (uint32_t i
=0; i
< count
; ++i
) {
372 const char* name
= ::lto_module_get_symbol_name(_module
, i
);
373 lto_symbol_attributes attr
= lto_module_get_symbol_attribute(_module
, i
);
375 // <rdar://problem/6378110> LTO doesn't like dtrace symbols
376 // ignore dtrace static probes for now
377 // later when codegen is done and a mach-o file is produces the probes will be processed
378 if ( (strncmp(name
, "___dtrace_probe$", 16) == 0) || (strncmp(name
, "___dtrace_isenabled$", 20) == 0) )
381 ld::Atom::Definition def
;
382 ld::Atom::Combine combine
= ld::Atom::combineNever
;
383 switch ( attr
& LTO_SYMBOL_DEFINITION_MASK
) {
384 case LTO_SYMBOL_DEFINITION_REGULAR
:
385 def
= ld::Atom::definitionRegular
;
387 case LTO_SYMBOL_DEFINITION_TENTATIVE
:
388 def
= ld::Atom::definitionTentative
;
390 case LTO_SYMBOL_DEFINITION_WEAK
:
391 def
= ld::Atom::definitionRegular
;
392 combine
= ld::Atom::combineByName
;
394 case LTO_SYMBOL_DEFINITION_UNDEFINED
:
395 case LTO_SYMBOL_DEFINITION_WEAKUNDEF
:
396 def
= ld::Atom::definitionProxy
;
399 throwf("unknown definition kind for symbol %s in bitcode file %s", name
, pth
);
402 // make LLVM atoms for definitions and a reference for undefines
403 if ( def
!= ld::Atom::definitionProxy
) {
404 ld::Atom::Scope scope
;
405 bool autohide
= false;
406 switch ( attr
& LTO_SYMBOL_SCOPE_MASK
) {
407 case LTO_SYMBOL_SCOPE_INTERNAL
:
408 scope
= ld::Atom::scopeTranslationUnit
;
410 case LTO_SYMBOL_SCOPE_HIDDEN
:
411 scope
= ld::Atom::scopeLinkageUnit
;
413 case LTO_SYMBOL_SCOPE_DEFAULT
:
414 scope
= ld::Atom::scopeGlobal
;
416 #if LTO_API_VERSION >= 4
417 case LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN
:
418 scope
= ld::Atom::scopeGlobal
;
423 throwf("unknown scope for symbol %s in bitcode file %s", name
, pth
);
425 // only make atoms for non-internal symbols
426 if ( scope
== ld::Atom::scopeTranslationUnit
)
428 uint8_t alignment
= (attr
& LTO_SYMBOL_ALIGNMENT_MASK
);
429 // make Atom using placement new operator
430 new (&_atomArray
[_atomArrayCount
++]) Atom(*this, name
, scope
, def
, combine
, alignment
, autohide
);
431 if ( scope
!= ld::Atom::scopeTranslationUnit
)
432 _internalAtom
.addReference(name
);
433 if ( log
) fprintf(stderr
, "\t0x%08X %s\n", attr
, name
);
436 // add to list of external references
437 _internalAtom
.addReference(name
);
438 if ( log
) fprintf(stderr
, "\t%s (undefined)\n", name
);
442 #if LTO_API_VERSION >= 11
443 if ( sSupportsLocalContext
)
453 bool File::mergeIntoGenerator(lto_code_gen_t generator
, bool useSetModule
) {
454 #if LTO_API_VERSION >= 11
455 if ( sSupportsLocalContext
) {
456 assert(!_module
&& "Expected module to be disposed");
457 _module
= ::lto_module_create_in_codegen_context(_content
, _contentLength
,
459 if ( _module
== NULL
)
460 throwf("could not reparse object file %s: '%s', using libLTO version '%s'",
461 _path
, ::lto_get_error_message(), ::lto_get_version());
464 assert(_module
&& "Expected module to stick around");
465 #if LTO_API_VERSION >= 13
467 // lto_codegen_set_module will transfer ownership of the module to LTO code generator,
468 // so we don't need to release the module here.
469 ::lto_codegen_set_module(generator
, _module
);
473 if ( ::lto_codegen_add_module(generator
, _module
) )
476 // <rdar://problem/15471128> linker should release module as soon as possible
483 if ( _module
!= NULL
)
484 ::lto_module_dispose(_module
);
488 bool File::forEachAtom(ld::File::AtomHandler
& handler
) const
490 handler
.doAtom(_internalAtom
);
491 for(uint32_t i
=0; i
< _atomArrayCount
; ++i
) {
492 handler
.doAtom(_atomArray
[i
]);
497 InternalAtom::InternalAtom(File
& f
)
498 : ld::Atom(f
._section
, ld::Atom::definitionRegular
, ld::Atom::combineNever
, ld::Atom::scopeTranslationUnit
,
499 ld::Atom::typeLTOtemporary
, ld::Atom::symbolTableNotIn
, true, false, false, ld::Atom::Alignment(0)),
504 Atom::Atom(File
& f
, const char* nm
, ld::Atom::Scope s
, ld::Atom::Definition d
, ld::Atom::Combine c
,
505 ld::Atom::Alignment a
, bool ah
)
506 : ld::Atom(f
._section
, d
, c
, s
, ld::Atom::typeLTOtemporary
,
507 ld::Atom::symbolTableIn
, false, false, false, a
),
508 _file(f
), _name(strdup(nm
)), _compiledAtom(NULL
)
514 void Atom::setCompiledAtom(const ld::Atom
& atom
)
516 // set delegate so virtual methods go to it
517 _compiledAtom
= &atom
;
519 //fprintf(stderr, "setting lto atom %p to delegate to mach-o atom %p (%s)\n", this, &atom, atom.name());
521 // update fields in ld::Atom to match newly constructed mach-o atom
522 (const_cast<Atom
*>(this))->setAttributesFromAtom(atom
);
527 // <rdar://problem/12379604> The order that files are merged must match command line order
528 struct CommandLineOrderFileSorter
530 bool operator()(File
* left
, File
* right
)
532 return ( left
->ordinal() < right
->ordinal() );
537 #if LTO_API_VERSION >= 7
538 void Parser::ltoDiagnosticHandler(lto_codegen_diagnostic_severity_t severity
, const char* message
, void*)
540 switch ( severity
) {
541 #if LTO_API_VERSION >= 10
543 fprintf(stderr
, "ld: LTO remark: %s\n", message
);
548 warning("%s", message
);
551 throwf("%s", message
);
556 bool Parser::optimize( const std::vector
<const ld::Atom
*>& allAtoms
,
558 const OptimizeOptions
& options
,
559 ld::File::AtomHandler
& handler
,
560 std::vector
<const ld::Atom
*>& newAtoms
,
561 std::vector
<const char*>& additionalUndefines
)
563 const bool logMustPreserve
= false;
564 const bool logExtraOptions
= false;
565 const bool logBitcodeFiles
= false;
566 const bool logAtomsBeforeSync
= false;
568 // exit quickly if nothing to do
569 if ( _s_files
.size() == 0 )
572 // print out LTO version string if -v was used
573 if ( options
.verbose
)
574 fprintf(stderr
, "%s\n", ::lto_get_version());
576 // create optimizer and add each Reader
577 lto_code_gen_t generator
= NULL
;
578 #if LTO_API_VERSION >= 11
579 if ( File::sSupportsLocalContext
)
580 generator
= ::lto_codegen_create_in_local_context();
583 generator
= ::lto_codegen_create();
584 #if LTO_API_VERSION >= 7
585 lto_codegen_set_diagnostic_handler(generator
, ltoDiagnosticHandler
, NULL
);
588 // <rdar://problem/12379604> The order that files are merged must match command line order
589 std::sort(_s_files
.begin(), _s_files
.end(), CommandLineOrderFileSorter());
590 ld::File::Ordinal lastOrdinal
;
592 // When flto_codegen_only is on and we have a single .bc file, use lto_codegen_set_module instead of
593 // lto_codegen_add_module, to make sure the the destination module will be the same as the input .bc file.
594 bool useSetModule
= false;
595 #if LTO_API_VERSION >= 13
596 useSetModule
= (_s_files
.size() == 1) && options
.ltoCodegenOnly
&& (::lto_api_version() >= 13);
598 for (std::vector
<File
*>::iterator it
=_s_files
.begin(); it
!= _s_files
.end(); ++it
) {
600 assert(f
->ordinal() > lastOrdinal
);
601 if ( logBitcodeFiles
&& !useSetModule
) fprintf(stderr
, "lto_codegen_add_module(%s)\n", f
->path());
602 if ( logBitcodeFiles
&& useSetModule
) fprintf(stderr
, "lto_codegen_set_module(%s)\n", f
->path());
603 if ( f
->mergeIntoGenerator(generator
, useSetModule
) )
604 throwf("lto: could not merge in %s because '%s', using libLTO version '%s'", f
->path(), ::lto_get_error_message(), ::lto_get_version());
605 lastOrdinal
= f
->ordinal();
608 // add any -mllvm command line options
609 for (std::vector
<const char*>::const_iterator it
=options
.llvmOptions
->begin(); it
!= options
.llvmOptions
->end(); ++it
) {
610 if ( logExtraOptions
) fprintf(stderr
, "passing option to llvm: %s\n", *it
);
611 ::lto_codegen_debug_options(generator
, *it
);
614 // <rdar://problem/13687397> Need a way for LTO to get cpu variants (until that info is in bitcode)
615 if ( options
.mcpu
!= NULL
)
616 ::lto_codegen_set_cpu(generator
, options
.mcpu
);
618 // The atom graph uses directed edges (references). Collect all references where
619 // originating atom is not part of any LTO Reader. This allows optimizer to optimize an
620 // external (i.e. not originated from same .o file) reference if all originating atoms are also
621 // defined in llvm bitcode file.
622 CStringSet nonLLVMRefs
;
623 CStringToAtom llvmAtoms
;
624 bool hasNonllvmAtoms
= false;
625 for (std::vector
<const ld::Atom
*>::const_iterator it
= allAtoms
.begin(); it
!= allAtoms
.end(); ++it
) {
626 const ld::Atom
* atom
= *it
;
627 // only look at references that come from an atom that is not an llvm atom
628 if ( atom
->contentType() != ld::Atom::typeLTOtemporary
) {
629 if ( (atom
->section().type() != ld::Section::typeMachHeader
) && (atom
->definition() != ld::Atom::definitionProxy
) ) {
630 hasNonllvmAtoms
= true;
632 const ld::Atom
* target
;
633 for (ld::Fixup::iterator fit
=atom
->fixupsBegin(); fit
!= atom
->fixupsEnd(); ++fit
) {
634 switch ( fit
->binding
) {
635 case ld::Fixup::bindingDirectlyBound
:
636 // that reference an llvm atom
637 if ( fit
->u
.target
->contentType() == ld::Atom::typeLTOtemporary
)
638 nonLLVMRefs
.insert(fit
->u
.target
->name());
640 case ld::Fixup::bindingsIndirectlyBound
:
641 target
= state
.indirectBindingTable
[fit
->u
.bindingIndex
];
642 if ( (target
!= NULL
) && (target
->contentType() == ld::Atom::typeLTOtemporary
) )
643 nonLLVMRefs
.insert(target
->name());
649 else if ( atom
->scope() >= ld::Atom::scopeLinkageUnit
) {
650 llvmAtoms
[atom
->name()] = (Atom
*)atom
;
653 // if entry point is in a llvm bitcode file, it must be preserved by LTO
654 if ( state
.entryPoint
!= NULL
) {
655 if ( state
.entryPoint
->contentType() == ld::Atom::typeLTOtemporary
)
656 nonLLVMRefs
.insert(state
.entryPoint
->name());
659 // deadAtoms are the atoms that the linker coalesced. For instance weak or tentative definitions
660 // overriden by another atom. If any of these deadAtoms are llvm atoms and they were replaced
661 // with a mach-o atom, we need to tell the lto engine to preserve (not optimize away) its dead
662 // atom so that the linker can replace it with the mach-o one later.
663 CStringToAtom deadllvmAtoms
;
664 for (std::vector
<const ld::Atom
*>::const_iterator it
= allAtoms
.begin(); it
!= allAtoms
.end(); ++it
) {
665 const ld::Atom
* atom
= *it
;
666 if ( atom
->coalescedAway() && (atom
->contentType() == ld::Atom::typeLTOtemporary
) ) {
667 const char* name
= atom
->name();
668 if ( logMustPreserve
) fprintf(stderr
, "lto_codegen_add_must_preserve_symbol(%s) because linker coalesce away and replace with a mach-o atom\n", name
);
669 ::lto_codegen_add_must_preserve_symbol(generator
, name
);
670 deadllvmAtoms
[name
] = (Atom
*)atom
;
673 for (std::vector
<File
*>::iterator it
=_s_files
.begin(); it
!= _s_files
.end(); ++it
) {
675 for(uint32_t i
=0; i
< file
->_atomArrayCount
; ++i
) {
676 Atom
* llvmAtom
= &file
->_atomArray
[i
];
677 if ( llvmAtom
->coalescedAway() ) {
678 const char* name
= llvmAtom
->name();
679 if ( deadllvmAtoms
.find(name
) == deadllvmAtoms
.end() ) {
680 if ( logMustPreserve
)
681 fprintf(stderr
, "lto_codegen_add_must_preserve_symbol(%s) because linker coalesce away and replace with a mach-o atom\n", name
);
682 ::lto_codegen_add_must_preserve_symbol(generator
, name
);
683 deadllvmAtoms
[name
] = (Atom
*)llvmAtom
;
686 else if ( options
.linkerDeadStripping
&& !llvmAtom
->live() ) {
687 const char* name
= llvmAtom
->name();
688 deadllvmAtoms
[name
] = (Atom
*)llvmAtom
;
693 // tell code generator about symbols that must be preserved
694 for (CStringToAtom::iterator it
= llvmAtoms
.begin(); it
!= llvmAtoms
.end(); ++it
) {
695 const char* name
= it
->first
;
696 Atom
* atom
= it
->second
;
697 // Include llvm Symbol in export list if it meets one of following two conditions
698 // 1 - atom scope is global (and not linkage unit).
699 // 2 - included in nonLLVMRefs set.
700 // If a symbol is not listed in exportList then LTO is free to optimize it away.
701 if ( (atom
->scope() == ld::Atom::scopeGlobal
) && options
.preserveAllGlobals
) {
702 if ( logMustPreserve
) fprintf(stderr
, "lto_codegen_add_must_preserve_symbol(%s) because global symbol\n", name
);
703 ::lto_codegen_add_must_preserve_symbol(generator
, name
);
705 else if ( nonLLVMRefs
.find(name
) != nonLLVMRefs
.end() ) {
706 if ( logMustPreserve
) fprintf(stderr
, "lto_codegen_add_must_preserve_symbol(%s) because referenced by a mach-o atom\n", name
);
707 ::lto_codegen_add_must_preserve_symbol(generator
, name
);
709 else if ( options
.relocatable
&& hasNonllvmAtoms
) {
710 // <rdar://problem/14334895> ld -r mode but merging in some mach-o files, so need to keep libLTO from optimizing away anything
711 if ( logMustPreserve
) fprintf(stderr
, "lto_codegen_add_must_preserve_symbol(%s) because -r mode disable LTO dead stripping\n", name
);
712 ::lto_codegen_add_must_preserve_symbol(generator
, name
);
716 // <rdar://problem/16165191> tell code generator to preserve initial undefines
717 for( std::vector
<const char*>::const_iterator it
=options
.initialUndefines
->begin(); it
!= options
.initialUndefines
->end(); ++it
) {
718 if ( logMustPreserve
) fprintf(stderr
, "lto_codegen_add_must_preserve_symbol(%s) because it is an initial undefine\n", *it
);
719 ::lto_codegen_add_must_preserve_symbol(generator
, *it
);
722 // special case running ld -r on all bitcode files to produce another bitcode file (instead of mach-o)
723 if ( options
.relocatable
&& !hasNonllvmAtoms
) {
724 #if LTO_API_VERSION >= 15
725 ::lto_codegen_set_should_embed_uselists(generator
, false);
727 if ( ! ::lto_codegen_write_merged_modules(generator
, options
.outputFilePath
) ) {
728 // HACK, no good way to tell linker we are all done, so just quit
731 warning("could not produce merged bitcode file");
734 // set code-gen model
735 lto_codegen_model model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC
;
736 if ( options
.mainExecutable
) {
737 if ( options
.staticExecutable
) {
738 // x86_64 "static" or any "-static -pie" is really dynamic code model
739 if ( (options
.arch
== CPU_TYPE_X86_64
) || options
.pie
)
740 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC
;
742 model
= LTO_CODEGEN_PIC_MODEL_STATIC
;
746 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC
;
748 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC
;
752 if ( options
.allowTextRelocs
)
753 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC
;
755 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC
;
757 if ( ::lto_codegen_set_pic_model(generator
, model
) )
758 throwf("could not create set codegen model: %s", lto_get_error_message());
760 // if requested, save off merged bitcode file
761 if ( options
.saveTemps
) {
762 char tempBitcodePath
[MAXPATHLEN
];
763 strcpy(tempBitcodePath
, options
.outputFilePath
);
764 strcat(tempBitcodePath
, ".lto.bc");
765 #if LTO_API_VERSION >= 15
766 ::lto_codegen_set_should_embed_uselists(generator
, true);
768 ::lto_codegen_write_merged_modules(generator
, tempBitcodePath
);
771 #if LTO_API_VERSION >= 3
772 // find assembler next to linker
774 uint32_t bufSize
= PATH_MAX
;
775 if ( _NSGetExecutablePath(path
, &bufSize
) != -1 ) {
776 char* lastSlash
= strrchr(path
, '/');
777 if ( lastSlash
!= NULL
) {
778 strcpy(lastSlash
+1, "as");
779 struct stat statInfo
;
780 if ( stat(path
, &statInfo
) == 0 )
781 ::lto_codegen_set_assembler_path(generator
, path
);
786 // When lto API version is greater than or equal to 12, we use lto_codegen_optimize and lto_codegen_compile_optimized
787 // instead of lto_codegen_compile, and we save the merged bitcode file in between.
788 bool useSplitAPI
= false;
789 #if LTO_API_VERSION >= 12
790 if ( ::lto_api_version() >= 12)
794 size_t machOFileLen
= 0;
795 const uint8_t* machOFile
= NULL
;
797 #if LTO_API_VERSION >= 12
798 #if LTO_API_VERSION >= 14
799 if ( ::lto_api_version() >= 14 && options
.ltoCodegenOnly
)
800 lto_codegen_set_should_internalize(generator
, false);
803 if ( !options
.ltoCodegenOnly
&& ::lto_codegen_optimize(generator
) )
804 throwf("could not do LTO optimization: '%s', using libLTO version '%s'", ::lto_get_error_message(), ::lto_get_version());
806 if ( options
.saveTemps
|| options
.bitcodeBundle
) {
807 // save off merged bitcode file
808 char tempOptBitcodePath
[MAXPATHLEN
];
809 strcpy(tempOptBitcodePath
, options
.outputFilePath
);
810 strcat(tempOptBitcodePath
, ".lto.opt.bc");
811 #if LTO_API_VERSION >= 15
812 ::lto_codegen_set_should_embed_uselists(generator
, true);
814 ::lto_codegen_write_merged_modules(generator
, tempOptBitcodePath
);
815 if ( options
.bitcodeBundle
)
816 state
.ltoBitcodePath
= tempOptBitcodePath
;
819 // run code generator
820 machOFile
= (uint8_t*)::lto_codegen_compile_optimized(generator
, &machOFileLen
);
822 if ( machOFile
== NULL
)
823 throwf("could not do LTO codegen: '%s', using libLTO version '%s'", ::lto_get_error_message(), ::lto_get_version());
826 // run optimizer and code generator
827 machOFile
= (uint8_t*)::lto_codegen_compile(generator
, &machOFileLen
);
828 if ( machOFile
== NULL
)
829 throwf("could not do LTO codegen: '%s', using libLTO version '%s'", ::lto_get_error_message(), ::lto_get_version());
830 if ( options
.saveTemps
) {
831 // save off merged bitcode file
832 char tempOptBitcodePath
[MAXPATHLEN
];
833 strcpy(tempOptBitcodePath
, options
.outputFilePath
);
834 strcat(tempOptBitcodePath
, ".lto.opt.bc");
835 #if LTO_API_VERSION >= 15
836 ::lto_codegen_set_should_embed_uselists(generator
, true);
838 ::lto_codegen_write_merged_modules(generator
, tempOptBitcodePath
);
842 // if requested, save off temp mach-o file
843 if ( options
.saveTemps
) {
844 char tempMachoPath
[MAXPATHLEN
];
845 strcpy(tempMachoPath
, options
.outputFilePath
);
846 strcat(tempMachoPath
, ".lto.o");
847 int fd
= ::open(tempMachoPath
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
849 ::write(fd
, machOFile
, machOFileLen
);
854 // if needed, save temp mach-o file to specific location
855 if ( options
.tmpObjectFilePath
!= NULL
) {
856 int fd
= ::open(options
.tmpObjectFilePath
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
858 ::write(fd
, machOFile
, machOFileLen
);
862 warning("could not write LTO temp file '%s', errno=%d", options
.tmpObjectFilePath
, errno
);
866 // parse generated mach-o file into a MachOReader
867 ld::relocatable::File
* machoFile
= parseMachOFile(machOFile
, machOFileLen
, options
);
869 // sync generated mach-o atoms with existing atoms ld knows about
870 if ( logAtomsBeforeSync
) {
871 fprintf(stderr
, "llvmAtoms:\n");
872 for (CStringToAtom::iterator it
= llvmAtoms
.begin(); it
!= llvmAtoms
.end(); ++it
) {
873 const char* name
= it
->first
;
874 Atom
* atom
= it
->second
;
875 fprintf(stderr
, "\t%p\t%s\n", atom
, name
);
877 fprintf(stderr
, "deadllvmAtoms:\n");
878 for (CStringToAtom::iterator it
= deadllvmAtoms
.begin(); it
!= deadllvmAtoms
.end(); ++it
) {
879 const char* name
= it
->first
;
880 Atom
* atom
= it
->second
;
881 fprintf(stderr
, "\t%p\t%s\n", atom
, name
);
884 AtomSyncer
syncer(additionalUndefines
, newAtoms
, llvmAtoms
, deadllvmAtoms
, options
);
885 machoFile
->forEachAtom(syncer
);
887 // Remove InternalAtoms from ld
888 for (std::vector
<File
*>::iterator it
=_s_files
.begin(); it
!= _s_files
.end(); ++it
) {
889 (*it
)->internalAtom().setCoalescedAway();
891 // Remove Atoms from ld if code generator optimized them away
892 for (CStringToAtom::iterator li
= llvmAtoms
.begin(), le
= llvmAtoms
.end(); li
!= le
; ++li
) {
893 // check if setRealAtom() called on this Atom
894 if ( li
->second
->compiledAtom() == NULL
) {
895 //fprintf(stderr, "llvm optimized away %p %s\n", li->second, li->second->name());
896 li
->second
->setCoalescedAway();
900 // notify about file level attributes
901 handler
.doFile(*machoFile
);
903 // if final mach-o file has debug info, update original bitcode files to match
904 for (std::vector
<File
*>::iterator it
=_s_files
.begin(); it
!= _s_files
.end(); ++it
) {
905 (*it
)->setDebugInfo(machoFile
->debugInfo(), machoFile
->path(),
906 machoFile
->modificationTime(), machoFile
->cpuSubType());
913 void Parser::AtomSyncer::doAtom(const ld::Atom
& machoAtom
)
915 static const bool log
= false;
916 static const ld::Atom
* lastProxiedAtom
= NULL
;
917 static const ld::File
* lastProxiedFile
= NULL
;
918 // update proxy atoms to point to real atoms and find new atoms
919 const char* name
= machoAtom
.name();
920 CStringToAtom::iterator pos
= _llvmAtoms
.find(name
);
921 if ( pos
!= _llvmAtoms
.end() ) {
922 // turn Atom into a proxy for this mach-o atom
923 pos
->second
->setCompiledAtom(machoAtom
);
924 lastProxiedAtom
= &machoAtom
;
925 lastProxiedFile
= pos
->second
->file();
926 if (log
) fprintf(stderr
, "AtomSyncer, mach-o atom %p synced to lto atom %p (name=%s)\n", &machoAtom
, pos
->second
, machoAtom
.name());
929 // an atom of this name was not in the allAtoms list the linker gave us
930 if ( _deadllvmAtoms
.find(name
) != _deadllvmAtoms
.end() ) {
931 // this corresponding to an atom that the linker coalesced away or marked not-live
932 if ( _options
.linkerDeadStripping
) {
933 // llvm seems to want this atom and -dead_strip is enabled, so it will be deleted if not needed, so add back
934 Atom
* llvmAtom
= _deadllvmAtoms
[name
];
935 llvmAtom
->setCompiledAtom(machoAtom
);
936 _newAtoms
.push_back(&machoAtom
);
937 if (log
) fprintf(stderr
, "AtomSyncer, mach-o atom %p matches dead lto atom %p but adding back (name=%s)\n", &machoAtom
, llvmAtom
, machoAtom
.name());
940 // Don't pass it back as a new atom
941 if (log
) fprintf(stderr
, "AtomSyncer, mach-o atom %p matches dead lto atom %p (name=%s)\n", &machoAtom
, _deadllvmAtoms
[name
], machoAtom
.name());
946 // this is something new that lto conjured up, tell ld its new
947 _newAtoms
.push_back(&machoAtom
);
948 // <rdar://problem/15469363> if new static atom in same section as previous non-static atom, assign to same file as previous
949 if ( (lastProxiedAtom
!= NULL
) && (lastProxiedAtom
->section() == machoAtom
.section()) ) {
950 ld::Atom
* ma
= const_cast<ld::Atom
*>(&machoAtom
);
951 ma
->setFile(lastProxiedFile
);
953 if (log
) fprintf(stderr
, "AtomSyncer, mach-o atom %p is totally new (name=%s)\n", &machoAtom
, machoAtom
.name());
957 // adjust fixups to go through proxy atoms
958 if (log
) fprintf(stderr
, " adjusting fixups in atom: %s\n", machoAtom
.name());
959 for (ld::Fixup::iterator fit
=machoAtom
.fixupsBegin(); fit
!= machoAtom
.fixupsEnd(); ++fit
) {
960 switch ( fit
->binding
) {
961 case ld::Fixup::bindingNone
:
963 case ld::Fixup::bindingByNameUnbound
:
964 // don't know if this target has been seen by linker before or if it is new
965 // be conservative and tell linker it is new
966 _additionalUndefines
.push_back(fit
->u
.name
);
967 if (log
) fprintf(stderr
, " adding by-name symbol %s\n", fit
->u
.name
);
969 case ld::Fixup::bindingDirectlyBound
:
970 // If mach-o atom is referencing another mach-o atom then
971 // reference is not going through Atom proxy. Fix it here to ensure that all
972 // llvm symbol references always go through Atom proxy.
974 const char* targetName
= fit
->u
.target
->name();
975 CStringToAtom::iterator post
= _llvmAtoms
.find(targetName
);
976 if ( post
!= _llvmAtoms
.end() ) {
977 const ld::Atom
* t
= post
->second
;
978 if (log
) fprintf(stderr
, " updating direct reference to %p to be ref to %p: %s\n", fit
->u
.target
, t
, targetName
);
982 // <rdar://problem/12859831> Don't unbind follow-on reference into by-name reference
983 if ( (_deadllvmAtoms
.find(targetName
) != _deadllvmAtoms
.end()) && (fit
->kind
!= ld::Fixup::kindNoneFollowOn
) && (fit
->u
.target
->scope() != ld::Atom::scopeTranslationUnit
) ) {
984 // target was coalesed away and replace by mach-o atom from a non llvm .o file
985 fit
->binding
= ld::Fixup::bindingByNameUnbound
;
986 fit
->u
.name
= targetName
;
990 //fprintf(stderr, " direct ref to: %s (scope=%d)\n", fit->u.target->name(), fit->u.target->scope());
992 case ld::Fixup::bindingByContentBound
:
993 //fprintf(stderr, " direct by content to: %s\n", fit->u.target->name());
995 case ld::Fixup::bindingsIndirectlyBound
:
996 assert(0 && "indirect binding found in initial mach-o file?");
997 //fprintf(stderr, " indirect by content to: %u\n", fit->u.bindingIndex);
1005 static pthread_mutex_t lto_lock
;
1007 Mutex() { pthread_mutex_lock(<o_lock
); }
1008 ~Mutex() { pthread_mutex_unlock(<o_lock
); }
1010 pthread_mutex_t
Mutex::lto_lock
= PTHREAD_MUTEX_INITIALIZER
;
1011 bool File::sSupportsLocalContext
= false;
1012 bool File::sHasTriedLocalContext
= false;
1015 // Used by archive reader to see if member is an llvm bitcode file
1017 bool isObjectFile(const uint8_t* fileContent
, uint64_t fileLength
, cpu_type_t architecture
, cpu_subtype_t subarch
)
1020 return Parser::validFile(fileContent
, fileLength
, architecture
, subarch
);
1024 static ld::relocatable::File
*parseImpl(
1025 const uint8_t *fileContent
, uint64_t fileLength
, const char *path
,
1026 time_t modTime
, ld::File::Ordinal ordinal
, cpu_type_t architecture
,
1027 cpu_subtype_t subarch
, bool logAllFiles
,
1028 bool verboseOptimizationHints
)
1030 if ( Parser::validFile(fileContent
, fileLength
, architecture
, subarch
) )
1031 return Parser::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, architecture
, subarch
, logAllFiles
, verboseOptimizationHints
);
1037 // main function used by linker to instantiate ld::Files
1039 ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
1040 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
1041 cpu_type_t architecture
, cpu_subtype_t subarch
, bool logAllFiles
,
1042 bool verboseOptimizationHints
)
1044 // Note: Once lto_module_create_in_local_context() and friends are thread safe
1045 // this lock can be removed.
1047 return parseImpl(fileContent
, fileLength
, path
, modTime
, ordinal
,
1048 architecture
, subarch
, logAllFiles
,
1049 verboseOptimizationHints
);
1053 // used by "ld -v" to report version of libLTO.dylib being used
1055 const char* version()
1058 return ::lto_get_version();
1063 // used by ld for error reporting
1065 bool libLTOisLoaded()
1068 return (::lto_get_version() != NULL
);
1072 // used by ld for error reporting
1074 const char* archName(const uint8_t* fileContent
, uint64_t fileLength
)
1077 return Parser::fileKind(fileContent
, fileLength
);
1081 // used by ld for doing link time optimization
1083 bool optimize( const std::vector
<const ld::Atom
*>& allAtoms
,
1084 ld::Internal
& state
,
1085 const OptimizeOptions
& options
,
1086 ld::File::AtomHandler
& handler
,
1087 std::vector
<const ld::Atom
*>& newAtoms
,
1088 std::vector
<const char*>& additionalUndefines
)
1091 return Parser::optimize(allAtoms
, state
, options
, handler
, newAtoms
, additionalUndefines
);