• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 Copyright (C) 2001-present by Serge Lamikhov-Center
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 
23 #ifdef _MSC_VER
24 #define _SCL_SECURE_NO_WARNINGS
25 #endif
26 
27 #define BOOST_TEST_MODULE ELFIO_Test
28 #include <boost/test/included/unit_test.hpp>
29 
30 #include <elfio/elfio.hpp>
31 
32 using namespace ELFIO;
33 
34 ////////////////////////////////////////////////////////////////////////////////
checkHeader(elfio & reader,unsigned char nClass,unsigned char encoding,unsigned char elfVersion,Elf_Half type,Elf_Half machine,Elf_Word version,Elf64_Addr entry,Elf_Word flags,Elf_Half secNum,Elf_Half segNum,unsigned char OSABI,unsigned char ABIVersion)35 void checkHeader( elfio&        reader,
36                   unsigned char nClass,
37                   unsigned char encoding,
38                   unsigned char elfVersion,
39                   Elf_Half      type,
40                   Elf_Half      machine,
41                   Elf_Word      version,
42                   Elf64_Addr    entry,
43                   Elf_Word      flags,
44                   Elf_Half      secNum,
45                   Elf_Half      segNum,
46                   unsigned char OSABI,
47                   unsigned char ABIVersion )
48 {
49     BOOST_CHECK_EQUAL( reader.get_class(), nClass );
50     BOOST_CHECK_EQUAL( reader.get_encoding(), encoding );
51     BOOST_CHECK_EQUAL( reader.get_elf_version(), elfVersion );
52     BOOST_CHECK_EQUAL( reader.get_os_abi(), OSABI );
53     BOOST_CHECK_EQUAL( reader.get_abi_version(), ABIVersion );
54     BOOST_CHECK_EQUAL( reader.get_type(), type );
55     BOOST_CHECK_EQUAL( reader.get_machine(), machine );
56     BOOST_CHECK_EQUAL( reader.get_version(), version );
57     BOOST_CHECK_EQUAL( reader.get_entry(), entry );
58     BOOST_CHECK_EQUAL( reader.get_flags(), flags );
59     BOOST_CHECK_EQUAL( reader.sections.size(), secNum );
60     BOOST_CHECK_EQUAL( reader.segments.size(), segNum );
61 }
62 
63 ////////////////////////////////////////////////////////////////////////////////
checkSection(const section * sec,Elf_Half index,std::string name,Elf_Word type,Elf_Xword flags,Elf64_Addr address,Elf_Xword size,Elf_Word link,Elf_Word info,Elf_Xword addrAlign,Elf_Xword entrySize)64 void checkSection( const section* sec,
65                    Elf_Half       index,
66                    std::string    name,
67                    Elf_Word       type,
68                    Elf_Xword      flags,
69                    Elf64_Addr     address,
70                    Elf_Xword      size,
71                    Elf_Word       link,
72                    Elf_Word       info,
73                    Elf_Xword      addrAlign,
74                    Elf_Xword      entrySize )
75 {
76     BOOST_CHECK_EQUAL( sec->get_index(), index );
77     BOOST_CHECK_EQUAL( sec->get_name(), name );
78     BOOST_CHECK_EQUAL( sec->get_type(), type );
79     BOOST_CHECK_EQUAL( sec->get_flags(), flags );
80     BOOST_CHECK_EQUAL( sec->get_address(), address );
81     BOOST_CHECK_EQUAL( sec->get_size(), size );
82     BOOST_CHECK_EQUAL( sec->get_link(), link );
83     BOOST_CHECK_EQUAL( sec->get_info(), info );
84     BOOST_CHECK_EQUAL( sec->get_addr_align(), addrAlign );
85     BOOST_CHECK_EQUAL( sec->get_entry_size(), entrySize );
86 }
87 
88 ////////////////////////////////////////////////////////////////////////////////
checkSection(const section * sec,const std::string & name,Elf_Word type,Elf_Xword flags,Elf64_Addr address,Elf_Xword size,Elf_Word link,Elf_Word info,Elf_Xword addrAlign,Elf_Xword entrySize)89 void checkSection( const section*     sec,
90                    const std::string& name,
91                    Elf_Word           type,
92                    Elf_Xword          flags,
93                    Elf64_Addr         address,
94                    Elf_Xword          size,
95                    Elf_Word           link,
96                    Elf_Word           info,
97                    Elf_Xword          addrAlign,
98                    Elf_Xword          entrySize )
99 {
100     checkSection( sec, sec->get_index(), name, type, flags, address, size, link,
101                   info, addrAlign, entrySize );
102 }
103 
104 ////////////////////////////////////////////////////////////////////////////////
checkSegment(const segment * seg,Elf_Word type,Elf64_Addr vaddr,Elf64_Addr paddr,Elf_Xword fsize,Elf_Xword msize,Elf_Word flags,Elf_Xword align)105 void checkSegment( const segment* seg,
106                    Elf_Word       type,
107                    Elf64_Addr     vaddr,
108                    Elf64_Addr     paddr,
109                    Elf_Xword      fsize,
110                    Elf_Xword      msize,
111                    Elf_Word       flags,
112                    Elf_Xword      align )
113 {
114     BOOST_CHECK_EQUAL( seg->get_type(), type );
115     BOOST_CHECK_EQUAL( seg->get_virtual_address(), vaddr );
116     BOOST_CHECK_EQUAL( seg->get_physical_address(), paddr );
117     BOOST_CHECK_EQUAL( seg->get_file_size(), fsize );
118     BOOST_CHECK_EQUAL( seg->get_memory_size(), msize );
119     BOOST_CHECK_EQUAL( seg->get_flags(), flags );
120     BOOST_CHECK_EQUAL( seg->get_align(), align );
121 }
122 
123 ////////////////////////////////////////////////////////////////////////////////
checkSymbol(const symbol_section_accessor & sr,Elf_Xword index,std::string name_,Elf64_Addr value_,Elf_Xword size_,unsigned char bind_,unsigned char type_,Elf_Half section_,unsigned char other_)124 void checkSymbol( const symbol_section_accessor& sr,
125                   Elf_Xword                      index,
126                   std::string                    name_,
127                   Elf64_Addr                     value_,
128                   Elf_Xword                      size_,
129                   unsigned char                  bind_,
130                   unsigned char                  type_,
131                   Elf_Half                       section_,
132                   unsigned char                  other_ )
133 {
134     std::string   name;
135     Elf64_Addr    value;
136     Elf_Xword     size;
137     unsigned char bind;
138     unsigned char type;
139     Elf_Half      section;
140     unsigned char other;
141 
142     BOOST_REQUIRE_EQUAL(
143         sr.get_symbol( index, name, value, size, bind, type, section, other ),
144         true );
145     BOOST_CHECK_EQUAL( name, name_ );
146     BOOST_CHECK_EQUAL( value, value_ );
147     BOOST_CHECK_EQUAL( size, size_ );
148     BOOST_CHECK_EQUAL( bind, bind_ );
149     BOOST_CHECK_EQUAL( type, type_ );
150     BOOST_CHECK_EQUAL( section, section_ );
151     BOOST_CHECK_EQUAL( other, other_ );
152 }
153 
154 ////////////////////////////////////////////////////////////////////////////////
checkRelocation(const relocation_section_accessor * pRT,Elf_Xword index,Elf64_Addr offset_,Elf64_Addr symbolValue_,std::string symbolName_,unsigned char type_,Elf_Sxword addend_,Elf_Sxword calcValue_)155 void checkRelocation( const relocation_section_accessor* pRT,
156                       Elf_Xword                          index,
157                       Elf64_Addr                         offset_,
158                       Elf64_Addr                         symbolValue_,
159                       std::string                        symbolName_,
160                       unsigned char                      type_,
161                       Elf_Sxword                         addend_,
162                       Elf_Sxword                         calcValue_ )
163 {
164     Elf64_Addr  offset;
165     Elf64_Addr  symbolValue;
166     std::string symbolName;
167     Elf_Word    type;
168     Elf_Sxword  addend;
169     Elf_Sxword  calcValue;
170 
171     BOOST_REQUIRE_EQUAL( pRT->get_entry( index, offset, symbolValue, symbolName,
172                                          type, addend, calcValue ),
173                          true );
174     BOOST_CHECK_EQUAL( offset, offset_ );
175     BOOST_CHECK_EQUAL( symbolValue, symbolValue_ );
176     BOOST_CHECK_EQUAL( symbolName, symbolName_ );
177     BOOST_CHECK_EQUAL( type, type_ );
178     BOOST_CHECK_EQUAL( addend, addend_ );
179     BOOST_CHECK_EQUAL( calcValue, calcValue_ );
180 }
181 
182 ////////////////////////////////////////////////////////////////////////////////
checkNote(const note_section_accessor & notes,Elf_Word index,Elf_Word type_,std::string name_,Elf_Word descSize_)183 void checkNote( const note_section_accessor& notes,
184                 Elf_Word                     index,
185                 Elf_Word                     type_,
186                 std::string                  name_,
187                 Elf_Word                     descSize_ )
188 {
189     Elf_Word    type;
190     std::string name;
191     void*       desc;
192     Elf_Word    descSize;
193 
194     BOOST_REQUIRE_EQUAL( notes.get_note( index, type, name, desc, descSize ),
195                          true );
196     BOOST_CHECK_EQUAL( type, type_ );
197     BOOST_CHECK_EQUAL( name, name_ );
198     BOOST_CHECK_EQUAL( descSize, descSize_ );
199 }
200 
201 ////////////////////////////////////////////////////////////////////////////////
BOOST_AUTO_TEST_CASE(load32)202 BOOST_AUTO_TEST_CASE( load32 )
203 {
204     elfio reader;
205     BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/hello_32" ), true );
206     checkHeader( reader, ELFCLASS32, ELFDATA2LSB, EV_CURRENT, ET_EXEC, EM_386,
207                  1, 0x80482b0, 0, 28, 7, 0, 0 );
208 
209     ////////////////////////////////////////////////////////////////////////////
210     // Check sections
211     section* sec = reader.sections[0];
212     checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 );
213 
214     sec = reader.sections[1];
215     checkSection( sec, 1, ".interp", SHT_PROGBITS, SHF_ALLOC, 0x08048114, 0x13,
216                   0, 0, 1, 0 );
217 
218     sec = reader.sections[9];
219     checkSection( sec, 9, ".rel.plt", SHT_REL, SHF_ALLOC, 0x08048234, 0x18, 4,
220                   11, 4, 8 );
221 
222     sec = reader.sections[19];
223     checkSection( sec, 19, ".dynamic", SHT_DYNAMIC, SHF_WRITE | SHF_ALLOC,
224                   0x080494a0, 0xc8, 5, 0, 4, 8 );
225 
226     sec = reader.sections[27];
227     checkSection( sec, 27, ".strtab", SHT_STRTAB, 0, 0x0, 0x259, 0, 0, 1, 0 );
228 
229     const section* sec1 = reader.sections[".strtab"];
230     BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() );
231 
232     ////////////////////////////////////////////////////////////////////////////
233     // Check segments
234     segment* seg = reader.segments[0];
235     checkSegment( seg, PT_PHDR, 0x08048034, 0x08048034, 0x000e0, 0x000e0,
236                   PF_R + PF_X, 4 );
237 
238     seg = reader.segments[4];
239     checkSegment( seg, PT_DYNAMIC, 0x080494a0, 0x080494a0, 0x000c8, 0x000c8,
240                   PF_R + PF_W, 4 );
241 
242     seg = reader.segments[6];
243     checkSegment( seg, 0x6474E551, 0x0, 0x0, 0x0, 0x0, PF_R + PF_W, 4 );
244 
245     ////////////////////////////////////////////////////////////////////////////
246     // Check symbol table
247     sec = reader.sections[".symtab"];
248 
249     symbol_section_accessor sr( reader, sec );
250 
251     BOOST_CHECK_EQUAL( sr.get_symbols_num(), 68 );
252     checkSymbol( sr, 0, "", 0x00000000, 0, STB_LOCAL, STT_NOTYPE, STN_UNDEF,
253                  ELF_ST_VISIBILITY( STV_DEFAULT ) );
254     checkSymbol( sr, 1, "", 0x08048114, 0, STB_LOCAL, STT_SECTION, 1,
255                  ELF_ST_VISIBILITY( STV_DEFAULT ) );
256     checkSymbol( sr, 39, "hello.c", 0x00000000, 0, STB_LOCAL, STT_FILE, SHN_ABS,
257                  ELF_ST_VISIBILITY( STV_DEFAULT ) );
258     checkSymbol( sr, 65, "__i686.get_pc_thunk.bx", 0x08048429, 0, STB_GLOBAL,
259                  STT_FUNC, 12, ELF_ST_VISIBILITY( STV_HIDDEN ) );
260     checkSymbol( sr, 66, "main", 0x08048384, 43, STB_GLOBAL, STT_FUNC, 12,
261                  ELF_ST_VISIBILITY( STV_DEFAULT ) );
262     checkSymbol( sr, 67, "_init", 0x0804824c, 0, STB_GLOBAL, STT_FUNC, 10,
263                  ELF_ST_VISIBILITY( STV_DEFAULT ) );
264 
265     ////////////////////////////////////////////////////////////////////////////
266     // Check relocation table
267     sec = reader.sections[".rel.dyn"];
268 
269     relocation_section_accessor reloc( reader, sec );
270     BOOST_CHECK_EQUAL( reloc.get_entries_num(), 1 );
271 
272     checkRelocation( &reloc, 0, 0x08049568, 0x0, "__gmon_start__",
273                      R_386_GLOB_DAT, 0, 0 );
274 
275     sec = reader.sections[".rel.plt"];
276 
277     relocation_section_accessor reloc1( reader, sec );
278     BOOST_CHECK_EQUAL( reloc1.get_entries_num(), 3 );
279 
280     checkRelocation( &reloc1, 0, 0x08049578, 0x0, "__gmon_start__",
281                      R_X86_64_JUMP_SLOT, 0, 0 );
282     checkRelocation( &reloc1, 1, 0x0804957c, 0x0, "__libc_start_main",
283                      R_X86_64_JUMP_SLOT, 0, 0 );
284     checkRelocation( &reloc1, 2, 0x08049580, 0x0, "puts", R_X86_64_JUMP_SLOT, 0,
285                      0 );
286 
287     ////////////////////////////////////////////////////////////////////////////
288     // Check note reader
289     sec = reader.sections[".note.ABI-tag"];
290 
291     note_section_accessor notes( reader, sec );
292     BOOST_CHECK_EQUAL( notes.get_notes_num(), 1u );
293 
294     checkNote( notes, 0, 1, std::string( "GNU" ), 16 );
295 }
296 
297 ////////////////////////////////////////////////////////////////////////////////
BOOST_AUTO_TEST_CASE(load64)298 BOOST_AUTO_TEST_CASE( load64 )
299 {
300     elfio reader;
301 
302     BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/hello_64" ), true );
303 
304     ////////////////////////////////////////////////////////////////////////////
305     // Check ELF header
306     checkHeader( reader, ELFCLASS64, ELFDATA2LSB, EV_CURRENT, ET_EXEC,
307                  EM_X86_64, 1, 0x4003c0, 0, 29, 8, 0, 0 );
308 
309     ////////////////////////////////////////////////////////////////////////////
310     // Check sections
311     section* sec = reader.sections[0];
312 
313     checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 );
314 
315     sec = reader.sections[1];
316 
317     checkSection( sec, 1, ".interp", SHT_PROGBITS, SHF_ALLOC,
318                   0x0000000000400200, 0x1c, 0, 0, 1, 0 );
319 
320     sec = reader.sections[9];
321 
322     checkSection( sec, 9, ".rela.plt", SHT_RELA, SHF_ALLOC, 0x0000000000400340,
323                   0x30, 4, 11, 8, 0x18 );
324 
325     sec = reader.sections[20];
326 
327     checkSection( sec, 20, ".dynamic", SHT_DYNAMIC, SHF_WRITE | SHF_ALLOC,
328                   0x0000000000600698, 0x190, 5, 0, 8, 0x10 );
329 
330     sec = reader.sections[28];
331 
332     checkSection( sec, 28, ".strtab", SHT_STRTAB, 0, 0x0, 0x23f, 0, 0, 1, 0 );
333 
334     const section* sec1 = reader.sections[".strtab"];
335     BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() );
336 
337     ////////////////////////////////////////////////////////////////////////////
338     // Check segments
339     segment* seg = reader.segments[0];
340     checkSegment( seg, PT_PHDR, 0x0000000000400040, 0x0000000000400040,
341                   0x00000000000001c0, 0x00000000000001c0, PF_R + PF_X, 8 );
342 
343     seg = reader.segments[2];
344     checkSegment( seg, PT_LOAD, 0x0000000000400000, 0x0000000000400000,
345                   0x000000000000066c, 0x000000000000066c, PF_R + PF_X,
346                   0x200000 );
347 
348     seg = reader.segments[7];
349     checkSegment( seg, 0x6474E551, 0x0, 0x0, 0x0, 0x0, PF_R + PF_W, 8 );
350 
351     ////////////////////////////////////////////////////////////////////////////
352     // Check symbol table
353     sec = reader.sections[".symtab"];
354 
355     symbol_section_accessor sr( reader, sec );
356 
357     BOOST_CHECK_EQUAL( sr.get_symbols_num(), 67 );
358     checkSymbol( sr, 0, "", 0x00000000, 0, STB_LOCAL, STT_NOTYPE, STN_UNDEF,
359                  ELF_ST_VISIBILITY( STV_DEFAULT ) );
360     checkSymbol( sr, 1, "", 0x00400200, 0, STB_LOCAL, STT_SECTION, 1,
361                  ELF_ST_VISIBILITY( STV_DEFAULT ) );
362     checkSymbol( sr, 40, "hello.c", 0x00000000, 0, STB_LOCAL, STT_FILE, SHN_ABS,
363                  ELF_ST_VISIBILITY( STV_DEFAULT ) );
364     checkSymbol( sr, 52, "__gmon_start__", 0x00000000, 0, STB_WEAK, STT_NOTYPE,
365                  STN_UNDEF, ELF_ST_VISIBILITY( STV_DEFAULT ) );
366     checkSymbol( sr, 64, "_edata", 0x0060085c, 0, STB_GLOBAL, STT_NOTYPE,
367                  SHN_ABS, ELF_ST_VISIBILITY( STV_DEFAULT ) );
368     checkSymbol( sr, 65, "main", 0x00400498, 21, STB_GLOBAL, STT_FUNC, 12,
369                  ELF_ST_VISIBILITY( STV_DEFAULT ) );
370     checkSymbol( sr, 66, "_init", 0x00400370, 0, STB_GLOBAL, STT_FUNC, 10,
371                  ELF_ST_VISIBILITY( STV_DEFAULT ) );
372 
373     ////////////////////////////////////////////////////////////////////////////
374     // Check relocation table
375     sec = reader.sections[".rela.dyn"];
376 
377     relocation_section_accessor reloc( reader, sec );
378     BOOST_CHECK_EQUAL( reloc.get_entries_num(), 1 );
379 
380     checkRelocation( &reloc, 0, 0x00600828, 0x0, "__gmon_start__",
381                      R_X86_64_GLOB_DAT, 0, 0 );
382 
383     sec = reader.sections[".rela.plt"];
384 
385     relocation_section_accessor reloc1( reader, sec );
386     BOOST_CHECK_EQUAL( reloc1.get_entries_num(), 2 );
387 
388     checkRelocation( &reloc1, 0, 0x00600848, 0x0, "puts", R_X86_64_JUMP_SLOT, 0,
389                      0 );
390     checkRelocation( &reloc1, 1, 0x00600850, 0x0, "__libc_start_main",
391                      R_X86_64_JUMP_SLOT, 0, 0 );
392 
393     ////////////////////////////////////////////////////////////////////////////
394     // Check note reader
395     sec = reader.sections[".note.ABI-tag"];
396 
397     note_section_accessor notes( reader, sec );
398     BOOST_CHECK_EQUAL( notes.get_notes_num(), 1u );
399 
400     checkNote( notes, 0, 1, std::string( "GNU" ), 16 );
401 }
402 
403 ////////////////////////////////////////////////////////////////////////////////
BOOST_AUTO_TEST_CASE(hello_64_o)404 BOOST_AUTO_TEST_CASE( hello_64_o )
405 {
406     elfio reader;
407 
408     BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/hello_64.o" ), true );
409 
410     ////////////////////////////////////////////////////////////////////////////
411     // Check ELF header
412     checkHeader( reader, ELFCLASS64, ELFDATA2LSB, EV_CURRENT, ET_REL, EM_X86_64,
413                  1, 0, 0, 13, 0, 0, 0 );
414 
415     ////////////////////////////////////////////////////////////////////////////
416     // Check sections
417     section* sec = reader.sections[0];
418 
419     checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 );
420 
421     sec = reader.sections[1];
422 
423     checkSection( sec, 1, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, 0x0,
424                   0x15, 0, 0, 4, 0 );
425 
426     section* sec1 = reader.sections[".text"];
427     BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() );
428 
429     sec = reader.sections[12];
430     checkSection( sec, 12, ".strtab", SHT_STRTAB, 0, 0x0, 0x13, 0, 0, 1, 0 );
431 
432     sec1 = reader.sections[".strtab"];
433     BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() );
434 
435     ////////////////////////////////////////////////////////////////////////////
436     // Check symbol table
437     sec = reader.sections[".symtab"];
438 
439     symbol_section_accessor sr( reader, sec );
440 
441     BOOST_CHECK_EQUAL( sr.get_symbols_num(), 11 );
442     checkSymbol( sr, 9, "main", 0x00000000, 21, STB_GLOBAL, STT_FUNC, 1,
443                  ELF_ST_VISIBILITY( STV_DEFAULT ) );
444 
445     ////////////////////////////////////////////////////////////////////////////
446     // Check relocation table
447     sec = reader.sections[".rela.text"];
448 
449     relocation_section_accessor reloc( reader, sec );
450     BOOST_CHECK_EQUAL( reloc.get_entries_num(), 2 );
451 
452     checkRelocation( &reloc, 0, 0x00000005, 0x0, "", R_X86_64_32, 0, 0 );
453     checkRelocation( &reloc, 1, 0x0000000A, 0x0, "puts", R_X86_64_PC32,
454                      0xfffffffffffffffcULL, -14 );
455 
456     sec = reader.sections[".rela.eh_frame"];
457 
458     relocation_section_accessor reloc1( reader, sec );
459     BOOST_CHECK_EQUAL( reloc1.get_entries_num(), 1 );
460 
461     checkRelocation( &reloc1, 0, 0x00000020, 0x0, "", R_X86_64_32, 0, 0 );
462 }
463 
464 ////////////////////////////////////////////////////////////////////////////////
BOOST_AUTO_TEST_CASE(hello_32_o)465 BOOST_AUTO_TEST_CASE( hello_32_o )
466 {
467     elfio reader;
468 
469     BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/hello_32.o" ), true );
470 
471     ////////////////////////////////////////////////////////////////////////////
472     // Check ELF header
473     checkHeader( reader, ELFCLASS32, ELFDATA2LSB, EV_CURRENT, ET_REL, EM_386, 1,
474                  0, 0, 11, 0, 0, 0 );
475 
476     ////////////////////////////////////////////////////////////////////////////
477     // Check sections
478     section* sec = reader.sections[0];
479 
480     checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 );
481 
482     sec = reader.sections[1];
483 
484     checkSection( sec, 1, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, 0x0,
485                   0x2b, 0, 0, 4, 0 );
486 
487     section* sec1 = reader.sections[".text"];
488     BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() );
489 
490     sec = reader.sections[10];
491 
492     checkSection( sec, 10, ".strtab", SHT_STRTAB, 0, 0x0, 0x13, 0, 0, 1, 0 );
493 
494     sec1 = reader.sections[".strtab"];
495     BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() );
496 
497     ////////////////////////////////////////////////////////////////////////////
498     // Check symbol table
499     sec = reader.sections[".symtab"];
500 
501     symbol_section_accessor sr( reader, sec );
502 
503     BOOST_CHECK_EQUAL( sr.get_symbols_num(), 10 );
504     checkSymbol( sr, 8, "main", 0x00000000, 43, STB_GLOBAL, STT_FUNC, 1,
505                  ELF_ST_VISIBILITY( STV_DEFAULT ) );
506 
507     ////////////////////////////////////////////////////////////////////////////
508     // Check relocation table
509     sec = reader.sections[".rel.text"];
510 
511     relocation_section_accessor reloc( reader, sec );
512     BOOST_CHECK_EQUAL( reloc.get_entries_num(), 2 );
513 
514     checkRelocation( &reloc, 0, 0x00000014, 0x0, "", R_386_32, 0, 0 );
515     checkRelocation( &reloc, 1, 0x00000019, 0x0, "puts", R_386_PC32, 0x0, -25 );
516 }
517 
518 ////////////////////////////////////////////////////////////////////////////////
BOOST_AUTO_TEST_CASE(test_ppc_o)519 BOOST_AUTO_TEST_CASE( test_ppc_o )
520 {
521     elfio reader;
522 
523     BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/test_ppc.o" ), true );
524 
525     ////////////////////////////////////////////////////////////////////////////
526     // Check ELF header
527     checkHeader( reader, ELFCLASS32, ELFDATA2MSB, EV_CURRENT, ET_REL, EM_PPC, 1,
528                  0, 0, 16, 0, 0, 0 );
529 
530     ////////////////////////////////////////////////////////////////////////////
531     // Check sections
532     section* sec = reader.sections[0];
533 
534     checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 );
535 
536     sec = reader.sections[1];
537 
538     checkSection( sec, 1, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, 0x0,
539                   0x118, 0, 0, 4, 0 );
540 
541     section* sec1 = reader.sections[".text"];
542     BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() );
543 
544     sec = reader.sections[15];
545 
546     checkSection( sec, 15, ".strtab", SHT_STRTAB, 0, 0x0, 0x14f, 0, 0, 1, 0 );
547 
548     sec1 = reader.sections[".strtab"];
549     BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() );
550 
551     ////////////////////////////////////////////////////////////////////////////
552     // Check symbol table
553     sec = reader.sections[".symtab"];
554 
555     symbol_section_accessor sr( reader, sec );
556 
557     BOOST_CHECK_EQUAL( sr.get_symbols_num(), 24 );
558     checkSymbol( sr, 14, "main", 0x00000000, 92, STB_GLOBAL, STT_FUNC, 1,
559                  ELF_ST_VISIBILITY( STV_DEFAULT ) );
560     checkSymbol( sr, 8, "_GLOBAL__I_main", 0x000000DC, 60, STB_LOCAL, STT_FUNC,
561                  1, ELF_ST_VISIBILITY( STV_DEFAULT ) );
562 
563     ////////////////////////////////////////////////////////////////////////////
564     // Check relocation table
565     sec = reader.sections[".rela.text"];
566 
567     relocation_section_accessor reloc( reader, sec );
568     BOOST_CHECK_EQUAL( reloc.get_entries_num(), 18 );
569 
570     checkRelocation( &reloc, 0, 0x00000016, 0x0, "_ZSt4cout", 6, 0, 0 );
571     checkRelocation( &reloc, 1, 0x0000001a, 0x0, "_ZSt4cout", 4, 0x0, 0 );
572     checkRelocation( &reloc, 17, 0x000000c0, 0x0, "__cxa_atexit", 10, 0x0, 0 );
573 
574     sec = reader.sections[".rela.ctors"];
575 
576     relocation_section_accessor reloc1( reader, sec );
577     BOOST_CHECK_EQUAL( reloc1.get_entries_num(), 1 );
578 
579     checkRelocation( &reloc1, 0, 0x00000000, 0x0, "", 1, 0xDC, 0xDC );
580 
581     sec = reader.sections[".rela.eh_frame"];
582 
583     relocation_section_accessor reloc2( reader, sec );
584     BOOST_CHECK_EQUAL( reloc2.get_entries_num(), 3 );
585 
586     checkRelocation( &reloc2, 1, 0x00000020, 0x0, "", 1, 0x0, 0x0 );
587 }
588 
589 ////////////////////////////////////////////////////////////////////////////////
BOOST_AUTO_TEST_CASE(test_ppc)590 BOOST_AUTO_TEST_CASE( test_ppc )
591 {
592     elfio reader;
593 
594     BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/test_ppc" ), true );
595 
596     ////////////////////////////////////////////////////////////////////////////
597     // Check ELF header
598     checkHeader( reader, ELFCLASS32, ELFDATA2MSB, EV_CURRENT, ET_EXEC, EM_PPC,
599                  1, 0x10000550, 0, 31, 8, 0, 0 );
600 
601     ////////////////////////////////////////////////////////////////////////////
602     // Check sections
603     section* sec = reader.sections[0];
604 
605     checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 );
606 
607     sec = reader.sections[1];
608 
609     checkSection( sec, 1, ".interp", SHT_PROGBITS, SHF_ALLOC,
610                   0x0000000010000134, 0xd, 0, 0, 1, 0 );
611 
612     sec = reader.sections[9];
613 
614     checkSection( sec, 9, ".rela.plt", SHT_RELA, SHF_ALLOC, 0x00000000010000494,
615                   0x6c, 4, 22, 4, 0xc );
616 
617     sec = reader.sections[20];
618 
619     checkSection( sec, 20, ".dynamic", SHT_DYNAMIC, SHF_WRITE | SHF_ALLOC,
620                   0x0000000010010aec, 0xe8, 5, 0, 4, 0x8 );
621 
622     sec = reader.sections[28];
623 
624     checkSection( sec, 28, ".shstrtab", SHT_STRTAB, 0, 0x0, 0x101, 0, 0, 1, 0 );
625 
626     const section* sec1 = reader.sections[".shstrtab"];
627     BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() );
628 
629     ////////////////////////////////////////////////////////////////////////////
630     // Check segments
631     segment* seg = reader.segments[0];
632     checkSegment( seg, PT_PHDR, 0x10000034, 0x10000034, 0x00100, 0x00100,
633                   PF_R + PF_X, 4 );
634 
635     seg = reader.segments[2];
636     checkSegment( seg, PT_LOAD, 0x10000000, 0x10000000, 0x00acc, 0x00acc,
637                   PF_R + PF_X, 0x10000 );
638 
639     seg = reader.segments[7];
640     checkSegment( seg, 0x6474E551, 0x0, 0x0, 0x0, 0x0, PF_R + PF_W, 0x4 );
641 
642     ////////////////////////////////////////////////////////////////////////////
643     // Check symbol table
644     sec = reader.sections[".symtab"];
645 
646     symbol_section_accessor sr( reader, sec );
647 
648     BOOST_CHECK_EQUAL( sr.get_symbols_num(), 80 );
649     checkSymbol( sr, 0, "", 0x00000000, 0, STB_LOCAL, STT_NOTYPE, STN_UNDEF,
650                  ELF_ST_VISIBILITY( STV_DEFAULT ) );
651     checkSymbol( sr, 1, "", 0x10000134, 0, STB_LOCAL, STT_SECTION, 1,
652                  ELF_ST_VISIBILITY( STV_DEFAULT ) );
653     checkSymbol( sr, 40, "__CTOR_END__", 0x10010AD4, 0, STB_LOCAL, STT_OBJECT,
654                  16, ELF_ST_VISIBILITY( STV_DEFAULT ) );
655     checkSymbol( sr, 52, "__init_array_start", 0x10010acc, 0, STB_LOCAL,
656                  STT_NOTYPE, 16, ELF_ST_VISIBILITY( STV_HIDDEN ) );
657     checkSymbol( sr, 64, "_ZNSt8ios_base4InitD1Ev@@GLIBCXX_3.4", 0x10000920,
658                  204, STB_GLOBAL, STT_FUNC, SHN_UNDEF,
659                  ELF_ST_VISIBILITY( STV_DEFAULT ) );
660     checkSymbol( sr, 78, "main", 0x1000069c, 92, STB_GLOBAL, STT_FUNC, 11,
661                  ELF_ST_VISIBILITY( STV_DEFAULT ) );
662     checkSymbol( sr, 79, "_init", 0x10000500, 0, STB_GLOBAL, STT_FUNC, 10,
663                  ELF_ST_VISIBILITY( STV_DEFAULT ) );
664 
665     ////////////////////////////////////////////////////////////////////////////
666     // Check relocation table
667     sec = reader.sections[".rela.dyn"];
668 
669     relocation_section_accessor reloc( reader, sec );
670     BOOST_CHECK_EQUAL( reloc.get_entries_num(), 2 );
671 
672     checkRelocation( &reloc, 1, 0x10010c0c, 0x10010c0c, "_ZSt4cout", 19, 0, 0 );
673 
674     sec = reader.sections[".rela.plt"];
675 
676     relocation_section_accessor reloc1( reader, sec );
677     BOOST_CHECK_EQUAL( reloc1.get_entries_num(), 9 );
678 
679     checkRelocation( &reloc1, 0, 0x10010be4, 0x100008e0, "__cxa_atexit", 21, 0,
680                      0 );
681     checkRelocation( &reloc1, 1, 0x10010be8, 0x0, "__gmon_start__", 21, 0, 0 );
682 
683     ////////////////////////////////////////////////////////////////////////////
684     // Check note reader
685     sec = reader.sections[".note.ABI-tag"];
686 
687     note_section_accessor notes( reader, sec );
688     BOOST_CHECK_EQUAL( notes.get_notes_num(), 1u );
689 
690     checkNote( notes, 0, 1, std::string( "GNU" ), 16 );
691 }
692 
693 ////////////////////////////////////////////////////////////////////////////////
BOOST_AUTO_TEST_CASE(test_dummy_out_i386_32)694 BOOST_AUTO_TEST_CASE( test_dummy_out_i386_32 )
695 {
696     elfio writer;
697 
698     writer.create( ELFCLASS32, ELFDATA2LSB );
699 
700     writer.set_os_abi( 0 );
701     writer.set_abi_version( 0 );
702     writer.set_type( ET_REL );
703     writer.set_machine( EM_386 );
704     writer.set_flags( 0 );
705 
706     // Set program entry point
707     writer.set_entry( 0x80482b0 );
708 
709     // Add Note section
710     section* note_sec = writer.sections.add( ".note" );
711     note_sec->set_type( SHT_NOTE );
712     note_sec->set_flags( SHF_ALLOC );
713     note_sec->set_addr_align( 4 );
714     note_section_accessor note_writer( writer, note_sec );
715     char                  descr[6] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
716     note_writer.add_note( 0x77, "Hello", &descr, 6 );
717     BOOST_CHECK_EQUAL( note_sec->get_index(), 2 );
718 
719     // Create ELF file
720     writer.save( "elf_examples/elf_dummy_header_i386_32.elf" );
721 
722     elfio reader;
723     BOOST_REQUIRE_EQUAL(
724         reader.load( "elf_examples/elf_dummy_header_i386_32.elf" ), true );
725 
726     ////////////////////////////////////////////////////////////////////////////
727     // Check ELF header
728     checkHeader( reader, ELFCLASS32, ELFDATA2LSB, EV_CURRENT, ET_REL, EM_386,
729                  EV_CURRENT, 0x80482b0, 0, 3, 0, 0, 0 );
730     ////////////////////////////////////////////////////////////////////////////
731     // Check sections
732     section* sec = reader.sections[""];
733 
734     checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 );
735 
736     sec = reader.sections[".shstrtab"];
737 
738     checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0, 0, 17, 0, 0, 1, 0 );
739 
740     sec = reader.sections[".note"];
741 
742     BOOST_CHECK_EQUAL( sec->get_index(), 2 );
743     checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC, 0, 28, 0, 0, 4, 0 );
744 }
745 
746 ////////////////////////////////////////////////////////////////////////////////
BOOST_AUTO_TEST_CASE(test_dummy_out_ppc_32)747 BOOST_AUTO_TEST_CASE( test_dummy_out_ppc_32 )
748 {
749     elfio writer;
750 
751     writer.create( ELFCLASS32, ELFDATA2MSB );
752 
753     writer.set_os_abi( 0 );
754     writer.set_abi_version( 0 );
755     writer.set_type( ET_REL );
756     writer.set_machine( EM_PPC );
757     writer.set_flags( 0 );
758 
759     // Set program entry point
760     writer.set_entry( 0x80482b0 );
761 
762     // Add Note section
763     section* note_sec = writer.sections.add( ".note" );
764     note_sec->set_type( SHT_NOTE );
765     note_sec->set_flags( SHF_ALLOC );
766     note_sec->set_addr_align( 4 );
767     note_section_accessor note_writer( writer, note_sec );
768     char                  descr[6] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
769     note_writer.add_note( 0x77, "Hello", &descr, 6 );
770     BOOST_CHECK_EQUAL( note_sec->get_index(), 2 );
771 
772     // Create ELF file
773     writer.save( "elf_examples/elf_dummy_header_ppc_32.elf" );
774 
775     elfio reader;
776     BOOST_REQUIRE_EQUAL(
777         reader.load( "elf_examples/elf_dummy_header_ppc_32.elf" ), true );
778 
779     ////////////////////////////////////////////////////////////////////////////
780     // Check ELF header
781     checkHeader( reader, ELFCLASS32, ELFDATA2MSB, EV_CURRENT, ET_REL, EM_PPC,
782                  EV_CURRENT, 0x80482b0, 0, 3, 0, 0, 0 );
783     ////////////////////////////////////////////////////////////////////////////
784     // Check sections
785     section* sec = reader.sections[""];
786 
787     checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 );
788 
789     sec = reader.sections[".note"];
790 
791     BOOST_CHECK_EQUAL( sec->get_index(), 2 );
792     checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC, 0, 28, 0, 0, 4, 0 );
793 
794     sec = reader.sections[".shstrtab"];
795 
796     checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0, 0, 17, 0, 0, 1, 0 );
797 }
798 
799 ////////////////////////////////////////////////////////////////////////////////
BOOST_AUTO_TEST_CASE(test_dummy_out_i386_64)800 BOOST_AUTO_TEST_CASE( test_dummy_out_i386_64 )
801 {
802     elfio writer;
803 
804     writer.create( ELFCLASS64, ELFDATA2LSB );
805 
806     writer.set_os_abi( 0 );
807     writer.set_abi_version( 0 );
808     writer.set_type( ET_REL );
809     writer.set_machine( EM_X86_64 );
810     writer.set_flags( 0 );
811 
812     // Set program entry point
813     writer.set_entry( 0x120380482b0ull );
814 
815     // Add Note section
816     section* note_sec = writer.sections.add( ".note" );
817     note_sec->set_type( SHT_NOTE );
818     note_sec->set_flags( SHF_ALLOC );
819     note_sec->set_addr_align( 4 );
820     note_section_accessor note_writer( writer, note_sec );
821     char                  descr[6] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
822     note_writer.add_note( 0x77, "Hello", &descr, 6 );
823     BOOST_CHECK_EQUAL( note_sec->get_index(), 2 );
824 
825     // Create ELF file
826     writer.save( "elf_examples/elf_dummy_header_i386_64.elf" );
827 
828     elfio reader;
829     BOOST_REQUIRE_EQUAL(
830         reader.load( "elf_examples/elf_dummy_header_i386_64.elf" ), true );
831 
832     ////////////////////////////////////////////////////////////////////////////
833     // Check ELF header
834     checkHeader( reader, ELFCLASS64, ELFDATA2LSB, EV_CURRENT, ET_REL, EM_X86_64,
835                  EV_CURRENT, 0x120380482b0ull, 0, 3, 0, 0, 0 );
836     ////////////////////////////////////////////////////////////////////////////
837     // Check sections
838     section* sec = reader.sections[""];
839 
840     checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 );
841 
842     sec = reader.sections[".note"];
843 
844     BOOST_CHECK_EQUAL( sec->get_index(), 2 );
845     checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC, 0, 28, 0, 0, 4, 0 );
846 
847     sec = reader.sections[".shstrtab"];
848 
849     checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0, 0, 17, 0, 0, 1, 0 );
850 }
851 
852 ////////////////////////////////////////////////////////////////////////////////
BOOST_AUTO_TEST_CASE(test_dummy_out_ppc_64)853 BOOST_AUTO_TEST_CASE( test_dummy_out_ppc_64 )
854 {
855     elfio writer;
856 
857     writer.create( ELFCLASS64, ELFDATA2MSB );
858 
859     writer.set_os_abi( 0 );
860     writer.set_abi_version( 0 );
861     writer.set_type( ET_REL );
862     writer.set_machine( EM_PPC64 );
863     writer.set_flags( 0 );
864 
865     // Set program entry point
866     writer.set_entry( 0x120380482b0ull );
867 
868     // Add Note section
869     section* note_sec = writer.sections.add( ".note" );
870     note_sec->set_type( SHT_NOTE );
871     note_sec->set_flags( SHF_ALLOC );
872     note_sec->set_addr_align( 4 );
873     note_section_accessor note_writer( writer, note_sec );
874     char                  descr[6] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
875     note_writer.add_note( 0x77, "Hello", &descr, 6 );
876     BOOST_CHECK_EQUAL( note_sec->get_index(), 2 );
877 
878     // Create ELF file
879     writer.save( "elf_examples/elf_dummy_header_ppc_64.elf" );
880 
881     elfio reader;
882     BOOST_REQUIRE_EQUAL(
883         reader.load( "elf_examples/elf_dummy_header_ppc_64.elf" ), true );
884 
885     ////////////////////////////////////////////////////////////////////////////
886     // Check ELF header
887     checkHeader( reader, ELFCLASS64, ELFDATA2MSB, EV_CURRENT, ET_REL, EM_PPC64,
888                  EV_CURRENT, 0x120380482b0ull, 0, 3, 0, 0, 0 );
889     ////////////////////////////////////////////////////////////////////////////
890     // Check sections
891     section* sec = reader.sections[""];
892 
893     checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 );
894 
895     sec = reader.sections[".shstrtab"];
896 
897     checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0, 0, 17, 0, 0, 1, 0 );
898 
899     sec = reader.sections[".note"];
900 
901     BOOST_CHECK_EQUAL( sec->get_index(), 2 );
902     checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC, 0, 28, 0, 0, 4, 0 );
903 }
904 
905 ////////////////////////////////////////////////////////////////////////////////
BOOST_AUTO_TEST_CASE(test_dynamic_64_1)906 BOOST_AUTO_TEST_CASE( test_dynamic_64_1 )
907 {
908     elfio reader;
909 
910     BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/main" ), true );
911 
912     section* dynsec = reader.sections[".dynamic"];
913     BOOST_REQUIRE( dynsec != NULL );
914 
915     dynamic_section_accessor da( reader, dynsec );
916 
917     BOOST_CHECK_EQUAL( da.get_entries_num(), 21 );
918 
919     Elf_Xword   tag;
920     Elf_Xword   value;
921     std::string str;
922     da.get_entry( 0, tag, value, str );
923     BOOST_CHECK_EQUAL( tag, DT_NEEDED );
924     BOOST_CHECK_EQUAL( str, "libfunc.so" );
925     da.get_entry( 1, tag, value, str );
926     BOOST_CHECK_EQUAL( tag, DT_NEEDED );
927     BOOST_CHECK_EQUAL( str, "libc.so.6" );
928     da.get_entry( 2, tag, value, str );
929     BOOST_CHECK_EQUAL( tag, DT_INIT );
930     BOOST_CHECK_EQUAL( value, 0x400530 );
931     da.get_entry( 19, tag, value, str );
932     BOOST_CHECK_EQUAL( tag, 0x6ffffff0 );
933     BOOST_CHECK_EQUAL( value, 0x40047e );
934     da.get_entry( 20, tag, value, str );
935     BOOST_CHECK_EQUAL( tag, DT_NULL );
936     BOOST_CHECK_EQUAL( value, 0 );
937 }
938 
939 ////////////////////////////////////////////////////////////////////////////////
BOOST_AUTO_TEST_CASE(test_dynamic_64_2)940 BOOST_AUTO_TEST_CASE( test_dynamic_64_2 )
941 {
942     elfio reader;
943 
944     BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/libfunc.so" ), true );
945 
946     section* dynsec = reader.sections[".dynamic"];
947     BOOST_REQUIRE( dynsec != NULL );
948 
949     dynamic_section_accessor da( reader, dynsec );
950 
951     BOOST_CHECK_EQUAL( da.get_entries_num(), 20 );
952 
953     Elf_Xword   tag;
954     Elf_Xword   value;
955     std::string str;
956     da.get_entry( 0, tag, value, str );
957     BOOST_CHECK_EQUAL( tag, DT_NEEDED );
958     BOOST_CHECK_EQUAL( str, "libc.so.6" );
959     da.get_entry( 1, tag, value, str );
960     BOOST_CHECK_EQUAL( tag, DT_INIT );
961     BOOST_CHECK_EQUAL( value, 0x480 );
962     da.get_entry( 18, tag, value, str );
963     BOOST_CHECK_EQUAL( tag, 0x6ffffff9 );
964     BOOST_CHECK_EQUAL( value, 1 );
965     da.get_entry( 19, tag, value, str );
966     BOOST_CHECK_EQUAL( tag, DT_NULL );
967     BOOST_CHECK_EQUAL( value, 0 );
968 }
969 
970 ////////////////////////////////////////////////////////////////////////////////
BOOST_AUTO_TEST_CASE(test_dynamic_64_3)971 BOOST_AUTO_TEST_CASE( test_dynamic_64_3 )
972 {
973     elfio reader;
974 
975     BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/main" ), true );
976 
977     section* dynsec = reader.sections[".dynamic"];
978     BOOST_REQUIRE( dynsec != NULL );
979 
980     dynamic_section_accessor da( reader, dynsec );
981     BOOST_CHECK_EQUAL( da.get_entries_num(), 21 );
982 
983     section* strsec1 = reader.sections.add( ".dynstr" );
984     strsec1->set_type( SHT_STRTAB );
985     strsec1->set_entry_size( reader.get_default_entry_size( SHT_STRTAB ) );
986 
987     section* dynsec1 = reader.sections.add( ".dynamic1" );
988     dynsec1->set_type( SHT_DYNAMIC );
989     dynsec1->set_entry_size( reader.get_default_entry_size( SHT_DYNAMIC ) );
990     dynsec1->set_link( strsec1->get_index() );
991     dynamic_section_accessor da1( reader, dynsec1 );
992 
993     Elf_Xword   tag, tag1;
994     Elf_Xword   value, value1;
995     std::string str, str1;
996 
997     for ( unsigned int i = 0; i < da.get_entries_num(); ++i ) {
998         da.get_entry( i, tag, value, str );
999         if ( tag == DT_NEEDED || tag == DT_SONAME || tag == DT_RPATH ||
1000              tag == DT_RUNPATH ) {
1001             da1.add_entry( tag, str );
1002         }
1003         else {
1004             da1.add_entry( tag, value );
1005         }
1006     }
1007 
1008     for ( unsigned int i = 0; i < da.get_entries_num(); ++i ) {
1009         da.get_entry( i, tag, value, str );
1010         da1.get_entry( i, tag1, value1, str1 );
1011 
1012         BOOST_CHECK_EQUAL( tag, tag1 );
1013         if ( tag == DT_NEEDED || tag == DT_SONAME || tag == DT_RPATH ||
1014              tag == DT_RUNPATH ) {
1015             BOOST_CHECK_EQUAL( str, str1 );
1016         }
1017         else {
1018             BOOST_CHECK_EQUAL( value, value1 );
1019         }
1020     }
1021 }
1022