]>
Commit | Line | Data |
---|---|---|
31e31b8a | 1 | /* This is the Linux kernel elf-loading code, ported into user space */ |
edf8e2af MW |
2 | #include <sys/time.h> |
3 | #include <sys/param.h> | |
31e31b8a FB |
4 | |
5 | #include <stdio.h> | |
6 | #include <sys/types.h> | |
7 | #include <fcntl.h> | |
31e31b8a FB |
8 | #include <errno.h> |
9 | #include <unistd.h> | |
10 | #include <sys/mman.h> | |
edf8e2af | 11 | #include <sys/resource.h> |
31e31b8a FB |
12 | #include <stdlib.h> |
13 | #include <string.h> | |
edf8e2af | 14 | #include <time.h> |
31e31b8a | 15 | |
3ef693a0 | 16 | #include "qemu.h" |
76cad711 | 17 | #include "disas/disas.h" |
31e31b8a | 18 | |
e58ffeb3 | 19 | #ifdef _ARCH_PPC64 |
a6cc84f4 | 20 | #undef ARCH_DLINFO |
21 | #undef ELF_PLATFORM | |
22 | #undef ELF_HWCAP | |
ad6919dc | 23 | #undef ELF_HWCAP2 |
a6cc84f4 | 24 | #undef ELF_CLASS |
25 | #undef ELF_DATA | |
26 | #undef ELF_ARCH | |
27 | #endif | |
28 | ||
edf8e2af MW |
29 | #define ELF_OSABI ELFOSABI_SYSV |
30 | ||
cb33da57 BS |
31 | /* from personality.h */ |
32 | ||
33 | /* | |
34 | * Flags for bug emulation. | |
35 | * | |
36 | * These occupy the top three bytes. | |
37 | */ | |
38 | enum { | |
d97ef72e RH |
39 | ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */ |
40 | FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to | |
41 | descriptors (signal handling) */ | |
42 | MMAP_PAGE_ZERO = 0x0100000, | |
43 | ADDR_COMPAT_LAYOUT = 0x0200000, | |
44 | READ_IMPLIES_EXEC = 0x0400000, | |
45 | ADDR_LIMIT_32BIT = 0x0800000, | |
46 | SHORT_INODE = 0x1000000, | |
47 | WHOLE_SECONDS = 0x2000000, | |
48 | STICKY_TIMEOUTS = 0x4000000, | |
49 | ADDR_LIMIT_3GB = 0x8000000, | |
cb33da57 BS |
50 | }; |
51 | ||
52 | /* | |
53 | * Personality types. | |
54 | * | |
55 | * These go in the low byte. Avoid using the top bit, it will | |
56 | * conflict with error returns. | |
57 | */ | |
58 | enum { | |
d97ef72e RH |
59 | PER_LINUX = 0x0000, |
60 | PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT, | |
61 | PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS, | |
62 | PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO, | |
63 | PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE, | |
64 | PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE, | |
65 | PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS, | |
66 | PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE, | |
67 | PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS, | |
68 | PER_BSD = 0x0006, | |
69 | PER_SUNOS = 0x0006 | STICKY_TIMEOUTS, | |
70 | PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE, | |
71 | PER_LINUX32 = 0x0008, | |
72 | PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB, | |
73 | PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */ | |
74 | PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */ | |
75 | PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */ | |
76 | PER_RISCOS = 0x000c, | |
77 | PER_SOLARIS = 0x000d | STICKY_TIMEOUTS, | |
78 | PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO, | |
79 | PER_OSF4 = 0x000f, /* OSF/1 v4 */ | |
80 | PER_HPUX = 0x0010, | |
81 | PER_MASK = 0x00ff, | |
cb33da57 BS |
82 | }; |
83 | ||
84 | /* | |
85 | * Return the base personality without flags. | |
86 | */ | |
d97ef72e | 87 | #define personality(pers) (pers & PER_MASK) |
cb33da57 | 88 | |
83fb7adf FB |
89 | /* this flag is uneffective under linux too, should be deleted */ |
90 | #ifndef MAP_DENYWRITE | |
91 | #define MAP_DENYWRITE 0 | |
92 | #endif | |
93 | ||
94 | /* should probably go in elf.h */ | |
95 | #ifndef ELIBBAD | |
96 | #define ELIBBAD 80 | |
97 | #endif | |
98 | ||
28490231 RH |
99 | #ifdef TARGET_WORDS_BIGENDIAN |
100 | #define ELF_DATA ELFDATA2MSB | |
101 | #else | |
102 | #define ELF_DATA ELFDATA2LSB | |
103 | #endif | |
104 | ||
a29f998d | 105 | #ifdef TARGET_ABI_MIPSN32 |
918fc54c PB |
106 | typedef abi_ullong target_elf_greg_t; |
107 | #define tswapreg(ptr) tswap64(ptr) | |
a29f998d PB |
108 | #else |
109 | typedef abi_ulong target_elf_greg_t; | |
110 | #define tswapreg(ptr) tswapal(ptr) | |
111 | #endif | |
112 | ||
21e807fa | 113 | #ifdef USE_UID16 |
1ddd592f PB |
114 | typedef abi_ushort target_uid_t; |
115 | typedef abi_ushort target_gid_t; | |
21e807fa | 116 | #else |
f8fd4fc4 PB |
117 | typedef abi_uint target_uid_t; |
118 | typedef abi_uint target_gid_t; | |
21e807fa | 119 | #endif |
f8fd4fc4 | 120 | typedef abi_int target_pid_t; |
21e807fa | 121 | |
30ac07d4 FB |
122 | #ifdef TARGET_I386 |
123 | ||
15338fd7 FB |
124 | #define ELF_PLATFORM get_elf_platform() |
125 | ||
126 | static const char *get_elf_platform(void) | |
127 | { | |
128 | static char elf_platform[] = "i386"; | |
a2247f8e | 129 | int family = object_property_get_int(OBJECT(thread_cpu), "family", NULL); |
15338fd7 FB |
130 | if (family > 6) |
131 | family = 6; | |
132 | if (family >= 3) | |
133 | elf_platform[1] = '0' + family; | |
134 | return elf_platform; | |
135 | } | |
136 | ||
137 | #define ELF_HWCAP get_elf_hwcap() | |
138 | ||
139 | static uint32_t get_elf_hwcap(void) | |
140 | { | |
a2247f8e AF |
141 | X86CPU *cpu = X86_CPU(thread_cpu); |
142 | ||
143 | return cpu->env.features[FEAT_1_EDX]; | |
15338fd7 FB |
144 | } |
145 | ||
84409ddb JM |
146 | #ifdef TARGET_X86_64 |
147 | #define ELF_START_MMAP 0x2aaaaab000ULL | |
148 | #define elf_check_arch(x) ( ((x) == ELF_ARCH) ) | |
149 | ||
150 | #define ELF_CLASS ELFCLASS64 | |
84409ddb JM |
151 | #define ELF_ARCH EM_X86_64 |
152 | ||
153 | static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) | |
154 | { | |
155 | regs->rax = 0; | |
156 | regs->rsp = infop->start_stack; | |
157 | regs->rip = infop->entry; | |
158 | } | |
159 | ||
9edc5d79 | 160 | #define ELF_NREG 27 |
c227f099 | 161 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; |
9edc5d79 MW |
162 | |
163 | /* | |
164 | * Note that ELF_NREG should be 29 as there should be place for | |
165 | * TRAPNO and ERR "registers" as well but linux doesn't dump | |
166 | * those. | |
167 | * | |
168 | * See linux kernel: arch/x86/include/asm/elf.h | |
169 | */ | |
05390248 | 170 | static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env) |
9edc5d79 MW |
171 | { |
172 | (*regs)[0] = env->regs[15]; | |
173 | (*regs)[1] = env->regs[14]; | |
174 | (*regs)[2] = env->regs[13]; | |
175 | (*regs)[3] = env->regs[12]; | |
176 | (*regs)[4] = env->regs[R_EBP]; | |
177 | (*regs)[5] = env->regs[R_EBX]; | |
178 | (*regs)[6] = env->regs[11]; | |
179 | (*regs)[7] = env->regs[10]; | |
180 | (*regs)[8] = env->regs[9]; | |
181 | (*regs)[9] = env->regs[8]; | |
182 | (*regs)[10] = env->regs[R_EAX]; | |
183 | (*regs)[11] = env->regs[R_ECX]; | |
184 | (*regs)[12] = env->regs[R_EDX]; | |
185 | (*regs)[13] = env->regs[R_ESI]; | |
186 | (*regs)[14] = env->regs[R_EDI]; | |
187 | (*regs)[15] = env->regs[R_EAX]; /* XXX */ | |
188 | (*regs)[16] = env->eip; | |
189 | (*regs)[17] = env->segs[R_CS].selector & 0xffff; | |
190 | (*regs)[18] = env->eflags; | |
191 | (*regs)[19] = env->regs[R_ESP]; | |
192 | (*regs)[20] = env->segs[R_SS].selector & 0xffff; | |
193 | (*regs)[21] = env->segs[R_FS].selector & 0xffff; | |
194 | (*regs)[22] = env->segs[R_GS].selector & 0xffff; | |
195 | (*regs)[23] = env->segs[R_DS].selector & 0xffff; | |
196 | (*regs)[24] = env->segs[R_ES].selector & 0xffff; | |
197 | (*regs)[25] = env->segs[R_FS].selector & 0xffff; | |
198 | (*regs)[26] = env->segs[R_GS].selector & 0xffff; | |
199 | } | |
200 | ||
84409ddb JM |
201 | #else |
202 | ||
30ac07d4 FB |
203 | #define ELF_START_MMAP 0x80000000 |
204 | ||
30ac07d4 FB |
205 | /* |
206 | * This is used to ensure we don't load something for the wrong architecture. | |
207 | */ | |
208 | #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) ) | |
209 | ||
210 | /* | |
211 | * These are used to set parameters in the core dumps. | |
212 | */ | |
d97ef72e | 213 | #define ELF_CLASS ELFCLASS32 |
d97ef72e | 214 | #define ELF_ARCH EM_386 |
30ac07d4 | 215 | |
d97ef72e RH |
216 | static inline void init_thread(struct target_pt_regs *regs, |
217 | struct image_info *infop) | |
b346ff46 FB |
218 | { |
219 | regs->esp = infop->start_stack; | |
220 | regs->eip = infop->entry; | |
e5fe0c52 PB |
221 | |
222 | /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program | |
223 | starts %edx contains a pointer to a function which might be | |
224 | registered using `atexit'. This provides a mean for the | |
225 | dynamic linker to call DT_FINI functions for shared libraries | |
226 | that have been loaded before the code runs. | |
227 | ||
228 | A value of 0 tells we have no such handler. */ | |
229 | regs->edx = 0; | |
b346ff46 | 230 | } |
9edc5d79 | 231 | |
9edc5d79 | 232 | #define ELF_NREG 17 |
c227f099 | 233 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; |
9edc5d79 MW |
234 | |
235 | /* | |
236 | * Note that ELF_NREG should be 19 as there should be place for | |
237 | * TRAPNO and ERR "registers" as well but linux doesn't dump | |
238 | * those. | |
239 | * | |
240 | * See linux kernel: arch/x86/include/asm/elf.h | |
241 | */ | |
05390248 | 242 | static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env) |
9edc5d79 MW |
243 | { |
244 | (*regs)[0] = env->regs[R_EBX]; | |
245 | (*regs)[1] = env->regs[R_ECX]; | |
246 | (*regs)[2] = env->regs[R_EDX]; | |
247 | (*regs)[3] = env->regs[R_ESI]; | |
248 | (*regs)[4] = env->regs[R_EDI]; | |
249 | (*regs)[5] = env->regs[R_EBP]; | |
250 | (*regs)[6] = env->regs[R_EAX]; | |
251 | (*regs)[7] = env->segs[R_DS].selector & 0xffff; | |
252 | (*regs)[8] = env->segs[R_ES].selector & 0xffff; | |
253 | (*regs)[9] = env->segs[R_FS].selector & 0xffff; | |
254 | (*regs)[10] = env->segs[R_GS].selector & 0xffff; | |
255 | (*regs)[11] = env->regs[R_EAX]; /* XXX */ | |
256 | (*regs)[12] = env->eip; | |
257 | (*regs)[13] = env->segs[R_CS].selector & 0xffff; | |
258 | (*regs)[14] = env->eflags; | |
259 | (*regs)[15] = env->regs[R_ESP]; | |
260 | (*regs)[16] = env->segs[R_SS].selector & 0xffff; | |
261 | } | |
84409ddb | 262 | #endif |
b346ff46 | 263 | |
9edc5d79 | 264 | #define USE_ELF_CORE_DUMP |
d97ef72e | 265 | #define ELF_EXEC_PAGESIZE 4096 |
b346ff46 FB |
266 | |
267 | #endif | |
268 | ||
269 | #ifdef TARGET_ARM | |
270 | ||
24e76ff0 PM |
271 | #ifndef TARGET_AARCH64 |
272 | /* 32 bit ARM definitions */ | |
273 | ||
b346ff46 FB |
274 | #define ELF_START_MMAP 0x80000000 |
275 | ||
99033cae | 276 | #define elf_check_arch(x) ((x) == ELF_MACHINE) |
b346ff46 | 277 | |
99033cae | 278 | #define ELF_ARCH ELF_MACHINE |
d97ef72e | 279 | #define ELF_CLASS ELFCLASS32 |
b346ff46 | 280 | |
d97ef72e RH |
281 | static inline void init_thread(struct target_pt_regs *regs, |
282 | struct image_info *infop) | |
b346ff46 | 283 | { |
992f48a0 | 284 | abi_long stack = infop->start_stack; |
b346ff46 | 285 | memset(regs, 0, sizeof(*regs)); |
99033cae | 286 | |
b346ff46 | 287 | regs->ARM_cpsr = 0x10; |
0240ded8 | 288 | if (infop->entry & 1) |
d97ef72e | 289 | regs->ARM_cpsr |= CPSR_T; |
0240ded8 | 290 | regs->ARM_pc = infop->entry & 0xfffffffe; |
b346ff46 | 291 | regs->ARM_sp = infop->start_stack; |
2f619698 FB |
292 | /* FIXME - what to for failure of get_user()? */ |
293 | get_user_ual(regs->ARM_r2, stack + 8); /* envp */ | |
294 | get_user_ual(regs->ARM_r1, stack + 4); /* envp */ | |
a1516e92 | 295 | /* XXX: it seems that r0 is zeroed after ! */ |
e5fe0c52 PB |
296 | regs->ARM_r0 = 0; |
297 | /* For uClinux PIC binaries. */ | |
863cf0b7 | 298 | /* XXX: Linux does this only on ARM with no MMU (do we care ?) */ |
e5fe0c52 | 299 | regs->ARM_r10 = infop->start_data; |
b346ff46 FB |
300 | } |
301 | ||
edf8e2af | 302 | #define ELF_NREG 18 |
c227f099 | 303 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; |
edf8e2af | 304 | |
05390248 | 305 | static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env) |
edf8e2af | 306 | { |
86cd7b2d PB |
307 | (*regs)[0] = tswapreg(env->regs[0]); |
308 | (*regs)[1] = tswapreg(env->regs[1]); | |
309 | (*regs)[2] = tswapreg(env->regs[2]); | |
310 | (*regs)[3] = tswapreg(env->regs[3]); | |
311 | (*regs)[4] = tswapreg(env->regs[4]); | |
312 | (*regs)[5] = tswapreg(env->regs[5]); | |
313 | (*regs)[6] = tswapreg(env->regs[6]); | |
314 | (*regs)[7] = tswapreg(env->regs[7]); | |
315 | (*regs)[8] = tswapreg(env->regs[8]); | |
316 | (*regs)[9] = tswapreg(env->regs[9]); | |
317 | (*regs)[10] = tswapreg(env->regs[10]); | |
318 | (*regs)[11] = tswapreg(env->regs[11]); | |
319 | (*regs)[12] = tswapreg(env->regs[12]); | |
320 | (*regs)[13] = tswapreg(env->regs[13]); | |
321 | (*regs)[14] = tswapreg(env->regs[14]); | |
322 | (*regs)[15] = tswapreg(env->regs[15]); | |
323 | ||
324 | (*regs)[16] = tswapreg(cpsr_read((CPUARMState *)env)); | |
325 | (*regs)[17] = tswapreg(env->regs[0]); /* XXX */ | |
edf8e2af MW |
326 | } |
327 | ||
30ac07d4 | 328 | #define USE_ELF_CORE_DUMP |
d97ef72e | 329 | #define ELF_EXEC_PAGESIZE 4096 |
30ac07d4 | 330 | |
afce2927 FB |
331 | enum |
332 | { | |
d97ef72e RH |
333 | ARM_HWCAP_ARM_SWP = 1 << 0, |
334 | ARM_HWCAP_ARM_HALF = 1 << 1, | |
335 | ARM_HWCAP_ARM_THUMB = 1 << 2, | |
336 | ARM_HWCAP_ARM_26BIT = 1 << 3, | |
337 | ARM_HWCAP_ARM_FAST_MULT = 1 << 4, | |
338 | ARM_HWCAP_ARM_FPA = 1 << 5, | |
339 | ARM_HWCAP_ARM_VFP = 1 << 6, | |
340 | ARM_HWCAP_ARM_EDSP = 1 << 7, | |
341 | ARM_HWCAP_ARM_JAVA = 1 << 8, | |
342 | ARM_HWCAP_ARM_IWMMXT = 1 << 9, | |
43ce393e PM |
343 | ARM_HWCAP_ARM_CRUNCH = 1 << 10, |
344 | ARM_HWCAP_ARM_THUMBEE = 1 << 11, | |
345 | ARM_HWCAP_ARM_NEON = 1 << 12, | |
346 | ARM_HWCAP_ARM_VFPv3 = 1 << 13, | |
347 | ARM_HWCAP_ARM_VFPv3D16 = 1 << 14, | |
24682654 PM |
348 | ARM_HWCAP_ARM_TLS = 1 << 15, |
349 | ARM_HWCAP_ARM_VFPv4 = 1 << 16, | |
350 | ARM_HWCAP_ARM_IDIVA = 1 << 17, | |
351 | ARM_HWCAP_ARM_IDIVT = 1 << 18, | |
352 | ARM_HWCAP_ARM_VFPD32 = 1 << 19, | |
353 | ARM_HWCAP_ARM_LPAE = 1 << 20, | |
354 | ARM_HWCAP_ARM_EVTSTRM = 1 << 21, | |
afce2927 FB |
355 | }; |
356 | ||
ad6919dc PM |
357 | enum { |
358 | ARM_HWCAP2_ARM_AES = 1 << 0, | |
359 | ARM_HWCAP2_ARM_PMULL = 1 << 1, | |
360 | ARM_HWCAP2_ARM_SHA1 = 1 << 2, | |
361 | ARM_HWCAP2_ARM_SHA2 = 1 << 3, | |
362 | ARM_HWCAP2_ARM_CRC32 = 1 << 4, | |
363 | }; | |
364 | ||
6b1275ff PM |
365 | /* The commpage only exists for 32 bit kernels */ |
366 | ||
806d1021 MI |
367 | #define TARGET_HAS_VALIDATE_GUEST_SPACE |
368 | /* Return 1 if the proposed guest space is suitable for the guest. | |
369 | * Return 0 if the proposed guest space isn't suitable, but another | |
370 | * address space should be tried. | |
371 | * Return -1 if there is no way the proposed guest space can be | |
372 | * valid regardless of the base. | |
373 | * The guest code may leave a page mapped and populate it if the | |
374 | * address is suitable. | |
375 | */ | |
376 | static int validate_guest_space(unsigned long guest_base, | |
377 | unsigned long guest_size) | |
97cc7560 DDAG |
378 | { |
379 | unsigned long real_start, test_page_addr; | |
380 | ||
381 | /* We need to check that we can force a fault on access to the | |
382 | * commpage at 0xffff0fxx | |
383 | */ | |
384 | test_page_addr = guest_base + (0xffff0f00 & qemu_host_page_mask); | |
806d1021 MI |
385 | |
386 | /* If the commpage lies within the already allocated guest space, | |
387 | * then there is no way we can allocate it. | |
388 | */ | |
389 | if (test_page_addr >= guest_base | |
390 | && test_page_addr <= (guest_base + guest_size)) { | |
391 | return -1; | |
392 | } | |
393 | ||
97cc7560 DDAG |
394 | /* Note it needs to be writeable to let us initialise it */ |
395 | real_start = (unsigned long) | |
396 | mmap((void *)test_page_addr, qemu_host_page_size, | |
397 | PROT_READ | PROT_WRITE, | |
398 | MAP_ANONYMOUS | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
399 | ||
400 | /* If we can't map it then try another address */ | |
401 | if (real_start == -1ul) { | |
402 | return 0; | |
403 | } | |
404 | ||
405 | if (real_start != test_page_addr) { | |
406 | /* OS didn't put the page where we asked - unmap and reject */ | |
407 | munmap((void *)real_start, qemu_host_page_size); | |
408 | return 0; | |
409 | } | |
410 | ||
411 | /* Leave the page mapped | |
412 | * Populate it (mmap should have left it all 0'd) | |
413 | */ | |
414 | ||
415 | /* Kernel helper versions */ | |
416 | __put_user(5, (uint32_t *)g2h(0xffff0ffcul)); | |
417 | ||
418 | /* Now it's populated make it RO */ | |
419 | if (mprotect((void *)test_page_addr, qemu_host_page_size, PROT_READ)) { | |
420 | perror("Protecting guest commpage"); | |
421 | exit(-1); | |
422 | } | |
423 | ||
424 | return 1; /* All good */ | |
425 | } | |
adf050b1 BC |
426 | |
427 | #define ELF_HWCAP get_elf_hwcap() | |
ad6919dc | 428 | #define ELF_HWCAP2 get_elf_hwcap2() |
adf050b1 BC |
429 | |
430 | static uint32_t get_elf_hwcap(void) | |
431 | { | |
a2247f8e | 432 | ARMCPU *cpu = ARM_CPU(thread_cpu); |
adf050b1 BC |
433 | uint32_t hwcaps = 0; |
434 | ||
435 | hwcaps |= ARM_HWCAP_ARM_SWP; | |
436 | hwcaps |= ARM_HWCAP_ARM_HALF; | |
437 | hwcaps |= ARM_HWCAP_ARM_THUMB; | |
438 | hwcaps |= ARM_HWCAP_ARM_FAST_MULT; | |
adf050b1 BC |
439 | |
440 | /* probe for the extra features */ | |
441 | #define GET_FEATURE(feat, hwcap) \ | |
a2247f8e | 442 | do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0) |
24682654 PM |
443 | /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */ |
444 | GET_FEATURE(ARM_FEATURE_V5, ARM_HWCAP_ARM_EDSP); | |
adf050b1 BC |
445 | GET_FEATURE(ARM_FEATURE_VFP, ARM_HWCAP_ARM_VFP); |
446 | GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT); | |
447 | GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE); | |
448 | GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON); | |
449 | GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPv3); | |
24682654 PM |
450 | GET_FEATURE(ARM_FEATURE_V6K, ARM_HWCAP_ARM_TLS); |
451 | GET_FEATURE(ARM_FEATURE_VFP4, ARM_HWCAP_ARM_VFPv4); | |
452 | GET_FEATURE(ARM_FEATURE_ARM_DIV, ARM_HWCAP_ARM_IDIVA); | |
453 | GET_FEATURE(ARM_FEATURE_THUMB_DIV, ARM_HWCAP_ARM_IDIVT); | |
454 | /* All QEMU's VFPv3 CPUs have 32 registers, see VFP_DREG in translate.c. | |
455 | * Note that the ARM_HWCAP_ARM_VFPv3D16 bit is always the inverse of | |
456 | * ARM_HWCAP_ARM_VFPD32 (and so always clear for QEMU); it is unrelated | |
457 | * to our VFP_FP16 feature bit. | |
458 | */ | |
459 | GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPD32); | |
460 | GET_FEATURE(ARM_FEATURE_LPAE, ARM_HWCAP_ARM_LPAE); | |
adf050b1 BC |
461 | |
462 | return hwcaps; | |
463 | } | |
afce2927 | 464 | |
ad6919dc PM |
465 | static uint32_t get_elf_hwcap2(void) |
466 | { | |
467 | ARMCPU *cpu = ARM_CPU(thread_cpu); | |
468 | uint32_t hwcaps = 0; | |
469 | ||
470 | GET_FEATURE(ARM_FEATURE_V8_AES, ARM_HWCAP2_ARM_AES); | |
4e624eda | 471 | GET_FEATURE(ARM_FEATURE_V8_PMULL, ARM_HWCAP2_ARM_PMULL); |
f1ecb913 AB |
472 | GET_FEATURE(ARM_FEATURE_V8_SHA1, ARM_HWCAP2_ARM_SHA1); |
473 | GET_FEATURE(ARM_FEATURE_V8_SHA256, ARM_HWCAP2_ARM_SHA2); | |
ad6919dc PM |
474 | GET_FEATURE(ARM_FEATURE_CRC, ARM_HWCAP2_ARM_CRC32); |
475 | return hwcaps; | |
476 | } | |
477 | ||
478 | #undef GET_FEATURE | |
479 | ||
24e76ff0 PM |
480 | #else |
481 | /* 64 bit ARM definitions */ | |
482 | #define ELF_START_MMAP 0x80000000 | |
483 | ||
484 | #define elf_check_arch(x) ((x) == ELF_MACHINE) | |
485 | ||
486 | #define ELF_ARCH ELF_MACHINE | |
487 | #define ELF_CLASS ELFCLASS64 | |
488 | #define ELF_PLATFORM "aarch64" | |
489 | ||
490 | static inline void init_thread(struct target_pt_regs *regs, | |
491 | struct image_info *infop) | |
492 | { | |
493 | abi_long stack = infop->start_stack; | |
494 | memset(regs, 0, sizeof(*regs)); | |
495 | ||
496 | regs->pc = infop->entry & ~0x3ULL; | |
497 | regs->sp = stack; | |
498 | } | |
499 | ||
500 | #define ELF_NREG 34 | |
501 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; | |
502 | ||
503 | static void elf_core_copy_regs(target_elf_gregset_t *regs, | |
504 | const CPUARMState *env) | |
505 | { | |
506 | int i; | |
507 | ||
508 | for (i = 0; i < 32; i++) { | |
509 | (*regs)[i] = tswapreg(env->xregs[i]); | |
510 | } | |
511 | (*regs)[32] = tswapreg(env->pc); | |
512 | (*regs)[33] = tswapreg(pstate_read((CPUARMState *)env)); | |
513 | } | |
514 | ||
515 | #define USE_ELF_CORE_DUMP | |
516 | #define ELF_EXEC_PAGESIZE 4096 | |
517 | ||
518 | enum { | |
519 | ARM_HWCAP_A64_FP = 1 << 0, | |
520 | ARM_HWCAP_A64_ASIMD = 1 << 1, | |
521 | ARM_HWCAP_A64_EVTSTRM = 1 << 2, | |
522 | ARM_HWCAP_A64_AES = 1 << 3, | |
523 | ARM_HWCAP_A64_PMULL = 1 << 4, | |
524 | ARM_HWCAP_A64_SHA1 = 1 << 5, | |
525 | ARM_HWCAP_A64_SHA2 = 1 << 6, | |
526 | ARM_HWCAP_A64_CRC32 = 1 << 7, | |
527 | }; | |
528 | ||
529 | #define ELF_HWCAP get_elf_hwcap() | |
530 | ||
531 | static uint32_t get_elf_hwcap(void) | |
532 | { | |
533 | ARMCPU *cpu = ARM_CPU(thread_cpu); | |
534 | uint32_t hwcaps = 0; | |
535 | ||
536 | hwcaps |= ARM_HWCAP_A64_FP; | |
537 | hwcaps |= ARM_HWCAP_A64_ASIMD; | |
538 | ||
539 | /* probe for the extra features */ | |
540 | #define GET_FEATURE(feat, hwcap) \ | |
541 | do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0) | |
5acc765c | 542 | GET_FEATURE(ARM_FEATURE_V8_AES, ARM_HWCAP_A64_AES); |
411bdc78 | 543 | GET_FEATURE(ARM_FEATURE_V8_PMULL, ARM_HWCAP_A64_PMULL); |
f6fe04d5 PM |
544 | GET_FEATURE(ARM_FEATURE_V8_SHA1, ARM_HWCAP_A64_SHA1); |
545 | GET_FEATURE(ARM_FEATURE_V8_SHA256, ARM_HWCAP_A64_SHA2); | |
130f2e7d | 546 | GET_FEATURE(ARM_FEATURE_CRC, ARM_HWCAP_A64_CRC32); |
24e76ff0 PM |
547 | #undef GET_FEATURE |
548 | ||
549 | return hwcaps; | |
550 | } | |
551 | ||
552 | #endif /* not TARGET_AARCH64 */ | |
553 | #endif /* TARGET_ARM */ | |
30ac07d4 | 554 | |
d2fbca94 GX |
555 | #ifdef TARGET_UNICORE32 |
556 | ||
557 | #define ELF_START_MMAP 0x80000000 | |
558 | ||
559 | #define elf_check_arch(x) ((x) == EM_UNICORE32) | |
560 | ||
561 | #define ELF_CLASS ELFCLASS32 | |
562 | #define ELF_DATA ELFDATA2LSB | |
563 | #define ELF_ARCH EM_UNICORE32 | |
564 | ||
565 | static inline void init_thread(struct target_pt_regs *regs, | |
566 | struct image_info *infop) | |
567 | { | |
568 | abi_long stack = infop->start_stack; | |
569 | memset(regs, 0, sizeof(*regs)); | |
570 | regs->UC32_REG_asr = 0x10; | |
571 | regs->UC32_REG_pc = infop->entry & 0xfffffffe; | |
572 | regs->UC32_REG_sp = infop->start_stack; | |
573 | /* FIXME - what to for failure of get_user()? */ | |
574 | get_user_ual(regs->UC32_REG_02, stack + 8); /* envp */ | |
575 | get_user_ual(regs->UC32_REG_01, stack + 4); /* envp */ | |
576 | /* XXX: it seems that r0 is zeroed after ! */ | |
577 | regs->UC32_REG_00 = 0; | |
578 | } | |
579 | ||
580 | #define ELF_NREG 34 | |
581 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; | |
582 | ||
05390248 | 583 | static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUUniCore32State *env) |
d2fbca94 GX |
584 | { |
585 | (*regs)[0] = env->regs[0]; | |
586 | (*regs)[1] = env->regs[1]; | |
587 | (*regs)[2] = env->regs[2]; | |
588 | (*regs)[3] = env->regs[3]; | |
589 | (*regs)[4] = env->regs[4]; | |
590 | (*regs)[5] = env->regs[5]; | |
591 | (*regs)[6] = env->regs[6]; | |
592 | (*regs)[7] = env->regs[7]; | |
593 | (*regs)[8] = env->regs[8]; | |
594 | (*regs)[9] = env->regs[9]; | |
595 | (*regs)[10] = env->regs[10]; | |
596 | (*regs)[11] = env->regs[11]; | |
597 | (*regs)[12] = env->regs[12]; | |
598 | (*regs)[13] = env->regs[13]; | |
599 | (*regs)[14] = env->regs[14]; | |
600 | (*regs)[15] = env->regs[15]; | |
601 | (*regs)[16] = env->regs[16]; | |
602 | (*regs)[17] = env->regs[17]; | |
603 | (*regs)[18] = env->regs[18]; | |
604 | (*regs)[19] = env->regs[19]; | |
605 | (*regs)[20] = env->regs[20]; | |
606 | (*regs)[21] = env->regs[21]; | |
607 | (*regs)[22] = env->regs[22]; | |
608 | (*regs)[23] = env->regs[23]; | |
609 | (*regs)[24] = env->regs[24]; | |
610 | (*regs)[25] = env->regs[25]; | |
611 | (*regs)[26] = env->regs[26]; | |
612 | (*regs)[27] = env->regs[27]; | |
613 | (*regs)[28] = env->regs[28]; | |
614 | (*regs)[29] = env->regs[29]; | |
615 | (*regs)[30] = env->regs[30]; | |
616 | (*regs)[31] = env->regs[31]; | |
617 | ||
05390248 | 618 | (*regs)[32] = cpu_asr_read((CPUUniCore32State *)env); |
d2fbca94 GX |
619 | (*regs)[33] = env->regs[0]; /* XXX */ |
620 | } | |
621 | ||
622 | #define USE_ELF_CORE_DUMP | |
623 | #define ELF_EXEC_PAGESIZE 4096 | |
624 | ||
625 | #define ELF_HWCAP (UC32_HWCAP_CMOV | UC32_HWCAP_UCF64) | |
626 | ||
627 | #endif | |
628 | ||
853d6f7a | 629 | #ifdef TARGET_SPARC |
a315a145 | 630 | #ifdef TARGET_SPARC64 |
853d6f7a FB |
631 | |
632 | #define ELF_START_MMAP 0x80000000 | |
cf973e46 AT |
633 | #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \ |
634 | | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9) | |
992f48a0 | 635 | #ifndef TARGET_ABI32 |
cb33da57 | 636 | #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS ) |
992f48a0 BS |
637 | #else |
638 | #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC ) | |
639 | #endif | |
853d6f7a | 640 | |
a315a145 | 641 | #define ELF_CLASS ELFCLASS64 |
5ef54116 FB |
642 | #define ELF_ARCH EM_SPARCV9 |
643 | ||
d97ef72e | 644 | #define STACK_BIAS 2047 |
a315a145 | 645 | |
d97ef72e RH |
646 | static inline void init_thread(struct target_pt_regs *regs, |
647 | struct image_info *infop) | |
a315a145 | 648 | { |
992f48a0 | 649 | #ifndef TARGET_ABI32 |
a315a145 | 650 | regs->tstate = 0; |
992f48a0 | 651 | #endif |
a315a145 FB |
652 | regs->pc = infop->entry; |
653 | regs->npc = regs->pc + 4; | |
654 | regs->y = 0; | |
992f48a0 BS |
655 | #ifdef TARGET_ABI32 |
656 | regs->u_regs[14] = infop->start_stack - 16 * 4; | |
657 | #else | |
cb33da57 BS |
658 | if (personality(infop->personality) == PER_LINUX32) |
659 | regs->u_regs[14] = infop->start_stack - 16 * 4; | |
660 | else | |
661 | regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS; | |
992f48a0 | 662 | #endif |
a315a145 FB |
663 | } |
664 | ||
665 | #else | |
666 | #define ELF_START_MMAP 0x80000000 | |
cf973e46 AT |
667 | #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \ |
668 | | HWCAP_SPARC_MULDIV) | |
a315a145 FB |
669 | #define elf_check_arch(x) ( (x) == EM_SPARC ) |
670 | ||
853d6f7a | 671 | #define ELF_CLASS ELFCLASS32 |
853d6f7a FB |
672 | #define ELF_ARCH EM_SPARC |
673 | ||
d97ef72e RH |
674 | static inline void init_thread(struct target_pt_regs *regs, |
675 | struct image_info *infop) | |
853d6f7a | 676 | { |
f5155289 FB |
677 | regs->psr = 0; |
678 | regs->pc = infop->entry; | |
679 | regs->npc = regs->pc + 4; | |
680 | regs->y = 0; | |
681 | regs->u_regs[14] = infop->start_stack - 16 * 4; | |
853d6f7a FB |
682 | } |
683 | ||
a315a145 | 684 | #endif |
853d6f7a FB |
685 | #endif |
686 | ||
67867308 FB |
687 | #ifdef TARGET_PPC |
688 | ||
689 | #define ELF_START_MMAP 0x80000000 | |
690 | ||
e85e7c6e | 691 | #if defined(TARGET_PPC64) && !defined(TARGET_ABI32) |
84409ddb JM |
692 | |
693 | #define elf_check_arch(x) ( (x) == EM_PPC64 ) | |
694 | ||
d97ef72e | 695 | #define ELF_CLASS ELFCLASS64 |
84409ddb JM |
696 | |
697 | #else | |
698 | ||
67867308 FB |
699 | #define elf_check_arch(x) ( (x) == EM_PPC ) |
700 | ||
d97ef72e | 701 | #define ELF_CLASS ELFCLASS32 |
84409ddb JM |
702 | |
703 | #endif | |
704 | ||
d97ef72e | 705 | #define ELF_ARCH EM_PPC |
67867308 | 706 | |
df84e4f3 NF |
707 | /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP). |
708 | See arch/powerpc/include/asm/cputable.h. */ | |
709 | enum { | |
3efa9a67 | 710 | QEMU_PPC_FEATURE_32 = 0x80000000, |
711 | QEMU_PPC_FEATURE_64 = 0x40000000, | |
712 | QEMU_PPC_FEATURE_601_INSTR = 0x20000000, | |
713 | QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000, | |
714 | QEMU_PPC_FEATURE_HAS_FPU = 0x08000000, | |
715 | QEMU_PPC_FEATURE_HAS_MMU = 0x04000000, | |
716 | QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000, | |
717 | QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000, | |
718 | QEMU_PPC_FEATURE_HAS_SPE = 0x00800000, | |
719 | QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000, | |
720 | QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000, | |
721 | QEMU_PPC_FEATURE_NO_TB = 0x00100000, | |
722 | QEMU_PPC_FEATURE_POWER4 = 0x00080000, | |
723 | QEMU_PPC_FEATURE_POWER5 = 0x00040000, | |
724 | QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000, | |
725 | QEMU_PPC_FEATURE_CELL = 0x00010000, | |
726 | QEMU_PPC_FEATURE_BOOKE = 0x00008000, | |
727 | QEMU_PPC_FEATURE_SMT = 0x00004000, | |
728 | QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000, | |
729 | QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000, | |
730 | QEMU_PPC_FEATURE_PA6T = 0x00000800, | |
731 | QEMU_PPC_FEATURE_HAS_DFP = 0x00000400, | |
732 | QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200, | |
733 | QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100, | |
734 | QEMU_PPC_FEATURE_HAS_VSX = 0x00000080, | |
735 | QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040, | |
736 | ||
737 | QEMU_PPC_FEATURE_TRUE_LE = 0x00000002, | |
738 | QEMU_PPC_FEATURE_PPC_LE = 0x00000001, | |
df84e4f3 NF |
739 | }; |
740 | ||
741 | #define ELF_HWCAP get_elf_hwcap() | |
742 | ||
743 | static uint32_t get_elf_hwcap(void) | |
744 | { | |
a2247f8e | 745 | PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); |
df84e4f3 NF |
746 | uint32_t features = 0; |
747 | ||
748 | /* We don't have to be terribly complete here; the high points are | |
749 | Altivec/FP/SPE support. Anything else is just a bonus. */ | |
d97ef72e | 750 | #define GET_FEATURE(flag, feature) \ |
a2247f8e | 751 | do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0) |
3efa9a67 | 752 | GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64); |
753 | GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU); | |
754 | GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC); | |
755 | GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE); | |
756 | GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE); | |
757 | GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE); | |
758 | GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE); | |
759 | GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC); | |
df84e4f3 NF |
760 | #undef GET_FEATURE |
761 | ||
762 | return features; | |
763 | } | |
764 | ||
f5155289 FB |
765 | /* |
766 | * The requirements here are: | |
767 | * - keep the final alignment of sp (sp & 0xf) | |
768 | * - make sure the 32-bit value at the first 16 byte aligned position of | |
769 | * AUXV is greater than 16 for glibc compatibility. | |
770 | * AT_IGNOREPPC is used for that. | |
771 | * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC, | |
772 | * even if DLINFO_ARCH_ITEMS goes to zero or is undefined. | |
773 | */ | |
0bccf03d | 774 | #define DLINFO_ARCH_ITEMS 5 |
d97ef72e RH |
775 | #define ARCH_DLINFO \ |
776 | do { \ | |
777 | NEW_AUX_ENT(AT_DCACHEBSIZE, 0x20); \ | |
778 | NEW_AUX_ENT(AT_ICACHEBSIZE, 0x20); \ | |
779 | NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \ | |
780 | /* \ | |
781 | * Now handle glibc compatibility. \ | |
782 | */ \ | |
783 | NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \ | |
784 | NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \ | |
785 | } while (0) | |
f5155289 | 786 | |
d90b94cd DK |
787 | static inline uint32_t get_ppc64_abi(struct image_info *infop); |
788 | ||
67867308 FB |
789 | static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop) |
790 | { | |
67867308 | 791 | _regs->gpr[1] = infop->start_stack; |
e85e7c6e | 792 | #if defined(TARGET_PPC64) && !defined(TARGET_ABI32) |
d90b94cd DK |
793 | if (get_ppc64_abi(infop) < 2) { |
794 | _regs->gpr[2] = ldq_raw(infop->entry + 8) + infop->load_bias; | |
795 | infop->entry = ldq_raw(infop->entry) + infop->load_bias; | |
796 | } else { | |
797 | _regs->gpr[12] = infop->entry; /* r12 set to global entry address */ | |
798 | } | |
84409ddb | 799 | #endif |
67867308 FB |
800 | _regs->nip = infop->entry; |
801 | } | |
802 | ||
e2f3e741 NF |
803 | /* See linux kernel: arch/powerpc/include/asm/elf.h. */ |
804 | #define ELF_NREG 48 | |
805 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; | |
806 | ||
05390248 | 807 | static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env) |
e2f3e741 NF |
808 | { |
809 | int i; | |
810 | target_ulong ccr = 0; | |
811 | ||
812 | for (i = 0; i < ARRAY_SIZE(env->gpr); i++) { | |
86cd7b2d | 813 | (*regs)[i] = tswapreg(env->gpr[i]); |
e2f3e741 NF |
814 | } |
815 | ||
86cd7b2d PB |
816 | (*regs)[32] = tswapreg(env->nip); |
817 | (*regs)[33] = tswapreg(env->msr); | |
818 | (*regs)[35] = tswapreg(env->ctr); | |
819 | (*regs)[36] = tswapreg(env->lr); | |
820 | (*regs)[37] = tswapreg(env->xer); | |
e2f3e741 NF |
821 | |
822 | for (i = 0; i < ARRAY_SIZE(env->crf); i++) { | |
823 | ccr |= env->crf[i] << (32 - ((i + 1) * 4)); | |
824 | } | |
86cd7b2d | 825 | (*regs)[38] = tswapreg(ccr); |
e2f3e741 NF |
826 | } |
827 | ||
828 | #define USE_ELF_CORE_DUMP | |
d97ef72e | 829 | #define ELF_EXEC_PAGESIZE 4096 |
67867308 FB |
830 | |
831 | #endif | |
832 | ||
048f6b4d FB |
833 | #ifdef TARGET_MIPS |
834 | ||
835 | #define ELF_START_MMAP 0x80000000 | |
836 | ||
837 | #define elf_check_arch(x) ( (x) == EM_MIPS ) | |
838 | ||
388bb21a TS |
839 | #ifdef TARGET_MIPS64 |
840 | #define ELF_CLASS ELFCLASS64 | |
841 | #else | |
048f6b4d | 842 | #define ELF_CLASS ELFCLASS32 |
388bb21a | 843 | #endif |
048f6b4d FB |
844 | #define ELF_ARCH EM_MIPS |
845 | ||
d97ef72e RH |
846 | static inline void init_thread(struct target_pt_regs *regs, |
847 | struct image_info *infop) | |
048f6b4d | 848 | { |
623a930e | 849 | regs->cp0_status = 2 << CP0St_KSU; |
048f6b4d FB |
850 | regs->cp0_epc = infop->entry; |
851 | regs->regs[29] = infop->start_stack; | |
852 | } | |
853 | ||
51e52606 NF |
854 | /* See linux kernel: arch/mips/include/asm/elf.h. */ |
855 | #define ELF_NREG 45 | |
856 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; | |
857 | ||
858 | /* See linux kernel: arch/mips/include/asm/reg.h. */ | |
859 | enum { | |
860 | #ifdef TARGET_MIPS64 | |
861 | TARGET_EF_R0 = 0, | |
862 | #else | |
863 | TARGET_EF_R0 = 6, | |
864 | #endif | |
865 | TARGET_EF_R26 = TARGET_EF_R0 + 26, | |
866 | TARGET_EF_R27 = TARGET_EF_R0 + 27, | |
867 | TARGET_EF_LO = TARGET_EF_R0 + 32, | |
868 | TARGET_EF_HI = TARGET_EF_R0 + 33, | |
869 | TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34, | |
870 | TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35, | |
871 | TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36, | |
872 | TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37 | |
873 | }; | |
874 | ||
875 | /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */ | |
05390248 | 876 | static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env) |
51e52606 NF |
877 | { |
878 | int i; | |
879 | ||
880 | for (i = 0; i < TARGET_EF_R0; i++) { | |
881 | (*regs)[i] = 0; | |
882 | } | |
883 | (*regs)[TARGET_EF_R0] = 0; | |
884 | ||
885 | for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) { | |
a29f998d | 886 | (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]); |
51e52606 NF |
887 | } |
888 | ||
889 | (*regs)[TARGET_EF_R26] = 0; | |
890 | (*regs)[TARGET_EF_R27] = 0; | |
a29f998d PB |
891 | (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]); |
892 | (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]); | |
893 | (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC); | |
894 | (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr); | |
895 | (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status); | |
896 | (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause); | |
51e52606 NF |
897 | } |
898 | ||
899 | #define USE_ELF_CORE_DUMP | |
388bb21a TS |
900 | #define ELF_EXEC_PAGESIZE 4096 |
901 | ||
048f6b4d FB |
902 | #endif /* TARGET_MIPS */ |
903 | ||
b779e29e EI |
904 | #ifdef TARGET_MICROBLAZE |
905 | ||
906 | #define ELF_START_MMAP 0x80000000 | |
907 | ||
0d5d4699 | 908 | #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD) |
b779e29e EI |
909 | |
910 | #define ELF_CLASS ELFCLASS32 | |
0d5d4699 | 911 | #define ELF_ARCH EM_MICROBLAZE |
b779e29e | 912 | |
d97ef72e RH |
913 | static inline void init_thread(struct target_pt_regs *regs, |
914 | struct image_info *infop) | |
b779e29e EI |
915 | { |
916 | regs->pc = infop->entry; | |
917 | regs->r1 = infop->start_stack; | |
918 | ||
919 | } | |
920 | ||
b779e29e EI |
921 | #define ELF_EXEC_PAGESIZE 4096 |
922 | ||
e4cbd44d EI |
923 | #define USE_ELF_CORE_DUMP |
924 | #define ELF_NREG 38 | |
925 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; | |
926 | ||
927 | /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */ | |
05390248 | 928 | static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env) |
e4cbd44d EI |
929 | { |
930 | int i, pos = 0; | |
931 | ||
932 | for (i = 0; i < 32; i++) { | |
86cd7b2d | 933 | (*regs)[pos++] = tswapreg(env->regs[i]); |
e4cbd44d EI |
934 | } |
935 | ||
936 | for (i = 0; i < 6; i++) { | |
86cd7b2d | 937 | (*regs)[pos++] = tswapreg(env->sregs[i]); |
e4cbd44d EI |
938 | } |
939 | } | |
940 | ||
b779e29e EI |
941 | #endif /* TARGET_MICROBLAZE */ |
942 | ||
d962783e JL |
943 | #ifdef TARGET_OPENRISC |
944 | ||
945 | #define ELF_START_MMAP 0x08000000 | |
946 | ||
947 | #define elf_check_arch(x) ((x) == EM_OPENRISC) | |
948 | ||
949 | #define ELF_ARCH EM_OPENRISC | |
950 | #define ELF_CLASS ELFCLASS32 | |
951 | #define ELF_DATA ELFDATA2MSB | |
952 | ||
953 | static inline void init_thread(struct target_pt_regs *regs, | |
954 | struct image_info *infop) | |
955 | { | |
956 | regs->pc = infop->entry; | |
957 | regs->gpr[1] = infop->start_stack; | |
958 | } | |
959 | ||
960 | #define USE_ELF_CORE_DUMP | |
961 | #define ELF_EXEC_PAGESIZE 8192 | |
962 | ||
963 | /* See linux kernel arch/openrisc/include/asm/elf.h. */ | |
964 | #define ELF_NREG 34 /* gprs and pc, sr */ | |
965 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; | |
966 | ||
967 | static void elf_core_copy_regs(target_elf_gregset_t *regs, | |
968 | const CPUOpenRISCState *env) | |
969 | { | |
970 | int i; | |
971 | ||
972 | for (i = 0; i < 32; i++) { | |
86cd7b2d | 973 | (*regs)[i] = tswapreg(env->gpr[i]); |
d962783e JL |
974 | } |
975 | ||
86cd7b2d PB |
976 | (*regs)[32] = tswapreg(env->pc); |
977 | (*regs)[33] = tswapreg(env->sr); | |
d962783e JL |
978 | } |
979 | #define ELF_HWCAP 0 | |
980 | #define ELF_PLATFORM NULL | |
981 | ||
982 | #endif /* TARGET_OPENRISC */ | |
983 | ||
fdf9b3e8 FB |
984 | #ifdef TARGET_SH4 |
985 | ||
986 | #define ELF_START_MMAP 0x80000000 | |
987 | ||
988 | #define elf_check_arch(x) ( (x) == EM_SH ) | |
989 | ||
990 | #define ELF_CLASS ELFCLASS32 | |
fdf9b3e8 FB |
991 | #define ELF_ARCH EM_SH |
992 | ||
d97ef72e RH |
993 | static inline void init_thread(struct target_pt_regs *regs, |
994 | struct image_info *infop) | |
fdf9b3e8 | 995 | { |
d97ef72e RH |
996 | /* Check other registers XXXXX */ |
997 | regs->pc = infop->entry; | |
998 | regs->regs[15] = infop->start_stack; | |
fdf9b3e8 FB |
999 | } |
1000 | ||
7631c97e NF |
1001 | /* See linux kernel: arch/sh/include/asm/elf.h. */ |
1002 | #define ELF_NREG 23 | |
1003 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; | |
1004 | ||
1005 | /* See linux kernel: arch/sh/include/asm/ptrace.h. */ | |
1006 | enum { | |
1007 | TARGET_REG_PC = 16, | |
1008 | TARGET_REG_PR = 17, | |
1009 | TARGET_REG_SR = 18, | |
1010 | TARGET_REG_GBR = 19, | |
1011 | TARGET_REG_MACH = 20, | |
1012 | TARGET_REG_MACL = 21, | |
1013 | TARGET_REG_SYSCALL = 22 | |
1014 | }; | |
1015 | ||
d97ef72e | 1016 | static inline void elf_core_copy_regs(target_elf_gregset_t *regs, |
05390248 | 1017 | const CPUSH4State *env) |
7631c97e NF |
1018 | { |
1019 | int i; | |
1020 | ||
1021 | for (i = 0; i < 16; i++) { | |
86cd7b2d | 1022 | (*regs[i]) = tswapreg(env->gregs[i]); |
7631c97e NF |
1023 | } |
1024 | ||
86cd7b2d PB |
1025 | (*regs)[TARGET_REG_PC] = tswapreg(env->pc); |
1026 | (*regs)[TARGET_REG_PR] = tswapreg(env->pr); | |
1027 | (*regs)[TARGET_REG_SR] = tswapreg(env->sr); | |
1028 | (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr); | |
1029 | (*regs)[TARGET_REG_MACH] = tswapreg(env->mach); | |
1030 | (*regs)[TARGET_REG_MACL] = tswapreg(env->macl); | |
7631c97e NF |
1031 | (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */ |
1032 | } | |
1033 | ||
1034 | #define USE_ELF_CORE_DUMP | |
fdf9b3e8 FB |
1035 | #define ELF_EXEC_PAGESIZE 4096 |
1036 | ||
1037 | #endif | |
1038 | ||
48733d19 TS |
1039 | #ifdef TARGET_CRIS |
1040 | ||
1041 | #define ELF_START_MMAP 0x80000000 | |
1042 | ||
1043 | #define elf_check_arch(x) ( (x) == EM_CRIS ) | |
1044 | ||
1045 | #define ELF_CLASS ELFCLASS32 | |
48733d19 TS |
1046 | #define ELF_ARCH EM_CRIS |
1047 | ||
d97ef72e RH |
1048 | static inline void init_thread(struct target_pt_regs *regs, |
1049 | struct image_info *infop) | |
48733d19 | 1050 | { |
d97ef72e | 1051 | regs->erp = infop->entry; |
48733d19 TS |
1052 | } |
1053 | ||
48733d19 TS |
1054 | #define ELF_EXEC_PAGESIZE 8192 |
1055 | ||
1056 | #endif | |
1057 | ||
e6e5906b PB |
1058 | #ifdef TARGET_M68K |
1059 | ||
1060 | #define ELF_START_MMAP 0x80000000 | |
1061 | ||
1062 | #define elf_check_arch(x) ( (x) == EM_68K ) | |
1063 | ||
d97ef72e | 1064 | #define ELF_CLASS ELFCLASS32 |
d97ef72e | 1065 | #define ELF_ARCH EM_68K |
e6e5906b PB |
1066 | |
1067 | /* ??? Does this need to do anything? | |
d97ef72e | 1068 | #define ELF_PLAT_INIT(_r) */ |
e6e5906b | 1069 | |
d97ef72e RH |
1070 | static inline void init_thread(struct target_pt_regs *regs, |
1071 | struct image_info *infop) | |
e6e5906b PB |
1072 | { |
1073 | regs->usp = infop->start_stack; | |
1074 | regs->sr = 0; | |
1075 | regs->pc = infop->entry; | |
1076 | } | |
1077 | ||
7a93cc55 NF |
1078 | /* See linux kernel: arch/m68k/include/asm/elf.h. */ |
1079 | #define ELF_NREG 20 | |
1080 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; | |
1081 | ||
05390248 | 1082 | static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env) |
7a93cc55 | 1083 | { |
86cd7b2d PB |
1084 | (*regs)[0] = tswapreg(env->dregs[1]); |
1085 | (*regs)[1] = tswapreg(env->dregs[2]); | |
1086 | (*regs)[2] = tswapreg(env->dregs[3]); | |
1087 | (*regs)[3] = tswapreg(env->dregs[4]); | |
1088 | (*regs)[4] = tswapreg(env->dregs[5]); | |
1089 | (*regs)[5] = tswapreg(env->dregs[6]); | |
1090 | (*regs)[6] = tswapreg(env->dregs[7]); | |
1091 | (*regs)[7] = tswapreg(env->aregs[0]); | |
1092 | (*regs)[8] = tswapreg(env->aregs[1]); | |
1093 | (*regs)[9] = tswapreg(env->aregs[2]); | |
1094 | (*regs)[10] = tswapreg(env->aregs[3]); | |
1095 | (*regs)[11] = tswapreg(env->aregs[4]); | |
1096 | (*regs)[12] = tswapreg(env->aregs[5]); | |
1097 | (*regs)[13] = tswapreg(env->aregs[6]); | |
1098 | (*regs)[14] = tswapreg(env->dregs[0]); | |
1099 | (*regs)[15] = tswapreg(env->aregs[7]); | |
1100 | (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */ | |
1101 | (*regs)[17] = tswapreg(env->sr); | |
1102 | (*regs)[18] = tswapreg(env->pc); | |
7a93cc55 NF |
1103 | (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */ |
1104 | } | |
1105 | ||
1106 | #define USE_ELF_CORE_DUMP | |
d97ef72e | 1107 | #define ELF_EXEC_PAGESIZE 8192 |
e6e5906b PB |
1108 | |
1109 | #endif | |
1110 | ||
7a3148a9 JM |
1111 | #ifdef TARGET_ALPHA |
1112 | ||
1113 | #define ELF_START_MMAP (0x30000000000ULL) | |
1114 | ||
1115 | #define elf_check_arch(x) ( (x) == ELF_ARCH ) | |
1116 | ||
1117 | #define ELF_CLASS ELFCLASS64 | |
7a3148a9 JM |
1118 | #define ELF_ARCH EM_ALPHA |
1119 | ||
d97ef72e RH |
1120 | static inline void init_thread(struct target_pt_regs *regs, |
1121 | struct image_info *infop) | |
7a3148a9 JM |
1122 | { |
1123 | regs->pc = infop->entry; | |
1124 | regs->ps = 8; | |
1125 | regs->usp = infop->start_stack; | |
7a3148a9 JM |
1126 | } |
1127 | ||
7a3148a9 JM |
1128 | #define ELF_EXEC_PAGESIZE 8192 |
1129 | ||
1130 | #endif /* TARGET_ALPHA */ | |
1131 | ||
a4c075f1 UH |
1132 | #ifdef TARGET_S390X |
1133 | ||
1134 | #define ELF_START_MMAP (0x20000000000ULL) | |
1135 | ||
1136 | #define elf_check_arch(x) ( (x) == ELF_ARCH ) | |
1137 | ||
1138 | #define ELF_CLASS ELFCLASS64 | |
1139 | #define ELF_DATA ELFDATA2MSB | |
1140 | #define ELF_ARCH EM_S390 | |
1141 | ||
1142 | static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) | |
1143 | { | |
1144 | regs->psw.addr = infop->entry; | |
1145 | regs->psw.mask = PSW_MASK_64 | PSW_MASK_32; | |
1146 | regs->gprs[15] = infop->start_stack; | |
1147 | } | |
1148 | ||
1149 | #endif /* TARGET_S390X */ | |
1150 | ||
15338fd7 FB |
1151 | #ifndef ELF_PLATFORM |
1152 | #define ELF_PLATFORM (NULL) | |
1153 | #endif | |
1154 | ||
1155 | #ifndef ELF_HWCAP | |
1156 | #define ELF_HWCAP 0 | |
1157 | #endif | |
1158 | ||
992f48a0 | 1159 | #ifdef TARGET_ABI32 |
cb33da57 | 1160 | #undef ELF_CLASS |
992f48a0 | 1161 | #define ELF_CLASS ELFCLASS32 |
cb33da57 BS |
1162 | #undef bswaptls |
1163 | #define bswaptls(ptr) bswap32s(ptr) | |
1164 | #endif | |
1165 | ||
31e31b8a | 1166 | #include "elf.h" |
09bfb054 | 1167 | |
d90b94cd DK |
1168 | #ifdef TARGET_PPC |
1169 | static inline uint32_t get_ppc64_abi(struct image_info *infop) | |
1170 | { | |
1171 | return infop->elf_flags & EF_PPC64_ABI; | |
1172 | } | |
1173 | #endif | |
1174 | ||
09bfb054 FB |
1175 | struct exec |
1176 | { | |
d97ef72e RH |
1177 | unsigned int a_info; /* Use macros N_MAGIC, etc for access */ |
1178 | unsigned int a_text; /* length of text, in bytes */ | |
1179 | unsigned int a_data; /* length of data, in bytes */ | |
1180 | unsigned int a_bss; /* length of uninitialized data area, in bytes */ | |
1181 | unsigned int a_syms; /* length of symbol table data in file, in bytes */ | |
1182 | unsigned int a_entry; /* start address */ | |
1183 | unsigned int a_trsize; /* length of relocation info for text, in bytes */ | |
1184 | unsigned int a_drsize; /* length of relocation info for data, in bytes */ | |
09bfb054 FB |
1185 | }; |
1186 | ||
1187 | ||
1188 | #define N_MAGIC(exec) ((exec).a_info & 0xffff) | |
1189 | #define OMAGIC 0407 | |
1190 | #define NMAGIC 0410 | |
1191 | #define ZMAGIC 0413 | |
1192 | #define QMAGIC 0314 | |
1193 | ||
31e31b8a | 1194 | /* Necessary parameters */ |
54936004 FB |
1195 | #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE |
1196 | #define TARGET_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE-1)) | |
1197 | #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1)) | |
31e31b8a | 1198 | |
ad1c7e0f | 1199 | #define DLINFO_ITEMS 14 |
31e31b8a | 1200 | |
09bfb054 FB |
1201 | static inline void memcpy_fromfs(void * to, const void * from, unsigned long n) |
1202 | { | |
d97ef72e | 1203 | memcpy(to, from, n); |
09bfb054 | 1204 | } |
d691f669 | 1205 | |
31e31b8a | 1206 | #ifdef BSWAP_NEEDED |
92a31b1f | 1207 | static void bswap_ehdr(struct elfhdr *ehdr) |
31e31b8a | 1208 | { |
d97ef72e RH |
1209 | bswap16s(&ehdr->e_type); /* Object file type */ |
1210 | bswap16s(&ehdr->e_machine); /* Architecture */ | |
1211 | bswap32s(&ehdr->e_version); /* Object file version */ | |
1212 | bswaptls(&ehdr->e_entry); /* Entry point virtual address */ | |
1213 | bswaptls(&ehdr->e_phoff); /* Program header table file offset */ | |
1214 | bswaptls(&ehdr->e_shoff); /* Section header table file offset */ | |
1215 | bswap32s(&ehdr->e_flags); /* Processor-specific flags */ | |
1216 | bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */ | |
1217 | bswap16s(&ehdr->e_phentsize); /* Program header table entry size */ | |
1218 | bswap16s(&ehdr->e_phnum); /* Program header table entry count */ | |
1219 | bswap16s(&ehdr->e_shentsize); /* Section header table entry size */ | |
1220 | bswap16s(&ehdr->e_shnum); /* Section header table entry count */ | |
1221 | bswap16s(&ehdr->e_shstrndx); /* Section header string table index */ | |
31e31b8a FB |
1222 | } |
1223 | ||
991f8f0c | 1224 | static void bswap_phdr(struct elf_phdr *phdr, int phnum) |
31e31b8a | 1225 | { |
991f8f0c RH |
1226 | int i; |
1227 | for (i = 0; i < phnum; ++i, ++phdr) { | |
1228 | bswap32s(&phdr->p_type); /* Segment type */ | |
1229 | bswap32s(&phdr->p_flags); /* Segment flags */ | |
1230 | bswaptls(&phdr->p_offset); /* Segment file offset */ | |
1231 | bswaptls(&phdr->p_vaddr); /* Segment virtual address */ | |
1232 | bswaptls(&phdr->p_paddr); /* Segment physical address */ | |
1233 | bswaptls(&phdr->p_filesz); /* Segment size in file */ | |
1234 | bswaptls(&phdr->p_memsz); /* Segment size in memory */ | |
1235 | bswaptls(&phdr->p_align); /* Segment alignment */ | |
1236 | } | |
31e31b8a | 1237 | } |
689f936f | 1238 | |
991f8f0c | 1239 | static void bswap_shdr(struct elf_shdr *shdr, int shnum) |
689f936f | 1240 | { |
991f8f0c RH |
1241 | int i; |
1242 | for (i = 0; i < shnum; ++i, ++shdr) { | |
1243 | bswap32s(&shdr->sh_name); | |
1244 | bswap32s(&shdr->sh_type); | |
1245 | bswaptls(&shdr->sh_flags); | |
1246 | bswaptls(&shdr->sh_addr); | |
1247 | bswaptls(&shdr->sh_offset); | |
1248 | bswaptls(&shdr->sh_size); | |
1249 | bswap32s(&shdr->sh_link); | |
1250 | bswap32s(&shdr->sh_info); | |
1251 | bswaptls(&shdr->sh_addralign); | |
1252 | bswaptls(&shdr->sh_entsize); | |
1253 | } | |
689f936f FB |
1254 | } |
1255 | ||
7a3148a9 | 1256 | static void bswap_sym(struct elf_sym *sym) |
689f936f FB |
1257 | { |
1258 | bswap32s(&sym->st_name); | |
7a3148a9 JM |
1259 | bswaptls(&sym->st_value); |
1260 | bswaptls(&sym->st_size); | |
689f936f FB |
1261 | bswap16s(&sym->st_shndx); |
1262 | } | |
991f8f0c RH |
1263 | #else |
1264 | static inline void bswap_ehdr(struct elfhdr *ehdr) { } | |
1265 | static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { } | |
1266 | static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { } | |
1267 | static inline void bswap_sym(struct elf_sym *sym) { } | |
31e31b8a FB |
1268 | #endif |
1269 | ||
edf8e2af | 1270 | #ifdef USE_ELF_CORE_DUMP |
9349b4f9 | 1271 | static int elf_core_dump(int, const CPUArchState *); |
edf8e2af | 1272 | #endif /* USE_ELF_CORE_DUMP */ |
682674b8 | 1273 | static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias); |
edf8e2af | 1274 | |
9058abdd RH |
1275 | /* Verify the portions of EHDR within E_IDENT for the target. |
1276 | This can be performed before bswapping the entire header. */ | |
1277 | static bool elf_check_ident(struct elfhdr *ehdr) | |
1278 | { | |
1279 | return (ehdr->e_ident[EI_MAG0] == ELFMAG0 | |
1280 | && ehdr->e_ident[EI_MAG1] == ELFMAG1 | |
1281 | && ehdr->e_ident[EI_MAG2] == ELFMAG2 | |
1282 | && ehdr->e_ident[EI_MAG3] == ELFMAG3 | |
1283 | && ehdr->e_ident[EI_CLASS] == ELF_CLASS | |
1284 | && ehdr->e_ident[EI_DATA] == ELF_DATA | |
1285 | && ehdr->e_ident[EI_VERSION] == EV_CURRENT); | |
1286 | } | |
1287 | ||
1288 | /* Verify the portions of EHDR outside of E_IDENT for the target. | |
1289 | This has to wait until after bswapping the header. */ | |
1290 | static bool elf_check_ehdr(struct elfhdr *ehdr) | |
1291 | { | |
1292 | return (elf_check_arch(ehdr->e_machine) | |
1293 | && ehdr->e_ehsize == sizeof(struct elfhdr) | |
1294 | && ehdr->e_phentsize == sizeof(struct elf_phdr) | |
9058abdd RH |
1295 | && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN)); |
1296 | } | |
1297 | ||
31e31b8a | 1298 | /* |
e5fe0c52 | 1299 | * 'copy_elf_strings()' copies argument/envelope strings from user |
31e31b8a FB |
1300 | * memory to free pages in kernel mem. These are in a format ready |
1301 | * to be put directly into the top of new user memory. | |
1302 | * | |
1303 | */ | |
992f48a0 BS |
1304 | static abi_ulong copy_elf_strings(int argc,char ** argv, void **page, |
1305 | abi_ulong p) | |
31e31b8a FB |
1306 | { |
1307 | char *tmp, *tmp1, *pag = NULL; | |
1308 | int len, offset = 0; | |
1309 | ||
1310 | if (!p) { | |
d97ef72e | 1311 | return 0; /* bullet-proofing */ |
31e31b8a FB |
1312 | } |
1313 | while (argc-- > 0) { | |
edf779ff FB |
1314 | tmp = argv[argc]; |
1315 | if (!tmp) { | |
d97ef72e RH |
1316 | fprintf(stderr, "VFS: argc is wrong"); |
1317 | exit(-1); | |
1318 | } | |
edf779ff | 1319 | tmp1 = tmp; |
d97ef72e RH |
1320 | while (*tmp++); |
1321 | len = tmp - tmp1; | |
1322 | if (p < len) { /* this shouldn't happen - 128kB */ | |
1323 | return 0; | |
1324 | } | |
1325 | while (len) { | |
1326 | --p; --tmp; --len; | |
1327 | if (--offset < 0) { | |
1328 | offset = p % TARGET_PAGE_SIZE; | |
53a5960a | 1329 | pag = (char *)page[p/TARGET_PAGE_SIZE]; |
44a91cae | 1330 | if (!pag) { |
7dd47667 | 1331 | pag = g_try_malloc0(TARGET_PAGE_SIZE); |
53a5960a | 1332 | page[p/TARGET_PAGE_SIZE] = pag; |
44a91cae FB |
1333 | if (!pag) |
1334 | return 0; | |
d97ef72e RH |
1335 | } |
1336 | } | |
1337 | if (len == 0 || offset == 0) { | |
1338 | *(pag + offset) = *tmp; | |
1339 | } | |
1340 | else { | |
1341 | int bytes_to_copy = (len > offset) ? offset : len; | |
1342 | tmp -= bytes_to_copy; | |
1343 | p -= bytes_to_copy; | |
1344 | offset -= bytes_to_copy; | |
1345 | len -= bytes_to_copy; | |
1346 | memcpy_fromfs(pag + offset, tmp, bytes_to_copy + 1); | |
1347 | } | |
1348 | } | |
31e31b8a FB |
1349 | } |
1350 | return p; | |
1351 | } | |
1352 | ||
992f48a0 BS |
1353 | static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm, |
1354 | struct image_info *info) | |
53a5960a | 1355 | { |
60dcbcb5 | 1356 | abi_ulong stack_base, size, error, guard; |
31e31b8a | 1357 | int i; |
31e31b8a | 1358 | |
09bfb054 | 1359 | /* Create enough stack to hold everything. If we don't use |
60dcbcb5 | 1360 | it for args, we'll use it for something else. */ |
703e0e89 | 1361 | size = guest_stack_size; |
60dcbcb5 | 1362 | if (size < MAX_ARG_PAGES*TARGET_PAGE_SIZE) { |
54936004 | 1363 | size = MAX_ARG_PAGES*TARGET_PAGE_SIZE; |
60dcbcb5 RH |
1364 | } |
1365 | guard = TARGET_PAGE_SIZE; | |
1366 | if (guard < qemu_real_host_page_size) { | |
1367 | guard = qemu_real_host_page_size; | |
1368 | } | |
1369 | ||
1370 | error = target_mmap(0, size + guard, PROT_READ | PROT_WRITE, | |
1371 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
09bfb054 | 1372 | if (error == -1) { |
60dcbcb5 | 1373 | perror("mmap stack"); |
09bfb054 FB |
1374 | exit(-1); |
1375 | } | |
31e31b8a | 1376 | |
60dcbcb5 RH |
1377 | /* We reserve one extra page at the top of the stack as guard. */ |
1378 | target_mprotect(error, guard, PROT_NONE); | |
1379 | ||
1380 | info->stack_limit = error + guard; | |
1381 | stack_base = info->stack_limit + size - MAX_ARG_PAGES*TARGET_PAGE_SIZE; | |
31e31b8a | 1382 | p += stack_base; |
09bfb054 | 1383 | |
31e31b8a | 1384 | for (i = 0 ; i < MAX_ARG_PAGES ; i++) { |
d97ef72e RH |
1385 | if (bprm->page[i]) { |
1386 | info->rss++; | |
579a97f7 | 1387 | /* FIXME - check return value of memcpy_to_target() for failure */ |
d97ef72e | 1388 | memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE); |
7dd47667 | 1389 | g_free(bprm->page[i]); |
d97ef72e | 1390 | } |
53a5960a | 1391 | stack_base += TARGET_PAGE_SIZE; |
31e31b8a FB |
1392 | } |
1393 | return p; | |
1394 | } | |
1395 | ||
cf129f3a RH |
1396 | /* Map and zero the bss. We need to explicitly zero any fractional pages |
1397 | after the data section (i.e. bss). */ | |
1398 | static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot) | |
31e31b8a | 1399 | { |
cf129f3a RH |
1400 | uintptr_t host_start, host_map_start, host_end; |
1401 | ||
1402 | last_bss = TARGET_PAGE_ALIGN(last_bss); | |
1403 | ||
1404 | /* ??? There is confusion between qemu_real_host_page_size and | |
1405 | qemu_host_page_size here and elsewhere in target_mmap, which | |
1406 | may lead to the end of the data section mapping from the file | |
1407 | not being mapped. At least there was an explicit test and | |
1408 | comment for that here, suggesting that "the file size must | |
1409 | be known". The comment probably pre-dates the introduction | |
1410 | of the fstat system call in target_mmap which does in fact | |
1411 | find out the size. What isn't clear is if the workaround | |
1412 | here is still actually needed. For now, continue with it, | |
1413 | but merge it with the "normal" mmap that would allocate the bss. */ | |
1414 | ||
1415 | host_start = (uintptr_t) g2h(elf_bss); | |
1416 | host_end = (uintptr_t) g2h(last_bss); | |
1417 | host_map_start = (host_start + qemu_real_host_page_size - 1); | |
1418 | host_map_start &= -qemu_real_host_page_size; | |
1419 | ||
1420 | if (host_map_start < host_end) { | |
1421 | void *p = mmap((void *)host_map_start, host_end - host_map_start, | |
1422 | prot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
1423 | if (p == MAP_FAILED) { | |
1424 | perror("cannot mmap brk"); | |
1425 | exit(-1); | |
853d6f7a | 1426 | } |
f46e9a0b | 1427 | } |
853d6f7a | 1428 | |
f46e9a0b TM |
1429 | /* Ensure that the bss page(s) are valid */ |
1430 | if ((page_get_flags(last_bss-1) & prot) != prot) { | |
1431 | page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_VALID); | |
cf129f3a | 1432 | } |
31e31b8a | 1433 | |
cf129f3a RH |
1434 | if (host_start < host_map_start) { |
1435 | memset((void *)host_start, 0, host_map_start - host_start); | |
1436 | } | |
1437 | } | |
53a5960a | 1438 | |
1af02e83 MF |
1439 | #ifdef CONFIG_USE_FDPIC |
1440 | static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp) | |
1441 | { | |
1442 | uint16_t n; | |
1443 | struct elf32_fdpic_loadseg *loadsegs = info->loadsegs; | |
1444 | ||
1445 | /* elf32_fdpic_loadseg */ | |
1446 | n = info->nsegs; | |
1447 | while (n--) { | |
1448 | sp -= 12; | |
1449 | put_user_u32(loadsegs[n].addr, sp+0); | |
1450 | put_user_u32(loadsegs[n].p_vaddr, sp+4); | |
1451 | put_user_u32(loadsegs[n].p_memsz, sp+8); | |
1452 | } | |
1453 | ||
1454 | /* elf32_fdpic_loadmap */ | |
1455 | sp -= 4; | |
1456 | put_user_u16(0, sp+0); /* version */ | |
1457 | put_user_u16(info->nsegs, sp+2); /* nsegs */ | |
1458 | ||
1459 | info->personality = PER_LINUX_FDPIC; | |
1460 | info->loadmap_addr = sp; | |
1461 | ||
1462 | return sp; | |
1463 | } | |
1464 | #endif | |
1465 | ||
992f48a0 | 1466 | static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc, |
8e62a717 RH |
1467 | struct elfhdr *exec, |
1468 | struct image_info *info, | |
1469 | struct image_info *interp_info) | |
31e31b8a | 1470 | { |
d97ef72e | 1471 | abi_ulong sp; |
125b0f55 | 1472 | abi_ulong sp_auxv; |
d97ef72e | 1473 | int size; |
14322bad LA |
1474 | int i; |
1475 | abi_ulong u_rand_bytes; | |
1476 | uint8_t k_rand_bytes[16]; | |
d97ef72e RH |
1477 | abi_ulong u_platform; |
1478 | const char *k_platform; | |
1479 | const int n = sizeof(elf_addr_t); | |
1480 | ||
1481 | sp = p; | |
1af02e83 MF |
1482 | |
1483 | #ifdef CONFIG_USE_FDPIC | |
1484 | /* Needs to be before we load the env/argc/... */ | |
1485 | if (elf_is_fdpic(exec)) { | |
1486 | /* Need 4 byte alignment for these structs */ | |
1487 | sp &= ~3; | |
1488 | sp = loader_build_fdpic_loadmap(info, sp); | |
1489 | info->other_info = interp_info; | |
1490 | if (interp_info) { | |
1491 | interp_info->other_info = info; | |
1492 | sp = loader_build_fdpic_loadmap(interp_info, sp); | |
1493 | } | |
1494 | } | |
1495 | #endif | |
1496 | ||
d97ef72e RH |
1497 | u_platform = 0; |
1498 | k_platform = ELF_PLATFORM; | |
1499 | if (k_platform) { | |
1500 | size_t len = strlen(k_platform) + 1; | |
1501 | sp -= (len + n - 1) & ~(n - 1); | |
1502 | u_platform = sp; | |
1503 | /* FIXME - check return value of memcpy_to_target() for failure */ | |
1504 | memcpy_to_target(sp, k_platform, len); | |
1505 | } | |
14322bad LA |
1506 | |
1507 | /* | |
1508 | * Generate 16 random bytes for userspace PRNG seeding (not | |
1509 | * cryptically secure but it's not the aim of QEMU). | |
1510 | */ | |
1511 | srand((unsigned int) time(NULL)); | |
1512 | for (i = 0; i < 16; i++) { | |
1513 | k_rand_bytes[i] = rand(); | |
1514 | } | |
1515 | sp -= 16; | |
1516 | u_rand_bytes = sp; | |
1517 | /* FIXME - check return value of memcpy_to_target() for failure */ | |
1518 | memcpy_to_target(sp, k_rand_bytes, 16); | |
1519 | ||
d97ef72e RH |
1520 | /* |
1521 | * Force 16 byte _final_ alignment here for generality. | |
1522 | */ | |
1523 | sp = sp &~ (abi_ulong)15; | |
1524 | size = (DLINFO_ITEMS + 1) * 2; | |
1525 | if (k_platform) | |
1526 | size += 2; | |
f5155289 | 1527 | #ifdef DLINFO_ARCH_ITEMS |
d97ef72e | 1528 | size += DLINFO_ARCH_ITEMS * 2; |
ad6919dc PM |
1529 | #endif |
1530 | #ifdef ELF_HWCAP2 | |
1531 | size += 2; | |
f5155289 | 1532 | #endif |
d97ef72e | 1533 | size += envc + argc + 2; |
b9329d4b | 1534 | size += 1; /* argc itself */ |
d97ef72e RH |
1535 | size *= n; |
1536 | if (size & 15) | |
1537 | sp -= 16 - (size & 15); | |
1538 | ||
1539 | /* This is correct because Linux defines | |
1540 | * elf_addr_t as Elf32_Off / Elf64_Off | |
1541 | */ | |
1542 | #define NEW_AUX_ENT(id, val) do { \ | |
1543 | sp -= n; put_user_ual(val, sp); \ | |
1544 | sp -= n; put_user_ual(id, sp); \ | |
1545 | } while(0) | |
1546 | ||
125b0f55 | 1547 | sp_auxv = sp; |
d97ef72e RH |
1548 | NEW_AUX_ENT (AT_NULL, 0); |
1549 | ||
1550 | /* There must be exactly DLINFO_ITEMS entries here. */ | |
8e62a717 | 1551 | NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff)); |
d97ef72e RH |
1552 | NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr))); |
1553 | NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum)); | |
a70daba3 | 1554 | NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(MAX(TARGET_PAGE_SIZE, getpagesize()))); |
8e62a717 | 1555 | NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0)); |
d97ef72e | 1556 | NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0); |
8e62a717 | 1557 | NEW_AUX_ENT(AT_ENTRY, info->entry); |
d97ef72e RH |
1558 | NEW_AUX_ENT(AT_UID, (abi_ulong) getuid()); |
1559 | NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid()); | |
1560 | NEW_AUX_ENT(AT_GID, (abi_ulong) getgid()); | |
1561 | NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid()); | |
1562 | NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP); | |
1563 | NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK)); | |
14322bad LA |
1564 | NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes); |
1565 | ||
ad6919dc PM |
1566 | #ifdef ELF_HWCAP2 |
1567 | NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2); | |
1568 | #endif | |
1569 | ||
d97ef72e RH |
1570 | if (k_platform) |
1571 | NEW_AUX_ENT(AT_PLATFORM, u_platform); | |
f5155289 | 1572 | #ifdef ARCH_DLINFO |
d97ef72e RH |
1573 | /* |
1574 | * ARCH_DLINFO must come last so platform specific code can enforce | |
1575 | * special alignment requirements on the AUXV if necessary (eg. PPC). | |
1576 | */ | |
1577 | ARCH_DLINFO; | |
f5155289 FB |
1578 | #endif |
1579 | #undef NEW_AUX_ENT | |
1580 | ||
d97ef72e | 1581 | info->saved_auxv = sp; |
125b0f55 | 1582 | info->auxv_len = sp_auxv - sp; |
edf8e2af | 1583 | |
b9329d4b | 1584 | sp = loader_build_argptr(envc, argc, sp, p, 0); |
8c0f0a60 JH |
1585 | /* Check the right amount of stack was allocated for auxvec, envp & argv. */ |
1586 | assert(sp_auxv - sp == size); | |
d97ef72e | 1587 | return sp; |
31e31b8a FB |
1588 | } |
1589 | ||
806d1021 | 1590 | #ifndef TARGET_HAS_VALIDATE_GUEST_SPACE |
97cc7560 | 1591 | /* If the guest doesn't have a validation function just agree */ |
806d1021 MI |
1592 | static int validate_guest_space(unsigned long guest_base, |
1593 | unsigned long guest_size) | |
97cc7560 DDAG |
1594 | { |
1595 | return 1; | |
1596 | } | |
1597 | #endif | |
1598 | ||
dce10401 MI |
1599 | unsigned long init_guest_space(unsigned long host_start, |
1600 | unsigned long host_size, | |
1601 | unsigned long guest_start, | |
1602 | bool fixed) | |
1603 | { | |
1604 | unsigned long current_start, real_start; | |
1605 | int flags; | |
1606 | ||
1607 | assert(host_start || host_size); | |
1608 | ||
1609 | /* If just a starting address is given, then just verify that | |
1610 | * address. */ | |
1611 | if (host_start && !host_size) { | |
806d1021 | 1612 | if (validate_guest_space(host_start, host_size) == 1) { |
dce10401 MI |
1613 | return host_start; |
1614 | } else { | |
1615 | return (unsigned long)-1; | |
1616 | } | |
1617 | } | |
1618 | ||
1619 | /* Setup the initial flags and start address. */ | |
1620 | current_start = host_start & qemu_host_page_mask; | |
1621 | flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE; | |
1622 | if (fixed) { | |
1623 | flags |= MAP_FIXED; | |
1624 | } | |
1625 | ||
1626 | /* Otherwise, a non-zero size region of memory needs to be mapped | |
1627 | * and validated. */ | |
1628 | while (1) { | |
806d1021 MI |
1629 | unsigned long real_size = host_size; |
1630 | ||
dce10401 MI |
1631 | /* Do not use mmap_find_vma here because that is limited to the |
1632 | * guest address space. We are going to make the | |
1633 | * guest address space fit whatever we're given. | |
1634 | */ | |
1635 | real_start = (unsigned long) | |
1636 | mmap((void *)current_start, host_size, PROT_NONE, flags, -1, 0); | |
1637 | if (real_start == (unsigned long)-1) { | |
1638 | return (unsigned long)-1; | |
1639 | } | |
1640 | ||
806d1021 MI |
1641 | /* Ensure the address is properly aligned. */ |
1642 | if (real_start & ~qemu_host_page_mask) { | |
1643 | munmap((void *)real_start, host_size); | |
1644 | real_size = host_size + qemu_host_page_size; | |
1645 | real_start = (unsigned long) | |
1646 | mmap((void *)real_start, real_size, PROT_NONE, flags, -1, 0); | |
1647 | if (real_start == (unsigned long)-1) { | |
1648 | return (unsigned long)-1; | |
1649 | } | |
1650 | real_start = HOST_PAGE_ALIGN(real_start); | |
1651 | } | |
1652 | ||
1653 | /* Check to see if the address is valid. */ | |
1654 | if (!host_start || real_start == current_start) { | |
1655 | int valid = validate_guest_space(real_start - guest_start, | |
1656 | real_size); | |
1657 | if (valid == 1) { | |
1658 | break; | |
1659 | } else if (valid == -1) { | |
1660 | return (unsigned long)-1; | |
1661 | } | |
1662 | /* valid == 0, so try again. */ | |
dce10401 MI |
1663 | } |
1664 | ||
1665 | /* That address didn't work. Unmap and try a different one. | |
1666 | * The address the host picked because is typically right at | |
1667 | * the top of the host address space and leaves the guest with | |
1668 | * no usable address space. Resort to a linear search. We | |
1669 | * already compensated for mmap_min_addr, so this should not | |
1670 | * happen often. Probably means we got unlucky and host | |
1671 | * address space randomization put a shared library somewhere | |
1672 | * inconvenient. | |
1673 | */ | |
1674 | munmap((void *)real_start, host_size); | |
1675 | current_start += qemu_host_page_size; | |
1676 | if (host_start == current_start) { | |
1677 | /* Theoretically possible if host doesn't have any suitably | |
1678 | * aligned areas. Normally the first mmap will fail. | |
1679 | */ | |
1680 | return (unsigned long)-1; | |
1681 | } | |
1682 | } | |
1683 | ||
806d1021 MI |
1684 | qemu_log("Reserved 0x%lx bytes of guest address space\n", host_size); |
1685 | ||
dce10401 MI |
1686 | return real_start; |
1687 | } | |
1688 | ||
f3ed1f5d PM |
1689 | static void probe_guest_base(const char *image_name, |
1690 | abi_ulong loaddr, abi_ulong hiaddr) | |
1691 | { | |
1692 | /* Probe for a suitable guest base address, if the user has not set | |
1693 | * it explicitly, and set guest_base appropriately. | |
1694 | * In case of error we will print a suitable message and exit. | |
1695 | */ | |
1696 | #if defined(CONFIG_USE_GUEST_BASE) | |
1697 | const char *errmsg; | |
1698 | if (!have_guest_base && !reserved_va) { | |
1699 | unsigned long host_start, real_start, host_size; | |
1700 | ||
1701 | /* Round addresses to page boundaries. */ | |
1702 | loaddr &= qemu_host_page_mask; | |
1703 | hiaddr = HOST_PAGE_ALIGN(hiaddr); | |
1704 | ||
1705 | if (loaddr < mmap_min_addr) { | |
1706 | host_start = HOST_PAGE_ALIGN(mmap_min_addr); | |
1707 | } else { | |
1708 | host_start = loaddr; | |
1709 | if (host_start != loaddr) { | |
1710 | errmsg = "Address overflow loading ELF binary"; | |
1711 | goto exit_errmsg; | |
1712 | } | |
1713 | } | |
1714 | host_size = hiaddr - loaddr; | |
dce10401 MI |
1715 | |
1716 | /* Setup the initial guest memory space with ranges gleaned from | |
1717 | * the ELF image that is being loaded. | |
1718 | */ | |
1719 | real_start = init_guest_space(host_start, host_size, loaddr, false); | |
1720 | if (real_start == (unsigned long)-1) { | |
1721 | errmsg = "Unable to find space for application"; | |
1722 | goto exit_errmsg; | |
f3ed1f5d | 1723 | } |
dce10401 MI |
1724 | guest_base = real_start - loaddr; |
1725 | ||
f3ed1f5d PM |
1726 | qemu_log("Relocating guest address space from 0x" |
1727 | TARGET_ABI_FMT_lx " to 0x%lx\n", | |
1728 | loaddr, real_start); | |
f3ed1f5d PM |
1729 | } |
1730 | return; | |
1731 | ||
f3ed1f5d PM |
1732 | exit_errmsg: |
1733 | fprintf(stderr, "%s: %s\n", image_name, errmsg); | |
1734 | exit(-1); | |
1735 | #endif | |
1736 | } | |
1737 | ||
1738 | ||
8e62a717 | 1739 | /* Load an ELF image into the address space. |
31e31b8a | 1740 | |
8e62a717 RH |
1741 | IMAGE_NAME is the filename of the image, to use in error messages. |
1742 | IMAGE_FD is the open file descriptor for the image. | |
1743 | ||
1744 | BPRM_BUF is a copy of the beginning of the file; this of course | |
1745 | contains the elf file header at offset 0. It is assumed that this | |
1746 | buffer is sufficiently aligned to present no problems to the host | |
1747 | in accessing data at aligned offsets within the buffer. | |
1748 | ||
1749 | On return: INFO values will be filled in, as necessary or available. */ | |
1750 | ||
1751 | static void load_elf_image(const char *image_name, int image_fd, | |
bf858897 | 1752 | struct image_info *info, char **pinterp_name, |
8e62a717 | 1753 | char bprm_buf[BPRM_BUF_SIZE]) |
31e31b8a | 1754 | { |
8e62a717 RH |
1755 | struct elfhdr *ehdr = (struct elfhdr *)bprm_buf; |
1756 | struct elf_phdr *phdr; | |
1757 | abi_ulong load_addr, load_bias, loaddr, hiaddr, error; | |
1758 | int i, retval; | |
1759 | const char *errmsg; | |
5fafdf24 | 1760 | |
8e62a717 RH |
1761 | /* First of all, some simple consistency checks */ |
1762 | errmsg = "Invalid ELF image for this architecture"; | |
1763 | if (!elf_check_ident(ehdr)) { | |
1764 | goto exit_errmsg; | |
1765 | } | |
1766 | bswap_ehdr(ehdr); | |
1767 | if (!elf_check_ehdr(ehdr)) { | |
1768 | goto exit_errmsg; | |
d97ef72e | 1769 | } |
5fafdf24 | 1770 | |
8e62a717 RH |
1771 | i = ehdr->e_phnum * sizeof(struct elf_phdr); |
1772 | if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) { | |
1773 | phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff); | |
9955ffac | 1774 | } else { |
8e62a717 RH |
1775 | phdr = (struct elf_phdr *) alloca(i); |
1776 | retval = pread(image_fd, phdr, i, ehdr->e_phoff); | |
9955ffac | 1777 | if (retval != i) { |
8e62a717 | 1778 | goto exit_read; |
9955ffac | 1779 | } |
d97ef72e | 1780 | } |
8e62a717 | 1781 | bswap_phdr(phdr, ehdr->e_phnum); |
09bfb054 | 1782 | |
1af02e83 MF |
1783 | #ifdef CONFIG_USE_FDPIC |
1784 | info->nsegs = 0; | |
1785 | info->pt_dynamic_addr = 0; | |
1786 | #endif | |
1787 | ||
682674b8 RH |
1788 | /* Find the maximum size of the image and allocate an appropriate |
1789 | amount of memory to handle that. */ | |
1790 | loaddr = -1, hiaddr = 0; | |
8e62a717 RH |
1791 | for (i = 0; i < ehdr->e_phnum; ++i) { |
1792 | if (phdr[i].p_type == PT_LOAD) { | |
1793 | abi_ulong a = phdr[i].p_vaddr; | |
682674b8 RH |
1794 | if (a < loaddr) { |
1795 | loaddr = a; | |
1796 | } | |
8e62a717 | 1797 | a += phdr[i].p_memsz; |
682674b8 RH |
1798 | if (a > hiaddr) { |
1799 | hiaddr = a; | |
1800 | } | |
1af02e83 MF |
1801 | #ifdef CONFIG_USE_FDPIC |
1802 | ++info->nsegs; | |
1803 | #endif | |
682674b8 RH |
1804 | } |
1805 | } | |
1806 | ||
1807 | load_addr = loaddr; | |
8e62a717 | 1808 | if (ehdr->e_type == ET_DYN) { |
682674b8 RH |
1809 | /* The image indicates that it can be loaded anywhere. Find a |
1810 | location that can hold the memory space required. If the | |
1811 | image is pre-linked, LOADDR will be non-zero. Since we do | |
1812 | not supply MAP_FIXED here we'll use that address if and | |
1813 | only if it remains available. */ | |
1814 | load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE, | |
1815 | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, | |
1816 | -1, 0); | |
1817 | if (load_addr == -1) { | |
8e62a717 | 1818 | goto exit_perror; |
d97ef72e | 1819 | } |
bf858897 RH |
1820 | } else if (pinterp_name != NULL) { |
1821 | /* This is the main executable. Make sure that the low | |
1822 | address does not conflict with MMAP_MIN_ADDR or the | |
1823 | QEMU application itself. */ | |
f3ed1f5d | 1824 | probe_guest_base(image_name, loaddr, hiaddr); |
d97ef72e | 1825 | } |
682674b8 | 1826 | load_bias = load_addr - loaddr; |
d97ef72e | 1827 | |
1af02e83 MF |
1828 | #ifdef CONFIG_USE_FDPIC |
1829 | { | |
1830 | struct elf32_fdpic_loadseg *loadsegs = info->loadsegs = | |
7267c094 | 1831 | g_malloc(sizeof(*loadsegs) * info->nsegs); |
1af02e83 MF |
1832 | |
1833 | for (i = 0; i < ehdr->e_phnum; ++i) { | |
1834 | switch (phdr[i].p_type) { | |
1835 | case PT_DYNAMIC: | |
1836 | info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias; | |
1837 | break; | |
1838 | case PT_LOAD: | |
1839 | loadsegs->addr = phdr[i].p_vaddr + load_bias; | |
1840 | loadsegs->p_vaddr = phdr[i].p_vaddr; | |
1841 | loadsegs->p_memsz = phdr[i].p_memsz; | |
1842 | ++loadsegs; | |
1843 | break; | |
1844 | } | |
1845 | } | |
1846 | } | |
1847 | #endif | |
1848 | ||
8e62a717 RH |
1849 | info->load_bias = load_bias; |
1850 | info->load_addr = load_addr; | |
1851 | info->entry = ehdr->e_entry + load_bias; | |
1852 | info->start_code = -1; | |
1853 | info->end_code = 0; | |
1854 | info->start_data = -1; | |
1855 | info->end_data = 0; | |
1856 | info->brk = 0; | |
d8fd2954 | 1857 | info->elf_flags = ehdr->e_flags; |
8e62a717 RH |
1858 | |
1859 | for (i = 0; i < ehdr->e_phnum; i++) { | |
1860 | struct elf_phdr *eppnt = phdr + i; | |
d97ef72e | 1861 | if (eppnt->p_type == PT_LOAD) { |
682674b8 | 1862 | abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em; |
d97ef72e | 1863 | int elf_prot = 0; |
d97ef72e RH |
1864 | |
1865 | if (eppnt->p_flags & PF_R) elf_prot = PROT_READ; | |
1866 | if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE; | |
1867 | if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC; | |
d97ef72e | 1868 | |
682674b8 RH |
1869 | vaddr = load_bias + eppnt->p_vaddr; |
1870 | vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr); | |
1871 | vaddr_ps = TARGET_ELF_PAGESTART(vaddr); | |
1872 | ||
1873 | error = target_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po, | |
1874 | elf_prot, MAP_PRIVATE | MAP_FIXED, | |
8e62a717 | 1875 | image_fd, eppnt->p_offset - vaddr_po); |
09bfb054 | 1876 | if (error == -1) { |
8e62a717 | 1877 | goto exit_perror; |
09bfb054 | 1878 | } |
09bfb054 | 1879 | |
682674b8 RH |
1880 | vaddr_ef = vaddr + eppnt->p_filesz; |
1881 | vaddr_em = vaddr + eppnt->p_memsz; | |
31e31b8a | 1882 | |
cf129f3a | 1883 | /* If the load segment requests extra zeros (e.g. bss), map it. */ |
682674b8 RH |
1884 | if (vaddr_ef < vaddr_em) { |
1885 | zero_bss(vaddr_ef, vaddr_em, elf_prot); | |
cf129f3a | 1886 | } |
8e62a717 RH |
1887 | |
1888 | /* Find the full program boundaries. */ | |
1889 | if (elf_prot & PROT_EXEC) { | |
1890 | if (vaddr < info->start_code) { | |
1891 | info->start_code = vaddr; | |
1892 | } | |
1893 | if (vaddr_ef > info->end_code) { | |
1894 | info->end_code = vaddr_ef; | |
1895 | } | |
1896 | } | |
1897 | if (elf_prot & PROT_WRITE) { | |
1898 | if (vaddr < info->start_data) { | |
1899 | info->start_data = vaddr; | |
1900 | } | |
1901 | if (vaddr_ef > info->end_data) { | |
1902 | info->end_data = vaddr_ef; | |
1903 | } | |
1904 | if (vaddr_em > info->brk) { | |
1905 | info->brk = vaddr_em; | |
1906 | } | |
1907 | } | |
bf858897 RH |
1908 | } else if (eppnt->p_type == PT_INTERP && pinterp_name) { |
1909 | char *interp_name; | |
1910 | ||
1911 | if (*pinterp_name) { | |
1912 | errmsg = "Multiple PT_INTERP entries"; | |
1913 | goto exit_errmsg; | |
1914 | } | |
1915 | interp_name = malloc(eppnt->p_filesz); | |
1916 | if (!interp_name) { | |
1917 | goto exit_perror; | |
1918 | } | |
1919 | ||
1920 | if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) { | |
1921 | memcpy(interp_name, bprm_buf + eppnt->p_offset, | |
1922 | eppnt->p_filesz); | |
1923 | } else { | |
1924 | retval = pread(image_fd, interp_name, eppnt->p_filesz, | |
1925 | eppnt->p_offset); | |
1926 | if (retval != eppnt->p_filesz) { | |
1927 | goto exit_perror; | |
1928 | } | |
1929 | } | |
1930 | if (interp_name[eppnt->p_filesz - 1] != 0) { | |
1931 | errmsg = "Invalid PT_INTERP entry"; | |
1932 | goto exit_errmsg; | |
1933 | } | |
1934 | *pinterp_name = interp_name; | |
d97ef72e | 1935 | } |
682674b8 | 1936 | } |
5fafdf24 | 1937 | |
8e62a717 RH |
1938 | if (info->end_data == 0) { |
1939 | info->start_data = info->end_code; | |
1940 | info->end_data = info->end_code; | |
1941 | info->brk = info->end_code; | |
1942 | } | |
1943 | ||
682674b8 | 1944 | if (qemu_log_enabled()) { |
8e62a717 | 1945 | load_symbols(ehdr, image_fd, load_bias); |
682674b8 | 1946 | } |
31e31b8a | 1947 | |
8e62a717 RH |
1948 | close(image_fd); |
1949 | return; | |
1950 | ||
1951 | exit_read: | |
1952 | if (retval >= 0) { | |
1953 | errmsg = "Incomplete read of file header"; | |
1954 | goto exit_errmsg; | |
1955 | } | |
1956 | exit_perror: | |
1957 | errmsg = strerror(errno); | |
1958 | exit_errmsg: | |
1959 | fprintf(stderr, "%s: %s\n", image_name, errmsg); | |
1960 | exit(-1); | |
1961 | } | |
1962 | ||
1963 | static void load_elf_interp(const char *filename, struct image_info *info, | |
1964 | char bprm_buf[BPRM_BUF_SIZE]) | |
1965 | { | |
1966 | int fd, retval; | |
1967 | ||
1968 | fd = open(path(filename), O_RDONLY); | |
1969 | if (fd < 0) { | |
1970 | goto exit_perror; | |
1971 | } | |
31e31b8a | 1972 | |
8e62a717 RH |
1973 | retval = read(fd, bprm_buf, BPRM_BUF_SIZE); |
1974 | if (retval < 0) { | |
1975 | goto exit_perror; | |
1976 | } | |
1977 | if (retval < BPRM_BUF_SIZE) { | |
1978 | memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval); | |
1979 | } | |
1980 | ||
bf858897 | 1981 | load_elf_image(filename, fd, info, NULL, bprm_buf); |
8e62a717 RH |
1982 | return; |
1983 | ||
1984 | exit_perror: | |
1985 | fprintf(stderr, "%s: %s\n", filename, strerror(errno)); | |
1986 | exit(-1); | |
31e31b8a FB |
1987 | } |
1988 | ||
49918a75 PB |
1989 | static int symfind(const void *s0, const void *s1) |
1990 | { | |
c7c530cd | 1991 | target_ulong addr = *(target_ulong *)s0; |
49918a75 PB |
1992 | struct elf_sym *sym = (struct elf_sym *)s1; |
1993 | int result = 0; | |
c7c530cd | 1994 | if (addr < sym->st_value) { |
49918a75 | 1995 | result = -1; |
c7c530cd | 1996 | } else if (addr >= sym->st_value + sym->st_size) { |
49918a75 PB |
1997 | result = 1; |
1998 | } | |
1999 | return result; | |
2000 | } | |
2001 | ||
2002 | static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr) | |
2003 | { | |
2004 | #if ELF_CLASS == ELFCLASS32 | |
2005 | struct elf_sym *syms = s->disas_symtab.elf32; | |
2006 | #else | |
2007 | struct elf_sym *syms = s->disas_symtab.elf64; | |
2008 | #endif | |
2009 | ||
2010 | // binary search | |
49918a75 PB |
2011 | struct elf_sym *sym; |
2012 | ||
c7c530cd | 2013 | sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind); |
7cba04f6 | 2014 | if (sym != NULL) { |
49918a75 PB |
2015 | return s->disas_strtab + sym->st_name; |
2016 | } | |
2017 | ||
2018 | return ""; | |
2019 | } | |
2020 | ||
2021 | /* FIXME: This should use elf_ops.h */ | |
2022 | static int symcmp(const void *s0, const void *s1) | |
2023 | { | |
2024 | struct elf_sym *sym0 = (struct elf_sym *)s0; | |
2025 | struct elf_sym *sym1 = (struct elf_sym *)s1; | |
2026 | return (sym0->st_value < sym1->st_value) | |
2027 | ? -1 | |
2028 | : ((sym0->st_value > sym1->st_value) ? 1 : 0); | |
2029 | } | |
2030 | ||
689f936f | 2031 | /* Best attempt to load symbols from this ELF object. */ |
682674b8 | 2032 | static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias) |
689f936f | 2033 | { |
682674b8 RH |
2034 | int i, shnum, nsyms, sym_idx = 0, str_idx = 0; |
2035 | struct elf_shdr *shdr; | |
b9475279 CV |
2036 | char *strings = NULL; |
2037 | struct syminfo *s = NULL; | |
2038 | struct elf_sym *new_syms, *syms = NULL; | |
689f936f | 2039 | |
682674b8 RH |
2040 | shnum = hdr->e_shnum; |
2041 | i = shnum * sizeof(struct elf_shdr); | |
2042 | shdr = (struct elf_shdr *)alloca(i); | |
2043 | if (pread(fd, shdr, i, hdr->e_shoff) != i) { | |
2044 | return; | |
2045 | } | |
2046 | ||
2047 | bswap_shdr(shdr, shnum); | |
2048 | for (i = 0; i < shnum; ++i) { | |
2049 | if (shdr[i].sh_type == SHT_SYMTAB) { | |
2050 | sym_idx = i; | |
2051 | str_idx = shdr[i].sh_link; | |
49918a75 PB |
2052 | goto found; |
2053 | } | |
689f936f | 2054 | } |
682674b8 RH |
2055 | |
2056 | /* There will be no symbol table if the file was stripped. */ | |
2057 | return; | |
689f936f FB |
2058 | |
2059 | found: | |
682674b8 | 2060 | /* Now know where the strtab and symtab are. Snarf them. */ |
e80cfcfc | 2061 | s = malloc(sizeof(*s)); |
682674b8 | 2062 | if (!s) { |
b9475279 | 2063 | goto give_up; |
682674b8 | 2064 | } |
5fafdf24 | 2065 | |
682674b8 RH |
2066 | i = shdr[str_idx].sh_size; |
2067 | s->disas_strtab = strings = malloc(i); | |
2068 | if (!strings || pread(fd, strings, i, shdr[str_idx].sh_offset) != i) { | |
b9475279 | 2069 | goto give_up; |
682674b8 | 2070 | } |
49918a75 | 2071 | |
682674b8 RH |
2072 | i = shdr[sym_idx].sh_size; |
2073 | syms = malloc(i); | |
2074 | if (!syms || pread(fd, syms, i, shdr[sym_idx].sh_offset) != i) { | |
b9475279 | 2075 | goto give_up; |
682674b8 | 2076 | } |
31e31b8a | 2077 | |
682674b8 RH |
2078 | nsyms = i / sizeof(struct elf_sym); |
2079 | for (i = 0; i < nsyms; ) { | |
49918a75 | 2080 | bswap_sym(syms + i); |
682674b8 RH |
2081 | /* Throw away entries which we do not need. */ |
2082 | if (syms[i].st_shndx == SHN_UNDEF | |
2083 | || syms[i].st_shndx >= SHN_LORESERVE | |
2084 | || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) { | |
2085 | if (i < --nsyms) { | |
49918a75 PB |
2086 | syms[i] = syms[nsyms]; |
2087 | } | |
682674b8 | 2088 | } else { |
49918a75 | 2089 | #if defined(TARGET_ARM) || defined (TARGET_MIPS) |
682674b8 RH |
2090 | /* The bottom address bit marks a Thumb or MIPS16 symbol. */ |
2091 | syms[i].st_value &= ~(target_ulong)1; | |
0774bed1 | 2092 | #endif |
682674b8 RH |
2093 | syms[i].st_value += load_bias; |
2094 | i++; | |
2095 | } | |
0774bed1 | 2096 | } |
49918a75 | 2097 | |
b9475279 CV |
2098 | /* No "useful" symbol. */ |
2099 | if (nsyms == 0) { | |
2100 | goto give_up; | |
2101 | } | |
2102 | ||
5d5c9930 RH |
2103 | /* Attempt to free the storage associated with the local symbols |
2104 | that we threw away. Whether or not this has any effect on the | |
2105 | memory allocation depends on the malloc implementation and how | |
2106 | many symbols we managed to discard. */ | |
8d79de6e SW |
2107 | new_syms = realloc(syms, nsyms * sizeof(*syms)); |
2108 | if (new_syms == NULL) { | |
b9475279 | 2109 | goto give_up; |
5d5c9930 | 2110 | } |
8d79de6e | 2111 | syms = new_syms; |
5d5c9930 | 2112 | |
49918a75 | 2113 | qsort(syms, nsyms, sizeof(*syms), symcmp); |
689f936f | 2114 | |
49918a75 PB |
2115 | s->disas_num_syms = nsyms; |
2116 | #if ELF_CLASS == ELFCLASS32 | |
2117 | s->disas_symtab.elf32 = syms; | |
49918a75 PB |
2118 | #else |
2119 | s->disas_symtab.elf64 = syms; | |
49918a75 | 2120 | #endif |
682674b8 | 2121 | s->lookup_symbol = lookup_symbolxx; |
e80cfcfc FB |
2122 | s->next = syminfos; |
2123 | syminfos = s; | |
b9475279 CV |
2124 | |
2125 | return; | |
2126 | ||
2127 | give_up: | |
2128 | free(s); | |
2129 | free(strings); | |
2130 | free(syms); | |
689f936f | 2131 | } |
31e31b8a | 2132 | |
f0116c54 | 2133 | int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) |
31e31b8a | 2134 | { |
8e62a717 | 2135 | struct image_info interp_info; |
31e31b8a | 2136 | struct elfhdr elf_ex; |
8e62a717 | 2137 | char *elf_interpreter = NULL; |
31e31b8a | 2138 | |
bf858897 RH |
2139 | info->start_mmap = (abi_ulong)ELF_START_MMAP; |
2140 | info->mmap = 0; | |
2141 | info->rss = 0; | |
2142 | ||
2143 | load_elf_image(bprm->filename, bprm->fd, info, | |
2144 | &elf_interpreter, bprm->buf); | |
31e31b8a | 2145 | |
bf858897 RH |
2146 | /* ??? We need a copy of the elf header for passing to create_elf_tables. |
2147 | If we do nothing, we'll have overwritten this when we re-use bprm->buf | |
2148 | when we load the interpreter. */ | |
2149 | elf_ex = *(struct elfhdr *)bprm->buf; | |
31e31b8a | 2150 | |
e5fe0c52 PB |
2151 | bprm->p = copy_elf_strings(1, &bprm->filename, bprm->page, bprm->p); |
2152 | bprm->p = copy_elf_strings(bprm->envc,bprm->envp,bprm->page,bprm->p); | |
2153 | bprm->p = copy_elf_strings(bprm->argc,bprm->argv,bprm->page,bprm->p); | |
2154 | if (!bprm->p) { | |
bf858897 RH |
2155 | fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG)); |
2156 | exit(-1); | |
379f6698 | 2157 | } |
379f6698 | 2158 | |
31e31b8a FB |
2159 | /* Do this so that we can load the interpreter, if need be. We will |
2160 | change some of these later */ | |
31e31b8a | 2161 | bprm->p = setup_arg_pages(bprm->p, bprm, info); |
31e31b8a | 2162 | |
8e62a717 RH |
2163 | if (elf_interpreter) { |
2164 | load_elf_interp(elf_interpreter, &interp_info, bprm->buf); | |
31e31b8a | 2165 | |
8e62a717 RH |
2166 | /* If the program interpreter is one of these two, then assume |
2167 | an iBCS2 image. Otherwise assume a native linux image. */ | |
2168 | ||
2169 | if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0 | |
2170 | || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) { | |
2171 | info->personality = PER_SVR4; | |
31e31b8a | 2172 | |
8e62a717 RH |
2173 | /* Why this, you ask??? Well SVr4 maps page 0 as read-only, |
2174 | and some applications "depend" upon this behavior. Since | |
2175 | we do not have the power to recompile these, we emulate | |
2176 | the SVr4 behavior. Sigh. */ | |
2177 | target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC, | |
2178 | MAP_FIXED | MAP_PRIVATE, -1, 0); | |
2179 | } | |
31e31b8a FB |
2180 | } |
2181 | ||
8e62a717 RH |
2182 | bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex, |
2183 | info, (elf_interpreter ? &interp_info : NULL)); | |
2184 | info->start_stack = bprm->p; | |
2185 | ||
2186 | /* If we have an interpreter, set that as the program's entry point. | |
8e78064e | 2187 | Copy the load_bias as well, to help PPC64 interpret the entry |
8e62a717 RH |
2188 | point as a function descriptor. Do this after creating elf tables |
2189 | so that we copy the original program entry point into the AUXV. */ | |
2190 | if (elf_interpreter) { | |
8e78064e | 2191 | info->load_bias = interp_info.load_bias; |
8e62a717 | 2192 | info->entry = interp_info.entry; |
bf858897 | 2193 | free(elf_interpreter); |
8e62a717 | 2194 | } |
31e31b8a | 2195 | |
edf8e2af MW |
2196 | #ifdef USE_ELF_CORE_DUMP |
2197 | bprm->core_dump = &elf_core_dump; | |
2198 | #endif | |
2199 | ||
31e31b8a FB |
2200 | return 0; |
2201 | } | |
2202 | ||
edf8e2af | 2203 | #ifdef USE_ELF_CORE_DUMP |
edf8e2af MW |
2204 | /* |
2205 | * Definitions to generate Intel SVR4-like core files. | |
a2547a13 | 2206 | * These mostly have the same names as the SVR4 types with "target_elf_" |
edf8e2af MW |
2207 | * tacked on the front to prevent clashes with linux definitions, |
2208 | * and the typedef forms have been avoided. This is mostly like | |
2209 | * the SVR4 structure, but more Linuxy, with things that Linux does | |
2210 | * not support and which gdb doesn't really use excluded. | |
2211 | * | |
2212 | * Fields we don't dump (their contents is zero) in linux-user qemu | |
2213 | * are marked with XXX. | |
2214 | * | |
2215 | * Core dump code is copied from linux kernel (fs/binfmt_elf.c). | |
2216 | * | |
2217 | * Porting ELF coredump for target is (quite) simple process. First you | |
dd0a3651 | 2218 | * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for |
edf8e2af MW |
2219 | * the target resides): |
2220 | * | |
2221 | * #define USE_ELF_CORE_DUMP | |
2222 | * | |
2223 | * Next you define type of register set used for dumping. ELF specification | |
2224 | * says that it needs to be array of elf_greg_t that has size of ELF_NREG. | |
2225 | * | |
c227f099 | 2226 | * typedef <target_regtype> target_elf_greg_t; |
edf8e2af | 2227 | * #define ELF_NREG <number of registers> |
c227f099 | 2228 | * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG]; |
edf8e2af | 2229 | * |
edf8e2af MW |
2230 | * Last step is to implement target specific function that copies registers |
2231 | * from given cpu into just specified register set. Prototype is: | |
2232 | * | |
c227f099 | 2233 | * static void elf_core_copy_regs(taret_elf_gregset_t *regs, |
9349b4f9 | 2234 | * const CPUArchState *env); |
edf8e2af MW |
2235 | * |
2236 | * Parameters: | |
2237 | * regs - copy register values into here (allocated and zeroed by caller) | |
2238 | * env - copy registers from here | |
2239 | * | |
2240 | * Example for ARM target is provided in this file. | |
2241 | */ | |
2242 | ||
2243 | /* An ELF note in memory */ | |
2244 | struct memelfnote { | |
2245 | const char *name; | |
2246 | size_t namesz; | |
2247 | size_t namesz_rounded; | |
2248 | int type; | |
2249 | size_t datasz; | |
80f5ce75 | 2250 | size_t datasz_rounded; |
edf8e2af MW |
2251 | void *data; |
2252 | size_t notesz; | |
2253 | }; | |
2254 | ||
a2547a13 | 2255 | struct target_elf_siginfo { |
f8fd4fc4 PB |
2256 | abi_int si_signo; /* signal number */ |
2257 | abi_int si_code; /* extra code */ | |
2258 | abi_int si_errno; /* errno */ | |
edf8e2af MW |
2259 | }; |
2260 | ||
a2547a13 LD |
2261 | struct target_elf_prstatus { |
2262 | struct target_elf_siginfo pr_info; /* Info associated with signal */ | |
1ddd592f | 2263 | abi_short pr_cursig; /* Current signal */ |
ca98ac83 PB |
2264 | abi_ulong pr_sigpend; /* XXX */ |
2265 | abi_ulong pr_sighold; /* XXX */ | |
c227f099 AL |
2266 | target_pid_t pr_pid; |
2267 | target_pid_t pr_ppid; | |
2268 | target_pid_t pr_pgrp; | |
2269 | target_pid_t pr_sid; | |
edf8e2af MW |
2270 | struct target_timeval pr_utime; /* XXX User time */ |
2271 | struct target_timeval pr_stime; /* XXX System time */ | |
2272 | struct target_timeval pr_cutime; /* XXX Cumulative user time */ | |
2273 | struct target_timeval pr_cstime; /* XXX Cumulative system time */ | |
c227f099 | 2274 | target_elf_gregset_t pr_reg; /* GP registers */ |
f8fd4fc4 | 2275 | abi_int pr_fpvalid; /* XXX */ |
edf8e2af MW |
2276 | }; |
2277 | ||
2278 | #define ELF_PRARGSZ (80) /* Number of chars for args */ | |
2279 | ||
a2547a13 | 2280 | struct target_elf_prpsinfo { |
edf8e2af MW |
2281 | char pr_state; /* numeric process state */ |
2282 | char pr_sname; /* char for pr_state */ | |
2283 | char pr_zomb; /* zombie */ | |
2284 | char pr_nice; /* nice val */ | |
ca98ac83 | 2285 | abi_ulong pr_flag; /* flags */ |
c227f099 AL |
2286 | target_uid_t pr_uid; |
2287 | target_gid_t pr_gid; | |
2288 | target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid; | |
edf8e2af MW |
2289 | /* Lots missing */ |
2290 | char pr_fname[16]; /* filename of executable */ | |
2291 | char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */ | |
2292 | }; | |
2293 | ||
2294 | /* Here is the structure in which status of each thread is captured. */ | |
2295 | struct elf_thread_status { | |
72cf2d4f | 2296 | QTAILQ_ENTRY(elf_thread_status) ets_link; |
a2547a13 | 2297 | struct target_elf_prstatus prstatus; /* NT_PRSTATUS */ |
edf8e2af MW |
2298 | #if 0 |
2299 | elf_fpregset_t fpu; /* NT_PRFPREG */ | |
2300 | struct task_struct *thread; | |
2301 | elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */ | |
2302 | #endif | |
2303 | struct memelfnote notes[1]; | |
2304 | int num_notes; | |
2305 | }; | |
2306 | ||
2307 | struct elf_note_info { | |
2308 | struct memelfnote *notes; | |
a2547a13 LD |
2309 | struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */ |
2310 | struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */ | |
edf8e2af | 2311 | |
72cf2d4f | 2312 | QTAILQ_HEAD(thread_list_head, elf_thread_status) thread_list; |
edf8e2af MW |
2313 | #if 0 |
2314 | /* | |
2315 | * Current version of ELF coredump doesn't support | |
2316 | * dumping fp regs etc. | |
2317 | */ | |
2318 | elf_fpregset_t *fpu; | |
2319 | elf_fpxregset_t *xfpu; | |
2320 | int thread_status_size; | |
2321 | #endif | |
2322 | int notes_size; | |
2323 | int numnote; | |
2324 | }; | |
2325 | ||
2326 | struct vm_area_struct { | |
2327 | abi_ulong vma_start; /* start vaddr of memory region */ | |
2328 | abi_ulong vma_end; /* end vaddr of memory region */ | |
2329 | abi_ulong vma_flags; /* protection etc. flags for the region */ | |
72cf2d4f | 2330 | QTAILQ_ENTRY(vm_area_struct) vma_link; |
edf8e2af MW |
2331 | }; |
2332 | ||
2333 | struct mm_struct { | |
72cf2d4f | 2334 | QTAILQ_HEAD(, vm_area_struct) mm_mmap; |
edf8e2af MW |
2335 | int mm_count; /* number of mappings */ |
2336 | }; | |
2337 | ||
2338 | static struct mm_struct *vma_init(void); | |
2339 | static void vma_delete(struct mm_struct *); | |
2340 | static int vma_add_mapping(struct mm_struct *, abi_ulong, | |
d97ef72e | 2341 | abi_ulong, abi_ulong); |
edf8e2af MW |
2342 | static int vma_get_mapping_count(const struct mm_struct *); |
2343 | static struct vm_area_struct *vma_first(const struct mm_struct *); | |
2344 | static struct vm_area_struct *vma_next(struct vm_area_struct *); | |
2345 | static abi_ulong vma_dump_size(const struct vm_area_struct *); | |
b480d9b7 | 2346 | static int vma_walker(void *priv, abi_ulong start, abi_ulong end, |
d97ef72e | 2347 | unsigned long flags); |
edf8e2af MW |
2348 | |
2349 | static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t); | |
2350 | static void fill_note(struct memelfnote *, const char *, int, | |
d97ef72e | 2351 | unsigned int, void *); |
a2547a13 LD |
2352 | static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int); |
2353 | static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *); | |
edf8e2af MW |
2354 | static void fill_auxv_note(struct memelfnote *, const TaskState *); |
2355 | static void fill_elf_note_phdr(struct elf_phdr *, int, off_t); | |
2356 | static size_t note_size(const struct memelfnote *); | |
2357 | static void free_note_info(struct elf_note_info *); | |
9349b4f9 AF |
2358 | static int fill_note_info(struct elf_note_info *, long, const CPUArchState *); |
2359 | static void fill_thread_info(struct elf_note_info *, const CPUArchState *); | |
edf8e2af MW |
2360 | static int core_dump_filename(const TaskState *, char *, size_t); |
2361 | ||
2362 | static int dump_write(int, const void *, size_t); | |
2363 | static int write_note(struct memelfnote *, int); | |
2364 | static int write_note_info(struct elf_note_info *, int); | |
2365 | ||
2366 | #ifdef BSWAP_NEEDED | |
a2547a13 | 2367 | static void bswap_prstatus(struct target_elf_prstatus *prstatus) |
edf8e2af | 2368 | { |
ca98ac83 PB |
2369 | prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo); |
2370 | prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code); | |
2371 | prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno); | |
edf8e2af | 2372 | prstatus->pr_cursig = tswap16(prstatus->pr_cursig); |
ca98ac83 PB |
2373 | prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend); |
2374 | prstatus->pr_sighold = tswapal(prstatus->pr_sighold); | |
edf8e2af MW |
2375 | prstatus->pr_pid = tswap32(prstatus->pr_pid); |
2376 | prstatus->pr_ppid = tswap32(prstatus->pr_ppid); | |
2377 | prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp); | |
2378 | prstatus->pr_sid = tswap32(prstatus->pr_sid); | |
2379 | /* cpu times are not filled, so we skip them */ | |
2380 | /* regs should be in correct format already */ | |
2381 | prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid); | |
2382 | } | |
2383 | ||
a2547a13 | 2384 | static void bswap_psinfo(struct target_elf_prpsinfo *psinfo) |
edf8e2af | 2385 | { |
ca98ac83 | 2386 | psinfo->pr_flag = tswapal(psinfo->pr_flag); |
edf8e2af MW |
2387 | psinfo->pr_uid = tswap16(psinfo->pr_uid); |
2388 | psinfo->pr_gid = tswap16(psinfo->pr_gid); | |
2389 | psinfo->pr_pid = tswap32(psinfo->pr_pid); | |
2390 | psinfo->pr_ppid = tswap32(psinfo->pr_ppid); | |
2391 | psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp); | |
2392 | psinfo->pr_sid = tswap32(psinfo->pr_sid); | |
2393 | } | |
991f8f0c RH |
2394 | |
2395 | static void bswap_note(struct elf_note *en) | |
2396 | { | |
2397 | bswap32s(&en->n_namesz); | |
2398 | bswap32s(&en->n_descsz); | |
2399 | bswap32s(&en->n_type); | |
2400 | } | |
2401 | #else | |
2402 | static inline void bswap_prstatus(struct target_elf_prstatus *p) { } | |
2403 | static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {} | |
2404 | static inline void bswap_note(struct elf_note *en) { } | |
edf8e2af MW |
2405 | #endif /* BSWAP_NEEDED */ |
2406 | ||
2407 | /* | |
2408 | * Minimal support for linux memory regions. These are needed | |
2409 | * when we are finding out what memory exactly belongs to | |
2410 | * emulated process. No locks needed here, as long as | |
2411 | * thread that received the signal is stopped. | |
2412 | */ | |
2413 | ||
2414 | static struct mm_struct *vma_init(void) | |
2415 | { | |
2416 | struct mm_struct *mm; | |
2417 | ||
7267c094 | 2418 | if ((mm = g_malloc(sizeof (*mm))) == NULL) |
edf8e2af MW |
2419 | return (NULL); |
2420 | ||
2421 | mm->mm_count = 0; | |
72cf2d4f | 2422 | QTAILQ_INIT(&mm->mm_mmap); |
edf8e2af MW |
2423 | |
2424 | return (mm); | |
2425 | } | |
2426 | ||
2427 | static void vma_delete(struct mm_struct *mm) | |
2428 | { | |
2429 | struct vm_area_struct *vma; | |
2430 | ||
2431 | while ((vma = vma_first(mm)) != NULL) { | |
72cf2d4f | 2432 | QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link); |
7267c094 | 2433 | g_free(vma); |
edf8e2af | 2434 | } |
7267c094 | 2435 | g_free(mm); |
edf8e2af MW |
2436 | } |
2437 | ||
2438 | static int vma_add_mapping(struct mm_struct *mm, abi_ulong start, | |
d97ef72e | 2439 | abi_ulong end, abi_ulong flags) |
edf8e2af MW |
2440 | { |
2441 | struct vm_area_struct *vma; | |
2442 | ||
7267c094 | 2443 | if ((vma = g_malloc0(sizeof (*vma))) == NULL) |
edf8e2af MW |
2444 | return (-1); |
2445 | ||
2446 | vma->vma_start = start; | |
2447 | vma->vma_end = end; | |
2448 | vma->vma_flags = flags; | |
2449 | ||
72cf2d4f | 2450 | QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link); |
edf8e2af MW |
2451 | mm->mm_count++; |
2452 | ||
2453 | return (0); | |
2454 | } | |
2455 | ||
2456 | static struct vm_area_struct *vma_first(const struct mm_struct *mm) | |
2457 | { | |
72cf2d4f | 2458 | return (QTAILQ_FIRST(&mm->mm_mmap)); |
edf8e2af MW |
2459 | } |
2460 | ||
2461 | static struct vm_area_struct *vma_next(struct vm_area_struct *vma) | |
2462 | { | |
72cf2d4f | 2463 | return (QTAILQ_NEXT(vma, vma_link)); |
edf8e2af MW |
2464 | } |
2465 | ||
2466 | static int vma_get_mapping_count(const struct mm_struct *mm) | |
2467 | { | |
2468 | return (mm->mm_count); | |
2469 | } | |
2470 | ||
2471 | /* | |
2472 | * Calculate file (dump) size of given memory region. | |
2473 | */ | |
2474 | static abi_ulong vma_dump_size(const struct vm_area_struct *vma) | |
2475 | { | |
2476 | /* if we cannot even read the first page, skip it */ | |
2477 | if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE)) | |
2478 | return (0); | |
2479 | ||
2480 | /* | |
2481 | * Usually we don't dump executable pages as they contain | |
2482 | * non-writable code that debugger can read directly from | |
2483 | * target library etc. However, thread stacks are marked | |
2484 | * also executable so we read in first page of given region | |
2485 | * and check whether it contains elf header. If there is | |
2486 | * no elf header, we dump it. | |
2487 | */ | |
2488 | if (vma->vma_flags & PROT_EXEC) { | |
2489 | char page[TARGET_PAGE_SIZE]; | |
2490 | ||
2491 | copy_from_user(page, vma->vma_start, sizeof (page)); | |
2492 | if ((page[EI_MAG0] == ELFMAG0) && | |
2493 | (page[EI_MAG1] == ELFMAG1) && | |
2494 | (page[EI_MAG2] == ELFMAG2) && | |
2495 | (page[EI_MAG3] == ELFMAG3)) { | |
2496 | /* | |
2497 | * Mappings are possibly from ELF binary. Don't dump | |
2498 | * them. | |
2499 | */ | |
2500 | return (0); | |
2501 | } | |
2502 | } | |
2503 | ||
2504 | return (vma->vma_end - vma->vma_start); | |
2505 | } | |
2506 | ||
b480d9b7 | 2507 | static int vma_walker(void *priv, abi_ulong start, abi_ulong end, |
d97ef72e | 2508 | unsigned long flags) |
edf8e2af MW |
2509 | { |
2510 | struct mm_struct *mm = (struct mm_struct *)priv; | |
2511 | ||
edf8e2af MW |
2512 | vma_add_mapping(mm, start, end, flags); |
2513 | return (0); | |
2514 | } | |
2515 | ||
2516 | static void fill_note(struct memelfnote *note, const char *name, int type, | |
d97ef72e | 2517 | unsigned int sz, void *data) |
edf8e2af MW |
2518 | { |
2519 | unsigned int namesz; | |
2520 | ||
2521 | namesz = strlen(name) + 1; | |
2522 | note->name = name; | |
2523 | note->namesz = namesz; | |
2524 | note->namesz_rounded = roundup(namesz, sizeof (int32_t)); | |
2525 | note->type = type; | |
80f5ce75 LV |
2526 | note->datasz = sz; |
2527 | note->datasz_rounded = roundup(sz, sizeof (int32_t)); | |
2528 | ||
edf8e2af MW |
2529 | note->data = data; |
2530 | ||
2531 | /* | |
2532 | * We calculate rounded up note size here as specified by | |
2533 | * ELF document. | |
2534 | */ | |
2535 | note->notesz = sizeof (struct elf_note) + | |
80f5ce75 | 2536 | note->namesz_rounded + note->datasz_rounded; |
edf8e2af MW |
2537 | } |
2538 | ||
2539 | static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine, | |
d97ef72e | 2540 | uint32_t flags) |
edf8e2af MW |
2541 | { |
2542 | (void) memset(elf, 0, sizeof(*elf)); | |
2543 | ||
2544 | (void) memcpy(elf->e_ident, ELFMAG, SELFMAG); | |
2545 | elf->e_ident[EI_CLASS] = ELF_CLASS; | |
2546 | elf->e_ident[EI_DATA] = ELF_DATA; | |
2547 | elf->e_ident[EI_VERSION] = EV_CURRENT; | |
2548 | elf->e_ident[EI_OSABI] = ELF_OSABI; | |
2549 | ||
2550 | elf->e_type = ET_CORE; | |
2551 | elf->e_machine = machine; | |
2552 | elf->e_version = EV_CURRENT; | |
2553 | elf->e_phoff = sizeof(struct elfhdr); | |
2554 | elf->e_flags = flags; | |
2555 | elf->e_ehsize = sizeof(struct elfhdr); | |
2556 | elf->e_phentsize = sizeof(struct elf_phdr); | |
2557 | elf->e_phnum = segs; | |
2558 | ||
edf8e2af | 2559 | bswap_ehdr(elf); |
edf8e2af MW |
2560 | } |
2561 | ||
2562 | static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset) | |
2563 | { | |
2564 | phdr->p_type = PT_NOTE; | |
2565 | phdr->p_offset = offset; | |
2566 | phdr->p_vaddr = 0; | |
2567 | phdr->p_paddr = 0; | |
2568 | phdr->p_filesz = sz; | |
2569 | phdr->p_memsz = 0; | |
2570 | phdr->p_flags = 0; | |
2571 | phdr->p_align = 0; | |
2572 | ||
991f8f0c | 2573 | bswap_phdr(phdr, 1); |
edf8e2af MW |
2574 | } |
2575 | ||
2576 | static size_t note_size(const struct memelfnote *note) | |
2577 | { | |
2578 | return (note->notesz); | |
2579 | } | |
2580 | ||
a2547a13 | 2581 | static void fill_prstatus(struct target_elf_prstatus *prstatus, |
d97ef72e | 2582 | const TaskState *ts, int signr) |
edf8e2af MW |
2583 | { |
2584 | (void) memset(prstatus, 0, sizeof (*prstatus)); | |
2585 | prstatus->pr_info.si_signo = prstatus->pr_cursig = signr; | |
2586 | prstatus->pr_pid = ts->ts_tid; | |
2587 | prstatus->pr_ppid = getppid(); | |
2588 | prstatus->pr_pgrp = getpgrp(); | |
2589 | prstatus->pr_sid = getsid(0); | |
2590 | ||
edf8e2af | 2591 | bswap_prstatus(prstatus); |
edf8e2af MW |
2592 | } |
2593 | ||
a2547a13 | 2594 | static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts) |
edf8e2af | 2595 | { |
900cfbca | 2596 | char *base_filename; |
edf8e2af MW |
2597 | unsigned int i, len; |
2598 | ||
2599 | (void) memset(psinfo, 0, sizeof (*psinfo)); | |
2600 | ||
2601 | len = ts->info->arg_end - ts->info->arg_start; | |
2602 | if (len >= ELF_PRARGSZ) | |
2603 | len = ELF_PRARGSZ - 1; | |
2604 | if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len)) | |
2605 | return -EFAULT; | |
2606 | for (i = 0; i < len; i++) | |
2607 | if (psinfo->pr_psargs[i] == 0) | |
2608 | psinfo->pr_psargs[i] = ' '; | |
2609 | psinfo->pr_psargs[len] = 0; | |
2610 | ||
2611 | psinfo->pr_pid = getpid(); | |
2612 | psinfo->pr_ppid = getppid(); | |
2613 | psinfo->pr_pgrp = getpgrp(); | |
2614 | psinfo->pr_sid = getsid(0); | |
2615 | psinfo->pr_uid = getuid(); | |
2616 | psinfo->pr_gid = getgid(); | |
2617 | ||
900cfbca JM |
2618 | base_filename = g_path_get_basename(ts->bprm->filename); |
2619 | /* | |
2620 | * Using strncpy here is fine: at max-length, | |
2621 | * this field is not NUL-terminated. | |
2622 | */ | |
edf8e2af | 2623 | (void) strncpy(psinfo->pr_fname, base_filename, |
d97ef72e | 2624 | sizeof(psinfo->pr_fname)); |
edf8e2af | 2625 | |
900cfbca | 2626 | g_free(base_filename); |
edf8e2af | 2627 | bswap_psinfo(psinfo); |
edf8e2af MW |
2628 | return (0); |
2629 | } | |
2630 | ||
2631 | static void fill_auxv_note(struct memelfnote *note, const TaskState *ts) | |
2632 | { | |
2633 | elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv; | |
2634 | elf_addr_t orig_auxv = auxv; | |
edf8e2af | 2635 | void *ptr; |
125b0f55 | 2636 | int len = ts->info->auxv_len; |
edf8e2af MW |
2637 | |
2638 | /* | |
2639 | * Auxiliary vector is stored in target process stack. It contains | |
2640 | * {type, value} pairs that we need to dump into note. This is not | |
2641 | * strictly necessary but we do it here for sake of completeness. | |
2642 | */ | |
2643 | ||
edf8e2af MW |
2644 | /* read in whole auxv vector and copy it to memelfnote */ |
2645 | ptr = lock_user(VERIFY_READ, orig_auxv, len, 0); | |
2646 | if (ptr != NULL) { | |
2647 | fill_note(note, "CORE", NT_AUXV, len, ptr); | |
2648 | unlock_user(ptr, auxv, len); | |
2649 | } | |
2650 | } | |
2651 | ||
2652 | /* | |
2653 | * Constructs name of coredump file. We have following convention | |
2654 | * for the name: | |
2655 | * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core | |
2656 | * | |
2657 | * Returns 0 in case of success, -1 otherwise (errno is set). | |
2658 | */ | |
2659 | static int core_dump_filename(const TaskState *ts, char *buf, | |
d97ef72e | 2660 | size_t bufsize) |
edf8e2af MW |
2661 | { |
2662 | char timestamp[64]; | |
2663 | char *filename = NULL; | |
2664 | char *base_filename = NULL; | |
2665 | struct timeval tv; | |
2666 | struct tm tm; | |
2667 | ||
2668 | assert(bufsize >= PATH_MAX); | |
2669 | ||
2670 | if (gettimeofday(&tv, NULL) < 0) { | |
2671 | (void) fprintf(stderr, "unable to get current timestamp: %s", | |
d97ef72e | 2672 | strerror(errno)); |
edf8e2af MW |
2673 | return (-1); |
2674 | } | |
2675 | ||
2676 | filename = strdup(ts->bprm->filename); | |
2677 | base_filename = strdup(basename(filename)); | |
2678 | (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S", | |
d97ef72e | 2679 | localtime_r(&tv.tv_sec, &tm)); |
edf8e2af | 2680 | (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core", |
d97ef72e | 2681 | base_filename, timestamp, (int)getpid()); |
edf8e2af MW |
2682 | free(base_filename); |
2683 | free(filename); | |
2684 | ||
2685 | return (0); | |
2686 | } | |
2687 | ||
2688 | static int dump_write(int fd, const void *ptr, size_t size) | |
2689 | { | |
2690 | const char *bufp = (const char *)ptr; | |
2691 | ssize_t bytes_written, bytes_left; | |
2692 | struct rlimit dumpsize; | |
2693 | off_t pos; | |
2694 | ||
2695 | bytes_written = 0; | |
2696 | getrlimit(RLIMIT_CORE, &dumpsize); | |
2697 | if ((pos = lseek(fd, 0, SEEK_CUR))==-1) { | |
2698 | if (errno == ESPIPE) { /* not a seekable stream */ | |
2699 | bytes_left = size; | |
2700 | } else { | |
2701 | return pos; | |
2702 | } | |
2703 | } else { | |
2704 | if (dumpsize.rlim_cur <= pos) { | |
2705 | return -1; | |
2706 | } else if (dumpsize.rlim_cur == RLIM_INFINITY) { | |
2707 | bytes_left = size; | |
2708 | } else { | |
2709 | size_t limit_left=dumpsize.rlim_cur - pos; | |
2710 | bytes_left = limit_left >= size ? size : limit_left ; | |
2711 | } | |
2712 | } | |
2713 | ||
2714 | /* | |
2715 | * In normal conditions, single write(2) should do but | |
2716 | * in case of socket etc. this mechanism is more portable. | |
2717 | */ | |
2718 | do { | |
2719 | bytes_written = write(fd, bufp, bytes_left); | |
2720 | if (bytes_written < 0) { | |
2721 | if (errno == EINTR) | |
2722 | continue; | |
2723 | return (-1); | |
2724 | } else if (bytes_written == 0) { /* eof */ | |
2725 | return (-1); | |
2726 | } | |
2727 | bufp += bytes_written; | |
2728 | bytes_left -= bytes_written; | |
2729 | } while (bytes_left > 0); | |
2730 | ||
2731 | return (0); | |
2732 | } | |
2733 | ||
2734 | static int write_note(struct memelfnote *men, int fd) | |
2735 | { | |
2736 | struct elf_note en; | |
2737 | ||
2738 | en.n_namesz = men->namesz; | |
2739 | en.n_type = men->type; | |
2740 | en.n_descsz = men->datasz; | |
2741 | ||
edf8e2af | 2742 | bswap_note(&en); |
edf8e2af MW |
2743 | |
2744 | if (dump_write(fd, &en, sizeof(en)) != 0) | |
2745 | return (-1); | |
2746 | if (dump_write(fd, men->name, men->namesz_rounded) != 0) | |
2747 | return (-1); | |
80f5ce75 | 2748 | if (dump_write(fd, men->data, men->datasz_rounded) != 0) |
edf8e2af MW |
2749 | return (-1); |
2750 | ||
2751 | return (0); | |
2752 | } | |
2753 | ||
9349b4f9 | 2754 | static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env) |
edf8e2af | 2755 | { |
0429a971 AF |
2756 | CPUState *cpu = ENV_GET_CPU((CPUArchState *)env); |
2757 | TaskState *ts = (TaskState *)cpu->opaque; | |
edf8e2af MW |
2758 | struct elf_thread_status *ets; |
2759 | ||
7267c094 | 2760 | ets = g_malloc0(sizeof (*ets)); |
edf8e2af MW |
2761 | ets->num_notes = 1; /* only prstatus is dumped */ |
2762 | fill_prstatus(&ets->prstatus, ts, 0); | |
2763 | elf_core_copy_regs(&ets->prstatus.pr_reg, env); | |
2764 | fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus), | |
d97ef72e | 2765 | &ets->prstatus); |
edf8e2af | 2766 | |
72cf2d4f | 2767 | QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link); |
edf8e2af MW |
2768 | |
2769 | info->notes_size += note_size(&ets->notes[0]); | |
2770 | } | |
2771 | ||
6afafa86 PM |
2772 | static void init_note_info(struct elf_note_info *info) |
2773 | { | |
2774 | /* Initialize the elf_note_info structure so that it is at | |
2775 | * least safe to call free_note_info() on it. Must be | |
2776 | * called before calling fill_note_info(). | |
2777 | */ | |
2778 | memset(info, 0, sizeof (*info)); | |
2779 | QTAILQ_INIT(&info->thread_list); | |
2780 | } | |
2781 | ||
edf8e2af | 2782 | static int fill_note_info(struct elf_note_info *info, |
9349b4f9 | 2783 | long signr, const CPUArchState *env) |
edf8e2af MW |
2784 | { |
2785 | #define NUMNOTES 3 | |
0429a971 AF |
2786 | CPUState *cpu = ENV_GET_CPU((CPUArchState *)env); |
2787 | TaskState *ts = (TaskState *)cpu->opaque; | |
edf8e2af MW |
2788 | int i; |
2789 | ||
7267c094 | 2790 | info->notes = g_malloc0(NUMNOTES * sizeof (struct memelfnote)); |
edf8e2af MW |
2791 | if (info->notes == NULL) |
2792 | return (-ENOMEM); | |
7267c094 | 2793 | info->prstatus = g_malloc0(sizeof (*info->prstatus)); |
edf8e2af MW |
2794 | if (info->prstatus == NULL) |
2795 | return (-ENOMEM); | |
7267c094 | 2796 | info->psinfo = g_malloc0(sizeof (*info->psinfo)); |
edf8e2af MW |
2797 | if (info->prstatus == NULL) |
2798 | return (-ENOMEM); | |
2799 | ||
2800 | /* | |
2801 | * First fill in status (and registers) of current thread | |
2802 | * including process info & aux vector. | |
2803 | */ | |
2804 | fill_prstatus(info->prstatus, ts, signr); | |
2805 | elf_core_copy_regs(&info->prstatus->pr_reg, env); | |
2806 | fill_note(&info->notes[0], "CORE", NT_PRSTATUS, | |
d97ef72e | 2807 | sizeof (*info->prstatus), info->prstatus); |
edf8e2af MW |
2808 | fill_psinfo(info->psinfo, ts); |
2809 | fill_note(&info->notes[1], "CORE", NT_PRPSINFO, | |
d97ef72e | 2810 | sizeof (*info->psinfo), info->psinfo); |
edf8e2af MW |
2811 | fill_auxv_note(&info->notes[2], ts); |
2812 | info->numnote = 3; | |
2813 | ||
2814 | info->notes_size = 0; | |
2815 | for (i = 0; i < info->numnote; i++) | |
2816 | info->notes_size += note_size(&info->notes[i]); | |
2817 | ||
2818 | /* read and fill status of all threads */ | |
2819 | cpu_list_lock(); | |
bdc44640 | 2820 | CPU_FOREACH(cpu) { |
a2247f8e | 2821 | if (cpu == thread_cpu) { |
edf8e2af | 2822 | continue; |
182735ef AF |
2823 | } |
2824 | fill_thread_info(info, (CPUArchState *)cpu->env_ptr); | |
edf8e2af MW |
2825 | } |
2826 | cpu_list_unlock(); | |
2827 | ||
2828 | return (0); | |
2829 | } | |
2830 | ||
2831 | static void free_note_info(struct elf_note_info *info) | |
2832 | { | |
2833 | struct elf_thread_status *ets; | |
2834 | ||
72cf2d4f BS |
2835 | while (!QTAILQ_EMPTY(&info->thread_list)) { |
2836 | ets = QTAILQ_FIRST(&info->thread_list); | |
2837 | QTAILQ_REMOVE(&info->thread_list, ets, ets_link); | |
7267c094 | 2838 | g_free(ets); |
edf8e2af MW |
2839 | } |
2840 | ||
7267c094 AL |
2841 | g_free(info->prstatus); |
2842 | g_free(info->psinfo); | |
2843 | g_free(info->notes); | |
edf8e2af MW |
2844 | } |
2845 | ||
2846 | static int write_note_info(struct elf_note_info *info, int fd) | |
2847 | { | |
2848 | struct elf_thread_status *ets; | |
2849 | int i, error = 0; | |
2850 | ||
2851 | /* write prstatus, psinfo and auxv for current thread */ | |
2852 | for (i = 0; i < info->numnote; i++) | |
2853 | if ((error = write_note(&info->notes[i], fd)) != 0) | |
2854 | return (error); | |
2855 | ||
2856 | /* write prstatus for each thread */ | |
2857 | for (ets = info->thread_list.tqh_first; ets != NULL; | |
d97ef72e | 2858 | ets = ets->ets_link.tqe_next) { |
edf8e2af MW |
2859 | if ((error = write_note(&ets->notes[0], fd)) != 0) |
2860 | return (error); | |
2861 | } | |
2862 | ||
2863 | return (0); | |
2864 | } | |
2865 | ||
2866 | /* | |
2867 | * Write out ELF coredump. | |
2868 | * | |
2869 | * See documentation of ELF object file format in: | |
2870 | * http://www.caldera.com/developers/devspecs/gabi41.pdf | |
2871 | * | |
2872 | * Coredump format in linux is following: | |
2873 | * | |
2874 | * 0 +----------------------+ \ | |
2875 | * | ELF header | ET_CORE | | |
2876 | * +----------------------+ | | |
2877 | * | ELF program headers | |--- headers | |
2878 | * | - NOTE section | | | |
2879 | * | - PT_LOAD sections | | | |
2880 | * +----------------------+ / | |
2881 | * | NOTEs: | | |
2882 | * | - NT_PRSTATUS | | |
2883 | * | - NT_PRSINFO | | |
2884 | * | - NT_AUXV | | |
2885 | * +----------------------+ <-- aligned to target page | |
2886 | * | Process memory dump | | |
2887 | * : : | |
2888 | * . . | |
2889 | * : : | |
2890 | * | | | |
2891 | * +----------------------+ | |
2892 | * | |
2893 | * NT_PRSTATUS -> struct elf_prstatus (per thread) | |
2894 | * NT_PRSINFO -> struct elf_prpsinfo | |
2895 | * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()). | |
2896 | * | |
2897 | * Format follows System V format as close as possible. Current | |
2898 | * version limitations are as follows: | |
2899 | * - no floating point registers are dumped | |
2900 | * | |
2901 | * Function returns 0 in case of success, negative errno otherwise. | |
2902 | * | |
2903 | * TODO: make this work also during runtime: it should be | |
2904 | * possible to force coredump from running process and then | |
2905 | * continue processing. For example qemu could set up SIGUSR2 | |
2906 | * handler (provided that target process haven't registered | |
2907 | * handler for that) that does the dump when signal is received. | |
2908 | */ | |
9349b4f9 | 2909 | static int elf_core_dump(int signr, const CPUArchState *env) |
edf8e2af | 2910 | { |
0429a971 AF |
2911 | const CPUState *cpu = ENV_GET_CPU((CPUArchState *)env); |
2912 | const TaskState *ts = (const TaskState *)cpu->opaque; | |
edf8e2af MW |
2913 | struct vm_area_struct *vma = NULL; |
2914 | char corefile[PATH_MAX]; | |
2915 | struct elf_note_info info; | |
2916 | struct elfhdr elf; | |
2917 | struct elf_phdr phdr; | |
2918 | struct rlimit dumpsize; | |
2919 | struct mm_struct *mm = NULL; | |
2920 | off_t offset = 0, data_offset = 0; | |
2921 | int segs = 0; | |
2922 | int fd = -1; | |
2923 | ||
6afafa86 PM |
2924 | init_note_info(&info); |
2925 | ||
edf8e2af MW |
2926 | errno = 0; |
2927 | getrlimit(RLIMIT_CORE, &dumpsize); | |
2928 | if (dumpsize.rlim_cur == 0) | |
d97ef72e | 2929 | return 0; |
edf8e2af MW |
2930 | |
2931 | if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0) | |
2932 | return (-errno); | |
2933 | ||
2934 | if ((fd = open(corefile, O_WRONLY | O_CREAT, | |
d97ef72e | 2935 | S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) |
edf8e2af MW |
2936 | return (-errno); |
2937 | ||
2938 | /* | |
2939 | * Walk through target process memory mappings and | |
2940 | * set up structure containing this information. After | |
2941 | * this point vma_xxx functions can be used. | |
2942 | */ | |
2943 | if ((mm = vma_init()) == NULL) | |
2944 | goto out; | |
2945 | ||
2946 | walk_memory_regions(mm, vma_walker); | |
2947 | segs = vma_get_mapping_count(mm); | |
2948 | ||
2949 | /* | |
2950 | * Construct valid coredump ELF header. We also | |
2951 | * add one more segment for notes. | |
2952 | */ | |
2953 | fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0); | |
2954 | if (dump_write(fd, &elf, sizeof (elf)) != 0) | |
2955 | goto out; | |
2956 | ||
2957 | /* fill in in-memory version of notes */ | |
2958 | if (fill_note_info(&info, signr, env) < 0) | |
2959 | goto out; | |
2960 | ||
2961 | offset += sizeof (elf); /* elf header */ | |
2962 | offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */ | |
2963 | ||
2964 | /* write out notes program header */ | |
2965 | fill_elf_note_phdr(&phdr, info.notes_size, offset); | |
2966 | ||
2967 | offset += info.notes_size; | |
2968 | if (dump_write(fd, &phdr, sizeof (phdr)) != 0) | |
2969 | goto out; | |
2970 | ||
2971 | /* | |
2972 | * ELF specification wants data to start at page boundary so | |
2973 | * we align it here. | |
2974 | */ | |
80f5ce75 | 2975 | data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE); |
edf8e2af MW |
2976 | |
2977 | /* | |
2978 | * Write program headers for memory regions mapped in | |
2979 | * the target process. | |
2980 | */ | |
2981 | for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) { | |
2982 | (void) memset(&phdr, 0, sizeof (phdr)); | |
2983 | ||
2984 | phdr.p_type = PT_LOAD; | |
2985 | phdr.p_offset = offset; | |
2986 | phdr.p_vaddr = vma->vma_start; | |
2987 | phdr.p_paddr = 0; | |
2988 | phdr.p_filesz = vma_dump_size(vma); | |
2989 | offset += phdr.p_filesz; | |
2990 | phdr.p_memsz = vma->vma_end - vma->vma_start; | |
2991 | phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0; | |
2992 | if (vma->vma_flags & PROT_WRITE) | |
2993 | phdr.p_flags |= PF_W; | |
2994 | if (vma->vma_flags & PROT_EXEC) | |
2995 | phdr.p_flags |= PF_X; | |
2996 | phdr.p_align = ELF_EXEC_PAGESIZE; | |
2997 | ||
80f5ce75 | 2998 | bswap_phdr(&phdr, 1); |
edf8e2af MW |
2999 | dump_write(fd, &phdr, sizeof (phdr)); |
3000 | } | |
3001 | ||
3002 | /* | |
3003 | * Next we write notes just after program headers. No | |
3004 | * alignment needed here. | |
3005 | */ | |
3006 | if (write_note_info(&info, fd) < 0) | |
3007 | goto out; | |
3008 | ||
3009 | /* align data to page boundary */ | |
edf8e2af MW |
3010 | if (lseek(fd, data_offset, SEEK_SET) != data_offset) |
3011 | goto out; | |
3012 | ||
3013 | /* | |
3014 | * Finally we can dump process memory into corefile as well. | |
3015 | */ | |
3016 | for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) { | |
3017 | abi_ulong addr; | |
3018 | abi_ulong end; | |
3019 | ||
3020 | end = vma->vma_start + vma_dump_size(vma); | |
3021 | ||
3022 | for (addr = vma->vma_start; addr < end; | |
d97ef72e | 3023 | addr += TARGET_PAGE_SIZE) { |
edf8e2af MW |
3024 | char page[TARGET_PAGE_SIZE]; |
3025 | int error; | |
3026 | ||
3027 | /* | |
3028 | * Read in page from target process memory and | |
3029 | * write it to coredump file. | |
3030 | */ | |
3031 | error = copy_from_user(page, addr, sizeof (page)); | |
3032 | if (error != 0) { | |
49995e17 | 3033 | (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n", |
d97ef72e | 3034 | addr); |
edf8e2af MW |
3035 | errno = -error; |
3036 | goto out; | |
3037 | } | |
3038 | if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0) | |
3039 | goto out; | |
3040 | } | |
3041 | } | |
3042 | ||
d97ef72e | 3043 | out: |
edf8e2af MW |
3044 | free_note_info(&info); |
3045 | if (mm != NULL) | |
3046 | vma_delete(mm); | |
3047 | (void) close(fd); | |
3048 | ||
3049 | if (errno != 0) | |
3050 | return (-errno); | |
3051 | return (0); | |
3052 | } | |
edf8e2af MW |
3053 | #endif /* USE_ELF_CORE_DUMP */ |
3054 | ||
e5fe0c52 PB |
3055 | void do_init_thread(struct target_pt_regs *regs, struct image_info *infop) |
3056 | { | |
3057 | init_thread(regs, infop); | |
3058 | } |