]> git.saurik.com Git - apple/ld64.git/blame - src/ld/parsers/lto_file.cpp
ld64-133.3.tar.gz
[apple/ld64.git] / src / ld / parsers / lto_file.cpp
CommitLineData
a645023d
A
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
afe874b1
A
44// #defines are a work around for <rdar://problem/8760268>
45#define __STDC_LIMIT_MACROS 1
46#define __STDC_CONSTANT_MACROS 1
a645023d
A
47#include "llvm-c/lto.h"
48
49
50namespace 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//
60class InternalAtom : public ld::Atom
61{
62public:
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
67 { return false; }
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()]; }
75
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)); }
80private:
81
82 ld::File& _file;
83 mutable std::vector<ld::Fixup> _undefs;
84};
85
86
87//
88// LLVM bitcode file
89//
90class File : public ld::relocatable::File
91{
92public:
93 File(const char* path, time_t mTime, const uint8_t* content,
ebf6f434 94 uint32_t contentLength, cpu_type_t arch);
a645023d
A
95 virtual ~File();
96
97 // overrides of ld::File
98 virtual bool forEachAtom(ld::File::AtomHandler&) const;
99 virtual bool justInTimeforEachAtom(const char* name, ld::File::AtomHandler&) const
100 { return false; }
101 virtual uint32_t cpuSubType() const { return _cpuSubType; }
102
103 // overrides of ld::relocatable::File
a645023d
A
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; }
110
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)
115 { _debugInfo = k;
116 _debugInfoPath = pth;
117 _debugInfoModTime = modTime;
118 _cpuSubType = subtype;}
119
120private:
121 friend class Atom;
122 friend class InternalAtom;
123 friend class Parser;
124
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;
136};
137
138//
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.
144//
145class Atom : public ld::Atom
146{
147public:
148 Atom(File& f, const char* name, ld::Atom::Scope s,
b2fa67a8 149 ld::Atom::Definition d, ld::Atom::Combine c, ld::Atom::Alignment a, bool ah);
a645023d
A
150
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); }
178
179 const ld::Atom* compiledAtom() { return _compiledAtom; }
180 void setCompiledAtom(const ld::Atom& atom);
181
182private:
183
184 File& _file;
185 const char* _name;
186 const ld::Atom* _compiledAtom;
187};
188
189
190
191
192
193
194
195class Parser
196{
197public:
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,
ebf6f434 201 time_t modTime, cpu_type_t architecture, cpu_subtype_t subarch, bool logAllFiles);
a645023d
A
202 static bool libLTOisLoaded() { return (::lto_get_version() != NULL); }
203 static bool optimize( const std::vector<const ld::Atom*>& allAtoms,
204 ld::Internal& state,
a645023d
A
205 const OptimizeOptions& options,
206 ld::File::AtomHandler& handler,
207 std::vector<const ld::Atom*>& newAtoms,
208 std::vector<const char*>& additionalUndefines);
209
210 static const char* ltoVersion() { return ::lto_get_version(); }
211
212private:
213 static const char* tripletPrefixForArch(cpu_type_t arch);
ebf6f434 214 static ld::relocatable::File* parseMachOFile(const uint8_t* p, size_t len, const OptimizeOptions& options);
a645023d
A
215
216 class CStringEquals
217 {
218 public:
219 bool operator()(const char* left, const char* right) const { return (strcmp(left, right) == 0); }
220 };
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;
223
224 class AtomSyncer : public ld::File::AtomHandler {
225 public:
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&) { }
231
232 const OptimizeOptions& _options;
233 std::vector<const char*>& _additionalUndefines;
234 std::vector<const ld::Atom*>& _newAtoms;
235 CStringToAtom _llvmAtoms;
236 CStringToAtom _deadllvmAtoms;
237 };
238
239 static std::vector<File*> _s_files;
240};
241
242std::vector<File*> Parser::_s_files;
243
244
245bool Parser::validFile(const uint8_t* fileContent, uint64_t fileLength, cpu_type_t architecture, cpu_subtype_t subarch)
246{
ebf6f434
A
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);
250 if ( !result ) {
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);
254 }
a645023d 255 }
ebf6f434
A
256 return result;
257 }
a645023d
A
258 }
259 return false;
260}
261
262const char* Parser::fileKind(const uint8_t* p, uint64_t fileLength)
263{
264 if ( (p[0] == 0xDE) && (p[1] == 0xC0) && (p[2] == 0x17) && (p[3] == 0x0B) ) {
ebf6f434
A
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 ) {
afe874b1 269 if ( ::lto_module_is_object_file_in_memory_for_target(p, fileLength, t->llvmTriplePrefix) )
ebf6f434
A
270 return t->archName;
271 }
272 else {
273 return t->archName;
afe874b1 274 }
ebf6f434 275 }
a645023d
A
276 }
277 return "unknown bitcode architecture";
278 }
279 return NULL;
280}
281
282File* Parser::parse(const uint8_t* fileContent, uint64_t fileLength, const char* path, time_t modTime,
ebf6f434 283 cpu_type_t architecture, cpu_subtype_t subarch, bool logAllFiles)
a645023d 284{
ebf6f434 285 File* f = new File(path, modTime, fileContent, fileLength, architecture);
a645023d
A
286 _s_files.push_back(f);
287 if ( logAllFiles )
288 printf("%s\n", path);
289 return f;
290}
291
292
ebf6f434 293ld::relocatable::File* Parser::parseMachOFile(const uint8_t* p, size_t len, const OptimizeOptions& options)
a645023d
A
294{
295 mach_o::relocatable::ParserOptions objOpts;
296 objOpts.architecture = options.arch;
297 objOpts.objSubtypeMustMatch = false;
298 objOpts.logAllFiles = false;
299 objOpts.convertUnwindInfo = true;
300 objOpts.subType = 0;
301
302 // mach-o parsing is done in-memory, but need path for debug notes
303 const char* path = "/tmp/lto.o";
304 time_t modTime = 0;
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;
310 }
311
ebf6f434 312 ld::relocatable::File* result = mach_o::relocatable::parse(p, len, path, modTime, ld::File::Ordinal::LTOOrdinal(), objOpts);
a645023d
A
313 if ( result != NULL )
314 return result;
315 throw "LLVM LTO, file is not of required architecture";
316}
317
318
319
ebf6f434
A
320File::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),
a645023d
A
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)
326{
327 const bool log = false;
328
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());
333
334 if ( log ) fprintf(stderr, "bitcode file: %s\n", pth);
335
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);
342
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) )
347 continue;
348
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;
354 break;
355 case LTO_SYMBOL_DEFINITION_TENTATIVE:
356 def = ld::Atom::definitionTentative;
357 break;
358 case LTO_SYMBOL_DEFINITION_WEAK:
359 def = ld::Atom::definitionRegular;
360 combine = ld::Atom::combineByName;
361 break;
362 case LTO_SYMBOL_DEFINITION_UNDEFINED:
363 case LTO_SYMBOL_DEFINITION_WEAKUNDEF:
364 def = ld::Atom::definitionProxy;
365 break;
366 default:
367 throwf("unknown definition kind for symbol %s in bitcode file %s", name, pth);
368 }
369
370 // make LLVM atoms for definitions and a reference for undefines
371 if ( def != ld::Atom::definitionProxy ) {
372 ld::Atom::Scope scope;
b2fa67a8 373 bool autohide = false;
a645023d
A
374 switch ( attr & LTO_SYMBOL_SCOPE_MASK) {
375 case LTO_SYMBOL_SCOPE_INTERNAL:
376 scope = ld::Atom::scopeTranslationUnit;
377 break;
378 case LTO_SYMBOL_SCOPE_HIDDEN:
379 scope = ld::Atom::scopeLinkageUnit;
380 break;
381 case LTO_SYMBOL_SCOPE_DEFAULT:
382 scope = ld::Atom::scopeGlobal;
383 break;
b2fa67a8
A
384#if LTO_API_VERSION >= 4
385 case LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN:
386 scope = ld::Atom::scopeGlobal;
387 autohide = true;
388 break;
389#endif
a645023d
A
390 default:
391 throwf("unknown scope for symbol %s in bitcode file %s", name, pth);
392 }
393 // only make atoms for non-internal symbols
394 if ( scope == ld::Atom::scopeTranslationUnit )
395 continue;
396 uint8_t alignment = (attr & LTO_SYMBOL_ALIGNMENT_MASK);
397 // make Atom using placement new operator
b2fa67a8 398 new (&_atomArray[_atomArrayCount++]) Atom(*this, name, scope, def, combine, alignment, autohide);
ebf6f434 399 if ( scope != ld::Atom::scopeTranslationUnit )
a645023d
A
400 _internalAtom.addReference(name);
401 if ( log ) fprintf(stderr, "\t0x%08X %s\n", attr, name);
402 }
403 else {
404 // add to list of external references
405 _internalAtom.addReference(name);
406 if ( log ) fprintf(stderr, "\t%s (undefined)\n", name);
407 }
408 }
409}
410
411File::~File()
412{
413 if ( _module != NULL )
414 ::lto_module_dispose(_module);
415}
416
417bool File::forEachAtom(ld::File::AtomHandler& handler) const
418{
419 handler.doAtom(_internalAtom);
420 for(uint32_t i=0; i < _atomArrayCount; ++i) {
421 handler.doAtom(_atomArray[i]);
422 }
423 return true;
424}
425
426InternalAtom::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)),
429 _file(f)
430{
431}
432
b2fa67a8
A
433Atom::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)
a645023d
A
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)
438{
b2fa67a8
A
439 if ( ah )
440 this->setAutoHide();
a645023d
A
441}
442
443void Atom::setCompiledAtom(const ld::Atom& atom)
444{
445 // set delegate so virtual methods go to it
446 _compiledAtom = &atom;
447
448 //fprintf(stderr, "setting lto atom %p to delegate to mach-o atom %p (%s)\n", this, &atom, atom.name());
449
450 // update fields in ld::Atom to match newly constructed mach-o atom
451 (const_cast<Atom*>(this))->setAttributesFromAtom(atom);
452}
453
454
455
456bool Parser::optimize( const std::vector<const ld::Atom*>& allAtoms,
457 ld::Internal& state,
a645023d
A
458 const OptimizeOptions& options,
459 ld::File::AtomHandler& handler,
460 std::vector<const ld::Atom*>& newAtoms,
461 std::vector<const char*>& additionalUndefines)
462{
463 const bool logMustPreserve = false;
464 const bool logExtraOptions = false;
465 const bool logBitcodeFiles = false;
466 const bool logAtomsBeforeSync = false;
467
468 // exit quickly if nothing to do
469 if ( _s_files.size() == 0 )
470 return false;
471
472 // print out LTO version string if -v was used
473 if ( options.verbose )
474 fprintf(stderr, "%s\n", lto_get_version());
475
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());
482 }
483
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);
488 }
489
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;
503 }
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());
511 break;
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());
519 default:
520 break;
521 }
522 }
523 }
524 else {
525 llvmAtoms[atom->name()] = (Atom*)atom;
526 }
527 }
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());
532 }
533
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;
546 }
547 }
548 for (std::vector<File*>::iterator it=_s_files.begin(); it != _s_files.end(); ++it) {
549 File* file = *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;
559 }
560 }
561 else if ( options.linkerDeadStripping && !llvmAtom->live() ) {
562 const char* name = llvmAtom->name();
563 deadllvmAtoms[name] = (Atom*)llvmAtom;
564 }
565 }
566 }
567
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.
ebf6f434 576 if ( (atom->scope() == ld::Atom::scopeGlobal) && options.preserveAllGlobals ) {
a645023d
A
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);
579 }
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);
583 }
584 }
585
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
590 exit(0);
591 }
592 warning("could not produce merged bitcode file");
593 }
594
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;
602 else
603 model = LTO_CODEGEN_PIC_MODEL_STATIC;
604 }
605 else {
606 if ( options.pie )
607 model = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
608 else
609 model = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC;
610 }
611 }
612 else {
613 if ( options.allowTextRelocs )
614 model = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC;
615 else
616 model = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
617 }
618 if ( ::lto_codegen_set_pic_model(generator, model) )
619 throwf("could not create set codegen model: %s", lto_get_error_message());
620
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);
627 }
628
629#if LTO_API_VERSION >= 3
630 // find assembler next to linker
631 char path[PATH_MAX];
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);
640 }
641 }
642#endif
643 // run code generator
644 size_t machOFileLen;
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());
648
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);
655 if ( fd != -1) {
656 ::write(fd, machOFile, machOFileLen);
657 ::close(fd);
658 }
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);
664 }
665
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);
669 if ( fd != -1) {
670 ::write(fd, machOFile, machOFileLen);
671 ::close(fd);
672 }
673 else {
674 warning("could not write LTO temp file '%s', errno=%d", options.tmpObjectFilePath, errno);
675 }
676 }
677
678 // parse generated mach-o file into a MachOReader
ebf6f434 679 ld::relocatable::File* machoFile = parseMachOFile(machOFile, machOFileLen, options);
a645023d
A
680
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);
688 }
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);
694 }
695 }
696 AtomSyncer syncer(additionalUndefines, newAtoms, llvmAtoms, deadllvmAtoms, options);
697 machoFile->forEachAtom(syncer);
698
699 // Remove InternalAtoms from ld
700 for (std::vector<File*>::iterator it=_s_files.begin(); it != _s_files.end(); ++it) {
701 (*it)->internalAtom().setCoalescedAway();
702 }
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();
709 }
710 }
711
712 // notify about file level attributes
713 handler.doFile(*machoFile);
714
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());
719 }
720
721 return true;
722}
723
724
725void Parser::AtomSyncer::doAtom(const ld::Atom& machoAtom)
726{
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);
734 }
735 else {
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);
744 }
745 else {
746 // Don't pass it back as a new atom
747 }
748 }
749 else
750 {
751 // this is something new that lto conjured up, tell ld its new
752 _newAtoms.push_back(&machoAtom);
753 }
754 }
755 }
756 else {
757 // ld only knew about non-static atoms, so this one must be new
758 _newAtoms.push_back(&machoAtom);
759 }
760
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:
766 break;
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);
772 break;
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;
782 }
783 else {
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;
788 }
789 }
790 }
791 //fprintf(stderr, " direct ref to: %s (scope=%d)\n", fit->u.target->name(), fit->u.target->scope());
792 break;
793 case ld::Fixup::bindingByContentBound:
794 //fprintf(stderr, " direct by content to: %s\n", fit->u.target->name());
795 break;
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);
799 break;
800 }
801 }
802
803}
804
ebf6f434
A
805class Mutex {
806 static pthread_mutex_t lto_lock;
807public:
808 Mutex() { pthread_mutex_lock(&lto_lock); }
809 ~Mutex() { pthread_mutex_unlock(&lto_lock); }
810};
811pthread_mutex_t Mutex::lto_lock = PTHREAD_MUTEX_INITIALIZER;
a645023d
A
812
813//
814// Used by archive reader to see if member is an llvm bitcode file
815//
816bool isObjectFile(const uint8_t* fileContent, uint64_t fileLength, cpu_type_t architecture, cpu_subtype_t subarch)
817{
ebf6f434 818 Mutex lock;
a645023d
A
819 return Parser::validFile(fileContent, fileLength, architecture, subarch);
820}
821
822
823//
824// main function used by linker to instantiate ld::Files
825//
826ld::relocatable::File* parse(const uint8_t* fileContent, uint64_t fileLength,
ebf6f434 827 const char* path, time_t modTime,
a645023d
A
828 cpu_type_t architecture, cpu_subtype_t subarch, bool logAllFiles)
829{
ebf6f434 830 Mutex lock;
a645023d 831 if ( Parser::validFile(fileContent, fileLength, architecture, subarch) )
ebf6f434 832 return Parser::parse(fileContent, fileLength, path, modTime, architecture, subarch, logAllFiles);
a645023d
A
833 else
834 return NULL;
835}
836
837//
838// used by "ld -v" to report version of libLTO.dylib being used
839//
840const char* version()
841{
ebf6f434 842 Mutex lock;
a645023d
A
843 return ::lto_get_version();
844}
845
846
847//
848// used by ld for error reporting
849//
850bool libLTOisLoaded()
851{
ebf6f434 852 Mutex lock;
a645023d
A
853 return (::lto_get_version() != NULL);
854}
855
856//
857// used by ld for error reporting
858//
859const char* archName(const uint8_t* fileContent, uint64_t fileLength)
860{
ebf6f434 861 Mutex lock;
a645023d
A
862 return Parser::fileKind(fileContent, fileLength);
863}
864
865//
866// used by ld for doing link time optimization
867//
868bool optimize( const std::vector<const ld::Atom*>& allAtoms,
869 ld::Internal& state,
a645023d
A
870 const OptimizeOptions& options,
871 ld::File::AtomHandler& handler,
872 std::vector<const ld::Atom*>& newAtoms,
873 std::vector<const char*>& additionalUndefines)
874{
ebf6f434
A
875 Mutex lock;
876 return Parser::optimize(allAtoms, state, options, handler, newAtoms, additionalUndefines);
a645023d
A
877}
878
879
880
881}; // namespace lto
882
883
884#endif
885