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