2 Declaration for Linux kernel compatibility
10 #include <sys/ptrace.h>
15 char *victim_exe
= NULL
;
17 #define TRAP_BIT (0x80000000)
19 static struct nlist
*exe_nlist
;
20 static int exe_nlist_n
;
22 /* unsigned long __get_free_page(int type) { assert(0); } */
23 /* void *kmalloc(size_t size, int type) { assert(0); } */
24 void free_page(unsigned long page
) { assert(0); }
25 /* void kfree(void *mem) { assert(0); } */
26 void vfree(void *mem
) { assert(0); }
28 size_t strncpy_from_user(char *addr
, const char *user_name
, size_t size
)
31 /* void lock_kernel(void) { assert(0); } */
32 /* void unlock_kernel(void) { assert(0); } */
33 void __asm__(char *str
) { assert(0); }
35 extern void *__vmalloc(unsigned long size
, int gfp_mask
, pgprot_t prot
)
39 void kallsyms_sections(void *infop
,
40 int (*fp
)(void *token
, const char *modname
, const char *secname
,
41 ElfW(Addr
) secstart
, ElfW(Addr
) secend
, ElfW(Word
) secflags
))
45 unsigned long __generic_copy_to_user(void *x
, const void *y
, unsigned long z
)
47 unsigned long __generic_copy_from_user(void *x
, const void *y
, unsigned long z
)
50 /* void read_lock(struct lock *lock) { assert(0); } */
51 /* void read_unlock(struct lock *lock) { assert(0); } */
52 void udelay(unsigned long usecs
) { assert(0); }
53 int copy_to_user(void * result_record
, void *res
, size_t size
)
55 memcpy(result_record
, res
, size
);
59 void panic(char *str
) { assert(0); }
61 void printk(char *fmt
, ...)
66 vfprintf(stderr
, fmt
, ap
);
70 int kallsyms_address_to_symbol(db_expr_t off
,
71 const char * *mod_name
, unsigned long *mod_start
, unsigned long *mod_end
,
72 const char * *sec_name
, unsigned long *sec_start
, unsigned long *sec_end
,
73 const char * *sym_name
, unsigned long *sym_start
, unsigned long *sym_end
)
75 static char name
[sizeof(((struct nlist
*)0)->n_name
)+1];
78 unsigned long btext
, etext
;
79 struct nlist
*below
, *above
;
82 load_nlist(victim_exe
, &btext
, &etext
);
84 for (i
= 0; i
<exe_nlist_n
; i
++)
86 if ((exe_nlist
[i
].n_sclass
& N_SECT
) != N_TEXT
)
88 if (exe_nlist
[i
].n_value
<= off
)
90 if (!below
|| exe_nlist
[i
].n_value
> below
->n_value
)
93 if (exe_nlist
[i
].n_value
> off
)
95 if (!above
|| exe_nlist
[i
].n_value
< above
->n_value
)
102 printf("found '%.*s' at 0x%x\n", sizeof(below
->n_name
),
103 below
->n_name
, below
->n_value
);
107 printf("found '%.*s' at 0x%x\n", sizeof(above
->n_name
),
108 above
->n_name
, above
->n_value
);
115 *mod_name
= victim_exe
;
122 assert(below
&& above
);
124 memcpy(name
, below
->n_name
, sizeof(below
->n_name
));
125 name
[sizeof(below
->n_name
)]= '\0';
128 *sym_start
= below
->n_value
| TRAP_BIT
;
129 *sym_end
= above
->n_value
| TRAP_BIT
;
134 struct module
*module_list
;
135 struct task_struct
*task_list
;
136 struct lock tasklist_lock
;
138 unsigned long text_read_ul(void *addr
)
143 for (i
= 0; i
<sizeof(value
); i
++)
145 ((unsigned char *)&value
)[i
]= text_read_ub((char *)addr
+i
);
150 unsigned char text_read_ub(void *addr
)
155 vaddr
= (unsigned long)addr
;
157 v
= ptrace(T_READB_INS
, victim_pid
, vaddr
, 0);
161 "text_read_ub: trace T_READB_INS failed on pid %d, addr 0x%x: %s\n",
162 victim_pid
, vaddr
, strerror(errno
));
168 void text_write_ul(void *addr
, unsigned long value
)
172 for (i
= 0; i
<sizeof(value
); i
++)
174 text_write_ub((char *)addr
+i
, ((unsigned char *)&value
)[i
]);
178 void text_write_ub(void *addr
, unsigned char value
)
183 vaddr
= (unsigned long)addr
;
185 v
= ptrace(T_WRITEB_INS
, victim_pid
, vaddr
, value
);
189 "text_read_ub: trace T_WRITEB_INS failed on pid %d, addr 0x%x: %s\n",
190 victim_pid
, vaddr
, strerror(errno
));
195 void load_nlist(exe_name
, btextp
, etextp
)
197 unsigned long *btextp
;
198 unsigned long *etextp
;
201 unsigned long btext
, etext
;
205 exe_nlist_n
= read_nlist(exe_name
, &exe_nlist
);
206 if (exe_nlist_n
<= 0)
208 if (exe_nlist_n
== -1)
211 "error reading name list from '%s': %s\n",
212 exe_name
, strerror(errno
));
215 fprintf(stderr
, "no name list in '%s'\n",
221 if (!btextp
&& !etextp
)
225 btext
= (unsigned long)-1;
226 for (i
= 0; i
<exe_nlist_n
; i
++)
228 if ((exe_nlist
[i
].n_sclass
& N_SECT
) != N_TEXT
)
230 if (exe_nlist
[i
].n_value
< btext
)
231 btext
= exe_nlist
[i
].n_value
;
232 if (exe_nlist
[i
].n_value
> etext
)
233 etext
= exe_nlist
[i
].n_value
;
238 fprintf(stderr
, "Bad btext (0x%x) or etext (0x%x) in %d\n",
239 btext
, etext
, exe_name
);