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