2012-06-01 06:34:00 +08:00
|
|
|
//===- lib/ReaderWriter/MachO/StubsPass.hpp -------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-11-15 11:09:26 +08:00
|
|
|
#ifndef LLD_READER_WRITER_MACHO_STUBS_PASS_H
|
|
|
|
#define LLD_READER_WRITER_MACHO_STUBS_PASS_H
|
2012-06-01 06:34:00 +08:00
|
|
|
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
|
|
|
|
#include "lld/Core/DefinedAtom.h"
|
|
|
|
#include "lld/Core/SharedLibraryAtom.h"
|
|
|
|
#include "lld/Core/File.h"
|
|
|
|
#include "lld/Core/Reference.h"
|
|
|
|
#include "lld/Core/Pass.h"
|
2013-01-25 06:52:25 +08:00
|
|
|
#include "lld/ReaderWriter/Simple.h"
|
2012-06-01 06:34:00 +08:00
|
|
|
|
|
|
|
#include "ReferenceKinds.h"
|
|
|
|
#include "StubAtoms.hpp"
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace mach_o {
|
|
|
|
|
|
|
|
|
|
|
|
class StubsPass : public lld::StubsPass {
|
|
|
|
public:
|
2013-08-07 06:31:59 +08:00
|
|
|
StubsPass(const MachOLinkingContext &context)
|
|
|
|
: _context(context)
|
|
|
|
, _kindHandler(_context.kindHandler())
|
|
|
|
, _file(context)
|
2013-01-25 15:39:18 +08:00
|
|
|
, _helperCommonAtom(nullptr)
|
|
|
|
, _helperCacheAtom(nullptr)
|
|
|
|
, _helperBinderAtom(nullptr) {
|
2012-06-01 06:34:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool noTextRelocs() {
|
2013-04-05 02:59:24 +08:00
|
|
|
return true;
|
2012-06-01 06:34:00 +08:00
|
|
|
}
|
|
|
|
|
[lld] Introduce registry and Reference kind tuple
The main changes are in:
include/lld/Core/Reference.h
include/lld/ReaderWriter/Reader.h
Everything else is details to support the main change.
1) Registration based Readers
Previously, lld had a tangled interdependency with all the Readers. It would
have been impossible to make a streamlined linker (say for a JIT) which
just supported one file format and one architecture (no yaml, no archives, etc).
The old model also required a LinkingContext to read an object file, which
would have made .o inspection tools awkward.
The new model is that there is a global Registry object. You programmatically
register the Readers you want with the registry object. Whenever you need to
read/parse a file, you ask the registry to do it, and the registry tries each
registered reader.
For ease of use with the existing lld code base, there is one Registry
object inside the LinkingContext object.
2) Changing kind value to be a tuple
Beside Readers, the registry also keeps track of the mapping for Reference
Kind values to and from strings. Along with that, this patch also fixes
an ambiguity with the previous Reference::Kind values. The problem was that
we wanted to reuse existing relocation type values as Reference::Kind values.
But then how can the YAML write know how to convert a value to a string? The
fix is to change the 32-bit Reference::Kind into a tuple with an 8-bit namespace
(e.g. ELF, COFFF, etc), an 8-bit architecture (e.g. x86_64, PowerPC, etc), and
a 16-bit value. This tuple system allows conversion to and from strings with
no ambiguities.
llvm-svn: 197727
2013-12-20 05:58:00 +08:00
|
|
|
virtual bool isCallSite(const Reference &ref) {
|
|
|
|
return _kindHandler.isCallSite(ref);
|
2012-06-01 06:34:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual const DefinedAtom* getStub(const Atom& target) {
|
|
|
|
auto pos = _targetToStub.find(&target);
|
|
|
|
if ( pos != _targetToStub.end() ) {
|
|
|
|
// Reuse an existing stub.
|
|
|
|
assert(pos->second != nullptr);
|
|
|
|
return pos->second;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// There is no existing stub, so create a new one.
|
|
|
|
return this->makeStub(target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const DefinedAtom* makeStub(const Atom& target) {
|
2013-08-07 06:31:59 +08:00
|
|
|
switch (_context.arch()) {
|
|
|
|
case MachOLinkingContext::arch_x86_64:
|
2012-06-01 06:34:00 +08:00
|
|
|
return makeStub_x86_64(target);
|
2013-08-07 06:31:59 +08:00
|
|
|
case MachOLinkingContext::arch_x86:
|
2012-06-01 06:34:00 +08:00
|
|
|
return makeStub_x86(target);
|
2013-08-07 06:31:59 +08:00
|
|
|
case MachOLinkingContext::arch_armv6:
|
|
|
|
case MachOLinkingContext::arch_armv7:
|
|
|
|
case MachOLinkingContext::arch_armv7s:
|
2012-06-01 06:34:00 +08:00
|
|
|
return makeStub_arm(target);
|
2013-01-23 09:18:43 +08:00
|
|
|
default:
|
2013-04-05 02:59:24 +08:00
|
|
|
llvm_unreachable("Unknown mach-o arch");
|
2012-06-01 06:34:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const DefinedAtom* makeStub_x86_64(const Atom& target) {
|
|
|
|
if ( _helperCommonAtom == nullptr ) {
|
|
|
|
// Lazily create common helper code and data.
|
|
|
|
_helperCacheAtom = new X86_64NonLazyPointerAtom(_file);
|
|
|
|
_binderAtom = new StubBinderAtom(_file);
|
|
|
|
_helperBinderAtom = new X86_64NonLazyPointerAtom(_file, *_binderAtom);
|
|
|
|
_helperCommonAtom = new X86_64StubHelperCommonAtom(_file,
|
|
|
|
*_helperCacheAtom, *_helperBinderAtom);
|
|
|
|
}
|
|
|
|
const DefinedAtom* helper = new X86_64StubHelperAtom(_file,
|
|
|
|
*_helperCommonAtom);
|
|
|
|
_stubHelperAtoms.push_back(helper);
|
|
|
|
const DefinedAtom* lp = new X86_64LazyPointerAtom(_file, *helper, target);
|
|
|
|
assert(lp->contentType() == DefinedAtom::typeLazyPointer);
|
|
|
|
_lazyPointers.push_back(lp);
|
|
|
|
const DefinedAtom* stub = new X86_64StubAtom(_file, *lp);
|
|
|
|
assert(stub->contentType() == DefinedAtom::typeStub);
|
|
|
|
_targetToStub[&target] = stub;
|
|
|
|
return stub;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DefinedAtom* makeStub_x86(const Atom& target) {
|
2012-06-12 06:53:15 +08:00
|
|
|
if ( _helperCommonAtom == nullptr ) {
|
|
|
|
// Lazily create common helper code and data.
|
|
|
|
_helperCacheAtom = new X86NonLazyPointerAtom(_file);
|
|
|
|
_binderAtom = new StubBinderAtom(_file);
|
|
|
|
_helperBinderAtom = new X86NonLazyPointerAtom(_file, *_binderAtom);
|
|
|
|
_helperCommonAtom = new X86StubHelperCommonAtom(_file,
|
|
|
|
*_helperCacheAtom, *_helperBinderAtom);
|
|
|
|
}
|
|
|
|
const DefinedAtom* helper = new X86StubHelperAtom(_file,
|
|
|
|
*_helperCommonAtom);
|
|
|
|
_stubHelperAtoms.push_back(helper);
|
|
|
|
const DefinedAtom* lp = new X86LazyPointerAtom(_file, *helper, target);
|
|
|
|
assert(lp->contentType() == DefinedAtom::typeLazyPointer);
|
|
|
|
_lazyPointers.push_back(lp);
|
|
|
|
const DefinedAtom* stub = new X86StubAtom(_file, *lp);
|
|
|
|
assert(stub->contentType() == DefinedAtom::typeStub);
|
|
|
|
_targetToStub[&target] = stub;
|
|
|
|
return stub;
|
2012-06-01 06:34:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const DefinedAtom* makeStub_arm(const Atom& target) {
|
|
|
|
assert(0 && "stubs not yet implemented for arm");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-12 10:45:54 +08:00
|
|
|
virtual void addStubAtoms(MutableFile &mergedFile) {
|
2012-07-31 04:10:46 +08:00
|
|
|
// Exit early if no stubs needed.
|
2013-11-13 13:19:47 +08:00
|
|
|
if (_targetToStub.empty())
|
2012-07-31 04:10:46 +08:00
|
|
|
return;
|
2012-06-01 06:34:00 +08:00
|
|
|
// Add all stubs to master file.
|
|
|
|
for (auto it : _targetToStub) {
|
|
|
|
mergedFile.addAtom(*it.second);
|
|
|
|
}
|
|
|
|
// Add helper code atoms.
|
|
|
|
mergedFile.addAtom(*_helperCommonAtom);
|
|
|
|
for (const DefinedAtom *lp : _stubHelperAtoms) {
|
|
|
|
mergedFile.addAtom(*lp);
|
|
|
|
}
|
|
|
|
// Add GOT slots used for lazy binding.
|
|
|
|
mergedFile.addAtom(*_helperBinderAtom);
|
|
|
|
mergedFile.addAtom(*_helperCacheAtom);
|
|
|
|
// Add all lazy pointers to master file.
|
|
|
|
for (const DefinedAtom *lp : _lazyPointers) {
|
|
|
|
mergedFile.addAtom(*lp);
|
|
|
|
}
|
|
|
|
// Add sharedlibrary atom
|
|
|
|
mergedFile.addAtom(*_binderAtom);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
class File : public SimpleFile {
|
|
|
|
public:
|
2013-12-20 15:48:29 +08:00
|
|
|
File(const MachOLinkingContext &context) : SimpleFile("MachO Stubs pass") {}
|
2012-06-01 06:34:00 +08:00
|
|
|
};
|
|
|
|
|
2013-12-20 15:48:29 +08:00
|
|
|
const MachOLinkingContext &_context;
|
2013-04-05 02:59:24 +08:00
|
|
|
mach_o::KindHandler &_kindHandler;
|
2012-06-01 06:34:00 +08:00
|
|
|
File _file;
|
|
|
|
llvm::DenseMap<const Atom*, const DefinedAtom*> _targetToStub;
|
|
|
|
std::vector<const DefinedAtom*> _lazyPointers;
|
|
|
|
std::vector<const DefinedAtom*> _stubHelperAtoms;
|
|
|
|
const SharedLibraryAtom *_binderAtom;
|
|
|
|
const DefinedAtom* _helperCommonAtom;
|
|
|
|
const DefinedAtom* _helperCacheAtom;
|
|
|
|
const DefinedAtom* _helperBinderAtom;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace mach_o
|
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
|
2013-11-15 11:09:26 +08:00
|
|
|
#endif // LLD_READER_WRITER_MACHO_STUBS_PASS_H
|