From 755b92c5804528e42798cfc47fe37e91c92d4934 Mon Sep 17 00:00:00 2001
From: Mario Werner <mario.werner@iaik.tugraz.at>
Date: Fri, 14 Nov 2014 13:40:25 +0100
Subject: [PATCH] move the section table to the end of the elf file

---
 elf_examples/write_exe_i386_32_match   | Bin 4239 -> 4439 bytes
 elf_examples/write_obj_i386_32_match.o | Bin 580 -> 580 bytes
 elf_examples/write_obj_i386_64_match.o | Bin 820 -> 820 bytes
 elfio/elfio.hpp                        |  53 +++++++++++--------------
 4 files changed, 23 insertions(+), 30 deletions(-)

diff --git a/elf_examples/write_exe_i386_32_match b/elf_examples/write_exe_i386_32_match
index 4d1a9da4710c28767ee1bcf03e4c2fd8fd1fd40d..dc085d3701d94c88466c5641aafacc2e3374f0d4 100644
GIT binary patch
delta 30
mcmeBIysk7sfvI0$qEgl70LBe`lLHvHOcLmySgbK|&H@0cDhsRt

delta 23
fcmcbv)UP-}fvJRHqEgl5?E(unRvh7*B+w54X><uA

diff --git a/elf_examples/write_obj_i386_32_match.o b/elf_examples/write_obj_i386_32_match.o
index 48f0942d0f8e0fa0f3386ee0f350ad8a32c530cb..5137cf1dc99c5e5273363fc786189cbf901cb6c5 100644
GIT binary patch
delta 94
zcmX@Ya)f1qf-nms0~nYvC;({<1`Y<s$;FHpC%TGH4B(j<z&bHTfXRemVxAgP0mH;;
tYD^OtChk*Xod;CQGdYn_c;X)crZo(c`54uh?l4R?15!^ICdV;~0RX@178(Em

delta 94
zcmX@Ya)f1qg0Kk#1eh==02vw#91M(;85uPv>o5v1l`u}WV^m|>z&JS#NL^r@+{dWK
qdXJHTfoI}I;mK!!D&8<oz6X?&VVcawq{gbk#K531aihpY)AImJ;uK2&

diff --git a/elf_examples/write_obj_i386_64_match.o b/elf_examples/write_obj_i386_64_match.o
index 8335573feecfdb3b9004c53faf053f6eb551b11a..62c606717cbe34f9e826c455d62edde6a06a656e 100644
GIT binary patch
delta 90
zcmdnOwuNnihPVkM0~|OoSOD1$3>*xMlkJ(lPIR@N7{EHQL6VVS;w*b0aSKRHmSO~v
XQDAZwkYt>Ei_x9~YKp?-L`Hc47hn;v

delta 79
zcmdnOwuNni#^fGG4t@q8aA2?ilN=0;lMNXiCzmivGBQo>W3&g7=fET%6Nv2xCi{RS
P^W<}2l8;$!qQe&edbAMy

diff --git a/elfio/elfio.hpp b/elfio/elfio.hpp
index 5007058..ad71e43 100644
--- a/elfio/elfio.hpp
+++ b/elfio/elfio.hpp
@@ -155,11 +155,23 @@ class elfio
         }
 
         bool is_still_good = true;
-        fill_final_attributes();
-        set_current_file_position();
+
+        // define layout specific header fields
+        // The position of the segment table is fixed after the header.
+        // The position of the section table is variable and needs to be fixed
+        // before saving.
+        header->set_segments_num( segments.size() );
+        header->set_segments_offset( segments.size() ? header->get_header_size() : 0 );
+        header->set_sections_num( sections.size() );
+        header->set_sections_offset( 0 );
+
+        // layout the first section right after the segment table
+        current_file_pos = header->get_header_size() +
+                    header->get_segment_entry_size() * header->get_segments_num();
 
         is_still_good = layout_segments_and_their_sections();
         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 );
@@ -419,24 +431,6 @@ class elfio
         return true;
     }
 
-//------------------------------------------------------------------------------
-    void fill_final_attributes()
-    {
-        // 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() );
-    }
-
 //------------------------------------------------------------------------------
     bool save_header( std::ofstream& f )
     {
@@ -472,16 +466,6 @@ class elfio
         return true;
     }
 
-
-//------------------------------------------------------------------------------
-    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();
-    }
-
-
 //------------------------------------------------------------------------------
     bool is_section_without_segment( unsigned int section_index )
     {
@@ -662,6 +646,15 @@ class elfio
         return true;
     }
 
+//------------------------------------------------------------------------------
+    bool layout_section_table()
+    {
+        // simply place the section table at the end for now
+        // TODO should this table be aligned?
+        header->set_sections_offset(current_file_pos);
+        return true;
+    }
+
 
 //------------------------------------------------------------------------------
   public:
-- 
GitLab