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

junyan.he at inbox.com junyan.he at inbox.com
Sun Jun 11 05:49:48 UTC 2017


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

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();
     }
 
-- 
2.7.4





More information about the Beignet mailing list