165042b18Sopenharmony_ci/* 265042b18Sopenharmony_ciCopyright (C) 2001-present by Serge Lamikhov-Center 365042b18Sopenharmony_ci 465042b18Sopenharmony_ciPermission is hereby granted, free of charge, to any person obtaining a copy 565042b18Sopenharmony_ciof this software and associated documentation files (the "Software"), to deal 665042b18Sopenharmony_ciin the Software without restriction, including without limitation the rights 765042b18Sopenharmony_cito use, copy, modify, merge, publish, distribute, sublicense, and/or sell 865042b18Sopenharmony_cicopies of the Software, and to permit persons to whom the Software is 965042b18Sopenharmony_cifurnished to do so, subject to the following conditions: 1065042b18Sopenharmony_ci 1165042b18Sopenharmony_ciThe above copyright notice and this permission notice shall be included in 1265042b18Sopenharmony_ciall copies or substantial portions of the Software. 1365042b18Sopenharmony_ci 1465042b18Sopenharmony_ciTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1565042b18Sopenharmony_ciIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1665042b18Sopenharmony_ciFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1765042b18Sopenharmony_ciAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1865042b18Sopenharmony_ciLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 1965042b18Sopenharmony_ciOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 2065042b18Sopenharmony_ciTHE SOFTWARE. 2165042b18Sopenharmony_ci*/ 2265042b18Sopenharmony_ci 2365042b18Sopenharmony_ci#ifdef _MSC_VER 2465042b18Sopenharmony_ci#define _SCL_SECURE_NO_WARNINGS 2565042b18Sopenharmony_ci#endif 2665042b18Sopenharmony_ci 2765042b18Sopenharmony_ci#include <gtest/gtest.h> 2865042b18Sopenharmony_ci 2965042b18Sopenharmony_ci#include <elfio/elfio.hpp> 3065042b18Sopenharmony_ci#include <elfio/elfio_utils.hpp> 3165042b18Sopenharmony_ci 3265042b18Sopenharmony_ciusing namespace ELFIO; 3365042b18Sopenharmony_ci 3465042b18Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 3565042b18Sopenharmony_civoid checkHeader( const elfio& reader, 3665042b18Sopenharmony_ci unsigned char nClass, 3765042b18Sopenharmony_ci unsigned char encoding, 3865042b18Sopenharmony_ci unsigned char elfVersion, 3965042b18Sopenharmony_ci Elf_Half type, 4065042b18Sopenharmony_ci Elf_Half machine, 4165042b18Sopenharmony_ci Elf_Word version, 4265042b18Sopenharmony_ci Elf64_Addr entry, 4365042b18Sopenharmony_ci Elf_Word flags, 4465042b18Sopenharmony_ci Elf_Half secNum, 4565042b18Sopenharmony_ci Elf_Half segNum, 4665042b18Sopenharmony_ci unsigned char OSABI, 4765042b18Sopenharmony_ci unsigned char ABIVersion ) 4865042b18Sopenharmony_ci{ 4965042b18Sopenharmony_ci EXPECT_EQ( reader.get_class(), nClass ); 5065042b18Sopenharmony_ci EXPECT_EQ( reader.get_encoding(), encoding ); 5165042b18Sopenharmony_ci EXPECT_EQ( reader.get_elf_version(), elfVersion ); 5265042b18Sopenharmony_ci EXPECT_EQ( reader.get_os_abi(), OSABI ); 5365042b18Sopenharmony_ci EXPECT_EQ( reader.get_abi_version(), ABIVersion ); 5465042b18Sopenharmony_ci EXPECT_EQ( reader.get_type(), type ); 5565042b18Sopenharmony_ci EXPECT_EQ( reader.get_machine(), machine ); 5665042b18Sopenharmony_ci EXPECT_EQ( reader.get_version(), version ); 5765042b18Sopenharmony_ci EXPECT_EQ( reader.get_entry(), entry ); 5865042b18Sopenharmony_ci EXPECT_EQ( reader.get_flags(), flags ); 5965042b18Sopenharmony_ci EXPECT_EQ( reader.sections.size(), secNum ); 6065042b18Sopenharmony_ci EXPECT_EQ( reader.segments.size(), segNum ); 6165042b18Sopenharmony_ci} 6265042b18Sopenharmony_ci 6365042b18Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 6465042b18Sopenharmony_civoid checkSection( const section* sec, 6565042b18Sopenharmony_ci Elf_Half index, 6665042b18Sopenharmony_ci const std::string& name, 6765042b18Sopenharmony_ci Elf_Word type, 6865042b18Sopenharmony_ci Elf_Xword flags, 6965042b18Sopenharmony_ci Elf64_Addr address, 7065042b18Sopenharmony_ci Elf_Xword size, 7165042b18Sopenharmony_ci Elf_Word link, 7265042b18Sopenharmony_ci Elf_Word info, 7365042b18Sopenharmony_ci Elf_Xword addrAlign, 7465042b18Sopenharmony_ci Elf_Xword entrySize ) 7565042b18Sopenharmony_ci{ 7665042b18Sopenharmony_ci EXPECT_EQ( sec->get_index(), index ); 7765042b18Sopenharmony_ci EXPECT_EQ( sec->get_name(), name ); 7865042b18Sopenharmony_ci EXPECT_EQ( sec->get_type(), type ); 7965042b18Sopenharmony_ci EXPECT_EQ( sec->get_flags(), flags ); 8065042b18Sopenharmony_ci EXPECT_EQ( sec->get_address(), address ); 8165042b18Sopenharmony_ci EXPECT_EQ( sec->get_size(), size ); 8265042b18Sopenharmony_ci EXPECT_EQ( sec->get_link(), link ); 8365042b18Sopenharmony_ci EXPECT_EQ( sec->get_info(), info ); 8465042b18Sopenharmony_ci EXPECT_EQ( sec->get_addr_align(), addrAlign ); 8565042b18Sopenharmony_ci EXPECT_EQ( sec->get_entry_size(), entrySize ); 8665042b18Sopenharmony_ci} 8765042b18Sopenharmony_ci 8865042b18Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 8965042b18Sopenharmony_civoid checkSection( const section* sec, 9065042b18Sopenharmony_ci const std::string& name, 9165042b18Sopenharmony_ci Elf_Word type, 9265042b18Sopenharmony_ci Elf_Xword flags, 9365042b18Sopenharmony_ci Elf64_Addr address, 9465042b18Sopenharmony_ci Elf_Xword size, 9565042b18Sopenharmony_ci Elf_Word link, 9665042b18Sopenharmony_ci Elf_Word info, 9765042b18Sopenharmony_ci Elf_Xword addrAlign, 9865042b18Sopenharmony_ci Elf_Xword entrySize ) 9965042b18Sopenharmony_ci{ 10065042b18Sopenharmony_ci checkSection( sec, sec->get_index(), name, type, flags, address, size, link, 10165042b18Sopenharmony_ci info, addrAlign, entrySize ); 10265042b18Sopenharmony_ci} 10365042b18Sopenharmony_ci 10465042b18Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 10565042b18Sopenharmony_civoid checkSegment( const segment* seg, 10665042b18Sopenharmony_ci Elf_Word type, 10765042b18Sopenharmony_ci Elf64_Addr vaddr, 10865042b18Sopenharmony_ci Elf64_Addr paddr, 10965042b18Sopenharmony_ci Elf_Xword fsize, 11065042b18Sopenharmony_ci Elf_Xword msize, 11165042b18Sopenharmony_ci Elf_Word flags, 11265042b18Sopenharmony_ci Elf_Xword align ) 11365042b18Sopenharmony_ci{ 11465042b18Sopenharmony_ci EXPECT_EQ( seg->get_type(), type ); 11565042b18Sopenharmony_ci EXPECT_EQ( seg->get_virtual_address(), vaddr ); 11665042b18Sopenharmony_ci EXPECT_EQ( seg->get_physical_address(), paddr ); 11765042b18Sopenharmony_ci EXPECT_EQ( seg->get_file_size(), fsize ); 11865042b18Sopenharmony_ci EXPECT_EQ( seg->get_memory_size(), msize ); 11965042b18Sopenharmony_ci EXPECT_EQ( seg->get_flags(), flags ); 12065042b18Sopenharmony_ci EXPECT_EQ( seg->get_align(), align ); 12165042b18Sopenharmony_ci} 12265042b18Sopenharmony_ci 12365042b18Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 12465042b18Sopenharmony_civoid checkSymbol( const const_symbol_section_accessor& sr, 12565042b18Sopenharmony_ci Elf_Xword index, 12665042b18Sopenharmony_ci const std::string& name_, 12765042b18Sopenharmony_ci Elf64_Addr value_, 12865042b18Sopenharmony_ci Elf_Xword size_, 12965042b18Sopenharmony_ci unsigned char bind_, 13065042b18Sopenharmony_ci unsigned char type_, 13165042b18Sopenharmony_ci Elf_Half section_, 13265042b18Sopenharmony_ci unsigned char other_ ) 13365042b18Sopenharmony_ci{ 13465042b18Sopenharmony_ci std::string name; 13565042b18Sopenharmony_ci Elf64_Addr value; 13665042b18Sopenharmony_ci Elf_Xword size; 13765042b18Sopenharmony_ci unsigned char bind; 13865042b18Sopenharmony_ci unsigned char type; 13965042b18Sopenharmony_ci Elf_Half section; 14065042b18Sopenharmony_ci unsigned char other; 14165042b18Sopenharmony_ci 14265042b18Sopenharmony_ci ASSERT_EQ( 14365042b18Sopenharmony_ci sr.get_symbol( index, name, value, size, bind, type, section, other ), 14465042b18Sopenharmony_ci true ); 14565042b18Sopenharmony_ci EXPECT_EQ( name, name_ ); 14665042b18Sopenharmony_ci EXPECT_EQ( value, value_ ); 14765042b18Sopenharmony_ci EXPECT_EQ( size, size_ ); 14865042b18Sopenharmony_ci EXPECT_EQ( bind, bind_ ); 14965042b18Sopenharmony_ci EXPECT_EQ( type, type_ ); 15065042b18Sopenharmony_ci EXPECT_EQ( section, section_ ); 15165042b18Sopenharmony_ci EXPECT_EQ( other, other_ ); 15265042b18Sopenharmony_ci} 15365042b18Sopenharmony_ci 15465042b18Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 15565042b18Sopenharmony_civoid checkRelocation( const const_relocation_section_accessor* pRT, 15665042b18Sopenharmony_ci Elf_Xword index, 15765042b18Sopenharmony_ci Elf64_Addr offset_, 15865042b18Sopenharmony_ci Elf64_Addr symbolValue_, 15965042b18Sopenharmony_ci const std::string& symbolName_, 16065042b18Sopenharmony_ci unsigned char type_, 16165042b18Sopenharmony_ci Elf_Sxword addend_, 16265042b18Sopenharmony_ci Elf_Sxword calcValue_ ) 16365042b18Sopenharmony_ci{ 16465042b18Sopenharmony_ci Elf64_Addr offset; 16565042b18Sopenharmony_ci Elf64_Addr symbolValue; 16665042b18Sopenharmony_ci std::string symbolName; 16765042b18Sopenharmony_ci unsigned type; 16865042b18Sopenharmony_ci Elf_Sxword addend; 16965042b18Sopenharmony_ci Elf_Sxword calcValue; 17065042b18Sopenharmony_ci 17165042b18Sopenharmony_ci ASSERT_EQ( pRT->get_entry( index, offset, symbolValue, symbolName, type, 17265042b18Sopenharmony_ci addend, calcValue ), 17365042b18Sopenharmony_ci true ); 17465042b18Sopenharmony_ci EXPECT_EQ( offset, offset_ ); 17565042b18Sopenharmony_ci EXPECT_EQ( symbolValue, symbolValue_ ); 17665042b18Sopenharmony_ci EXPECT_EQ( symbolName, symbolName_ ); 17765042b18Sopenharmony_ci EXPECT_EQ( type, type_ ); 17865042b18Sopenharmony_ci EXPECT_EQ( addend, addend_ ); 17965042b18Sopenharmony_ci EXPECT_EQ( calcValue, calcValue_ ); 18065042b18Sopenharmony_ci} 18165042b18Sopenharmony_ci 18265042b18Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 18365042b18Sopenharmony_civoid checkNote( const const_note_section_accessor& notes, 18465042b18Sopenharmony_ci Elf_Word index, 18565042b18Sopenharmony_ci Elf_Word type_, 18665042b18Sopenharmony_ci const std::string& name_, 18765042b18Sopenharmony_ci Elf_Word descSize_ ) 18865042b18Sopenharmony_ci{ 18965042b18Sopenharmony_ci Elf_Word type; 19065042b18Sopenharmony_ci std::string name; 19165042b18Sopenharmony_ci char* desc; 19265042b18Sopenharmony_ci Elf_Word descSize; 19365042b18Sopenharmony_ci 19465042b18Sopenharmony_ci ASSERT_EQ( notes.get_note( index, type, name, desc, descSize ), true ); 19565042b18Sopenharmony_ci EXPECT_EQ( type, type_ ); 19665042b18Sopenharmony_ci EXPECT_EQ( name, name_ ); 19765042b18Sopenharmony_ci EXPECT_EQ( descSize, descSize_ ); 19865042b18Sopenharmony_ci} 19965042b18Sopenharmony_ci 20065042b18Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 20165042b18Sopenharmony_ciTEST( ELFIOTest, load32 ) 20265042b18Sopenharmony_ci{ 20365042b18Sopenharmony_ci bool is_lazy = false; 20465042b18Sopenharmony_ci do { 20565042b18Sopenharmony_ci is_lazy = !is_lazy; 20665042b18Sopenharmony_ci elfio reader; 20765042b18Sopenharmony_ci ASSERT_EQ( reader.load( "elf_examples/hello_32", is_lazy ), true ); 20865042b18Sopenharmony_ci checkHeader( reader, ELFCLASS32, ELFDATA2LSB, EV_CURRENT, ET_EXEC, 20965042b18Sopenharmony_ci EM_386, 1, 0x80482b0, 0, 28, 7, 0, 0 ); 21065042b18Sopenharmony_ci 21165042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 21265042b18Sopenharmony_ci // Check sections 21365042b18Sopenharmony_ci const section* sec = reader.sections[0]; 21465042b18Sopenharmony_ci checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 ); 21565042b18Sopenharmony_ci 21665042b18Sopenharmony_ci sec = reader.sections[1]; 21765042b18Sopenharmony_ci checkSection( sec, 1, ".interp", SHT_PROGBITS, SHF_ALLOC, 0x08048114, 21865042b18Sopenharmony_ci 0x13, 0, 0, 1, 0 ); 21965042b18Sopenharmony_ci 22065042b18Sopenharmony_ci sec = reader.sections[9]; 22165042b18Sopenharmony_ci checkSection( sec, 9, ".rel.plt", SHT_REL, SHF_ALLOC, 0x08048234, 0x18, 22265042b18Sopenharmony_ci 4, 11, 4, 8 ); 22365042b18Sopenharmony_ci 22465042b18Sopenharmony_ci sec = reader.sections[19]; 22565042b18Sopenharmony_ci checkSection( sec, 19, ".dynamic", SHT_DYNAMIC, SHF_WRITE | SHF_ALLOC, 22665042b18Sopenharmony_ci 0x080494a0, 0xc8, 5, 0, 4, 8 ); 22765042b18Sopenharmony_ci 22865042b18Sopenharmony_ci sec = reader.sections[27]; 22965042b18Sopenharmony_ci checkSection( sec, 27, ".strtab", SHT_STRTAB, 0, 0x0, 0x259, 0, 0, 1, 23065042b18Sopenharmony_ci 0 ); 23165042b18Sopenharmony_ci 23265042b18Sopenharmony_ci for ( Elf_Half i = 0; i < reader.sections.size(); ++i ) { 23365042b18Sopenharmony_ci sec = reader.sections[i]; 23465042b18Sopenharmony_ci EXPECT_EQ( sec->get_index(), i ); 23565042b18Sopenharmony_ci } 23665042b18Sopenharmony_ci 23765042b18Sopenharmony_ci const section* sec1 = reader.sections[".strtab"]; 23865042b18Sopenharmony_ci EXPECT_EQ( sec->get_index(), sec1->get_index() ); 23965042b18Sopenharmony_ci 24065042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 24165042b18Sopenharmony_ci // Check segments 24265042b18Sopenharmony_ci const segment* seg = reader.segments[0]; 24365042b18Sopenharmony_ci checkSegment( seg, PT_PHDR, 0x08048034, 0x08048034, 0x000e0, 0x000e0, 24465042b18Sopenharmony_ci PF_R + PF_X, 4 ); 24565042b18Sopenharmony_ci 24665042b18Sopenharmony_ci seg = reader.segments[4]; 24765042b18Sopenharmony_ci checkSegment( seg, PT_DYNAMIC, 0x080494a0, 0x080494a0, 0x000c8, 0x000c8, 24865042b18Sopenharmony_ci PF_R + PF_W, 4 ); 24965042b18Sopenharmony_ci 25065042b18Sopenharmony_ci seg = reader.segments[6]; 25165042b18Sopenharmony_ci checkSegment( seg, 0x6474E551, 0x0, 0x0, 0x0, 0x0, PF_R + PF_W, 4 ); 25265042b18Sopenharmony_ci 25365042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 25465042b18Sopenharmony_ci // Check symbol table 25565042b18Sopenharmony_ci sec = reader.sections[".symtab"]; 25665042b18Sopenharmony_ci 25765042b18Sopenharmony_ci const_symbol_section_accessor sr( reader, sec ); 25865042b18Sopenharmony_ci 25965042b18Sopenharmony_ci EXPECT_EQ( sr.get_symbols_num(), 68 ); 26065042b18Sopenharmony_ci checkSymbol( sr, 0, "", 0x00000000, 0, STB_LOCAL, STT_NOTYPE, STN_UNDEF, 26165042b18Sopenharmony_ci ELF_ST_VISIBILITY( STV_DEFAULT ) ); 26265042b18Sopenharmony_ci checkSymbol( sr, 1, "", 0x08048114, 0, STB_LOCAL, STT_SECTION, 1, 26365042b18Sopenharmony_ci ELF_ST_VISIBILITY( STV_DEFAULT ) ); 26465042b18Sopenharmony_ci checkSymbol( sr, 39, "hello.c", 0x00000000, 0, STB_LOCAL, STT_FILE, 26565042b18Sopenharmony_ci SHN_ABS, ELF_ST_VISIBILITY( STV_DEFAULT ) ); 26665042b18Sopenharmony_ci checkSymbol( sr, 65, "__i686.get_pc_thunk.bx", 0x08048429, 0, 26765042b18Sopenharmony_ci STB_GLOBAL, STT_FUNC, 12, 26865042b18Sopenharmony_ci ELF_ST_VISIBILITY( STV_HIDDEN ) ); 26965042b18Sopenharmony_ci checkSymbol( sr, 66, "main", 0x08048384, 43, STB_GLOBAL, STT_FUNC, 12, 27065042b18Sopenharmony_ci ELF_ST_VISIBILITY( STV_DEFAULT ) ); 27165042b18Sopenharmony_ci checkSymbol( sr, 67, "_init", 0x0804824c, 0, STB_GLOBAL, STT_FUNC, 10, 27265042b18Sopenharmony_ci ELF_ST_VISIBILITY( STV_DEFAULT ) ); 27365042b18Sopenharmony_ci 27465042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 27565042b18Sopenharmony_ci // Check relocation table 27665042b18Sopenharmony_ci sec = reader.sections[".rel.dyn"]; 27765042b18Sopenharmony_ci 27865042b18Sopenharmony_ci const_relocation_section_accessor reloc( reader, sec ); 27965042b18Sopenharmony_ci EXPECT_EQ( reloc.get_entries_num(), 1 ); 28065042b18Sopenharmony_ci 28165042b18Sopenharmony_ci checkRelocation( &reloc, 0, 0x08049568, 0x0, "__gmon_start__", 28265042b18Sopenharmony_ci R_386_GLOB_DAT, 0, 0 ); 28365042b18Sopenharmony_ci 28465042b18Sopenharmony_ci sec = reader.sections[".rel.plt"]; 28565042b18Sopenharmony_ci 28665042b18Sopenharmony_ci const_relocation_section_accessor reloc1( reader, sec ); 28765042b18Sopenharmony_ci EXPECT_EQ( reloc1.get_entries_num(), 3 ); 28865042b18Sopenharmony_ci 28965042b18Sopenharmony_ci checkRelocation( &reloc1, 0, 0x08049578, 0x0, "__gmon_start__", 29065042b18Sopenharmony_ci R_X86_64_JUMP_SLOT, 0, 0 ); 29165042b18Sopenharmony_ci checkRelocation( &reloc1, 1, 0x0804957c, 0x0, "__libc_start_main", 29265042b18Sopenharmony_ci R_X86_64_JUMP_SLOT, 0, 0 ); 29365042b18Sopenharmony_ci checkRelocation( &reloc1, 2, 0x08049580, 0x0, "puts", 29465042b18Sopenharmony_ci R_X86_64_JUMP_SLOT, 0, 0 ); 29565042b18Sopenharmony_ci 29665042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 29765042b18Sopenharmony_ci // Check note reader 29865042b18Sopenharmony_ci sec = reader.sections[".note.ABI-tag"]; 29965042b18Sopenharmony_ci 30065042b18Sopenharmony_ci const_note_section_accessor notes( reader, sec ); 30165042b18Sopenharmony_ci EXPECT_EQ( notes.get_notes_num(), 1u ); 30265042b18Sopenharmony_ci 30365042b18Sopenharmony_ci checkNote( notes, 0, 1, std::string( "GNU" ), 16 ); 30465042b18Sopenharmony_ci } while ( is_lazy ); 30565042b18Sopenharmony_ci} 30665042b18Sopenharmony_ci 30765042b18Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 30865042b18Sopenharmony_ciTEST( ELFIOTest, load64 ) 30965042b18Sopenharmony_ci{ 31065042b18Sopenharmony_ci bool is_lazy = false; 31165042b18Sopenharmony_ci do { 31265042b18Sopenharmony_ci is_lazy = !is_lazy; 31365042b18Sopenharmony_ci elfio reader; 31465042b18Sopenharmony_ci 31565042b18Sopenharmony_ci ASSERT_EQ( reader.load( "elf_examples/hello_64", is_lazy ), true ); 31665042b18Sopenharmony_ci 31765042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 31865042b18Sopenharmony_ci // Check ELF header 31965042b18Sopenharmony_ci checkHeader( reader, ELFCLASS64, ELFDATA2LSB, EV_CURRENT, ET_EXEC, 32065042b18Sopenharmony_ci EM_X86_64, 1, 0x4003c0, 0, 29, 8, 0, 0 ); 32165042b18Sopenharmony_ci 32265042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 32365042b18Sopenharmony_ci // Check sections 32465042b18Sopenharmony_ci const section* sec = reader.sections[0]; 32565042b18Sopenharmony_ci 32665042b18Sopenharmony_ci checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 ); 32765042b18Sopenharmony_ci 32865042b18Sopenharmony_ci sec = reader.sections[1]; 32965042b18Sopenharmony_ci 33065042b18Sopenharmony_ci checkSection( sec, 1, ".interp", SHT_PROGBITS, SHF_ALLOC, 33165042b18Sopenharmony_ci 0x0000000000400200, 0x1c, 0, 0, 1, 0 ); 33265042b18Sopenharmony_ci 33365042b18Sopenharmony_ci sec = reader.sections[9]; 33465042b18Sopenharmony_ci 33565042b18Sopenharmony_ci checkSection( sec, 9, ".rela.plt", SHT_RELA, SHF_ALLOC, 33665042b18Sopenharmony_ci 0x0000000000400340, 0x30, 4, 11, 8, 0x18 ); 33765042b18Sopenharmony_ci 33865042b18Sopenharmony_ci sec = reader.sections[20]; 33965042b18Sopenharmony_ci 34065042b18Sopenharmony_ci checkSection( sec, 20, ".dynamic", SHT_DYNAMIC, SHF_WRITE | SHF_ALLOC, 34165042b18Sopenharmony_ci 0x0000000000600698, 0x190, 5, 0, 8, 0x10 ); 34265042b18Sopenharmony_ci 34365042b18Sopenharmony_ci sec = reader.sections[28]; 34465042b18Sopenharmony_ci 34565042b18Sopenharmony_ci checkSection( sec, 28, ".strtab", SHT_STRTAB, 0, 0x0, 0x23f, 0, 0, 1, 34665042b18Sopenharmony_ci 0 ); 34765042b18Sopenharmony_ci 34865042b18Sopenharmony_ci const section* sec1 = reader.sections[".strtab"]; 34965042b18Sopenharmony_ci EXPECT_EQ( sec->get_index(), sec1->get_index() ); 35065042b18Sopenharmony_ci 35165042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 35265042b18Sopenharmony_ci // Check segments 35365042b18Sopenharmony_ci const segment* seg = reader.segments[0]; 35465042b18Sopenharmony_ci checkSegment( seg, PT_PHDR, 0x0000000000400040, 0x0000000000400040, 35565042b18Sopenharmony_ci 0x00000000000001c0, 0x00000000000001c0, PF_R + PF_X, 8 ); 35665042b18Sopenharmony_ci 35765042b18Sopenharmony_ci seg = reader.segments[2]; 35865042b18Sopenharmony_ci checkSegment( seg, PT_LOAD, 0x0000000000400000, 0x0000000000400000, 35965042b18Sopenharmony_ci 0x000000000000066c, 0x000000000000066c, PF_R + PF_X, 36065042b18Sopenharmony_ci 0x200000 ); 36165042b18Sopenharmony_ci 36265042b18Sopenharmony_ci seg = reader.segments[7]; 36365042b18Sopenharmony_ci checkSegment( seg, 0x6474E551, 0x0, 0x0, 0x0, 0x0, PF_R + PF_W, 8 ); 36465042b18Sopenharmony_ci 36565042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 36665042b18Sopenharmony_ci // Check symbol table 36765042b18Sopenharmony_ci sec = reader.sections[".symtab"]; 36865042b18Sopenharmony_ci 36965042b18Sopenharmony_ci const_symbol_section_accessor sr( reader, sec ); 37065042b18Sopenharmony_ci 37165042b18Sopenharmony_ci EXPECT_EQ( sr.get_symbols_num(), 67 ); 37265042b18Sopenharmony_ci checkSymbol( sr, 0, "", 0x00000000, 0, STB_LOCAL, STT_NOTYPE, STN_UNDEF, 37365042b18Sopenharmony_ci ELF_ST_VISIBILITY( STV_DEFAULT ) ); 37465042b18Sopenharmony_ci checkSymbol( sr, 1, "", 0x00400200, 0, STB_LOCAL, STT_SECTION, 1, 37565042b18Sopenharmony_ci ELF_ST_VISIBILITY( STV_DEFAULT ) ); 37665042b18Sopenharmony_ci checkSymbol( sr, 40, "hello.c", 0x00000000, 0, STB_LOCAL, STT_FILE, 37765042b18Sopenharmony_ci SHN_ABS, ELF_ST_VISIBILITY( STV_DEFAULT ) ); 37865042b18Sopenharmony_ci checkSymbol( sr, 52, "__gmon_start__", 0x00000000, 0, STB_WEAK, 37965042b18Sopenharmony_ci STT_NOTYPE, STN_UNDEF, ELF_ST_VISIBILITY( STV_DEFAULT ) ); 38065042b18Sopenharmony_ci checkSymbol( sr, 64, "_edata", 0x0060085c, 0, STB_GLOBAL, STT_NOTYPE, 38165042b18Sopenharmony_ci SHN_ABS, ELF_ST_VISIBILITY( STV_DEFAULT ) ); 38265042b18Sopenharmony_ci checkSymbol( sr, 65, "main", 0x00400498, 21, STB_GLOBAL, STT_FUNC, 12, 38365042b18Sopenharmony_ci ELF_ST_VISIBILITY( STV_DEFAULT ) ); 38465042b18Sopenharmony_ci checkSymbol( sr, 66, "_init", 0x00400370, 0, STB_GLOBAL, STT_FUNC, 10, 38565042b18Sopenharmony_ci ELF_ST_VISIBILITY( STV_DEFAULT ) ); 38665042b18Sopenharmony_ci 38765042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 38865042b18Sopenharmony_ci // Check relocation table 38965042b18Sopenharmony_ci sec = reader.sections[".rela.dyn"]; 39065042b18Sopenharmony_ci 39165042b18Sopenharmony_ci const_relocation_section_accessor reloc( reader, sec ); 39265042b18Sopenharmony_ci EXPECT_EQ( reloc.get_entries_num(), 1 ); 39365042b18Sopenharmony_ci 39465042b18Sopenharmony_ci checkRelocation( &reloc, 0, 0x00600828, 0x0, "__gmon_start__", 39565042b18Sopenharmony_ci R_X86_64_GLOB_DAT, 0, 0 ); 39665042b18Sopenharmony_ci 39765042b18Sopenharmony_ci sec = reader.sections[".rela.plt"]; 39865042b18Sopenharmony_ci 39965042b18Sopenharmony_ci const_relocation_section_accessor reloc1( reader, sec ); 40065042b18Sopenharmony_ci EXPECT_EQ( reloc1.get_entries_num(), 2 ); 40165042b18Sopenharmony_ci 40265042b18Sopenharmony_ci checkRelocation( &reloc1, 0, 0x00600848, 0x0, "puts", 40365042b18Sopenharmony_ci R_X86_64_JUMP_SLOT, 0, 0 ); 40465042b18Sopenharmony_ci checkRelocation( &reloc1, 1, 0x00600850, 0x0, "__libc_start_main", 40565042b18Sopenharmony_ci R_X86_64_JUMP_SLOT, 0, 0 ); 40665042b18Sopenharmony_ci 40765042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 40865042b18Sopenharmony_ci // Check note reader 40965042b18Sopenharmony_ci sec = reader.sections[".note.ABI-tag"]; 41065042b18Sopenharmony_ci 41165042b18Sopenharmony_ci const_note_section_accessor notes( reader, sec ); 41265042b18Sopenharmony_ci EXPECT_EQ( notes.get_notes_num(), 1u ); 41365042b18Sopenharmony_ci 41465042b18Sopenharmony_ci checkNote( notes, 0, 1, std::string( "GNU" ), 16 ); 41565042b18Sopenharmony_ci } while ( is_lazy ); 41665042b18Sopenharmony_ci} 41765042b18Sopenharmony_ci 41865042b18Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 41965042b18Sopenharmony_ciTEST( ELFIOTest, hello_64_o ) 42065042b18Sopenharmony_ci{ 42165042b18Sopenharmony_ci elfio reader; 42265042b18Sopenharmony_ci 42365042b18Sopenharmony_ci ASSERT_EQ( reader.load( "elf_examples/hello_64.o" ), true ); 42465042b18Sopenharmony_ci 42565042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 42665042b18Sopenharmony_ci // Check ELF header 42765042b18Sopenharmony_ci checkHeader( reader, ELFCLASS64, ELFDATA2LSB, EV_CURRENT, ET_REL, EM_X86_64, 42865042b18Sopenharmony_ci 1, 0, 0, 13, 0, 0, 0 ); 42965042b18Sopenharmony_ci 43065042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 43165042b18Sopenharmony_ci // Check sections 43265042b18Sopenharmony_ci const section* sec = reader.sections[0]; 43365042b18Sopenharmony_ci 43465042b18Sopenharmony_ci checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 ); 43565042b18Sopenharmony_ci 43665042b18Sopenharmony_ci sec = reader.sections[1]; 43765042b18Sopenharmony_ci 43865042b18Sopenharmony_ci checkSection( sec, 1, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, 0x0, 43965042b18Sopenharmony_ci 0x15, 0, 0, 4, 0 ); 44065042b18Sopenharmony_ci 44165042b18Sopenharmony_ci const section* sec1 = reader.sections[".text"]; 44265042b18Sopenharmony_ci EXPECT_EQ( sec->get_index(), sec1->get_index() ); 44365042b18Sopenharmony_ci 44465042b18Sopenharmony_ci sec = reader.sections[12]; 44565042b18Sopenharmony_ci checkSection( sec, 12, ".strtab", SHT_STRTAB, 0, 0x0, 0x13, 0, 0, 1, 0 ); 44665042b18Sopenharmony_ci 44765042b18Sopenharmony_ci sec1 = reader.sections[".strtab"]; 44865042b18Sopenharmony_ci EXPECT_EQ( sec->get_index(), sec1->get_index() ); 44965042b18Sopenharmony_ci 45065042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 45165042b18Sopenharmony_ci // Check symbol table 45265042b18Sopenharmony_ci sec = reader.sections[".symtab"]; 45365042b18Sopenharmony_ci 45465042b18Sopenharmony_ci const_symbol_section_accessor sr( reader, sec ); 45565042b18Sopenharmony_ci 45665042b18Sopenharmony_ci EXPECT_EQ( sr.get_symbols_num(), 11 ); 45765042b18Sopenharmony_ci checkSymbol( sr, 9, "main", 0x00000000, 21, STB_GLOBAL, STT_FUNC, 1, 45865042b18Sopenharmony_ci ELF_ST_VISIBILITY( STV_DEFAULT ) ); 45965042b18Sopenharmony_ci 46065042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 46165042b18Sopenharmony_ci // Check relocation table 46265042b18Sopenharmony_ci sec = reader.sections[".rela.text"]; 46365042b18Sopenharmony_ci 46465042b18Sopenharmony_ci const_relocation_section_accessor reloc( reader, sec ); 46565042b18Sopenharmony_ci EXPECT_EQ( reloc.get_entries_num(), 2 ); 46665042b18Sopenharmony_ci 46765042b18Sopenharmony_ci checkRelocation( &reloc, 0, 0x00000005, 0x0, "", R_X86_64_32, 0, 0 ); 46865042b18Sopenharmony_ci checkRelocation( &reloc, 1, 0x0000000A, 0x0, "puts", R_X86_64_PC32, 46965042b18Sopenharmony_ci 0xfffffffffffffffcULL, -14 ); 47065042b18Sopenharmony_ci 47165042b18Sopenharmony_ci sec = reader.sections[".rela.eh_frame"]; 47265042b18Sopenharmony_ci 47365042b18Sopenharmony_ci const_relocation_section_accessor reloc1( reader, sec ); 47465042b18Sopenharmony_ci EXPECT_EQ( reloc1.get_entries_num(), 1 ); 47565042b18Sopenharmony_ci 47665042b18Sopenharmony_ci checkRelocation( &reloc1, 0, 0x00000020, 0x0, "", R_X86_64_32, 0, 0 ); 47765042b18Sopenharmony_ci} 47865042b18Sopenharmony_ci 47965042b18Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 48065042b18Sopenharmony_ciTEST( ELFIOTest, hello_32_o ) 48165042b18Sopenharmony_ci{ 48265042b18Sopenharmony_ci elfio reader; 48365042b18Sopenharmony_ci 48465042b18Sopenharmony_ci ASSERT_EQ( reader.load( "elf_examples/hello_32.o" ), true ); 48565042b18Sopenharmony_ci 48665042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 48765042b18Sopenharmony_ci // Check ELF header 48865042b18Sopenharmony_ci checkHeader( reader, ELFCLASS32, ELFDATA2LSB, EV_CURRENT, ET_REL, EM_386, 1, 48965042b18Sopenharmony_ci 0, 0, 11, 0, 0, 0 ); 49065042b18Sopenharmony_ci 49165042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 49265042b18Sopenharmony_ci // Check sections 49365042b18Sopenharmony_ci const section* sec = reader.sections[0]; 49465042b18Sopenharmony_ci 49565042b18Sopenharmony_ci checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 ); 49665042b18Sopenharmony_ci 49765042b18Sopenharmony_ci sec = reader.sections[1]; 49865042b18Sopenharmony_ci 49965042b18Sopenharmony_ci checkSection( sec, 1, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, 0x0, 50065042b18Sopenharmony_ci 0x2b, 0, 0, 4, 0 ); 50165042b18Sopenharmony_ci 50265042b18Sopenharmony_ci const section* sec1 = reader.sections[".text"]; 50365042b18Sopenharmony_ci EXPECT_EQ( sec->get_index(), sec1->get_index() ); 50465042b18Sopenharmony_ci 50565042b18Sopenharmony_ci sec = reader.sections[10]; 50665042b18Sopenharmony_ci 50765042b18Sopenharmony_ci checkSection( sec, 10, ".strtab", SHT_STRTAB, 0, 0x0, 0x13, 0, 0, 1, 0 ); 50865042b18Sopenharmony_ci 50965042b18Sopenharmony_ci sec1 = reader.sections[".strtab"]; 51065042b18Sopenharmony_ci EXPECT_EQ( sec->get_index(), sec1->get_index() ); 51165042b18Sopenharmony_ci 51265042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 51365042b18Sopenharmony_ci // Check symbol table 51465042b18Sopenharmony_ci sec = reader.sections[".symtab"]; 51565042b18Sopenharmony_ci 51665042b18Sopenharmony_ci const_symbol_section_accessor sr( reader, sec ); 51765042b18Sopenharmony_ci 51865042b18Sopenharmony_ci EXPECT_EQ( sr.get_symbols_num(), 10 ); 51965042b18Sopenharmony_ci checkSymbol( sr, 8, "main", 0x00000000, 43, STB_GLOBAL, STT_FUNC, 1, 52065042b18Sopenharmony_ci ELF_ST_VISIBILITY( STV_DEFAULT ) ); 52165042b18Sopenharmony_ci 52265042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 52365042b18Sopenharmony_ci // Check relocation table 52465042b18Sopenharmony_ci sec = reader.sections[".rel.text"]; 52565042b18Sopenharmony_ci 52665042b18Sopenharmony_ci const_relocation_section_accessor reloc( reader, sec ); 52765042b18Sopenharmony_ci EXPECT_EQ( reloc.get_entries_num(), 2 ); 52865042b18Sopenharmony_ci 52965042b18Sopenharmony_ci checkRelocation( &reloc, 0, 0x00000014, 0x0, "", R_386_32, 0, 0 ); 53065042b18Sopenharmony_ci checkRelocation( &reloc, 1, 0x00000019, 0x0, "puts", R_386_PC32, 0x0, -25 ); 53165042b18Sopenharmony_ci} 53265042b18Sopenharmony_ci 53365042b18Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 53465042b18Sopenharmony_ciTEST( ELFIOTest, test_ppc_o ) 53565042b18Sopenharmony_ci{ 53665042b18Sopenharmony_ci elfio reader; 53765042b18Sopenharmony_ci 53865042b18Sopenharmony_ci ASSERT_EQ( reader.load( "elf_examples/test_ppc.o" ), true ); 53965042b18Sopenharmony_ci 54065042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 54165042b18Sopenharmony_ci // Check ELF header 54265042b18Sopenharmony_ci checkHeader( reader, ELFCLASS32, ELFDATA2MSB, EV_CURRENT, ET_REL, EM_PPC, 1, 54365042b18Sopenharmony_ci 0, 0, 16, 0, 0, 0 ); 54465042b18Sopenharmony_ci 54565042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 54665042b18Sopenharmony_ci // Check sections 54765042b18Sopenharmony_ci const section* sec = reader.sections[0]; 54865042b18Sopenharmony_ci 54965042b18Sopenharmony_ci checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 ); 55065042b18Sopenharmony_ci 55165042b18Sopenharmony_ci sec = reader.sections[1]; 55265042b18Sopenharmony_ci 55365042b18Sopenharmony_ci checkSection( sec, 1, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, 0x0, 55465042b18Sopenharmony_ci 0x118, 0, 0, 4, 0 ); 55565042b18Sopenharmony_ci 55665042b18Sopenharmony_ci const section* sec1 = reader.sections[".text"]; 55765042b18Sopenharmony_ci EXPECT_EQ( sec->get_index(), sec1->get_index() ); 55865042b18Sopenharmony_ci 55965042b18Sopenharmony_ci sec = reader.sections[15]; 56065042b18Sopenharmony_ci 56165042b18Sopenharmony_ci checkSection( sec, 15, ".strtab", SHT_STRTAB, 0, 0x0, 0x14f, 0, 0, 1, 0 ); 56265042b18Sopenharmony_ci 56365042b18Sopenharmony_ci sec1 = reader.sections[".strtab"]; 56465042b18Sopenharmony_ci EXPECT_EQ( sec->get_index(), sec1->get_index() ); 56565042b18Sopenharmony_ci 56665042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 56765042b18Sopenharmony_ci // Check symbol table 56865042b18Sopenharmony_ci sec = reader.sections[".symtab"]; 56965042b18Sopenharmony_ci 57065042b18Sopenharmony_ci const_symbol_section_accessor sr( reader, sec ); 57165042b18Sopenharmony_ci 57265042b18Sopenharmony_ci EXPECT_EQ( sr.get_symbols_num(), 24 ); 57365042b18Sopenharmony_ci checkSymbol( sr, 14, "main", 0x00000000, 92, STB_GLOBAL, STT_FUNC, 1, 57465042b18Sopenharmony_ci ELF_ST_VISIBILITY( STV_DEFAULT ) ); 57565042b18Sopenharmony_ci checkSymbol( sr, 8, "_GLOBAL__I_main", 0x000000DC, 60, STB_LOCAL, STT_FUNC, 57665042b18Sopenharmony_ci 1, ELF_ST_VISIBILITY( STV_DEFAULT ) ); 57765042b18Sopenharmony_ci 57865042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 57965042b18Sopenharmony_ci // Check relocation table 58065042b18Sopenharmony_ci sec = reader.sections[".rela.text"]; 58165042b18Sopenharmony_ci 58265042b18Sopenharmony_ci const_relocation_section_accessor reloc( reader, sec ); 58365042b18Sopenharmony_ci EXPECT_EQ( reloc.get_entries_num(), 18 ); 58465042b18Sopenharmony_ci 58565042b18Sopenharmony_ci checkRelocation( &reloc, 0, 0x00000016, 0x0, "_ZSt4cout", 6, 0, 0 ); 58665042b18Sopenharmony_ci checkRelocation( &reloc, 1, 0x0000001a, 0x0, "_ZSt4cout", 4, 0x0, 0 ); 58765042b18Sopenharmony_ci checkRelocation( &reloc, 17, 0x000000c0, 0x0, "__cxa_atexit", 10, 0x0, 0 ); 58865042b18Sopenharmony_ci 58965042b18Sopenharmony_ci sec = reader.sections[".rela.ctors"]; 59065042b18Sopenharmony_ci 59165042b18Sopenharmony_ci const_relocation_section_accessor reloc1( reader, sec ); 59265042b18Sopenharmony_ci EXPECT_EQ( reloc1.get_entries_num(), 1 ); 59365042b18Sopenharmony_ci 59465042b18Sopenharmony_ci checkRelocation( &reloc1, 0, 0x00000000, 0x0, "", 1, 0xDC, 0xDC ); 59565042b18Sopenharmony_ci 59665042b18Sopenharmony_ci sec = reader.sections[".rela.eh_frame"]; 59765042b18Sopenharmony_ci 59865042b18Sopenharmony_ci const_relocation_section_accessor reloc2( reader, sec ); 59965042b18Sopenharmony_ci EXPECT_EQ( reloc2.get_entries_num(), 3 ); 60065042b18Sopenharmony_ci 60165042b18Sopenharmony_ci checkRelocation( &reloc2, 1, 0x00000020, 0x0, "", 1, 0x0, 0x0 ); 60265042b18Sopenharmony_ci} 60365042b18Sopenharmony_ci 60465042b18Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 60565042b18Sopenharmony_ciTEST( ELFIOTest, test_ppc ) 60665042b18Sopenharmony_ci{ 60765042b18Sopenharmony_ci elfio reader; 60865042b18Sopenharmony_ci 60965042b18Sopenharmony_ci ASSERT_EQ( reader.load( "elf_examples/test_ppc" ), true ); 61065042b18Sopenharmony_ci 61165042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 61265042b18Sopenharmony_ci // Check ELF header 61365042b18Sopenharmony_ci checkHeader( reader, ELFCLASS32, ELFDATA2MSB, EV_CURRENT, ET_EXEC, EM_PPC, 61465042b18Sopenharmony_ci 1, 0x10000550, 0, 31, 8, 0, 0 ); 61565042b18Sopenharmony_ci 61665042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 61765042b18Sopenharmony_ci // Check sections 61865042b18Sopenharmony_ci const section* sec = reader.sections[0]; 61965042b18Sopenharmony_ci 62065042b18Sopenharmony_ci checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 ); 62165042b18Sopenharmony_ci 62265042b18Sopenharmony_ci sec = reader.sections[1]; 62365042b18Sopenharmony_ci 62465042b18Sopenharmony_ci checkSection( sec, 1, ".interp", SHT_PROGBITS, SHF_ALLOC, 62565042b18Sopenharmony_ci 0x0000000010000134, 0xd, 0, 0, 1, 0 ); 62665042b18Sopenharmony_ci 62765042b18Sopenharmony_ci sec = reader.sections[9]; 62865042b18Sopenharmony_ci 62965042b18Sopenharmony_ci checkSection( sec, 9, ".rela.plt", SHT_RELA, SHF_ALLOC, 0x00000000010000494, 63065042b18Sopenharmony_ci 0x6c, 4, 22, 4, 0xc ); 63165042b18Sopenharmony_ci 63265042b18Sopenharmony_ci sec = reader.sections[20]; 63365042b18Sopenharmony_ci 63465042b18Sopenharmony_ci checkSection( sec, 20, ".dynamic", SHT_DYNAMIC, SHF_WRITE | SHF_ALLOC, 63565042b18Sopenharmony_ci 0x0000000010010aec, 0xe8, 5, 0, 4, 0x8 ); 63665042b18Sopenharmony_ci 63765042b18Sopenharmony_ci sec = reader.sections[28]; 63865042b18Sopenharmony_ci 63965042b18Sopenharmony_ci checkSection( sec, 28, ".shstrtab", SHT_STRTAB, 0, 0x0, 0x101, 0, 0, 1, 0 ); 64065042b18Sopenharmony_ci 64165042b18Sopenharmony_ci const section* sec1 = reader.sections[".shstrtab"]; 64265042b18Sopenharmony_ci EXPECT_EQ( sec->get_index(), sec1->get_index() ); 64365042b18Sopenharmony_ci 64465042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 64565042b18Sopenharmony_ci // Check segments 64665042b18Sopenharmony_ci const segment* seg = reader.segments[0]; 64765042b18Sopenharmony_ci checkSegment( seg, PT_PHDR, 0x10000034, 0x10000034, 0x00100, 0x00100, 64865042b18Sopenharmony_ci PF_R + PF_X, 4 ); 64965042b18Sopenharmony_ci 65065042b18Sopenharmony_ci seg = reader.segments[2]; 65165042b18Sopenharmony_ci checkSegment( seg, PT_LOAD, 0x10000000, 0x10000000, 0x00acc, 0x00acc, 65265042b18Sopenharmony_ci PF_R + PF_X, 0x10000 ); 65365042b18Sopenharmony_ci 65465042b18Sopenharmony_ci seg = reader.segments[7]; 65565042b18Sopenharmony_ci checkSegment( seg, 0x6474E551, 0x0, 0x0, 0x0, 0x0, PF_R + PF_W, 0x4 ); 65665042b18Sopenharmony_ci 65765042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 65865042b18Sopenharmony_ci // Check symbol table 65965042b18Sopenharmony_ci sec = reader.sections[".symtab"]; 66065042b18Sopenharmony_ci 66165042b18Sopenharmony_ci const_symbol_section_accessor sr( reader, sec ); 66265042b18Sopenharmony_ci 66365042b18Sopenharmony_ci EXPECT_EQ( sr.get_symbols_num(), 80 ); 66465042b18Sopenharmony_ci checkSymbol( sr, 0, "", 0x00000000, 0, STB_LOCAL, STT_NOTYPE, STN_UNDEF, 66565042b18Sopenharmony_ci ELF_ST_VISIBILITY( STV_DEFAULT ) ); 66665042b18Sopenharmony_ci checkSymbol( sr, 1, "", 0x10000134, 0, STB_LOCAL, STT_SECTION, 1, 66765042b18Sopenharmony_ci ELF_ST_VISIBILITY( STV_DEFAULT ) ); 66865042b18Sopenharmony_ci checkSymbol( sr, 40, "__CTOR_END__", 0x10010AD4, 0, STB_LOCAL, STT_OBJECT, 66965042b18Sopenharmony_ci 16, ELF_ST_VISIBILITY( STV_DEFAULT ) ); 67065042b18Sopenharmony_ci checkSymbol( sr, 52, "__init_array_start", 0x10010acc, 0, STB_LOCAL, 67165042b18Sopenharmony_ci STT_NOTYPE, 16, ELF_ST_VISIBILITY( STV_HIDDEN ) ); 67265042b18Sopenharmony_ci checkSymbol( sr, 64, "_ZNSt8ios_base4InitD1Ev@@GLIBCXX_3.4", 0x10000920, 67365042b18Sopenharmony_ci 204, STB_GLOBAL, STT_FUNC, SHN_UNDEF, 67465042b18Sopenharmony_ci ELF_ST_VISIBILITY( STV_DEFAULT ) ); 67565042b18Sopenharmony_ci checkSymbol( sr, 78, "main", 0x1000069c, 92, STB_GLOBAL, STT_FUNC, 11, 67665042b18Sopenharmony_ci ELF_ST_VISIBILITY( STV_DEFAULT ) ); 67765042b18Sopenharmony_ci checkSymbol( sr, 79, "_init", 0x10000500, 0, STB_GLOBAL, STT_FUNC, 10, 67865042b18Sopenharmony_ci ELF_ST_VISIBILITY( STV_DEFAULT ) ); 67965042b18Sopenharmony_ci 68065042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 68165042b18Sopenharmony_ci // Check relocation table 68265042b18Sopenharmony_ci sec = reader.sections[".rela.dyn"]; 68365042b18Sopenharmony_ci 68465042b18Sopenharmony_ci const_relocation_section_accessor reloc( reader, sec ); 68565042b18Sopenharmony_ci EXPECT_EQ( reloc.get_entries_num(), 2 ); 68665042b18Sopenharmony_ci 68765042b18Sopenharmony_ci checkRelocation( &reloc, 1, 0x10010c0c, 0x10010c0c, "_ZSt4cout", 19, 0, 0 ); 68865042b18Sopenharmony_ci 68965042b18Sopenharmony_ci sec = reader.sections[".rela.plt"]; 69065042b18Sopenharmony_ci 69165042b18Sopenharmony_ci const_relocation_section_accessor reloc1( reader, sec ); 69265042b18Sopenharmony_ci EXPECT_EQ( reloc1.get_entries_num(), 9 ); 69365042b18Sopenharmony_ci 69465042b18Sopenharmony_ci checkRelocation( &reloc1, 0, 0x10010be4, 0x100008e0, "__cxa_atexit", 21, 0, 69565042b18Sopenharmony_ci 0 ); 69665042b18Sopenharmony_ci checkRelocation( &reloc1, 1, 0x10010be8, 0x0, "__gmon_start__", 21, 0, 0 ); 69765042b18Sopenharmony_ci 69865042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 69965042b18Sopenharmony_ci // Check note reader 70065042b18Sopenharmony_ci sec = reader.sections[".note.ABI-tag"]; 70165042b18Sopenharmony_ci 70265042b18Sopenharmony_ci const_note_section_accessor notes( reader, sec ); 70365042b18Sopenharmony_ci EXPECT_EQ( notes.get_notes_num(), 1u ); 70465042b18Sopenharmony_ci 70565042b18Sopenharmony_ci checkNote( notes, 0, 1, std::string( "GNU" ), 16 ); 70665042b18Sopenharmony_ci} 70765042b18Sopenharmony_ci 70865042b18Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 70965042b18Sopenharmony_ciTEST( ELFIOTest, test_dummy_out_i386_32 ) 71065042b18Sopenharmony_ci{ 71165042b18Sopenharmony_ci elfio writer; 71265042b18Sopenharmony_ci 71365042b18Sopenharmony_ci writer.create( ELFCLASS32, ELFDATA2LSB ); 71465042b18Sopenharmony_ci 71565042b18Sopenharmony_ci writer.set_os_abi( 0 ); 71665042b18Sopenharmony_ci writer.set_abi_version( 0 ); 71765042b18Sopenharmony_ci writer.set_type( ET_REL ); 71865042b18Sopenharmony_ci writer.set_machine( EM_386 ); 71965042b18Sopenharmony_ci writer.set_flags( 0 ); 72065042b18Sopenharmony_ci 72165042b18Sopenharmony_ci // Set program entry point 72265042b18Sopenharmony_ci writer.set_entry( 0x80482b0 ); 72365042b18Sopenharmony_ci 72465042b18Sopenharmony_ci // Add Note section 72565042b18Sopenharmony_ci section* note_sec = writer.sections.add( ".note" ); 72665042b18Sopenharmony_ci note_sec->set_type( SHT_NOTE ); 72765042b18Sopenharmony_ci note_sec->set_flags( SHF_ALLOC ); 72865042b18Sopenharmony_ci note_sec->set_addr_align( 4 ); 72965042b18Sopenharmony_ci note_section_accessor note_writer( writer, note_sec ); 73065042b18Sopenharmony_ci char descr[6] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; 73165042b18Sopenharmony_ci note_writer.add_note( 0x77, "Hello", descr, 6 ); 73265042b18Sopenharmony_ci EXPECT_EQ( note_sec->get_index(), 2 ); 73365042b18Sopenharmony_ci 73465042b18Sopenharmony_ci // Create ELF file 73565042b18Sopenharmony_ci writer.save( "elf_examples/elf_dummy_header_i386_32.elf" ); 73665042b18Sopenharmony_ci 73765042b18Sopenharmony_ci elfio reader; 73865042b18Sopenharmony_ci ASSERT_EQ( reader.load( "elf_examples/elf_dummy_header_i386_32.elf" ), 73965042b18Sopenharmony_ci true ); 74065042b18Sopenharmony_ci 74165042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 74265042b18Sopenharmony_ci // Check ELF header 74365042b18Sopenharmony_ci checkHeader( reader, ELFCLASS32, ELFDATA2LSB, EV_CURRENT, ET_REL, EM_386, 74465042b18Sopenharmony_ci EV_CURRENT, 0x80482b0, 0, 3, 0, 0, 0 ); 74565042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 74665042b18Sopenharmony_ci // Check sections 74765042b18Sopenharmony_ci const section* sec = reader.sections[""]; 74865042b18Sopenharmony_ci 74965042b18Sopenharmony_ci checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 ); 75065042b18Sopenharmony_ci 75165042b18Sopenharmony_ci sec = reader.sections[".shstrtab"]; 75265042b18Sopenharmony_ci 75365042b18Sopenharmony_ci checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0, 0, 17, 0, 0, 1, 0 ); 75465042b18Sopenharmony_ci 75565042b18Sopenharmony_ci sec = reader.sections[".note"]; 75665042b18Sopenharmony_ci 75765042b18Sopenharmony_ci EXPECT_EQ( sec->get_index(), 2 ); 75865042b18Sopenharmony_ci checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC, 0, 28, 0, 0, 4, 0 ); 75965042b18Sopenharmony_ci} 76065042b18Sopenharmony_ci 76165042b18Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 76265042b18Sopenharmony_ciTEST( ELFIOTest, test_dummy_out_ppc_32 ) 76365042b18Sopenharmony_ci{ 76465042b18Sopenharmony_ci elfio writer; 76565042b18Sopenharmony_ci 76665042b18Sopenharmony_ci writer.create( ELFCLASS32, ELFDATA2MSB ); 76765042b18Sopenharmony_ci 76865042b18Sopenharmony_ci writer.set_os_abi( 0 ); 76965042b18Sopenharmony_ci writer.set_abi_version( 0 ); 77065042b18Sopenharmony_ci writer.set_type( ET_REL ); 77165042b18Sopenharmony_ci writer.set_machine( EM_PPC ); 77265042b18Sopenharmony_ci writer.set_flags( 0 ); 77365042b18Sopenharmony_ci 77465042b18Sopenharmony_ci // Set program entry point 77565042b18Sopenharmony_ci writer.set_entry( 0x80482b0 ); 77665042b18Sopenharmony_ci 77765042b18Sopenharmony_ci // Add Note section 77865042b18Sopenharmony_ci section* note_sec = writer.sections.add( ".note" ); 77965042b18Sopenharmony_ci note_sec->set_type( SHT_NOTE ); 78065042b18Sopenharmony_ci note_sec->set_flags( SHF_ALLOC ); 78165042b18Sopenharmony_ci note_sec->set_addr_align( 4 ); 78265042b18Sopenharmony_ci note_section_accessor note_writer( writer, note_sec ); 78365042b18Sopenharmony_ci char descr[6] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; 78465042b18Sopenharmony_ci note_writer.add_note( 0x77, "Hello", descr, 6 ); 78565042b18Sopenharmony_ci EXPECT_EQ( note_sec->get_index(), 2 ); 78665042b18Sopenharmony_ci 78765042b18Sopenharmony_ci // Create ELF file 78865042b18Sopenharmony_ci writer.save( "elf_examples/elf_dummy_header_ppc_32.elf" ); 78965042b18Sopenharmony_ci 79065042b18Sopenharmony_ci elfio reader; 79165042b18Sopenharmony_ci ASSERT_EQ( reader.load( "elf_examples/elf_dummy_header_ppc_32.elf" ), 79265042b18Sopenharmony_ci true ); 79365042b18Sopenharmony_ci 79465042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 79565042b18Sopenharmony_ci // Check ELF header 79665042b18Sopenharmony_ci checkHeader( reader, ELFCLASS32, ELFDATA2MSB, EV_CURRENT, ET_REL, EM_PPC, 79765042b18Sopenharmony_ci EV_CURRENT, 0x80482b0, 0, 3, 0, 0, 0 ); 79865042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 79965042b18Sopenharmony_ci // Check sections 80065042b18Sopenharmony_ci const section* sec = reader.sections[""]; 80165042b18Sopenharmony_ci 80265042b18Sopenharmony_ci checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 ); 80365042b18Sopenharmony_ci 80465042b18Sopenharmony_ci sec = reader.sections[".note"]; 80565042b18Sopenharmony_ci 80665042b18Sopenharmony_ci EXPECT_EQ( sec->get_index(), 2 ); 80765042b18Sopenharmony_ci checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC, 0, 28, 0, 0, 4, 0 ); 80865042b18Sopenharmony_ci 80965042b18Sopenharmony_ci sec = reader.sections[".shstrtab"]; 81065042b18Sopenharmony_ci 81165042b18Sopenharmony_ci checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0, 0, 17, 0, 0, 1, 0 ); 81265042b18Sopenharmony_ci} 81365042b18Sopenharmony_ci 81465042b18Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 81565042b18Sopenharmony_ciTEST( ELFIOTest, test_dummy_out_i386_64 ) 81665042b18Sopenharmony_ci{ 81765042b18Sopenharmony_ci elfio writer; 81865042b18Sopenharmony_ci 81965042b18Sopenharmony_ci writer.create( ELFCLASS64, ELFDATA2LSB ); 82065042b18Sopenharmony_ci 82165042b18Sopenharmony_ci writer.set_os_abi( 0 ); 82265042b18Sopenharmony_ci writer.set_abi_version( 0 ); 82365042b18Sopenharmony_ci writer.set_type( ET_REL ); 82465042b18Sopenharmony_ci writer.set_machine( EM_X86_64 ); 82565042b18Sopenharmony_ci writer.set_flags( 0 ); 82665042b18Sopenharmony_ci 82765042b18Sopenharmony_ci // Set program entry point 82865042b18Sopenharmony_ci writer.set_entry( 0x120380482b0ULL ); 82965042b18Sopenharmony_ci 83065042b18Sopenharmony_ci // Add Note section 83165042b18Sopenharmony_ci section* note_sec = writer.sections.add( ".note" ); 83265042b18Sopenharmony_ci note_sec->set_type( SHT_NOTE ); 83365042b18Sopenharmony_ci note_sec->set_flags( SHF_ALLOC ); 83465042b18Sopenharmony_ci note_sec->set_addr_align( 4 ); 83565042b18Sopenharmony_ci note_section_accessor note_writer( writer, note_sec ); 83665042b18Sopenharmony_ci char descr[6] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; 83765042b18Sopenharmony_ci note_writer.add_note( 0x77, "Hello", descr, 6 ); 83865042b18Sopenharmony_ci EXPECT_EQ( note_sec->get_index(), 2 ); 83965042b18Sopenharmony_ci 84065042b18Sopenharmony_ci // Create ELF file 84165042b18Sopenharmony_ci writer.save( "elf_examples/elf_dummy_header_i386_64.elf" ); 84265042b18Sopenharmony_ci 84365042b18Sopenharmony_ci elfio reader; 84465042b18Sopenharmony_ci ASSERT_EQ( reader.load( "elf_examples/elf_dummy_header_i386_64.elf" ), 84565042b18Sopenharmony_ci true ); 84665042b18Sopenharmony_ci 84765042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 84865042b18Sopenharmony_ci // Check ELF header 84965042b18Sopenharmony_ci checkHeader( reader, ELFCLASS64, ELFDATA2LSB, EV_CURRENT, ET_REL, EM_X86_64, 85065042b18Sopenharmony_ci EV_CURRENT, 0x120380482b0ULL, 0, 3, 0, 0, 0 ); 85165042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 85265042b18Sopenharmony_ci // Check sections 85365042b18Sopenharmony_ci const section* sec = reader.sections[""]; 85465042b18Sopenharmony_ci 85565042b18Sopenharmony_ci checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 ); 85665042b18Sopenharmony_ci 85765042b18Sopenharmony_ci sec = reader.sections[".note"]; 85865042b18Sopenharmony_ci 85965042b18Sopenharmony_ci EXPECT_EQ( sec->get_index(), 2 ); 86065042b18Sopenharmony_ci checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC, 0, 28, 0, 0, 4, 0 ); 86165042b18Sopenharmony_ci 86265042b18Sopenharmony_ci sec = reader.sections[".shstrtab"]; 86365042b18Sopenharmony_ci 86465042b18Sopenharmony_ci checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0, 0, 17, 0, 0, 1, 0 ); 86565042b18Sopenharmony_ci} 86665042b18Sopenharmony_ci 86765042b18Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 86865042b18Sopenharmony_ciTEST( ELFIOTest, test_dummy_out_ppc_64 ) 86965042b18Sopenharmony_ci{ 87065042b18Sopenharmony_ci elfio writer; 87165042b18Sopenharmony_ci 87265042b18Sopenharmony_ci writer.create( ELFCLASS64, ELFDATA2MSB ); 87365042b18Sopenharmony_ci 87465042b18Sopenharmony_ci writer.set_os_abi( 0 ); 87565042b18Sopenharmony_ci writer.set_abi_version( 0 ); 87665042b18Sopenharmony_ci writer.set_type( ET_REL ); 87765042b18Sopenharmony_ci writer.set_machine( EM_PPC64 ); 87865042b18Sopenharmony_ci writer.set_flags( 0 ); 87965042b18Sopenharmony_ci 88065042b18Sopenharmony_ci // Set program entry point 88165042b18Sopenharmony_ci writer.set_entry( 0x120380482b0ULL ); 88265042b18Sopenharmony_ci 88365042b18Sopenharmony_ci // Add Note section 88465042b18Sopenharmony_ci section* note_sec = writer.sections.add( ".note" ); 88565042b18Sopenharmony_ci note_sec->set_type( SHT_NOTE ); 88665042b18Sopenharmony_ci note_sec->set_flags( SHF_ALLOC ); 88765042b18Sopenharmony_ci note_sec->set_addr_align( 4 ); 88865042b18Sopenharmony_ci note_section_accessor note_writer( writer, note_sec ); 88965042b18Sopenharmony_ci char descr[6] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; 89065042b18Sopenharmony_ci note_writer.add_note( 0x77, "Hello", descr, 6 ); 89165042b18Sopenharmony_ci EXPECT_EQ( note_sec->get_index(), 2 ); 89265042b18Sopenharmony_ci 89365042b18Sopenharmony_ci // Create ELF file 89465042b18Sopenharmony_ci writer.save( "elf_examples/elf_dummy_header_ppc_64.elf" ); 89565042b18Sopenharmony_ci 89665042b18Sopenharmony_ci elfio reader; 89765042b18Sopenharmony_ci ASSERT_EQ( reader.load( "elf_examples/elf_dummy_header_ppc_64.elf" ), 89865042b18Sopenharmony_ci true ); 89965042b18Sopenharmony_ci 90065042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 90165042b18Sopenharmony_ci // Check ELF header 90265042b18Sopenharmony_ci checkHeader( reader, ELFCLASS64, ELFDATA2MSB, EV_CURRENT, ET_REL, EM_PPC64, 90365042b18Sopenharmony_ci EV_CURRENT, 0x120380482b0ULL, 0, 3, 0, 0, 0 ); 90465042b18Sopenharmony_ci //////////////////////////////////////////////////////////////////////////// 90565042b18Sopenharmony_ci // Check sections 90665042b18Sopenharmony_ci const section* sec = reader.sections[""]; 90765042b18Sopenharmony_ci 90865042b18Sopenharmony_ci checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 ); 90965042b18Sopenharmony_ci 91065042b18Sopenharmony_ci sec = reader.sections[".shstrtab"]; 91165042b18Sopenharmony_ci 91265042b18Sopenharmony_ci checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0, 0, 17, 0, 0, 1, 0 ); 91365042b18Sopenharmony_ci 91465042b18Sopenharmony_ci sec = reader.sections[".note"]; 91565042b18Sopenharmony_ci 91665042b18Sopenharmony_ci EXPECT_EQ( sec->get_index(), 2 ); 91765042b18Sopenharmony_ci checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC, 0, 28, 0, 0, 4, 0 ); 91865042b18Sopenharmony_ci} 91965042b18Sopenharmony_ci 92065042b18Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 92165042b18Sopenharmony_ciTEST( ELFIOTest, test_dynamic_64_1 ) 92265042b18Sopenharmony_ci{ 92365042b18Sopenharmony_ci elfio reader; 92465042b18Sopenharmony_ci 92565042b18Sopenharmony_ci ASSERT_EQ( reader.load( "elf_examples/main" ), true ); 92665042b18Sopenharmony_ci 92765042b18Sopenharmony_ci section* dynsec = reader.sections[".dynamic"]; 92865042b18Sopenharmony_ci ASSERT_TRUE( dynsec != nullptr ); 92965042b18Sopenharmony_ci 93065042b18Sopenharmony_ci dynamic_section_accessor da( reader, dynsec ); 93165042b18Sopenharmony_ci 93265042b18Sopenharmony_ci EXPECT_EQ( da.get_entries_num(), 21 ); 93365042b18Sopenharmony_ci 93465042b18Sopenharmony_ci Elf_Xword tag; 93565042b18Sopenharmony_ci Elf_Xword value; 93665042b18Sopenharmony_ci std::string str; 93765042b18Sopenharmony_ci da.get_entry( 0, tag, value, str ); 93865042b18Sopenharmony_ci EXPECT_EQ( tag, DT_NEEDED ); 93965042b18Sopenharmony_ci EXPECT_EQ( str, "libfunc.so" ); 94065042b18Sopenharmony_ci da.get_entry( 1, tag, value, str ); 94165042b18Sopenharmony_ci EXPECT_EQ( tag, DT_NEEDED ); 94265042b18Sopenharmony_ci EXPECT_EQ( str, "libc.so.6" ); 94365042b18Sopenharmony_ci da.get_entry( 2, tag, value, str ); 94465042b18Sopenharmony_ci EXPECT_EQ( tag, DT_INIT ); 94565042b18Sopenharmony_ci EXPECT_EQ( value, 0x400530 ); 94665042b18Sopenharmony_ci da.get_entry( 19, tag, value, str ); 94765042b18Sopenharmony_ci EXPECT_EQ( tag, 0x6ffffff0 ); 94865042b18Sopenharmony_ci EXPECT_EQ( value, 0x40047e ); 94965042b18Sopenharmony_ci da.get_entry( 20, tag, value, str ); 95065042b18Sopenharmony_ci EXPECT_EQ( tag, DT_NULL ); 95165042b18Sopenharmony_ci EXPECT_EQ( value, 0 ); 95265042b18Sopenharmony_ci} 95365042b18Sopenharmony_ci 95465042b18Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 95565042b18Sopenharmony_ciTEST( ELFIOTest, test_dynamic_64_2 ) 95665042b18Sopenharmony_ci{ 95765042b18Sopenharmony_ci elfio reader; 95865042b18Sopenharmony_ci 95965042b18Sopenharmony_ci ASSERT_EQ( reader.load( "elf_examples/libfunc.so" ), true ); 96065042b18Sopenharmony_ci 96165042b18Sopenharmony_ci section* dynsec = reader.sections[".dynamic"]; 96265042b18Sopenharmony_ci ASSERT_TRUE( dynsec != nullptr ); 96365042b18Sopenharmony_ci 96465042b18Sopenharmony_ci dynamic_section_accessor da( reader, dynsec ); 96565042b18Sopenharmony_ci 96665042b18Sopenharmony_ci EXPECT_EQ( da.get_entries_num(), 20 ); 96765042b18Sopenharmony_ci 96865042b18Sopenharmony_ci Elf_Xword tag; 96965042b18Sopenharmony_ci Elf_Xword value; 97065042b18Sopenharmony_ci std::string str; 97165042b18Sopenharmony_ci da.get_entry( 0, tag, value, str ); 97265042b18Sopenharmony_ci EXPECT_EQ( tag, DT_NEEDED ); 97365042b18Sopenharmony_ci EXPECT_EQ( str, "libc.so.6" ); 97465042b18Sopenharmony_ci da.get_entry( 1, tag, value, str ); 97565042b18Sopenharmony_ci EXPECT_EQ( tag, DT_INIT ); 97665042b18Sopenharmony_ci EXPECT_EQ( value, 0x480 ); 97765042b18Sopenharmony_ci da.get_entry( 18, tag, value, str ); 97865042b18Sopenharmony_ci EXPECT_EQ( tag, 0x6ffffff9 ); 97965042b18Sopenharmony_ci EXPECT_EQ( value, 1 ); 98065042b18Sopenharmony_ci da.get_entry( 19, tag, value, str ); 98165042b18Sopenharmony_ci EXPECT_EQ( tag, DT_NULL ); 98265042b18Sopenharmony_ci EXPECT_EQ( value, 0 ); 98365042b18Sopenharmony_ci} 98465042b18Sopenharmony_ci 98565042b18Sopenharmony_ciclass mock_wiiu_compression : public compression_interface 98665042b18Sopenharmony_ci{ 98765042b18Sopenharmony_ci public: 98865042b18Sopenharmony_ci std::unique_ptr<char[]> 98965042b18Sopenharmony_ci inflate( const char* data, 99065042b18Sopenharmony_ci const endianess_convertor* convertor, 99165042b18Sopenharmony_ci Elf_Xword compressed_size, 99265042b18Sopenharmony_ci Elf_Xword& uncompressed_size ) const override 99365042b18Sopenharmony_ci { 99465042b18Sopenharmony_ci uncompressed_size = 2 * compressed_size; 99565042b18Sopenharmony_ci return std::unique_ptr<char[]>( 99665042b18Sopenharmony_ci new ( std::nothrow ) char[uncompressed_size + 1] ); 99765042b18Sopenharmony_ci } 99865042b18Sopenharmony_ci 99965042b18Sopenharmony_ci std::unique_ptr<char[]> deflate( const char* data, 100065042b18Sopenharmony_ci const endianess_convertor* convertor, 100165042b18Sopenharmony_ci Elf_Xword decompressed_size, 100265042b18Sopenharmony_ci Elf_Xword& compressed_size ) const override 100365042b18Sopenharmony_ci { 100465042b18Sopenharmony_ci compressed_size = decompressed_size / 2; 100565042b18Sopenharmony_ci return std::unique_ptr<char[]>( 100665042b18Sopenharmony_ci new ( std::nothrow ) char[compressed_size + 1] ); 100765042b18Sopenharmony_ci } 100865042b18Sopenharmony_ci}; 100965042b18Sopenharmony_ci 101065042b18Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 101165042b18Sopenharmony_ci// Given: a valid RPX file 101265042b18Sopenharmony_ci// When: we load it with no compression implementation 101365042b18Sopenharmony_ci// Then: the size returns the raw section size (compressed size) 101465042b18Sopenharmony_ci// When: we load it with a mock compression implementation 101565042b18Sopenharmony_ci// Then: the size changes to reflect the mock compression implementation is being called 101665042b18Sopenharmony_ci// 101765042b18Sopenharmony_ci// This test does not do any further validation because doing so would require providing 101865042b18Sopenharmony_ci// a real compression implementation 101965042b18Sopenharmony_ciTEST( ELFIOTest, test_rpx ) 102065042b18Sopenharmony_ci{ 102165042b18Sopenharmony_ci elfio reader( new ( std::nothrow ) mock_wiiu_compression() ); 102265042b18Sopenharmony_ci elfio reader_no_compression; 102365042b18Sopenharmony_ci 102465042b18Sopenharmony_ci ASSERT_EQ( reader_no_compression.load( "elf_examples/helloworld.rpx" ), 102565042b18Sopenharmony_ci true ); 102665042b18Sopenharmony_ci const section* text1 = reader_no_compression.sections[1]; 102765042b18Sopenharmony_ci EXPECT_EQ( text1->get_size(), 36744 ); 102865042b18Sopenharmony_ci 102965042b18Sopenharmony_ci ASSERT_EQ( reader.load( "elf_examples/helloworld.rpx" ), true ); 103065042b18Sopenharmony_ci const section* text2 = reader.sections[1]; 103165042b18Sopenharmony_ci EXPECT_EQ( text2->get_size(), text1->get_size() * 2 ); 103265042b18Sopenharmony_ci} 103365042b18Sopenharmony_ci 103465042b18Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////// 103565042b18Sopenharmony_ciTEST( ELFIOTest, test_dynamic_64_3 ) 103665042b18Sopenharmony_ci{ 103765042b18Sopenharmony_ci elfio reader; 103865042b18Sopenharmony_ci 103965042b18Sopenharmony_ci ASSERT_EQ( reader.load( "elf_examples/main" ), true ); 104065042b18Sopenharmony_ci 104165042b18Sopenharmony_ci section* dynsec = reader.sections[".dynamic"]; 104265042b18Sopenharmony_ci ASSERT_TRUE( dynsec != nullptr ); 104365042b18Sopenharmony_ci 104465042b18Sopenharmony_ci dynamic_section_accessor da( reader, dynsec ); 104565042b18Sopenharmony_ci EXPECT_EQ( da.get_entries_num(), 21 ); 104665042b18Sopenharmony_ci 104765042b18Sopenharmony_ci section* strsec1 = reader.sections.add( ".dynstr" ); 104865042b18Sopenharmony_ci strsec1->set_type( SHT_STRTAB ); 104965042b18Sopenharmony_ci strsec1->set_entry_size( reader.get_default_entry_size( SHT_STRTAB ) ); 105065042b18Sopenharmony_ci 105165042b18Sopenharmony_ci section* dynsec1 = reader.sections.add( ".dynamic1" ); 105265042b18Sopenharmony_ci dynsec1->set_type( SHT_DYNAMIC ); 105365042b18Sopenharmony_ci dynsec1->set_entry_size( reader.get_default_entry_size( SHT_DYNAMIC ) ); 105465042b18Sopenharmony_ci dynsec1->set_link( strsec1->get_index() ); 105565042b18Sopenharmony_ci dynamic_section_accessor da1( reader, dynsec1 ); 105665042b18Sopenharmony_ci 105765042b18Sopenharmony_ci Elf_Xword tag; 105865042b18Sopenharmony_ci Elf_Xword tag1; 105965042b18Sopenharmony_ci Elf_Xword value; 106065042b18Sopenharmony_ci Elf_Xword value1; 106165042b18Sopenharmony_ci std::string str; 106265042b18Sopenharmony_ci std::string str1; 106365042b18Sopenharmony_ci 106465042b18Sopenharmony_ci for ( unsigned int i = 0; i < da.get_entries_num(); ++i ) { 106565042b18Sopenharmony_ci da.get_entry( i, tag, value, str ); 106665042b18Sopenharmony_ci if ( tag == DT_NEEDED || tag == DT_SONAME || tag == DT_RPATH || 106765042b18Sopenharmony_ci tag == DT_RUNPATH ) { 106865042b18Sopenharmony_ci da1.add_entry( tag, str ); 106965042b18Sopenharmony_ci } 107065042b18Sopenharmony_ci else { 107165042b18Sopenharmony_ci da1.add_entry( tag, value ); 107265042b18Sopenharmony_ci } 107365042b18Sopenharmony_ci } 107465042b18Sopenharmony_ci 107565042b18Sopenharmony_ci for ( unsigned int i = 0; i < da.get_entries_num(); ++i ) { 107665042b18Sopenharmony_ci da.get_entry( i, tag, value, str ); 107765042b18Sopenharmony_ci da1.get_entry( i, tag1, value1, str1 ); 107865042b18Sopenharmony_ci 107965042b18Sopenharmony_ci EXPECT_EQ( tag, tag1 ); 108065042b18Sopenharmony_ci if ( tag == DT_NEEDED || tag == DT_SONAME || tag == DT_RPATH || 108165042b18Sopenharmony_ci tag == DT_RUNPATH ) { 108265042b18Sopenharmony_ci EXPECT_EQ( str, str1 ); 108365042b18Sopenharmony_ci } 108465042b18Sopenharmony_ci else { 108565042b18Sopenharmony_ci EXPECT_EQ( value, value1 ); 108665042b18Sopenharmony_ci } 108765042b18Sopenharmony_ci } 108865042b18Sopenharmony_ci} 1089