| Michael J. Spencer | e68f903 | 2013-01-29 22:03:39 +0000 | [diff] [blame] | 1 | //===- lib/ReaderWriter/ELF/ExecutableAtoms.h -----------------------------===// | 
| Shankar Easwaran | 2ca8e7d | 2013-01-10 03:16:27 +0000 | [diff] [blame] | 2 | // | 
|  | 3 | //                             The LLVM Linker | 
|  | 4 | // | 
|  | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
|  | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 |  | 
| Michael J. Spencer | e68f903 | 2013-01-29 22:03:39 +0000 | [diff] [blame] | 10 | #ifndef LLD_READER_WRITER_ELF_EXECUTABLE_ATOM_H | 
|  | 11 | #define LLD_READER_WRITER_ELF_EXECUTABLE_ATOM_H | 
| Shankar Easwaran | 2ca8e7d | 2013-01-10 03:16:27 +0000 | [diff] [blame] | 12 |  | 
| Michael J. Spencer | e68f903 | 2013-01-29 22:03:39 +0000 | [diff] [blame] | 13 | #include "Atoms.h" | 
|  | 14 | #include "File.h" | 
| Michael J. Spencer | 43ecac5 | 2013-01-29 19:53:41 +0000 | [diff] [blame] | 15 |  | 
| Shankar Easwaran | 2ca8e7d | 2013-01-10 03:16:27 +0000 | [diff] [blame] | 16 | #include "lld/Core/DefinedAtom.h" | 
| Shankar Easwaran | 2ca8e7d | 2013-01-10 03:16:27 +0000 | [diff] [blame] | 17 | #include "lld/Core/File.h" | 
|  | 18 | #include "lld/Core/Reference.h" | 
| Shankar Easwaran | 3256d4f | 2013-01-25 07:39:18 +0000 | [diff] [blame] | 19 | #include "lld/Core/UndefinedAtom.h" | 
| Michael J. Spencer | 64afcb4 | 2013-01-23 01:18:43 +0000 | [diff] [blame] | 20 | #include "lld/ReaderWriter/Writer.h" | 
| Shankar Easwaran | 2ca8e7d | 2013-01-10 03:16:27 +0000 | [diff] [blame] | 21 |  | 
|  | 22 | namespace lld { | 
|  | 23 | namespace elf { | 
| Shankar Easwaran | 2ca8e7d | 2013-01-10 03:16:27 +0000 | [diff] [blame] | 24 | /// \brief All atoms are owned by a File. To add linker specific atoms | 
|  | 25 | /// the atoms need to be inserted to a file called (CRuntimeFile) which | 
|  | 26 | /// are basically additional symbols required by libc and other runtime | 
|  | 27 | /// libraries part of executing a program. This class provides support | 
|  | 28 | /// for adding absolute symbols and undefined symbols | 
| Michael J. Spencer | e68f903 | 2013-01-29 22:03:39 +0000 | [diff] [blame] | 29 | template <class ELFT> class CRuntimeFile : public ELFFile<ELFT> { | 
| Shankar Easwaran | 2ca8e7d | 2013-01-10 03:16:27 +0000 | [diff] [blame] | 30 | public: | 
| Michael J. Spencer | b03f6c4 | 2013-01-15 07:53:22 +0000 | [diff] [blame] | 31 | typedef llvm::object::Elf_Sym_Impl<ELFT> Elf_Sym; | 
| Michael J. Spencer | e68f903 | 2013-01-29 22:03:39 +0000 | [diff] [blame] | 32 | CRuntimeFile(const ELFTargetInfo &ti) : ELFFile<ELFT>(ti, "C runtime") {} | 
| Shankar Easwaran | 3256d4f | 2013-01-25 07:39:18 +0000 | [diff] [blame] | 33 |  | 
| Shankar Easwaran | 2ca8e7d | 2013-01-10 03:16:27 +0000 | [diff] [blame] | 34 | /// \brief add a global absolute atom | 
|  | 35 | void addAbsoluteAtom(const StringRef symbolName) { | 
|  | 36 | Elf_Sym *symbol = new(_allocator.Allocate<Elf_Sym>()) Elf_Sym; | 
|  | 37 | symbol->st_name = 0; | 
|  | 38 | symbol->st_value = 0; | 
|  | 39 | symbol->st_shndx = llvm::ELF::SHN_ABS; | 
|  | 40 | symbol->setBindingAndType(llvm::ELF::STB_GLOBAL, | 
|  | 41 | llvm::ELF::STT_OBJECT); | 
|  | 42 | symbol->st_other = llvm::ELF::STV_DEFAULT; | 
|  | 43 | symbol->st_size = 0; | 
| Michael J. Spencer | e68f903 | 2013-01-29 22:03:39 +0000 | [diff] [blame] | 44 | auto *newAtom = | 
|  | 45 | new (_allocator) ELFAbsoluteAtom<ELFT>(*this, symbolName, symbol, -1); | 
| Shankar Easwaran | 2ca8e7d | 2013-01-10 03:16:27 +0000 | [diff] [blame] | 46 | _absoluteAtoms._atoms.push_back(newAtom); | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | /// \brief add an undefined atom | 
|  | 50 | void addUndefinedAtom(const StringRef symbolName) { | 
| Michael J. Spencer | e68f903 | 2013-01-29 22:03:39 +0000 | [diff] [blame] | 51 | Elf_Sym *symbol = new (_allocator) Elf_Sym; | 
| Shankar Easwaran | 2ca8e7d | 2013-01-10 03:16:27 +0000 | [diff] [blame] | 52 | symbol->st_name = 0; | 
|  | 53 | symbol->st_value = 0; | 
|  | 54 | symbol->st_shndx = llvm::ELF::SHN_UNDEF; | 
|  | 55 | symbol->st_other = llvm::ELF::STV_DEFAULT; | 
|  | 56 | symbol->st_size = 0; | 
| Michael J. Spencer | e68f903 | 2013-01-29 22:03:39 +0000 | [diff] [blame] | 57 | auto *newAtom = | 
|  | 58 | new (_allocator) ELFUndefinedAtom<ELFT>(*this, symbolName, symbol); | 
| Shankar Easwaran | 2ca8e7d | 2013-01-10 03:16:27 +0000 | [diff] [blame] | 59 | _undefinedAtoms._atoms.push_back(newAtom); | 
|  | 60 | } | 
|  | 61 |  | 
| Shankar Easwaran | 3256d4f | 2013-01-25 07:39:18 +0000 | [diff] [blame] | 62 | const File::atom_collection<DefinedAtom> &defined() const { | 
| Shankar Easwaran | 2ca8e7d | 2013-01-10 03:16:27 +0000 | [diff] [blame] | 63 | return _definedAtoms; | 
|  | 64 | } | 
|  | 65 |  | 
| Shankar Easwaran | 3256d4f | 2013-01-25 07:39:18 +0000 | [diff] [blame] | 66 | const File::atom_collection<UndefinedAtom> &undefined() const { | 
| Shankar Easwaran | 2ca8e7d | 2013-01-10 03:16:27 +0000 | [diff] [blame] | 67 | return _undefinedAtoms; | 
|  | 68 | } | 
|  | 69 |  | 
| Shankar Easwaran | 3256d4f | 2013-01-25 07:39:18 +0000 | [diff] [blame] | 70 | const File::atom_collection<SharedLibraryAtom> &sharedLibrary() const { | 
| Shankar Easwaran | 2ca8e7d | 2013-01-10 03:16:27 +0000 | [diff] [blame] | 71 | return _sharedLibraryAtoms; | 
|  | 72 | } | 
|  | 73 |  | 
| Shankar Easwaran | 3256d4f | 2013-01-25 07:39:18 +0000 | [diff] [blame] | 74 | const File::atom_collection<AbsoluteAtom> &absolute() const { | 
| Shankar Easwaran | 2ca8e7d | 2013-01-10 03:16:27 +0000 | [diff] [blame] | 75 | return _absoluteAtoms; | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | // cannot add atoms to C Runtime file | 
|  | 79 | virtual void addAtom(const Atom&) { | 
|  | 80 | llvm_unreachable("cannot add atoms to C Runtime files"); | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | private: | 
|  | 84 | llvm::BumpPtrAllocator _allocator; | 
| Shankar Easwaran | 3256d4f | 2013-01-25 07:39:18 +0000 | [diff] [blame] | 85 | File::atom_collection_vector<DefinedAtom> _definedAtoms; | 
|  | 86 | File::atom_collection_vector<UndefinedAtom> _undefinedAtoms; | 
|  | 87 | File::atom_collection_vector<SharedLibraryAtom> _sharedLibraryAtoms; | 
|  | 88 | File::atom_collection_vector<AbsoluteAtom> _absoluteAtoms; | 
| Shankar Easwaran | 2ca8e7d | 2013-01-10 03:16:27 +0000 | [diff] [blame] | 89 | }; | 
| Michael J. Spencer | e68f903 | 2013-01-29 22:03:39 +0000 | [diff] [blame] | 90 | } // end namespace elf | 
|  | 91 | } // end namespace lld | 
| Shankar Easwaran | 2ca8e7d | 2013-01-10 03:16:27 +0000 | [diff] [blame] | 92 |  | 
| Michael J. Spencer | e68f903 | 2013-01-29 22:03:39 +0000 | [diff] [blame] | 93 | #endif |