]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: fix elf64phdr to allow using upx (and other broken ELF loaders).
authorOneOfOne <oneofone@gmail.com>
Fri, 15 Jan 2016 17:24:44 +0000 (19:24 +0200)
committerIan Lance Taylor <iant@golang.org>
Tue, 19 Jan 2016 18:01:33 +0000 (18:01 +0000)
The linker already applies the fix for elf32, so this just extends it to elf64.

Inspired by https://github.com/pwaller/goupx

Fixes #13974

Change-Id: I65d92b5be9590657060a0e8e80ff5b86ba40017f
Reviewed-on: https://go-review.googlesource.com/18690
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/link/internal/ld/elf.go

index a34cf3cac865e72c16cabc2d5ef021ca3f2ab5ac..6d34978d5a0c0b9a790a5a3df3266ed977f44b42 100644 (file)
@@ -850,7 +850,26 @@ func Elfinit() {
        }
 }
 
+// Make sure PT_LOAD is aligned properly and
+// that there is no gap,
+// correct ELF loaders will do this implicitly,
+// but buggy ELF loaders like the one in some
+// versions of QEMU and UPX won't.
+func fixElfPhdr(e *ElfPhdr) {
+       frag := int(e.vaddr & (e.align - 1))
+
+       e.off -= uint64(frag)
+       e.vaddr -= uint64(frag)
+       e.paddr -= uint64(frag)
+       e.filesz += uint64(frag)
+       e.memsz += uint64(frag)
+}
+
 func elf64phdr(e *ElfPhdr) {
+       if e.type_ == PT_LOAD {
+               fixElfPhdr(e)
+       }
+
        Thearch.Lput(e.type_)
        Thearch.Lput(e.flags)
        Thearch.Vput(e.off)
@@ -863,16 +882,7 @@ func elf64phdr(e *ElfPhdr) {
 
 func elf32phdr(e *ElfPhdr) {
        if e.type_ == PT_LOAD {
-               // Correct ELF loaders will do this implicitly,
-               // but buggy ELF loaders like the one in some
-               // versions of QEMU won't.
-               frag := int(e.vaddr & (e.align - 1))
-
-               e.off -= uint64(frag)
-               e.vaddr -= uint64(frag)
-               e.paddr -= uint64(frag)
-               e.filesz += uint64(frag)
-               e.memsz += uint64(frag)
+               fixElfPhdr(e)
        }
 
        Thearch.Lput(e.type_)