bintools/checklib/object/elf_object.cpp
changeset 2 39c28ec933dd
equal deleted inserted replaced
1:820b22e13ff1 2:39c28ec933dd
       
     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 #include <cstring>
       
    26 
       
    27 Elf_object::Elf_object(const char* p1, const char* p2)
       
    28 {
       
    29     const char* first_p = p1;
       
    30     const char* last_p = p2;
       
    31 
       
    32     elf::File_header fh(first_p, last_p);
       
    33 
       
    34     const char* sh_table_p = first_p + fh.get_shoff();
       
    35 
       
    36     const char* p = sh_table_p;
       
    37 
       
    38     unsigned D = fh.get_shentsize();
       
    39     unsigned N = fh.get_shnum();
       
    40 
       
    41     // Iterate over the section header table.
       
    42     for (unsigned i = 0; i < N; i++, p += D )
       
    43     {
       
    44         elf::Section_header sh(p, last_p);
       
    45 
       
    46         if ( sh.is_type_symtab() ) // We've found the symbol table's section header.
       
    47         {
       
    48             // Locate the string table.
       
    49             elf::Section_header strtab_sh( sh_table_p + D*sh.get_link(), last_p );
       
    50             elf::String_table strtab(first_p + strtab_sh.get_offset(), strtab_sh.get_size() );
       
    51 
       
    52             unsigned D = sh.get_entsize();  // The difference between two symbol pointers.
       
    53             unsigned N = sh.get_size() / D; // The number of symbols.
       
    54 
       
    55             const char* p = first_p + sh.get_offset();
       
    56 
       
    57             // Iterate over all symbols.
       
    58             for (unsigned i = 0; i < N; i++, p += D)
       
    59             {
       
    60                 const elf::Symbol s(p);
       
    61 
       
    62                 if (s.get_shndx() == 0)
       
    63                 {
       
    64                     m_undef_symbols.push_back( strtab.get_string( s.get_name() ) );
       
    65                 }
       
    66             }
       
    67 
       
    68             break; // We're only interested in the symbol table ...
       
    69         }
       
    70     }
       
    71 }
       
    72 
       
    73 Elf_object::~Elf_object() {}
       
    74 
       
    75 bool Elf_object::is_undef(const char a_sym[]) const
       
    76 {
       
    77     using std::find_if;
       
    78     using std::not1;
       
    79     using std::bind2nd;
       
    80     using std::ptr_fun;
       
    81     using std::strcmp;
       
    82 
       
    83     typedef std::vector<const char*> T;
       
    84 
       
    85     T::const_iterator beg_p = m_undef_symbols.begin();
       
    86     T::const_iterator end_p = m_undef_symbols.end();
       
    87 
       
    88     // "STL considered harmful."
       
    89     T::const_iterator pos = find_if( beg_p, end_p, not1(bind2nd( ptr_fun(strcmp), a_sym)) );
       
    90 
       
    91     return (pos != end_p);
       
    92 }
       
    93