aboutsummaryrefslogtreecommitdiff
path: root/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp
blob: 6a657e33541d2f25ef9c7a9a309ed063c30e694c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//===- lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp -------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "Atoms.h"
#include "EdataPass.h"
#include "IdataPass.h"
#include "InferSubsystemPass.h"
#include "LinkerGeneratedSymbolFile.h"
#include "LoadConfigPass.h"
#include "OrderPass.h"
#include "PDBPass.h"
#include "lld/Core/PassManager.h"
#include "lld/Core/Reader.h"
#include "lld/Core/Simple.h"
#include "lld/Core/Writer.h"
#include "lld/ReaderWriter/PECOFFLinkingContext.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/Path.h"
#include <bitset>
#include <climits>
#include <set>

namespace lld {

bool PECOFFLinkingContext::validateImpl(raw_ostream &diagnostics) {
  if (_stackReserve < _stackCommit) {
    diagnostics << "Invalid stack size: reserve size must be equal to or "
                << "greater than commit size, but got " << _stackCommit
                << " and " << _stackReserve << ".\n";
    return false;
  }

  if (_heapReserve < _heapCommit) {
    diagnostics << "Invalid heap size: reserve size must be equal to or "
                << "greater than commit size, but got " << _heapCommit
                << " and " << _heapReserve << ".\n";
    return false;
  }

  // It's an error if the base address is not multiple of 64K.
  if (getBaseAddress() & 0xffff) {
    diagnostics << "Base address have to be multiple of 64K, but got "
                << getBaseAddress() << "\n";
    return false;
  }

  // Specifing /noentry without /dll is an error.
  if (!hasEntry() && !isDll()) {
    diagnostics << "/noentry must be specified with /dll\n";
    return false;
  }

  // Check for duplicate export ordinals.
  std::set<int> exports;
  for (const PECOFFLinkingContext::ExportDesc &desc : getDllExports()) {
    if (desc.ordinal == -1)
      continue;
    if (exports.count(desc.ordinal) == 1) {
      diagnostics << "Duplicate export ordinals: " << desc.ordinal << "\n";
      return false;
    }
    exports.insert(desc.ordinal);
  }

  // Check for /align.
  std::bitset<64> alignment(_sectionDefaultAlignment);
  if (alignment.count() != 1) {
    diagnostics << "Section alignment must be a power of 2, but got "
                << _sectionDefaultAlignment << "\n";
    return false;
  }

  _writer = createWriterPECOFF(*this);
  return true;
}

const std::set<std::string> &PECOFFLinkingContext::definedSymbols() {
  std::lock_guard<std::recursive_mutex> lock(_mutex);
  for (std::unique_ptr<Node> &node : getNodes()) {
    if (_seen.count(node.get()) > 0)
      continue;
    FileNode *fnode = dyn_cast<FileNode>(node.get());
    if (!fnode)
      continue;
    File *file = fnode->getFile();
    if (file->parse())
      continue;
    if (auto *archive = dyn_cast<ArchiveLibraryFile>(file)) {
      for (const std::string &sym : archive->getDefinedSymbols())
        _definedSyms.insert(sym);
      continue;
    }
    for (const DefinedAtom *atom : file->defined())
      if (!atom->name().empty())
        _definedSyms.insert(atom->name());
  }
  return _definedSyms;
}

std::unique_ptr<File> PECOFFLinkingContext::createEntrySymbolFile() const {
  return LinkingContext::createEntrySymbolFile("<command line option /entry>");
}

std::unique_ptr<File> PECOFFLinkingContext::createUndefinedSymbolFile() const {
  return LinkingContext::createUndefinedSymbolFile(
      "<command line option /include>");
}

static int getGroupStartPos(std::vector<std::unique_ptr<Node>> &nodes) {
  for (int i = 0, e = nodes.size(); i < e; ++i)
    if (GroupEnd *group = dyn_cast<GroupEnd>(nodes[i].get()))
      return i - group->getSize();
  llvm::report_fatal_error("internal error");
}

void PECOFFLinkingContext::addLibraryFile(std::unique_ptr<FileNode> file) {
  GroupEnd *currentGroupEnd;
  int pos = -1;
  std::vector<std::unique_ptr<Node>> &elements = getNodes();
  for (int i = 0, e = elements.size(); i < e; ++i) {
    if ((currentGroupEnd = dyn_cast<GroupEnd>(elements[i].get()))) {
      pos = i;
      break;
    }
  }
  assert(pos >= 0);
  elements.insert(elements.begin() + pos, std::move(file));
  elements[pos + 1] = llvm::make_unique<GroupEnd>(
      currentGroupEnd->getSize() + 1);
}

bool PECOFFLinkingContext::createImplicitFiles(
    std::vector<std::unique_ptr<File>> &) {
  std::vector<std::unique_ptr<Node>> &members = getNodes();

  // Create a file for the entry point function.
  std::unique_ptr<FileNode> entry(new FileNode(
      llvm::make_unique<pecoff::EntryPointFile>(*this)));
  members.insert(members.begin() + getGroupStartPos(members), std::move(entry));

  // Create a file for __ImageBase.
  std::unique_ptr<FileNode> fileNode(new FileNode(
      llvm::make_unique<pecoff::LinkerGeneratedSymbolFile>(*this)));
  members.push_back(std::move(fileNode));

  // Create a file for _imp_ symbols.
  std::unique_ptr<FileNode> impFileNode(new FileNode(
      llvm::make_unique<pecoff::LocallyImportedSymbolFile>(*this)));
  members.push_back(std::move(impFileNode));

  // Create a file for dllexported symbols.
  std::unique_ptr<FileNode> exportNode(new FileNode(
      llvm::make_unique<pecoff::ExportedSymbolRenameFile>(*this)));
  addLibraryFile(std::move(exportNode));

  return true;
}

/// Returns the section name in the resulting executable.
///
/// Sections in object files are usually output to the executable with the same
/// name, but you can rename by command line option. /merge:from=to makes the
/// linker to combine "from" section contents to "to" section in the
/// executable. We have a mapping for the renaming. This method looks up the
/// table and returns a new section name if renamed.
StringRef
PECOFFLinkingContext::getOutputSectionName(StringRef sectionName) const {
  auto it = _renamedSections.find(sectionName);
  if (it == _renamedSections.end())
    return sectionName;
  return getOutputSectionName(it->second);
}

/// Adds a mapping to the section renaming table. This method will be used for
/// /merge command line option.
bool PECOFFLinkingContext::addSectionRenaming(raw_ostream &diagnostics,
                                              StringRef from, StringRef to) {
  auto it = _renamedSections.find(from);
  if (it != _renamedSections.end()) {
    if (it->second == to)
      // There's already the same mapping.
      return true;
    diagnostics << "Section \"" << from << "\" is already mapped to \""
                << it->second << ", so it cannot be mapped to \"" << to << "\".";
    return true;
  }

  // Add a mapping, and check if there's no cycle in the renaming mapping. The
  // cycle detection algorithm we use here is naive, but that's OK because the
  // number of mapping is usually less than 10.
  _renamedSections[from] = to;
  for (auto elem : _renamedSections) {
    StringRef sectionName = elem.first;
    std::set<StringRef> visited;
    visited.insert(sectionName);
    for (;;) {
      auto pos = _renamedSections.find(sectionName);
      if (pos == _renamedSections.end())
        break;
      if (visited.count(pos->second)) {
        diagnostics << "/merge:" << from << "=" << to << " makes a cycle";
        return false;
      }
      sectionName = pos->second;
      visited.insert(sectionName);
    }
  }
  return true;
}

/// Try to find the input library file from the search paths and append it to
/// the input file list. Returns true if the library file is found.
StringRef PECOFFLinkingContext::searchLibraryFile(StringRef filename) const {
  // Current directory always takes precedence over the search paths.
  if (llvm::sys::path::is_absolute(filename) || llvm::sys::fs::exists(filename))
    return filename;
  // Iterate over the search paths.
  for (StringRef dir : _inputSearchPaths) {
    SmallString<128> path = dir;
    llvm::sys::path::append(path, filename);
    if (llvm::sys::fs::exists(path.str()))
      return allocate(path.str());
  }
  return filename;
}

/// Returns the decorated name of the given symbol name. On 32-bit x86, it
/// adds "_" at the beginning of the string. On other architectures, the
/// return value is the same as the argument.
StringRef PECOFFLinkingContext::decorateSymbol(StringRef name) const {
  if (_machineType != llvm::COFF::IMAGE_FILE_MACHINE_I386)
    return name;
  std::string str = "_";
  str.append(name);
  return allocate(str);
}

StringRef PECOFFLinkingContext::undecorateSymbol(StringRef name) const {
  if (_machineType != llvm::COFF::IMAGE_FILE_MACHINE_I386)
    return name;
  if (!name.startswith("_"))
    return name;
  return name.substr(1);
}

uint64_t PECOFFLinkingContext::getBaseAddress() const {
  if (_baseAddress == invalidBaseAddress)
    return is64Bit() ? pe32PlusDefaultBaseAddress : pe32DefaultBaseAddress;
  return _baseAddress;
}

Writer &PECOFFLinkingContext::writer() const { return *_writer; }

void PECOFFLinkingContext::setSectionSetMask(StringRef sectionName,
                                             uint32_t newFlags) {
  _sectionSetMask[sectionName] |= newFlags;
  _sectionClearMask[sectionName] &= ~newFlags;
  const uint32_t rwx = (llvm::COFF::IMAGE_SCN_MEM_READ |
                        llvm::COFF::IMAGE_SCN_MEM_WRITE |
                        llvm::COFF::IMAGE_SCN_MEM_EXECUTE);
  if (newFlags & rwx)
    _sectionClearMask[sectionName] |= ~_sectionSetMask[sectionName] & rwx;
  assert((_sectionSetMask[sectionName] & _sectionClearMask[sectionName]) == 0);
}

void PECOFFLinkingContext::setSectionClearMask(StringRef sectionName,
                                               uint32_t newFlags) {
  _sectionClearMask[sectionName] |= newFlags;
  _sectionSetMask[sectionName] &= ~newFlags;
  assert((_sectionSetMask[sectionName] & _sectionClearMask[sectionName]) == 0);
}

uint32_t PECOFFLinkingContext::getSectionAttributes(StringRef sectionName,
                                                    uint32_t flags) const {
  auto si = _sectionSetMask.find(sectionName);
  uint32_t setMask = (si == _sectionSetMask.end()) ? 0 : si->second;
  auto ci = _sectionClearMask.find(sectionName);
  uint32_t clearMask = (ci == _sectionClearMask.end()) ? 0 : ci->second;
  return (flags | setMask) & ~clearMask;
}

// Returns true if two export descriptors are the same.
static bool sameExportDesc(const PECOFFLinkingContext::ExportDesc &a,
                           const PECOFFLinkingContext::ExportDesc &b) {
  return a.ordinal == b.ordinal && a.ordinal == b.ordinal &&
         a.noname == b.noname && a.isData == b.isData;
}

void PECOFFLinkingContext::addDllExport(ExportDesc &desc) {
  addInitialUndefinedSymbol(allocate(desc.name));

  // MSVC link.exe silently drops characters after the first atsign.
  // For example, /export:foo@4=bar is equivalent to /export:foo=bar.
  // We do the same thing for compatibility.
  if (!desc.externalName.empty()) {
    StringRef s(desc.externalName);
    size_t pos = s.find('@');
    if (pos != s.npos)
      desc.externalName = s.substr(0, pos);
  }

  // Scan the vector to look for existing entry. It's not very fast,
  // but because the number of exported symbol is usually not that
  // much, it should be okay.
  for (ExportDesc &e : _dllExports) {
    if (e.name != desc.name)
      continue;
    if (!sameExportDesc(e, desc))
      llvm::errs() << "Export symbol '" << desc.name
                   << "' specified more than once.\n";
    return;
  }
  _dllExports.push_back(desc);
}

static std::string replaceExtension(StringRef path, StringRef ext) {
  SmallString<128> ss = path;
  llvm::sys::path::replace_extension(ss, ext);
  return ss.str();
}

std::string PECOFFLinkingContext::getOutputImportLibraryPath() const {
  if (!_implib.empty())
    return _implib;
  return replaceExtension(outputPath(), ".lib");
}

std::string PECOFFLinkingContext::getPDBFilePath() const {
  assert(_debug);
  if (!_pdbFilePath.empty())
    return _pdbFilePath;
  return replaceExtension(outputPath(), ".pdb");
}

void PECOFFLinkingContext::addPasses(PassManager &pm) {
  pm.add(llvm::make_unique<pecoff::PDBPass>(*this));
  pm.add(llvm::make_unique<pecoff::EdataPass>(*this));
  pm.add(llvm::make_unique<pecoff::IdataPass>(*this));
  pm.add(llvm::make_unique<pecoff::OrderPass>());
  pm.add(llvm::make_unique<pecoff::LoadConfigPass>(*this));
  pm.add(llvm::make_unique<pecoff::InferSubsystemPass>(*this));
}

} // end namespace lld