aboutsummaryrefslogtreecommitdiff
path: root/src/clients/reconcilio/littlelf/elf_relocation_section.cc
blob: 460a62c1c35d8ab9ab1342713f6bd7222f265bd1 (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
#include "elf_relocation_section.hh"
#include "elf_types.hh"
#include "elf.hh"

#include <src/clients/reconcilio/util/byte_swap.hh>

#include <paludis/util/private_implementation_pattern-impl.hh>
#include <paludis/util/visitor-impl.hh>
#include <paludis/util/wrapped_forward_iterator-impl.hh>

#include <istream>
#include <vector>

using namespace paludis;

namespace paludis
{
    template <typename ElfType_, typename Relocation_>
    struct Implementation<RelocationSection<ElfType_, Relocation_> >
    {
        std::vector<typename Relocation_::Entry> relocations;
    };
}

namespace
{
    template <typename, typename> struct ByteSwapRelocation;

    template <typename ElfType_>
    struct ByteSwapRelocation<ElfType_, Relocation<ElfType_> >
    {
        static void swap_in_place(typename ElfType_::Relocation & rel)
        {
            rel.r_offset = byte_swap(rel.r_offset);
            rel.r_info   = byte_swap(rel.r_info);
        }
    };

    template <typename ElfType_>
    struct ByteSwapRelocation<ElfType_, RelocationA<ElfType_> >
    {
        static void swap_in_place(typename ElfType_::RelocationA & rela)
        {
            rela.r_offset = byte_swap(rela.r_offset);
            rela.r_info   = byte_swap(rela.r_info);
            rela.r_addend = byte_swap(rela.r_addend);
        }
    };
}

template <typename ElfType_>
RelocationEntry<ElfType_>::RelocationEntry(const typename ElfType_::Relocation & my_relocation) :
    _my_relocation(my_relocation)
{
}

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

template <typename ElfType_>
RelocationAEntry<ElfType_>::RelocationAEntry(const typename ElfType_::RelocationA & my_relocation) :
    _my_relocation(my_relocation)
{
}

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

template <typename ElfType_> const std::string Relocation<ElfType_>::type_name = "REL";
template <typename ElfType_> const std::string RelocationA<ElfType_>::type_name = "RELA";

template <typename ElfType_, typename Relocation_>
RelocationSection<ElfType_, Relocation_>::RelocationSection(
    const typename ElfType_::SectionHeader & shdr, std::istream & stream, bool need_byte_swap) :
    Section<ElfType_>(shdr),
    PrivateImplementationPattern<RelocationSection>(new Implementation<RelocationSection>)
{
    if (sizeof(typename Relocation_::Type) != shdr.sh_entsize)
        throw InvalidElfFileError();

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

    for (typename std::vector<typename Relocation_::Type>::iterator i = relocations.begin(); i != relocations.end(); ++i)
        _imp->relocations.push_back(typename Relocation_::Entry(*i));
}

template <typename ElfType_, typename Relocation_>
RelocationSection<ElfType_, Relocation_>::~RelocationSection()
{
}

template <typename ElfType_, typename Relocation_>
typename RelocationSection<ElfType_, Relocation_>::RelocationIterator
RelocationSection<ElfType_, Relocation_>::relocation_begin() const
{
    return RelocationIterator(_imp->relocations.begin());
}

template <typename ElfType_, typename Relocation_>
typename RelocationSection<ElfType_, Relocation_>::RelocationIterator
RelocationSection<ElfType_, Relocation_>::relocation_end() const
{
    return RelocationIterator(_imp->relocations.end());
}

template class RelocationSection<Elf32Type, Relocation<Elf32Type> >;
template class WrappedForwardIterator<RelocationSection<Elf32Type, Relocation<Elf32Type> >::RelocationIteratorTag,
         const Relocation<Elf32Type>::Entry>;

template class RelocationSection<Elf32Type, RelocationA<Elf32Type> >;
template class WrappedForwardIterator<RelocationSection<Elf32Type, RelocationA<Elf32Type> >::RelocationIteratorTag,
         const RelocationA<Elf32Type>::Entry>;

template class RelocationSection<Elf64Type, Relocation<Elf64Type> >;
template class WrappedForwardIterator<RelocationSection<Elf64Type, Relocation<Elf64Type> >::RelocationIteratorTag,
         const Relocation<Elf64Type>::Entry>;

template class RelocationSection<Elf64Type, RelocationA<Elf64Type> >;
template class WrappedForwardIterator<RelocationSection<Elf64Type, RelocationA<Elf64Type> >::RelocationIteratorTag,
         const RelocationA<Elf64Type>::Entry>;