From 56b84c3624c59c8a1796bf411b7ba950d12ddfc5 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sun, 22 Jan 2017 18:58:46 +0100
Subject: Kept the nominal order between ELF program and section headers if
 possible.

---
 ChangeLog                 |  5 +++++
 src/glibext/gbinportion.c | 23 +++++++++++++++++++----
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 0c3d3b7..0249b88 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 17-01-22  Cyrille Bagard <nocbos@gmail.com>
 
+	* src/glibext/gbinportion.c:
+	Keep the nominal order between ELF program and section headers if possible.
+
+17-01-22  Cyrille Bagard <nocbos@gmail.com>
+
 	* src/analysis/disass/area.c:
 	Update virtual addresses when filling the gap between two binary areas.
 
diff --git a/src/glibext/gbinportion.c b/src/glibext/gbinportion.c
index a28f1a7..542936a 100644
--- a/src/glibext/gbinportion.c
+++ b/src/glibext/gbinportion.c
@@ -693,15 +693,30 @@ void g_binary_portion_include(GBinPortion *portion, GBinPortion *sub)
         /**
          * On prend ici en compte le genre de situations suivantes :
          *
-         *  [21] .bss                NOBITS          00088240 07823c 0018c8 00  WA  0   0  8
-         *  [22] __libc_freeres_ptrs NOBITS          00089b08 07823c 000018 00  WA  0   0  4
-         *  [23] .comment            PROGBITS        00000000 07823c 000022 01  MS  0   0  1
+         *   [21] .bss                NOBITS          00088240 07823c 0018c8 00  WA  0   0  8
+         *   [22] __libc_freeres_ptrs NOBITS          00089b08 07823c 000018 00  WA  0   0  4
+         *   [23] .comment            PROGBITS        00000000 07823c 000022 01  MS  0   0  1
          *
          * Pendant le désassemblage, la procédure n'aime pas trop les intersections
          * de zones mémoire.
+         *
+         * Par contre, on évite quand même les cas de figure où les portions sont identiques :
+         *
+         *   En-têtes de section:
+         *     [Nr] Nom               Type            Adr      Décala.Taille ES Fan LN Inf Al
+         *     ...
+         *     [ 2] .data             PROGBITS        00010098 000098 00000c 00  WA  0   0  1
+         *
+         *   En-têtes de programme:
+         *     Type           Décalage Adr. vir.  Adr.phys.  T.Fich. T.Mém.  Fan Alignement
+         *     ...
+         *     LOAD           0x000098 0x00010098 0x00010098 0x0000c 0x0000c RW  0x8000
+         *
+         * Ici l'ordre original doit être conservé !
          */
 
-        if (mrange_includes_mrange(&sub->range, &portion->subs[best]->range) == 0)
+        if (mrange_includes_mrange(&sub->range, &portion->subs[best]->range) == 0
+            && cmp_mrange(&sub->range, &portion->subs[best]->range) != 0)
         {
             tmp = portion->subs[best];
 
-- 
cgit v0.11.2-87-g4458