ELF: calculate the padding properly when joining sections
authorH. Peter Anvin <hpa@zytor.com>
Tue, 15 Jan 2008 19:42:32 +0000 (14:42 -0500)
committerH. Peter Anvin <hpa@zytor.com>
Tue, 15 Jan 2008 19:42:32 +0000 (14:42 -0500)
Fix the computation of padding when merging PHDRs.

elf.c

diff --git a/elf.c b/elf.c
index b2204b3..040d68e 100644 (file)
--- a/elf.c
+++ b/elf.c
@@ -77,7 +77,7 @@ static int gen_elf(struct segment *segs, Elf32_Ehdr *ehdr, FILE *out)
                        align_up(address+memsize, s->next->align))) {
                        s = s->next;
                        align = UINT32_C(1) << s->align;
-                       pad = (s->address - address) & (align-1);
+                       pad = (s->address - (address+memsize)) & (align-1);
                        memsize += s->length + pad;
                }
        }
@@ -136,10 +136,11 @@ static int gen_elf(struct segment *segs, Elf32_Ehdr *ehdr, FILE *out)
                        align_up(address+memsize, s->next->align))) {
                        s = s->next;
                        align = UINT32_C(1) << s->align;
-                       pad = (s->address - address) & (align-1);
+                       pad = (s->address - (address+memsize)) & (align-1);
                        if (s->sh_type != SHT_NOBITS) {
                                assert(pad ==
-                                      ((s->address - offset) & (align-1)));
+                                      ((s->address - (offset+filesize))
+                                       & (align-1)));
                                filesize += s->length + pad;
                        }
                        memsize += s->length + pad;