[02/57] Modify the elfio lib, make it generate memory image.

Submitted by junyan.he@inbox.com on June 11, 2017, 5:49 a.m.

Details

Message ID 1497160243-6286-2-git-send-email-junyan.he@inbox.com
State New
Headers show
Series "Series without cover letter" ( rev: 1 ) in Beignet

Not browsing as part of any series.

Commit Message

junyan.he@inbox.com June 11, 2017, 5:49 a.m.
From: Junyan He <junyan.he@intel.com>

We do not want to write the elf binary directly to a file.
We prefer to keep it in the memory and analyse the elf image
in runtime.

Signed-off-by: Junyan He <junyan.he@intel.com>
Reviewed-by: Yang Rong <rong.r.yang@intel.com>
---
 backend/src/elfio/elfio.hpp        | 37 ++++++++++++++++++++++---------------
 backend/src/elfio/elfio_header.hpp |  1 -
 2 files changed, 22 insertions(+), 16 deletions(-)

Patch hide | download patch | download mbox

diff --git a/backend/src/elfio/elfio.hpp b/backend/src/elfio/elfio.hpp
index 3a86c93..7b9501c 100644
--- a/backend/src/elfio/elfio.hpp
+++ b/backend/src/elfio/elfio.hpp
@@ -148,14 +148,7 @@  class elfio
     }
 
 //------------------------------------------------------------------------------
-    bool save( const std::string& file_name )
-    {
-        std::ofstream f( file_name.c_str(), std::ios::out | std::ios::binary );
-
-        if ( !f ) {
-            return false;
-        }
-
+    bool save( std::ostream &stream) {
         bool is_still_good = true;
 
         // Define layout specific header fields
@@ -175,15 +168,29 @@  class elfio
         is_still_good = is_still_good && layout_sections_without_segments();
         is_still_good = is_still_good && layout_section_table();
 
-        is_still_good = is_still_good && save_header( f );
-        is_still_good = is_still_good && save_sections( f );
-        is_still_good = is_still_good && save_segments( f );
+        is_still_good = is_still_good && save_header( stream );
+        is_still_good = is_still_good && save_sections( stream );
+        is_still_good = is_still_good && save_segments( stream );
 
-        f.close();
 
         return is_still_good;
     }
 
+    bool save( const std::string& file_name )
+    {
+        bool ret;
+
+        std::ofstream f( file_name.c_str(), std::ios::out | std::ios::binary );
+
+        if ( !f ) {
+            return false;
+        }
+
+        ret = save( f );
+        f.close();
+        return ret;
+    }
+
 //------------------------------------------------------------------------------
     // ELF header access functions
     ELFIO_HEADER_ACCESS_GET( unsigned char, class              );
@@ -435,13 +442,13 @@  class elfio
     }
 
 //------------------------------------------------------------------------------
-    bool save_header( std::ofstream& f )
+    bool save_header( std::ostream& f )
     {
         return header->save( f );
     }
 
 //------------------------------------------------------------------------------
-    bool save_sections( std::ofstream& f )
+    bool save_sections( std::ostream& f )
     {
         for ( unsigned int i = 0; i < sections_.size(); ++i ) {
             section *sec = sections_.at(i);
@@ -456,7 +463,7 @@  class elfio
     }
 
 //------------------------------------------------------------------------------
-    bool save_segments( std::ofstream& f )
+    bool save_segments( std::ostream& f )
     {
         for ( unsigned int i = 0; i < segments_.size(); ++i ) {
             segment *seg = segments_.at(i);
diff --git a/backend/src/elfio/elfio_header.hpp b/backend/src/elfio/elfio_header.hpp
index d689a88..16e1dee 100644
--- a/backend/src/elfio/elfio_header.hpp
+++ b/backend/src/elfio/elfio_header.hpp
@@ -111,7 +111,6 @@  template< class T > class elf_header_impl : public elf_header
     {
         stream.seekp( 0 );
         stream.write( reinterpret_cast<const char*>( &header ), sizeof( header ) );
-
         return stream.good();
     }