blob: 55552c1ed16c87c59fdde239adb7f1600c638c9c [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 Ueyama1a11b3b2013-11-25 02:00:00 +000015#include "SetSubsystemPass.h"
Rui Ueyama9e568392013-05-28 18:13:31 +000016
Rui Ueyamad95a1552013-06-17 16:59:54 +000017#include "lld/Core/PassManager.h"
18#include "lld/Passes/LayoutPass.h"
Rui Ueyamac9752fa2013-11-01 19:52:37 +000019#include "lld/Passes/RoundTripNativePass.h"
20#include "lld/Passes/RoundTripYAMLPass.h"
Rui Ueyama0ca149f2013-08-06 22:31:59 +000021#include "lld/ReaderWriter/PECOFFLinkingContext.h"
Rui Ueyama9e568392013-05-28 18:13:31 +000022#include "lld/ReaderWriter/Reader.h"
Rui Ueyamafd502832013-07-24 22:53:23 +000023#include "lld/ReaderWriter/Simple.h"
Rui Ueyama9e568392013-05-28 18:13:31 +000024#include "lld/ReaderWriter/Writer.h"
Rui Ueyamac9752fa2013-11-01 19:52:37 +000025#include "llvm/ADT/SmallString.h"
26#include "llvm/Support/Allocator.h"
27#include "llvm/Support/Path.h"
Rui Ueyama9e568392013-05-28 18:13:31 +000028
Rui Ueyama91491812013-09-23 19:52:35 +000029#include <bitset>
Rui Ueyama4af032d2013-12-20 10:02:59 +000030#include <climits>
Rui Ueyama863931c2013-10-26 00:46:57 +000031#include <set>
Rui Ueyama91491812013-09-23 19:52:35 +000032
Rui Ueyama9e568392013-05-28 18:13:31 +000033namespace lld {
34
Rui Ueyama4af032d2013-12-20 10:02:59 +000035static void assignOrdinals(PECOFFLinkingContext &ctx) {
36 int maxOrdinal = -1;
37 for (const PECOFFLinkingContext::ExportDesc &desc : ctx.getDllExports())
38 maxOrdinal = std::max(maxOrdinal, desc.ordinal);
39 int nextOrdinal = (maxOrdinal == -1) ? 1 : (maxOrdinal + 1);
40 for (PECOFFLinkingContext::ExportDesc &desc : ctx.getDllExports())
41 if (desc.ordinal == -1)
42 desc.ordinal = nextOrdinal++;
43}
44
Rui Ueyama0ca149f2013-08-06 22:31:59 +000045bool PECOFFLinkingContext::validateImpl(raw_ostream &diagnostics) {
Rui Ueyamaeb0cc962013-06-08 03:59:00 +000046 if (_stackReserve < _stackCommit) {
47 diagnostics << "Invalid stack size: reserve size must be equal to or "
Rui Ueyama0ca149f2013-08-06 22:31:59 +000048 << "greater than commit size, but got " << _stackCommit
49 << " and " << _stackReserve << ".\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +000050 return false;
Rui Ueyamaeb0cc962013-06-08 03:59:00 +000051 }
52
Rui Ueyama9dd08d92013-06-08 22:59:10 +000053 if (_heapReserve < _heapCommit) {
54 diagnostics << "Invalid heap size: reserve size must be equal to or "
Rui Ueyama0ca149f2013-08-06 22:31:59 +000055 << "greater than commit size, but got " << _heapCommit
56 << " and " << _heapReserve << ".\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +000057 return false;
Rui Ueyama9dd08d92013-06-08 22:59:10 +000058 }
59
Rui Ueyama530488c2013-09-03 22:57:00 +000060 // It's an error if the base address is not multiple of 64K.
61 if (_baseAddress & 0xffff) {
62 diagnostics << "Base address have to be multiple of 64K, but got "
63 << _baseAddress << "\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +000064 return false;
Rui Ueyama530488c2013-09-03 22:57:00 +000065 }
66
Rui Ueyamaa6fddab2013-12-16 09:15:58 +000067 // Check for duplicate export ordinals.
68 std::set<int> exports;
69 for (const PECOFFLinkingContext::ExportDesc &desc : getDllExports()) {
70 if (desc.ordinal == -1)
71 continue;
72 if (exports.count(desc.ordinal) == 1) {
73 diagnostics << "Duplicate export ordinals: " << desc.ordinal << "\n";
74 return false;
75 }
76 exports.insert(desc.ordinal);
77 }
78
Rui Ueyama41b99dc2013-11-06 19:30:14 +000079 std::bitset<64> alignment(_sectionDefaultAlignment);
Rui Ueyama91491812013-09-23 19:52:35 +000080 if (alignment.count() != 1) {
81 diagnostics << "Section alignment must be a power of 2, but got "
Rui Ueyama41b99dc2013-11-06 19:30:14 +000082 << _sectionDefaultAlignment << "\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +000083 return false;
Rui Ueyama91491812013-09-23 19:52:35 +000084 }
85
Rui Ueyama98896ed2013-09-12 19:46:53 +000086 // Architectures other than i386 is not supported yet.
87 if (_machineType != llvm::COFF::IMAGE_FILE_MACHINE_I386) {
88 diagnostics << "Machine type other than x86 is not supported.\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +000089 return false;
Rui Ueyama98896ed2013-09-12 19:46:53 +000090 }
91
Rui Ueyama4af032d2013-12-20 10:02:59 +000092 // Assign default ordinals to export symbols.
93 assignOrdinals(*this);
94
Rui Ueyama9e568392013-05-28 18:13:31 +000095 _writer = createWriterPECOFF(*this);
Rui Ueyama8db1edd2013-09-24 23:26:34 +000096 return true;
Rui Ueyama9e568392013-05-28 18:13:31 +000097}
98
Shankar Easwarana96f3a32013-10-07 02:47:09 +000099std::unique_ptr<File> PECOFFLinkingContext::createEntrySymbolFile() const {
Shankar Easwarand26c8e32013-08-31 05:27:38 +0000100 if (entrySymbolName().empty())
101 return nullptr;
102 std::unique_ptr<SimpleFile> entryFile(
Nick Kledzike5552772013-12-19 21:58:00 +0000103 new SimpleFile("command line option /entry"));
Shankar Easwarand26c8e32013-08-31 05:27:38 +0000104 entryFile->addAtom(
105 *(new (_allocator) SimpleUndefinedAtom(*entryFile, entrySymbolName())));
106 return std::move(entryFile);
107}
Rui Ueyama908606d2013-08-09 04:44:15 +0000108
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000109std::unique_ptr<File> PECOFFLinkingContext::createUndefinedSymbolFile() const {
Shankar Easwarand26c8e32013-08-31 05:27:38 +0000110 if (_initialUndefinedSymbols.empty())
111 return nullptr;
112 std::unique_ptr<SimpleFile> undefinedSymFile(
Nick Kledzike5552772013-12-19 21:58:00 +0000113 new SimpleFile("command line option /c (or) /include"));
Shankar Easwarand26c8e32013-08-31 05:27:38 +0000114 for (auto undefSymStr : _initialUndefinedSymbols)
115 undefinedSymFile->addAtom(*(new (_allocator) SimpleUndefinedAtom(
116 *undefinedSymFile, undefSymStr)));
117 return std::move(undefinedSymFile);
118}
119
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000120bool PECOFFLinkingContext::createImplicitFiles(
121 std::vector<std::unique_ptr<File> > &) const {
122 std::unique_ptr<SimpleFileNode> fileNode(
123 new SimpleFileNode("Implicit Files"));
124 std::unique_ptr<File> linkerGeneratedSymFile(
Rui Ueyama091071f2013-12-13 02:58:27 +0000125 new pecoff::LinkerGeneratedSymbolFile(*this));
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000126 fileNode->appendInputFile(std::move(linkerGeneratedSymFile));
127 inputGraph().insertOneElementAt(std::move(fileNode),
128 InputGraph::Position::END);
129 return true;
Rui Ueyamafd502832013-07-24 22:53:23 +0000130}
131
Rui Ueyama863931c2013-10-26 00:46:57 +0000132/// Returns the section name in the resulting executable.
133///
134/// Sections in object files are usually output to the executable with the same
135/// name, but you can rename by command line option. /merge:from=to makes the
136/// linker to combine "from" section contents to "to" section in the
137/// executable. We have a mapping for the renaming. This method looks up the
138/// table and returns a new section name if renamed.
139StringRef
Rui Ueyama951dd1d2013-11-27 18:03:31 +0000140PECOFFLinkingContext::getOutputSectionName(StringRef sectionName) const {
Rui Ueyama863931c2013-10-26 00:46:57 +0000141 auto it = _renamedSections.find(sectionName);
142 if (it == _renamedSections.end())
143 return sectionName;
Rui Ueyama951dd1d2013-11-27 18:03:31 +0000144 return getOutputSectionName(it->second);
Rui Ueyama863931c2013-10-26 00:46:57 +0000145}
146
147/// Adds a mapping to the section renaming table. This method will be used for
148/// /merge command line option.
149bool PECOFFLinkingContext::addSectionRenaming(raw_ostream &diagnostics,
150 StringRef from, StringRef to) {
151 auto it = _renamedSections.find(from);
152 if (it != _renamedSections.end()) {
153 if (it->second == to)
154 // There's already the same mapping.
155 return true;
156 diagnostics << "Section \"" << from << "\" is already mapped to \""
157 << it->second << ", so it cannot be mapped to \"" << to << "\".";
158 return true;
159 }
160
161 // Add a mapping, and check if there's no cycle in the renaming mapping. The
162 // cycle detection algorithm we use here is naive, but that's OK because the
163 // number of mapping is usually less than 10.
164 _renamedSections[from] = to;
165 for (auto elem : _renamedSections) {
166 StringRef sectionName = elem.first;
167 std::set<StringRef> visited;
168 visited.insert(sectionName);
169 for (;;) {
Nick Kledzik3df81042013-11-06 21:30:15 +0000170 auto pos = _renamedSections.find(sectionName);
171 if (pos == _renamedSections.end())
Rui Ueyama863931c2013-10-26 00:46:57 +0000172 break;
Nick Kledzik3df81042013-11-06 21:30:15 +0000173 if (visited.count(pos->second)) {
Rui Ueyama863931c2013-10-26 00:46:57 +0000174 diagnostics << "/merge:" << from << "=" << to << " makes a cycle";
175 return false;
176 }
Nick Kledzik3df81042013-11-06 21:30:15 +0000177 sectionName = pos->second;
Rui Ueyama863931c2013-10-26 00:46:57 +0000178 visited.insert(sectionName);
179 }
180 }
181 return true;
182}
183
Rui Ueyama34d6e9b2013-12-09 01:47:32 +0000184StringRef PECOFFLinkingContext::getAlternateName(StringRef def) const {
185 auto it = _alternateNames.find(def);
186 if (it == _alternateNames.end())
187 return "";
188 return it->second;
189}
190
191void PECOFFLinkingContext::setAlternateName(StringRef weak, StringRef def) {
192 _alternateNames[def] = weak;
193}
194
Rui Ueyama2897feb2013-07-19 02:18:25 +0000195/// Try to find the input library file from the search paths and append it to
196/// the input file list. Returns true if the library file is found.
Shankar Easwarane44104b2013-08-21 22:57:10 +0000197StringRef PECOFFLinkingContext::searchLibraryFile(StringRef filename) const {
Rui Ueyama2897feb2013-07-19 02:18:25 +0000198 // Current directory always takes precedence over the search paths.
Shankar Easwarane44104b2013-08-21 22:57:10 +0000199 if (llvm::sys::path::is_absolute(filename) || llvm::sys::fs::exists(filename))
200 return filename;
Rui Ueyama2897feb2013-07-19 02:18:25 +0000201 // Iterate over the search paths.
202 for (StringRef dir : _inputSearchPaths) {
203 SmallString<128> path = dir;
204 llvm::sys::path::append(path, filename);
Shankar Easwarane44104b2013-08-21 22:57:10 +0000205 if (llvm::sys::fs::exists(path.str()))
Rui Ueyama90bcd112013-11-21 00:17:31 +0000206 return allocate(path.str());
Rui Ueyama2897feb2013-07-19 02:18:25 +0000207 }
Shankar Easwarane44104b2013-08-21 22:57:10 +0000208 return filename;
Rui Ueyama2897feb2013-07-19 02:18:25 +0000209}
210
Rui Ueyamaabc227b2013-12-14 04:32:29 +0000211/// Returns the decorated name of the given symbol name. On 32-bit x86, it
212/// adds "_" at the beginning of the string. On other architectures, the
213/// return value is the same as the argument.
214StringRef PECOFFLinkingContext::decorateSymbol(StringRef name) const {
215 if (_machineType != llvm::COFF::IMAGE_FILE_MACHINE_I386)
216 return name;
217 std::string str = "_";
218 str.append(name);
219 return allocate(str);
220}
221
Rui Ueyama090a7cd2013-12-24 09:15:57 +0000222StringRef PECOFFLinkingContext::undecorateSymbol(StringRef name) const {
223 if (_machineType != llvm::COFF::IMAGE_FILE_MACHINE_I386)
224 return name;
225 assert(name.startswith("_"));
226 return name.substr(1);
227}
228
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000229Writer &PECOFFLinkingContext::writer() const { return *_writer; }
Rui Ueyama9e568392013-05-28 18:13:31 +0000230
Rui Ueyama9e568392013-05-28 18:13:31 +0000231
Rui Ueyama615b2002013-11-27 21:34:16 +0000232void PECOFFLinkingContext::setSectionSetMask(StringRef sectionName,
233 uint32_t newFlags) {
234 _sectionSetMask[sectionName] |= newFlags;
235 _sectionClearMask[sectionName] &= ~newFlags;
236 const uint32_t rwx = (llvm::COFF::IMAGE_SCN_MEM_READ |
237 llvm::COFF::IMAGE_SCN_MEM_WRITE |
238 llvm::COFF::IMAGE_SCN_MEM_EXECUTE);
239 if (newFlags & rwx)
240 _sectionClearMask[sectionName] |= ~_sectionSetMask[sectionName] & rwx;
241 assert((_sectionSetMask[sectionName] & _sectionClearMask[sectionName]) == 0);
242}
243
244void PECOFFLinkingContext::setSectionClearMask(StringRef sectionName,
245 uint32_t newFlags) {
246 _sectionClearMask[sectionName] |= newFlags;
247 _sectionSetMask[sectionName] &= ~newFlags;
248 assert((_sectionSetMask[sectionName] & _sectionClearMask[sectionName]) == 0);
249}
250
251uint32_t PECOFFLinkingContext::getSectionAttributes(StringRef sectionName,
252 uint32_t flags) const {
253 auto si = _sectionSetMask.find(sectionName);
254 uint32_t setMask = (si == _sectionSetMask.end()) ? 0 : si->second;
255 auto ci = _sectionClearMask.find(sectionName);
256 uint32_t clearMask = (ci == _sectionClearMask.end()) ? 0 : ci->second;
257 return (flags | setMask) & ~clearMask;
258}
Rui Ueyama170a1a82013-12-20 07:48:29 +0000259
Rui Ueyamacf461612013-12-25 06:46:45 +0000260void PECOFFLinkingContext::addDllExport(ExportDesc &desc) {
261 if (_dllExportSet.count(desc.name)) {
262 llvm::errs() << "Export symbol '" << desc.name
263 << "' specified more than once.";
264 return;
265 }
266 _dllExports.push_back(desc);
267 _dllExportSet.insert(desc.name);
268}
269
Shankar Easwaran2bc24922013-10-29 05:12:14 +0000270void PECOFFLinkingContext::addPasses(PassManager &pm) {
Rui Ueyama1a11b3b2013-11-25 02:00:00 +0000271 pm.add(std::unique_ptr<Pass>(new pecoff::SetSubsystemPass(*this)));
Rui Ueyamac91c24e32013-12-13 06:58:27 +0000272 pm.add(std::unique_ptr<Pass>(new pecoff::EdataPass(*this)));
Rui Ueyama3ee2bf62013-09-15 22:33:15 +0000273 pm.add(std::unique_ptr<Pass>(new pecoff::IdataPass(*this)));
Rui Ueyamad95a1552013-06-17 16:59:54 +0000274 pm.add(std::unique_ptr<Pass>(new LayoutPass()));
Rui Ueyama32c3f172013-12-07 00:27:17 +0000275 pm.add(std::unique_ptr<Pass>(new pecoff::GroupedSectionsPass()));
Rui Ueyamad95a1552013-06-17 16:59:54 +0000276}
Rui Ueyama091071f2013-12-13 02:58:27 +0000277
Rui Ueyama9e568392013-05-28 18:13:31 +0000278} // end namespace lld