]> git.saurik.com Git - apple/ld64.git/blob - src/ld/parsers/lto_file.cpp
c9c4ed04acc6e23e543a52d0e3b0fa61f68bce91
[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 <mach-o/dyld.h>
34 #include <vector>
35 #include <ext/hash_set>
36 #include <ext/hash_map>
37
38 #include "MachOFileAbstraction.hpp"
39 #include "Architectures.hpp"
40 #include "ld.hpp"
41 #include "macho_relocatable_file.h"
42 #include "lto_file.h"
43
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"
48
49
50 namespace lto {
51
52
53 //
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.
59 //
60 class InternalAtom : public ld::Atom
61 {
62 public:
63 InternalAtom(class File& f);
64 // overrides of ld::Atom
65 virtual ld::File* file() const { return &_file; }
66 virtual const char* name() const { return "import-atom"; }
67 virtual uint64_t size() const { return 0; }
68 virtual uint64_t objectAddress() const { return 0; }
69 virtual void copyRawContent(uint8_t buffer[]) const { }
70 virtual void setScope(Scope) { }
71 virtual ld::Fixup::iterator fixupsBegin() const { return &_undefs[0]; }
72 virtual ld::Fixup::iterator fixupsEnd() const { return &_undefs[_undefs.size()]; }
73
74 // for adding references to symbols outside bitcode file
75 void addReference(const char* nm)
76 { _undefs.push_back(ld::Fixup(0, ld::Fixup::k1of1,
77 ld::Fixup::kindNone, false, nm)); }
78 private:
79
80 ld::File& _file;
81 mutable std::vector<ld::Fixup> _undefs;
82 };
83
84
85 //
86 // LLVM bitcode file
87 //
88 class File : public ld::relocatable::File
89 {
90 public:
91 File(const char* path, time_t mTime, ld::File::Ordinal ordinal,
92 const uint8_t* content, uint32_t contentLength, cpu_type_t arch);
93 virtual ~File();
94
95 // overrides of ld::File
96 virtual bool forEachAtom(ld::File::AtomHandler&) const;
97 virtual bool justInTimeforEachAtom(const char* name, ld::File::AtomHandler&) const
98 { return false; }
99 virtual uint32_t cpuSubType() const { return _cpuSubType; }
100
101 // overrides of ld::relocatable::File
102 virtual DebugInfoKind debugInfo() const { return _debugInfo; }
103 virtual const char* debugInfoPath() const { return _debugInfoPath; }
104 virtual time_t debugInfoModificationTime() const
105 { return _debugInfoModTime; }
106 virtual const std::vector<ld::relocatable::File::Stab>* stabs() const { return NULL; }
107 virtual bool canScatterAtoms() const { return true; }
108
109 lto_module_t module() { return _module; }
110 class InternalAtom& internalAtom() { return _internalAtom; }
111 void setDebugInfo(ld::relocatable::File::DebugInfoKind k,
112 const char* pth, time_t modTime, uint32_t subtype)
113 { _debugInfo = k;
114 _debugInfoPath = pth;
115 _debugInfoModTime = modTime;
116 _cpuSubType = subtype;}
117
118 private:
119 friend class Atom;
120 friend class InternalAtom;
121 friend class Parser;
122
123 cpu_type_t _architecture;
124 class InternalAtom _internalAtom;
125 class Atom* _atomArray;
126 uint32_t _atomArrayCount;
127 lto_module_t _module;
128 const char* _debugInfoPath;
129 time_t _debugInfoModTime;
130 ld::Section _section;
131 ld::Fixup _fixupToInternal;
132 ld::relocatable::File::DebugInfoKind _debugInfo;
133 uint32_t _cpuSubType;
134 };
135
136 //
137 // Atom acts as a proxy Atom for the symbols that are exported by LLVM bitcode file. Initially,
138 // Reader creates Atoms to allow linker proceed with usual symbol resolution phase. After
139 // optimization is performed, real Atoms are created for these symobls. However these real Atoms
140 // are not inserted into global symbol table. Atom holds real Atom and forwards appropriate
141 // methods to real atom.
142 //
143 class Atom : public ld::Atom
144 {
145 public:
146 Atom(File& f, const char* name, ld::Atom::Scope s,
147 ld::Atom::Definition d, ld::Atom::Combine c, ld::Atom::Alignment a, bool ah);
148
149 // overrides of ld::Atom
150 virtual ld::File* file() const { return &_file; }
151 virtual const char* translationUnitSource() const
152 { return (_compiledAtom ? _compiledAtom->translationUnitSource() : NULL); }
153 virtual const char* name() const { return _name; }
154 virtual uint64_t size() const { return (_compiledAtom ? _compiledAtom->size() : 0); }
155 virtual uint64_t objectAddress() const { return (_compiledAtom ? _compiledAtom->objectAddress() : 0); }
156 virtual void copyRawContent(uint8_t buffer[]) const
157 { if (_compiledAtom) _compiledAtom->copyRawContent(buffer); }
158 virtual const uint8_t* rawContentPointer() const
159 { return (_compiledAtom ? _compiledAtom->rawContentPointer() : NULL); }
160 virtual unsigned long contentHash(const class ld::IndirectBindingTable& ibt) const
161 { return (_compiledAtom ? _compiledAtom->contentHash(ibt) : 0); }
162 virtual bool canCoalesceWith(const ld::Atom& rhs, const class ld::IndirectBindingTable& ibt) const
163 { return (_compiledAtom ? _compiledAtom->canCoalesceWith(rhs,ibt) : false); }
164 virtual ld::Fixup::iterator fixupsBegin() const
165 { return (_compiledAtom ? _compiledAtom->fixupsBegin() : (ld::Fixup*)&_file._fixupToInternal); }
166 virtual ld::Fixup::iterator fixupsEnd() const
167 { return (_compiledAtom ? _compiledAtom->fixupsEnd() : &((ld::Fixup*)&_file._fixupToInternal)[1]); }
168 virtual ld::Atom::UnwindInfo::iterator beginUnwind() const
169 { return (_compiledAtom ? _compiledAtom->beginUnwind() : NULL); }
170 virtual ld::Atom::UnwindInfo::iterator endUnwind() const
171 { return (_compiledAtom ? _compiledAtom->endUnwind() : NULL); }
172 virtual ld::Atom::LineInfo::iterator beginLineInfo() const
173 { return (_compiledAtom ? _compiledAtom->beginLineInfo() : NULL); }
174 virtual ld::Atom::LineInfo::iterator endLineInfo() const
175 { return (_compiledAtom ? _compiledAtom->endLineInfo() : NULL); }
176
177 const ld::Atom* compiledAtom() { return _compiledAtom; }
178 void setCompiledAtom(const ld::Atom& atom);
179
180 private:
181
182 File& _file;
183 const char* _name;
184 const ld::Atom* _compiledAtom;
185 };
186
187
188
189
190
191
192
193 class Parser
194 {
195 public:
196 static bool validFile(const uint8_t* fileContent, uint64_t fileLength, cpu_type_t architecture, cpu_subtype_t subarch);
197 static const char* fileKind(const uint8_t* fileContent, uint64_t fileLength);
198 static File* parse(const uint8_t* fileContent, uint64_t fileLength, const char* path,
199 time_t modTime, ld::File::Ordinal ordinal, cpu_type_t architecture, cpu_subtype_t subarch, bool logAllFiles);
200 static bool libLTOisLoaded() { return (::lto_get_version() != NULL); }
201 static bool optimize( const std::vector<const ld::Atom*>& allAtoms,
202 ld::Internal& state,
203 const OptimizeOptions& options,
204 ld::File::AtomHandler& handler,
205 std::vector<const ld::Atom*>& newAtoms,
206 std::vector<const char*>& additionalUndefines);
207
208 static const char* ltoVersion() { return ::lto_get_version(); }
209
210 private:
211 static const char* tripletPrefixForArch(cpu_type_t arch);
212 static ld::relocatable::File* parseMachOFile(const uint8_t* p, size_t len, const OptimizeOptions& options);
213
214 class CStringEquals
215 {
216 public:
217 bool operator()(const char* left, const char* right) const { return (strcmp(left, right) == 0); }
218 };
219 typedef __gnu_cxx::hash_set<const char*, __gnu_cxx::hash<const char*>, CStringEquals> CStringSet;
220 typedef __gnu_cxx::hash_map<const char*, Atom*, __gnu_cxx::hash<const char*>, CStringEquals> CStringToAtom;
221
222 class AtomSyncer : public ld::File::AtomHandler {
223 public:
224 AtomSyncer(std::vector<const char*>& a, std::vector<const ld::Atom*>&na,
225 CStringToAtom la, CStringToAtom dla, const OptimizeOptions& options) :
226 _options(options), _additionalUndefines(a), _newAtoms(na), _llvmAtoms(la), _deadllvmAtoms(dla) { }
227 virtual void doAtom(const class ld::Atom&);
228 virtual void doFile(const class ld::File&) { }
229
230 const OptimizeOptions& _options;
231 std::vector<const char*>& _additionalUndefines;
232 std::vector<const ld::Atom*>& _newAtoms;
233 CStringToAtom _llvmAtoms;
234 CStringToAtom _deadllvmAtoms;
235 };
236
237 static std::vector<File*> _s_files;
238 };
239
240 std::vector<File*> Parser::_s_files;
241
242
243 bool Parser::validFile(const uint8_t* fileContent, uint64_t fileLength, cpu_type_t architecture, cpu_subtype_t subarch)
244 {
245 for (const ArchInfo* t=archInfoArray; t->archName != NULL; ++t) {
246 if ( (architecture == t->cpuType) && (!(t->isSubType) || (subarch == t->cpuSubType)) ) {
247 bool result = ::lto_module_is_object_file_in_memory_for_target(fileContent, fileLength, t->llvmTriplePrefix);
248 if ( !result ) {
249 // <rdar://problem/8434487> LTO only supports thumbv7 not armv7
250 if ( t->llvmTriplePrefixAlt[0] != '\0' ) {
251 result = ::lto_module_is_object_file_in_memory_for_target(fileContent, fileLength, t->llvmTriplePrefixAlt);
252 }
253 }
254 return result;
255 }
256 }
257 return false;
258 }
259
260 const char* Parser::fileKind(const uint8_t* p, uint64_t fileLength)
261 {
262 if ( (p[0] == 0xDE) && (p[1] == 0xC0) && (p[2] == 0x17) && (p[3] == 0x0B) ) {
263 cpu_type_t arch = LittleEndian::get32(*((uint32_t*)(&p[16])));
264 for (const ArchInfo* t=archInfoArray; t->archName != NULL; ++t) {
265 if ( arch == t->cpuType ) {
266 if ( t->isSubType ) {
267 if ( ::lto_module_is_object_file_in_memory_for_target(p, fileLength, t->llvmTriplePrefix) )
268 return t->archName;
269 }
270 else {
271 return t->archName;
272 }
273 }
274 }
275 return "unknown bitcode architecture";
276 }
277 return NULL;
278 }
279
280 File* Parser::parse(const uint8_t* fileContent, uint64_t fileLength, const char* path, time_t modTime, ld::File::Ordinal ordinal,
281 cpu_type_t architecture, cpu_subtype_t subarch, bool logAllFiles)
282 {
283 File* f = new File(path, modTime, ordinal, fileContent, fileLength, architecture);
284 _s_files.push_back(f);
285 if ( logAllFiles )
286 printf("%s\n", path);
287 return f;
288 }
289
290
291 ld::relocatable::File* Parser::parseMachOFile(const uint8_t* p, size_t len, const OptimizeOptions& options)
292 {
293 mach_o::relocatable::ParserOptions objOpts;
294 objOpts.architecture = options.arch;
295 objOpts.objSubtypeMustMatch = false;
296 objOpts.logAllFiles = false;
297 objOpts.convertUnwindInfo = true;
298 objOpts.subType = 0;
299
300 // mach-o parsing is done in-memory, but need path for debug notes
301 const char* path = "/tmp/lto.o";
302 time_t modTime = 0;
303 if ( options.tmpObjectFilePath != NULL ) {
304 path = options.tmpObjectFilePath;
305 struct stat statBuffer;
306 if ( stat(options.tmpObjectFilePath, &statBuffer) == 0 )
307 modTime = statBuffer.st_mtime;
308 }
309
310 ld::relocatable::File* result = mach_o::relocatable::parse(p, len, path, modTime, ld::File::Ordinal::LTOOrdinal(), objOpts);
311 if ( result != NULL )
312 return result;
313 throw "LLVM LTO, file is not of required architecture";
314 }
315
316
317
318 File::File(const char* pth, time_t mTime, ld::File::Ordinal ordinal, const uint8_t* content, uint32_t contentLength, cpu_type_t arch)
319 : ld::relocatable::File(pth,mTime,ordinal), _architecture(arch), _internalAtom(*this),
320 _atomArray(NULL), _atomArrayCount(0), _module(NULL), _debugInfoPath(pth),
321 _section("__TEXT_", "__tmp_lto", ld::Section::typeTempLTO),
322 _fixupToInternal(0, ld::Fixup::k1of1, ld::Fixup::kindNone, &_internalAtom),
323 _debugInfo(ld::relocatable::File::kDebugInfoNone), _cpuSubType(0)
324 {
325 const bool log = false;
326
327 // create llvm module
328 _module = ::lto_module_create_from_memory(content, contentLength);
329 if ( _module == NULL )
330 throwf("could not parse object file %s: %s", pth, lto_get_error_message());
331
332 if ( log ) fprintf(stderr, "bitcode file: %s\n", pth);
333
334 // create atom for each global symbol in module
335 uint32_t count = ::lto_module_get_num_symbols(_module);
336 _atomArray = (Atom*)malloc(sizeof(Atom)*count);
337 for (uint32_t i=0; i < count; ++i) {
338 const char* name = ::lto_module_get_symbol_name(_module, i);
339 lto_symbol_attributes attr = lto_module_get_symbol_attribute(_module, i);
340
341 // <rdar://problem/6378110> LTO doesn't like dtrace symbols
342 // ignore dtrace static probes for now
343 // later when codegen is done and a mach-o file is produces the probes will be processed
344 if ( (strncmp(name, "___dtrace_probe$", 16) == 0) || (strncmp(name, "___dtrace_isenabled$", 20) == 0) )
345 continue;
346
347 ld::Atom::Definition def;
348 ld::Atom::Combine combine = ld::Atom::combineNever;
349 switch ( attr & LTO_SYMBOL_DEFINITION_MASK ) {
350 case LTO_SYMBOL_DEFINITION_REGULAR:
351 def = ld::Atom::definitionRegular;
352 break;
353 case LTO_SYMBOL_DEFINITION_TENTATIVE:
354 def = ld::Atom::definitionTentative;
355 break;
356 case LTO_SYMBOL_DEFINITION_WEAK:
357 def = ld::Atom::definitionRegular;
358 combine = ld::Atom::combineByName;
359 break;
360 case LTO_SYMBOL_DEFINITION_UNDEFINED:
361 case LTO_SYMBOL_DEFINITION_WEAKUNDEF:
362 def = ld::Atom::definitionProxy;
363 break;
364 default:
365 throwf("unknown definition kind for symbol %s in bitcode file %s", name, pth);
366 }
367
368 // make LLVM atoms for definitions and a reference for undefines
369 if ( def != ld::Atom::definitionProxy ) {
370 ld::Atom::Scope scope;
371 bool autohide = false;
372 switch ( attr & LTO_SYMBOL_SCOPE_MASK) {
373 case LTO_SYMBOL_SCOPE_INTERNAL:
374 scope = ld::Atom::scopeTranslationUnit;
375 break;
376 case LTO_SYMBOL_SCOPE_HIDDEN:
377 scope = ld::Atom::scopeLinkageUnit;
378 break;
379 case LTO_SYMBOL_SCOPE_DEFAULT:
380 scope = ld::Atom::scopeGlobal;
381 break;
382 #if LTO_API_VERSION >= 4
383 case LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN:
384 scope = ld::Atom::scopeGlobal;
385 autohide = true;
386 break;
387 #endif
388 default:
389 throwf("unknown scope for symbol %s in bitcode file %s", name, pth);
390 }
391 // only make atoms for non-internal symbols
392 if ( scope == ld::Atom::scopeTranslationUnit )
393 continue;
394 uint8_t alignment = (attr & LTO_SYMBOL_ALIGNMENT_MASK);
395 // make Atom using placement new operator
396 new (&_atomArray[_atomArrayCount++]) Atom(*this, name, scope, def, combine, alignment, autohide);
397 if ( scope != ld::Atom::scopeTranslationUnit )
398 _internalAtom.addReference(name);
399 if ( log ) fprintf(stderr, "\t0x%08X %s\n", attr, name);
400 }
401 else {
402 // add to list of external references
403 _internalAtom.addReference(name);
404 if ( log ) fprintf(stderr, "\t%s (undefined)\n", name);
405 }
406 }
407 }
408
409 File::~File()
410 {
411 if ( _module != NULL )
412 ::lto_module_dispose(_module);
413 }
414
415 bool File::forEachAtom(ld::File::AtomHandler& handler) const
416 {
417 handler.doAtom(_internalAtom);
418 for(uint32_t i=0; i < _atomArrayCount; ++i) {
419 handler.doAtom(_atomArray[i]);
420 }
421 return true;
422 }
423
424 InternalAtom::InternalAtom(File& f)
425 : ld::Atom(f._section, ld::Atom::definitionRegular, ld::Atom::combineNever, ld::Atom::scopeTranslationUnit,
426 ld::Atom::typeLTOtemporary, ld::Atom::symbolTableNotIn, true, false, false, ld::Atom::Alignment(0)),
427 _file(f)
428 {
429 }
430
431 Atom::Atom(File& f, const char* nm, ld::Atom::Scope s, ld::Atom::Definition d, ld::Atom::Combine c,
432 ld::Atom::Alignment a, bool ah)
433 : ld::Atom(f._section, d, c, s, ld::Atom::typeLTOtemporary,
434 ld::Atom::symbolTableIn, false, false, false, a),
435 _file(f), _name(nm), _compiledAtom(NULL)
436 {
437 if ( ah )
438 this->setAutoHide();
439 }
440
441 void Atom::setCompiledAtom(const ld::Atom& atom)
442 {
443 // set delegate so virtual methods go to it
444 _compiledAtom = &atom;
445
446 //fprintf(stderr, "setting lto atom %p to delegate to mach-o atom %p (%s)\n", this, &atom, atom.name());
447
448 // update fields in ld::Atom to match newly constructed mach-o atom
449 (const_cast<Atom*>(this))->setAttributesFromAtom(atom);
450 }
451
452
453
454 bool Parser::optimize( const std::vector<const ld::Atom*>& allAtoms,
455 ld::Internal& state,
456 const OptimizeOptions& options,
457 ld::File::AtomHandler& handler,
458 std::vector<const ld::Atom*>& newAtoms,
459 std::vector<const char*>& additionalUndefines)
460 {
461 const bool logMustPreserve = false;
462 const bool logExtraOptions = false;
463 const bool logBitcodeFiles = false;
464 const bool logAtomsBeforeSync = false;
465
466 // exit quickly if nothing to do
467 if ( _s_files.size() == 0 )
468 return false;
469
470 // print out LTO version string if -v was used
471 if ( options.verbose )
472 fprintf(stderr, "%s\n", lto_get_version());
473
474 // create optimizer and add each Reader
475 lto_code_gen_t generator = ::lto_codegen_create();
476 for (std::vector<File*>::iterator it=_s_files.begin(); it != _s_files.end(); ++it) {
477 if ( logBitcodeFiles ) fprintf(stderr, "lto_codegen_add_module(%s)\n", (*it)->path());
478 if ( ::lto_codegen_add_module(generator, (*it)->module()) )
479 throwf("lto: could not merge in %s because %s", (*it)->path(), ::lto_get_error_message());
480 }
481
482 // add any -mllvm command line options
483 for (std::vector<const char*>::const_iterator it=options.llvmOptions->begin(); it != options.llvmOptions->end(); ++it) {
484 if ( logExtraOptions ) fprintf(stderr, "passing option to llvm: %s\n", *it);
485 ::lto_codegen_debug_options(generator, *it);
486 }
487
488 // The atom graph uses directed edges (references). Collect all references where
489 // originating atom is not part of any LTO Reader. This allows optimizer to optimize an
490 // external (i.e. not originated from same .o file) reference if all originating atoms are also
491 // defined in llvm bitcode file.
492 CStringSet nonLLVMRefs;
493 CStringToAtom llvmAtoms;
494 bool hasNonllvmAtoms = false;
495 for (std::vector<const ld::Atom*>::const_iterator it = allAtoms.begin(); it != allAtoms.end(); ++it) {
496 const ld::Atom* atom = *it;
497 // only look at references that come from an atom that is not an llvm atom
498 if ( atom->contentType() != ld::Atom::typeLTOtemporary ) {
499 if ( (atom->section().type() != ld::Section::typeMachHeader) && (atom->definition() != ld::Atom::definitionProxy) ) {
500 hasNonllvmAtoms = true;
501 }
502 const ld::Atom* target;
503 for (ld::Fixup::iterator fit=atom->fixupsBegin(); fit != atom->fixupsEnd(); ++fit) {
504 switch ( fit->binding ) {
505 case ld::Fixup::bindingDirectlyBound:
506 // that reference an llvm atom
507 if ( fit->u.target->contentType() == ld::Atom::typeLTOtemporary )
508 nonLLVMRefs.insert(fit->u.target->name());
509 break;
510 case ld::Fixup::bindingsIndirectlyBound:
511 target = state.indirectBindingTable[fit->u.bindingIndex];
512 if ( target == NULL )
513 throwf("'%s' in %s contains undefined reference", atom->name(), atom->file()->path());
514 assert(target != NULL);
515 if ( target->contentType() == ld::Atom::typeLTOtemporary )
516 nonLLVMRefs.insert(target->name());
517 default:
518 break;
519 }
520 }
521 }
522 else {
523 llvmAtoms[atom->name()] = (Atom*)atom;
524 }
525 }
526 // if entry point is in a llvm bitcode file, it must be preserved by LTO
527 if ( state.entryPoint!= NULL ) {
528 if ( state.entryPoint->contentType() == ld::Atom::typeLTOtemporary )
529 nonLLVMRefs.insert(state.entryPoint->name());
530 }
531
532 // deadAtoms are the atoms that the linker coalesced. For instance weak or tentative definitions
533 // overriden by another atom. If any of these deadAtoms are llvm atoms and they were replaced
534 // with a mach-o atom, we need to tell the lto engine to preserve (not optimize away) its dead
535 // atom so that the linker can replace it with the mach-o one later.
536 CStringToAtom deadllvmAtoms;
537 for (std::vector<const ld::Atom*>::const_iterator it = allAtoms.begin(); it != allAtoms.end(); ++it) {
538 const ld::Atom* atom = *it;
539 if ( atom->coalescedAway() && (atom->contentType() == ld::Atom::typeLTOtemporary) ) {
540 const char* name = atom->name();
541 if ( logMustPreserve ) fprintf(stderr, "lto_codegen_add_must_preserve_symbol(%s) because linker coalesce away and replace with a mach-o atom\n", name);
542 ::lto_codegen_add_must_preserve_symbol(generator, name);
543 deadllvmAtoms[name] = (Atom*)atom;
544 }
545 }
546 for (std::vector<File*>::iterator it=_s_files.begin(); it != _s_files.end(); ++it) {
547 File* file = *it;
548 for(uint32_t i=0; i < file->_atomArrayCount; ++i) {
549 Atom* llvmAtom = &file->_atomArray[i];
550 if ( llvmAtom->coalescedAway() ) {
551 const char* name = llvmAtom->name();
552 if ( deadllvmAtoms.find(name) == deadllvmAtoms.end() ) {
553 if ( logMustPreserve )
554 fprintf(stderr, "lto_codegen_add_must_preserve_symbol(%s) because linker coalesce away and replace with a mach-o atom\n", name);
555 ::lto_codegen_add_must_preserve_symbol(generator, name);
556 deadllvmAtoms[name] = (Atom*)llvmAtom;
557 }
558 }
559 else if ( options.linkerDeadStripping && !llvmAtom->live() ) {
560 const char* name = llvmAtom->name();
561 deadllvmAtoms[name] = (Atom*)llvmAtom;
562 }
563 }
564 }
565
566 // tell code generator about symbols that must be preserved
567 for (CStringToAtom::iterator it = llvmAtoms.begin(); it != llvmAtoms.end(); ++it) {
568 const char* name = it->first;
569 Atom* atom = it->second;
570 // Include llvm Symbol in export list if it meets one of following two conditions
571 // 1 - atom scope is global (and not linkage unit).
572 // 2 - included in nonLLVMRefs set.
573 // If a symbol is not listed in exportList then LTO is free to optimize it away.
574 if ( (atom->scope() == ld::Atom::scopeGlobal) && options.preserveAllGlobals ) {
575 if ( logMustPreserve ) fprintf(stderr, "lto_codegen_add_must_preserve_symbol(%s) because global symbol\n", name);
576 ::lto_codegen_add_must_preserve_symbol(generator, name);
577 }
578 else if ( nonLLVMRefs.find(name) != nonLLVMRefs.end() ) {
579 if ( logMustPreserve ) fprintf(stderr, "lto_codegen_add_must_preserve_symbol(%s) because referenced by a mach-o atom\n", name);
580 ::lto_codegen_add_must_preserve_symbol(generator, name);
581 }
582 }
583
584 // special case running ld -r on all bitcode files to produce another bitcode file (instead of mach-o)
585 if ( options.relocatable && !hasNonllvmAtoms ) {
586 if ( ! ::lto_codegen_write_merged_modules(generator, options.outputFilePath) ) {
587 // HACK, no good way to tell linker we are all done, so just quit
588 exit(0);
589 }
590 warning("could not produce merged bitcode file");
591 }
592
593 // set code-gen model
594 lto_codegen_model model = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
595 if ( options.mainExecutable ) {
596 if ( options.staticExecutable ) {
597 // darwin x86_64 "static" code model is really dynamic code model
598 if ( options.arch == CPU_TYPE_X86_64 )
599 model = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
600 else
601 model = LTO_CODEGEN_PIC_MODEL_STATIC;
602 }
603 else {
604 if ( options.pie )
605 model = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
606 else
607 model = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC;
608 }
609 }
610 else {
611 if ( options.allowTextRelocs )
612 model = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC;
613 else
614 model = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
615 }
616 if ( ::lto_codegen_set_pic_model(generator, model) )
617 throwf("could not create set codegen model: %s", lto_get_error_message());
618
619 // if requested, save off merged bitcode file
620 if ( options.saveTemps ) {
621 char tempBitcodePath[MAXPATHLEN];
622 strcpy(tempBitcodePath, options.outputFilePath);
623 strcat(tempBitcodePath, ".lto.bc");
624 ::lto_codegen_write_merged_modules(generator, tempBitcodePath);
625 }
626
627 #if LTO_API_VERSION >= 3
628 // find assembler next to linker
629 char path[PATH_MAX];
630 uint32_t bufSize = PATH_MAX;
631 if ( _NSGetExecutablePath(path, &bufSize) != -1 ) {
632 char* lastSlash = strrchr(path, '/');
633 if ( lastSlash != NULL ) {
634 strcpy(lastSlash+1, "as");
635 struct stat statInfo;
636 if ( stat(path, &statInfo) == 0 )
637 ::lto_codegen_set_assembler_path(generator, path);
638 }
639 }
640 #endif
641 // run code generator
642 size_t machOFileLen;
643 const uint8_t* machOFile = (uint8_t*)::lto_codegen_compile(generator, &machOFileLen);
644 if ( machOFile == NULL )
645 throwf("could not do LTO codegen: %s", ::lto_get_error_message());
646
647 // if requested, save off temp mach-o file
648 if ( options.saveTemps ) {
649 char tempMachoPath[MAXPATHLEN];
650 strcpy(tempMachoPath, options.outputFilePath);
651 strcat(tempMachoPath, ".lto.o");
652 int fd = ::open(tempMachoPath, O_CREAT | O_WRONLY | O_TRUNC, 0666);
653 if ( fd != -1) {
654 ::write(fd, machOFile, machOFileLen);
655 ::close(fd);
656 }
657 // save off merged bitcode file
658 char tempOptBitcodePath[MAXPATHLEN];
659 strcpy(tempOptBitcodePath, options.outputFilePath);
660 strcat(tempOptBitcodePath, ".lto.opt.bc");
661 ::lto_codegen_write_merged_modules(generator, tempOptBitcodePath);
662 }
663
664 // if needed, save temp mach-o file to specific location
665 if ( options.tmpObjectFilePath != NULL ) {
666 int fd = ::open(options.tmpObjectFilePath, O_CREAT | O_WRONLY | O_TRUNC, 0666);
667 if ( fd != -1) {
668 ::write(fd, machOFile, machOFileLen);
669 ::close(fd);
670 }
671 else {
672 warning("could not write LTO temp file '%s', errno=%d", options.tmpObjectFilePath, errno);
673 }
674 }
675
676 // parse generated mach-o file into a MachOReader
677 ld::relocatable::File* machoFile = parseMachOFile(machOFile, machOFileLen, options);
678
679 // sync generated mach-o atoms with existing atoms ld knows about
680 if ( logAtomsBeforeSync ) {
681 fprintf(stderr, "llvmAtoms:\n");
682 for (CStringToAtom::iterator it = llvmAtoms.begin(); it != llvmAtoms.end(); ++it) {
683 const char* name = it->first;
684 //Atom* atom = it->second;
685 fprintf(stderr, "\t%s\n", name);
686 }
687 fprintf(stderr, "deadllvmAtoms:\n");
688 for (CStringToAtom::iterator it = deadllvmAtoms.begin(); it != deadllvmAtoms.end(); ++it) {
689 const char* name = it->first;
690 //Atom* atom = it->second;
691 fprintf(stderr, "\t%s\n", name);
692 }
693 }
694 AtomSyncer syncer(additionalUndefines, newAtoms, llvmAtoms, deadllvmAtoms, options);
695 machoFile->forEachAtom(syncer);
696
697 // Remove InternalAtoms from ld
698 for (std::vector<File*>::iterator it=_s_files.begin(); it != _s_files.end(); ++it) {
699 (*it)->internalAtom().setCoalescedAway();
700 }
701 // Remove Atoms from ld if code generator optimized them away
702 for (CStringToAtom::iterator li = llvmAtoms.begin(), le = llvmAtoms.end(); li != le; ++li) {
703 // check if setRealAtom() called on this Atom
704 if ( li->second->compiledAtom() == NULL ) {
705 //fprintf(stderr, "llvm optimized away %p %s\n", li->second, li->second->name());
706 li->second->setCoalescedAway();
707 }
708 }
709
710 // notify about file level attributes
711 handler.doFile(*machoFile);
712
713 // if final mach-o file has debug info, update original bitcode files to match
714 for (std::vector<File*>::iterator it=_s_files.begin(); it != _s_files.end(); ++it) {
715 (*it)->setDebugInfo(machoFile->debugInfo(), machoFile->path(),
716 machoFile->modificationTime(), machoFile->cpuSubType());
717 }
718
719 return true;
720 }
721
722
723 void Parser::AtomSyncer::doAtom(const ld::Atom& machoAtom)
724 {
725 // update proxy atoms to point to real atoms and find new atoms
726 const char* name = machoAtom.name();
727 if ( machoAtom.scope() >= ld::Atom::scopeLinkageUnit ) {
728 CStringToAtom::iterator pos = _llvmAtoms.find(name);
729 if ( pos != _llvmAtoms.end() ) {
730 // turn Atom into a proxy for this mach-o atom
731 pos->second->setCompiledAtom(machoAtom);
732 }
733 else {
734 // an atom of this name was not in the allAtoms list the linker gave us
735 if ( _deadllvmAtoms.find(name) != _deadllvmAtoms.end() ) {
736 // this corresponding to an atom that the linker coalesced away or marked not-live
737 if ( _options.linkerDeadStripping ) {
738 // llvm seems to want this atom and -dead_strip is enabled, so it will be deleted if not needed, so add back
739 Atom* llvmAtom = _deadllvmAtoms[name];
740 llvmAtom->setCompiledAtom(machoAtom);
741 _newAtoms.push_back(&machoAtom);
742 }
743 else {
744 // Don't pass it back as a new atom
745 }
746 }
747 else
748 {
749 // this is something new that lto conjured up, tell ld its new
750 _newAtoms.push_back(&machoAtom);
751 }
752 }
753 }
754 else {
755 // ld only knew about non-static atoms, so this one must be new
756 _newAtoms.push_back(&machoAtom);
757 }
758
759 // adjust fixups to go through proxy atoms
760 //fprintf(stderr, "adjusting fixups in atom: %s\n", machoAtom.name());
761 for (ld::Fixup::iterator fit=machoAtom.fixupsBegin(); fit != machoAtom.fixupsEnd(); ++fit) {
762 switch ( fit->binding ) {
763 case ld::Fixup::bindingNone:
764 break;
765 case ld::Fixup::bindingByNameUnbound:
766 // don't know if this target has been seen by linker before or if it is new
767 // be conservative and tell linker it is new
768 _additionalUndefines.push_back(fit->u.name);
769 //fprintf(stderr, " by name ref to: %s\n", fit->u.name);
770 break;
771 case ld::Fixup::bindingDirectlyBound:
772 // If mach-o atom is referencing another mach-o atom then
773 // reference is not going through Atom proxy. Fix it here to ensure that all
774 // llvm symbol references always go through Atom proxy.
775 if ( fit->u.target->scope() != ld::Atom::scopeTranslationUnit ) {
776 const char* targetName = fit->u.target->name();
777 CStringToAtom::iterator pos = _llvmAtoms.find(targetName);
778 if ( pos != _llvmAtoms.end() ) {
779 fit->u.target = pos->second;
780 }
781 else {
782 if ( _deadllvmAtoms.find(targetName) != _deadllvmAtoms.end() ) {
783 // target was coalesed away and replace by mach-o atom from a non llvm .o file
784 fit->binding = ld::Fixup::bindingByNameUnbound;
785 fit->u.name = targetName;
786 }
787 }
788 }
789 //fprintf(stderr, " direct ref to: %s (scope=%d)\n", fit->u.target->name(), fit->u.target->scope());
790 break;
791 case ld::Fixup::bindingByContentBound:
792 //fprintf(stderr, " direct by content to: %s\n", fit->u.target->name());
793 break;
794 case ld::Fixup::bindingsIndirectlyBound:
795 assert(0 && "indirect binding found in initial mach-o file?");
796 //fprintf(stderr, " indirect by content to: %u\n", fit->u.bindingIndex);
797 break;
798 }
799 }
800
801 }
802
803 class Mutex {
804 static pthread_mutex_t lto_lock;
805 public:
806 Mutex() { pthread_mutex_lock(&lto_lock); }
807 ~Mutex() { pthread_mutex_unlock(&lto_lock); }
808 };
809 pthread_mutex_t Mutex::lto_lock = PTHREAD_MUTEX_INITIALIZER;
810
811 //
812 // Used by archive reader to see if member is an llvm bitcode file
813 //
814 bool isObjectFile(const uint8_t* fileContent, uint64_t fileLength, cpu_type_t architecture, cpu_subtype_t subarch)
815 {
816 Mutex lock;
817 return Parser::validFile(fileContent, fileLength, architecture, subarch);
818 }
819
820
821 //
822 // main function used by linker to instantiate ld::Files
823 //
824 ld::relocatable::File* parse(const uint8_t* fileContent, uint64_t fileLength,
825 const char* path, time_t modTime, ld::File::Ordinal ordinal,
826 cpu_type_t architecture, cpu_subtype_t subarch, bool logAllFiles)
827 {
828 Mutex lock;
829 if ( Parser::validFile(fileContent, fileLength, architecture, subarch) )
830 return Parser::parse(fileContent, fileLength, path, modTime, ordinal, architecture, subarch, logAllFiles);
831 else
832 return NULL;
833 }
834
835 //
836 // used by "ld -v" to report version of libLTO.dylib being used
837 //
838 const char* version()
839 {
840 Mutex lock;
841 return ::lto_get_version();
842 }
843
844
845 //
846 // used by ld for error reporting
847 //
848 bool libLTOisLoaded()
849 {
850 Mutex lock;
851 return (::lto_get_version() != NULL);
852 }
853
854 //
855 // used by ld for error reporting
856 //
857 const char* archName(const uint8_t* fileContent, uint64_t fileLength)
858 {
859 Mutex lock;
860 return Parser::fileKind(fileContent, fileLength);
861 }
862
863 //
864 // used by ld for doing link time optimization
865 //
866 bool optimize( const std::vector<const ld::Atom*>& allAtoms,
867 ld::Internal& state,
868 const OptimizeOptions& options,
869 ld::File::AtomHandler& handler,
870 std::vector<const ld::Atom*>& newAtoms,
871 std::vector<const char*>& additionalUndefines)
872 {
873 Mutex lock;
874 return Parser::optimize(allAtoms, state, options, handler, newAtoms, additionalUndefines);
875 }
876
877
878
879 }; // namespace lto
880
881
882 #endif
883