Skip to content
Snippets Groups Projects
Commit 17f03740 authored by Serge Lamikhov-Center's avatar Serge Lamikhov-Center Committed by Serge Lamikhov-Center
Browse files

Using macros for header fields access functions

Set segment's index upon its creation
Move to VS2013 projects
parent 1d2930ab
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,7 @@
#define ELFIO_NO_INTTYPES
#endif
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE ELFIO_Test
#include <boost/test/unit_test.hpp>
......@@ -1013,7 +1014,7 @@ BOOST_AUTO_TEST_CASE( test_dynamic_64_3 )
Elf_Xword value, value1;
std::string str, str1;
for ( int i = 0; i < da.get_entries_num(); ++i ) {
for ( unsigned int i = 0; i < da.get_entries_num(); ++i ) {
da.get_entry( i, tag, value, str );
if ( tag == DT_NEEDED ||
tag == DT_SONAME ||
......@@ -1025,7 +1026,7 @@ BOOST_AUTO_TEST_CASE( test_dynamic_64_3 )
da1.add_entry( tag, value );
}
}
for ( int i = 0; i < da.get_entries_num(); ++i ) {
for ( unsigned int i = 0; i < da.get_entries_num(); ++i ) {
da.get_entry( i, tag, value, str );
da1.get_entry( i, tag1, value1, str1 );
......
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
......@@ -14,20 +14,20 @@
<ProjectGuid>{FC8A3379-B8AA-4C32-804F-F8C43AFBC2A1}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>ELFIOTest</RootNamespace>
<VCTargetsPath Condition="'$(VCTargetsPath11)' != '' and '$(VSVersion)' == '' and $(VisualStudioVersion) == ''">$(VCTargetsPath11)</VCTargetsPath>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v110</PlatformToolset>
<PlatformToolset>v120</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v120</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
......@@ -53,7 +53,7 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..;\Developer\boost_1_52_0;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>..;\Developer\boost_1_55_0;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<EnablePREfast>false</EnablePREfast>
</ClCompile>
<Link>
......@@ -61,7 +61,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>
</AdditionalDependencies>
<AdditionalLibraryDirectories>\Developer\boost_1_52_0\stage12\lib</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories>c:\Developer\boost_1_55_0\stage\lib</AdditionalLibraryDirectories>
<Profile>false</Profile>
</Link>
<PostBuildEvent>
......
CXX=g++
CPPFLAGS=-c -Wall -std=c++0x -I../elfio
LDLIBS=-lboost_test_exec_monitor-mt
CPPFLAGS=-c -Wall -std=c++0x -I..
LDLIBS=-lboost_unit_test_framework-mt
ELFIODIR=../elfio/
SOURCES=ELFIOTest.cpp ELFIOTest1.cpp
......
......@@ -303,6 +303,7 @@ class elfio
return 0;
}
new_segment->set_index( (Elf_Half)segments_.size() );
segments_.push_back( new_segment );
return new_segment;
......@@ -381,14 +382,15 @@ class elfio
// add sections to the segments based on the load address
Elf64_Off segBaseOffset = seg->get_offset();
Elf64_Off segEndOffset = segBaseOffset + seg->get_memory_size();
Elf_Half sec_num = sections.size();
for(Elf_Half j = 0; j < sec_num; ++j) {
Elf64_Off segEndOffset = segBaseOffset + seg->get_memory_size();
Elf_Half sec_num = sections.size();
for( Elf_Half j = 0; j < sec_num; ++j ) {
const section* psec = sections[j];
Elf64_Off secOffset = psec->get_offset();
if( segBaseOffset <= secOffset && secOffset < segEndOffset)
seg->add_section_index(psec->get_index(),
psec->get_addr_align());
if( segBaseOffset <= secOffset && secOffset < segEndOffset ) {
seg->add_section_index( psec->get_index(),
psec->get_addr_align() );
}
}
// Add section into the segments' container
......@@ -403,12 +405,14 @@ class elfio
{
// Fill not completed fields in the header
header->set_segments_num( segments.size() );
if ( segments.size() == 0 ) {
header->set_segments_offset( 0 );
}
else {
header->set_segments_offset( header->get_header_size() );
}
header->set_sections_num( sections.size() );
header->set_sections_offset( header->get_header_size() +
header->get_segment_entry_size() * segments.size() );
......@@ -425,8 +429,8 @@ class elfio
void set_current_file_position()
{
current_file_pos = header->get_header_size() +
header->get_segment_entry_size() * header->get_segments_num() +
header->get_section_entry_size() * header->get_sections_num();
header->get_segment_entry_size() * header->get_segments_num() +
header->get_section_entry_size() * header->get_sections_num();
}
......@@ -434,8 +438,11 @@ class elfio
bool is_section_without_segment( unsigned int section_index )
{
bool found = false;
for ( unsigned int j = 0; !found && ( j < segments.size() ); ++j ) {
for ( unsigned int k = 0; !found && ( k < segments[j]->get_sections_num() ); ++k ) {
for ( unsigned int k = 0;
!found && ( k < segments[j]->get_sections_num() );
++k ) {
found = segments[j]->get_section_index_at( k ) == section_index;
}
}
......@@ -444,40 +451,48 @@ class elfio
}
//------------------------------------------------------------------------------
bool is_subsequence_of( segment* small, segment* big)
bool is_subsequence_of( segment* seg1, segment* seg2 )
{
const std::vector<Elf_Half>& smallV = small->get_sections();
const std::vector<Elf_Half>& bigV = big->get_sections();
bool found = std::search(bigV.begin(), bigV.end(),
smallV.begin(), smallV.end()) != bigV.end();
return found && smallV.size() != bigV.size();
// Return 'true' if sections of seg1 are a subset of sections in seg2
const std::vector<Elf_Half>& sections1 = seg1->get_sections();
const std::vector<Elf_Half>& sections2 = seg2->get_sections();
bool found = std::search( sections2.begin(), sections2.end(),
sections1.begin(), sections1.end() )
!= sections2.end();
return found &&
( sections2.size() != 0 ) &&
( sections1.size() != sections2.size() );
}
//------------------------------------------------------------------------------
std::vector<segment*> get_ordered_segments( )
{
std::vector<segment*> res;
std::deque<segment*> worklist;
std::deque<segment*> worklist;
res.reserve(segments.size());
std::copy( segments_.begin(), segments_.end(),
std::back_inserter( worklist )) ;
std::copy(segments_.begin(),segments_.end(),std::back_inserter(worklist));
while(!worklist.empty()) {
while ( !worklist.empty() ) {
segment *seg = worklist.front();
worklist.pop_front();
size_t i = 0;
for( ; i < worklist.size(); ++i)
{
if( is_subsequence_of( seg, worklist[i] ) )
break;
for ( ; i < worklist.size(); ++i ) {
if ( is_subsequence_of( seg, worklist[i] ) ) {
break;
}
}
if( i < worklist.size() )
worklist.push_back(seg);
if ( i < worklist.size() )
worklist.push_back(seg);
else
res.push_back(seg);
res.push_back(seg);
}
return res;
}
......@@ -493,10 +508,13 @@ class elfio
current_file_pos % section_align;
}
std::streampos headerPosition = (std::streamoff)header->get_sections_offset() +
std::streampos headerPosition =
(std::streamoff)header->get_sections_offset() +
header->get_section_entry_size() * sections_[i]->get_index();
sections_[i]->save( f, headerPosition, (std::streamoff)current_file_pos );
sections_[i]->save( f,
headerPosition,
(std::streamoff)current_file_pos );
if ( SHT_NOBITS != sections_[i]->get_type() &&
SHT_NULL != sections_[i]->get_type() ) {
......@@ -512,8 +530,8 @@ class elfio
//------------------------------------------------------------------------------
bool save_segments_and_their_sections( std::ofstream& f )
{
std::vector<segment*> worklist;
std::vector<bool> section_generated(sections.size(),false);
std::vector<segment*> worklist;
std::vector<bool> section_generated(sections.size(),false);
std::vector<Elf_Xword> section_alignment(sections.size(),0);
// get segments in a order in where segments which contain a
......@@ -526,28 +544,32 @@ class elfio
for ( unsigned int j = 0; j <segments[i]->get_sections_num(); ++j )
{
Elf_Half index = segments[i]->get_section_index_at( j );
section* sec = sections[ index ];
if(sec->get_addr_align() > section_alignment[index])
section_alignment[index] = sec->get_addr_align();
section* sec = sections[ index ];
if (sec->get_addr_align() > section_alignment[index]) {
section_alignment[index] = sec->get_addr_align();
}
if(segments[i]->get_align() > section_alignment[index] &&
j == 0)
if ( segments[i]->get_align() > section_alignment[index] &&
j == 0 ) {
section_alignment[index] = segments[i]->get_align();
}
}
}
for ( unsigned int i = 0; i < worklist.size(); ++i ) {
Elf_Xword segment_memory = 0;
Elf_Xword segment_filesize = 0;
Elf_Xword current_data_pos = 0;
Elf_Xword segment_memory = 0;
Elf_Xword segment_filesize = 0;
Elf_Xword current_data_pos = 0;
// Write segment's data
for ( unsigned int j = 0; j <worklist[i]->get_sections_num(); ++j ) {
Elf_Half index = worklist[i]->get_section_index_at( j );
section* sec = sections[ worklist[i]->get_section_index_at( j )];
std::streampos headerPosition = (std::streamoff)header->get_sections_offset() +
header->get_section_entry_size()*sec->get_index();
std::streampos headerPosition =
(std::streamoff)header->get_sections_offset() +
header->get_section_entry_size()*sec->get_index();
// calculate the required alignment
// when the has already been generated then no alignment is required
......@@ -555,7 +577,8 @@ class elfio
if ( section_alignment[index] > 1 &&
current_file_pos % section_alignment[index] != 0 &&
!section_generated[index] ) {
secAlign = section_alignment[index] - current_file_pos % section_alignment[index];
secAlign = section_alignment[index] -
current_file_pos % section_alignment[index];
}
// determine the segment file and memory sizes
......@@ -586,7 +609,9 @@ class elfio
worklist[i]->set_file_size( segment_filesize );
worklist[i]->set_memory_size( segment_memory );
worklist[i]->save( f, (std::streamoff)segment_header_position, (std::streamoff)current_data_pos );
worklist[i]->save( f,
(std::streamoff)segment_header_position,
(std::streamoff)current_data_pos );
}
return true;
......@@ -628,7 +653,9 @@ class elfio
section* sec = 0;
std::vector<section*>::const_iterator it;
for ( it = parent->sections_.begin(); it != parent->sections_.end(); ++it ) {
for ( it = parent->sections_.begin();
it != parent->sections_.end();
++it ) {
if ( (*it)->get_name() == name ) {
sec = *it;
break;
......
......@@ -34,8 +34,7 @@ class section
public:
virtual ~section() {};
virtual Elf_Half get_index() const = 0;
ELFIO_GET_ACCESS_DECL ( Elf_Half, index );
ELFIO_GET_SET_ACCESS_DECL( std::string, name );
ELFIO_GET_SET_ACCESS_DECL( Elf_Word, type );
ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, flags );
......@@ -54,8 +53,9 @@ class section
virtual void append_data( const std::string& data ) = 0;
protected:
virtual Elf64_Off get_offset() const = 0;
virtual void set_index( Elf_Half ) = 0;
ELFIO_GET_ACCESS_DECL( Elf64_Off, offset );
ELFIO_SET_ACCESS_DECL( Elf_Half, index );
virtual void load( std::ifstream& f,
std::streampos header_offset ) = 0;
virtual void save( std::ofstream& f,
......@@ -196,11 +196,7 @@ class section_impl : public section
//------------------------------------------------------------------------------
protected:
//------------------------------------------------------------------------------
Elf64_Off
get_offset() const
{
return header.sh_offset;
}
ELFIO_GET_ACCESS( Elf64_Off, offset, header.sh_offset );
//------------------------------------------------------------------------------
void
......
......@@ -34,8 +34,7 @@ class segment
public:
virtual ~segment() {};
virtual Elf_Half get_index() const = 0;
ELFIO_GET_ACCESS_DECL ( Elf_Half, index );
ELFIO_GET_SET_ACCESS_DECL( Elf_Word, type );
ELFIO_GET_SET_ACCESS_DECL( Elf_Word, flags );
ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, align );
......@@ -51,7 +50,8 @@ class segment
virtual Elf_Half get_section_index_at( Elf_Half num ) const = 0;
protected:
virtual Elf64_Off get_offset() const = 0;
ELFIO_GET_ACCESS_DECL( Elf64_Off, offset );
virtual const std::vector<Elf_Half>& get_sections() const = 0;
virtual void set_index( Elf_Half ) = 0;
virtual void load( std::ifstream& stream, std::streampos header_offset ) = 0;
......@@ -136,16 +136,14 @@ class segment_impl : public segment
//------------------------------------------------------------------------------
protected:
//------------------------------------------------------------------------------
Elf64_Off
get_offset() const
{
return ph.p_offset;
}
ELFIO_GET_ACCESS( Elf64_Off, offset, ph.p_offset );
//------------------------------------------------------------------------------
const std::vector<Elf_Half>& get_sections() const
{
return sections;
}
//------------------------------------------------------------------------------
void
set_index( Elf_Half value )
......@@ -184,9 +182,9 @@ class segment_impl : public segment
//------------------------------------------------------------------------------
private:
mutable T ph;
T ph;
Elf_Half index;
mutable char* data;
char* data;
std::vector<Elf_Half> sections;
endianess_convertor* convertor;
};
......
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
......@@ -14,21 +14,20 @@
<ProjectGuid>{CBAA735F-E237-4976-909F-2349920ED871}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>ELFDump</RootNamespace>
<VCTargetsPath Condition="'$(VCTargetsPath11)' != '' and '$(VSVersion)' == '' and $(VisualStudioVersion) == ''">$(VCTargetsPath11)</VCTargetsPath>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v110</PlatformToolset>
<PlatformToolset>v120</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v110</PlatformToolset>
<PlatformToolset>v120</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
......
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
......@@ -19,13 +19,13 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<PlatformToolset>v120</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<PlatformToolset>v120</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment