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