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