]> git.saurik.com Git - apple/ld64.git/blob - src/ld/parsers/lto_file.cpp
023f6e303e2962fcb2ff7d24042b676e3a08891f
[apple/ld64.git] / src / ld / parsers / lto_file.cpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2006-2010 Apple Inc. All rights reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
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
12 * file.
13 *
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.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25 #ifndef __LTO_READER_H__
26 #define __LTO_READER_H__
27
28 #include <stdlib.h>
29 #include <sys/param.h>
30 #include <sys/fcntl.h>
31 #include <sys/stat.h>
32 #include <errno.h>
33 #include <pthread.h>
34 #include <mach-o/dyld.h>
35 #include <vector>
36 #include <unordered_set>
37 #include <unordered_map>
38
39 #include "MachOFileAbstraction.hpp"
40 #include "Architectures.hpp"
41 #include "ld.hpp"
42 #include "macho_relocatable_file.h"
43 #include "lto_file.h"
44
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"
49
50
51 namespace lto {
52
53
54 //
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.
60 //
61 class InternalAtom : public ld::Atom
62 {
63 public:
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()]; }
74
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)); }
79 private:
80
81 ld::File& _file;
82 mutable std::vector<ld::Fixup> _undefs;
83 };
84
85
86 //
87 // LLVM bitcode file
88 //
89 class File : public ld::relocatable::File
90 {
91 public:
92 File(const char* path, time_t mTime, ld::File::Ordinal ordinal,
93 const uint8_t* content, uint32_t contentLength, cpu_type_t arch);
94 virtual ~File();
95
96 // overrides of ld::File
97 virtual bool forEachAtom(ld::File::AtomHandler&) const;
98 virtual bool justInTimeforEachAtom(const char* name, ld::File::AtomHandler&) const
99 { return false; }
100 virtual uint32_t cpuSubType() const { return _cpuSubType; }
101
102 // overrides of ld::relocatable::File
103 virtual DebugInfoKind debugInfo() const { return _debugInfo; }
104 virtual const char* debugInfoPath() const { return _debugInfoPath; }
105 virtual time_t debugInfoModificationTime() const
106 { return _debugInfoModTime; }
107 virtual const std::vector<ld::relocatable::File::Stab>* stabs() const { return NULL; }
108 virtual bool canScatterAtoms() const { return true; }
109
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)
114 { _debugInfo = k;
115 _debugInfoPath = pth;
116 _debugInfoModTime = modTime;
117 _cpuSubType = subtype;}
118
119 private:
120 friend class Atom;
121 friend class InternalAtom;
122 friend class Parser;
123
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;
135 };
136
137 //
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.
143 //
144 class Atom : public ld::Atom
145 {
146 public:
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);
149
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); }
177
178 const ld::Atom* compiledAtom() { return _compiledAtom; }
179 void setCompiledAtom(const ld::Atom& atom);
180
181 private:
182
183 File& _file;
184 const char* _name;
185 const ld::Atom* _compiledAtom;
186 };
187
188
189
190
191
192
193
194 class Parser
195 {
196 public:
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,
203 ld::Internal& state,
204 const OptimizeOptions& options,
205 ld::File::AtomHandler& handler,
206 std::vector<const ld::Atom*>& newAtoms,
207 std::vector<const char*>& additionalUndefines);
208
209 static const char* ltoVersion() { return ::lto_get_version(); }
210
211 private:
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);
214
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;
217
218 class AtomSyncer : public ld::File::AtomHandler {
219 public:
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&) { }
225
226 const OptimizeOptions& _options;
227 std::vector<const char*>& _additionalUndefines;
228 std::vector<const ld::Atom*>& _newAtoms;
229 CStringToAtom _llvmAtoms;
230 CStringToAtom _deadllvmAtoms;
231 };
232
233 static std::vector<File*> _s_files;
234 };
235
236 std::vector<File*> Parser::_s_files;
237
238
239 bool Parser::validFile(const uint8_t* fileContent, uint64_t fileLength, cpu_type_t architecture, cpu_subtype_t subarch)
240 {
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);
244 if ( !result ) {
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);
248 }
249 }
250 return result;
251 }
252 }
253 return false;
254 }
255
256 const char* Parser::fileKind(const uint8_t* p, uint64_t fileLength)
257 {
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) )
264 return t->archName;
265 }
266 else {
267 return t->archName;
268 }
269 }
270 }
271 return "unknown bitcode architecture";
272 }
273 return NULL;
274 }
275
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)
278 {
279 File* f = new File(path, modTime, ordinal, fileContent, fileLength, architecture);
280 _s_files.push_back(f);
281 if ( logAllFiles )
282 printf("%s\n", path);
283 return f;
284 }
285
286
287 ld::relocatable::File* Parser::parseMachOFile(const uint8_t* p, size_t len, const OptimizeOptions& options)
288 {
289 mach_o::relocatable::ParserOptions objOpts;
290 objOpts.architecture = options.arch;
291 objOpts.objSubtypeMustMatch = false;
292 objOpts.logAllFiles = false;
293 objOpts.convertUnwindInfo = true;
294 objOpts.subType = 0;
295
296 // mach-o parsing is done in-memory, but need path for debug notes
297 const char* path = "/tmp/lto.o";
298 time_t modTime = 0;
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;
304 }
305
306 ld::relocatable::File* result = mach_o::relocatable::parse(p, len, path, modTime, ld::File::Ordinal::LTOOrdinal(), objOpts);
307 if ( result != NULL )
308 return result;
309 throw "LLVM LTO, file is not of required architecture";
310 }
311
312
313
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)
320 {
321 const bool log = false;
322
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());
327
328 if ( log ) fprintf(stderr, "bitcode file: %s\n", pth);
329
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);
336
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) )
341 continue;
342
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;
348 break;
349 case LTO_SYMBOL_DEFINITION_TENTATIVE:
350 def = ld::Atom::definitionTentative;
351 break;
352 case LTO_SYMBOL_DEFINITION_WEAK:
353 def = ld::Atom::definitionRegular;
354 combine = ld::Atom::combineByName;
355 break;
356 case LTO_SYMBOL_DEFINITION_UNDEFINED:
357 case LTO_SYMBOL_DEFINITION_WEAKUNDEF:
358 def = ld::Atom::definitionProxy;
359 break;
360 default:
361 throwf("unknown definition kind for symbol %s in bitcode file %s", name, pth);
362 }
363
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;
371 break;
372 case LTO_SYMBOL_SCOPE_HIDDEN:
373 scope = ld::Atom::scopeLinkageUnit;
374 break;
375 case LTO_SYMBOL_SCOPE_DEFAULT:
376 scope = ld::Atom::scopeGlobal;
377 break;
378 #if LTO_API_VERSION >= 4
379 case LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN:
380 scope = ld::Atom::scopeGlobal;
381 autohide = true;
382 break;
383 #endif
384 default:
385 throwf("unknown scope for symbol %s in bitcode file %s", name, pth);
386 }
387 // only make atoms for non-internal symbols
388 if ( scope == ld::Atom::scopeTranslationUnit )
389 continue;
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);
396 }
397 else {
398 // add to list of external references
399 _internalAtom.addReference(name);
400 if ( log ) fprintf(stderr, "\t%s (undefined)\n", name);
401 }
402 }
403 }
404
405 File::~File()
406 {
407 if ( _module != NULL )
408 ::lto_module_dispose(_module);
409 }
410
411 bool File::forEachAtom(ld::File::AtomHandler& handler) const
412 {
413 handler.doAtom(_internalAtom);
414 for(uint32_t i=0; i < _atomArrayCount; ++i) {
415 handler.doAtom(_atomArray[i]);
416 }
417 return true;
418 }
419
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)),
423 _file(f)
424 {
425 }
426
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)
432 {
433 if ( ah )
434 this->setAutoHide();
435 }
436
437 void Atom::setCompiledAtom(const ld::Atom& atom)
438 {
439 // set delegate so virtual methods go to it
440 _compiledAtom = &atom;
441
442 //fprintf(stderr, "setting lto atom %p to delegate to mach-o atom %p (%s)\n", this, &atom, atom.name());
443
444 // update fields in ld::Atom to match newly constructed mach-o atom
445 (const_cast<Atom*>(this))->setAttributesFromAtom(atom);
446 }
447
448
449
450 bool Parser::optimize( const std::vector<const ld::Atom*>& allAtoms,
451 ld::Internal& state,
452 const OptimizeOptions& options,
453 ld::File::AtomHandler& handler,
454 std::vector<const ld::Atom*>& newAtoms,
455 std::vector<const char*>& additionalUndefines)
456 {
457 const bool logMustPreserve = false;
458 const bool logExtraOptions = false;
459 const bool logBitcodeFiles = false;
460 const bool logAtomsBeforeSync = false;
461
462 // exit quickly if nothing to do
463 if ( _s_files.size() == 0 )
464 return false;
465
466 // print out LTO version string if -v was used
467 if ( options.verbose )
468 fprintf(stderr, "%s\n", ::lto_get_version());
469
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());
476 }
477
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);
482 }
483
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;
497 }
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());
505 break;
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());
513 default:
514 break;
515 }
516 }
517 }
518 else {
519 llvmAtoms[atom->name()] = (Atom*)atom;
520 }
521 }
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());
526 }
527
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;
540 }
541 }
542 for (std::vector<File*>::iterator it=_s_files.begin(); it != _s_files.end(); ++it) {
543 File* file = *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;
553 }
554 }
555 else if ( options.linkerDeadStripping && !llvmAtom->live() ) {
556 const char* name = llvmAtom->name();
557 deadllvmAtoms[name] = (Atom*)llvmAtom;
558 }
559 }
560 }
561
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);
573 }
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);
577 }
578 }
579
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
584 exit(0);
585 }
586 warning("could not produce merged bitcode file");
587 }
588
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;
596 else
597 model = LTO_CODEGEN_PIC_MODEL_STATIC;
598 }
599 else {
600 if ( options.pie )
601 model = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
602 else
603 model = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC;
604 }
605 }
606 else {
607 if ( options.allowTextRelocs )
608 model = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC;
609 else
610 model = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
611 }
612 if ( ::lto_codegen_set_pic_model(generator, model) )
613 throwf("could not create set codegen model: %s", lto_get_error_message());
614
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);
621 }
622
623 #if LTO_API_VERSION >= 3
624 // find assembler next to linker
625 char path[PATH_MAX];
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);
634 }
635 }
636 #endif
637 // run code generator
638 size_t machOFileLen;
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());
642
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);
649 if ( fd != -1) {
650 ::write(fd, machOFile, machOFileLen);
651 ::close(fd);
652 }
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);
658 }
659
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);
663 if ( fd != -1) {
664 ::write(fd, machOFile, machOFileLen);
665 ::close(fd);
666 }
667 else {
668 warning("could not write LTO temp file '%s', errno=%d", options.tmpObjectFilePath, errno);
669 }
670 }
671
672 // parse generated mach-o file into a MachOReader
673 ld::relocatable::File* machoFile = parseMachOFile(machOFile, machOFileLen, options);
674
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);
682 }
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);
688 }
689 }
690 AtomSyncer syncer(additionalUndefines, newAtoms, llvmAtoms, deadllvmAtoms, options);
691 machoFile->forEachAtom(syncer);
692
693 // Remove InternalAtoms from ld
694 for (std::vector<File*>::iterator it=_s_files.begin(); it != _s_files.end(); ++it) {
695 (*it)->internalAtom().setCoalescedAway();
696 }
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();
703 }
704 }
705
706 // notify about file level attributes
707 handler.doFile(*machoFile);
708
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());
713 }
714
715 return true;
716 }
717
718
719 void Parser::AtomSyncer::doAtom(const ld::Atom& machoAtom)
720 {
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);
728 }
729 else {
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);
738 }
739 else {
740 // Don't pass it back as a new atom
741 }
742 }
743 else
744 {
745 // this is something new that lto conjured up, tell ld its new
746 _newAtoms.push_back(&machoAtom);
747 }
748 }
749 }
750 else {
751 // ld only knew about non-static atoms, so this one must be new
752 _newAtoms.push_back(&machoAtom);
753 }
754
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:
760 break;
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);
766 break;
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;
776 }
777 else {
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;
782 }
783 }
784 }
785 //fprintf(stderr, " direct ref to: %s (scope=%d)\n", fit->u.target->name(), fit->u.target->scope());
786 break;
787 case ld::Fixup::bindingByContentBound:
788 //fprintf(stderr, " direct by content to: %s\n", fit->u.target->name());
789 break;
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);
793 break;
794 }
795 }
796
797 }
798
799 class Mutex {
800 static pthread_mutex_t lto_lock;
801 public:
802 Mutex() { pthread_mutex_lock(&lto_lock); }
803 ~Mutex() { pthread_mutex_unlock(&lto_lock); }
804 };
805 pthread_mutex_t Mutex::lto_lock = PTHREAD_MUTEX_INITIALIZER;
806
807 //
808 // Used by archive reader to see if member is an llvm bitcode file
809 //
810 bool isObjectFile(const uint8_t* fileContent, uint64_t fileLength, cpu_type_t architecture, cpu_subtype_t subarch)
811 {
812 Mutex lock;
813 return Parser::validFile(fileContent, fileLength, architecture, subarch);
814 }
815
816
817 //
818 // main function used by linker to instantiate ld::Files
819 //
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)
823 {
824 Mutex lock;
825 if ( Parser::validFile(fileContent, fileLength, architecture, subarch) )
826 return Parser::parse(fileContent, fileLength, path, modTime, ordinal, architecture, subarch, logAllFiles);
827 else
828 return NULL;
829 }
830
831 //
832 // used by "ld -v" to report version of libLTO.dylib being used
833 //
834 const char* version()
835 {
836 Mutex lock;
837 return ::lto_get_version();
838 }
839
840
841 //
842 // used by ld for error reporting
843 //
844 bool libLTOisLoaded()
845 {
846 Mutex lock;
847 return (::lto_get_version() != NULL);
848 }
849
850 //
851 // used by ld for error reporting
852 //
853 const char* archName(const uint8_t* fileContent, uint64_t fileLength)
854 {
855 Mutex lock;
856 return Parser::fileKind(fileContent, fileLength);
857 }
858
859 //
860 // used by ld for doing link time optimization
861 //
862 bool optimize( const std::vector<const ld::Atom*>& allAtoms,
863 ld::Internal& state,
864 const OptimizeOptions& options,
865 ld::File::AtomHandler& handler,
866 std::vector<const ld::Atom*>& newAtoms,
867 std::vector<const char*>& additionalUndefines)
868 {
869 Mutex lock;
870 return Parser::optimize(allAtoms, state, options, handler, newAtoms, additionalUndefines);
871 }
872
873
874
875 }; // namespace lto
876
877
878 #endif
879