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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
//===- lib/ReaderWriter/PECOFF/ReaderCOFF.cpp -----------------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "ReaderCOFF"
#include "Atoms.h"
#include "ReaderImportHeader.h"
#include "lld/Core/File.h"
#include "lld/ReaderWriter/Reader.h"
#include "lld/ReaderWriter/ReaderArchive.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Object/COFF.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Memory.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/system_error.h"
#include <map>
#include <vector>
using std::vector;
using lld::coff::COFFAbsoluteAtom;
using lld::coff::COFFDefinedAtom;
using lld::coff::COFFReference;
using lld::coff::COFFUndefinedAtom;
using llvm::object::coff_relocation;
using llvm::object::coff_section;
using llvm::object::coff_symbol;
using namespace lld;
namespace { // anonymous
class FileCOFF : public File {
private:
typedef vector<const coff_symbol *> SymbolVectorT;
typedef std::map<const coff_section *, SymbolVectorT> SectionToSymbolsT;
typedef std::map<const StringRef, Atom *> SymbolNameToAtomT;
typedef std::map<const coff_section *, vector<COFFDefinedAtom *> >
SectionToAtomsT;
public:
FileCOFF(const TargetInfo &ti, std::unique_ptr<llvm::MemoryBuffer> MB,
error_code &EC)
: File(MB->getBufferIdentifier(), kindObject), _targetInfo(ti) {
llvm::OwningPtr<llvm::object::Binary> Bin;
EC = llvm::object::createBinary(MB.release(), Bin);
if (EC)
return;
_obj.reset(llvm::dyn_cast<const llvm::object::COFFObjectFile>(Bin.get()));
if (!_obj) {
EC = make_error_code(llvm::object::object_error::invalid_file_type);
return;
}
Bin.take();
// Read the symbol table and atomize them if possible. Defined atoms
// cannot be atomized in one pass, so they will be not be atomized but
// added to symbolToAtom.
SectionToSymbolsT definedSymbols;
SymbolNameToAtomT symbolToAtom;
if ((EC = readSymbolTable(_absoluteAtoms._atoms, _undefinedAtoms._atoms,
definedSymbols, symbolToAtom)))
return;
// Atomize defined symbols. This is a separate pass from readSymbolTable()
// because in order to create an atom for a symbol we need to the adjacent
// symbols.
SectionToAtomsT sectionToAtoms;
if ((EC = AtomizeDefinedSymbols(definedSymbols, _definedAtoms._atoms,
symbolToAtom, sectionToAtoms)))
return;
EC = addRelocationReferenceToAtoms(symbolToAtom, sectionToAtoms);
}
virtual const atom_collection<DefinedAtom> &defined() const {
return _definedAtoms;
}
virtual const atom_collection<UndefinedAtom> &undefined() const {
return _undefinedAtoms;
}
virtual const atom_collection<SharedLibraryAtom> &sharedLibrary() const {
return _sharedLibraryAtoms;
}
virtual const atom_collection<AbsoluteAtom> &absolute() const {
return _absoluteAtoms;
}
virtual const TargetInfo &getTargetInfo() const { return _targetInfo; }
private:
/// Iterate over symbol table to process all symbols. Absolute or undefined
/// symbols are atomized in this method. Defined symbols are not atomized
/// but added to DefinedSymbols as is for further processing. Note that this
/// function is const, so it will not mutate objects other than arguments.
error_code readSymbolTable(vector<const AbsoluteAtom *> &absoluteAtoms,
vector<const UndefinedAtom *> &undefinedAtoms,
SectionToSymbolsT &definedSymbols,
SymbolNameToAtomT &symbolToAtom) const {
const llvm::object::coff_file_header *Header = nullptr;
if (error_code ec = _obj->getHeader(Header))
return ec;
for (uint32_t i = 0, e = Header->NumberOfSymbols; i != e; ++i) {
const coff_symbol *Symb;
if (error_code ec = _obj->getSymbol(i, Symb))
return ec;
StringRef Name;
if (error_code ec = _obj->getSymbolName(Symb, Name))
return ec;
int16_t SectionIndex = Symb->SectionNumber;
assert(SectionIndex != llvm::COFF::IMAGE_SYM_DEBUG &&
"Cannot atomize IMAGE_SYM_DEBUG!");
// Skip aux symbols.
i += Symb->NumberOfAuxSymbols;
// Create an absolute atom.
if (SectionIndex == llvm::COFF::IMAGE_SYM_ABSOLUTE) {
auto *atom = new (_atomStorage.Allocate<COFFAbsoluteAtom>())
COFFAbsoluteAtom(*this, Name, Symb);
if (!Name.empty())
symbolToAtom[Name] = atom;
absoluteAtoms.push_back(atom);
continue;
}
// Create an undefined atom.
if (SectionIndex == llvm::COFF::IMAGE_SYM_UNDEFINED) {
auto *atom = new (_atomStorage.Allocate<COFFUndefinedAtom>())
COFFUndefinedAtom(*this, Name);
if (!Name.empty())
symbolToAtom[Name] = atom;
undefinedAtoms.push_back(atom);
continue;
}
// This is actually a defined symbol. Add it to its section's list of
// symbols.
uint8_t SC = Symb->StorageClass;
if (SC != llvm::COFF::IMAGE_SYM_CLASS_EXTERNAL &&
SC != llvm::COFF::IMAGE_SYM_CLASS_STATIC &&
SC != llvm::COFF::IMAGE_SYM_CLASS_FUNCTION &&
SC != llvm::COFF::IMAGE_SYM_CLASS_LABEL) {
llvm::errs() << "Unable to create atom for: " << Name
<< " (" << (int)SC << ")\n";
return llvm::object::object_error::parse_failed;
}
const coff_section *Sec;
if (error_code ec = _obj->getSection(SectionIndex, Sec))
return ec;
assert(Sec && "SectionIndex > 0, Sec must be non-null!");
definedSymbols[Sec].push_back(Symb);
}
return error_code::success();
}
/// Atomize \p symbols and append the results to \p atoms. The symbols are
/// assumed to have been defined in the \p section.
error_code
AtomizeDefinedSymbolsInSection(const coff_section *section,
vector<const coff_symbol *> &symbols,
vector<COFFDefinedAtom *> &atoms) const {
// Sort symbols by position.
std::stable_sort(symbols.begin(), symbols.end(),
// For some reason MSVC fails to allow the lambda in this context with a
// "illegal use of local type in type instantiation". MSVC is clearly
// wrong here. Force a conversion to function pointer to work around.
static_cast<bool(*)(const coff_symbol*, const coff_symbol*)>(
[](const coff_symbol *A, const coff_symbol *B) -> bool {
return A->Value < B->Value;
}));
ArrayRef<uint8_t> SecData;
StringRef sectionName;
if (error_code ec = _obj->getSectionContents(section, SecData))
return ec;
if (error_code ec = _obj->getSectionName(section, sectionName))
return ec;
uint64_t ordinal = 0;
// We do not support debug information yet. We could keep data in ".debug$S"
// section in the resultant binary by copying as opaque bytes, but it would
// make the binary hard to debug because of extraneous data. So we'll skip
// the debug info.
if (sectionName == ".debug$S")
return error_code::success();
// A section with IMAGE_SCN_LNK_REMOVE attribute will never become
// a part of the output image. That's what the COFF spec says.
if (section->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE)
return error_code::success();
// Create an atom for the entire section.
if (symbols.empty()) {
ArrayRef<uint8_t> Data(SecData.data(), SecData.size());
atoms.push_back(new (_atomStorage.Allocate<COFFDefinedAtom>())
COFFDefinedAtom(*this, "", nullptr, section, Data,
sectionName, ordinal++));
return error_code::success();
}
// Create an unnamed atom if the first atom isn't at the start of the
// section.
if (symbols[0]->Value != 0) {
uint64_t Size = symbols[0]->Value;
ArrayRef<uint8_t> Data(SecData.data(), Size);
atoms.push_back(new (_atomStorage.Allocate<COFFDefinedAtom>())
COFFDefinedAtom(*this, "", nullptr, section, Data,
sectionName, ordinal++));
}
for (auto si = symbols.begin(), se = symbols.end(); si != se; ++si) {
const uint8_t *start = SecData.data() + (*si)->Value;
// if this is the last symbol, take up the remaining data.
const uint8_t *end = (si + 1 == se)
? start + SecData.size()
: SecData.data() + (*(si + 1))->Value;
ArrayRef<uint8_t> Data(start, end);
StringRef name;
if (error_code ec = _obj->getSymbolName(*si, name))
return ec;
atoms.push_back(new (_atomStorage.Allocate<COFFDefinedAtom>())
COFFDefinedAtom(*this, name, *si, section, Data,
sectionName, ordinal++));
}
return error_code::success();
}
error_code AtomizeDefinedSymbols(SectionToSymbolsT &definedSymbols,
vector<const DefinedAtom *> &definedAtoms,
SymbolNameToAtomT &symbolToAtom,
SectionToAtomsT §ionToAtoms) const {
// For each section, make atoms for all the symbols defined in the
// section, and append the atoms to the result objects.
for (auto &i : definedSymbols) {
const coff_section *section = i.first;
vector<const coff_symbol *> &symbols = i.second;
vector<COFFDefinedAtom *> atoms;
if (error_code ec =
AtomizeDefinedSymbolsInSection(section, symbols, atoms))
return ec;
// Connect atoms with layout-before/layout-after edges.
connectAtomsWithLayoutEdge(atoms);
for (COFFDefinedAtom *atom : atoms) {
if (!atom->name().empty())
symbolToAtom[atom->name()] = atom;
sectionToAtoms[section].push_back(atom);
definedAtoms.push_back(atom);
}
}
return error_code::success();
}
/// Find the atom that is at \p targetOffset in \p section. It is assumed
/// that \p atoms are sorted by position in the section.
COFFDefinedAtom *findAtomAt(uint32_t targetOffset,
const coff_section *section,
const vector<COFFDefinedAtom *> &atoms) const {
assert(std::is_sorted(atoms.begin(), atoms.end(),
[](const COFFDefinedAtom * a,
const COFFDefinedAtom * b) -> bool {
return a->originalOffset() < b->originalOffset();
}));
for (COFFDefinedAtom *atom : atoms)
if (targetOffset < atom->originalOffset() + atom->size())
return atom;
llvm_unreachable("Relocation target out of range");
}
/// Find the atom for the symbol that was at the \p index in the symbol
/// table.
error_code getAtomBySymbolIndex(uint32_t index,
SymbolNameToAtomT symbolToAtom,
Atom *&ret) const {
const coff_symbol *symbol;
if (error_code ec = _obj->getSymbol(index, symbol))
return ec;
StringRef symbolName;
if (error_code ec = _obj->getSymbolName(symbol, symbolName))
return ec;
ret = symbolToAtom[symbolName];
assert(ret);
return error_code::success();
}
/// Add relocation information to an atom based on \p rel. \p rel is an
/// relocation entry for the \p section, and \p atoms are all the atoms
/// defined in the \p section.
error_code
addRelocationReference(const coff_relocation *rel,
const coff_section *section,
const vector<COFFDefinedAtom *> &atoms,
const SymbolNameToAtomT symbolToAtom) const {
assert(atoms.size() > 0);
// The address of the item which relocation is applied. Section's
// VirtualAddress needs to be added for historical reasons, but the value
// is usually just zero, so adding it is usually no-op.
uint32_t itemAddress = rel->VirtualAddress + section->VirtualAddress;
Atom *targetAtom = nullptr;
if (error_code ec = getAtomBySymbolIndex(rel->SymbolTableIndex,
symbolToAtom, targetAtom))
return ec;
COFFDefinedAtom *atom = findAtomAt(rel->VirtualAddress, section, atoms);
uint32_t offsetInAtom = itemAddress - atom->originalOffset();
assert(offsetInAtom < atom->size());
atom->addReference(std::unique_ptr<COFFReference>(
new COFFReference(targetAtom, offsetInAtom, rel->Type)));
return error_code::success();
}
/// Add relocation information to atoms.
error_code addRelocationReferenceToAtoms(SymbolNameToAtomT symbolToAtom,
SectionToAtomsT §ionToAtoms) {
// Relocation entries are defined for each section.
error_code ec;
for (auto si = _obj->begin_sections(), se = _obj->end_sections(); si != se;
si.increment(ec)) {
const coff_section *section = _obj->getCOFFSection(si);
for (auto ri = si->begin_relocations(), re = si->end_relocations();
ri != re; ri.increment(ec)) {
const coff_relocation *rel = _obj->getCOFFRelocation(ri);
if ((ec = addRelocationReference(rel, section, sectionToAtoms[section],
symbolToAtom)))
return ec;
}
}
return error_code::success();
}
std::unique_ptr<const llvm::object::COFFObjectFile> _obj;
atom_collection_vector<DefinedAtom> _definedAtoms;
atom_collection_vector<UndefinedAtom> _undefinedAtoms;
atom_collection_vector<SharedLibraryAtom> _sharedLibraryAtoms;
atom_collection_vector<AbsoluteAtom> _absoluteAtoms;
mutable llvm::BumpPtrAllocator _atomStorage;
const TargetInfo &_targetInfo;
};
class ReaderCOFF : public Reader {
public:
explicit ReaderCOFF(const TargetInfo &ti)
: Reader(ti), _readerArchive(ti, *this) {}
error_code parseFile(std::unique_ptr<MemoryBuffer> &mb,
std::vector<std::unique_ptr<File> > &result) const {
StringRef magic(mb->getBufferStart(), mb->getBufferSize());
llvm::sys::fs::file_magic fileType = llvm::sys::fs::identify_magic(magic);
if (fileType == llvm::sys::fs::file_magic::coff_object)
return parseCOFFFile(mb, result);
if (fileType == llvm::sys::fs::file_magic::archive)
return _readerArchive.parseFile(mb, result);
return lld::coff::parseCOFFImportLibrary(_targetInfo, mb, result);
}
private:
error_code parseCOFFFile(std::unique_ptr<MemoryBuffer> &mb,
std::vector<std::unique_ptr<File> > &result) const {
error_code ec;
std::unique_ptr<File> file(new FileCOFF(_targetInfo, std::move(mb), ec));
if (ec)
return ec;
DEBUG({
llvm::dbgs() << "Defined atoms:\n";
for (const auto &atom : file->defined()) {
llvm::dbgs() << " " << atom->name() << "\n";
for (const Reference *ref : *atom)
llvm::dbgs() << " @" << ref->offsetInAtom() << " -> "
<< ref->target()->name() << "\n";
}
});
result.push_back(std::move(file));
return error_code::success();
}
ReaderArchive _readerArchive;
};
} // end namespace anonymous
namespace lld {
std::unique_ptr<Reader> createReaderPECOFF(const TargetInfo & ti) {
return std::unique_ptr<Reader>(new ReaderCOFF(ti));
}
}
|