aboutsummaryrefslogtreecommitdiff
path: root/paludis/util/elf_sections.hh
blob: 5095d236bc9ca7ee19dd114959ee5257f6317944 (plain)
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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2007 Tiziano Müller
 * Copyright (c) 2007 David Leverton
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef PALUDIS_GUARD_PALUDIS_UTIL_ELF_SECTIONS_HH
#define PALUDIS_GUARD_PALUDIS_UTIL_ELF_SECTIONS_HH 1

#include <string>
#include <iosfwd>
#include <paludis/util/simple_visitor.hh>
#include <paludis/util/type_list.hh>

namespace paludis
{
    template <typename ElfType_> class ElfObject;
    template <typename ElfType_> class Section;
    template <typename ElfType_> class StringSection;
    template <typename ElfType_> class DynamicSection;
    template <typename ElfType_> class SymbolSection;
    template <typename ElfType_> class GenericSection;
    template <typename ElfType_, class RelocationType_> class RelocationSection;

    template <typename ElfType_> class Relocation;
    template <typename ElfType_> class RelocationA;

    namespace littlelf_internals
    {
        template <typename ElfType_> class SectionNameResolvingVisitor;
    }

    template <typename ElfType_>
    class Section :
        public virtual paludis::DeclareAbstractAcceptMethods<Section<ElfType_>, typename paludis::MakeTypeList<
            StringSection<ElfType_>,
            DynamicSection<ElfType_>,
            SymbolSection<ElfType_>,
            GenericSection<ElfType_>,
            RelocationSection<ElfType_, Relocation<ElfType_> >,
            RelocationSection<ElfType_, RelocationA<ElfType_> >
        >::Type>
    {
        friend class littlelf_internals::SectionNameResolvingVisitor<ElfType_>;

        private:
            typename ElfType_::Word _index;
            typename ElfType_::SectionHeader _shdr;
            std::string _name;

        protected:
            void resolve_section_name(std::string name)
            {
                _name = name;
            }

            typename ElfType_::Word get_name_index() const
            {
                return _shdr.sh_name;
            }

        public:
            Section(typename ElfType_::Word, const typename ElfType_::SectionHeader &);
            virtual ~Section();

            virtual typename ElfType_::Offset get_data_offset() const
            {
                return _shdr.sh_offset;
            }

            virtual std::string get_name() const
            {
                return _name;
            }
            virtual std::string get_type() const = 0;
            typename ElfType_::Word get_index() const
            {
                return _index;
            }

            typename ElfType_::Word get_link_index() const
            {
                return _shdr.sh_link;
            }

            typename ElfType_::Address get_virtual_address() const
            {
                return _shdr.sh_addr;
            }

            typename ElfType_::SectionSize get_size() const
            {
                return _shdr.sh_size;
            }

            std::string description() const;
    };


    template <typename ElfType_>
    class GenericSection :
        public Section<ElfType_>,
        public paludis::ImplementAcceptMethods<Section<ElfType_>, GenericSection<ElfType_> >
    {
        public:
            GenericSection(typename ElfType_::Word, const typename ElfType_::SectionHeader &);
            virtual ~GenericSection();
            virtual std::string get_type() const;
    };

    template <typename ElfType_>
    class StringSection :
        public Section<ElfType_>,
        public paludis::ImplementAcceptMethods<Section<ElfType_>, StringSection<ElfType_> >
    {
        private:
            std::string _stringTable;

        public:
            StringSection(typename ElfType_::Word, const typename ElfType_::SectionHeader &, std::istream &, bool);
            virtual ~StringSection();

            std::string get_string(typename ElfType_::Word) const;
            typename ElfType_::Word get_max_string() const
            {
                return _stringTable.length();
            }

            virtual std::string get_type() const;
    };
}

#endif