bintools/checklib/object/elf_object.cpp
author Mike Kinghan <mikek@symbian.org>
Thu, 25 Nov 2010 13:59:07 +0000
changeset 40 68f68128601f
parent 2 39c28ec933dd
permissions -rwxr-xr-x
1) Add the sbsv1 components from sftools/dev/build to make the linux_build package independent of the obsolete buildtools package. 2) Enhance romnibus.pl so that it generate the symbol file for the built rom when invoked by Raptor 3) Make the maksym.pl tool portable for Linux as well as Windows. 4) Remove the of armasm2as.pl from the e32tools component in favour of the copy now exported from sbsv1/e32util.

// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
// All rights reserved.
// This component and the accompanying materials are made available
// under the terms of "Eclipse Public License v1.0"
// which accompanies this distribution, and is available
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
//
// Initial Contributors:
// Nokia Corporation - initial contribution.
//
// Contributors:
//
// Description:
//

#include "object.h"

#include "elf/elf_file_header.h"
#include "elf/elf_section_header.h"
#include "elf/elf_symbol.h"
#include "elf/elf_string_table.h"

#include <algorithm>
#include <functional>
#include <cstring>

Elf_object::Elf_object(const char* p1, const char* p2)
{
    const char* first_p = p1;
    const char* last_p = p2;

    elf::File_header fh(first_p, last_p);

    const char* sh_table_p = first_p + fh.get_shoff();

    const char* p = sh_table_p;

    unsigned D = fh.get_shentsize();
    unsigned N = fh.get_shnum();

    // Iterate over the section header table.
    for (unsigned i = 0; i < N; i++, p += D )
    {
        elf::Section_header sh(p, last_p);

        if ( sh.is_type_symtab() ) // We've found the symbol table's section header.
        {
            // Locate the string table.
            elf::Section_header strtab_sh( sh_table_p + D*sh.get_link(), last_p );
            elf::String_table strtab(first_p + strtab_sh.get_offset(), strtab_sh.get_size() );

            unsigned D = sh.get_entsize();  // The difference between two symbol pointers.
            unsigned N = sh.get_size() / D; // The number of symbols.

            const char* p = first_p + sh.get_offset();

            // Iterate over all symbols.
            for (unsigned i = 0; i < N; i++, p += D)
            {
                const elf::Symbol s(p);

                if (s.get_shndx() == 0)
                {
                    m_undef_symbols.push_back( strtab.get_string( s.get_name() ) );
                }
            }

            break; // We're only interested in the symbol table ...
        }
    }
}

Elf_object::~Elf_object() {}

bool Elf_object::is_undef(const char a_sym[]) const
{
    using std::find_if;
    using std::not1;
    using std::bind2nd;
    using std::ptr_fun;
    using std::strcmp;

    typedef std::vector<const char*> T;

    T::const_iterator beg_p = m_undef_symbols.begin();
    T::const_iterator end_p = m_undef_symbols.end();

    // "STL considered harmful."
    T::const_iterator pos = find_if( beg_p, end_p, not1(bind2nd( ptr_fun(strcmp), a_sym)) );

    return (pos != end_p);
}