2013-01-30 06:03:39 +08:00
|
|
|
//===- lib/ReaderWriter/ELF/ExecutableAtoms.h -----------------------------===//
|
2013-01-10 11:16:27 +08:00
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-30 06:03:39 +08:00
|
|
|
#ifndef LLD_READER_WRITER_ELF_EXECUTABLE_ATOM_H
|
|
|
|
#define LLD_READER_WRITER_ELF_EXECUTABLE_ATOM_H
|
2013-01-10 11:16:27 +08:00
|
|
|
|
2013-01-30 06:03:39 +08:00
|
|
|
#include "Atoms.h"
|
|
|
|
#include "File.h"
|
2013-01-30 03:53:41 +08:00
|
|
|
|
2013-01-10 11:16:27 +08:00
|
|
|
#include "lld/Core/DefinedAtom.h"
|
|
|
|
#include "lld/Core/File.h"
|
|
|
|
#include "lld/Core/Reference.h"
|
2013-01-25 15:39:18 +08:00
|
|
|
#include "lld/Core/UndefinedAtom.h"
|
2013-01-23 09:18:43 +08:00
|
|
|
#include "lld/ReaderWriter/Writer.h"
|
2013-01-10 11:16:27 +08:00
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace elf {
|
|
|
|
/// \brief All atoms are owned by a File. To add linker specific atoms
|
2013-03-15 00:09:49 +08:00
|
|
|
/// the atoms need to be inserted to a file called (CRuntimeFile) which
|
|
|
|
/// are basically additional symbols required by libc and other runtime
|
2013-01-10 11:16:27 +08:00
|
|
|
/// libraries part of executing a program. This class provides support
|
|
|
|
/// for adding absolute symbols and undefined symbols
|
2013-01-30 06:03:39 +08:00
|
|
|
template <class ELFT> class CRuntimeFile : public ELFFile<ELFT> {
|
2013-01-10 11:16:27 +08:00
|
|
|
public:
|
2013-01-15 15:53:22 +08:00
|
|
|
typedef llvm::object::Elf_Sym_Impl<ELFT> Elf_Sym;
|
2013-02-24 22:54:02 +08:00
|
|
|
CRuntimeFile(const ELFTargetInfo &ti, StringRef name = "C runtime")
|
|
|
|
: ELFFile<ELFT>(ti, name) {}
|
2013-01-25 15:39:18 +08:00
|
|
|
|
2013-01-10 11:16:27 +08:00
|
|
|
/// \brief add a global absolute atom
|
2013-02-24 23:11:55 +08:00
|
|
|
virtual void addAbsoluteAtom(StringRef symbolName) {
|
2013-01-30 15:11:43 +08:00
|
|
|
Elf_Sym *symbol = new (_allocator.Allocate<Elf_Sym>()) Elf_Sym;
|
2013-01-10 11:16:27 +08:00
|
|
|
symbol->st_name = 0;
|
|
|
|
symbol->st_value = 0;
|
|
|
|
symbol->st_shndx = llvm::ELF::SHN_ABS;
|
2013-03-15 00:09:49 +08:00
|
|
|
symbol->setBindingAndType(llvm::ELF::STB_GLOBAL,
|
2013-01-10 11:16:27 +08:00
|
|
|
llvm::ELF::STT_OBJECT);
|
|
|
|
symbol->st_other = llvm::ELF::STV_DEFAULT;
|
|
|
|
symbol->st_size = 0;
|
2013-01-30 06:03:39 +08:00
|
|
|
auto *newAtom =
|
|
|
|
new (_allocator) ELFAbsoluteAtom<ELFT>(*this, symbolName, symbol, -1);
|
2013-01-10 11:16:27 +08:00
|
|
|
_absoluteAtoms._atoms.push_back(newAtom);
|
|
|
|
}
|
|
|
|
|
2013-03-15 00:09:49 +08:00
|
|
|
/// \brief add an undefined atom
|
2013-02-24 23:11:55 +08:00
|
|
|
virtual void addUndefinedAtom(StringRef symbolName) {
|
2013-01-30 06:03:39 +08:00
|
|
|
Elf_Sym *symbol = new (_allocator) Elf_Sym;
|
2013-01-10 11:16:27 +08:00
|
|
|
symbol->st_name = 0;
|
|
|
|
symbol->st_value = 0;
|
|
|
|
symbol->st_shndx = llvm::ELF::SHN_UNDEF;
|
|
|
|
symbol->st_other = llvm::ELF::STV_DEFAULT;
|
|
|
|
symbol->st_size = 0;
|
2013-01-30 06:03:39 +08:00
|
|
|
auto *newAtom =
|
|
|
|
new (_allocator) ELFUndefinedAtom<ELFT>(*this, symbolName, symbol);
|
2013-01-10 11:16:27 +08:00
|
|
|
_undefinedAtoms._atoms.push_back(newAtom);
|
|
|
|
}
|
|
|
|
|
2013-02-24 23:11:55 +08:00
|
|
|
virtual const File::atom_collection<DefinedAtom> &defined() const {
|
2013-01-10 11:16:27 +08:00
|
|
|
return _definedAtoms;
|
|
|
|
}
|
|
|
|
|
2013-02-24 23:11:55 +08:00
|
|
|
virtual const File::atom_collection<UndefinedAtom> &undefined() const {
|
2013-01-10 11:16:27 +08:00
|
|
|
return _undefinedAtoms;
|
|
|
|
}
|
|
|
|
|
2013-02-24 23:11:55 +08:00
|
|
|
virtual const File::atom_collection<SharedLibraryAtom> &sharedLibrary() const {
|
2013-01-10 11:16:27 +08:00
|
|
|
return _sharedLibraryAtoms;
|
|
|
|
}
|
|
|
|
|
2013-02-24 23:11:55 +08:00
|
|
|
virtual const File::atom_collection<AbsoluteAtom> &absolute() const {
|
2013-01-10 11:16:27 +08:00
|
|
|
return _absoluteAtoms;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cannot add atoms to C Runtime file
|
2013-02-24 22:54:02 +08:00
|
|
|
virtual void addAtom(const Atom &) {
|
|
|
|
llvm_unreachable("cannot add atoms to Runtime files");
|
2013-01-10 11:16:27 +08:00
|
|
|
}
|
|
|
|
|
2013-02-24 23:11:55 +08:00
|
|
|
protected:
|
2013-01-10 11:16:27 +08:00
|
|
|
llvm::BumpPtrAllocator _allocator;
|
2013-01-25 15:39:18 +08:00
|
|
|
File::atom_collection_vector<DefinedAtom> _definedAtoms;
|
|
|
|
File::atom_collection_vector<UndefinedAtom> _undefinedAtoms;
|
|
|
|
File::atom_collection_vector<SharedLibraryAtom> _sharedLibraryAtoms;
|
|
|
|
File::atom_collection_vector<AbsoluteAtom> _absoluteAtoms;
|
2013-01-10 11:16:27 +08:00
|
|
|
};
|
2013-01-30 06:03:39 +08:00
|
|
|
} // end namespace elf
|
2013-03-15 00:09:49 +08:00
|
|
|
} // end namespace lld
|
2013-01-10 11:16:27 +08:00
|
|
|
|
2013-01-30 06:03:39 +08:00
|
|
|
#endif
|