blob: d0c72c31a7770ae33e80a18ff651490adcf1f675 [file] [log] [blame]
Rui Ueyama0ca149f2013-08-06 22:31:59 +00001//===- lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp -------------------===//
Rui Ueyama9e568392013-05-28 18:13:31 +00002//
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 Ueyamafd502832013-07-24 22:53:23 +000010#include "Atoms.h"
Rui Ueyama991f42c2013-06-19 17:46:57 +000011#include "GroupedSectionsPass.h"
Rui Ueyamac8a53792013-07-11 08:46:21 +000012#include "IdataPass.h"
Rui Ueyama908606d2013-08-09 04:44:15 +000013#include "LinkerGeneratedSymbolFile.h"
Rui Ueyama1a11b3b2013-11-25 02:00:00 +000014#include "SetSubsystemPass.h"
Rui Ueyama9e568392013-05-28 18:13:31 +000015
Rui Ueyamad95a1552013-06-17 16:59:54 +000016#include "lld/Core/PassManager.h"
17#include "lld/Passes/LayoutPass.h"
Rui Ueyamac9752fa2013-11-01 19:52:37 +000018#include "lld/Passes/RoundTripNativePass.h"
19#include "lld/Passes/RoundTripYAMLPass.h"
Rui Ueyama0ca149f2013-08-06 22:31:59 +000020#include "lld/ReaderWriter/PECOFFLinkingContext.h"
Rui Ueyama9e568392013-05-28 18:13:31 +000021#include "lld/ReaderWriter/Reader.h"
Rui Ueyamafd502832013-07-24 22:53:23 +000022#include "lld/ReaderWriter/Simple.h"
Rui Ueyama9e568392013-05-28 18:13:31 +000023#include "lld/ReaderWriter/Writer.h"
Rui Ueyamac9752fa2013-11-01 19:52:37 +000024#include "llvm/ADT/SmallString.h"
25#include "llvm/Support/Allocator.h"
26#include "llvm/Support/Path.h"
Rui Ueyama9e568392013-05-28 18:13:31 +000027
Rui Ueyama91491812013-09-23 19:52:35 +000028#include <bitset>
Rui Ueyama863931c2013-10-26 00:46:57 +000029#include <set>
Rui Ueyama91491812013-09-23 19:52:35 +000030
Rui Ueyama9e568392013-05-28 18:13:31 +000031namespace lld {
32
Rui Ueyama0ca149f2013-08-06 22:31:59 +000033bool PECOFFLinkingContext::validateImpl(raw_ostream &diagnostics) {
Rui Ueyamaeb0cc962013-06-08 03:59:00 +000034 if (_stackReserve < _stackCommit) {
35 diagnostics << "Invalid stack size: reserve size must be equal to or "
Rui Ueyama0ca149f2013-08-06 22:31:59 +000036 << "greater than commit size, but got " << _stackCommit
37 << " and " << _stackReserve << ".\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +000038 return false;
Rui Ueyamaeb0cc962013-06-08 03:59:00 +000039 }
40
Rui Ueyama9dd08d92013-06-08 22:59:10 +000041 if (_heapReserve < _heapCommit) {
42 diagnostics << "Invalid heap size: reserve size must be equal to or "
Rui Ueyama0ca149f2013-08-06 22:31:59 +000043 << "greater than commit size, but got " << _heapCommit
44 << " and " << _heapReserve << ".\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +000045 return false;
Rui Ueyama9dd08d92013-06-08 22:59:10 +000046 }
47
Rui Ueyama530488c2013-09-03 22:57:00 +000048 // It's an error if the base address is not multiple of 64K.
49 if (_baseAddress & 0xffff) {
50 diagnostics << "Base address have to be multiple of 64K, but got "
51 << _baseAddress << "\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +000052 return false;
Rui Ueyama530488c2013-09-03 22:57:00 +000053 }
54
Rui Ueyama41b99dc2013-11-06 19:30:14 +000055 std::bitset<64> alignment(_sectionDefaultAlignment);
Rui Ueyama91491812013-09-23 19:52:35 +000056 if (alignment.count() != 1) {
57 diagnostics << "Section alignment must be a power of 2, but got "
Rui Ueyama41b99dc2013-11-06 19:30:14 +000058 << _sectionDefaultAlignment << "\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +000059 return false;
Rui Ueyama91491812013-09-23 19:52:35 +000060 }
61
Rui Ueyama98896ed2013-09-12 19:46:53 +000062 // Architectures other than i386 is not supported yet.
63 if (_machineType != llvm::COFF::IMAGE_FILE_MACHINE_I386) {
64 diagnostics << "Machine type other than x86 is not supported.\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +000065 return false;
Rui Ueyama98896ed2013-09-12 19:46:53 +000066 }
67
Rui Ueyama9e568392013-05-28 18:13:31 +000068 _reader = createReaderPECOFF(*this);
69 _writer = createWriterPECOFF(*this);
Rui Ueyama8db1edd2013-09-24 23:26:34 +000070 return true;
Rui Ueyama9e568392013-05-28 18:13:31 +000071}
72
Shankar Easwarana96f3a32013-10-07 02:47:09 +000073std::unique_ptr<File> PECOFFLinkingContext::createEntrySymbolFile() const {
Shankar Easwarand26c8e32013-08-31 05:27:38 +000074 if (entrySymbolName().empty())
75 return nullptr;
76 std::unique_ptr<SimpleFile> entryFile(
77 new SimpleFile(*this, "command line option /entry"));
78 entryFile->addAtom(
79 *(new (_allocator) SimpleUndefinedAtom(*entryFile, entrySymbolName())));
80 return std::move(entryFile);
81}
Rui Ueyama908606d2013-08-09 04:44:15 +000082
Shankar Easwarana96f3a32013-10-07 02:47:09 +000083std::unique_ptr<File> PECOFFLinkingContext::createUndefinedSymbolFile() const {
Shankar Easwarand26c8e32013-08-31 05:27:38 +000084 if (_initialUndefinedSymbols.empty())
85 return nullptr;
86 std::unique_ptr<SimpleFile> undefinedSymFile(
87 new SimpleFile(*this, "command line option /c (or) /include"));
88 for (auto undefSymStr : _initialUndefinedSymbols)
89 undefinedSymFile->addAtom(*(new (_allocator) SimpleUndefinedAtom(
90 *undefinedSymFile, undefSymStr)));
91 return std::move(undefinedSymFile);
92}
93
Shankar Easwarana96f3a32013-10-07 02:47:09 +000094bool PECOFFLinkingContext::createImplicitFiles(
95 std::vector<std::unique_ptr<File> > &) const {
96 std::unique_ptr<SimpleFileNode> fileNode(
97 new SimpleFileNode("Implicit Files"));
98 std::unique_ptr<File> linkerGeneratedSymFile(
99 new coff::LinkerGeneratedSymbolFile(*this));
100 fileNode->appendInputFile(std::move(linkerGeneratedSymFile));
101 inputGraph().insertOneElementAt(std::move(fileNode),
102 InputGraph::Position::END);
103 return true;
Rui Ueyamafd502832013-07-24 22:53:23 +0000104}
105
Rui Ueyama863931c2013-10-26 00:46:57 +0000106/// Returns the section name in the resulting executable.
107///
108/// Sections in object files are usually output to the executable with the same
109/// name, but you can rename by command line option. /merge:from=to makes the
110/// linker to combine "from" section contents to "to" section in the
111/// executable. We have a mapping for the renaming. This method looks up the
112/// table and returns a new section name if renamed.
113StringRef
114PECOFFLinkingContext::getFinalSectionName(StringRef sectionName) const {
115 auto it = _renamedSections.find(sectionName);
116 if (it == _renamedSections.end())
117 return sectionName;
118 return getFinalSectionName(it->second);
119}
120
121/// Adds a mapping to the section renaming table. This method will be used for
122/// /merge command line option.
123bool PECOFFLinkingContext::addSectionRenaming(raw_ostream &diagnostics,
124 StringRef from, StringRef to) {
125 auto it = _renamedSections.find(from);
126 if (it != _renamedSections.end()) {
127 if (it->second == to)
128 // There's already the same mapping.
129 return true;
130 diagnostics << "Section \"" << from << "\" is already mapped to \""
131 << it->second << ", so it cannot be mapped to \"" << to << "\".";
132 return true;
133 }
134
135 // Add a mapping, and check if there's no cycle in the renaming mapping. The
136 // cycle detection algorithm we use here is naive, but that's OK because the
137 // number of mapping is usually less than 10.
138 _renamedSections[from] = to;
139 for (auto elem : _renamedSections) {
140 StringRef sectionName = elem.first;
141 std::set<StringRef> visited;
142 visited.insert(sectionName);
143 for (;;) {
Nick Kledzik3df81042013-11-06 21:30:15 +0000144 auto pos = _renamedSections.find(sectionName);
145 if (pos == _renamedSections.end())
Rui Ueyama863931c2013-10-26 00:46:57 +0000146 break;
Nick Kledzik3df81042013-11-06 21:30:15 +0000147 if (visited.count(pos->second)) {
Rui Ueyama863931c2013-10-26 00:46:57 +0000148 diagnostics << "/merge:" << from << "=" << to << " makes a cycle";
149 return false;
150 }
Nick Kledzik3df81042013-11-06 21:30:15 +0000151 sectionName = pos->second;
Rui Ueyama863931c2013-10-26 00:46:57 +0000152 visited.insert(sectionName);
153 }
154 }
155 return true;
156}
157
Rui Ueyama2897feb2013-07-19 02:18:25 +0000158/// Try to find the input library file from the search paths and append it to
159/// the input file list. Returns true if the library file is found.
Shankar Easwarane44104b2013-08-21 22:57:10 +0000160StringRef PECOFFLinkingContext::searchLibraryFile(StringRef filename) const {
Rui Ueyama2897feb2013-07-19 02:18:25 +0000161 // Current directory always takes precedence over the search paths.
Shankar Easwarane44104b2013-08-21 22:57:10 +0000162 if (llvm::sys::path::is_absolute(filename) || llvm::sys::fs::exists(filename))
163 return filename;
Rui Ueyama2897feb2013-07-19 02:18:25 +0000164 // Iterate over the search paths.
165 for (StringRef dir : _inputSearchPaths) {
166 SmallString<128> path = dir;
167 llvm::sys::path::append(path, filename);
Shankar Easwarane44104b2013-08-21 22:57:10 +0000168 if (llvm::sys::fs::exists(path.str()))
Rui Ueyama90bcd112013-11-21 00:17:31 +0000169 return allocate(path.str());
Rui Ueyama2897feb2013-07-19 02:18:25 +0000170 }
Shankar Easwarane44104b2013-08-21 22:57:10 +0000171 return filename;
Rui Ueyama2897feb2013-07-19 02:18:25 +0000172}
173
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000174Writer &PECOFFLinkingContext::writer() const { return *_writer; }
Rui Ueyama9e568392013-05-28 18:13:31 +0000175
176ErrorOr<Reference::Kind>
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000177PECOFFLinkingContext::relocKindFromString(StringRef str) const {
Rui Ueyamae1c30a42013-11-06 04:47:19 +0000178#define LLD_CASE(name) .Case(#name, llvm::COFF::name)
Shankar Easwaran53bae6f2013-10-26 19:38:31 +0000179 int32_t ret = llvm::StringSwitch<int32_t>(str)
180 LLD_CASE(IMAGE_REL_I386_ABSOLUTE)
181 LLD_CASE(IMAGE_REL_I386_DIR32)
182 LLD_CASE(IMAGE_REL_I386_DIR32NB)
183 LLD_CASE(IMAGE_REL_I386_REL32)
184 .Default(-1);
Rui Ueyamae1c30a42013-11-06 04:47:19 +0000185#undef LLD_CASE
Shankar Easwaran53bae6f2013-10-26 19:38:31 +0000186 if (ret == -1)
187 return make_error_code(YamlReaderError::illegal_value);
188 return ret;
Rui Ueyama9e568392013-05-28 18:13:31 +0000189}
190
191ErrorOr<std::string>
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000192PECOFFLinkingContext::stringFromRelocKind(Reference::Kind kind) const {
Shankar Easwaran53bae6f2013-10-26 19:38:31 +0000193 switch (kind) {
Rui Ueyamae1c30a42013-11-06 04:47:19 +0000194#define LLD_CASE(name) \
195 case llvm::COFF::name: \
196 return std::string(#name);
197
Shankar Easwaran53bae6f2013-10-26 19:38:31 +0000198 LLD_CASE(IMAGE_REL_I386_ABSOLUTE)
199 LLD_CASE(IMAGE_REL_I386_DIR32)
200 LLD_CASE(IMAGE_REL_I386_DIR32NB)
201 LLD_CASE(IMAGE_REL_I386_REL32)
Rui Ueyamae1c30a42013-11-06 04:47:19 +0000202#undef LLD_CASE
Shankar Easwaran53bae6f2013-10-26 19:38:31 +0000203 }
Rui Ueyamac6015f62013-10-09 00:57:22 +0000204 return make_error_code(YamlReaderError::illegal_value);
Rui Ueyama9e568392013-05-28 18:13:31 +0000205}
206
Shankar Easwaran2bc24922013-10-29 05:12:14 +0000207void PECOFFLinkingContext::addPasses(PassManager &pm) {
Rui Ueyama1a11b3b2013-11-25 02:00:00 +0000208 pm.add(std::unique_ptr<Pass>(new pecoff::SetSubsystemPass(*this)));
Rui Ueyama991f42c2013-06-19 17:46:57 +0000209 pm.add(std::unique_ptr<Pass>(new pecoff::GroupedSectionsPass()));
Rui Ueyama3ee2bf62013-09-15 22:33:15 +0000210 pm.add(std::unique_ptr<Pass>(new pecoff::IdataPass(*this)));
Rui Ueyamad95a1552013-06-17 16:59:54 +0000211 pm.add(std::unique_ptr<Pass>(new LayoutPass()));
212}
Rui Ueyama9e568392013-05-28 18:13:31 +0000213} // end namespace lld