blob: ff8ed1b4204a4cc9badb2895edf019f87a572df4 [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)));
Rui Ueyama680210f2014-04-30 03:31:46 +0000117 getInputGraph().insertElementAt(std::move(impFileNode),
118 InputGraph::Position::END);
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000119 return true;
Rui Ueyamafd502832013-07-24 22:53:23 +0000120}
121
Rui Ueyama863931c2013-10-26 00:46:57 +0000122/// Returns the section name in the resulting executable.
123///
124/// Sections in object files are usually output to the executable with the same
125/// name, but you can rename by command line option. /merge:from=to makes the
126/// linker to combine "from" section contents to "to" section in the
127/// executable. We have a mapping for the renaming. This method looks up the
128/// table and returns a new section name if renamed.
129StringRef
Rui Ueyama951dd1d2013-11-27 18:03:31 +0000130PECOFFLinkingContext::getOutputSectionName(StringRef sectionName) const {
Rui Ueyama863931c2013-10-26 00:46:57 +0000131 auto it = _renamedSections.find(sectionName);
132 if (it == _renamedSections.end())
133 return sectionName;
Rui Ueyama951dd1d2013-11-27 18:03:31 +0000134 return getOutputSectionName(it->second);
Rui Ueyama863931c2013-10-26 00:46:57 +0000135}
136
137/// Adds a mapping to the section renaming table. This method will be used for
138/// /merge command line option.
139bool PECOFFLinkingContext::addSectionRenaming(raw_ostream &diagnostics,
140 StringRef from, StringRef to) {
141 auto it = _renamedSections.find(from);
142 if (it != _renamedSections.end()) {
143 if (it->second == to)
144 // There's already the same mapping.
145 return true;
146 diagnostics << "Section \"" << from << "\" is already mapped to \""
147 << it->second << ", so it cannot be mapped to \"" << to << "\".";
148 return true;
149 }
150
151 // Add a mapping, and check if there's no cycle in the renaming mapping. The
152 // cycle detection algorithm we use here is naive, but that's OK because the
153 // number of mapping is usually less than 10.
154 _renamedSections[from] = to;
155 for (auto elem : _renamedSections) {
156 StringRef sectionName = elem.first;
157 std::set<StringRef> visited;
158 visited.insert(sectionName);
159 for (;;) {
Nick Kledzik3df81042013-11-06 21:30:15 +0000160 auto pos = _renamedSections.find(sectionName);
161 if (pos == _renamedSections.end())
Rui Ueyama863931c2013-10-26 00:46:57 +0000162 break;
Nick Kledzik3df81042013-11-06 21:30:15 +0000163 if (visited.count(pos->second)) {
Rui Ueyama863931c2013-10-26 00:46:57 +0000164 diagnostics << "/merge:" << from << "=" << to << " makes a cycle";
165 return false;
166 }
Nick Kledzik3df81042013-11-06 21:30:15 +0000167 sectionName = pos->second;
Rui Ueyama863931c2013-10-26 00:46:57 +0000168 visited.insert(sectionName);
169 }
170 }
171 return true;
172}
173
Rui Ueyama34d6e9b2013-12-09 01:47:32 +0000174StringRef PECOFFLinkingContext::getAlternateName(StringRef def) const {
175 auto it = _alternateNames.find(def);
176 if (it == _alternateNames.end())
177 return "";
178 return it->second;
179}
180
181void PECOFFLinkingContext::setAlternateName(StringRef weak, StringRef def) {
182 _alternateNames[def] = weak;
183}
184
Rui Ueyama2897feb2013-07-19 02:18:25 +0000185/// Try to find the input library file from the search paths and append it to
186/// the input file list. Returns true if the library file is found.
Shankar Easwarane44104b2013-08-21 22:57:10 +0000187StringRef PECOFFLinkingContext::searchLibraryFile(StringRef filename) const {
Rui Ueyama2897feb2013-07-19 02:18:25 +0000188 // Current directory always takes precedence over the search paths.
Shankar Easwarane44104b2013-08-21 22:57:10 +0000189 if (llvm::sys::path::is_absolute(filename) || llvm::sys::fs::exists(filename))
190 return filename;
Rui Ueyama2897feb2013-07-19 02:18:25 +0000191 // Iterate over the search paths.
192 for (StringRef dir : _inputSearchPaths) {
193 SmallString<128> path = dir;
194 llvm::sys::path::append(path, filename);
Shankar Easwarane44104b2013-08-21 22:57:10 +0000195 if (llvm::sys::fs::exists(path.str()))
Rui Ueyama90bcd112013-11-21 00:17:31 +0000196 return allocate(path.str());
Rui Ueyama2897feb2013-07-19 02:18:25 +0000197 }
Shankar Easwarane44104b2013-08-21 22:57:10 +0000198 return filename;
Rui Ueyama2897feb2013-07-19 02:18:25 +0000199}
200
Rui Ueyamaabc227b2013-12-14 04:32:29 +0000201/// Returns the decorated name of the given symbol name. On 32-bit x86, it
202/// adds "_" at the beginning of the string. On other architectures, the
203/// return value is the same as the argument.
204StringRef PECOFFLinkingContext::decorateSymbol(StringRef name) const {
205 if (_machineType != llvm::COFF::IMAGE_FILE_MACHINE_I386)
206 return name;
207 std::string str = "_";
208 str.append(name);
209 return allocate(str);
210}
211
Rui Ueyama090a7cd2013-12-24 09:15:57 +0000212StringRef PECOFFLinkingContext::undecorateSymbol(StringRef name) const {
213 if (_machineType != llvm::COFF::IMAGE_FILE_MACHINE_I386)
214 return name;
Rui Ueyama11f3f1f2014-04-24 17:04:19 +0000215 if (!name.startswith("_"))
216 return name;
Rui Ueyama090a7cd2013-12-24 09:15:57 +0000217 return name.substr(1);
218}
219
Rui Ueyamaea7e9302014-01-31 04:49:13 +0000220uint64_t PECOFFLinkingContext::getBaseAddress() const {
221 if (_baseAddress == invalidBaseAddress)
Rui Ueyama8851d452014-01-31 04:57:03 +0000222 return is64Bit() ? pe32PlusDefaultBaseAddress : pe32DefaultBaseAddress;
Rui Ueyamaea7e9302014-01-31 04:49:13 +0000223 return _baseAddress;
224}
Rui Ueyama9e568392013-05-28 18:13:31 +0000225
Rui Ueyamaea7e9302014-01-31 04:49:13 +0000226Writer &PECOFFLinkingContext::writer() const { return *_writer; }
Rui Ueyama9e568392013-05-28 18:13:31 +0000227
Rui Ueyama615b2002013-11-27 21:34:16 +0000228void PECOFFLinkingContext::setSectionSetMask(StringRef sectionName,
229 uint32_t newFlags) {
230 _sectionSetMask[sectionName] |= newFlags;
231 _sectionClearMask[sectionName] &= ~newFlags;
232 const uint32_t rwx = (llvm::COFF::IMAGE_SCN_MEM_READ |
233 llvm::COFF::IMAGE_SCN_MEM_WRITE |
234 llvm::COFF::IMAGE_SCN_MEM_EXECUTE);
235 if (newFlags & rwx)
236 _sectionClearMask[sectionName] |= ~_sectionSetMask[sectionName] & rwx;
237 assert((_sectionSetMask[sectionName] & _sectionClearMask[sectionName]) == 0);
238}
239
240void PECOFFLinkingContext::setSectionClearMask(StringRef sectionName,
241 uint32_t newFlags) {
242 _sectionClearMask[sectionName] |= newFlags;
243 _sectionSetMask[sectionName] &= ~newFlags;
244 assert((_sectionSetMask[sectionName] & _sectionClearMask[sectionName]) == 0);
245}
246
247uint32_t PECOFFLinkingContext::getSectionAttributes(StringRef sectionName,
248 uint32_t flags) const {
249 auto si = _sectionSetMask.find(sectionName);
250 uint32_t setMask = (si == _sectionSetMask.end()) ? 0 : si->second;
251 auto ci = _sectionClearMask.find(sectionName);
252 uint32_t clearMask = (ci == _sectionClearMask.end()) ? 0 : ci->second;
253 return (flags | setMask) & ~clearMask;
254}
Rui Ueyama170a1a82013-12-20 07:48:29 +0000255
Rui Ueyamaffd81052013-12-28 08:40:37 +0000256// Returns true if two export descriptors have conflicting contents,
257// e.g. different export ordinals.
258static bool exportConflicts(const PECOFFLinkingContext::ExportDesc &a,
259 const PECOFFLinkingContext::ExportDesc &b) {
Rui Ueyama93f76042013-12-28 10:09:21 +0000260 return (a.ordinal > 0 && b.ordinal > 0 && a.ordinal != b.ordinal) ||
261 a.noname != b.noname || a.isData != b.isData;
Rui Ueyamaffd81052013-12-28 08:40:37 +0000262}
263
Rui Ueyamacf461612013-12-25 06:46:45 +0000264void PECOFFLinkingContext::addDllExport(ExportDesc &desc) {
Rui Ueyamaffd81052013-12-28 08:40:37 +0000265 auto existing = _dllExports.insert(desc);
266 if (existing.second)
267 return;
268 if (!exportConflicts(*existing.first, desc)) {
269 _dllExports.erase(existing.first);
270 _dllExports.insert(desc);
Rui Ueyamacf461612013-12-25 06:46:45 +0000271 return;
272 }
Rui Ueyamaffd81052013-12-28 08:40:37 +0000273 llvm::errs() << "Export symbol '" << desc.name
274 << "' specified more than once.\n";
Rui Ueyamacf461612013-12-25 06:46:45 +0000275}
276
Rui Ueyama409ac182014-04-25 03:35:13 +0000277std::string PECOFFLinkingContext::getOutputImportLibraryPath() const {
278 if (!_implib.empty())
279 return _implib;
280 SmallString<128> path = outputPath();
281 llvm::sys::path::replace_extension(path, ".lib");
282 return path.str();
283}
284
Shankar Easwaran2bc24922013-10-29 05:12:14 +0000285void PECOFFLinkingContext::addPasses(PassManager &pm) {
Rui Ueyama1a11b3b2013-11-25 02:00:00 +0000286 pm.add(std::unique_ptr<Pass>(new pecoff::SetSubsystemPass(*this)));
Rui Ueyamac91c24e32013-12-13 06:58:27 +0000287 pm.add(std::unique_ptr<Pass>(new pecoff::EdataPass(*this)));
Rui Ueyama3ee2bf62013-09-15 22:33:15 +0000288 pm.add(std::unique_ptr<Pass>(new pecoff::IdataPass(*this)));
Nico Rieckb9d84f42014-02-24 21:14:37 +0000289 pm.add(std::unique_ptr<Pass>(new LayoutPass(registry())));
Rui Ueyama2e09d932014-02-26 08:27:59 +0000290 pm.add(std::unique_ptr<Pass>(new pecoff::LoadConfigPass(*this)));
Rui Ueyama32c3f172013-12-07 00:27:17 +0000291 pm.add(std::unique_ptr<Pass>(new pecoff::GroupedSectionsPass()));
Rui Ueyamad95a1552013-06-17 16:59:54 +0000292}
Rui Ueyama091071f2013-12-13 02:58:27 +0000293
Rui Ueyama9e568392013-05-28 18:13:31 +0000294} // end namespace lld