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"
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, 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; }
110 lto_module_t
module() { return _module
; }
111 class InternalAtom
& internalAtom() { return _internalAtom
; }
112 void setDebugInfo(ld::relocatable::File::DebugInfoKind k
,
113 const char* pth
, time_t modTime
, uint32_t subtype
)
115 _debugInfoPath
= pth
;
116 _debugInfoModTime
= modTime
;
117 _cpuSubType
= subtype
;}
121 friend class InternalAtom
;
124 cpu_type_t _architecture
;
125 class InternalAtom _internalAtom
;
126 class Atom
* _atomArray
;
127 uint32_t _atomArrayCount
;
128 lto_module_t _module
;
129 const char* _debugInfoPath
;
130 time_t _debugInfoModTime
;
131 ld::Section _section
;
132 ld::Fixup _fixupToInternal
;
133 ld::relocatable::File::DebugInfoKind _debugInfo
;
134 uint32_t _cpuSubType
;
138 // Atom acts as a proxy Atom for the symbols that are exported by LLVM bitcode file. Initially,
139 // Reader creates Atoms to allow linker proceed with usual symbol resolution phase. After
140 // optimization is performed, real Atoms are created for these symobls. However these real Atoms
141 // are not inserted into global symbol table. Atom holds real Atom and forwards appropriate
142 // methods to real atom.
144 class Atom
: public ld::Atom
147 Atom(File
& f
, const char* name
, ld::Atom::Scope s
,
148 ld::Atom::Definition d
, ld::Atom::Combine c
, ld::Atom::Alignment a
, bool ah
);
150 // overrides of ld::Atom
151 virtual ld::File
* file() const { return &_file
; }
152 virtual const char* translationUnitSource() const
153 { return (_compiledAtom
? _compiledAtom
->translationUnitSource() : NULL
); }
154 virtual const char* name() const { return _name
; }
155 virtual uint64_t size() const { return (_compiledAtom
? _compiledAtom
->size() : 0); }
156 virtual uint64_t objectAddress() const { return (_compiledAtom
? _compiledAtom
->objectAddress() : 0); }
157 virtual void copyRawContent(uint8_t buffer
[]) const
158 { if (_compiledAtom
) _compiledAtom
->copyRawContent(buffer
); }
159 virtual const uint8_t* rawContentPointer() const
160 { return (_compiledAtom
? _compiledAtom
->rawContentPointer() : NULL
); }
161 virtual unsigned long contentHash(const class ld::IndirectBindingTable
& ibt
) const
162 { return (_compiledAtom
? _compiledAtom
->contentHash(ibt
) : 0); }
163 virtual bool canCoalesceWith(const ld::Atom
& rhs
, const class ld::IndirectBindingTable
& ibt
) const
164 { return (_compiledAtom
? _compiledAtom
->canCoalesceWith(rhs
,ibt
) : false); }
165 virtual ld::Fixup::iterator
fixupsBegin() const
166 { return (_compiledAtom
? _compiledAtom
->fixupsBegin() : (ld::Fixup
*)&_file
._fixupToInternal
); }
167 virtual ld::Fixup::iterator
fixupsEnd() const
168 { return (_compiledAtom
? _compiledAtom
->fixupsEnd() : &((ld::Fixup
*)&_file
._fixupToInternal
)[1]); }
169 virtual ld::Atom::UnwindInfo::iterator
beginUnwind() const
170 { return (_compiledAtom
? _compiledAtom
->beginUnwind() : NULL
); }
171 virtual ld::Atom::UnwindInfo::iterator
endUnwind() const
172 { return (_compiledAtom
? _compiledAtom
->endUnwind() : NULL
); }
173 virtual ld::Atom::LineInfo::iterator
beginLineInfo() const
174 { return (_compiledAtom
? _compiledAtom
->beginLineInfo() : NULL
); }
175 virtual ld::Atom::LineInfo::iterator
endLineInfo() const
176 { return (_compiledAtom
? _compiledAtom
->endLineInfo() : NULL
); }
178 const ld::Atom
* compiledAtom() { return _compiledAtom
; }
179 void setCompiledAtom(const ld::Atom
& atom
);
185 const ld::Atom
* _compiledAtom
;
197 static bool validFile(const uint8_t* fileContent
, uint64_t fileLength
, cpu_type_t architecture
, cpu_subtype_t subarch
);
198 static const char* fileKind(const uint8_t* fileContent
, uint64_t fileLength
);
199 static File
* parse(const uint8_t* fileContent
, uint64_t fileLength
, const char* path
,
200 time_t modTime
, ld::File::Ordinal ordinal
, cpu_type_t architecture
, cpu_subtype_t subarch
, bool logAllFiles
);
201 static bool libLTOisLoaded() { return (::lto_get_version() != NULL
); }
202 static bool optimize( const std::vector
<const ld::Atom
*>& allAtoms
,
204 const OptimizeOptions
& options
,
205 ld::File::AtomHandler
& handler
,
206 std::vector
<const ld::Atom
*>& newAtoms
,
207 std::vector
<const char*>& additionalUndefines
);
209 static const char* ltoVersion() { return ::lto_get_version(); }
212 static const char* tripletPrefixForArch(cpu_type_t arch
);
213 static ld::relocatable::File
* parseMachOFile(const uint8_t* p
, size_t len
, const OptimizeOptions
& options
);
215 typedef std::unordered_set
<const char*, ld::CStringHash
, ld::CStringEquals
> CStringSet
;
216 typedef std::unordered_map
<const char*, Atom
*, ld::CStringHash
, ld::CStringEquals
> CStringToAtom
;
218 class AtomSyncer
: public ld::File::AtomHandler
{
220 AtomSyncer(std::vector
<const char*>& a
, std::vector
<const ld::Atom
*>&na
,
221 CStringToAtom la
, CStringToAtom dla
, const OptimizeOptions
& options
) :
222 _options(options
), _additionalUndefines(a
), _newAtoms(na
), _llvmAtoms(la
), _deadllvmAtoms(dla
) { }
223 virtual void doAtom(const class ld::Atom
&);
224 virtual void doFile(const class ld::File
&) { }
226 const OptimizeOptions
& _options
;
227 std::vector
<const char*>& _additionalUndefines
;
228 std::vector
<const ld::Atom
*>& _newAtoms
;
229 CStringToAtom _llvmAtoms
;
230 CStringToAtom _deadllvmAtoms
;
233 static std::vector
<File
*> _s_files
;
236 std::vector
<File
*> Parser::_s_files
;
239 bool Parser::validFile(const uint8_t* fileContent
, uint64_t fileLength
, cpu_type_t architecture
, cpu_subtype_t subarch
)
241 for (const ArchInfo
* t
=archInfoArray
; t
->archName
!= NULL
; ++t
) {
242 if ( (architecture
== t
->cpuType
) && (!(t
->isSubType
) || (subarch
== t
->cpuSubType
)) ) {
243 bool result
= ::lto_module_is_object_file_in_memory_for_target(fileContent
, fileLength
, t
->llvmTriplePrefix
);
245 // <rdar://problem/8434487> LTO only supports thumbv7 not armv7
246 if ( t
->llvmTriplePrefixAlt
[0] != '\0' ) {
247 result
= ::lto_module_is_object_file_in_memory_for_target(fileContent
, fileLength
, t
->llvmTriplePrefixAlt
);
256 const char* Parser::fileKind(const uint8_t* p
, uint64_t fileLength
)
258 if ( (p
[0] == 0xDE) && (p
[1] == 0xC0) && (p
[2] == 0x17) && (p
[3] == 0x0B) ) {
259 cpu_type_t arch
= LittleEndian::get32(*((uint32_t*)(&p
[16])));
260 for (const ArchInfo
* t
=archInfoArray
; t
->archName
!= NULL
; ++t
) {
261 if ( arch
== t
->cpuType
) {
262 if ( t
->isSubType
) {
263 if ( ::lto_module_is_object_file_in_memory_for_target(p
, fileLength
, t
->llvmTriplePrefix
) )
271 return "unknown bitcode architecture";
276 File
* Parser::parse(const uint8_t* fileContent
, uint64_t fileLength
, const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
277 cpu_type_t architecture
, cpu_subtype_t subarch
, bool logAllFiles
)
279 File
* f
= new File(path
, modTime
, ordinal
, fileContent
, fileLength
, architecture
);
280 _s_files
.push_back(f
);
282 printf("%s\n", path
);
287 ld::relocatable::File
* Parser::parseMachOFile(const uint8_t* p
, size_t len
, const OptimizeOptions
& options
)
289 mach_o::relocatable::ParserOptions objOpts
;
290 objOpts
.architecture
= options
.arch
;
291 objOpts
.objSubtypeMustMatch
= false;
292 objOpts
.logAllFiles
= false;
293 objOpts
.convertUnwindInfo
= true;
296 // mach-o parsing is done in-memory, but need path for debug notes
297 const char* path
= "/tmp/lto.o";
299 if ( options
.tmpObjectFilePath
!= NULL
) {
300 path
= options
.tmpObjectFilePath
;
301 struct stat statBuffer
;
302 if ( stat(options
.tmpObjectFilePath
, &statBuffer
) == 0 )
303 modTime
= statBuffer
.st_mtime
;
306 ld::relocatable::File
* result
= mach_o::relocatable::parse(p
, len
, path
, modTime
, ld::File::Ordinal::LTOOrdinal(), objOpts
);
307 if ( result
!= NULL
)
309 throw "LLVM LTO, file is not of required architecture";
314 File::File(const char* pth
, time_t mTime
, ld::File::Ordinal ordinal
, const uint8_t* content
, uint32_t contentLength
, cpu_type_t arch
)
315 : ld::relocatable::File(pth
,mTime
,ordinal
), _architecture(arch
), _internalAtom(*this),
316 _atomArray(NULL
), _atomArrayCount(0), _module(NULL
), _debugInfoPath(pth
),
317 _section("__TEXT_", "__tmp_lto", ld::Section::typeTempLTO
),
318 _fixupToInternal(0, ld::Fixup::k1of1
, ld::Fixup::kindNone
, &_internalAtom
),
319 _debugInfo(ld::relocatable::File::kDebugInfoNone
), _cpuSubType(0)
321 const bool log
= false;
323 // create llvm module
324 _module
= ::lto_module_create_from_memory(content
, contentLength
);
325 if ( _module
== NULL
)
326 throwf("could not parse object file %s: '%s', using libLTO version '%s'", pth
, ::lto_get_error_message(), ::lto_get_version());
328 if ( log
) fprintf(stderr
, "bitcode file: %s\n", pth
);
330 // create atom for each global symbol in module
331 uint32_t count
= ::lto_module_get_num_symbols(_module
);
332 _atomArray
= (Atom
*)malloc(sizeof(Atom
)*count
);
333 for (uint32_t i
=0; i
< count
; ++i
) {
334 const char* name
= ::lto_module_get_symbol_name(_module
, i
);
335 lto_symbol_attributes attr
= lto_module_get_symbol_attribute(_module
, i
);
337 // <rdar://problem/6378110> LTO doesn't like dtrace symbols
338 // ignore dtrace static probes for now
339 // later when codegen is done and a mach-o file is produces the probes will be processed
340 if ( (strncmp(name
, "___dtrace_probe$", 16) == 0) || (strncmp(name
, "___dtrace_isenabled$", 20) == 0) )
343 ld::Atom::Definition def
;
344 ld::Atom::Combine combine
= ld::Atom::combineNever
;
345 switch ( attr
& LTO_SYMBOL_DEFINITION_MASK
) {
346 case LTO_SYMBOL_DEFINITION_REGULAR
:
347 def
= ld::Atom::definitionRegular
;
349 case LTO_SYMBOL_DEFINITION_TENTATIVE
:
350 def
= ld::Atom::definitionTentative
;
352 case LTO_SYMBOL_DEFINITION_WEAK
:
353 def
= ld::Atom::definitionRegular
;
354 combine
= ld::Atom::combineByName
;
356 case LTO_SYMBOL_DEFINITION_UNDEFINED
:
357 case LTO_SYMBOL_DEFINITION_WEAKUNDEF
:
358 def
= ld::Atom::definitionProxy
;
361 throwf("unknown definition kind for symbol %s in bitcode file %s", name
, pth
);
364 // make LLVM atoms for definitions and a reference for undefines
365 if ( def
!= ld::Atom::definitionProxy
) {
366 ld::Atom::Scope scope
;
367 bool autohide
= false;
368 switch ( attr
& LTO_SYMBOL_SCOPE_MASK
) {
369 case LTO_SYMBOL_SCOPE_INTERNAL
:
370 scope
= ld::Atom::scopeTranslationUnit
;
372 case LTO_SYMBOL_SCOPE_HIDDEN
:
373 scope
= ld::Atom::scopeLinkageUnit
;
375 case LTO_SYMBOL_SCOPE_DEFAULT
:
376 scope
= ld::Atom::scopeGlobal
;
378 #if LTO_API_VERSION >= 4
379 case LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN
:
380 scope
= ld::Atom::scopeGlobal
;
385 throwf("unknown scope for symbol %s in bitcode file %s", name
, pth
);
387 // only make atoms for non-internal symbols
388 if ( scope
== ld::Atom::scopeTranslationUnit
)
390 uint8_t alignment
= (attr
& LTO_SYMBOL_ALIGNMENT_MASK
);
391 // make Atom using placement new operator
392 new (&_atomArray
[_atomArrayCount
++]) Atom(*this, name
, scope
, def
, combine
, alignment
, autohide
);
393 if ( scope
!= ld::Atom::scopeTranslationUnit
)
394 _internalAtom
.addReference(name
);
395 if ( log
) fprintf(stderr
, "\t0x%08X %s\n", attr
, name
);
398 // add to list of external references
399 _internalAtom
.addReference(name
);
400 if ( log
) fprintf(stderr
, "\t%s (undefined)\n", name
);
407 if ( _module
!= NULL
)
408 ::lto_module_dispose(_module
);
411 bool File::forEachAtom(ld::File::AtomHandler
& handler
) const
413 handler
.doAtom(_internalAtom
);
414 for(uint32_t i
=0; i
< _atomArrayCount
; ++i
) {
415 handler
.doAtom(_atomArray
[i
]);
420 InternalAtom::InternalAtom(File
& f
)
421 : ld::Atom(f
._section
, ld::Atom::definitionRegular
, ld::Atom::combineNever
, ld::Atom::scopeTranslationUnit
,
422 ld::Atom::typeLTOtemporary
, ld::Atom::symbolTableNotIn
, true, false, false, ld::Atom::Alignment(0)),
427 Atom::Atom(File
& f
, const char* nm
, ld::Atom::Scope s
, ld::Atom::Definition d
, ld::Atom::Combine c
,
428 ld::Atom::Alignment a
, bool ah
)
429 : ld::Atom(f
._section
, d
, c
, s
, ld::Atom::typeLTOtemporary
,
430 ld::Atom::symbolTableIn
, false, false, false, a
),
431 _file(f
), _name(nm
), _compiledAtom(NULL
)
437 void Atom::setCompiledAtom(const ld::Atom
& atom
)
439 // set delegate so virtual methods go to it
440 _compiledAtom
= &atom
;
442 //fprintf(stderr, "setting lto atom %p to delegate to mach-o atom %p (%s)\n", this, &atom, atom.name());
444 // update fields in ld::Atom to match newly constructed mach-o atom
445 (const_cast<Atom
*>(this))->setAttributesFromAtom(atom
);
450 bool Parser::optimize( const std::vector
<const ld::Atom
*>& allAtoms
,
452 const OptimizeOptions
& options
,
453 ld::File::AtomHandler
& handler
,
454 std::vector
<const ld::Atom
*>& newAtoms
,
455 std::vector
<const char*>& additionalUndefines
)
457 const bool logMustPreserve
= false;
458 const bool logExtraOptions
= false;
459 const bool logBitcodeFiles
= false;
460 const bool logAtomsBeforeSync
= false;
462 // exit quickly if nothing to do
463 if ( _s_files
.size() == 0 )
466 // print out LTO version string if -v was used
467 if ( options
.verbose
)
468 fprintf(stderr
, "%s\n", ::lto_get_version());
470 // create optimizer and add each Reader
471 lto_code_gen_t generator
= ::lto_codegen_create();
472 for (std::vector
<File
*>::iterator it
=_s_files
.begin(); it
!= _s_files
.end(); ++it
) {
473 if ( logBitcodeFiles
) fprintf(stderr
, "lto_codegen_add_module(%s)\n", (*it
)->path());
474 if ( ::lto_codegen_add_module(generator
, (*it
)->module()) )
475 throwf("lto: could not merge in %s because '%s', using libLTO version '%s'", (*it
)->path(), ::lto_get_error_message(), ::lto_get_version());
478 // add any -mllvm command line options
479 for (std::vector
<const char*>::const_iterator it
=options
.llvmOptions
->begin(); it
!= options
.llvmOptions
->end(); ++it
) {
480 if ( logExtraOptions
) fprintf(stderr
, "passing option to llvm: %s\n", *it
);
481 ::lto_codegen_debug_options(generator
, *it
);
484 // The atom graph uses directed edges (references). Collect all references where
485 // originating atom is not part of any LTO Reader. This allows optimizer to optimize an
486 // external (i.e. not originated from same .o file) reference if all originating atoms are also
487 // defined in llvm bitcode file.
488 CStringSet nonLLVMRefs
;
489 CStringToAtom llvmAtoms
;
490 bool hasNonllvmAtoms
= false;
491 for (std::vector
<const ld::Atom
*>::const_iterator it
= allAtoms
.begin(); it
!= allAtoms
.end(); ++it
) {
492 const ld::Atom
* atom
= *it
;
493 // only look at references that come from an atom that is not an llvm atom
494 if ( atom
->contentType() != ld::Atom::typeLTOtemporary
) {
495 if ( (atom
->section().type() != ld::Section::typeMachHeader
) && (atom
->definition() != ld::Atom::definitionProxy
) ) {
496 hasNonllvmAtoms
= true;
498 const ld::Atom
* target
;
499 for (ld::Fixup::iterator fit
=atom
->fixupsBegin(); fit
!= atom
->fixupsEnd(); ++fit
) {
500 switch ( fit
->binding
) {
501 case ld::Fixup::bindingDirectlyBound
:
502 // that reference an llvm atom
503 if ( fit
->u
.target
->contentType() == ld::Atom::typeLTOtemporary
)
504 nonLLVMRefs
.insert(fit
->u
.target
->name());
506 case ld::Fixup::bindingsIndirectlyBound
:
507 target
= state
.indirectBindingTable
[fit
->u
.bindingIndex
];
508 if ( target
== NULL
)
509 throwf("'%s' in %s contains undefined reference", atom
->name(), atom
->file()->path());
510 assert(target
!= NULL
);
511 if ( target
->contentType() == ld::Atom::typeLTOtemporary
)
512 nonLLVMRefs
.insert(target
->name());
519 llvmAtoms
[atom
->name()] = (Atom
*)atom
;
522 // if entry point is in a llvm bitcode file, it must be preserved by LTO
523 if ( state
.entryPoint
!= NULL
) {
524 if ( state
.entryPoint
->contentType() == ld::Atom::typeLTOtemporary
)
525 nonLLVMRefs
.insert(state
.entryPoint
->name());
528 // deadAtoms are the atoms that the linker coalesced. For instance weak or tentative definitions
529 // overriden by another atom. If any of these deadAtoms are llvm atoms and they were replaced
530 // with a mach-o atom, we need to tell the lto engine to preserve (not optimize away) its dead
531 // atom so that the linker can replace it with the mach-o one later.
532 CStringToAtom deadllvmAtoms
;
533 for (std::vector
<const ld::Atom
*>::const_iterator it
= allAtoms
.begin(); it
!= allAtoms
.end(); ++it
) {
534 const ld::Atom
* atom
= *it
;
535 if ( atom
->coalescedAway() && (atom
->contentType() == ld::Atom::typeLTOtemporary
) ) {
536 const char* name
= atom
->name();
537 if ( logMustPreserve
) fprintf(stderr
, "lto_codegen_add_must_preserve_symbol(%s) because linker coalesce away and replace with a mach-o atom\n", name
);
538 ::lto_codegen_add_must_preserve_symbol(generator
, name
);
539 deadllvmAtoms
[name
] = (Atom
*)atom
;
542 for (std::vector
<File
*>::iterator it
=_s_files
.begin(); it
!= _s_files
.end(); ++it
) {
544 for(uint32_t i
=0; i
< file
->_atomArrayCount
; ++i
) {
545 Atom
* llvmAtom
= &file
->_atomArray
[i
];
546 if ( llvmAtom
->coalescedAway() ) {
547 const char* name
= llvmAtom
->name();
548 if ( deadllvmAtoms
.find(name
) == deadllvmAtoms
.end() ) {
549 if ( logMustPreserve
)
550 fprintf(stderr
, "lto_codegen_add_must_preserve_symbol(%s) because linker coalesce away and replace with a mach-o atom\n", name
);
551 ::lto_codegen_add_must_preserve_symbol(generator
, name
);
552 deadllvmAtoms
[name
] = (Atom
*)llvmAtom
;
555 else if ( options
.linkerDeadStripping
&& !llvmAtom
->live() ) {
556 const char* name
= llvmAtom
->name();
557 deadllvmAtoms
[name
] = (Atom
*)llvmAtom
;
562 // tell code generator about symbols that must be preserved
563 for (CStringToAtom::iterator it
= llvmAtoms
.begin(); it
!= llvmAtoms
.end(); ++it
) {
564 const char* name
= it
->first
;
565 Atom
* atom
= it
->second
;
566 // Include llvm Symbol in export list if it meets one of following two conditions
567 // 1 - atom scope is global (and not linkage unit).
568 // 2 - included in nonLLVMRefs set.
569 // If a symbol is not listed in exportList then LTO is free to optimize it away.
570 if ( (atom
->scope() == ld::Atom::scopeGlobal
) && options
.preserveAllGlobals
) {
571 if ( logMustPreserve
) fprintf(stderr
, "lto_codegen_add_must_preserve_symbol(%s) because global symbol\n", name
);
572 ::lto_codegen_add_must_preserve_symbol(generator
, name
);
574 else if ( nonLLVMRefs
.find(name
) != nonLLVMRefs
.end() ) {
575 if ( logMustPreserve
) fprintf(stderr
, "lto_codegen_add_must_preserve_symbol(%s) because referenced by a mach-o atom\n", name
);
576 ::lto_codegen_add_must_preserve_symbol(generator
, name
);
580 // special case running ld -r on all bitcode files to produce another bitcode file (instead of mach-o)
581 if ( options
.relocatable
&& !hasNonllvmAtoms
) {
582 if ( ! ::lto_codegen_write_merged_modules(generator
, options
.outputFilePath
) ) {
583 // HACK, no good way to tell linker we are all done, so just quit
586 warning("could not produce merged bitcode file");
589 // set code-gen model
590 lto_codegen_model model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC
;
591 if ( options
.mainExecutable
) {
592 if ( options
.staticExecutable
) {
593 // x86_64 "static" or any "-static -pie" is really dynamic code model
594 if ( (options
.arch
== CPU_TYPE_X86_64
) || options
.pie
)
595 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC
;
597 model
= LTO_CODEGEN_PIC_MODEL_STATIC
;
601 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC
;
603 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC
;
607 if ( options
.allowTextRelocs
)
608 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC
;
610 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC
;
612 if ( ::lto_codegen_set_pic_model(generator
, model
) )
613 throwf("could not create set codegen model: %s", lto_get_error_message());
615 // if requested, save off merged bitcode file
616 if ( options
.saveTemps
) {
617 char tempBitcodePath
[MAXPATHLEN
];
618 strcpy(tempBitcodePath
, options
.outputFilePath
);
619 strcat(tempBitcodePath
, ".lto.bc");
620 ::lto_codegen_write_merged_modules(generator
, tempBitcodePath
);
623 #if LTO_API_VERSION >= 3
624 // find assembler next to linker
626 uint32_t bufSize
= PATH_MAX
;
627 if ( _NSGetExecutablePath(path
, &bufSize
) != -1 ) {
628 char* lastSlash
= strrchr(path
, '/');
629 if ( lastSlash
!= NULL
) {
630 strcpy(lastSlash
+1, "as");
631 struct stat statInfo
;
632 if ( stat(path
, &statInfo
) == 0 )
633 ::lto_codegen_set_assembler_path(generator
, path
);
637 // run code generator
639 const uint8_t* machOFile
= (uint8_t*)::lto_codegen_compile(generator
, &machOFileLen
);
640 if ( machOFile
== NULL
)
641 throwf("could not do LTO codegen: '%s', using libLTO version '%s'", ::lto_get_error_message(), ::lto_get_version());
643 // if requested, save off temp mach-o file
644 if ( options
.saveTemps
) {
645 char tempMachoPath
[MAXPATHLEN
];
646 strcpy(tempMachoPath
, options
.outputFilePath
);
647 strcat(tempMachoPath
, ".lto.o");
648 int fd
= ::open(tempMachoPath
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
650 ::write(fd
, machOFile
, machOFileLen
);
653 // save off merged bitcode file
654 char tempOptBitcodePath
[MAXPATHLEN
];
655 strcpy(tempOptBitcodePath
, options
.outputFilePath
);
656 strcat(tempOptBitcodePath
, ".lto.opt.bc");
657 ::lto_codegen_write_merged_modules(generator
, tempOptBitcodePath
);
660 // if needed, save temp mach-o file to specific location
661 if ( options
.tmpObjectFilePath
!= NULL
) {
662 int fd
= ::open(options
.tmpObjectFilePath
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
664 ::write(fd
, machOFile
, machOFileLen
);
668 warning("could not write LTO temp file '%s', errno=%d", options
.tmpObjectFilePath
, errno
);
672 // parse generated mach-o file into a MachOReader
673 ld::relocatable::File
* machoFile
= parseMachOFile(machOFile
, machOFileLen
, options
);
675 // sync generated mach-o atoms with existing atoms ld knows about
676 if ( logAtomsBeforeSync
) {
677 fprintf(stderr
, "llvmAtoms:\n");
678 for (CStringToAtom::iterator it
= llvmAtoms
.begin(); it
!= llvmAtoms
.end(); ++it
) {
679 const char* name
= it
->first
;
680 //Atom* atom = it->second;
681 fprintf(stderr
, "\t%s\n", name
);
683 fprintf(stderr
, "deadllvmAtoms:\n");
684 for (CStringToAtom::iterator it
= deadllvmAtoms
.begin(); it
!= deadllvmAtoms
.end(); ++it
) {
685 const char* name
= it
->first
;
686 //Atom* atom = it->second;
687 fprintf(stderr
, "\t%s\n", name
);
690 AtomSyncer
syncer(additionalUndefines
, newAtoms
, llvmAtoms
, deadllvmAtoms
, options
);
691 machoFile
->forEachAtom(syncer
);
693 // Remove InternalAtoms from ld
694 for (std::vector
<File
*>::iterator it
=_s_files
.begin(); it
!= _s_files
.end(); ++it
) {
695 (*it
)->internalAtom().setCoalescedAway();
697 // Remove Atoms from ld if code generator optimized them away
698 for (CStringToAtom::iterator li
= llvmAtoms
.begin(), le
= llvmAtoms
.end(); li
!= le
; ++li
) {
699 // check if setRealAtom() called on this Atom
700 if ( li
->second
->compiledAtom() == NULL
) {
701 //fprintf(stderr, "llvm optimized away %p %s\n", li->second, li->second->name());
702 li
->second
->setCoalescedAway();
706 // notify about file level attributes
707 handler
.doFile(*machoFile
);
709 // if final mach-o file has debug info, update original bitcode files to match
710 for (std::vector
<File
*>::iterator it
=_s_files
.begin(); it
!= _s_files
.end(); ++it
) {
711 (*it
)->setDebugInfo(machoFile
->debugInfo(), machoFile
->path(),
712 machoFile
->modificationTime(), machoFile
->cpuSubType());
719 void Parser::AtomSyncer::doAtom(const ld::Atom
& machoAtom
)
721 // update proxy atoms to point to real atoms and find new atoms
722 const char* name
= machoAtom
.name();
723 if ( machoAtom
.scope() >= ld::Atom::scopeLinkageUnit
) {
724 CStringToAtom::iterator pos
= _llvmAtoms
.find(name
);
725 if ( pos
!= _llvmAtoms
.end() ) {
726 // turn Atom into a proxy for this mach-o atom
727 pos
->second
->setCompiledAtom(machoAtom
);
730 // an atom of this name was not in the allAtoms list the linker gave us
731 if ( _deadllvmAtoms
.find(name
) != _deadllvmAtoms
.end() ) {
732 // this corresponding to an atom that the linker coalesced away or marked not-live
733 if ( _options
.linkerDeadStripping
) {
734 // llvm seems to want this atom and -dead_strip is enabled, so it will be deleted if not needed, so add back
735 Atom
* llvmAtom
= _deadllvmAtoms
[name
];
736 llvmAtom
->setCompiledAtom(machoAtom
);
737 _newAtoms
.push_back(&machoAtom
);
740 // Don't pass it back as a new atom
745 // this is something new that lto conjured up, tell ld its new
746 _newAtoms
.push_back(&machoAtom
);
751 // ld only knew about non-static atoms, so this one must be new
752 _newAtoms
.push_back(&machoAtom
);
755 // adjust fixups to go through proxy atoms
756 //fprintf(stderr, "adjusting fixups in atom: %s\n", machoAtom.name());
757 for (ld::Fixup::iterator fit
=machoAtom
.fixupsBegin(); fit
!= machoAtom
.fixupsEnd(); ++fit
) {
758 switch ( fit
->binding
) {
759 case ld::Fixup::bindingNone
:
761 case ld::Fixup::bindingByNameUnbound
:
762 // don't know if this target has been seen by linker before or if it is new
763 // be conservative and tell linker it is new
764 _additionalUndefines
.push_back(fit
->u
.name
);
765 //fprintf(stderr, " by name ref to: %s\n", fit->u.name);
767 case ld::Fixup::bindingDirectlyBound
:
768 // If mach-o atom is referencing another mach-o atom then
769 // reference is not going through Atom proxy. Fix it here to ensure that all
770 // llvm symbol references always go through Atom proxy.
771 if ( fit
->u
.target
->scope() != ld::Atom::scopeTranslationUnit
) {
772 const char* targetName
= fit
->u
.target
->name();
773 CStringToAtom::iterator pos
= _llvmAtoms
.find(targetName
);
774 if ( pos
!= _llvmAtoms
.end() ) {
775 fit
->u
.target
= pos
->second
;
778 if ( _deadllvmAtoms
.find(targetName
) != _deadllvmAtoms
.end() ) {
779 // target was coalesed away and replace by mach-o atom from a non llvm .o file
780 fit
->binding
= ld::Fixup::bindingByNameUnbound
;
781 fit
->u
.name
= targetName
;
785 //fprintf(stderr, " direct ref to: %s (scope=%d)\n", fit->u.target->name(), fit->u.target->scope());
787 case ld::Fixup::bindingByContentBound
:
788 //fprintf(stderr, " direct by content to: %s\n", fit->u.target->name());
790 case ld::Fixup::bindingsIndirectlyBound
:
791 assert(0 && "indirect binding found in initial mach-o file?");
792 //fprintf(stderr, " indirect by content to: %u\n", fit->u.bindingIndex);
800 static pthread_mutex_t lto_lock
;
802 Mutex() { pthread_mutex_lock(<o_lock
); }
803 ~Mutex() { pthread_mutex_unlock(<o_lock
); }
805 pthread_mutex_t
Mutex::lto_lock
= PTHREAD_MUTEX_INITIALIZER
;
808 // Used by archive reader to see if member is an llvm bitcode file
810 bool isObjectFile(const uint8_t* fileContent
, uint64_t fileLength
, cpu_type_t architecture
, cpu_subtype_t subarch
)
813 return Parser::validFile(fileContent
, fileLength
, architecture
, subarch
);
818 // main function used by linker to instantiate ld::Files
820 ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
821 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
,
822 cpu_type_t architecture
, cpu_subtype_t subarch
, bool logAllFiles
)
825 if ( Parser::validFile(fileContent
, fileLength
, architecture
, subarch
) )
826 return Parser::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, architecture
, subarch
, logAllFiles
);
832 // used by "ld -v" to report version of libLTO.dylib being used
834 const char* version()
837 return ::lto_get_version();
842 // used by ld for error reporting
844 bool libLTOisLoaded()
847 return (::lto_get_version() != NULL
);
851 // used by ld for error reporting
853 const char* archName(const uint8_t* fileContent
, uint64_t fileLength
)
856 return Parser::fileKind(fileContent
, fileLength
);
860 // used by ld for doing link time optimization
862 bool optimize( const std::vector
<const ld::Atom
*>& allAtoms
,
864 const OptimizeOptions
& options
,
865 ld::File::AtomHandler
& handler
,
866 std::vector
<const ld::Atom
*>& newAtoms
,
867 std::vector
<const char*>& additionalUndefines
)
870 return Parser::optimize(allAtoms
, state
, options
, handler
, newAtoms
, additionalUndefines
);