GPLv2 release
[centaur.git] / src / libelfu / modelops / layout.c
index 4026e58ce3e1e1a341201fe861bf8424b933aa17..dd60c77b2edb832cb62eb28ccf13c8e8ea806ff2 100644 (file)
@@ -1,3 +1,18 @@
+/* This file is part of centaur.
+ *
+ * centaur is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License 2 as
+ * published by the Free Software Foundation.
+
+ * centaur is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with centaur.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
@@ -101,7 +116,8 @@ static ElfuPhdr* appendPhdr(ElfuElf *me)
                                 &lowestOffs, &highestOffsEnd);
     if (phdr_maxsz < (me->ehdr.e_phnum + 1) * me->ehdr.e_phentsize
         && phdrmp == lowestAddr
-        && phdrmp == lowestOffs) {
+        && phdrmp == lowestOffs
+        && (lowestAddr->phdr.p_vaddr >= 2 * lowestAddr->phdr.p_align)) {
       ElfuPhdr *mp;
       ElfuScn *ms;
       GElf_Word size = ROUNDUP(me->ehdr.e_phentsize, phdrmp->phdr.p_align);
@@ -240,7 +256,8 @@ GElf_Addr elfu_mLayoutGetSpaceInPhdr(ElfuElf *me, GElf_Word size,
   } else if (((w && (lowestAddr->phdr.p_flags & PF_W))
               || (x && (lowestAddr->phdr.p_flags & PF_X)))
              && /* Enough space to expand downwards? */
-             (lowestAddr->phdr.p_vaddr > 3 * lowestAddr->phdr.p_align)
+             (lowestAddr->phdr.p_vaddr >= ((2 * lowestAddr->phdr.p_align)
+                                           + ROUNDUP(size, lowestAddr->phdr.p_align)))
              /* Merging only works if the LOAD is the first both in file and mem */
              && lowestAddr == lowestOffs) {
     /* Need to prepend or split up the PHDR. */
@@ -343,7 +360,6 @@ int elfu_mLayoutAuto(ElfuElf *me)
   ElfuPhdr *highestOffsEnd;
   ElfuPhdr *mp;
   ElfuScn *ms;
-  ElfuPhdr **phdrArr;
   GElf_Off lastend = 0;
 
   assert(me);
@@ -352,12 +368,6 @@ int elfu_mLayoutAuto(ElfuElf *me)
   elfu_mPhdrLoadLowestHighest(me, &lowestAddr, &highestAddr,
                               &lowestOffs, &highestOffsEnd);
 
-  phdrArr = malloc(elfu_mPhdrCount(me) * sizeof(*phdrArr));
-  if (!phdrArr) {
-    ELFU_WARN("elfu_mLayoutAuto: malloc failed for phdrArr.\n");
-    return 1;
-  }
-
 
   lastend = OFFS_END(highestOffsEnd->phdr.p_offset, highestOffsEnd->phdr.p_filesz);