bintools/checklib/object/elf_object.cpp
changeset 0 044383f39525
equal deleted inserted replaced
-1:000000000000 0:044383f39525
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "object.h"
       
    17 
       
    18 #include "elf/elf_file_header.h"
       
    19 #include "elf/elf_section_header.h"
       
    20 #include "elf/elf_symbol.h"
       
    21 #include "elf/elf_string_table.h"
       
    22 
       
    23 #include <algorithm>
       
    24 #include <functional>
       
    25 
       
    26 Elf_object::Elf_object(const char* p1, const char* p2)
       
    27 {
       
    28     const char* first_p = p1;
       
    29     const char* last_p = p2;
       
    30 
       
    31     elf::File_header fh(first_p, last_p);
       
    32 
       
    33     const char* sh_table_p = first_p + fh.get_shoff();
       
    34 
       
    35     const char* p = sh_table_p;
       
    36 
       
    37     unsigned D = fh.get_shentsize();
       
    38     unsigned N = fh.get_shnum();
       
    39 
       
    40     // Iterate over the section header table.
       
    41     for (unsigned i = 0; i < N; i++, p += D )
       
    42     {
       
    43         elf::Section_header sh(p, last_p);
       
    44 
       
    45         if ( sh.is_type_symtab() ) // We've found the symbol table's section header.
       
    46         {
       
    47             // Locate the string table.
       
    48             elf::Section_header strtab_sh( sh_table_p + D*sh.get_link(), last_p );
       
    49             elf::String_table strtab(first_p + strtab_sh.get_offset(), strtab_sh.get_size() );
       
    50 
       
    51             unsigned D = sh.get_entsize();  // The difference between two symbol pointers.
       
    52             unsigned N = sh.get_size() / D; // The number of symbols.
       
    53 
       
    54             const char* p = first_p + sh.get_offset();
       
    55 
       
    56             // Iterate over all symbols.
       
    57             for (unsigned i = 0; i < N; i++, p += D)
       
    58             {
       
    59                 const elf::Symbol s(p);
       
    60 
       
    61                 if (s.get_shndx() == 0)
       
    62                 {
       
    63                     m_undef_symbols.push_back( strtab.get_string( s.get_name() ) );
       
    64                 }
       
    65             }
       
    66 
       
    67             break; // We're only interested in the symbol table ...
       
    68         }
       
    69     }
       
    70 }
       
    71 
       
    72 Elf_object::~Elf_object() {}
       
    73 
       
    74 bool Elf_object::is_undef(const char a_sym[]) const
       
    75 {
       
    76     using std::find_if;
       
    77     using std::not1;
       
    78     using std::bind2nd;
       
    79     using std::ptr_fun;
       
    80     using std::strcmp;
       
    81 
       
    82     typedef std::vector<const char*> T;
       
    83 
       
    84     T::const_iterator beg_p = m_undef_symbols.begin();
       
    85     T::const_iterator end_p = m_undef_symbols.end();
       
    86 
       
    87     // "STL considered harmful."
       
    88     T::const_iterator pos = find_if( beg_p, end_p, not1(bind2nd( ptr_fun(strcmp), a_sym)) );
       
    89 
       
    90     return (pos != end_p);
       
    91 }
       
    92