blob: 163f78aeb25fea9d2f01bbddb76d4b4abbff7d17 [file] [log] [blame]
Michael J. Spencere68f9032013-01-29 22:03:39 +00001//===- lib/ReaderWriter/ELF/ExecutableAtoms.h -----------------------------===//
Shankar Easwaran2ca8e7d2013-01-10 03:16:27 +00002//
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. Spencere68f9032013-01-29 22:03:39 +000010#ifndef LLD_READER_WRITER_ELF_EXECUTABLE_ATOM_H
11#define LLD_READER_WRITER_ELF_EXECUTABLE_ATOM_H
Shankar Easwaran2ca8e7d2013-01-10 03:16:27 +000012
Michael J. Spencere68f9032013-01-29 22:03:39 +000013#include "Atoms.h"
14#include "File.h"
Michael J. Spencer43ecac52013-01-29 19:53:41 +000015
Shankar Easwaran2ca8e7d2013-01-10 03:16:27 +000016#include "lld/Core/DefinedAtom.h"
Shankar Easwaran2ca8e7d2013-01-10 03:16:27 +000017#include "lld/Core/File.h"
18#include "lld/Core/Reference.h"
Shankar Easwaran3256d4f2013-01-25 07:39:18 +000019#include "lld/Core/UndefinedAtom.h"
Michael J. Spencer64afcb42013-01-23 01:18:43 +000020#include "lld/ReaderWriter/Writer.h"
Shankar Easwaran2ca8e7d2013-01-10 03:16:27 +000021
22namespace lld {
23namespace elf {
Shankar Easwaran2ca8e7d2013-01-10 03:16:27 +000024/// \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. Spencere68f9032013-01-29 22:03:39 +000029template <class ELFT> class CRuntimeFile : public ELFFile<ELFT> {
Shankar Easwaran2ca8e7d2013-01-10 03:16:27 +000030public:
Michael J. Spencerb03f6c42013-01-15 07:53:22 +000031 typedef llvm::object::Elf_Sym_Impl<ELFT> Elf_Sym;
Michael J. Spencere68f9032013-01-29 22:03:39 +000032 CRuntimeFile(const ELFTargetInfo &ti) : ELFFile<ELFT>(ti, "C runtime") {}
Shankar Easwaran3256d4f2013-01-25 07:39:18 +000033
Shankar Easwaran2ca8e7d2013-01-10 03:16:27 +000034 /// \brief add a global absolute atom
Shankar Easwarana6f00fe2013-01-30 07:11:43 +000035 void addAbsoluteAtom(StringRef symbolName) {
36 Elf_Sym *symbol = new (_allocator.Allocate<Elf_Sym>()) Elf_Sym;
Shankar Easwaran2ca8e7d2013-01-10 03:16:27 +000037 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. Spencere68f9032013-01-29 22:03:39 +000044 auto *newAtom =
45 new (_allocator) ELFAbsoluteAtom<ELFT>(*this, symbolName, symbol, -1);
Shankar Easwaran2ca8e7d2013-01-10 03:16:27 +000046 _absoluteAtoms._atoms.push_back(newAtom);
47 }
48
49 /// \brief add an undefined atom
Shankar Easwarana6f00fe2013-01-30 07:11:43 +000050 void addUndefinedAtom(StringRef symbolName) {
Michael J. Spencere68f9032013-01-29 22:03:39 +000051 Elf_Sym *symbol = new (_allocator) Elf_Sym;
Shankar Easwaran2ca8e7d2013-01-10 03:16:27 +000052 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. Spencere68f9032013-01-29 22:03:39 +000057 auto *newAtom =
58 new (_allocator) ELFUndefinedAtom<ELFT>(*this, symbolName, symbol);
Shankar Easwaran2ca8e7d2013-01-10 03:16:27 +000059 _undefinedAtoms._atoms.push_back(newAtom);
60 }
61
Shankar Easwaran3256d4f2013-01-25 07:39:18 +000062 const File::atom_collection<DefinedAtom> &defined() const {
Shankar Easwaran2ca8e7d2013-01-10 03:16:27 +000063 return _definedAtoms;
64 }
65
Shankar Easwaran3256d4f2013-01-25 07:39:18 +000066 const File::atom_collection<UndefinedAtom> &undefined() const {
Shankar Easwaran2ca8e7d2013-01-10 03:16:27 +000067 return _undefinedAtoms;
68 }
69
Shankar Easwaran3256d4f2013-01-25 07:39:18 +000070 const File::atom_collection<SharedLibraryAtom> &sharedLibrary() const {
Shankar Easwaran2ca8e7d2013-01-10 03:16:27 +000071 return _sharedLibraryAtoms;
72 }
73
Shankar Easwaran3256d4f2013-01-25 07:39:18 +000074 const File::atom_collection<AbsoluteAtom> &absolute() const {
Shankar Easwaran2ca8e7d2013-01-10 03:16:27 +000075 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
83private:
84 llvm::BumpPtrAllocator _allocator;
Shankar Easwaran3256d4f2013-01-25 07:39:18 +000085 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 Easwaran2ca8e7d2013-01-10 03:16:27 +000089};
Michael J. Spencere68f9032013-01-29 22:03:39 +000090} // end namespace elf
91} // end namespace lld
Shankar Easwaran2ca8e7d2013-01-10 03:16:27 +000092
Michael J. Spencere68f9032013-01-29 22:03:39 +000093#endif