summaryrefslogtreecommitdiffstats
path: root/libs/elfio/elfio/elfio_symbols.hpp
blob: d18756a9af922d51f2daa3c5e3685443ca1132fa (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
/*
Copyright (C) 2001-2015 by Serge Lamikhov-Center

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#ifndef ELFIO_SYMBOLS_HPP
#define ELFIO_SYMBOLS_HPP

namespace ELFIO {

//------------------------------------------------------------------------------
template< class S >
class symbol_section_accessor_template
{
  public:
//------------------------------------------------------------------------------
    symbol_section_accessor_template( const elfio& elf_file_, S* symbol_section_ ) :
                                      elf_file( elf_file_ ),
                                      symbol_section( symbol_section_ )
    {
        find_hash_section();
    }

//------------------------------------------------------------------------------
    Elf_Xword
    get_symbols_num() const
    {
        Elf_Xword nRet = 0;
        if ( 0 != symbol_section->get_entry_size() ) {
            nRet = symbol_section->get_size() / symbol_section->get_entry_size();
        }

        return nRet;
    }

//------------------------------------------------------------------------------
    bool
    get_symbol( Elf_Xword      index,
                std::string&   name,
                Elf64_Addr&    value,
                Elf_Xword&     size,
                unsigned char& bind,
                unsigned char& type,
                Elf_Half&      section_index,
                unsigned char& other ) const
    {
        bool ret = false;

        if ( elf_file.get_class() == ELFCLASS32 ) {
            ret = generic_get_symbol<Elf32_Sym>( index, name, value, size, bind,
                                                 type, section_index, other );
        }
        else {
            ret = generic_get_symbol<Elf64_Sym>( index, name, value, size, bind,
                                                 type, section_index, other );
        }

        return ret;
    }

//------------------------------------------------------------------------------
    bool
    get_symbol( const std::string& name,
                Elf64_Addr&        value,
                Elf_Xword&         size,
                unsigned char&     bind,
                unsigned char&     type,
                Elf_Half&          section_index,
                unsigned char&     other ) const
    {
        bool ret = false;

        if ( 0 != get_hash_table_index() ) {
            Elf_Word nbucket = *(const Elf_Word*)hash_section->get_data();
            Elf_Word nchain  = *(const Elf_Word*)( hash_section->get_data() +
                                   sizeof( Elf_Word ) );
            Elf_Word val     = elf_hash( (const unsigned char*)name.c_str() );

            Elf_Word y   = *(const Elf_Word*)( hash_section->get_data() +
                               ( 2 + val % nbucket ) * sizeof( Elf_Word ) );
            std::string   str;
            get_symbol( y, str, value, size, bind, type, section_index, other );
            while ( str != name && STN_UNDEF != y && y < nchain ) {
                y = *(const Elf_Word*)( hash_section->get_data() +
                        ( 2 + nbucket + y ) * sizeof( Elf_Word ) );
                get_symbol( y, str, value, size, bind, type, section_index, other );
            }
            if (  str == name ) {
                ret = true;
            }
        }

        return ret;
    }

//------------------------------------------------------------------------------
    Elf_Word
    add_symbol( Elf_Word name, Elf64_Addr value, Elf_Xword size,
                unsigned char info, unsigned char other,
                Elf_Half shndx )
    {
        Elf_Word nRet;

        if ( symbol_section->get_size() == 0 ) {
            if ( elf_file.get_class() == ELFCLASS32 ) {
                nRet = generic_add_symbol<Elf32_Sym>( 0, 0, 0, 0, 0, 0 );
            }
            else {
                nRet = generic_add_symbol<Elf64_Sym>( 0, 0, 0, 0, 0, 0 );
            }
        }

        if ( elf_file.get_class() == ELFCLASS32 ) {
            nRet = generic_add_symbol<Elf32_Sym>( name, value, size, info, other,
                                                  shndx );
        }
        else {
            nRet = generic_add_symbol<Elf64_Sym>( name, value, size, info, other,
                                                  shndx );
        }

        return nRet;
    }

//------------------------------------------------------------------------------
    Elf_Word
    add_symbol( Elf_Word name, Elf64_Addr value, Elf_Xword size,
                unsigned char bind, unsigned char type, unsigned char other,
                Elf_Half shndx )
    {
        return add_symbol( name, value, size, ELF_ST_INFO( bind, type ), other, shndx );
    }

//------------------------------------------------------------------------------
    Elf_Word
    add_symbol( string_section_accessor& pStrWriter, const char* str,
                Elf64_Addr value, Elf_Xword size,
                unsigned char info, unsigned char other,
                Elf_Half shndx )
    {
        Elf_Word index = pStrWriter.add_string( str );
        return add_symbol( index, value, size, info, other, shndx );
    }

//------------------------------------------------------------------------------
    Elf_Word
    add_symbol( string_section_accessor& pStrWriter, const char* str,
                Elf64_Addr value, Elf_Xword size,
                unsigned char bind, unsigned char type, unsigned char other,
                Elf_Half shndx )
    {
        return add_symbol( pStrWriter, str, value, size, ELF_ST_INFO( bind, type ), other, shndx );
    }

//------------------------------------------------------------------------------
  private:
//------------------------------------------------------------------------------
    void
    find_hash_section()
    {
        hash_section       = 0;
        hash_section_index = 0;
        Elf_Half nSecNo = elf_file.sections.size();
        for ( Elf_Half i = 0; i < nSecNo && 0 == hash_section_index; ++i ) {
            const section* sec = elf_file.sections[i];
            if ( sec->get_link() == symbol_section->get_index() ) {
                hash_section       = sec;
                hash_section_index = i;
            }
        }
    }

//------------------------------------------------------------------------------
    Elf_Half
    get_string_table_index() const
    {
        return (Elf_Half)symbol_section->get_link();
    }

//------------------------------------------------------------------------------
    Elf_Half
    get_hash_table_index() const
    {
        return hash_section_index;
    }

//------------------------------------------------------------------------------
    template< class T >
    bool
    generic_get_symbol( Elf_Xword index,
                        std::string& name, Elf64_Addr& value,
                        Elf_Xword& size,
                        unsigned char& bind, unsigned char& type,
                        Elf_Half& section_index,
                        unsigned char& other ) const
    {
        bool ret = false;

        if ( index < get_symbols_num() ) {
            const T* pSym = reinterpret_cast<const T*>(
                symbol_section->get_data() +
                    index * symbol_section->get_entry_size() );

            const endianess_convertor& convertor = elf_file.get_convertor();

            section* string_section = elf_file.sections[get_string_table_index()];
            string_section_accessor str_reader( string_section );
            const char* pStr = str_reader.get_string( convertor( pSym->st_name ) );
            if ( 0 != pStr ) {
                name = pStr;
            }
            value   = convertor( pSym->st_value );
            size    = convertor( pSym->st_size );
            bind    = ELF_ST_BIND( pSym->st_info );
            type    = ELF_ST_TYPE( pSym->st_info );
            section_index = convertor( pSym->st_shndx );
            other   = pSym->st_other;

            ret = true;
        }

        return ret;
    }

//------------------------------------------------------------------------------
    template< class T >
    Elf_Word
    generic_add_symbol( Elf_Word name, Elf64_Addr value, Elf_Xword size,
                        unsigned char info, unsigned char other,
                        Elf_Half shndx )
    {
        const endianess_convertor& convertor = elf_file.get_convertor();

        T entry;
        entry.st_name  = convertor( name );
        entry.st_value = value;
        entry.st_value = convertor( entry.st_value );
        entry.st_size  = size;
        entry.st_size  = convertor( entry.st_size );
        entry.st_info  = convertor( info );
        entry.st_other = convertor( other );
        entry.st_shndx = convertor( shndx );

        symbol_section->append_data( reinterpret_cast<char*>( &entry ),
                                     sizeof( entry ) );

        Elf_Word nRet = symbol_section->get_size() / sizeof( entry ) - 1;

        return nRet;
    }

//------------------------------------------------------------------------------
  private:
    const elfio&   elf_file;
    S*             symbol_section;
    Elf_Half       hash_section_index;
    const section* hash_section;
};

using symbol_section_accessor = symbol_section_accessor_template<section>;
using const_symbol_section_accessor = symbol_section_accessor_template<const section>;

} // namespace ELFIO

#endif // ELFIO_SYMBOLS_HPP
OpenPOWER on IntegriCloud