blob: 8c2352319ec95638aa837b3bd8edd25fdb6e40ac [file] [log] [blame]
Rui Ueyama9e568392013-05-28 18:13:31 +00001//===- 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 Ueyama991f42c2013-06-19 17:46:57 +000010#include "GroupedSectionsPass.h"
Rui Ueyamac8a53792013-07-11 08:46:21 +000011#include "IdataPass.h"
Rui Ueyama9e568392013-05-28 18:13:31 +000012
Rui Ueyama2897feb2013-07-19 02:18:25 +000013#include "llvm/ADT/SmallString.h"
14#include "llvm/Support/Path.h"
Rui Ueyamad95a1552013-06-17 16:59:54 +000015#include "lld/Core/PassManager.h"
16#include "lld/Passes/LayoutPass.h"
Rui Ueyama991f42c2013-06-19 17:46:57 +000017#include "lld/ReaderWriter/PECOFFTargetInfo.h"
Rui Ueyama9e568392013-05-28 18:13:31 +000018#include "lld/ReaderWriter/Reader.h"
19#include "lld/ReaderWriter/Writer.h"
20
Rui Ueyama9e568392013-05-28 18:13:31 +000021namespace lld {
22
Rui Ueyama2897feb2013-07-19 02:18:25 +000023namespace {
24bool 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 Ueyama9e568392013-05-28 18:13:31 +000031error_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 Espindolac1b32682013-06-11 12:36:05 +000037bool PECOFFTargetInfo::validateImpl(raw_ostream &diagnostics) {
Rui Ueyamaeb0cc962013-06-08 03:59:00 +000038 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 Ueyama9dd08d92013-06-08 22:59:10 +000045 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 Ueyama9e568392013-05-28 18:13:31 +000052 _reader = createReaderPECOFF(*this);
53 _writer = createWriterPECOFF(*this);
54 return false;
55}
56
Rui Ueyama2897feb2013-07-19 02:18:25 +000057/// Append the given file to the input file list. The file must be an object
58/// file or an import library file.
59bool 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.
80bool 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 Ueyama9e568392013-05-28 18:13:31 +000098Writer &PECOFFTargetInfo::writer() const {
99 return *_writer;
100}
101
102ErrorOr<Reference::Kind>
103PECOFFTargetInfo::relocKindFromString(StringRef str) const {
104 return make_error_code(yaml_reader_error::illegal_value);
105}
106
107ErrorOr<std::string>
108PECOFFTargetInfo::stringFromRelocKind(Reference::Kind kind) const {
109 return make_error_code(yaml_reader_error::illegal_value);
110}
111
Rui Ueyamad95a1552013-06-17 16:59:54 +0000112void PECOFFTargetInfo::addPasses(PassManager &pm) const {
Rui Ueyama991f42c2013-06-19 17:46:57 +0000113 pm.add(std::unique_ptr<Pass>(new pecoff::GroupedSectionsPass()));
Rui Ueyamac8a53792013-07-11 08:46:21 +0000114 pm.add(std::unique_ptr<Pass>(new pecoff::IdataPass()));
Rui Ueyamad95a1552013-06-17 16:59:54 +0000115 pm.add(std::unique_ptr<Pass>(new LayoutPass()));
116}
117
Rui Ueyama9e568392013-05-28 18:13:31 +0000118} // end namespace lld