summaryrefslogtreecommitdiffstats
path: root/lld/lib/ReaderWriter/PECOFF/EdataPass.cpp
blob: ad79f171f3c9f26d9043b0a0d2cbeb599de8155a (plain)
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
//===- lib/ReaderWriter/PECOFF/EdataPass.cpp ------------------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "Pass.h"
#include "EdataPass.h"
#include "lld/Core/File.h"
#include "lld/Core/Pass.h"
#include "lld/Core/Simple.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Path.h"
#include <climits>
#include <ctime>
#include <utility>

using lld::pecoff::edata::EdataAtom;
using lld::pecoff::edata::TableEntry;
using llvm::object::export_address_table_entry;
using llvm::object::export_directory_table_entry;

namespace lld {
namespace pecoff {

typedef PECOFFLinkingContext::ExportDesc ExportDesc;

// dedupExports removes duplicate export entries. If two exports are
// referring the same symbol, they are considered duplicates.
// This could happen if the same symbol name is specified as an argument
// to /export more than once, or an unmangled and mangled name of the
// same symbol are given to /export. In the latter case, we choose
// unmangled (shorter) name.
static void dedupExports(PECOFFLinkingContext &ctx) {
  std::vector<ExportDesc> &exports = ctx.getDllExports();
  // Pass 1: find duplicate entries
  std::set<const ExportDesc *> dup;
  std::map<StringRef, ExportDesc *> map;
  for (ExportDesc &exp : exports) {
    if (!exp.externalName.empty())
      continue;
    StringRef symbol = exp.getRealName();
    auto it = map.find(symbol);
    if (it == map.end()) {
      map[symbol] = &exp;
    } else if (symbol.size() < it->second->getRealName().size()) {
      map[symbol] = &exp;
      dup.insert(it->second);
    } else {
      dup.insert(&exp);
    }
  }
  // Pass 2: remove duplicate entries
  auto pred = [&](const ExportDesc &exp) {
    return dup.count(&exp) == 1;
  };
  exports.erase(std::remove_if(exports.begin(), exports.end(), pred),
                exports.end());
}

static void assignOrdinals(PECOFFLinkingContext &ctx) {
  std::vector<ExportDesc> &exports = ctx.getDllExports();
  int maxOrdinal = -1;
  for (ExportDesc &desc : exports)
    maxOrdinal = std::max(maxOrdinal, desc.ordinal);

  std::sort(exports.begin(), exports.end(),
            [](const ExportDesc &a, const ExportDesc &b) {
    return a.getExternalName().compare(b.getExternalName()) < 0;
  });

  int nextOrdinal = (maxOrdinal == -1) ? 1 : (maxOrdinal + 1);
  for (ExportDesc &desc : exports)
    if (desc.ordinal == -1)
      desc.ordinal = nextOrdinal++;
}

static bool getExportedAtoms(PECOFFLinkingContext &ctx, MutableFile *file,
                             std::vector<TableEntry> &ret) {
  std::map<StringRef, const DefinedAtom *> definedAtoms;
  for (const DefinedAtom *atom : file->defined())
    definedAtoms[atom->name()] = atom;

  for (PECOFFLinkingContext::ExportDesc &desc : ctx.getDllExports()) {
    auto it = definedAtoms.find(desc.getRealName());
    if (it == definedAtoms.end()) {
      llvm::errs() << "Symbol <" << desc.name
                   << "> is exported but not defined.\n";
      return false;
    }
    const DefinedAtom *atom = it->second;

    // One can export a symbol with a different name than the symbol
    // name used in DLL. If such name is specified, use it in the
    // .edata section.
    ret.push_back(TableEntry(ctx.undecorateSymbol(desc.getExternalName()),
                             desc.ordinal, atom, desc.noname));
  }
  std::sort(ret.begin(), ret.end(),
            [](const TableEntry &a, const TableEntry &b) {
    return a.exportName.compare(b.exportName) < 0;
  });

  return true;
}

static std::pair<int, int> getOrdinalBase(std::vector<TableEntry> &entries) {
  int ordinalBase = INT_MAX;
  int maxOrdinal = -1;
  for (TableEntry &e : entries) {
    ordinalBase = std::min(ordinalBase, e.ordinal);
    maxOrdinal = std::max(maxOrdinal, e.ordinal);
  }
  return std::pair<int, int>(ordinalBase, maxOrdinal);
}

edata::EdataAtom *
EdataPass::createAddressTable(const std::vector<TableEntry> &entries,
                              int ordinalBase, int maxOrdinal) {
  EdataAtom *addressTable =
      new (_alloc) EdataAtom(_file, sizeof(export_address_table_entry) *
                                        (maxOrdinal - ordinalBase + 1));

  for (const TableEntry &e : entries) {
    int index = e.ordinal - ordinalBase;
    size_t offset = index * sizeof(export_address_table_entry);
    addDir32NBReloc(addressTable, e.atom, _ctx.getMachineType(), offset);
  }
  return addressTable;
}

edata::EdataAtom *
EdataPass::createNamePointerTable(const PECOFFLinkingContext &ctx,
                                  const std::vector<TableEntry> &entries,
                                  MutableFile *file) {
  EdataAtom *table =
      new (_alloc) EdataAtom(_file, sizeof(uint32_t) * entries.size());

  size_t offset = 0;
  for (const TableEntry &e : entries) {
    auto *stringAtom = new (_alloc) COFFStringAtom(
        _file, _stringOrdinal++, ".edata", e.exportName);
    file->addAtom(*stringAtom);
    addDir32NBReloc(table, stringAtom, _ctx.getMachineType(), offset);
    offset += sizeof(uint32_t);
  }
  return table;
}

edata::EdataAtom *EdataPass::createExportDirectoryTable(
    const std::vector<edata::TableEntry> &namedEntries, int ordinalBase,
    int maxOrdinal) {
  EdataAtom *ret =
      new (_alloc) EdataAtom(_file, sizeof(export_directory_table_entry));
  auto *data = ret->getContents<export_directory_table_entry>();
  data->TimeDateStamp = time(nullptr);
  data->OrdinalBase = ordinalBase;
  data->AddressTableEntries = maxOrdinal - ordinalBase + 1;
  data->NumberOfNamePointers = namedEntries.size();
  return ret;
}

edata::EdataAtom *
EdataPass::createOrdinalTable(const std::vector<TableEntry> &entries,
                              int ordinalBase) {
  EdataAtom *ret =
      new (_alloc) EdataAtom(_file, sizeof(uint16_t) * entries.size());
  uint16_t *data = ret->getContents<uint16_t>();
  int i = 0;
  for (const TableEntry &e : entries)
    data[i++] = e.ordinal - ordinalBase;
  return ret;
}

void EdataPass::perform(std::unique_ptr<MutableFile> &file) {
  dedupExports(_ctx);
  assignOrdinals(_ctx);

  std::vector<TableEntry> entries;
  if (!getExportedAtoms(_ctx, file.get(), entries))
    return;
  if (entries.empty())
    return;

  int ordinalBase, maxOrdinal;
  std::tie(ordinalBase, maxOrdinal) = getOrdinalBase(entries);

  std::vector<TableEntry> namedEntries;
  for (TableEntry &e : entries)
    if (!e.noname)
      namedEntries.push_back(e);

  EdataAtom *table =
      createExportDirectoryTable(namedEntries, ordinalBase, maxOrdinal);
  file->addAtom(*table);

  COFFStringAtom *dllName =
      new (_alloc) COFFStringAtom(_file, _stringOrdinal++, ".edata",
                                  llvm::sys::path::filename(_ctx.outputPath()));
  file->addAtom(*dllName);
  addDir32NBReloc(table, dllName, _ctx.getMachineType(),
                  offsetof(export_directory_table_entry, NameRVA));

  EdataAtom *addressTable =
      createAddressTable(entries, ordinalBase, maxOrdinal);
  file->addAtom(*addressTable);
  addDir32NBReloc(
      table, addressTable, _ctx.getMachineType(),
      offsetof(export_directory_table_entry, ExportAddressTableRVA));

  EdataAtom *namePointerTable =
      createNamePointerTable(_ctx, namedEntries, file.get());
  file->addAtom(*namePointerTable);
  addDir32NBReloc(table, namePointerTable, _ctx.getMachineType(),
                  offsetof(export_directory_table_entry, NamePointerRVA));

  EdataAtom *ordinalTable = createOrdinalTable(namedEntries, ordinalBase);
  file->addAtom(*ordinalTable);
  addDir32NBReloc(table, ordinalTable, _ctx.getMachineType(),
                  offsetof(export_directory_table_entry, OrdinalTableRVA));
}

} // namespace pecoff
} // namespace lld
OpenPOWER on IntegriCloud