| Rui Ueyama | 9e56839 | 2013-05-28 18:13:31 +0000 | [diff] [blame] | 1 | //===- lib/ReaderWriter/PECOFF/PECOFFTargetInfo.cpp -----------------------===// | 
|  | 2 | // | 
|  | 3 | //                             The LLVM Linker | 
|  | 4 | // | 
|  | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
|  | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 |  | 
| Rui Ueyama | 991f42c | 2013-06-19 17:46:57 +0000 | [diff] [blame] | 10 | #include "GroupedSectionsPass.h" | 
| Rui Ueyama | c8a5379 | 2013-07-11 08:46:21 +0000 | [diff] [blame] | 11 | #include "IdataPass.h" | 
| Rui Ueyama | 9e56839 | 2013-05-28 18:13:31 +0000 | [diff] [blame] | 12 |  | 
| Rui Ueyama | 2897feb | 2013-07-19 02:18:25 +0000 | [diff] [blame^] | 13 | #include "llvm/ADT/SmallString.h" | 
|  | 14 | #include "llvm/Support/Path.h" | 
| Rui Ueyama | d95a155 | 2013-06-17 16:59:54 +0000 | [diff] [blame] | 15 | #include "lld/Core/PassManager.h" | 
|  | 16 | #include "lld/Passes/LayoutPass.h" | 
| Rui Ueyama | 991f42c | 2013-06-19 17:46:57 +0000 | [diff] [blame] | 17 | #include "lld/ReaderWriter/PECOFFTargetInfo.h" | 
| Rui Ueyama | 9e56839 | 2013-05-28 18:13:31 +0000 | [diff] [blame] | 18 | #include "lld/ReaderWriter/Reader.h" | 
|  | 19 | #include "lld/ReaderWriter/Writer.h" | 
|  | 20 |  | 
| Rui Ueyama | 9e56839 | 2013-05-28 18:13:31 +0000 | [diff] [blame] | 21 | namespace lld { | 
|  | 22 |  | 
| Rui Ueyama | 2897feb | 2013-07-19 02:18:25 +0000 | [diff] [blame^] | 23 | namespace { | 
|  | 24 | bool containDirectoryName(StringRef path) { | 
|  | 25 | SmallString<128> smallStr = StringRef(path); | 
|  | 26 | llvm::sys::path::remove_filename(smallStr); | 
|  | 27 | return !smallStr.str().empty(); | 
|  | 28 | } | 
|  | 29 | } // anonymous namespace | 
|  | 30 |  | 
| Rui Ueyama | 9e56839 | 2013-05-28 18:13:31 +0000 | [diff] [blame] | 31 | error_code PECOFFTargetInfo::parseFile( | 
|  | 32 | std::unique_ptr<MemoryBuffer> &mb, | 
|  | 33 | std::vector<std::unique_ptr<File>> &result) const { | 
|  | 34 | return _reader->parseFile(mb, result); | 
|  | 35 | } | 
|  | 36 |  | 
| Rafael Espindola | c1b3268 | 2013-06-11 12:36:05 +0000 | [diff] [blame] | 37 | bool PECOFFTargetInfo::validateImpl(raw_ostream &diagnostics) { | 
| Rui Ueyama | eb0cc96 | 2013-06-08 03:59:00 +0000 | [diff] [blame] | 38 | if (_stackReserve < _stackCommit) { | 
|  | 39 | diagnostics << "Invalid stack size: reserve size must be equal to or " | 
|  | 40 | << "greater than commit size, but got " | 
|  | 41 | << _stackCommit << " and " << _stackReserve << ".\n"; | 
|  | 42 | return true; | 
|  | 43 | } | 
|  | 44 |  | 
| Rui Ueyama | 9dd08d9 | 2013-06-08 22:59:10 +0000 | [diff] [blame] | 45 | if (_heapReserve < _heapCommit) { | 
|  | 46 | diagnostics << "Invalid heap size: reserve size must be equal to or " | 
|  | 47 | << "greater than commit size, but got " | 
|  | 48 | << _heapCommit << " and " << _heapReserve << ".\n"; | 
|  | 49 | return true; | 
|  | 50 | } | 
|  | 51 |  | 
| Rui Ueyama | 9e56839 | 2013-05-28 18:13:31 +0000 | [diff] [blame] | 52 | _reader = createReaderPECOFF(*this); | 
|  | 53 | _writer = createWriterPECOFF(*this); | 
|  | 54 | return false; | 
|  | 55 | } | 
|  | 56 |  | 
| Rui Ueyama | 2897feb | 2013-07-19 02:18:25 +0000 | [diff] [blame^] | 57 | /// Append the given file to the input file list. The file must be an object | 
|  | 58 | /// file or an import library file. | 
|  | 59 | bool PECOFFTargetInfo::appendInputFileOrLibrary(std::string path) { | 
|  | 60 | StringRef ext = llvm::sys::path::extension(path).lower(); | 
|  | 61 | // This is an import library file. Look for the library file in the search | 
|  | 62 | // paths, unless the path contains a directory name. | 
|  | 63 | if (ext == ".lib") { | 
|  | 64 | if (containDirectoryName(path)) { | 
|  | 65 | appendInputFile(path); | 
|  | 66 | return true; | 
|  | 67 | } | 
|  | 68 | return appendLibraryFile(path); | 
|  | 69 | } | 
|  | 70 | // This is an object file otherwise. Add ".obj" extension if the given path | 
|  | 71 | // name has no file extension. | 
|  | 72 | if (ext.empty()) | 
|  | 73 | path.append(".obj"); | 
|  | 74 | appendInputFile(allocateString(path)); | 
|  | 75 | return true; | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | /// Try to find the input library file from the search paths and append it to | 
|  | 79 | /// the input file list. Returns true if the library file is found. | 
|  | 80 | bool PECOFFTargetInfo::appendLibraryFile(StringRef filename) { | 
|  | 81 | // Current directory always takes precedence over the search paths. | 
|  | 82 | if (llvm::sys::fs::exists(filename)) { | 
|  | 83 | appendInputFile(filename); | 
|  | 84 | return true; | 
|  | 85 | } | 
|  | 86 | // Iterate over the search paths. | 
|  | 87 | for (StringRef dir : _inputSearchPaths) { | 
|  | 88 | SmallString<128> path = dir; | 
|  | 89 | llvm::sys::path::append(path, filename); | 
|  | 90 | if (llvm::sys::fs::exists(path.str())) { | 
|  | 91 | appendInputFile(allocateString(path.str())); | 
|  | 92 | return true; | 
|  | 93 | } | 
|  | 94 | } | 
|  | 95 | return false; | 
|  | 96 | } | 
|  | 97 |  | 
| Rui Ueyama | 9e56839 | 2013-05-28 18:13:31 +0000 | [diff] [blame] | 98 | Writer &PECOFFTargetInfo::writer() const { | 
|  | 99 | return *_writer; | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | ErrorOr<Reference::Kind> | 
|  | 103 | PECOFFTargetInfo::relocKindFromString(StringRef str) const { | 
|  | 104 | return make_error_code(yaml_reader_error::illegal_value); | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | ErrorOr<std::string> | 
|  | 108 | PECOFFTargetInfo::stringFromRelocKind(Reference::Kind kind) const { | 
|  | 109 | return make_error_code(yaml_reader_error::illegal_value); | 
|  | 110 | } | 
|  | 111 |  | 
| Rui Ueyama | d95a155 | 2013-06-17 16:59:54 +0000 | [diff] [blame] | 112 | void PECOFFTargetInfo::addPasses(PassManager &pm) const { | 
| Rui Ueyama | 991f42c | 2013-06-19 17:46:57 +0000 | [diff] [blame] | 113 | pm.add(std::unique_ptr<Pass>(new pecoff::GroupedSectionsPass())); | 
| Rui Ueyama | c8a5379 | 2013-07-11 08:46:21 +0000 | [diff] [blame] | 114 | pm.add(std::unique_ptr<Pass>(new pecoff::IdataPass())); | 
| Rui Ueyama | d95a155 | 2013-06-17 16:59:54 +0000 | [diff] [blame] | 115 | pm.add(std::unique_ptr<Pass>(new LayoutPass())); | 
|  | 116 | } | 
|  | 117 |  | 
| Rui Ueyama | 9e56839 | 2013-05-28 18:13:31 +0000 | [diff] [blame] | 118 | } // end namespace lld |