Fix stupid copy
[centaur.git] / src / copy / segments.c
index 0928de6eac996539c3c7823392080d8e7fc071b6..0f320c178fceb0f3760717fdb6941471176209f0 100644 (file)
@@ -114,8 +114,9 @@ void elfu_copySegments2(Elf *eo, Elf *ei)
     if (phdr.p_type == PT_PHDR) {
       /* Skip PHDR entries and fix them up later */
     }
-    else if (phdr.p_filesz == 0) {
+    else if (phdr.p_memsz == 0) {
       phdrOut.p_filesz = 0;
+      phdrOut.p_memsz = 0;
     } else {
       /* Try to guess the end of the segment */
       Elf_Scn *scn, *scnOut;
@@ -144,17 +145,12 @@ void elfu_copySegments2(Elf *eo, Elf *ei)
         fprintf(stderr, "gelf_getshdr() failed: %s\n", elf_errmsg(-1));
       }
 
-      phdrOut.p_filesz = shdrOut.sh_offset - phdrOut.p_offset + shdrOut.sh_size;
-    }
+      phdrOut.p_filesz = shdrOut.sh_offset - phdrOut.p_offset;
+      if (shdrOut.sh_type != SHT_NOBITS) {
+        phdrOut.p_filesz += shdrOut.sh_size;
+      }
 
-    if (phdr.p_type == PT_PHDR) {
-      /* Skip PHDR entries and fix them up later */
-    }
-    else if (phdr.p_memsz == 0) {
-      phdrOut.p_memsz = 0;
-    } else {
-      // TODO: Calculate memory size (depends on .bss, etc.)
-      phdrOut.p_memsz = phdrOut.p_filesz; // FIXME
+      phdrOut.p_memsz = shdrOut.sh_offset - phdrOut.p_offset + shdrOut.sh_size;
     }
 
     if (!gelf_update_phdr (eo, i, &phdrOut)) {