about summary refs log tree commit diff
path: root/qemu_mode/patches/elfload.diff
blob: 34ec4847c4b66369e581e1966080f5a008535e28 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
--- qemu-2.10.0.orig/linux-user/elfload.c	2017-08-30 18:50:41.000000000 +0200
+++ qemu-2.10.0/linux-user/elfload.c	2018-10-23 12:48:16.421879765 +0200
@@ -20,6 +20,8 @@
 
 #define ELF_OSABI   ELFOSABI_SYSV
 
+extern abi_ulong afl_entry_point, afl_start_code, afl_end_code;
+
 /* from personality.h */
 
 /*
@@ -2085,6 +2087,8 @@
     info->brk = 0;
     info->elf_flags = ehdr->e_flags;
 
+    if (!afl_entry_point) afl_entry_point = info->entry;
+
     for (i = 0; i < ehdr->e_phnum; i++) {
         struct elf_phdr *eppnt = phdr + i;
         if (eppnt->p_type == PT_LOAD) {
@@ -2118,9 +2122,11 @@
             if (elf_prot & PROT_EXEC) {
                 if (vaddr < info->start_code) {
                     info->start_code = vaddr;
+                    if (!afl_start_code) afl_start_code = vaddr;
                 }
                 if (vaddr_ef > info->end_code) {
                     info->end_code = vaddr_ef;
+                    if (!afl_end_code) afl_end_code = vaddr_ef;
                 }
             }
             if (elf_prot & PROT_WRITE) {
@@ -2443,6 +2449,22 @@
                                 info, (elf_interpreter ? &interp_info : NULL));
     info->start_stack = bprm->p;
 
+#if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
+    // On PowerPC64 the entry point is the _function descriptor_
+    // of the entry function. For AFL to properly initialize,
+    // afl_entry_point needs to be set to the actual first instruction
+    // as opposed executed by the target program. This as opposed to 
+    // where the function's descriptor sits in memory.
+    
+    // Shameless copy of PPC init_thread
+    info_report("Adjusting afl_entry_point");
+    if (afl_entry_point && (get_ppc64_abi(info) < 2)) {
+        uint64_t val;
+        get_user_u64(val, afl_entry_point);
+        afl_entry_point = val + info->load_bias;
+    }
+#endif
+
     /* If we have an interpreter, set that as the program's entry point.
        Copy the load_bias as well, to help PPC64 interpret the entry
        point as a function descriptor.  Do this after creating elf tables