blob: 9c929db6d3b1affc5fb92c1f85c36d57bd3a618a [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 Ueyama0ca149f2013-08-06 22:31:59 +000035bool PECOFFLinkingContext::validateImpl(raw_ostream &diagnostics) {
Rui Ueyamaeb0cc962013-06-08 03:59:00 +000036 if (_stackReserve < _stackCommit) {
37 diagnostics << "Invalid stack size: reserve size must be equal to or "
Rui Ueyama0ca149f2013-08-06 22:31:59 +000038 << "greater than commit size, but got " << _stackCommit
39 << " and " << _stackReserve << ".\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +000040 return false;
Rui Ueyamaeb0cc962013-06-08 03:59:00 +000041 }
42
Rui Ueyama9dd08d92013-06-08 22:59:10 +000043 if (_heapReserve < _heapCommit) {
44 diagnostics << "Invalid heap size: reserve size must be equal to or "
Rui Ueyama0ca149f2013-08-06 22:31:59 +000045 << "greater than commit size, but got " << _heapCommit
46 << " and " << _heapReserve << ".\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +000047 return false;
Rui Ueyama9dd08d92013-06-08 22:59:10 +000048 }
49
Rui Ueyama530488c2013-09-03 22:57:00 +000050 // It's an error if the base address is not multiple of 64K.
51 if (_baseAddress & 0xffff) {
52 diagnostics << "Base address have to be multiple of 64K, but got "
53 << _baseAddress << "\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +000054 return false;
Rui Ueyama530488c2013-09-03 22:57:00 +000055 }
56
Rui Ueyamaa6fddab2013-12-16 09:15:58 +000057 // Check for duplicate export ordinals.
58 std::set<int> exports;
59 for (const PECOFFLinkingContext::ExportDesc &desc : getDllExports()) {
60 if (desc.ordinal == -1)
61 continue;
62 if (exports.count(desc.ordinal) == 1) {
63 diagnostics << "Duplicate export ordinals: " << desc.ordinal << "\n";
64 return false;
65 }
66 exports.insert(desc.ordinal);
67 }
68
Rui Ueyama41b99dc2013-11-06 19:30:14 +000069 std::bitset<64> alignment(_sectionDefaultAlignment);
Rui Ueyama91491812013-09-23 19:52:35 +000070 if (alignment.count() != 1) {
71 diagnostics << "Section alignment must be a power of 2, but got "
Rui Ueyama41b99dc2013-11-06 19:30:14 +000072 << _sectionDefaultAlignment << "\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +000073 return false;
Rui Ueyama91491812013-09-23 19:52:35 +000074 }
75
Rui Ueyama49bfd502014-01-24 19:17:05 +000076 // Architectures other than x86/x64 is not supported yet.
77 if (_machineType != llvm::COFF::IMAGE_FILE_MACHINE_I386 &&
78 _machineType != llvm::COFF::IMAGE_FILE_MACHINE_AMD64) {
79 diagnostics << "Machine type other than x86/x64 is not supported.\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +000080 return false;
Rui Ueyama98896ed2013-09-12 19:46:53 +000081 }
82
Rui Ueyama9e568392013-05-28 18:13:31 +000083 _writer = createWriterPECOFF(*this);
Rui Ueyama8db1edd2013-09-24 23:26:34 +000084 return true;
Rui Ueyama9e568392013-05-28 18:13:31 +000085}
86
Shankar Easwarana96f3a32013-10-07 02:47:09 +000087std::unique_ptr<File> PECOFFLinkingContext::createEntrySymbolFile() const {
Rui Ueyama61b851a2013-12-26 06:35:35 +000088 return LinkingContext::createEntrySymbolFile("command line option /entry");
Shankar Easwarand26c8e32013-08-31 05:27:38 +000089}
Rui Ueyama908606d2013-08-09 04:44:15 +000090
Shankar Easwarana96f3a32013-10-07 02:47:09 +000091std::unique_ptr<File> PECOFFLinkingContext::createUndefinedSymbolFile() const {
Rui Ueyama61b851a2013-12-26 06:35:35 +000092 return LinkingContext::createUndefinedSymbolFile("command line option /include");
Shankar Easwarand26c8e32013-08-31 05:27:38 +000093}
94
Shankar Easwarana96f3a32013-10-07 02:47:09 +000095bool PECOFFLinkingContext::createImplicitFiles(
96 std::vector<std::unique_ptr<File> > &) const {
97 std::unique_ptr<SimpleFileNode> fileNode(
98 new SimpleFileNode("Implicit Files"));
99 std::unique_ptr<File> linkerGeneratedSymFile(
Rui Ueyama091071f2013-12-13 02:58:27 +0000100 new pecoff::LinkerGeneratedSymbolFile(*this));
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000101 fileNode->appendInputFile(std::move(linkerGeneratedSymFile));
102 inputGraph().insertOneElementAt(std::move(fileNode),
103 InputGraph::Position::END);
104 return true;
Rui Ueyamafd502832013-07-24 22:53:23 +0000105}
106
Rui Ueyama863931c2013-10-26 00:46:57 +0000107/// Returns the section name in the resulting executable.
108///
109/// Sections in object files are usually output to the executable with the same
110/// name, but you can rename by command line option. /merge:from=to makes the
111/// linker to combine "from" section contents to "to" section in the
112/// executable. We have a mapping for the renaming. This method looks up the
113/// table and returns a new section name if renamed.
114StringRef
Rui Ueyama951dd1d2013-11-27 18:03:31 +0000115PECOFFLinkingContext::getOutputSectionName(StringRef sectionName) const {
Rui Ueyama863931c2013-10-26 00:46:57 +0000116 auto it = _renamedSections.find(sectionName);
117 if (it == _renamedSections.end())
118 return sectionName;
Rui Ueyama951dd1d2013-11-27 18:03:31 +0000119 return getOutputSectionName(it->second);
Rui Ueyama863931c2013-10-26 00:46:57 +0000120}
121
122/// Adds a mapping to the section renaming table. This method will be used for
123/// /merge command line option.
124bool PECOFFLinkingContext::addSectionRenaming(raw_ostream &diagnostics,
125 StringRef from, StringRef to) {
126 auto it = _renamedSections.find(from);
127 if (it != _renamedSections.end()) {
128 if (it->second == to)
129 // There's already the same mapping.
130 return true;
131 diagnostics << "Section \"" << from << "\" is already mapped to \""
132 << it->second << ", so it cannot be mapped to \"" << to << "\".";
133 return true;
134 }
135
136 // Add a mapping, and check if there's no cycle in the renaming mapping. The
137 // cycle detection algorithm we use here is naive, but that's OK because the
138 // number of mapping is usually less than 10.
139 _renamedSections[from] = to;
140 for (auto elem : _renamedSections) {
141 StringRef sectionName = elem.first;
142 std::set<StringRef> visited;
143 visited.insert(sectionName);
144 for (;;) {
Nick Kledzik3df81042013-11-06 21:30:15 +0000145 auto pos = _renamedSections.find(sectionName);
146 if (pos == _renamedSections.end())
Rui Ueyama863931c2013-10-26 00:46:57 +0000147 break;
Nick Kledzik3df81042013-11-06 21:30:15 +0000148 if (visited.count(pos->second)) {
Rui Ueyama863931c2013-10-26 00:46:57 +0000149 diagnostics << "/merge:" << from << "=" << to << " makes a cycle";
150 return false;
151 }
Nick Kledzik3df81042013-11-06 21:30:15 +0000152 sectionName = pos->second;
Rui Ueyama863931c2013-10-26 00:46:57 +0000153 visited.insert(sectionName);
154 }
155 }
156 return true;
157}
158
Rui Ueyama34d6e9b2013-12-09 01:47:32 +0000159StringRef PECOFFLinkingContext::getAlternateName(StringRef def) const {
160 auto it = _alternateNames.find(def);
161 if (it == _alternateNames.end())
162 return "";
163 return it->second;
164}
165
166void PECOFFLinkingContext::setAlternateName(StringRef weak, StringRef def) {
167 _alternateNames[def] = weak;
168}
169
Rui Ueyama2897feb2013-07-19 02:18:25 +0000170/// Try to find the input library file from the search paths and append it to
171/// the input file list. Returns true if the library file is found.
Shankar Easwarane44104b2013-08-21 22:57:10 +0000172StringRef PECOFFLinkingContext::searchLibraryFile(StringRef filename) const {
Rui Ueyama2897feb2013-07-19 02:18:25 +0000173 // Current directory always takes precedence over the search paths.
Shankar Easwarane44104b2013-08-21 22:57:10 +0000174 if (llvm::sys::path::is_absolute(filename) || llvm::sys::fs::exists(filename))
175 return filename;
Rui Ueyama2897feb2013-07-19 02:18:25 +0000176 // Iterate over the search paths.
177 for (StringRef dir : _inputSearchPaths) {
178 SmallString<128> path = dir;
179 llvm::sys::path::append(path, filename);
Shankar Easwarane44104b2013-08-21 22:57:10 +0000180 if (llvm::sys::fs::exists(path.str()))
Rui Ueyama90bcd112013-11-21 00:17:31 +0000181 return allocate(path.str());
Rui Ueyama2897feb2013-07-19 02:18:25 +0000182 }
Shankar Easwarane44104b2013-08-21 22:57:10 +0000183 return filename;
Rui Ueyama2897feb2013-07-19 02:18:25 +0000184}
185
Rui Ueyamaabc227b2013-12-14 04:32:29 +0000186/// Returns the decorated name of the given symbol name. On 32-bit x86, it
187/// adds "_" at the beginning of the string. On other architectures, the
188/// return value is the same as the argument.
189StringRef PECOFFLinkingContext::decorateSymbol(StringRef name) const {
190 if (_machineType != llvm::COFF::IMAGE_FILE_MACHINE_I386)
191 return name;
192 std::string str = "_";
193 str.append(name);
194 return allocate(str);
195}
196
Rui Ueyama090a7cd2013-12-24 09:15:57 +0000197StringRef PECOFFLinkingContext::undecorateSymbol(StringRef name) const {
198 if (_machineType != llvm::COFF::IMAGE_FILE_MACHINE_I386)
199 return name;
200 assert(name.startswith("_"));
201 return name.substr(1);
202}
203
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000204Writer &PECOFFLinkingContext::writer() const { return *_writer; }
Rui Ueyama9e568392013-05-28 18:13:31 +0000205
Rui Ueyama9e568392013-05-28 18:13:31 +0000206
Rui Ueyama615b2002013-11-27 21:34:16 +0000207void PECOFFLinkingContext::setSectionSetMask(StringRef sectionName,
208 uint32_t newFlags) {
209 _sectionSetMask[sectionName] |= newFlags;
210 _sectionClearMask[sectionName] &= ~newFlags;
211 const uint32_t rwx = (llvm::COFF::IMAGE_SCN_MEM_READ |
212 llvm::COFF::IMAGE_SCN_MEM_WRITE |
213 llvm::COFF::IMAGE_SCN_MEM_EXECUTE);
214 if (newFlags & rwx)
215 _sectionClearMask[sectionName] |= ~_sectionSetMask[sectionName] & rwx;
216 assert((_sectionSetMask[sectionName] & _sectionClearMask[sectionName]) == 0);
217}
218
219void PECOFFLinkingContext::setSectionClearMask(StringRef sectionName,
220 uint32_t newFlags) {
221 _sectionClearMask[sectionName] |= newFlags;
222 _sectionSetMask[sectionName] &= ~newFlags;
223 assert((_sectionSetMask[sectionName] & _sectionClearMask[sectionName]) == 0);
224}
225
226uint32_t PECOFFLinkingContext::getSectionAttributes(StringRef sectionName,
227 uint32_t flags) const {
228 auto si = _sectionSetMask.find(sectionName);
229 uint32_t setMask = (si == _sectionSetMask.end()) ? 0 : si->second;
230 auto ci = _sectionClearMask.find(sectionName);
231 uint32_t clearMask = (ci == _sectionClearMask.end()) ? 0 : ci->second;
232 return (flags | setMask) & ~clearMask;
233}
Rui Ueyama170a1a82013-12-20 07:48:29 +0000234
Rui Ueyamaffd81052013-12-28 08:40:37 +0000235// Returns true if two export descriptors have conflicting contents,
236// e.g. different export ordinals.
237static bool exportConflicts(const PECOFFLinkingContext::ExportDesc &a,
238 const PECOFFLinkingContext::ExportDesc &b) {
Rui Ueyama93f76042013-12-28 10:09:21 +0000239 return (a.ordinal > 0 && b.ordinal > 0 && a.ordinal != b.ordinal) ||
240 a.noname != b.noname || a.isData != b.isData;
Rui Ueyamaffd81052013-12-28 08:40:37 +0000241}
242
Rui Ueyamacf461612013-12-25 06:46:45 +0000243void PECOFFLinkingContext::addDllExport(ExportDesc &desc) {
Rui Ueyamaffd81052013-12-28 08:40:37 +0000244 auto existing = _dllExports.insert(desc);
245 if (existing.second)
246 return;
247 if (!exportConflicts(*existing.first, desc)) {
248 _dllExports.erase(existing.first);
249 _dllExports.insert(desc);
Rui Ueyamacf461612013-12-25 06:46:45 +0000250 return;
251 }
Rui Ueyamaffd81052013-12-28 08:40:37 +0000252 llvm::errs() << "Export symbol '" << desc.name
253 << "' specified more than once.\n";
Rui Ueyamacf461612013-12-25 06:46:45 +0000254}
255
Shankar Easwaran2bc24922013-10-29 05:12:14 +0000256void PECOFFLinkingContext::addPasses(PassManager &pm) {
Rui Ueyama1a11b3b2013-11-25 02:00:00 +0000257 pm.add(std::unique_ptr<Pass>(new pecoff::SetSubsystemPass(*this)));
Rui Ueyamac91c24e32013-12-13 06:58:27 +0000258 pm.add(std::unique_ptr<Pass>(new pecoff::EdataPass(*this)));
Rui Ueyama3ee2bf62013-09-15 22:33:15 +0000259 pm.add(std::unique_ptr<Pass>(new pecoff::IdataPass(*this)));
Rui Ueyamad95a1552013-06-17 16:59:54 +0000260 pm.add(std::unique_ptr<Pass>(new LayoutPass()));
Rui Ueyama32c3f172013-12-07 00:27:17 +0000261 pm.add(std::unique_ptr<Pass>(new pecoff::GroupedSectionsPass()));
Rui Ueyamad95a1552013-06-17 16:59:54 +0000262}
Rui Ueyama091071f2013-12-13 02:58:27 +0000263
Rui Ueyama9e568392013-05-28 18:13:31 +0000264} // end namespace lld