1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2006-2009 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
22 * @APPLE_LICENSE_HEADER_END@
25 #ifndef __LTO_READER_H__
26 #define __LTO_READER_H__
29 #include <mach-o/dyld.h>
31 #include <ext/hash_set>
32 #include <ext/hash_map>
34 #include "MachOFileAbstraction.hpp"
35 #include "Architectures.hpp"
38 #include "llvm-c/lto.h"
45 // ld64 only tracks non-internal symbols from an llvm bitcode file.
46 // We model this by having an InternalAtom which represent all internal functions and data.
47 // All non-interal symbols from a bitcode file are represented by an Atom
48 // and each Atom has a reference to the InternalAtom. The InternalAtom
49 // also has references to each symbol external to the bitcode file.
51 class InternalAtom : public ld::Atom
54 InternalAtom(class File& f);
55 // overrides of ld::Atom
56 virtual ld::File* file() const { return &_file; }
57 virtual bool translationUnitSource(const char** dir, const char** nm) const
59 virtual const char* name() const { return "import-atom"; }
60 virtual uint64_t size() const { return 0; }
61 virtual uint64_t objectAddress() const { return 0; }
62 virtual void copyRawContent(uint8_t buffer[]) const { }
63 virtual void setScope(Scope) { }
64 virtual ld::Fixup::iterator fixupsBegin() { return &_undefs[0]; }
65 virtual ld::Fixup::iterator fixupsEnd() { return &_undefs[_undefs.size()]; }
67 // for adding references to symbols outside bitcode file
68 void addReference(const char* name)
69 { _undefs.push_back(ld::Fixup(0, ld::Fixup::k1of1,
70 ld::Fixup::fixupNone, false, name)); }
74 std::vector<ld::Fixup> _undefs;
81 class File : public ld::relocatable::File
84 File(const char* path, time_t mTime, const uint8_t* content,
85 uint32_t contentLength, uint32_t ordinal, cpu_type_t arch);
88 // overrides of ld::File
89 virtual bool forEachAtom(ld::File::AtomHandler&);
90 virtual bool justInTimeforEachAtom(const char* name, ld::File::AtomHandler&)
93 // overrides of ld::relocatable::File
94 virtual bool objcReplacementClasses() { return false; }
95 virtual DebugInfoKind debugInfo() { return ld::relocatable::File::kDebugInfoNone; }
96 virtual std::vector<ld::relocatable::File::Stab>* stabs() { return NULL; }
97 virtual bool canScatterAtoms() { return true; }
99 lto_module_t module() { return _module; }
100 class InternalAtom& internalAtom() { return _internalAtom; }
103 friend class InternalAtom;
105 cpu_type_t _architecture;
106 class InternalAtom _internalAtom;
107 class Atom* _atomArray;
108 uint32_t _atomArrayCount;
109 lto_module_t _module;
110 ld::Section _section;
114 // Atom acts as a proxy Atom for the symbols that are exported by LLVM bitcode file. Initially,
115 // Reader creates Atoms to allow linker proceed with usual symbol resolution phase. After
116 // optimization is performed, real Atoms are created for these symobls. However these real Atoms
117 // are not inserted into global symbol table. Atom holds real Atom and forwards appropriate
118 // methods to real atom.
120 class Atom : public ld::Atom
123 Atom(File& f, const char* name, ld::Atom::Scope s,
124 ld::Atom::Definition d, ld::Atom::Alignment a);
126 // overrides of ld::Atom
127 virtual ld::File* file() const { return &_file; }
128 virtual bool translationUnitSource(const char** dir, const char** nm) const
129 { return (_compiledAtom ? _compiledAtom->translationUnitSource(dir, nm) : false); }
130 virtual const char* name() const { return _name; }
131 virtual uint64_t size() const { return (_compiledAtom ? _compiledAtom->size() : 0); }
132 virtual uint64_t objectAddress() const { return (_compiledAtom ? _compiledAtom->objectAddress() : 0); }
133 virtual void copyRawContent(uint8_t buffer[]) const
134 { if (_compiledAtom) _compiledAtom->copyRawContent(buffer); }
136 ld::Atom* compiledAtom() { return _compiledAtom; }
137 void setCompiledAtom(ld::Atom& atom)
138 { _compiledAtom = &atom; }
143 ld::Atom* _compiledAtom;
155 static bool validFile(const uint8_t* fileContent, uint64_t fileLength, cpu_type_t architecture);
156 static const char* fileKind(const uint8_t* fileContent);
157 static File* parse(const uint8_t* fileContent, uint64_t fileLength, const char* path,
158 time_t modTime, uint32_t ordinal, cpu_type_t architecture);
159 static bool libLTOisLoaded() { return (::lto_get_version() != NULL); }
160 static bool optimize(const std::vector<ld::Atom*>& allAtoms, std::vector<ld::Atom*>& newAtoms,
161 std::vector<const char*>& additionalUndefines,
162 const std::set<ld::Atom*>&,
163 std::vector<ld::Atom*>& newDeadAtoms,
164 uint32_t nextInputOrdinal,
165 ld::OutFile* writer, ld::Atom* entryPointAtom,
166 const std::vector<const char*>& llvmOptions,
167 bool allGlobalsAReDeadStripRoots,
168 bool verbose, bool saveTemps,
169 const char* outputFilePath,
170 bool pie, bool mainExecutable, bool staticExecutable, bool relocatable,
171 bool allowTextRelocs, cpu_type_t arch);
173 static const char* ltoVersion() { return ::lto_get_version(); }
176 static const char* tripletPrefixForArch(cpu_type_t arch);
177 static ld::relocatable::File* parseMachOFile(const uint8_t* p, size_t len, uint32_t nextInputOrdinal, cpu_type_t arch);
182 bool operator()(const char* left, const char* right) const { return (strcmp(left, right) == 0); }
184 typedef __gnu_cxx::hash_set<const char*, __gnu_cxx::hash<const char*>, CStringEquals> CStringSet;
185 typedef __gnu_cxx::hash_map<const char*, Atom*, __gnu_cxx::hash<const char*>, CStringEquals> CStringToAtom;
187 class AtomSyncer : public ld::File::AtomHandler {
189 AtomSyncer(std::vector<const char*>& a, std::vector<ld::Atom*>&na,
190 CStringToAtom la, CStringToAtom dla) :
191 additionalUndefines(a), newAtoms(na), llvmAtoms(la), deadllvmAtoms(dla) { }
192 virtual void doAtom(class ld::Atom&);
194 std::vector<const char*>& additionalUndefines;
195 std::vector<ld::Atom*>& newAtoms;
196 CStringToAtom llvmAtoms;
197 CStringToAtom deadllvmAtoms;
200 static std::vector<File*> _s_files;
203 std::vector<File*> Parser::_s_files;
206 const char* Parser::tripletPrefixForArch(cpu_type_t arch)
209 case CPU_TYPE_POWERPC:
211 case CPU_TYPE_POWERPC64:
215 case CPU_TYPE_X86_64:
223 bool Parser::validFile(const uint8_t* fileContent, uint64_t fileLength, cpu_type_t architecture)
225 return ::lto_module_is_object_file_in_memory_for_target(fileContent, fileLength, tripletPrefixForArch(architecture));
228 const char* Parser::fileKind(const uint8_t* p)
230 if ( (p[0] == 0xDE) && (p[1] == 0xC0) && (p[2] == 0x17) && (p[3] == 0x0B) ) {
231 uint32_t arch = LittleEndian::get32(*((uint32_t*)(&p[16])));
233 case CPU_TYPE_POWERPC:
237 case CPU_TYPE_X86_64:
242 return "unknown bitcode architecture";
247 File* Parser::parse(const uint8_t* fileContent, uint64_t fileLength, const char* path, time_t modTime,
248 uint32_t ordinal, cpu_type_t architecture)
250 File* f = new File(path, modTime, fileContent, fileLength, ordinal, architecture);
251 _s_files.push_back(f);
256 ld::relocatable::File* Parser::parseMachOFile(const uint8_t* p, size_t len, uint32_t nextInputOrdinal, cpu_type_t arch)
259 case CPU_TYPE_POWERPC:
260 if ( mach_o::relocatable::Parser<ppc>::validFile(p) )
261 return mach_o::relocatable::Parser<ppc>::parse(p, len, "/tmp/lto.o", 0, nextInputOrdinal);
263 case CPU_TYPE_POWERPC64:
264 if ( mach_o::relocatable::Parser<ppc64>::validFile(p) )
265 return mach_o::relocatable::Parser<ppc64>::parse(p, len, "/tmp/lto.o", 0, nextInputOrdinal);
268 if ( mach_o::relocatable::Parser<x86>::validFile(p) )
269 return mach_o::relocatable::Parser<x86>::parse(p, len, "/tmp/lto.o", 0, nextInputOrdinal);
271 case CPU_TYPE_X86_64:
272 if ( mach_o::relocatable::Parser<x86_64>::validFile(p) )
273 return mach_o::relocatable::Parser<x86_64>::parse(p, len, "/tmp/lto.o", 0, nextInputOrdinal);
276 if ( mach_o::relocatable::Parser<arm>::validFile(p) )
277 return mach_o::relocatable::Parser<arm>::parse(p, len, "/tmp/lto.o", 0, nextInputOrdinal);
280 throw "LLVM LTO, file is not of required architecture";
285 File::File(const char* path, time_t mTime, const uint8_t* content, uint32_t contentLength, uint32_t ordinal, cpu_type_t arch)
286 : ld::relocatable::File(path,mTime,ordinal), _architecture(arch), _internalAtom(*this),
287 _atomArray(NULL), _atomArrayCount(0), _module(NULL),
288 _section("__TEXT_", "__tmp_lto", ld::Section::typeUnclassified)
290 // create llvm module
291 _module = ::lto_module_create_from_memory(content, contentLength);
292 if ( _module == NULL )
293 throwf("could not parse object file %s: %s", path, lto_get_error_message());
295 // create atom for each global symbol in module
296 uint32_t count = ::lto_module_get_num_symbols(_module);
297 _atomArray = (Atom*)malloc(sizeof(Atom)*count);
298 for (uint32_t i=0; i < count; ++i) {
299 const char* name = ::lto_module_get_symbol_name(_module, i);
300 lto_symbol_attributes attr = lto_module_get_symbol_attribute(_module, i);
302 // <rdar://problem/6378110> LTO doesn't like dtrace symbols
303 // ignore dtrace static probes for now
304 // later when codegen is done and a mach-o file is produces the probes will be processed
305 if ( (strncmp(name, "___dtrace_probe$", 16) == 0) || (strncmp(name, "___dtrace_isenabled$", 20) == 0) )
308 ld::Atom::Definition def;
309 switch ( attr & LTO_SYMBOL_DEFINITION_MASK ) {
310 case LTO_SYMBOL_DEFINITION_REGULAR:
311 def = ld::Atom::definitionRegular;
313 case LTO_SYMBOL_DEFINITION_TENTATIVE:
314 def = ld::Atom::definitionTentative;
316 case LTO_SYMBOL_DEFINITION_WEAK:
317 def = ld::Atom::definitionRegular;
319 case LTO_SYMBOL_DEFINITION_UNDEFINED:
320 case LTO_SYMBOL_DEFINITION_WEAKUNDEF:
321 def = ld::Atom::definitionProxy;
324 throwf("unknown definition kind for symbol %s in bitcode file %s", name, path);
327 // make LLVM atoms for definitions and a reference for undefines
328 if ( def != ld::Atom::definitionProxy ) {
329 ld::Atom::Scope scope;
330 switch ( attr & LTO_SYMBOL_SCOPE_MASK) {
331 case LTO_SYMBOL_SCOPE_INTERNAL:
332 scope = ld::Atom::scopeTranslationUnit;
334 case LTO_SYMBOL_SCOPE_HIDDEN:
335 scope = ld::Atom::scopeLinkageUnit;
337 case LTO_SYMBOL_SCOPE_DEFAULT:
338 scope = ld::Atom::scopeGlobal;
341 throwf("unknown scope for symbol %s in bitcode file %s", name, path);
343 // only make atoms for non-internal symbols
344 if ( scope == ld::Atom::scopeTranslationUnit )
346 uint8_t alignment = (attr & LTO_SYMBOL_ALIGNMENT_MASK);
347 // make Atom using placement new operator
348 new (&_atomArray[_atomArrayCount++]) Atom(*this, name, scope, def, alignment);
351 // add to list of external references
352 _internalAtom.addReference(name);
359 if ( _module != NULL )
360 ::lto_module_dispose(_module);
363 bool File::forEachAtom(ld::File::AtomHandler& handler)
365 handler.doAtom(_internalAtom);
366 for(uint32_t i=0; i < _atomArrayCount; ++i) {
367 handler.doAtom(_atomArray[i]);
372 InternalAtom::InternalAtom(File& f)
373 : ld::Atom(f._section, ld::Atom::definitionRegular, ld::Atom::combineNever, ld::Atom::scopeTranslationUnit,
374 ld::Atom::typeLTOtemporary, ld::Atom::symbolTableNotIn, false, false, ld::Atom::Alignment(0)),
379 Atom::Atom(File& f, const char* name, ld::Atom::Scope s, ld::Atom::Definition d, ld::Atom::Alignment a)
380 : ld::Atom(f._section, d, ld::Atom::combineNever, s, ld::Atom::typeLTOtemporary, ld::Atom::symbolTableIn, false, false, a),
381 _file(f), _name(name), _compiledAtom(NULL)
388 bool Parser::optimize(const std::vector<ld::Atom*>& allAtoms, std::vector<ld::Atom*>& newAtoms,
389 std::vector<const char*>& additionalUndefines,
390 const std::set<ld::Atom*>& deadAtoms,
391 std::vector<ld::Atom*>& newlyDeadAtoms,
392 uint32_t nextInputOrdinal,
393 ld::OutFile* writer, ld::Atom* entryPointAtom,
394 const std::vector<const char*>& llvmOptions,
395 bool allGlobalsAReDeadStripRoots,
396 bool verbose, bool saveTemps,
397 const char* outputFilePath,
398 bool pie, bool mainExecutable, bool staticExecutable, bool relocatable,
399 bool allowTextRelocs, cpu_type_t arch)
401 // exit quickly if nothing to do
402 if ( _s_files.size() == 0 )
405 // print out LTO version string if -v was used
407 fprintf(stderr, "%s\n", lto_get_version());
409 // create optimizer and add each Reader
410 lto_code_gen_t generator = ::lto_codegen_create();
411 for (std::vector<File*>::iterator it=_s_files.begin(); it != _s_files.end(); ++it) {
412 if ( ::lto_codegen_add_module(generator, (*it)->module()) )
413 throwf("lto: could not merge in %s because %s", (*it)->path(), ::lto_get_error_message());
416 // add any -mllvm command line options
417 for (std::vector<const char*>::const_iterator it=llvmOptions.begin(); it != llvmOptions.end(); ++it) {
418 ::lto_codegen_debug_options(generator, *it);
421 // The atom graph uses directed edges (references). Collect all references where
422 // originating atom is not part of any LTO Reader. This allows optimizer to optimize an
423 // external (i.e. not originated from same .o file) reference if all originating atoms are also
424 // defined in llvm bitcode file.
425 CStringSet nonLLVMRefs;
426 CStringToAtom llvmAtoms;
427 bool hasNonllvmAtoms = false;
428 for (std::vector<ld::Atom*>::const_iterator it = allAtoms.begin(); it != allAtoms.end(); ++it) {
429 ld::Atom* atom = *it;
430 // only look at references that come from an atom that is not an llvm atom
431 if ( atom->contentType() != ld::Atom::typeLTOtemporary ) {
432 // remember if we've seen any atoms not from an llvm reader and not from the writer
433 // if ( atom->getFile() != writer )
434 // hasNonllvmAtoms = true;
435 for (ld::Fixup::iterator fit=atom->fixupsBegin(); fit != atom->fixupsEnd(); ++fit) {
436 if ( fit->binding != ld::Fixup::bindingByNameBound )
438 // and reference an llvm atom
439 if ( fit->u.target->contentType() == ld::Atom::typeLTOtemporary )
440 nonLLVMRefs.insert(fit->u.target->name());
444 llvmAtoms[atom->name()] = (Atom*)atom;
447 // if entry point is in a llvm bitcode file, it must be preserved by LTO
448 if ( entryPointAtom != NULL ) {
449 if ( entryPointAtom->contentType() == ld::Atom::typeLTOtemporary )
450 nonLLVMRefs.insert(entryPointAtom->name());
453 // deadAtoms are the atoms that the linker coalesced. For instance weak or tentative definitions
454 // overriden by another atom. If any of these deadAtoms are llvm atoms and they were replaced
455 // with a mach-o atom, we need to tell the lto engine to preserve (not optimize away) its dead
456 // atom so that the linker can replace it with the mach-o one later.
457 CStringToAtom deadllvmAtoms;
458 for (std::set<ld::Atom*>::iterator it = deadAtoms.begin(); it != deadAtoms.end(); ++it) {
459 ld::Atom* atom = *it;
460 if ( atom->contentType() == ld::Atom::typeLTOtemporary ) {
461 const char* name = atom->name();
462 ::lto_codegen_add_must_preserve_symbol(generator, name);
463 deadllvmAtoms[name] = (Atom*)atom;
468 // tell code generator about symbols that must be preserved
469 for (CStringToAtom::iterator it = llvmAtoms.begin(); it != llvmAtoms.end(); ++it) {
470 const char* name = it->first;
471 Atom* atom = it->second;
472 // Include llvm Symbol in export list if it meets one of following two conditions
473 // 1 - atom scope is global (and not linkage unit).
474 // 2 - included in nonLLVMRefs set.
475 // If a symbol is not listed in exportList then LTO is free to optimize it away.
476 if ( (atom->scope() == ld::Atom::scopeGlobal) )
477 ::lto_codegen_add_must_preserve_symbol(generator, name);
478 else if ( nonLLVMRefs.find(name) != nonLLVMRefs.end() )
479 ::lto_codegen_add_must_preserve_symbol(generator, name);
482 // special case running ld -r on all bitcode files to produce another bitcode file (instead of mach-o)
483 if ( relocatable && !hasNonllvmAtoms ) {
484 if ( ! ::lto_codegen_write_merged_modules(generator, outputFilePath) ) {
485 // HACK, no good way to tell linker we are all done, so just quit
488 warning("could not produce merged bitcode file");
491 // set code-gen model
492 lto_codegen_model model = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
493 if ( mainExecutable ) {
494 if ( staticExecutable ) {
495 // darwin x86_64 "static" code model is really dynamic code model
496 if ( arch == CPU_TYPE_X86_64 )
497 model = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
499 model = LTO_CODEGEN_PIC_MODEL_STATIC;
503 model = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
505 model = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC;
509 if ( allowTextRelocs )
510 model = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC;
512 model = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
514 if ( ::lto_codegen_set_pic_model(generator, model) )
515 throwf("could not create set codegen model: %s", lto_get_error_message());
517 // if requested, save off merged bitcode file
519 char tempBitcodePath[MAXPATHLEN];
520 strcpy(tempBitcodePath, outputFilePath);
521 strcat(tempBitcodePath, ".lto.bc");
522 ::lto_codegen_write_merged_modules(generator, tempBitcodePath);
525 #if LTO_API_VERSION >= 3
526 // find assembler next to linker
528 uint32_t bufSize = PATH_MAX;
529 if ( _NSGetExecutablePath(path, &bufSize) != -1 ) {
530 char* lastSlash = strrchr(path, '/');
531 if ( lastSlash != NULL ) {
532 strcpy(lastSlash+1, "as");
533 struct stat statInfo;
534 if ( stat(path, &statInfo) == 0 )
535 ::lto_codegen_set_assembler_path(generator, path);
539 // run code generator
541 const uint8_t* machOFile = (uint8_t*)::lto_codegen_compile(generator, &machOFileLen);
542 if ( machOFile == NULL )
543 throwf("could not do LTO codegen: %s", ::lto_get_error_message());
545 // if requested, save off temp mach-o file
547 char tempMachoPath[MAXPATHLEN];
548 strcpy(tempMachoPath, outputFilePath);
549 strcat(tempMachoPath, ".lto.o");
550 int fd = ::open(tempMachoPath, O_CREAT | O_WRONLY | O_TRUNC, 0666);
552 ::write(fd, machOFile, machOFileLen);
555 // save off merged bitcode file
556 char tempOptBitcodePath[MAXPATHLEN];
557 strcpy(tempOptBitcodePath, outputFilePath);
558 strcat(tempOptBitcodePath, ".lto.opt.bc");
559 ::lto_codegen_write_merged_modules(generator, tempOptBitcodePath);
562 // parse generated mach-o file into a MachOReader
563 ld::File* machoFile = parseMachOFile(machOFile, machOFileLen, nextInputOrdinal, arch);
565 // sync generated mach-o atoms with existing atoms ld knows about
566 AtomSyncer syncer(additionalUndefines,newAtoms,llvmAtoms,deadllvmAtoms);
567 machoFile->forEachAtom(syncer);
569 // Remove InternalAtoms from ld
570 for (std::vector<File*>::iterator it=_s_files.begin(); it != _s_files.end(); ++it) {
571 newlyDeadAtoms.push_back(&((*it)->internalAtom()));
573 // Remove Atoms from ld if code generator optimized them away
574 for (CStringToAtom::iterator li = llvmAtoms.begin(), le = llvmAtoms.end(); li != le; ++li) {
575 // check if setRealAtom() called on this Atom
576 if ( li->second->compiledAtom() == NULL )
577 newlyDeadAtoms.push_back(li->second);
584 void Parser::AtomSyncer::doAtom(ld::Atom& machoAtom)
586 // update proxy atoms to point to real atoms and find new atoms
587 const char* name = machoAtom.name();
588 if ( machoAtom.scope() >= ld::Atom::scopeLinkageUnit ) {
589 CStringToAtom::iterator pos = llvmAtoms.find(name);
590 if ( pos != llvmAtoms.end() ) {
591 // turn Atom into a proxy for this mach-o atom
592 pos->second->setCompiledAtom(machoAtom);
595 // an atom of this name was not in the allAtoms list the linker gave us
596 if ( deadllvmAtoms.find(name) != deadllvmAtoms.end() ) {
597 // this corresponding to an atom that the linker coalesced away.
598 // Don't pass it back as a new atom
602 // this is something new that lto conjured up, tell ld its new
603 newAtoms.push_back(&machoAtom);
608 // ld only knew about non-satic atoms, so this one must be new
609 newAtoms.push_back(&machoAtom);
612 // adjust fixups to go through proxy atoms
613 for (ld::Fixup::iterator fit=machoAtom.fixupsBegin(); fit != machoAtom.fixupsEnd(); ++fit) {
614 switch ( fit->binding ) {
615 case ld::Fixup::bindingNone:
617 case ld::Fixup::bindingByNameUnbound:
618 // don't know if this target has been seen by linker before or if it is new
619 // be conservitive and tell linker it is new
620 additionalUndefines.push_back(fit->u.name);
622 case ld::Fixup::bindingByNameBound:
624 case ld::Fixup::bindingDirectlyBound:
625 // If mach-o atom is referencing another mach-o atom then
626 // reference is not going through Atom proxy. Fix it here to ensure that all
627 // llvm symbol references always go through Atom proxy.
629 case ld::Fixup::bindingByContentBound: