UnivesalDisassembler(2003)

sections.cc

Go to the documentation of this file.
00001 // sections.cc
00002 //
00003 // uda - universal disassembler
00004 // Date: 2003-05-04
00005 //
00006 // Copyright (c) 2002-2003 Ales Smrcka 
00007 //
00008 // This program is licensed under GNU Library GPL. See the file COPYING.
00009 //
00010 // This module implements BinaryFile and Section class. It is used as
00011 // gateway to binary file.
00012 //
00013 
00014 #include <iostream> //debug()
00015 #include "sections.h"
00016 #include "udaclasses.h"
00017 #include "plugins.h"
00018 using namespace std;
00019 
00021 // BinaryFile
00022 
00023 BinaryFile::BinaryFile(string fname)
00024 {
00025     bfdata.name=fname;
00026 }
00027 
00028 BinaryFile::~BinaryFile()
00029 {
00030     Free();
00031 }
00032 
00033 // return value: number of loaded sections
00034 int BinaryFile::Init()
00035 {
00036     int numsec=0;
00037     VPlugins::iterator i;
00038     for (i=plugins.begin();i!=plugins.end();i++)
00039         if ((*i)->IsFileDefined(bfdata.name.c_str()))
00040         {
00041             numsec=(*i)->LoadSections(bfdata);
00042             break;
00043         }
00044     typedef MSections::value_type vt;
00045     for (int i=0;i<numsec;i++)
00046         sections.insert(vt(bfdata.Sections[i]->name, Section(bfdata.Sections[i])));
00047     return numsec;
00048 }
00049 
00050 // true if address is valid in any scanned section
00051 bool BinaryFile::ScanAddress(Address addr)
00052 {
00053     MSections::iterator i;
00054     for (i=sections.begin();i!=sections.end();i++)
00055         if (i->second.data->scanaddress && i->second.ValidAddress(addr))
00056             return true;
00057     return false;
00058 }
00059 
00060 // Free all sections
00061 void BinaryFile::Free()
00062 {
00063     debug("BinaryFile:freeing all sections\n");
00064     while (bfdata.Sections.size())
00065     {
00066         delete bfdata.Sections[0];
00067         bfdata.Sections.erase(bfdata.Sections.begin());
00068     }
00069 }
00070 
00071 // indexing section with error detection
00072 Section &BinaryFile::operator [](const string index)
00073 {
00074     MSections::iterator i=sections.find(index);
00075     if (i!=sections.end())
00076         return i->second;
00077     else
00078         throw Exception(string("section ")+index+" does not exists");
00079 }
00080 
00082 // Section
00083 
00084 Section::Section(SectionData *sec)
00085 {
00086     data=sec;
00087 }
00088 
00089 bool Section::ValidAddress(Address a)
00090 {
00091     return (data!=NULL)&&
00092            (data->content!=NULL)&&
00093            (a >= data->addr)&&
00094            (a < data->addr+data->size);
00095 }
00096 
00097 byte &Section::operator [](Address a)
00098 {
00099     if (ValidAddress(a))
00100         if (data!=NULL)
00101             return data->content[a-data->addr];
00102         else
00103             throw Exception(string("bad address ")+uint2hex(a)+" in section "+data->name);
00104     else
00105         throw Exception(string("bad address ")+uint2hex(a)+" in section "+data->name);
00106 }