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>
33 #include <mach-o/dyld.h>
35 #include <ext/hash_set>
36 #include <ext/hash_map>
38 #include "MachOFileAbstraction.hpp"
39 #include "Architectures.hpp"
41 #include "macho_relocatable_file.h"
44 // #defines are a work around for <rdar://problem/8760268>
45 #define __STDC_LIMIT_MACROS 1
46 #define __STDC_CONSTANT_MACROS 1
47 #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 bool translationUnitSource(const char** dir
, const char** nm
) const
68 virtual const char* name() const { return "import-atom"; }
69 virtual uint64_t size() const { return 0; }
70 virtual uint64_t objectAddress() const { return 0; }
71 virtual void copyRawContent(uint8_t buffer
[]) const { }
72 virtual void setScope(Scope
) { }
73 virtual ld::Fixup::iterator
fixupsBegin() const { return &_undefs
[0]; }
74 virtual ld::Fixup::iterator
fixupsEnd() const { return &_undefs
[_undefs
.size()]; }
76 // for adding references to symbols outside bitcode file
77 void addReference(const char* nm
)
78 { _undefs
.push_back(ld::Fixup(0, ld::Fixup::k1of1
,
79 ld::Fixup::kindNone
, false, nm
)); }
83 mutable std::vector
<ld::Fixup
> _undefs
;
90 class File
: public ld::relocatable::File
93 File(const char* path
, time_t mTime
, const uint8_t* content
,
94 uint32_t contentLength
, cpu_type_t arch
);
97 // overrides of ld::File
98 virtual bool forEachAtom(ld::File::AtomHandler
&) const;
99 virtual bool justInTimeforEachAtom(const char* name
, ld::File::AtomHandler
&) const
101 virtual uint32_t cpuSubType() const { return _cpuSubType
; }
103 // overrides of ld::relocatable::File
104 virtual DebugInfoKind
debugInfo() const { return _debugInfo
; }
105 virtual const char* debugInfoPath() const { return _debugInfoPath
; }
106 virtual time_t debugInfoModificationTime() const
107 { return _debugInfoModTime
; }
108 virtual const std::vector
<ld::relocatable::File::Stab
>* stabs() const { return NULL
; }
109 virtual bool canScatterAtoms() const { return true; }
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 bool translationUnitSource(const char** dir
, const char** nm
) const
154 { return (_compiledAtom
? _compiledAtom
->translationUnitSource(dir
, nm
) : false); }
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
, cpu_type_t architecture
, cpu_subtype_t subarch
, bool logAllFiles
);
202 static bool libLTOisLoaded() { return (::lto_get_version() != NULL
); }
203 static bool optimize( const std::vector
<const ld::Atom
*>& allAtoms
,
205 const OptimizeOptions
& options
,
206 ld::File::AtomHandler
& handler
,
207 std::vector
<const ld::Atom
*>& newAtoms
,
208 std::vector
<const char*>& additionalUndefines
);
210 static const char* ltoVersion() { return ::lto_get_version(); }
213 static const char* tripletPrefixForArch(cpu_type_t arch
);
214 static ld::relocatable::File
* parseMachOFile(const uint8_t* p
, size_t len
, const OptimizeOptions
& options
);
219 bool operator()(const char* left
, const char* right
) const { return (strcmp(left
, right
) == 0); }
221 typedef __gnu_cxx::hash_set
<const char*, __gnu_cxx::hash
<const char*>, CStringEquals
> CStringSet
;
222 typedef __gnu_cxx::hash_map
<const char*, Atom
*, __gnu_cxx::hash
<const char*>, CStringEquals
> CStringToAtom
;
224 class AtomSyncer
: public ld::File::AtomHandler
{
226 AtomSyncer(std::vector
<const char*>& a
, std::vector
<const ld::Atom
*>&na
,
227 CStringToAtom la
, CStringToAtom dla
, const OptimizeOptions
& options
) :
228 _options(options
), _additionalUndefines(a
), _newAtoms(na
), _llvmAtoms(la
), _deadllvmAtoms(dla
) { }
229 virtual void doAtom(const class ld::Atom
&);
230 virtual void doFile(const class ld::File
&) { }
232 const OptimizeOptions
& _options
;
233 std::vector
<const char*>& _additionalUndefines
;
234 std::vector
<const ld::Atom
*>& _newAtoms
;
235 CStringToAtom _llvmAtoms
;
236 CStringToAtom _deadllvmAtoms
;
239 static std::vector
<File
*> _s_files
;
242 std::vector
<File
*> Parser::_s_files
;
245 bool Parser::validFile(const uint8_t* fileContent
, uint64_t fileLength
, cpu_type_t architecture
, cpu_subtype_t subarch
)
247 for (const ArchInfo
* t
=archInfoArray
; t
->archName
!= NULL
; ++t
) {
248 if ( (architecture
== t
->cpuType
) && (!(t
->isSubType
) || (subarch
== t
->cpuSubType
)) ) {
249 bool result
= ::lto_module_is_object_file_in_memory_for_target(fileContent
, fileLength
, t
->llvmTriplePrefix
);
251 // <rdar://problem/8434487> LTO only supports thumbv7 not armv7
252 if ( t
->llvmTriplePrefixAlt
[0] != '\0' ) {
253 result
= ::lto_module_is_object_file_in_memory_for_target(fileContent
, fileLength
, t
->llvmTriplePrefixAlt
);
262 const char* Parser::fileKind(const uint8_t* p
, uint64_t fileLength
)
264 if ( (p
[0] == 0xDE) && (p
[1] == 0xC0) && (p
[2] == 0x17) && (p
[3] == 0x0B) ) {
265 cpu_type_t arch
= LittleEndian::get32(*((uint32_t*)(&p
[16])));
266 for (const ArchInfo
* t
=archInfoArray
; t
->archName
!= NULL
; ++t
) {
267 if ( arch
== t
->cpuType
) {
268 if ( t
->isSubType
) {
269 if ( ::lto_module_is_object_file_in_memory_for_target(p
, fileLength
, t
->llvmTriplePrefix
) )
277 return "unknown bitcode architecture";
282 File
* Parser::parse(const uint8_t* fileContent
, uint64_t fileLength
, const char* path
, time_t modTime
,
283 cpu_type_t architecture
, cpu_subtype_t subarch
, bool logAllFiles
)
285 File
* f
= new File(path
, modTime
, fileContent
, fileLength
, architecture
);
286 _s_files
.push_back(f
);
288 printf("%s\n", path
);
293 ld::relocatable::File
* Parser::parseMachOFile(const uint8_t* p
, size_t len
, const OptimizeOptions
& options
)
295 mach_o::relocatable::ParserOptions objOpts
;
296 objOpts
.architecture
= options
.arch
;
297 objOpts
.objSubtypeMustMatch
= false;
298 objOpts
.logAllFiles
= false;
299 objOpts
.convertUnwindInfo
= true;
302 // mach-o parsing is done in-memory, but need path for debug notes
303 const char* path
= "/tmp/lto.o";
305 if ( options
.tmpObjectFilePath
!= NULL
) {
306 path
= options
.tmpObjectFilePath
;
307 struct stat statBuffer
;
308 if ( stat(options
.tmpObjectFilePath
, &statBuffer
) == 0 )
309 modTime
= statBuffer
.st_mtime
;
312 ld::relocatable::File
* result
= mach_o::relocatable::parse(p
, len
, path
, modTime
, ld::File::Ordinal::LTOOrdinal(), objOpts
);
313 if ( result
!= NULL
)
315 throw "LLVM LTO, file is not of required architecture";
320 File::File(const char* pth
, time_t mTime
, const uint8_t* content
, uint32_t contentLength
, cpu_type_t arch
)
321 : ld::relocatable::File(pth
,mTime
,ld::File::Ordinal::LTOOrdinal()), _architecture(arch
), _internalAtom(*this),
322 _atomArray(NULL
), _atomArrayCount(0), _module(NULL
), _debugInfoPath(pth
),
323 _section("__TEXT_", "__tmp_lto", ld::Section::typeTempLTO
),
324 _fixupToInternal(0, ld::Fixup::k1of1
, ld::Fixup::kindNone
, &_internalAtom
),
325 _debugInfo(ld::relocatable::File::kDebugInfoNone
), _cpuSubType(0)
327 const bool log
= false;
329 // create llvm module
330 _module
= ::lto_module_create_from_memory(content
, contentLength
);
331 if ( _module
== NULL
)
332 throwf("could not parse object file %s: %s", pth
, lto_get_error_message());
334 if ( log
) fprintf(stderr
, "bitcode file: %s\n", pth
);
336 // create atom for each global symbol in module
337 uint32_t count
= ::lto_module_get_num_symbols(_module
);
338 _atomArray
= (Atom
*)malloc(sizeof(Atom
)*count
);
339 for (uint32_t i
=0; i
< count
; ++i
) {
340 const char* name
= ::lto_module_get_symbol_name(_module
, i
);
341 lto_symbol_attributes attr
= lto_module_get_symbol_attribute(_module
, i
);
343 // <rdar://problem/6378110> LTO doesn't like dtrace symbols
344 // ignore dtrace static probes for now
345 // later when codegen is done and a mach-o file is produces the probes will be processed
346 if ( (strncmp(name
, "___dtrace_probe$", 16) == 0) || (strncmp(name
, "___dtrace_isenabled$", 20) == 0) )
349 ld::Atom::Definition def
;
350 ld::Atom::Combine combine
= ld::Atom::combineNever
;
351 switch ( attr
& LTO_SYMBOL_DEFINITION_MASK
) {
352 case LTO_SYMBOL_DEFINITION_REGULAR
:
353 def
= ld::Atom::definitionRegular
;
355 case LTO_SYMBOL_DEFINITION_TENTATIVE
:
356 def
= ld::Atom::definitionTentative
;
358 case LTO_SYMBOL_DEFINITION_WEAK
:
359 def
= ld::Atom::definitionRegular
;
360 combine
= ld::Atom::combineByName
;
362 case LTO_SYMBOL_DEFINITION_UNDEFINED
:
363 case LTO_SYMBOL_DEFINITION_WEAKUNDEF
:
364 def
= ld::Atom::definitionProxy
;
367 throwf("unknown definition kind for symbol %s in bitcode file %s", name
, pth
);
370 // make LLVM atoms for definitions and a reference for undefines
371 if ( def
!= ld::Atom::definitionProxy
) {
372 ld::Atom::Scope scope
;
373 bool autohide
= false;
374 switch ( attr
& LTO_SYMBOL_SCOPE_MASK
) {
375 case LTO_SYMBOL_SCOPE_INTERNAL
:
376 scope
= ld::Atom::scopeTranslationUnit
;
378 case LTO_SYMBOL_SCOPE_HIDDEN
:
379 scope
= ld::Atom::scopeLinkageUnit
;
381 case LTO_SYMBOL_SCOPE_DEFAULT
:
382 scope
= ld::Atom::scopeGlobal
;
384 #if LTO_API_VERSION >= 4
385 case LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN
:
386 scope
= ld::Atom::scopeGlobal
;
391 throwf("unknown scope for symbol %s in bitcode file %s", name
, pth
);
393 // only make atoms for non-internal symbols
394 if ( scope
== ld::Atom::scopeTranslationUnit
)
396 uint8_t alignment
= (attr
& LTO_SYMBOL_ALIGNMENT_MASK
);
397 // make Atom using placement new operator
398 new (&_atomArray
[_atomArrayCount
++]) Atom(*this, name
, scope
, def
, combine
, alignment
, autohide
);
399 if ( scope
!= ld::Atom::scopeTranslationUnit
)
400 _internalAtom
.addReference(name
);
401 if ( log
) fprintf(stderr
, "\t0x%08X %s\n", attr
, name
);
404 // add to list of external references
405 _internalAtom
.addReference(name
);
406 if ( log
) fprintf(stderr
, "\t%s (undefined)\n", name
);
413 if ( _module
!= NULL
)
414 ::lto_module_dispose(_module
);
417 bool File::forEachAtom(ld::File::AtomHandler
& handler
) const
419 handler
.doAtom(_internalAtom
);
420 for(uint32_t i
=0; i
< _atomArrayCount
; ++i
) {
421 handler
.doAtom(_atomArray
[i
]);
426 InternalAtom::InternalAtom(File
& f
)
427 : ld::Atom(f
._section
, ld::Atom::definitionRegular
, ld::Atom::combineNever
, ld::Atom::scopeTranslationUnit
,
428 ld::Atom::typeLTOtemporary
, ld::Atom::symbolTableNotIn
, true, false, false, ld::Atom::Alignment(0)),
433 Atom::Atom(File
& f
, const char* nm
, ld::Atom::Scope s
, ld::Atom::Definition d
, ld::Atom::Combine c
,
434 ld::Atom::Alignment a
, bool ah
)
435 : ld::Atom(f
._section
, d
, c
, s
, ld::Atom::typeLTOtemporary
,
436 ld::Atom::symbolTableIn
, false, false, false, a
),
437 _file(f
), _name(nm
), _compiledAtom(NULL
)
443 void Atom::setCompiledAtom(const ld::Atom
& atom
)
445 // set delegate so virtual methods go to it
446 _compiledAtom
= &atom
;
448 //fprintf(stderr, "setting lto atom %p to delegate to mach-o atom %p (%s)\n", this, &atom, atom.name());
450 // update fields in ld::Atom to match newly constructed mach-o atom
451 (const_cast<Atom
*>(this))->setAttributesFromAtom(atom
);
456 bool Parser::optimize( const std::vector
<const ld::Atom
*>& allAtoms
,
458 const OptimizeOptions
& options
,
459 ld::File::AtomHandler
& handler
,
460 std::vector
<const ld::Atom
*>& newAtoms
,
461 std::vector
<const char*>& additionalUndefines
)
463 const bool logMustPreserve
= false;
464 const bool logExtraOptions
= false;
465 const bool logBitcodeFiles
= false;
466 const bool logAtomsBeforeSync
= false;
468 // exit quickly if nothing to do
469 if ( _s_files
.size() == 0 )
472 // print out LTO version string if -v was used
473 if ( options
.verbose
)
474 fprintf(stderr
, "%s\n", lto_get_version());
476 // create optimizer and add each Reader
477 lto_code_gen_t generator
= ::lto_codegen_create();
478 for (std::vector
<File
*>::iterator it
=_s_files
.begin(); it
!= _s_files
.end(); ++it
) {
479 if ( logBitcodeFiles
) fprintf(stderr
, "lto_codegen_add_module(%s)\n", (*it
)->path());
480 if ( ::lto_codegen_add_module(generator
, (*it
)->module()) )
481 throwf("lto: could not merge in %s because %s", (*it
)->path(), ::lto_get_error_message());
484 // add any -mllvm command line options
485 for (std::vector
<const char*>::const_iterator it
=options
.llvmOptions
->begin(); it
!= options
.llvmOptions
->end(); ++it
) {
486 if ( logExtraOptions
) fprintf(stderr
, "passing option to llvm: %s\n", *it
);
487 ::lto_codegen_debug_options(generator
, *it
);
490 // The atom graph uses directed edges (references). Collect all references where
491 // originating atom is not part of any LTO Reader. This allows optimizer to optimize an
492 // external (i.e. not originated from same .o file) reference if all originating atoms are also
493 // defined in llvm bitcode file.
494 CStringSet nonLLVMRefs
;
495 CStringToAtom llvmAtoms
;
496 bool hasNonllvmAtoms
= false;
497 for (std::vector
<const ld::Atom
*>::const_iterator it
= allAtoms
.begin(); it
!= allAtoms
.end(); ++it
) {
498 const ld::Atom
* atom
= *it
;
499 // only look at references that come from an atom that is not an llvm atom
500 if ( atom
->contentType() != ld::Atom::typeLTOtemporary
) {
501 if ( (atom
->section().type() != ld::Section::typeMachHeader
) && (atom
->definition() != ld::Atom::definitionProxy
) ) {
502 hasNonllvmAtoms
= true;
504 const ld::Atom
* target
;
505 for (ld::Fixup::iterator fit
=atom
->fixupsBegin(); fit
!= atom
->fixupsEnd(); ++fit
) {
506 switch ( fit
->binding
) {
507 case ld::Fixup::bindingDirectlyBound
:
508 // that reference an llvm atom
509 if ( fit
->u
.target
->contentType() == ld::Atom::typeLTOtemporary
)
510 nonLLVMRefs
.insert(fit
->u
.target
->name());
512 case ld::Fixup::bindingsIndirectlyBound
:
513 target
= state
.indirectBindingTable
[fit
->u
.bindingIndex
];
514 if ( target
== NULL
)
515 throwf("'%s' in %s contains undefined reference", atom
->name(), atom
->file()->path());
516 assert(target
!= NULL
);
517 if ( target
->contentType() == ld::Atom::typeLTOtemporary
)
518 nonLLVMRefs
.insert(target
->name());
525 llvmAtoms
[atom
->name()] = (Atom
*)atom
;
528 // if entry point is in a llvm bitcode file, it must be preserved by LTO
529 if ( state
.entryPoint
!= NULL
) {
530 if ( state
.entryPoint
->contentType() == ld::Atom::typeLTOtemporary
)
531 nonLLVMRefs
.insert(state
.entryPoint
->name());
534 // deadAtoms are the atoms that the linker coalesced. For instance weak or tentative definitions
535 // overriden by another atom. If any of these deadAtoms are llvm atoms and they were replaced
536 // with a mach-o atom, we need to tell the lto engine to preserve (not optimize away) its dead
537 // atom so that the linker can replace it with the mach-o one later.
538 CStringToAtom deadllvmAtoms
;
539 for (std::vector
<const ld::Atom
*>::const_iterator it
= allAtoms
.begin(); it
!= allAtoms
.end(); ++it
) {
540 const ld::Atom
* atom
= *it
;
541 if ( atom
->coalescedAway() && (atom
->contentType() == ld::Atom::typeLTOtemporary
) ) {
542 const char* name
= atom
->name();
543 if ( logMustPreserve
) fprintf(stderr
, "lto_codegen_add_must_preserve_symbol(%s) because linker coalesce away and replace with a mach-o atom\n", name
);
544 ::lto_codegen_add_must_preserve_symbol(generator
, name
);
545 deadllvmAtoms
[name
] = (Atom
*)atom
;
548 for (std::vector
<File
*>::iterator it
=_s_files
.begin(); it
!= _s_files
.end(); ++it
) {
550 for(uint32_t i
=0; i
< file
->_atomArrayCount
; ++i
) {
551 Atom
* llvmAtom
= &file
->_atomArray
[i
];
552 if ( llvmAtom
->coalescedAway() ) {
553 const char* name
= llvmAtom
->name();
554 if ( deadllvmAtoms
.find(name
) == deadllvmAtoms
.end() ) {
555 if ( logMustPreserve
)
556 fprintf(stderr
, "lto_codegen_add_must_preserve_symbol(%s) because linker coalesce away and replace with a mach-o atom\n", name
);
557 ::lto_codegen_add_must_preserve_symbol(generator
, name
);
558 deadllvmAtoms
[name
] = (Atom
*)llvmAtom
;
561 else if ( options
.linkerDeadStripping
&& !llvmAtom
->live() ) {
562 const char* name
= llvmAtom
->name();
563 deadllvmAtoms
[name
] = (Atom
*)llvmAtom
;
568 // tell code generator about symbols that must be preserved
569 for (CStringToAtom::iterator it
= llvmAtoms
.begin(); it
!= llvmAtoms
.end(); ++it
) {
570 const char* name
= it
->first
;
571 Atom
* atom
= it
->second
;
572 // Include llvm Symbol in export list if it meets one of following two conditions
573 // 1 - atom scope is global (and not linkage unit).
574 // 2 - included in nonLLVMRefs set.
575 // If a symbol is not listed in exportList then LTO is free to optimize it away.
576 if ( (atom
->scope() == ld::Atom::scopeGlobal
) && options
.preserveAllGlobals
) {
577 if ( logMustPreserve
) fprintf(stderr
, "lto_codegen_add_must_preserve_symbol(%s) because global symbol\n", name
);
578 ::lto_codegen_add_must_preserve_symbol(generator
, name
);
580 else if ( nonLLVMRefs
.find(name
) != nonLLVMRefs
.end() ) {
581 if ( logMustPreserve
) fprintf(stderr
, "lto_codegen_add_must_preserve_symbol(%s) because referenced by a mach-o atom\n", name
);
582 ::lto_codegen_add_must_preserve_symbol(generator
, name
);
586 // special case running ld -r on all bitcode files to produce another bitcode file (instead of mach-o)
587 if ( options
.relocatable
&& !hasNonllvmAtoms
) {
588 if ( ! ::lto_codegen_write_merged_modules(generator
, options
.outputFilePath
) ) {
589 // HACK, no good way to tell linker we are all done, so just quit
592 warning("could not produce merged bitcode file");
595 // set code-gen model
596 lto_codegen_model model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC
;
597 if ( options
.mainExecutable
) {
598 if ( options
.staticExecutable
) {
599 // darwin x86_64 "static" code model is really dynamic code model
600 if ( options
.arch
== CPU_TYPE_X86_64
)
601 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC
;
603 model
= LTO_CODEGEN_PIC_MODEL_STATIC
;
607 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC
;
609 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC
;
613 if ( options
.allowTextRelocs
)
614 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC
;
616 model
= LTO_CODEGEN_PIC_MODEL_DYNAMIC
;
618 if ( ::lto_codegen_set_pic_model(generator
, model
) )
619 throwf("could not create set codegen model: %s", lto_get_error_message());
621 // if requested, save off merged bitcode file
622 if ( options
.saveTemps
) {
623 char tempBitcodePath
[MAXPATHLEN
];
624 strcpy(tempBitcodePath
, options
.outputFilePath
);
625 strcat(tempBitcodePath
, ".lto.bc");
626 ::lto_codegen_write_merged_modules(generator
, tempBitcodePath
);
629 #if LTO_API_VERSION >= 3
630 // find assembler next to linker
632 uint32_t bufSize
= PATH_MAX
;
633 if ( _NSGetExecutablePath(path
, &bufSize
) != -1 ) {
634 char* lastSlash
= strrchr(path
, '/');
635 if ( lastSlash
!= NULL
) {
636 strcpy(lastSlash
+1, "as");
637 struct stat statInfo
;
638 if ( stat(path
, &statInfo
) == 0 )
639 ::lto_codegen_set_assembler_path(generator
, path
);
643 // run code generator
645 const uint8_t* machOFile
= (uint8_t*)::lto_codegen_compile(generator
, &machOFileLen
);
646 if ( machOFile
== NULL
)
647 throwf("could not do LTO codegen: %s", ::lto_get_error_message());
649 // if requested, save off temp mach-o file
650 if ( options
.saveTemps
) {
651 char tempMachoPath
[MAXPATHLEN
];
652 strcpy(tempMachoPath
, options
.outputFilePath
);
653 strcat(tempMachoPath
, ".lto.o");
654 int fd
= ::open(tempMachoPath
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
656 ::write(fd
, machOFile
, machOFileLen
);
659 // save off merged bitcode file
660 char tempOptBitcodePath
[MAXPATHLEN
];
661 strcpy(tempOptBitcodePath
, options
.outputFilePath
);
662 strcat(tempOptBitcodePath
, ".lto.opt.bc");
663 ::lto_codegen_write_merged_modules(generator
, tempOptBitcodePath
);
666 // if needed, save temp mach-o file to specific location
667 if ( options
.tmpObjectFilePath
!= NULL
) {
668 int fd
= ::open(options
.tmpObjectFilePath
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
670 ::write(fd
, machOFile
, machOFileLen
);
674 warning("could not write LTO temp file '%s', errno=%d", options
.tmpObjectFilePath
, errno
);
678 // parse generated mach-o file into a MachOReader
679 ld::relocatable::File
* machoFile
= parseMachOFile(machOFile
, machOFileLen
, options
);
681 // sync generated mach-o atoms with existing atoms ld knows about
682 if ( logAtomsBeforeSync
) {
683 fprintf(stderr
, "llvmAtoms:\n");
684 for (CStringToAtom::iterator it
= llvmAtoms
.begin(); it
!= llvmAtoms
.end(); ++it
) {
685 const char* name
= it
->first
;
686 //Atom* atom = it->second;
687 fprintf(stderr
, "\t%s\n", name
);
689 fprintf(stderr
, "deadllvmAtoms:\n");
690 for (CStringToAtom::iterator it
= deadllvmAtoms
.begin(); it
!= deadllvmAtoms
.end(); ++it
) {
691 const char* name
= it
->first
;
692 //Atom* atom = it->second;
693 fprintf(stderr
, "\t%s\n", name
);
696 AtomSyncer
syncer(additionalUndefines
, newAtoms
, llvmAtoms
, deadllvmAtoms
, options
);
697 machoFile
->forEachAtom(syncer
);
699 // Remove InternalAtoms from ld
700 for (std::vector
<File
*>::iterator it
=_s_files
.begin(); it
!= _s_files
.end(); ++it
) {
701 (*it
)->internalAtom().setCoalescedAway();
703 // Remove Atoms from ld if code generator optimized them away
704 for (CStringToAtom::iterator li
= llvmAtoms
.begin(), le
= llvmAtoms
.end(); li
!= le
; ++li
) {
705 // check if setRealAtom() called on this Atom
706 if ( li
->second
->compiledAtom() == NULL
) {
707 //fprintf(stderr, "llvm optimized away %p %s\n", li->second, li->second->name());
708 li
->second
->setCoalescedAway();
712 // notify about file level attributes
713 handler
.doFile(*machoFile
);
715 // if final mach-o file has debug info, update original bitcode files to match
716 for (std::vector
<File
*>::iterator it
=_s_files
.begin(); it
!= _s_files
.end(); ++it
) {
717 (*it
)->setDebugInfo(machoFile
->debugInfo(), machoFile
->path(),
718 machoFile
->modificationTime(), machoFile
->cpuSubType());
725 void Parser::AtomSyncer::doAtom(const ld::Atom
& machoAtom
)
727 // update proxy atoms to point to real atoms and find new atoms
728 const char* name
= machoAtom
.name();
729 if ( machoAtom
.scope() >= ld::Atom::scopeLinkageUnit
) {
730 CStringToAtom::iterator pos
= _llvmAtoms
.find(name
);
731 if ( pos
!= _llvmAtoms
.end() ) {
732 // turn Atom into a proxy for this mach-o atom
733 pos
->second
->setCompiledAtom(machoAtom
);
736 // an atom of this name was not in the allAtoms list the linker gave us
737 if ( _deadllvmAtoms
.find(name
) != _deadllvmAtoms
.end() ) {
738 // this corresponding to an atom that the linker coalesced away or marked not-live
739 if ( _options
.linkerDeadStripping
) {
740 // llvm seems to want this atom and -dead_strip is enabled, so it will be deleted if not needed, so add back
741 Atom
* llvmAtom
= _deadllvmAtoms
[name
];
742 llvmAtom
->setCompiledAtom(machoAtom
);
743 _newAtoms
.push_back(&machoAtom
);
746 // Don't pass it back as a new atom
751 // this is something new that lto conjured up, tell ld its new
752 _newAtoms
.push_back(&machoAtom
);
757 // ld only knew about non-static atoms, so this one must be new
758 _newAtoms
.push_back(&machoAtom
);
761 // adjust fixups to go through proxy atoms
762 //fprintf(stderr, "adjusting fixups in atom: %s\n", machoAtom.name());
763 for (ld::Fixup::iterator fit
=machoAtom
.fixupsBegin(); fit
!= machoAtom
.fixupsEnd(); ++fit
) {
764 switch ( fit
->binding
) {
765 case ld::Fixup::bindingNone
:
767 case ld::Fixup::bindingByNameUnbound
:
768 // don't know if this target has been seen by linker before or if it is new
769 // be conservative and tell linker it is new
770 _additionalUndefines
.push_back(fit
->u
.name
);
771 //fprintf(stderr, " by name ref to: %s\n", fit->u.name);
773 case ld::Fixup::bindingDirectlyBound
:
774 // If mach-o atom is referencing another mach-o atom then
775 // reference is not going through Atom proxy. Fix it here to ensure that all
776 // llvm symbol references always go through Atom proxy.
777 if ( fit
->u
.target
->scope() != ld::Atom::scopeTranslationUnit
) {
778 const char* targetName
= fit
->u
.target
->name();
779 CStringToAtom::iterator pos
= _llvmAtoms
.find(targetName
);
780 if ( pos
!= _llvmAtoms
.end() ) {
781 fit
->u
.target
= pos
->second
;
784 if ( _deadllvmAtoms
.find(targetName
) != _deadllvmAtoms
.end() ) {
785 // target was coalesed away and replace by mach-o atom from a non llvm .o file
786 fit
->binding
= ld::Fixup::bindingByNameUnbound
;
787 fit
->u
.name
= targetName
;
791 //fprintf(stderr, " direct ref to: %s (scope=%d)\n", fit->u.target->name(), fit->u.target->scope());
793 case ld::Fixup::bindingByContentBound
:
794 //fprintf(stderr, " direct by content to: %s\n", fit->u.target->name());
796 case ld::Fixup::bindingsIndirectlyBound
:
797 assert(0 && "indirect binding found in initial mach-o file?");
798 //fprintf(stderr, " indirect by content to: %u\n", fit->u.bindingIndex);
806 static pthread_mutex_t lto_lock
;
808 Mutex() { pthread_mutex_lock(<o_lock
); }
809 ~Mutex() { pthread_mutex_unlock(<o_lock
); }
811 pthread_mutex_t
Mutex::lto_lock
= PTHREAD_MUTEX_INITIALIZER
;
814 // Used by archive reader to see if member is an llvm bitcode file
816 bool isObjectFile(const uint8_t* fileContent
, uint64_t fileLength
, cpu_type_t architecture
, cpu_subtype_t subarch
)
819 return Parser::validFile(fileContent
, fileLength
, architecture
, subarch
);
824 // main function used by linker to instantiate ld::Files
826 ld::relocatable::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
827 const char* path
, time_t modTime
,
828 cpu_type_t architecture
, cpu_subtype_t subarch
, bool logAllFiles
)
831 if ( Parser::validFile(fileContent
, fileLength
, architecture
, subarch
) )
832 return Parser::parse(fileContent
, fileLength
, path
, modTime
, architecture
, subarch
, logAllFiles
);
838 // used by "ld -v" to report version of libLTO.dylib being used
840 const char* version()
843 return ::lto_get_version();
848 // used by ld for error reporting
850 bool libLTOisLoaded()
853 return (::lto_get_version() != NULL
);
857 // used by ld for error reporting
859 const char* archName(const uint8_t* fileContent
, uint64_t fileLength
)
862 return Parser::fileKind(fileContent
, fileLength
);
866 // used by ld for doing link time optimization
868 bool optimize( const std::vector
<const ld::Atom
*>& allAtoms
,
870 const OptimizeOptions
& options
,
871 ld::File::AtomHandler
& handler
,
872 std::vector
<const ld::Atom
*>& newAtoms
,
873 std::vector
<const char*>& additionalUndefines
)
876 return Parser::optimize(allAtoms
, state
, options
, handler
, newAtoms
, additionalUndefines
);