blob: d9117d05a5e73521382ced897611785a8bf531e7 [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 Ueyamac91c24e32013-12-13 06:58:27 +000011#include "EdataPass.h"
Rui Ueyama991f42c2013-06-19 17:46:57 +000012#include "GroupedSectionsPass.h"
Rui Ueyamac8a53792013-07-11 08:46:21 +000013#include "IdataPass.h"
Rui Ueyama908606d2013-08-09 04:44:15 +000014#include "LinkerGeneratedSymbolFile.h"
Rui Ueyama2e09d932014-02-26 08:27:59 +000015#include "LoadConfigPass.h"
Rui Ueyama1a11b3b2013-11-25 02:00:00 +000016#include "SetSubsystemPass.h"
Rui Ueyama9e568392013-05-28 18:13:31 +000017
Rui Ueyamad95a1552013-06-17 16:59:54 +000018#include "lld/Core/PassManager.h"
19#include "lld/Passes/LayoutPass.h"
Rui Ueyamac9752fa2013-11-01 19:52:37 +000020#include "lld/Passes/RoundTripNativePass.h"
21#include "lld/Passes/RoundTripYAMLPass.h"
Rui Ueyama0ca149f2013-08-06 22:31:59 +000022#include "lld/ReaderWriter/PECOFFLinkingContext.h"
Rui Ueyama9e568392013-05-28 18:13:31 +000023#include "lld/ReaderWriter/Reader.h"
Rui Ueyamafd502832013-07-24 22:53:23 +000024#include "lld/ReaderWriter/Simple.h"
Rui Ueyama9e568392013-05-28 18:13:31 +000025#include "lld/ReaderWriter/Writer.h"
Rui Ueyamac9752fa2013-11-01 19:52:37 +000026#include "llvm/ADT/SmallString.h"
27#include "llvm/Support/Allocator.h"
28#include "llvm/Support/Path.h"
Rui Ueyama9e568392013-05-28 18:13:31 +000029
Rui Ueyama91491812013-09-23 19:52:35 +000030#include <bitset>
Rui Ueyama4af032d2013-12-20 10:02:59 +000031#include <climits>
Rui Ueyama863931c2013-10-26 00:46:57 +000032#include <set>
Rui Ueyama91491812013-09-23 19:52:35 +000033
Rui Ueyama9e568392013-05-28 18:13:31 +000034namespace lld {
35
Rui Ueyama0ca149f2013-08-06 22:31:59 +000036bool PECOFFLinkingContext::validateImpl(raw_ostream &diagnostics) {
Rui Ueyamaeb0cc962013-06-08 03:59:00 +000037 if (_stackReserve < _stackCommit) {
38 diagnostics << "Invalid stack size: reserve size must be equal to or "
Rui Ueyama0ca149f2013-08-06 22:31:59 +000039 << "greater than commit size, but got " << _stackCommit
40 << " and " << _stackReserve << ".\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +000041 return false;
Rui Ueyamaeb0cc962013-06-08 03:59:00 +000042 }
43
Rui Ueyama9dd08d92013-06-08 22:59:10 +000044 if (_heapReserve < _heapCommit) {
45 diagnostics << "Invalid heap size: reserve size must be equal to or "
Rui Ueyama0ca149f2013-08-06 22:31:59 +000046 << "greater than commit size, but got " << _heapCommit
47 << " and " << _heapReserve << ".\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +000048 return false;
Rui Ueyama9dd08d92013-06-08 22:59:10 +000049 }
50
Rui Ueyama530488c2013-09-03 22:57:00 +000051 // It's an error if the base address is not multiple of 64K.
Rui Ueyamaea7e9302014-01-31 04:49:13 +000052 if (getBaseAddress() & 0xffff) {
Rui Ueyama530488c2013-09-03 22:57:00 +000053 diagnostics << "Base address have to be multiple of 64K, but got "
Rui Ueyamaea7e9302014-01-31 04:49:13 +000054 << getBaseAddress() << "\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +000055 return false;
Rui Ueyama530488c2013-09-03 22:57:00 +000056 }
57
Rui Ueyamaa6fddab2013-12-16 09:15:58 +000058 // Check for duplicate export ordinals.
59 std::set<int> exports;
60 for (const PECOFFLinkingContext::ExportDesc &desc : getDllExports()) {
61 if (desc.ordinal == -1)
62 continue;
63 if (exports.count(desc.ordinal) == 1) {
64 diagnostics << "Duplicate export ordinals: " << desc.ordinal << "\n";
65 return false;
66 }
67 exports.insert(desc.ordinal);
68 }
69
Rui Ueyama1710fe7d2014-02-27 00:05:43 +000070 // Check for /align.
Rui Ueyama41b99dc2013-11-06 19:30:14 +000071 std::bitset<64> alignment(_sectionDefaultAlignment);
Rui Ueyama91491812013-09-23 19:52:35 +000072 if (alignment.count() != 1) {
73 diagnostics << "Section alignment must be a power of 2, but got "
Rui Ueyama41b99dc2013-11-06 19:30:14 +000074 << _sectionDefaultAlignment << "\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +000075 return false;
Rui Ueyama91491812013-09-23 19:52:35 +000076 }
77
Rui Ueyama1710fe7d2014-02-27 00:05:43 +000078 // /safeseh is only valid for x86.
79 if (getMachineType() != llvm::COFF::IMAGE_FILE_MACHINE_I386 && noSEH()) {
80 diagnostics << "/SAFESEH:NO is only valid for x86.\n";
81 return false;
82 }
83
Rui Ueyama49bfd502014-01-24 19:17:05 +000084 // Architectures other than x86/x64 is not supported yet.
85 if (_machineType != llvm::COFF::IMAGE_FILE_MACHINE_I386 &&
86 _machineType != llvm::COFF::IMAGE_FILE_MACHINE_AMD64) {
87 diagnostics << "Machine type other than x86/x64 is not supported.\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +000088 return false;
Rui Ueyama98896ed2013-09-12 19:46:53 +000089 }
90
Rui Ueyama9e568392013-05-28 18:13:31 +000091 _writer = createWriterPECOFF(*this);
Rui Ueyama8db1edd2013-09-24 23:26:34 +000092 return true;
Rui Ueyama9e568392013-05-28 18:13:31 +000093}
94
Shankar Easwarana96f3a32013-10-07 02:47:09 +000095std::unique_ptr<File> PECOFFLinkingContext::createEntrySymbolFile() const {
Rui Ueyama3907f2a2014-03-28 19:02:06 +000096 return LinkingContext::createEntrySymbolFile("<command line option /entry>");
Shankar Easwarand26c8e32013-08-31 05:27:38 +000097}
Rui Ueyama908606d2013-08-09 04:44:15 +000098
Shankar Easwarana96f3a32013-10-07 02:47:09 +000099std::unique_ptr<File> PECOFFLinkingContext::createUndefinedSymbolFile() const {
Rui Ueyama3907f2a2014-03-28 19:02:06 +0000100 return LinkingContext::createUndefinedSymbolFile(
101 "<command line option /include>");
Shankar Easwarand26c8e32013-08-31 05:27:38 +0000102}
103
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000104bool PECOFFLinkingContext::createImplicitFiles(
105 std::vector<std::unique_ptr<File> > &) const {
106 std::unique_ptr<SimpleFileNode> fileNode(
107 new SimpleFileNode("Implicit Files"));
108 std::unique_ptr<File> linkerGeneratedSymFile(
Rui Ueyama091071f2013-12-13 02:58:27 +0000109 new pecoff::LinkerGeneratedSymbolFile(*this));
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000110 fileNode->appendInputFile(std::move(linkerGeneratedSymFile));
Rui Ueyama8bd093b2014-04-04 00:14:04 +0000111 getInputGraph().insertElementAt(std::move(fileNode),
112 InputGraph::Position::END);
Rui Ueyama72d57ab2014-04-29 00:32:00 +0000113
114 std::unique_ptr<SimpleFileNode> impFileNode(new SimpleFileNode("imp"));
115 impFileNode->appendInputFile(
116 std::unique_ptr<File>(new pecoff::LocallyImportedSymbolFile(*this)));
117 getLibraryGroup()->addFile(std::move(impFileNode));
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000118 return true;
Rui Ueyamafd502832013-07-24 22:53:23 +0000119}
120
Rui Ueyama863931c2013-10-26 00:46:57 +0000121/// Returns the section name in the resulting executable.
122///
123/// Sections in object files are usually output to the executable with the same
124/// name, but you can rename by command line option. /merge:from=to makes the
125/// linker to combine "from" section contents to "to" section in the
126/// executable. We have a mapping for the renaming. This method looks up the
127/// table and returns a new section name if renamed.
128StringRef
Rui Ueyama951dd1d2013-11-27 18:03:31 +0000129PECOFFLinkingContext::getOutputSectionName(StringRef sectionName) const {
Rui Ueyama863931c2013-10-26 00:46:57 +0000130 auto it = _renamedSections.find(sectionName);
131 if (it == _renamedSections.end())
132 return sectionName;
Rui Ueyama951dd1d2013-11-27 18:03:31 +0000133 return getOutputSectionName(it->second);
Rui Ueyama863931c2013-10-26 00:46:57 +0000134}
135
136/// Adds a mapping to the section renaming table. This method will be used for
137/// /merge command line option.
138bool PECOFFLinkingContext::addSectionRenaming(raw_ostream &diagnostics,
139 StringRef from, StringRef to) {
140 auto it = _renamedSections.find(from);
141 if (it != _renamedSections.end()) {
142 if (it->second == to)
143 // There's already the same mapping.
144 return true;
145 diagnostics << "Section \"" << from << "\" is already mapped to \""
146 << it->second << ", so it cannot be mapped to \"" << to << "\".";
147 return true;
148 }
149
150 // Add a mapping, and check if there's no cycle in the renaming mapping. The
151 // cycle detection algorithm we use here is naive, but that's OK because the
152 // number of mapping is usually less than 10.
153 _renamedSections[from] = to;
154 for (auto elem : _renamedSections) {
155 StringRef sectionName = elem.first;
156 std::set<StringRef> visited;
157 visited.insert(sectionName);
158 for (;;) {
Nick Kledzik3df81042013-11-06 21:30:15 +0000159 auto pos = _renamedSections.find(sectionName);
160 if (pos == _renamedSections.end())
Rui Ueyama863931c2013-10-26 00:46:57 +0000161 break;
Nick Kledzik3df81042013-11-06 21:30:15 +0000162 if (visited.count(pos->second)) {
Rui Ueyama863931c2013-10-26 00:46:57 +0000163 diagnostics << "/merge:" << from << "=" << to << " makes a cycle";
164 return false;
165 }
Nick Kledzik3df81042013-11-06 21:30:15 +0000166 sectionName = pos->second;
Rui Ueyama863931c2013-10-26 00:46:57 +0000167 visited.insert(sectionName);
168 }
169 }
170 return true;
171}
172
Rui Ueyama34d6e9b2013-12-09 01:47:32 +0000173StringRef PECOFFLinkingContext::getAlternateName(StringRef def) const {
174 auto it = _alternateNames.find(def);
175 if (it == _alternateNames.end())
176 return "";
177 return it->second;
178}
179
180void PECOFFLinkingContext::setAlternateName(StringRef weak, StringRef def) {
181 _alternateNames[def] = weak;
182}
183
Rui Ueyama2897feb2013-07-19 02:18:25 +0000184/// Try to find the input library file from the search paths and append it to
185/// the input file list. Returns true if the library file is found.
Shankar Easwarane44104b2013-08-21 22:57:10 +0000186StringRef PECOFFLinkingContext::searchLibraryFile(StringRef filename) const {
Rui Ueyama2897feb2013-07-19 02:18:25 +0000187 // Current directory always takes precedence over the search paths.
Shankar Easwarane44104b2013-08-21 22:57:10 +0000188 if (llvm::sys::path::is_absolute(filename) || llvm::sys::fs::exists(filename))
189 return filename;
Rui Ueyama2897feb2013-07-19 02:18:25 +0000190 // Iterate over the search paths.
191 for (StringRef dir : _inputSearchPaths) {
192 SmallString<128> path = dir;
193 llvm::sys::path::append(path, filename);
Shankar Easwarane44104b2013-08-21 22:57:10 +0000194 if (llvm::sys::fs::exists(path.str()))
Rui Ueyama90bcd112013-11-21 00:17:31 +0000195 return allocate(path.str());
Rui Ueyama2897feb2013-07-19 02:18:25 +0000196 }
Shankar Easwarane44104b2013-08-21 22:57:10 +0000197 return filename;
Rui Ueyama2897feb2013-07-19 02:18:25 +0000198}
199
Rui Ueyamaabc227b2013-12-14 04:32:29 +0000200/// Returns the decorated name of the given symbol name. On 32-bit x86, it
201/// adds "_" at the beginning of the string. On other architectures, the
202/// return value is the same as the argument.
203StringRef PECOFFLinkingContext::decorateSymbol(StringRef name) const {
204 if (_machineType != llvm::COFF::IMAGE_FILE_MACHINE_I386)
205 return name;
206 std::string str = "_";
207 str.append(name);
208 return allocate(str);
209}
210
Rui Ueyama090a7cd2013-12-24 09:15:57 +0000211StringRef PECOFFLinkingContext::undecorateSymbol(StringRef name) const {
212 if (_machineType != llvm::COFF::IMAGE_FILE_MACHINE_I386)
213 return name;
Rui Ueyama11f3f1f2014-04-24 17:04:19 +0000214 if (!name.startswith("_"))
215 return name;
Rui Ueyama090a7cd2013-12-24 09:15:57 +0000216 return name.substr(1);
217}
218
Rui Ueyamaea7e9302014-01-31 04:49:13 +0000219uint64_t PECOFFLinkingContext::getBaseAddress() const {
220 if (_baseAddress == invalidBaseAddress)
Rui Ueyama8851d452014-01-31 04:57:03 +0000221 return is64Bit() ? pe32PlusDefaultBaseAddress : pe32DefaultBaseAddress;
Rui Ueyamaea7e9302014-01-31 04:49:13 +0000222 return _baseAddress;
223}
Rui Ueyama9e568392013-05-28 18:13:31 +0000224
Rui Ueyamaea7e9302014-01-31 04:49:13 +0000225Writer &PECOFFLinkingContext::writer() const { return *_writer; }
Rui Ueyama9e568392013-05-28 18:13:31 +0000226
Rui Ueyama615b2002013-11-27 21:34:16 +0000227void PECOFFLinkingContext::setSectionSetMask(StringRef sectionName,
228 uint32_t newFlags) {
229 _sectionSetMask[sectionName] |= newFlags;
230 _sectionClearMask[sectionName] &= ~newFlags;
231 const uint32_t rwx = (llvm::COFF::IMAGE_SCN_MEM_READ |
232 llvm::COFF::IMAGE_SCN_MEM_WRITE |
233 llvm::COFF::IMAGE_SCN_MEM_EXECUTE);
234 if (newFlags & rwx)
235 _sectionClearMask[sectionName] |= ~_sectionSetMask[sectionName] & rwx;
236 assert((_sectionSetMask[sectionName] & _sectionClearMask[sectionName]) == 0);
237}
238
239void PECOFFLinkingContext::setSectionClearMask(StringRef sectionName,
240 uint32_t newFlags) {
241 _sectionClearMask[sectionName] |= newFlags;
242 _sectionSetMask[sectionName] &= ~newFlags;
243 assert((_sectionSetMask[sectionName] & _sectionClearMask[sectionName]) == 0);
244}
245
246uint32_t PECOFFLinkingContext::getSectionAttributes(StringRef sectionName,
247 uint32_t flags) const {
248 auto si = _sectionSetMask.find(sectionName);
249 uint32_t setMask = (si == _sectionSetMask.end()) ? 0 : si->second;
250 auto ci = _sectionClearMask.find(sectionName);
251 uint32_t clearMask = (ci == _sectionClearMask.end()) ? 0 : ci->second;
252 return (flags | setMask) & ~clearMask;
253}
Rui Ueyama170a1a82013-12-20 07:48:29 +0000254
Rui Ueyamaffd81052013-12-28 08:40:37 +0000255// Returns true if two export descriptors have conflicting contents,
256// e.g. different export ordinals.
257static bool exportConflicts(const PECOFFLinkingContext::ExportDesc &a,
258 const PECOFFLinkingContext::ExportDesc &b) {
Rui Ueyama93f76042013-12-28 10:09:21 +0000259 return (a.ordinal > 0 && b.ordinal > 0 && a.ordinal != b.ordinal) ||
260 a.noname != b.noname || a.isData != b.isData;
Rui Ueyamaffd81052013-12-28 08:40:37 +0000261}
262
Rui Ueyamacf461612013-12-25 06:46:45 +0000263void PECOFFLinkingContext::addDllExport(ExportDesc &desc) {
Rui Ueyamaffd81052013-12-28 08:40:37 +0000264 auto existing = _dllExports.insert(desc);
265 if (existing.second)
266 return;
267 if (!exportConflicts(*existing.first, desc)) {
268 _dllExports.erase(existing.first);
269 _dllExports.insert(desc);
Rui Ueyamacf461612013-12-25 06:46:45 +0000270 return;
271 }
Rui Ueyamaffd81052013-12-28 08:40:37 +0000272 llvm::errs() << "Export symbol '" << desc.name
273 << "' specified more than once.\n";
Rui Ueyamacf461612013-12-25 06:46:45 +0000274}
275
Rui Ueyama409ac182014-04-25 03:35:13 +0000276std::string PECOFFLinkingContext::getOutputImportLibraryPath() const {
277 if (!_implib.empty())
278 return _implib;
279 SmallString<128> path = outputPath();
280 llvm::sys::path::replace_extension(path, ".lib");
281 return path.str();
282}
283
Shankar Easwaran2bc24922013-10-29 05:12:14 +0000284void PECOFFLinkingContext::addPasses(PassManager &pm) {
Rui Ueyama1a11b3b2013-11-25 02:00:00 +0000285 pm.add(std::unique_ptr<Pass>(new pecoff::SetSubsystemPass(*this)));
Rui Ueyamac91c24e32013-12-13 06:58:27 +0000286 pm.add(std::unique_ptr<Pass>(new pecoff::EdataPass(*this)));
Rui Ueyama3ee2bf62013-09-15 22:33:15 +0000287 pm.add(std::unique_ptr<Pass>(new pecoff::IdataPass(*this)));
Nico Rieckb9d84f42014-02-24 21:14:37 +0000288 pm.add(std::unique_ptr<Pass>(new LayoutPass(registry())));
Rui Ueyama2e09d932014-02-26 08:27:59 +0000289 pm.add(std::unique_ptr<Pass>(new pecoff::LoadConfigPass(*this)));
Rui Ueyama32c3f172013-12-07 00:27:17 +0000290 pm.add(std::unique_ptr<Pass>(new pecoff::GroupedSectionsPass()));
Rui Ueyamad95a1552013-06-17 16:59:54 +0000291}
Rui Ueyama091071f2013-12-13 02:58:27 +0000292
Rui Ueyama9e568392013-05-28 18:13:31 +0000293} // end namespace lld