aboutsummaryrefslogtreecommitdiff
path: root/paludis/util/elf_dynamic_section.cc
blob: cfb9391765fb661e63a8c3e8a8c6f939c02b699f (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* 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
 */

#include <paludis/util/elf_dynamic_section.hh>
#include <paludis/util/elf_sections.hh>
#include <paludis/util/elf_types.hh>
#include <paludis/util/elf_relocation_section.hh>
#include <paludis/util/elf_symbol_section.hh>
#include <paludis/util/elf.hh>

#include <paludis/util/byte_swap.hh>
#include <paludis/util/clone-impl.hh>
#include <paludis/util/singleton-impl.hh>
#include <paludis/util/pimp-impl.hh>
#include <paludis/util/wrapped_forward_iterator-impl.hh>
#include <paludis/util/indirect_iterator-impl.hh>

#include <istream>
#include <map>
#include <vector>
#include <stdexcept>
#include <algorithm>

using namespace paludis;

namespace paludis
{
    template <typename ElfType_>
    struct Imp<DynamicEntries<ElfType_> >
    {
        std::map<typename ElfType_::DynamicTag, std::shared_ptr<DynamicEntry<ElfType_> > > available_types;
    };

    template <typename ElfType_>
    struct Imp<DynamicSection<ElfType_> >
    {
        std::vector<std::shared_ptr<DynamicEntry<ElfType_> > > dynamic_entries;
    };

    template <typename ElfType_>
    struct WrappedForwardIteratorTraits<DynamicSectionEntryIteratorTag<ElfType_> >
    {
        typedef IndirectIterator<typename std::vector<std::shared_ptr<DynamicEntry<ElfType_> > >::const_iterator> UnderlyingIterator;
    };
}

namespace paludis
{
    namespace littlelf_internals
    {
        template <typename ElfType_>
        class DynEntriesStringResolvingVisitor
        {
            private:
                const DynamicSection<ElfType_> & _dyn_section;
                const StringSection<ElfType_> & _string_section;

            public:
                DynEntriesStringResolvingVisitor(
                    const DynamicSection<ElfType_> & dyn_section,
                    const StringSection<ElfType_> & string_section) :
                    _dyn_section(dyn_section),
                    _string_section(string_section)
                {
                }

                void visit(DynamicEntry<ElfType_> &)
                {
                }

                void visit(DynamicEntryString<ElfType_> & entry)
                {
                    try
                    {
                        entry.resolve_string(_string_section.get_string(entry.get_string_index()));
                    }
                    catch (std::out_of_range &)
                    {
                        throw InvalidElfFileError(
                            entry.description() + " in " + _dyn_section.description() + " has out-of-range string index " +
                            stringify(entry.get_string_index()) + " for " + _string_section.description() +
                            " (max " + stringify(_string_section.get_max_string()) + ")");
                    }
                }
        };
    }
}

namespace
{
    template <typename ElfType_>
    struct ByteSwapDynamicEntry
    {
        static void swap_in_place(typename ElfType_::DynamicEntry & entry)
        {
            entry.d_tag      = byte_swap(entry.d_tag);
            entry.d_un.d_val = byte_swap(entry.d_un.d_val);
        }
    };

    template <typename ElfType_>
    class DynamicSectionStringResolvingVisitor
    {
        private:
            const DynamicSection<ElfType_> & _dyn_section;
            typename std::vector<std::shared_ptr<DynamicEntry<ElfType_> > >::iterator _begin, _end;

        public:
            DynamicSectionStringResolvingVisitor(const DynamicSection<ElfType_> & dyn_section,
                typename std::vector<std::shared_ptr<DynamicEntry<ElfType_> > >::iterator begin,
                typename std::vector<std::shared_ptr<DynamicEntry<ElfType_> > >::iterator end) :
                _dyn_section(dyn_section),
                _begin(begin),
                _end(end)

            {
            }

            void visit(Section<ElfType_> &)
            {
            }

            void visit(StringSection<ElfType_> & section)
            {
                littlelf_internals::DynEntriesStringResolvingVisitor<ElfType_> v(_dyn_section, section);
                for(typename std::vector<std::shared_ptr<DynamicEntry<ElfType_> > >::iterator i = _begin; i != _end; ++i)
                    (*i)->accept(v);
            }
    };
}

template <typename ElfType_>
DynamicEntry<ElfType_>::DynamicEntry(const std::string & my_tag_name) :
    _tag_name(my_tag_name)
{
}

template <typename ElfType_>
DynamicEntry<ElfType_>::~DynamicEntry()
{
}

template <typename ElfType_>
void
DynamicEntry<ElfType_>::initialize(typename ElfType_::Word index, const typename ElfType_::DynamicEntry &)
{
    _index = index;
}

template <typename ElfType_>
std::string
DynamicEntry<ElfType_>::description() const
{
    return tag_name() + " dynamic entry " + stringify(_index);
}

template <typename ElfType_>
DynamicEntryUnknown<ElfType_>::DynamicEntryUnknown() :
    DynamicEntry<ElfType_>("unknown")
{
}

template <typename ElfType_>
DynamicEntryUnknown<ElfType_>::~DynamicEntryUnknown()
{
}

template <typename ElfType_>
DynamicEntryFlag<ElfType_>::DynamicEntryFlag(const std::string & name) :
    DynamicEntry<ElfType_>(name)
{
}

template <typename ElfType_>
DynamicEntryFlag<ElfType_>::~DynamicEntryFlag()
{
}

template <typename ElfType_>
DynamicEntryValue<ElfType_>::DynamicEntryValue(const std::string & name) :
    DynamicEntry<ElfType_>(name)
{
}

template <typename ElfType_>
DynamicEntryValue<ElfType_>::~DynamicEntryValue()
{
}

template <typename ElfType_>
void
DynamicEntryValue<ElfType_>::initialize(typename ElfType_::Word index, const typename ElfType_::DynamicEntry & entry)
{
    DynamicEntry<ElfType_>::initialize(index, entry);
    _value = entry.d_un.d_val;
}

template <typename ElfType_>
DynamicEntryPointer<ElfType_>::DynamicEntryPointer(const std::string & name) :
    DynamicEntry<ElfType_>(name)
{
}

template <typename ElfType_>
DynamicEntryPointer<ElfType_>::~DynamicEntryPointer()
{
}

template <typename ElfType_>
void
DynamicEntryPointer<ElfType_>::initialize(typename ElfType_::Word index, const typename ElfType_::DynamicEntry & entry)
{
    DynamicEntry<ElfType_>::initialize(index, entry);
    _pointer = entry.d_un.d_ptr;
}

template <typename ElfType_>
DynamicEntryString<ElfType_>::DynamicEntryString(const std::string & name) :
    DynamicEntry<ElfType_>(name),
    _str("")
{
}

template <typename ElfType_>
DynamicEntryString<ElfType_>::~DynamicEntryString()
{
}

template <typename ElfType_>
void
DynamicEntryString<ElfType_>::initialize(typename ElfType_::Word index, const typename ElfType_::DynamicEntry & entry)
{
    DynamicEntry<ElfType_>::initialize(index, entry);
    _value = entry.d_un.d_val;
}

template <typename ElfType_>
DynamicEntries<ElfType_>::DynamicEntries() :
    _imp()
{
    register_type(DT_NEEDED,  std::make_shared<DynamicEntryString<ElfType_> >("NEEDED"));
    register_type(DT_RPATH,   std::make_shared<DynamicEntryString<ElfType_> >("RPATH"));
    register_type(DT_RUNPATH, std::make_shared<DynamicEntryString<ElfType_> >("RUNPATH"));
    register_type(DT_SONAME,  std::make_shared<DynamicEntryString<ElfType_> >("SONAME"));
    register_type(DT_TEXTREL, std::make_shared<DynamicEntryFlag<ElfType_> >("TEXTREL"));
    register_type(DT_NULL,    std::make_shared<DynamicEntryFlag<ElfType_> >("NULL"));
    register_type(DT_SYMTAB,  std::make_shared<DynamicEntryPointer<ElfType_> >("SYMTAB"));
    register_type(DT_STRTAB,  std::make_shared<DynamicEntryPointer<ElfType_> >("STRTAB"));
}

template <typename ElfType_>
DynamicEntries<ElfType_>::~DynamicEntries()
{
}

template <typename ElfType_>
void
DynamicEntries<ElfType_>::register_type(typename ElfType_::DynamicTag identifier, std::shared_ptr<DynamicEntry<ElfType_> > entry)
{
    _imp->available_types[identifier] = entry;
}

template <typename ElfType_>
std::shared_ptr<DynamicEntry<ElfType_> >
DynamicEntries<ElfType_>::get_entry(typename ElfType_::DynamicTag tag) const
{
    typename std::map<typename ElfType_::DynamicTag, std::shared_ptr<DynamicEntry<ElfType_> > >::const_iterator i;
    if (( i = _imp->available_types.find(tag)) != _imp->available_types.end())
        return i->second->clone();
    return std::make_shared<DynamicEntryUnknown<ElfType_> >();
}

template <typename ElfType_>
bool
DynamicEntries<ElfType_>::has_entry(typename ElfType_::DynamicTag identifier) const
{
    return ( _imp->available_types.find(identifier) != _imp->available_types.end() );
}

template <typename ElfType_>
DynamicSection<ElfType_>::DynamicSection(typename ElfType_::Word index, const typename ElfType_::SectionHeader & shdr, std::istream & stream, bool need_byte_swap) :
    Section<ElfType_>(index, shdr),
    _imp()
{
    if (sizeof(typename ElfType_::DynamicEntry) != shdr.sh_entsize)
        throw InvalidElfFileError(
            "bad sh_entsize for " + this->description() + ": got " + stringify(shdr.sh_entsize) + ", expected " +
            stringify(sizeof(typename ElfType_::DynamicEntry)));

    stream.seekg(shdr.sh_offset, std::ios::beg);
    std::vector<typename ElfType_::DynamicEntry> tmp_entries(shdr.sh_size / sizeof(typename ElfType_::DynamicEntry));
    stream.read( reinterpret_cast<char *>(&tmp_entries.front()), shdr.sh_size );
    if (need_byte_swap)
        std::for_each(tmp_entries.begin(), tmp_entries.end(),
                      &ByteSwapDynamicEntry<ElfType_>::swap_in_place);

    for (typename std::vector<typename ElfType_::DynamicEntry>::iterator i = tmp_entries.begin(); i != tmp_entries.end(); ++i)
    {
        std::shared_ptr<DynamicEntry<ElfType_> > instance(DynamicEntries<ElfType_>::get_instance()->get_entry(i->d_tag));
        instance->initialize(_imp->dynamic_entries.size(), *i);
        _imp->dynamic_entries.push_back(instance);
    }
}

template <typename ElfType_>
DynamicSection<ElfType_>::~DynamicSection()
{
}

template <typename ElfType_>
std::string
DynamicSection<ElfType_>::get_type() const
{
    static std::string type("DYNAMIC");
    return type;
}

template <typename ElfType_>
void
DynamicSection<ElfType_>::resolve_entry_names(Section<ElfType_> & string_section)
{
    DynamicSectionStringResolvingVisitor<ElfType_> v(
        *this, _imp->dynamic_entries.begin(), _imp->dynamic_entries.end());
    string_section.accept(v);
}

template <typename ElfType_>
typename DynamicSection<ElfType_>::EntryIterator
DynamicSection<ElfType_>::entry_begin() const
{
    return EntryIterator(indirect_iterator(_imp->dynamic_entries.begin()));
}

template <typename ElfType_>
typename DynamicSection<ElfType_>::EntryIterator
DynamicSection<ElfType_>::entry_end() const
{
    return EntryIterator(indirect_iterator(_imp->dynamic_entries.end()));
}

namespace paludis
{
    template class DynamicSection<Elf32Type>;
    template class DynamicSection<Elf64Type>;

    template class WrappedForwardIterator<DynamicSection<Elf32Type>::EntryIteratorTag, DynamicEntry<Elf32Type> >;
    template class WrappedForwardIterator<DynamicSection<Elf64Type>::EntryIteratorTag, DynamicEntry<Elf64Type> >;
}