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>
36 #include <unordered_set>
37 #include <unordered_map>
39 #include "MachOFileAbstraction.hpp"
40 #include "Architectures.hpp"
42 #include "macho_relocatable_file.h"
45 // #defines are a work around for <rdar://problem/8760268>
46 #define __STDC_LIMIT_MACROS 1
47 #define __STDC_CONSTANT_MACROS 1
48 #include "llvm-c/lto.h"
54 // ld64 only tracks non-internal symbols from an llvm bitcode file.
55 // We model this by having an InternalAtom which represent all internal functions and data.
56 // All non-interal symbols from a bitcode file are represented by an Atom
57 // and each Atom has a reference to the InternalAtom. The InternalAtom
58 // also has references to each symbol external to the bitcode file.
60 class InternalAtom
: public ld::Atom
63 InternalAtom(class File
& f
);
64 // overrides of ld::Atom
65 virtual ld::File
* file() const { return &_file
; }
66 virtual const char* name() const { return "import-atom"; }
67 virtual uint64_t size() const { return 0; }
68 virtual uint64_t objectAddress() const { return 0; }
69 virtual void copyRawContent(uint8_t buffer
[]) const { }
70 virtual void setScope(Scope
) { }
71 virtual ld::Fixup::iterator
fixupsBegin() const { return &_undefs
[0]; }
72 virtual ld::Fixup::iterator
fixupsEnd() const { return &_undefs
[_undefs
.size()]; }
74 // for adding references to symbols outside bitcode file
75 void addReference(const char* nm
)
76 { _undefs
.push_back(ld::Fixup(0, ld::Fixup::k1of1
,
77 ld::Fixup::kindNone
, false, nm
)); }
81 mutable std::vector
<ld::Fixup
> _undefs
;
88 class File
: public ld::relocatable::File
91 File(const char* path
, time_t mTime
, ld::File::Ordinal ordinal
,
92 const uint8_t* content
, uint32_t contentLength
, cpu_type_t arch
);
95 // overrides of ld::File
96 virtual bool forEachAtom(ld::File::AtomHandler
&) const;
97 virtual bool justInTimeforEachAtom(const char* name
, ld::File::AtomHandler
&) const
99 virtual uint32_t cpuSubType() const { return _cpuSubType
; }
101 // overrides of ld::relocatable::File
102 virtual DebugInfoKind
debugInfo() const { return _debugInfo
; }
103 virtual const char* debugInfoPath() const { return _debugInfoPath
; }
104 virtual time_t debugInfoModificationTime() const
105 { return _debugInfoModTime
; }
106 virtual const std::vector
<ld::relocatable::File::Stab
>* stabs() const { return NULL
; }
107 virtual bool canScatterAtoms() const { return true; }
108 virtual LinkerOptionsList
* linkerOptions() const { return NULL
; }
111 lto_module_t
module() { return _module
; }
112 class InternalAtom
& internalAtom() { return _internalAtom
; }
113 void setDebugInfo(ld::relocatable::File::DebugInfoKind k
,
114 const char* pth
, time_t modTime
, uint32_t subtype
)
116 _debugInfoPath
= pth
;
117 _debugInfoModTime
= modTime
;
118 _cpuSubType
= subtype
;}
122 friend class InternalAtom
;
125 cpu_type_t _architecture
;
126 class InternalAtom _internalAtom
;
127 class Atom
* _atomArray
;
128 uint32_t _atomArrayCount
;
129 lto_module_t _module
;
130 const char* _debugInfoPath
;
131 time_t _debugInfoModTime
;
132 ld::Section _section
;
133 ld::Fixup _fixupToInternal
;
134 ld::relocatable::File::DebugInfoKind _debugInfo
;
135 uint32_t _cpuSubType
;
139 // Atom acts as a proxy Atom for the symbols that are exported by LLVM bitcode file. Initially,
140 // Reader creates Atoms to allow linker proceed with usual symbol resolution phase. After
141 // optimization is performed, real Atoms are created for these symobls. However these real Atoms
142 // are not inserted into global symbol table. Atom holds real Atom and forwards appropriate
143 // methods to real atom.
145 class Atom
: public ld::Atom
148 Atom(File
& f
, const char* name
, ld::Atom::Scope s
,
149 ld::Atom::Definition d
, ld::Atom::Combine c
, ld::Atom::Alignment a
, bool ah
);
151 // overrides of ld::Atom
152 virtual ld::File
* file() const { return &_file
; }
153 virtual const char* translationUnitSource() const
154 { return (_compiledAtom
? _compiledAtom
->translationUnitSource() : NULL
); }
155 virtual const char* name() const { return _name
; }
156 virtual uint64_t size() const { return (_compiledAtom
? _compiledAtom
->size() : 0); }
157 virtual uint64_t objectAddress() const { return (_compiledAtom
? _compiledAtom
->objectAddress() : 0); }
158 virtual void copyRawContent(uint8_t buffer
[]) const
159 { if (_compiledAtom
) _compiledAtom
->copyRawContent(buffer
); }
160 virtual const uint8_t* rawContentPointer() const
161 { return (_compiledAtom
? _compiledAtom
->rawContentPointer() : NULL
); }
162 virtual unsigned long contentHash(const class ld::IndirectBindingTable
& ibt
) const
163 { return (_compiledAtom
? _compiledAtom
->contentHash(ibt
) : 0); }
164 virtual bool canCoalesceWith(const ld::Atom
& rhs
, const class ld::IndirectBindingTable
& ibt
) const
165 { return (_compiledAtom
? _compiledAtom
->canCoalesceWith(rhs
,ibt
) : false); }
166 virtual ld::Fixup::iterator
fixupsBegin() const
167 { return (_compiledAtom
? _compiledAtom
->fixupsBegin() : (ld::Fixup
*)&_file
._fixupToInternal
); }
168 virtual ld::Fixup::iterator
fixupsEnd() const
169 { return (_compiledAtom
? _compiledAtom
->fixupsEnd() : &((ld::Fixup
*)&_file
._fixupToInternal
)[1]); }
170 virtual ld::Atom::UnwindInfo::iterator
beginUnwind() const
171 { return (_compiledAtom
? _compiledAtom
->beginUnwind() : NULL
); }
172 virtual ld::Atom::UnwindInfo::iterator
endUnwind() const
173 { return (_compiledAtom
? _compiledAtom
->endUnwind() : NULL
); }
174 virtual ld::Atom::LineInfo::iterator
beginLineInfo() const
175 { return (_compiledAtom
? _compiledAtom
->beginLineInfo() : NULL
); }
176 virtual ld::Atom::LineInfo::iterator
endLineInfo() const
177 { return (_compiledAtom
? _compiledAtom
->endLineInfo() : NULL
); }
179 const ld::Atom
* compiledAtom() { return _compiledAtom
; }
180 void setCompiledAtom(const ld::Atom
& atom
);
186 const ld::Atom
* _compiledAtom
;
198 static bool validFile(const uint8_t* fileContent
, uint64_t fileLength
, cpu_type_t architecture
, cpu_subtype_t subarch
);
199 static const char* fileKind(const uint8_t* fileContent
, uint64_t fileLength
);
200 static File
* parse(const uint8_t* fileContent
, uint64_t fileLength
, const char* path
,
201 time_t modTime
, ld::File::Ordinal ordinal
, cpu_type_t architecture
, cpu_subtype_t subarch
,
202 bool logAllFiles
, bool verboseOptimizationHints
);
203 static bool libLTOisLoaded() { return (::lto_get_version() != NULL
); }
204 static bool optimize( const std::vector
<const ld::Atom
*>& allAtoms
,
206 const OptimizeOptions
& options
,
207 ld::File::AtomHandler
& handler
,
208 std::vector
<const ld::Atom
*>& newAtoms
,
209 std::vector
<const char*>& additionalUndefines
);
211 static const char* ltoVersion() { return ::lto_get_version(); }
214 static const char* tripletPrefixForArch(cpu_type_t arch
);
215 static ld::relocatable::File
* parseMachOFile(const uint8_t* p
, size_t len
, const OptimizeOptions
& options
);
216 #if LTO_API_VERSION >= 7
217 static void ltoDiagnosticHandler(lto_codegen_diagnostic_severity_t
, const char*, void*);
220 typedef std::unordered_set
<const char*, ld::CStringHash
, ld::CStringEquals
> CStringSet
;
221 typedef std::unordered_map
<const char*, Atom
*, ld::CStringHash
, ld::CStringEquals
> CStringToAtom
;
223 class AtomSyncer
: public ld::File::AtomHandler
{
225 AtomSyncer(std::vector
<const char*>& a
, std::vector
<const ld::Atom
*>&na
,
226 CStringToAtom la
, CStringToAtom dla
, const OptimizeOptions
& options
) :
227 _options(options
), _additionalUndefines(a
), _newAtoms(na
), _llvmAtoms(la
), _deadllvmAtoms(dla
) { }
228 virtual void doAtom(const class ld::Atom
&);
229 virtual void doFile(const class ld::File
&) { }
231 const OptimizeOptions
& _options
;
232 std::vector
<const char*>& _additionalUndefines
;
233 std::vector
<const ld::Atom
*>& _newAtoms
;
234 CStringToAtom _llvmAtoms
;
235 CStringToAtom _deadllvmAtoms
;
238 static std::vector
<File
*> _s_files
;
241 std::vector
<File
*> Parser::_s_files
;
244 bool Parser::validFile(const uint8_t* fileContent
, uint64_t fileLength
, cpu_type_t architecture
, cpu_subtype_t subarch
)
246 for (const ArchInfo
* t
=archInfoArray
; t
->archName
!= NULL
; ++t
) {
247 if ( (architecture
== t
->cpuType
) && (!(t
->isSubType
) || (subarch
== t
->cpuSubType
)) ) {
248 bool result
= ::lto_module_is_object_file_in_memory_for_target(fileContent
, fileLength
, t
->llvmTriplePrefix
);
250 // <rdar://problem/8434487> LTO only supports thumbv7 not armv7
251 if ( t
->llvmTriplePrefixAlt
[0] != '\0' ) {
252 result
= ::lto_module_is_object_file_in_memory_for_target(fileContent
, fileLength
, t
->llvmTriplePrefixAlt
);
261 const char* Parser::fileKind(const uint8_t* p
, uint64_t fileLength
)
263 if ( (p
[0] == 0xDE) && (p
[1] == 0xC0) && (p
[2] == 0x17) && (p
[3] == 0x0B) ) {
264 cpu_type_t arch
= LittleEndian::get32(*((uint32_t*)(&p
[16])));
265 for (const ArchInfo
* t
=archInfoArray
; t
->archName
!= NULL
; ++t
) {
266 if ( arch
== t
->cpuType
) {
267 if ( t
->isSubType
) {
268 if ( ::lto_module_is_object_file_in_memory_for_target(p
, fileLength
, t
->llvmTriplePrefix
) )
276 return "unknown bitcode architecture";
281 File
* Parser::parse(const uint8_t* fileContent
, uint64_t fileLength
, const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
282 cpu_type_t architecture
, cpu_subtype_t subarch
, bool logAllFiles
, bool verboseOptimizationHints
)
284 File
* f
= new File(path
, modTime
, ordinal
, fileContent
, fileLength
, architecture
);
285 _s_files
.push_back(f
);
287 printf("%s\n", path
);
292 ld::relocatable::File
* Parser::parseMachOFile(const uint8_t* p
, size_t len
, const OptimizeOptions
& options
)
294 mach_o::relocatable::ParserOptions objOpts
;
295 objOpts
.architecture
= options
.arch
;
296 objOpts
.objSubtypeMustMatch
= false;
297 objOpts
.logAllFiles
= false;
298 objOpts
.warnUnwindConversionProblems
= options
.needsUnwindInfoSection
;
299 objOpts
.keepDwarfUnwind
= options
.keepDwarfUnwind
;
300 objOpts
.forceDwarfConversion
= false;
301 objOpts
.neverConvertDwarf
= false;
302 objOpts
.verboseOptimizationHints
= options
.verboseOptimizationHints
;
305 // mach-o parsing is done in-memory, but need path for debug notes
306 const char* path
= "/tmp/lto.o";
308 if ( options
.tmpObjectFilePath
!= NULL
) {
309 path
= options
.tmpObjectFilePath
;
310 struct stat statBuffer
;
311 if ( stat(options
.tmpObjectFilePath
, &statBuffer
) == 0 )
312 modTime
= statBuffer
.st_mtime
;
315 ld::relocatable::File
* result
= mach_o::relocatable::parse(p
, len
, path
, modTime
, ld::File::Ordinal::LTOOrdinal(), objOpts
);
316 if ( result
!= NULL
)
318 throw "LLVM LTO, file is not of required architecture";
323 File::File(const char* pth
, time_t mTime
, ld::File::Ordinal ordinal
, const uint8_t* content
, uint32_t contentLength
, cpu_type_t arch
)
324 : ld::relocatable::File(pth
,mTime
,ordinal
), _architecture(arch
), _internalAtom(*this),
325 _atomArray(NULL
), _atomArrayCount(0), _module(NULL
), _debugInfoPath(pth
),
326 _section("__TEXT_", "__tmp_lto", ld::Section::typeTempLTO
),
327 _fixupToInternal(0, ld::Fixup::k1of1
, ld::Fixup::kindNone
, &_internalAtom
),
328 _debugInfo(ld::relocatable::File::kDebugInfoNone
), _cpuSubType(0)
330 const bool log
= false;
332 // create llvm module
333 _module
= ::lto_module_create_from_memory(content
, contentLength
);
334 if ( _module
== NULL
)
335 throwf("could not parse object file %s: '%s', using libLTO version '%s'", pth
, ::lto_get_error_message(), ::lto_get_version());
337 if ( log
) fprintf(stderr
, "bitcode file: %s\n", pth
);
339 // create atom for each global symbol in module
340 uint32_t count
= ::lto_module_get_num_symbols(_module
);
341 _atomArray
= (Atom
*)malloc(sizeof(Atom
)*count
);
342 for (uint32_t i
=0; i
< count
; ++i
) {
343 const char* name
= ::lto_module_get_symbol_name(_module
, i
);
344 lto_symbol_attributes attr
= lto_module_get_symbol_attribute(_module
, i
);
346 // <rdar://problem/6378110> LTO doesn't like dtrace symbols
347 // ignore dtrace static probes for now
348 // later when codegen is done and a mach-o file is produces the probes will be processed
349 if ( (strncmp(name
, "___dtrace_probe$", 16) == 0) || (strncmp(name
, "___dtrace_isenabled$", 20) == 0) )
352 ld::Atom::Definition def
;
353 ld::Atom::Combine combine
= ld::Atom::combineNever
;
354 switch ( attr
& LTO_SYMBOL_DEFINITION_MASK
) {
355 case LTO_SYMBOL_DEFINITION_REGULAR
:
356 def
= ld::Atom::definitionRegular
;
358 case LTO_SYMBOL_DEFINITION_TENTATIVE
:
359 def
= ld::Atom::definitionTentative
;
361 case LTO_SYMBOL_DEFINITION_WEAK
:
362 def
= ld::Atom::definitionRegular
;
363 combine
= ld::Atom::combineByName
;
365 case LTO_SYMBOL_DEFINITION_UNDEFINED
:
366 case LTO_SYMBOL_DEFINITION_WEAKUNDEF
:
367 def
= ld::Atom::definitionProxy
;
370 throwf("unknown definition kind for symbol %s in bitcode file %s", name
, pth
);
373 // make LLVM atoms for definitions and a reference for undefines
374 if ( def
!= ld::Atom::definitionProxy
) {
375 ld::Atom::Scope scope
;
376 bool autohide
= false;
377 switch ( attr
& LTO_SYMBOL_SCOPE_MASK
) {
378 case LTO_SYMBOL_SCOPE_INTERNAL
:
379 scope
= ld::Atom::scopeTranslationUnit
;
381 case LTO_SYMBOL_SCOPE_HIDDEN
:
382 scope
= ld::Atom::scopeLinkageUnit
;
384 case LTO_SYMBOL_SCOPE_DEFAULT
:
385 scope
= ld::Atom::scopeGlobal
;
387 #if LTO_API_VERSION >= 4
388 case LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN
:
389 scope
= ld::Atom::scopeGlobal
;
394 throwf("unknown scope for symbol %s in bitcode file %s", name
, pth
);
396 // only make atoms for non-internal symbols
397 if ( scope
== ld::Atom::scopeTranslationUnit
)
399 uint8_t alignment
= (attr
& LTO_SYMBOL_ALIGNMENT_MASK
);
400 // make Atom using placement new operator
401 new (&_atomArray
[_atomArrayCount
++]) Atom(*this, name
, scope
, def
, combine
, alignment
, autohide
);
402 if ( scope
!= ld::Atom::scopeTranslationUnit
)
403 _internalAtom
.addReference(name
);
404 if ( log
) fprintf(stderr
, "\t0x%08X %s\n", attr
, name
);
407 // add to list of external references
408 _internalAtom
.addReference(name
);
409 if ( log
) fprintf(stderr
, "\t%s (undefined)\n", name
);
416 if ( _module
!= NULL
)
417 ::lto_module_dispose(_module
);
420 bool File::forEachAtom(ld::File::AtomHandler
& handler
) const
422 handler
.doAtom(_internalAtom
);
423 for(uint32_t i
=0; i
< _atomArrayCount
; ++i
) {
424 handler
.doAtom(_atomArray
[i
]);
429 InternalAtom::InternalAtom(File
& f
)
430 : ld::Atom(f
._section
, ld::Atom::definitionRegular
, ld::Atom::combineNever
, ld::Atom::scopeTranslationUnit
,
431 ld::Atom::typeLTOtemporary
, ld::Atom::symbolTableNotIn
, true, false, false, ld::Atom::Alignment(0)),
436 Atom::Atom(File
& f
, const char* nm
, ld::Atom::Scope s
, ld::Atom::Definition d
, ld::Atom::Combine c
,
437 ld::Atom::Alignment a
, bool ah
)
438 : ld::Atom(f
._section
, d
, c
, s
, ld::Atom::typeLTOtemporary
,
439 ld::Atom::symbolTableIn
, false, false, false, a
),
440 _file(f
), _name(nm
), _compiledAtom(NULL
)
446 void Atom::setCompiledAtom(const ld::Atom
& atom
)
448 // set delegate so virtual methods go to it
449 _compiledAtom
= &atom
;
451 //fprintf(stderr, "setting lto atom %p to delegate to mach-o atom %p (%s)\n", this, &atom, atom.name());
453 // update fields in ld::Atom to match newly constructed mach-o atom
454 (const_cast<Atom
*>(this))->setAttributesFromAtom(atom
);
459 // <rdar://problem/12379604> The order that files are merged must match command line order
460 struct CommandLineOrderFileSorter
462 bool operator()(File
* left
, File
* right
)
464 return ( left
->ordinal() < right
->ordinal() );
469 #if LTO_API_VERSION >= 7
470 void Parser::ltoDiagnosticHandler(lto_codegen_diagnostic_severity_t severity
, const char* message
, void*)
472 switch ( severity
) {
475 warning("%s", message
);
478 throwf("%s", message
);
483 bool Parser::optimize( const std::vector
<const ld::Atom
*>& allAtoms
,
485 const OptimizeOptions
& options
,
486 ld::File::AtomHandler
& handler
,
487 std::vector
<const ld::Atom
*>& newAtoms
,
488 std::vector
<const char*>& additionalUndefines
)
490 const bool logMustPreserve
= false;
491 const bool logExtraOptions
= false;
492 const bool logBitcodeFiles
= false;
493 const bool logAtomsBeforeSync
= false;
495 // exit quickly if nothing to do
496 if ( _s_files
.size() == 0 )
499 // print out LTO version string if -v was used
500 if ( options
.verbose
)
501 fprintf(stderr
, "%s\n", ::lto_get_version());
503 // create optimizer and add each Reader
504 lto_code_gen_t generator
= ::lto_codegen_create();
505 #if LTO_API_VERSION >= 7
506 lto_codegen_set_diagnostic_handler(generator
, ltoDiagnosticHandler
, NULL
);
509 // <rdar://problem/12379604> The order that files are merged must match command line order
510 std::sort(_s_files
.begin(), _s_files
.end(), CommandLineOrderFileSorter());
511 ld::File::Ordinal lastOrdinal
;
512 for (std::vector
<File
*>::iterator it
=_s_files
.begin(); it
!= _s_files
.end(); ++it
) {
514 assert(f
->ordinal() > lastOrdinal
);
515 if ( logBitcodeFiles
) fprintf(stderr
, "lto_codegen_add_module(%s)\n", f
->path());
516 if ( ::lto_codegen_add_module(generator
, f
->module()) )
517 throwf("lto: could not merge in %s because '%s', using libLTO version '%s'", f
->path(), ::lto_get_error_message(), ::lto_get_version());
518 lastOrdinal
= f
->ordinal();
521 // add any -mllvm command line options
522 for (std::vector
<const char*>::const_iterator it
=options
.llvmOptions
->begin(); it
!= options
.llvmOptions
->end(); ++it
) {
523 if ( logExtraOptions
) fprintf(stderr
, "passing option to llvm: %s\n", *it
);
524 ::lto_codegen_debug_options(generator
, *it
);
527 // <rdar://problem/13687397> Need a way for LTO to get cpu variants (until that info is in bitcode)
528 if ( options
.mcpu
!= NULL
)
529 ::lto_codegen_set_cpu(generator
, options
.mcpu
);
531 // The atom graph uses directed edges (references). Collect all references where
532 // originating atom is not part of any LTO Reader. This allows optimizer to optimize an
533 // external (i.e. not originated from same .o file) reference if all originating atoms are also
534 // defined in llvm bitcode file.
535 CStringSet nonLLVMRefs
;
536 CStringToAtom llvmAtoms
;
537 bool hasNonllvmAtoms
= false;
538 for (std::vector
<const ld::Atom
*>::const_iterator it
= allAtoms
.begin(); it
!= allAtoms
.end(); ++it
) {
539 const ld::Atom
* atom
= *it
;
540 // only look at references that come from an atom that is not an llvm atom
541 if ( atom
->contentType() != ld::Atom::typeLTOtemporary
) {
542 if ( (atom
->section().type() != ld::Section::typeMachHeader
) && (atom
->definition() != ld::Atom::definitionProxy
) ) {
543 hasNonllvmAtoms
= true;
545 const ld::Atom
* target
;
546 for (ld::Fixup::iterator fit
=atom
->fixupsBegin(); fit
!= atom
->fixupsEnd(); ++fit
) {
547 switch ( fit
->binding
) {
548 case ld::Fixup::bindingDirectlyBound
:
549 // that reference an llvm atom
550 if ( fit
->u
.target
->contentType() == ld::Atom::typeLTOtemporary
)
551 nonLLVMRefs
.insert(fit
->u
.target
->name());
553 case ld::Fixup::bindingsIndirectlyBound
:
554 target
= state
.indirectBindingTable
[fit
->u
.bindingIndex
];
555 if ( (target
!= NULL
) && (target
->contentType() == ld::Atom::typeLTOtemporary
) )
556 nonLLVMRefs
.insert(target
->name());
563 llvmAtoms
[atom
->name()] = (Atom
*)atom
;
566 // if entry point is in a llvm bitcode file, it must be preserved by LTO
567 if ( state
.entryPoint
!= NULL
) {
568 if ( state
.entryPoint
->contentType() == ld::Atom::typeLTOtemporary
)
569 nonLLVMRefs
.insert(state
.entryPoint
->name());
572 // deadAtoms are the atoms that the linker coalesced. For instance weak or tentative definitions
573 // overriden by another atom. If any of these deadAtoms are llvm atoms and they were replaced
574 // with a mach-o atom, we need to tell the lto engine to preserve (not optimize away) its dead
575 // atom so that the linker can replace it with the mach-o one later.
576 CStringToAtom deadllvmAtoms
;
577 for (std::vector
<const ld::Atom
*>::const_iterator it
= allAtoms
.begin(); it
!= allAtoms
.end(); ++it
) {
578 const ld::Atom
* atom
= *it
;
579 if ( atom
->coalescedAway() && (atom
->contentType() == ld::Atom::typeLTOtemporary
) ) {
580 const char* name
= atom
->name();
581 if ( logMustPreserve
) fprintf(stderr
, "lto_codegen_add_must_preserve_symbol(%s) because linker coalesce away and replace with a mach-o atom\n", name
);
582 ::lto_codegen_add_must_preserve_symbol(generator
, name
);
583 deadllvmAtoms
[name
] = (Atom
*)atom
;
586 for (std::vector
<File
*>::iterator it
=_s_files
.begin(); it
!= _s_files
.end(); ++it
) {
588 for(uint32_t i
=0; i
< file
->_atomArrayCount
; ++i
) {
589 Atom
* llvmAtom
= &file
->_atomArray
[i
];
590 if ( llvmAtom
->coalescedAway() ) {
591 const char* name
= llvmAtom
->name();
592 if ( deadllvmAtoms
.find(name
) == deadllvmAtoms
.end() ) {
593 if ( logMustPreserve
)
594 fprintf(stderr
, "lto_codegen_add_must_preserve_symbol(%s) because linker coalesce away and replace with a mach-o atom\n", name
);
595 ::lto_codegen_add_must_preserve_symbol(generator
, name
);
596 deadllvmAtoms
[name
] = (Atom
*)llvmAtom
;
599 else if ( options
.linkerDeadStripping
&& !llvmAtom
->live() ) {
600 const char* name
= llvmAtom
->name();
601 deadllvmAtoms
[name
] = (Atom
*)llvmAtom
;
606 // tell code generator about symbols that must be preserved
607 for (CStringToAtom::iterator it
= llvmAtoms
.begin(); it
!= llvmAtoms
.end(); ++it
) {
608 const char* name
= it
->first
;
609 Atom
* atom
= it
->second
;
610 // Include llvm Symbol in export list if it meets one of following two conditions
611 // 1 - atom scope is global (and not linkage unit).
612 // 2 - included in nonLLVMRefs set.
613 // If a symbol is not listed in exportList then LTO is free to optimize it away.
614 if ( (atom
->scope() == ld::Atom::scopeGlobal
) && options
.preserveAllGlobals
) {
615 if ( logMustPreserve
) fprintf(stderr
, "lto_codegen_add_must_preserve_symbol(%s) because global symbol\n", name
);
616 ::lto_codegen_add_must_preserve_symbol(generator
, name
);
618 else if ( nonLLVMRefs
.find(name
) != nonLLVMRefs
.end() ) {
619 if ( logMustPreserve
) fprintf(stderr
, "lto_codegen_add_must_preserve_symbol(%s) because referenced by a mach-o atom\n", name
);
620 ::lto_codegen_add_must_preserve_symbol(generator
, name
);
624 // special case running ld -r on all bitcode files to produce another bitcode file (instead of mach-o)
625 if ( options
.relocatable
&& !hasNonllvmAtoms
) {
626 if ( ! ::lto_codegen_write_merged_modules(generator
, options
.outputFilePath
) ) {
627 // HACK, no good way to tell linker we are all done, so just quit
630 warning("could not produce merged bitcode file");
633 // set code-gen model
634 lto_codegen_model model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC
;
635 if ( options
.mainExecutable
) {
636 if ( options
.staticExecutable
) {
637 // x86_64 "static" or any "-static -pie" is really dynamic code model
638 if ( (options
.arch
== CPU_TYPE_X86_64
) || options
.pie
)
639 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC
;
641 model
= LTO_CODEGEN_PIC_MODEL_STATIC
;
645 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC
;
647 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC
;
651 if ( options
.allowTextRelocs
)
652 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC
;
654 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC
;
656 if ( ::lto_codegen_set_pic_model(generator
, model
) )
657 throwf("could not create set codegen model: %s", lto_get_error_message());
659 // if requested, save off merged bitcode file
660 if ( options
.saveTemps
) {
661 char tempBitcodePath
[MAXPATHLEN
];
662 strcpy(tempBitcodePath
, options
.outputFilePath
);
663 strcat(tempBitcodePath
, ".lto.bc");
664 ::lto_codegen_write_merged_modules(generator
, tempBitcodePath
);
667 #if LTO_API_VERSION >= 3
668 // find assembler next to linker
670 uint32_t bufSize
= PATH_MAX
;
671 if ( _NSGetExecutablePath(path
, &bufSize
) != -1 ) {
672 char* lastSlash
= strrchr(path
, '/');
673 if ( lastSlash
!= NULL
) {
674 strcpy(lastSlash
+1, "as");
675 struct stat statInfo
;
676 if ( stat(path
, &statInfo
) == 0 )
677 ::lto_codegen_set_assembler_path(generator
, path
);
681 // run code generator
683 const uint8_t* machOFile
= (uint8_t*)::lto_codegen_compile(generator
, &machOFileLen
);
684 if ( machOFile
== NULL
)
685 throwf("could not do LTO codegen: '%s', using libLTO version '%s'", ::lto_get_error_message(), ::lto_get_version());
687 // if requested, save off temp mach-o file
688 if ( options
.saveTemps
) {
689 char tempMachoPath
[MAXPATHLEN
];
690 strcpy(tempMachoPath
, options
.outputFilePath
);
691 strcat(tempMachoPath
, ".lto.o");
692 int fd
= ::open(tempMachoPath
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
694 ::write(fd
, machOFile
, machOFileLen
);
697 // save off merged bitcode file
698 char tempOptBitcodePath
[MAXPATHLEN
];
699 strcpy(tempOptBitcodePath
, options
.outputFilePath
);
700 strcat(tempOptBitcodePath
, ".lto.opt.bc");
701 ::lto_codegen_write_merged_modules(generator
, tempOptBitcodePath
);
704 // if needed, save temp mach-o file to specific location
705 if ( options
.tmpObjectFilePath
!= NULL
) {
706 int fd
= ::open(options
.tmpObjectFilePath
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
708 ::write(fd
, machOFile
, machOFileLen
);
712 warning("could not write LTO temp file '%s', errno=%d", options
.tmpObjectFilePath
, errno
);
716 // parse generated mach-o file into a MachOReader
717 ld::relocatable::File
* machoFile
= parseMachOFile(machOFile
, machOFileLen
, options
);
719 // sync generated mach-o atoms with existing atoms ld knows about
720 if ( logAtomsBeforeSync
) {
721 fprintf(stderr
, "llvmAtoms:\n");
722 for (CStringToAtom::iterator it
= llvmAtoms
.begin(); it
!= llvmAtoms
.end(); ++it
) {
723 const char* name
= it
->first
;
724 //Atom* atom = it->second;
725 fprintf(stderr
, "\t%s\n", name
);
727 fprintf(stderr
, "deadllvmAtoms:\n");
728 for (CStringToAtom::iterator it
= deadllvmAtoms
.begin(); it
!= deadllvmAtoms
.end(); ++it
) {
729 const char* name
= it
->first
;
730 //Atom* atom = it->second;
731 fprintf(stderr
, "\t%s\n", name
);
734 AtomSyncer
syncer(additionalUndefines
, newAtoms
, llvmAtoms
, deadllvmAtoms
, options
);
735 machoFile
->forEachAtom(syncer
);
737 // Remove InternalAtoms from ld
738 for (std::vector
<File
*>::iterator it
=_s_files
.begin(); it
!= _s_files
.end(); ++it
) {
739 (*it
)->internalAtom().setCoalescedAway();
741 // Remove Atoms from ld if code generator optimized them away
742 for (CStringToAtom::iterator li
= llvmAtoms
.begin(), le
= llvmAtoms
.end(); li
!= le
; ++li
) {
743 // check if setRealAtom() called on this Atom
744 if ( li
->second
->compiledAtom() == NULL
) {
745 //fprintf(stderr, "llvm optimized away %p %s\n", li->second, li->second->name());
746 li
->second
->setCoalescedAway();
750 // notify about file level attributes
751 handler
.doFile(*machoFile
);
753 // if final mach-o file has debug info, update original bitcode files to match
754 for (std::vector
<File
*>::iterator it
=_s_files
.begin(); it
!= _s_files
.end(); ++it
) {
755 (*it
)->setDebugInfo(machoFile
->debugInfo(), machoFile
->path(),
756 machoFile
->modificationTime(), machoFile
->cpuSubType());
763 void Parser::AtomSyncer::doAtom(const ld::Atom
& machoAtom
)
765 static const ld::Atom
* lastProxiedAtom
= NULL
;
766 static const ld::File
* lastProxiedFile
= NULL
;
767 // update proxy atoms to point to real atoms and find new atoms
768 const char* name
= machoAtom
.name();
769 if ( machoAtom
.scope() >= ld::Atom::scopeLinkageUnit
) {
770 CStringToAtom::iterator pos
= _llvmAtoms
.find(name
);
771 if ( pos
!= _llvmAtoms
.end() ) {
772 // turn Atom into a proxy for this mach-o atom
773 pos
->second
->setCompiledAtom(machoAtom
);
774 lastProxiedAtom
= &machoAtom
;
775 lastProxiedFile
= pos
->second
->file();
778 // an atom of this name was not in the allAtoms list the linker gave us
779 if ( _deadllvmAtoms
.find(name
) != _deadllvmAtoms
.end() ) {
780 // this corresponding to an atom that the linker coalesced away or marked not-live
781 if ( _options
.linkerDeadStripping
) {
782 // llvm seems to want this atom and -dead_strip is enabled, so it will be deleted if not needed, so add back
783 Atom
* llvmAtom
= _deadllvmAtoms
[name
];
784 llvmAtom
->setCompiledAtom(machoAtom
);
785 _newAtoms
.push_back(&machoAtom
);
788 // Don't pass it back as a new atom
793 // this is something new that lto conjured up, tell ld its new
794 _newAtoms
.push_back(&machoAtom
);
799 // ld only knew about non-static atoms, so this one must be new
800 _newAtoms
.push_back(&machoAtom
);
801 // <rdar://problem/15469363> if new static atom in same section as previous non-static atom, assign to same file as previous
802 if ( (lastProxiedAtom
!= NULL
) && (lastProxiedAtom
->section() == machoAtom
.section()) ) {
803 ld::Atom
* ma
= const_cast<ld::Atom
*>(&machoAtom
);
804 ma
->setFile(lastProxiedFile
);
808 // adjust fixups to go through proxy atoms
809 //fprintf(stderr, "adjusting fixups in atom: %s\n", machoAtom.name());
810 for (ld::Fixup::iterator fit
=machoAtom
.fixupsBegin(); fit
!= machoAtom
.fixupsEnd(); ++fit
) {
811 switch ( fit
->binding
) {
812 case ld::Fixup::bindingNone
:
814 case ld::Fixup::bindingByNameUnbound
:
815 // don't know if this target has been seen by linker before or if it is new
816 // be conservative and tell linker it is new
817 _additionalUndefines
.push_back(fit
->u
.name
);
818 //fprintf(stderr, " by name ref to: %s\n", fit->u.name);
820 case ld::Fixup::bindingDirectlyBound
:
821 // If mach-o atom is referencing another mach-o atom then
822 // reference is not going through Atom proxy. Fix it here to ensure that all
823 // llvm symbol references always go through Atom proxy.
824 if ( fit
->u
.target
->scope() != ld::Atom::scopeTranslationUnit
) {
825 const char* targetName
= fit
->u
.target
->name();
826 CStringToAtom::iterator pos
= _llvmAtoms
.find(targetName
);
827 if ( pos
!= _llvmAtoms
.end() ) {
828 fit
->u
.target
= pos
->second
;
831 // <rdar://problem/12859831> Don't unbind follow-on reference into by-name reference
832 if ( (_deadllvmAtoms
.find(targetName
) != _deadllvmAtoms
.end()) && (fit
->kind
!= ld::Fixup::kindNoneFollowOn
) ) {
833 // target was coalesed away and replace by mach-o atom from a non llvm .o file
834 fit
->binding
= ld::Fixup::bindingByNameUnbound
;
835 fit
->u
.name
= targetName
;
839 //fprintf(stderr, " direct ref to: %s (scope=%d)\n", fit->u.target->name(), fit->u.target->scope());
841 case ld::Fixup::bindingByContentBound
:
842 //fprintf(stderr, " direct by content to: %s\n", fit->u.target->name());
844 case ld::Fixup::bindingsIndirectlyBound
:
845 assert(0 && "indirect binding found in initial mach-o file?");
846 //fprintf(stderr, " indirect by content to: %u\n", fit->u.bindingIndex);
854 static pthread_mutex_t lto_lock
;
856 Mutex() { pthread_mutex_lock(<o_lock
); }
857 ~Mutex() { pthread_mutex_unlock(<o_lock
); }
859 pthread_mutex_t
Mutex::lto_lock
= PTHREAD_MUTEX_INITIALIZER
;
862 // Used by archive reader to see if member is an llvm bitcode file
864 bool isObjectFile(const uint8_t* fileContent
, uint64_t fileLength
, cpu_type_t architecture
, cpu_subtype_t subarch
)
867 return Parser::validFile(fileContent
, fileLength
, architecture
, subarch
);
872 // main function used by linker to instantiate ld::Files
874 ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
875 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
876 cpu_type_t architecture
, cpu_subtype_t subarch
, bool logAllFiles
,
877 bool verboseOptimizationHints
)
880 if ( Parser::validFile(fileContent
, fileLength
, architecture
, subarch
) )
881 return Parser::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, architecture
, subarch
, logAllFiles
, verboseOptimizationHints
);
887 // used by "ld -v" to report version of libLTO.dylib being used
889 const char* version()
892 return ::lto_get_version();
897 // used by ld for error reporting
899 bool libLTOisLoaded()
902 return (::lto_get_version() != NULL
);
906 // used by ld for error reporting
908 const char* archName(const uint8_t* fileContent
, uint64_t fileLength
)
911 return Parser::fileKind(fileContent
, fileLength
);
915 // used by ld for doing link time optimization
917 bool optimize( const std::vector
<const ld::Atom
*>& allAtoms
,
919 const OptimizeOptions
& options
,
920 ld::File::AtomHandler
& handler
,
921 std::vector
<const ld::Atom
*>& newAtoms
,
922 std::vector
<const char*>& additionalUndefines
)
925 return Parser::optimize(allAtoms
, state
, options
, handler
, newAtoms
, additionalUndefines
);