Archive-elf.C

Go to the documentation of this file.
00001 /*
00002  * See the dyninst/COPYRIGHT file for copyright information.
00003  * 
00004  * We provide the Paradyn Tools (below described as "Paradyn")
00005  * on an AS IS basis, and do not warrant its validity or performance.
00006  * We reserve the right to update, modify, or discontinue this
00007  * software at any time.  We shall have no obligation to supply such
00008  * updates or modifications or any other form of support to you.
00009  * 
00010  * By your use of Paradyn, you understand and agree that we (or any
00011  * other person or entity with proprietary rights in Paradyn) are
00012  * under no obligation to provide either maintenance services,
00013  * update services, notices of latent defects, or correction of
00014  * defects for Paradyn.
00015  * 
00016  * This library is free software; you can redistribute it and/or
00017  * modify it under the terms of the GNU Lesser General Public
00018  * License as published by the Free Software Foundation; either
00019  * version 2.1 of the License, or (at your option) any later version.
00020  * 
00021  * This library is distributed in the hope that it will be useful,
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00024  * Lesser General Public License for more details.
00025  * 
00026  * You should have received a copy of the GNU Lesser General Public
00027  * License along with this library; if not, write to the Free Software
00028  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00029  */
00030 
00031 #include <ar.h>
00032 
00033 #include "symtabAPI/h/Symtab.h"
00034 #include "symtabAPI/h/Archive.h"
00035 #include "symtabAPI/src/Object.h"
00036 
00037 using namespace Dyninst;
00038 using namespace Dyninst::SymtabAPI;
00039 
00040 Archive::Archive(std::string& filename, bool& err)
00041     : basePtr(NULL), symbolTableParsed(false)
00042 {
00043     mf = MappedFile::createMappedFile(filename);
00044 
00045     if( mf == NULL ) {
00046         serr = Not_A_File;
00047         errMsg = "Failed to locate file";
00048         err = false;
00049         return;
00050     }
00051 
00052     Elf_Cmd cmd = ELF_C_READ;
00053     Elf_Arhdr *archdr;
00054     Elf_X *arf = Elf_X::newElf_X(mf->getFD(), cmd, NULL, filename);
00055     if (elf_kind(arf->e_elfp()) != ELF_K_AR) {
00056         /* Don't close mf, because this file will most
00057          * likely be opened again as a normal Symtab
00058          * object
00059          */
00060     serr = Not_An_Archive;
00061     err = false;
00062     return;
00063     }
00064 
00065     basePtr = (void *) arf;
00066     Elf_X *newelf = Elf_X::newElf_X(mf->getFD(), cmd, arf);
00067 
00068     while( newelf->e_elfp() ) {
00069     archdr = elf_getarhdr(newelf->e_elfp());
00070     string member_name = archdr->ar_name;
00071 
00072         if (elf_kind(newelf->e_elfp()) == ELF_K_ELF) {
00073             /* The offset is to the beginning of the arhdr for the member, not
00074              * to the beginning of the elfhdr for the member. This allows the
00075              * the lazy construction of the object to use the elf_* methods to
00076              * get pointers to the ar and elf headers. This offset also matches
00077              * up with the offset in the archive global symbol table
00078              */
00079             Offset tmpOffset = elf_getbase(newelf->e_elfp()) - sizeof(struct ar_hdr);
00080 
00081             // Member is parsed lazily
00082             ArchiveMember *newMember = new ArchiveMember(member_name, tmpOffset);
00083 
00084             membersByName[member_name] = newMember;
00085             membersByOffset[tmpOffset] = newMember;
00086     }
00087 
00088     Elf_X *elfhandle = arf->e_next(newelf);
00089     newelf->end();
00090     newelf = elfhandle;
00091     }
00092 
00093     newelf->end();
00094     err = true;
00095 }
00096 
00097 Archive::Archive(char *, size_t, bool &err) 
00098     : basePtr(NULL), symbolTableParsed(false) 
00099 {
00100     err = false;
00101     serr = Obj_Parsing;
00102     errMsg = "current version of libelf doesn't fully support in memory archives";
00103 }
00104 
00105 bool Archive::parseMember(Symtab *&img, ArchiveMember *member) 
00106 {
00107     // Locate the member based on the stored offset
00108     Elf_X* elfX_Hdr = ((Elf_X *)basePtr)->e_rand(member->getOffset());
00109     Elf* elfHdr = elfX_Hdr->e_elfp();
00110 
00111     Elf_Arhdr *arhdr = elf_getarhdr(elfHdr);
00112     if( arhdr == NULL ) {
00113         serr = Obj_Parsing;
00114         errMsg = elf_errmsg(elf_errno());
00115         return false;
00116     }
00117 
00118     // Sanity check
00119     assert(member->getName() == string(arhdr->ar_name));
00120 
00121     size_t rawSize;
00122     char * rawMember = elf_rawfile(elfHdr, &rawSize);
00123 
00124     if( 0 == rawSize ) {
00125         rawSize = arhdr->ar_size;
00126     }
00127 
00128     if( rawMember == NULL || rawSize == 0 ) {
00129         serr = Obj_Parsing;
00130         errMsg = elf_errmsg(elf_errno());
00131         return false;
00132     }
00133 
00134     bool success = Symtab::openFile(img, (void *)rawMember, rawSize, member->getName());
00135     if( !success ) {
00136         serr = Obj_Parsing;
00137         errMsg = "problem creating underlying Symtab object";
00138         return false;
00139     }
00140 
00141     img->member_name_ = member->getName();
00142     img->member_offset_ = member->getOffset();
00143 
00144     img->parentArchive_ = this;
00145     member->setSymtab(img);
00146 
00147     elfX_Hdr->end();
00148 
00149     return true;
00150 }
00151 
00152 bool Archive::parseSymbolTable() {
00153     if( symbolTableParsed ) return true;
00154 
00155     Elf_Arsym *ar_syms;
00156     size_t numSyms;
00157     if( (ar_syms = elf_getarsym(static_cast<Elf_X *>(basePtr)->e_elfp(), &numSyms)) == NULL ) {
00158         serr = Obj_Parsing;
00159         errMsg = string("No symbol table found: ") + string(elf_errmsg(elf_errno()));
00160         return false;
00161     }
00162 
00163     // The last element is always a null element
00164     for(unsigned i = 0; i < (numSyms - 1); i++) {
00165         string symbol_name(ar_syms[i].as_name);
00166 
00167         // Duplicate symbols are okay here, they should be treated as errors
00168         // when necessary
00169         membersBySymbol.insert(make_pair(symbol_name, membersByOffset[ar_syms[i].as_off]));
00170     }
00171 
00172     symbolTableParsed = true;
00173 
00174     return true;
00175 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 12 Jul 2013 for SymtabAPI by  doxygen 1.6.1