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