-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreadelf.cpp
More file actions
99 lines (84 loc) · 2.83 KB
/
Copy pathreadelf.cpp
File metadata and controls
99 lines (84 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**********************************************************************************
* Implementation for readelf. This is a helper class for parsing elf files
*
* Copyright (C) 2004 Joe Fox All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*********************************************************************************/
#include <iterator>
#include <iterator>
#include "readelf.h"
namespace
{
typedef std::vector<Elf32_Shdr> SectionArray;
}
/*
* CReadElf elf(.../libappinfo.so);
* CharArray dataBuf = elf.getSection(".data");
* CharArray textBuf = elf.getSection(".text");
*
* hash dataBuf and textBuf
*
*/
namespace readelf
{
CReadElf::CReadElf(const char *szFileName)
{
// open the file
m_fElf.open(szFileName);
// if we got the file
if(m_fElf)
{
// read in the elf struct
m_fElf.read(reinterpret_cast<char*>(&m_elf), sizeof(m_elf));
// load the header struct
SectionArray arSections(m_elf.e_shnum);
m_fElf.seekg(m_elf.e_shoff);
m_fElf.read(reinterpret_cast<char*>(&arSections[0]), arSections.size() * m_elf.e_shentsize);
// read in the section string table
m_fElf.seekg(arSections[m_elf.e_shstrndx].sh_offset);
CharArray arSectionNames(arSections[m_elf.e_shstrndx].sh_size);
m_fElf.read(&arSectionNames[0], arSectionNames.size());
// now generate a map of names to sections
for(SectionArray::iterator itSec = arSections.begin(), itEnd = arSections.end()
; itSec != itEnd
; ++itSec)
{
// go to the offset in the sectionnames as indicated by the section. That is the name
// of the section (null terminated).
m_mpSections.insert(std::make_pair(&arSectionNames[0] + itSec->sh_name, *itSec));
}
}
}
CReadElf::~CReadElf()
{
}
UCharArray CReadElf::getSection(const char *szSectionName)
{
UCharArray arRetval;
// try to find the section
SectionMap::iterator itSec = m_mpSections.find(szSectionName);
// if we found the section
if(itSec != m_mpSections.end())
{
// seek to the section indicated
m_fElf.seekg(itSec->second.sh_offset);
// read the sections contents in
arRetval.resize(itSec->second.sh_size);
m_fElf.read(reinterpret_cast<char*>(&arRetval[0]), arRetval.size());
}
return arRetval;
}
}