| 1 | /* This is the Linux kernel elf-loading code, ported into user space */ |
| 2 | #include "qemu/osdep.h" |
| 3 | #include <sys/param.h> |
| 4 | |
| 5 | #include <sys/prctl.h> |
| 6 | #include <sys/resource.h> |
| 7 | |
| 8 | #include "qemu.h" |
| 9 | #include "user/tswap-target.h" |
| 10 | #include "user/page-protection.h" |
| 11 | #include "exec/page-protection.h" |
| 12 | #include "exec/mmap-lock.h" |
| 13 | #include "exec/translation-block.h" |
| 14 | #include "exec/tswap.h" |
| 15 | #include "user-internals.h" |
| 16 | #include "signal-common.h" |
| 17 | #include "loader.h" |
| 18 | #include "user-mmap.h" |
| 19 | #include "disas/disas.h" |
| 20 | #include "qemu/bitops.h" |
| 21 | #include "qemu/path.h" |
| 22 | #include "qemu/queue.h" |
| 23 | #include "qemu/guest-random.h" |
| 24 | #include "qemu/units.h" |
| 25 | #include "qemu/lockable.h" |
| 26 | #include "qapi/error.h" |
| 27 | #include "qemu/error-report.h" |
| 28 | #include "target_elf.h" |
| 29 | #include "target_signal.h" |
| 30 | #include "tcg/debuginfo.h" |
| 31 | |
| 32 | #ifdef TARGET_ARM |
| 33 | #include "target/arm/cpu-features.h" |
| 34 | #endif |
| 35 | |
| 36 | #ifndef TARGET_ARCH_HAS_SIGTRAMP_PAGE |
| 37 | #define TARGET_ARCH_HAS_SIGTRAMP_PAGE 0 |
| 38 | #endif |
| 39 | |
| 40 | #define ELF_OSABI ELFOSABI_SYSV |
| 41 | |
| 42 | /* from personality.h */ |
| 43 | |
| 44 | /* |
| 45 | * Flags for bug emulation. |
| 46 | * |
| 47 | * These occupy the top three bytes. |
| 48 | */ |
| 49 | enum { |
| 50 | ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */ |
| 51 | FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to |
| 52 | descriptors (signal handling) */ |
| 53 | MMAP_PAGE_ZERO = 0x0100000, |
| 54 | ADDR_COMPAT_LAYOUT = 0x0200000, |
| 55 | READ_IMPLIES_EXEC = 0x0400000, |
| 56 | ADDR_LIMIT_32BIT = 0x0800000, |
| 57 | SHORT_INODE = 0x1000000, |
| 58 | WHOLE_SECONDS = 0x2000000, |
| 59 | STICKY_TIMEOUTS = 0x4000000, |
| 60 | ADDR_LIMIT_3GB = 0x8000000, |
| 61 | }; |
| 62 | |
| 63 | /* |
| 64 | * Personality types. |
| 65 | * |
| 66 | * These go in the low byte. Avoid using the top bit, it will |
| 67 | * conflict with error returns. |
| 68 | */ |
| 69 | enum { |
| 70 | PER_LINUX = 0x0000, |
| 71 | PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT, |
| 72 | PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS, |
| 73 | PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO, |
| 74 | PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE, |
| 75 | PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE, |
| 76 | PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS, |
| 77 | PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE, |
| 78 | PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS, |
| 79 | PER_BSD = 0x0006, |
| 80 | PER_SUNOS = 0x0006 | STICKY_TIMEOUTS, |
| 81 | PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE, |
| 82 | PER_LINUX32 = 0x0008, |
| 83 | PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB, |
| 84 | PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */ |
| 85 | PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */ |
| 86 | PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */ |
| 87 | PER_RISCOS = 0x000c, |
| 88 | PER_SOLARIS = 0x000d | STICKY_TIMEOUTS, |
| 89 | PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO, |
| 90 | PER_OSF4 = 0x000f, /* OSF/1 v4 */ |
| 91 | PER_HPUX = 0x0010, |
| 92 | PER_MASK = 0x00ff, |
| 93 | }; |
| 94 | |
| 95 | /* |
| 96 | * Return the base personality without flags. |
| 97 | */ |
| 98 | #define personality(pers) (pers & PER_MASK) |
| 99 | |
| 100 | int info_is_fdpic(struct image_info *info) |
| 101 | { |
| 102 | return info->personality == PER_LINUX_FDPIC; |
| 103 | } |
| 104 | |
| 105 | #if TARGET_BIG_ENDIAN |
| 106 | #define ELF_DATA ELFDATA2MSB |
| 107 | #else |
| 108 | #define ELF_DATA ELFDATA2LSB |
| 109 | #endif |
| 110 | |
| 111 | #ifdef USE_UID16 |
| 112 | typedef abi_ushort target_uid_t; |
| 113 | typedef abi_ushort target_gid_t; |
| 114 | #else |
| 115 | typedef abi_uint target_uid_t; |
| 116 | typedef abi_uint target_gid_t; |
| 117 | #endif |
| 118 | typedef abi_int target_pid_t; |
| 119 | |
| 120 | #ifndef elf_check_machine |
| 121 | #define elf_check_machine(x) ((x) == ELF_MACHINE) |
| 122 | #endif |
| 123 | |
| 124 | #ifndef elf_check_abi |
| 125 | #define elf_check_abi(x) (1) |
| 126 | #endif |
| 127 | |
| 128 | #ifndef STACK_GROWS_DOWN |
| 129 | #define STACK_GROWS_DOWN 1 |
| 130 | #endif |
| 131 | |
| 132 | #ifndef STACK_ALIGNMENT |
| 133 | #define STACK_ALIGNMENT 16 |
| 134 | #endif |
| 135 | |
| 136 | #ifdef TARGET_ABI32 |
| 137 | #undef ELF_CLASS |
| 138 | #define ELF_CLASS ELFCLASS32 |
| 139 | #undef bswaptls |
| 140 | #define bswaptls(ptr) bswap32s(ptr) |
| 141 | #endif |
| 142 | |
| 143 | #ifndef EXSTACK_DEFAULT |
| 144 | #define EXSTACK_DEFAULT false |
| 145 | #endif |
| 146 | |
| 147 | /* |
| 148 | * Provide fallback definitions that the target may omit. |
| 149 | * One way or another, we'll get a link error if the setting of |
| 150 | * HAVE_* doesn't match the implementation. |
| 151 | */ |
| 152 | #ifndef HAVE_ELF_HWCAP |
| 153 | abi_ulong get_elf_hwcap(CPUState *cs) { return 0; } |
| 154 | #endif |
| 155 | #ifndef HAVE_ELF_HWCAP2 |
| 156 | abi_ulong get_elf_hwcap2(CPUState *cs) { g_assert_not_reached(); } |
| 157 | #define HAVE_ELF_HWCAP2 0 |
| 158 | #endif |
| 159 | #ifndef HAVE_ELF_PLATFORM |
| 160 | const char *get_elf_platform(CPUState *cs) { return NULL; } |
| 161 | #endif |
| 162 | #ifndef HAVE_ELF_BASE_PLATFORM |
| 163 | const char *get_elf_base_platform(CPUState *cs) { return NULL; } |
| 164 | #endif |
| 165 | |
| 166 | #ifndef HAVE_ELF_GNU_PROPERTY |
| 167 | bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz, |
| 168 | const uint32_t *data, struct image_info *info, |
| 169 | Error **errp) |
| 170 | { |
| 171 | g_assert_not_reached(); |
| 172 | } |
| 173 | #define HAVE_ELF_GNU_PROPERTY 0 |
| 174 | #endif |
| 175 | |
| 176 | #include "elf.h" |
| 177 | |
| 178 | #define DLINFO_ITEMS 16 |
| 179 | |
| 180 | static inline void memcpy_fromfs(void * to, const void * from, unsigned long n) |
| 181 | { |
| 182 | memcpy(to, from, n); |
| 183 | } |
| 184 | |
| 185 | static void bswap_ehdr(struct elfhdr *ehdr) |
| 186 | { |
| 187 | if (!target_needs_bswap()) { |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | bswap16s(&ehdr->e_type); /* Object file type */ |
| 192 | bswap16s(&ehdr->e_machine); /* Architecture */ |
| 193 | bswap32s(&ehdr->e_version); /* Object file version */ |
| 194 | bswaptls(&ehdr->e_entry); /* Entry point virtual address */ |
| 195 | bswaptls(&ehdr->e_phoff); /* Program header table file offset */ |
| 196 | bswaptls(&ehdr->e_shoff); /* Section header table file offset */ |
| 197 | bswap32s(&ehdr->e_flags); /* Processor-specific flags */ |
| 198 | bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */ |
| 199 | bswap16s(&ehdr->e_phentsize); /* Program header table entry size */ |
| 200 | bswap16s(&ehdr->e_phnum); /* Program header table entry count */ |
| 201 | bswap16s(&ehdr->e_shentsize); /* Section header table entry size */ |
| 202 | bswap16s(&ehdr->e_shnum); /* Section header table entry count */ |
| 203 | bswap16s(&ehdr->e_shstrndx); /* Section header string table index */ |
| 204 | } |
| 205 | |
| 206 | static void bswap_phdr(struct elf_phdr *phdr, int phnum) |
| 207 | { |
| 208 | if (!target_needs_bswap()) { |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | for (int i = 0; i < phnum; ++i, ++phdr) { |
| 213 | bswap32s(&phdr->p_type); /* Segment type */ |
| 214 | bswap32s(&phdr->p_flags); /* Segment flags */ |
| 215 | bswaptls(&phdr->p_offset); /* Segment file offset */ |
| 216 | bswaptls(&phdr->p_vaddr); /* Segment virtual address */ |
| 217 | bswaptls(&phdr->p_paddr); /* Segment physical address */ |
| 218 | bswaptls(&phdr->p_filesz); /* Segment size in file */ |
| 219 | bswaptls(&phdr->p_memsz); /* Segment size in memory */ |
| 220 | bswaptls(&phdr->p_align); /* Segment alignment */ |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | static void bswap_shdr(struct elf_shdr *shdr, int shnum) |
| 225 | { |
| 226 | if (!target_needs_bswap()) { |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | for (int i = 0; i < shnum; ++i, ++shdr) { |
| 231 | bswap32s(&shdr->sh_name); |
| 232 | bswap32s(&shdr->sh_type); |
| 233 | bswaptls(&shdr->sh_flags); |
| 234 | bswaptls(&shdr->sh_addr); |
| 235 | bswaptls(&shdr->sh_offset); |
| 236 | bswaptls(&shdr->sh_size); |
| 237 | bswap32s(&shdr->sh_link); |
| 238 | bswap32s(&shdr->sh_info); |
| 239 | bswaptls(&shdr->sh_addralign); |
| 240 | bswaptls(&shdr->sh_entsize); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | static void bswap_sym(struct elf_sym *sym) |
| 245 | { |
| 246 | if (!target_needs_bswap()) { |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | bswap32s(&sym->st_name); |
| 251 | bswaptls(&sym->st_value); |
| 252 | bswaptls(&sym->st_size); |
| 253 | bswap16s(&sym->st_shndx); |
| 254 | } |
| 255 | |
| 256 | #ifdef TARGET_MIPS |
| 257 | static void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) |
| 258 | { |
| 259 | if (!target_needs_bswap()) { |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | bswap16s(&abiflags->version); |
| 264 | bswap32s(&abiflags->ases); |
| 265 | bswap32s(&abiflags->isa_ext); |
| 266 | bswap32s(&abiflags->flags1); |
| 267 | bswap32s(&abiflags->flags2); |
| 268 | } |
| 269 | #endif |
| 270 | |
| 271 | #ifdef HAVE_ELF_CORE_DUMP |
| 272 | static int elf_core_dump(int, const CPUArchState *); |
| 273 | #endif /* HAVE_ELF_CORE_DUMP */ |
| 274 | static void load_symbols(struct elfhdr *hdr, const ImageSource *src, |
| 275 | abi_ulong load_bias); |
| 276 | |
| 277 | /* Verify the portions of EHDR within E_IDENT for the target. |
| 278 | This can be performed before bswapping the entire header. */ |
| 279 | static bool elf_check_ident(struct elfhdr *ehdr) |
| 280 | { |
| 281 | return (ehdr->e_ident[EI_MAG0] == ELFMAG0 |
| 282 | && ehdr->e_ident[EI_MAG1] == ELFMAG1 |
| 283 | && ehdr->e_ident[EI_MAG2] == ELFMAG2 |
| 284 | && ehdr->e_ident[EI_MAG3] == ELFMAG3 |
| 285 | && ehdr->e_ident[EI_CLASS] == ELF_CLASS |
| 286 | && ehdr->e_ident[EI_DATA] == ELF_DATA |
| 287 | && ehdr->e_ident[EI_VERSION] == EV_CURRENT); |
| 288 | } |
| 289 | |
| 290 | /* Verify the portions of EHDR outside of E_IDENT for the target. |
| 291 | This has to wait until after bswapping the header. */ |
| 292 | static bool elf_check_ehdr(struct elfhdr *ehdr) |
| 293 | { |
| 294 | return (elf_check_machine(ehdr->e_machine) |
| 295 | && elf_check_abi(ehdr->e_flags) |
| 296 | && ehdr->e_ehsize == sizeof(struct elfhdr) |
| 297 | && ehdr->e_phentsize == sizeof(struct elf_phdr) |
| 298 | && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN)); |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * 'copy_elf_strings()' copies argument/envelope strings from user |
| 303 | * memory to free pages in kernel mem. These are in a format ready |
| 304 | * to be put directly into the top of new user memory. |
| 305 | * |
| 306 | */ |
| 307 | static abi_ulong copy_elf_strings(int argc, char **argv, char *scratch, |
| 308 | abi_ulong p, abi_ulong stack_limit) |
| 309 | { |
| 310 | char *tmp; |
| 311 | int len, i; |
| 312 | abi_ulong top = p; |
| 313 | |
| 314 | if (!p) { |
| 315 | return 0; /* bullet-proofing */ |
| 316 | } |
| 317 | |
| 318 | if (STACK_GROWS_DOWN) { |
| 319 | int offset = ((p - 1) % TARGET_PAGE_SIZE) + 1; |
| 320 | for (i = argc - 1; i >= 0; --i) { |
| 321 | tmp = argv[i]; |
| 322 | if (!tmp) { |
| 323 | fprintf(stderr, "VFS: argc is wrong"); |
| 324 | exit(-1); |
| 325 | } |
| 326 | len = strlen(tmp) + 1; |
| 327 | tmp += len; |
| 328 | |
| 329 | if (len > (p - stack_limit)) { |
| 330 | return 0; |
| 331 | } |
| 332 | while (len) { |
| 333 | int bytes_to_copy = (len > offset) ? offset : len; |
| 334 | tmp -= bytes_to_copy; |
| 335 | p -= bytes_to_copy; |
| 336 | offset -= bytes_to_copy; |
| 337 | len -= bytes_to_copy; |
| 338 | |
| 339 | memcpy_fromfs(scratch + offset, tmp, bytes_to_copy); |
| 340 | |
| 341 | if (offset == 0) { |
| 342 | memcpy_to_target(p, scratch, top - p); |
| 343 | top = p; |
| 344 | offset = TARGET_PAGE_SIZE; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | if (p != top) { |
| 349 | memcpy_to_target(p, scratch + offset, top - p); |
| 350 | } |
| 351 | } else { |
| 352 | int remaining = TARGET_PAGE_SIZE - (p % TARGET_PAGE_SIZE); |
| 353 | for (i = 0; i < argc; ++i) { |
| 354 | tmp = argv[i]; |
| 355 | if (!tmp) { |
| 356 | fprintf(stderr, "VFS: argc is wrong"); |
| 357 | exit(-1); |
| 358 | } |
| 359 | len = strlen(tmp) + 1; |
| 360 | if (len > (stack_limit - p)) { |
| 361 | return 0; |
| 362 | } |
| 363 | while (len) { |
| 364 | int bytes_to_copy = (len > remaining) ? remaining : len; |
| 365 | |
| 366 | memcpy_fromfs(scratch + (p - top), tmp, bytes_to_copy); |
| 367 | |
| 368 | tmp += bytes_to_copy; |
| 369 | remaining -= bytes_to_copy; |
| 370 | p += bytes_to_copy; |
| 371 | len -= bytes_to_copy; |
| 372 | |
| 373 | if (remaining == 0) { |
| 374 | memcpy_to_target(top, scratch, p - top); |
| 375 | top = p; |
| 376 | remaining = TARGET_PAGE_SIZE; |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | if (p != top) { |
| 381 | memcpy_to_target(top, scratch, p - top); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | return p; |
| 386 | } |
| 387 | |
| 388 | /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of |
| 389 | * argument/environment space. Newer kernels (>2.6.33) allow more, |
| 390 | * dependent on stack size, but guarantee at least 32 pages for |
| 391 | * backwards compatibility. |
| 392 | */ |
| 393 | #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE) |
| 394 | |
| 395 | static abi_ulong setup_arg_pages(struct linux_binprm *bprm, |
| 396 | struct image_info *info) |
| 397 | { |
| 398 | abi_ulong size, error, guard; |
| 399 | int prot; |
| 400 | |
| 401 | size = guest_stack_size; |
| 402 | if (size < STACK_LOWER_LIMIT) { |
| 403 | size = STACK_LOWER_LIMIT; |
| 404 | } |
| 405 | |
| 406 | if (STACK_GROWS_DOWN) { |
| 407 | guard = TARGET_PAGE_SIZE; |
| 408 | if (guard < qemu_real_host_page_size()) { |
| 409 | guard = qemu_real_host_page_size(); |
| 410 | } |
| 411 | } else { |
| 412 | /* no guard page for hppa target where stack grows upwards. */ |
| 413 | guard = 0; |
| 414 | } |
| 415 | |
| 416 | prot = PROT_READ | PROT_WRITE; |
| 417 | if (info->exec_stack) { |
| 418 | prot |= PROT_EXEC; |
| 419 | } |
| 420 | error = target_mmap(0, size + guard, prot, |
| 421 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 422 | if (error == -1) { |
| 423 | perror("mmap stack"); |
| 424 | exit(-1); |
| 425 | } |
| 426 | |
| 427 | /* We reserve one extra page at the top of the stack as guard. */ |
| 428 | if (STACK_GROWS_DOWN) { |
| 429 | target_mprotect(error, guard, PROT_NONE); |
| 430 | info->stack_limit = error + guard; |
| 431 | return info->stack_limit + size - sizeof(void *); |
| 432 | } else { |
| 433 | info->stack_limit = error + size; |
| 434 | return error; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * zero_bss: |
| 440 | * |
| 441 | * Map and zero the bss. We need to explicitly zero any fractional pages |
| 442 | * after the data section (i.e. bss). Return false on mapping failure. |
| 443 | */ |
| 444 | static bool zero_bss(abi_ulong start_bss, abi_ulong end_bss, |
| 445 | int prot, Error **errp) |
| 446 | { |
| 447 | abi_ulong align_bss; |
| 448 | |
| 449 | align_bss = TARGET_PAGE_ALIGN(start_bss); |
| 450 | end_bss = TARGET_PAGE_ALIGN(end_bss); |
| 451 | |
| 452 | if (start_bss < align_bss) { |
| 453 | int flags = page_get_flags(start_bss); |
| 454 | |
| 455 | if (!(flags & PAGE_RWX)) { |
| 456 | /* |
| 457 | * The whole address space of the executable was reserved |
| 458 | * at the start, therefore all pages will be VALID. |
| 459 | * But assuming there are no PROT_NONE PT_LOAD segments, |
| 460 | * a PROT_NONE page means no data all bss, and we can |
| 461 | * simply extend the new anon mapping back to the start |
| 462 | * of the page of bss. |
| 463 | */ |
| 464 | align_bss -= TARGET_PAGE_SIZE; |
| 465 | } else { |
| 466 | abi_ulong start_page_aligned = start_bss & TARGET_PAGE_MASK; |
| 467 | /* |
| 468 | * The logical OR between flags and PAGE_WRITE works because |
| 469 | * in include/exec/page-protection.h they are defined as PROT_* |
| 470 | * values, matching mprotect(). |
| 471 | * Temporarily enable write access to zero the fractional bss. |
| 472 | * target_mprotect() handles TB invalidation if needed. |
| 473 | */ |
| 474 | if (!(flags & PAGE_WRITE)) { |
| 475 | if (target_mprotect(start_page_aligned, |
| 476 | TARGET_PAGE_SIZE, |
| 477 | prot | PAGE_WRITE) == -1) { |
| 478 | error_setg_errno(errp, errno, |
| 479 | "Error enabling write access for bss"); |
| 480 | return false; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | /* The page is already mapped and now guaranteed writable. */ |
| 485 | memset(g2h_untagged(start_bss), 0, align_bss - start_bss); |
| 486 | |
| 487 | if (!(flags & PAGE_WRITE)) { |
| 488 | if (target_mprotect(start_page_aligned, |
| 489 | TARGET_PAGE_SIZE, prot) == -1) { |
| 490 | error_setg_errno(errp, errno, |
| 491 | "Error restoring bss first permissions"); |
| 492 | return false; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | if (align_bss < end_bss && |
| 499 | target_mmap(align_bss, end_bss - align_bss, prot, |
| 500 | MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) == -1) { |
| 501 | error_setg_errno(errp, errno, "Error mapping bss"); |
| 502 | return false; |
| 503 | } |
| 504 | return true; |
| 505 | } |
| 506 | |
| 507 | #if defined(TARGET_ARM) |
| 508 | static int elf_is_fdpic(struct elfhdr *exec) |
| 509 | { |
| 510 | return exec->e_ident[EI_OSABI] == ELFOSABI_ARM_FDPIC; |
| 511 | } |
| 512 | #elif defined(TARGET_XTENSA) |
| 513 | static int elf_is_fdpic(struct elfhdr *exec) |
| 514 | { |
| 515 | return exec->e_ident[EI_OSABI] == ELFOSABI_XTENSA_FDPIC; |
| 516 | } |
| 517 | #else |
| 518 | /* Default implementation, always false. */ |
| 519 | static int elf_is_fdpic(struct elfhdr *exec) |
| 520 | { |
| 521 | return 0; |
| 522 | } |
| 523 | #endif |
| 524 | |
| 525 | static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp) |
| 526 | { |
| 527 | uint16_t n; |
| 528 | struct elf32_fdpic_loadseg *loadsegs = info->loadsegs; |
| 529 | |
| 530 | /* elf32_fdpic_loadseg */ |
| 531 | n = info->nsegs; |
| 532 | while (n--) { |
| 533 | sp -= 12; |
| 534 | put_user_u32(loadsegs[n].addr, sp+0); |
| 535 | put_user_u32(loadsegs[n].p_vaddr, sp+4); |
| 536 | put_user_u32(loadsegs[n].p_memsz, sp+8); |
| 537 | } |
| 538 | |
| 539 | /* elf32_fdpic_loadmap */ |
| 540 | sp -= 4; |
| 541 | put_user_u16(0, sp+0); /* version */ |
| 542 | put_user_u16(info->nsegs, sp+2); /* nsegs */ |
| 543 | |
| 544 | info->personality = PER_LINUX_FDPIC; |
| 545 | info->loadmap_addr = sp; |
| 546 | |
| 547 | return sp; |
| 548 | } |
| 549 | |
| 550 | static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc, |
| 551 | struct elfhdr *exec, |
| 552 | struct image_info *info, |
| 553 | struct image_info *interp_info, |
| 554 | struct image_info *vdso_info) |
| 555 | { |
| 556 | abi_ulong sp; |
| 557 | abi_ulong u_argc, u_argv, u_envp, u_auxv; |
| 558 | int size; |
| 559 | int i; |
| 560 | abi_ulong u_rand_bytes; |
| 561 | uint8_t k_rand_bytes[16]; |
| 562 | abi_ulong u_platform, u_base_platform; |
| 563 | const char *k_platform, *k_base_platform; |
| 564 | const int n = sizeof(elf_addr_t); |
| 565 | |
| 566 | sp = p; |
| 567 | |
| 568 | /* Needs to be before we load the env/argc/... */ |
| 569 | if (elf_is_fdpic(exec)) { |
| 570 | /* Need 4 byte alignment for these structs */ |
| 571 | sp &= ~3; |
| 572 | sp = loader_build_fdpic_loadmap(info, sp); |
| 573 | info->other_info = interp_info; |
| 574 | if (interp_info) { |
| 575 | interp_info->other_info = info; |
| 576 | sp = loader_build_fdpic_loadmap(interp_info, sp); |
| 577 | info->interpreter_loadmap_addr = interp_info->loadmap_addr; |
| 578 | info->interpreter_pt_dynamic_addr = interp_info->pt_dynamic_addr; |
| 579 | } else { |
| 580 | info->interpreter_loadmap_addr = 0; |
| 581 | info->interpreter_pt_dynamic_addr = 0; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | u_base_platform = 0; |
| 586 | k_base_platform = get_elf_base_platform(thread_cpu); |
| 587 | if (k_base_platform) { |
| 588 | size_t len = strlen(k_base_platform) + 1; |
| 589 | if (STACK_GROWS_DOWN) { |
| 590 | sp -= (len + n - 1) & ~(n - 1); |
| 591 | u_base_platform = sp; |
| 592 | /* FIXME - check return value of memcpy_to_target() for failure */ |
| 593 | memcpy_to_target(sp, k_base_platform, len); |
| 594 | } else { |
| 595 | memcpy_to_target(sp, k_base_platform, len); |
| 596 | u_base_platform = sp; |
| 597 | sp += len + 1; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | u_platform = 0; |
| 602 | k_platform = get_elf_platform(thread_cpu); |
| 603 | if (k_platform) { |
| 604 | size_t len = strlen(k_platform) + 1; |
| 605 | if (STACK_GROWS_DOWN) { |
| 606 | sp -= (len + n - 1) & ~(n - 1); |
| 607 | u_platform = sp; |
| 608 | /* FIXME - check return value of memcpy_to_target() for failure */ |
| 609 | memcpy_to_target(sp, k_platform, len); |
| 610 | } else { |
| 611 | memcpy_to_target(sp, k_platform, len); |
| 612 | u_platform = sp; |
| 613 | sp += len + 1; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | /* Provide 16 byte alignment for the PRNG, and basic alignment for |
| 618 | * the argv and envp pointers. |
| 619 | */ |
| 620 | if (STACK_GROWS_DOWN) { |
| 621 | sp = QEMU_ALIGN_DOWN(sp, 16); |
| 622 | } else { |
| 623 | sp = QEMU_ALIGN_UP(sp, 16); |
| 624 | } |
| 625 | |
| 626 | /* |
| 627 | * Generate 16 random bytes for userspace PRNG seeding. |
| 628 | */ |
| 629 | qemu_guest_getrandom_nofail(k_rand_bytes, sizeof(k_rand_bytes)); |
| 630 | if (STACK_GROWS_DOWN) { |
| 631 | sp -= 16; |
| 632 | u_rand_bytes = sp; |
| 633 | /* FIXME - check return value of memcpy_to_target() for failure */ |
| 634 | memcpy_to_target(sp, k_rand_bytes, 16); |
| 635 | } else { |
| 636 | memcpy_to_target(sp, k_rand_bytes, 16); |
| 637 | u_rand_bytes = sp; |
| 638 | sp += 16; |
| 639 | } |
| 640 | |
| 641 | size = (DLINFO_ITEMS + 1) * 2; |
| 642 | if (k_base_platform) { |
| 643 | size += 2; |
| 644 | } |
| 645 | if (k_platform) { |
| 646 | size += 2; |
| 647 | } |
| 648 | if (vdso_info) { |
| 649 | size += 2; |
| 650 | } |
| 651 | #ifdef DLINFO_ARCH_ITEMS |
| 652 | size += DLINFO_ARCH_ITEMS * 2; |
| 653 | #endif |
| 654 | if (HAVE_ELF_HWCAP2) { |
| 655 | size += 2; |
| 656 | } |
| 657 | info->auxv_len = size * n; |
| 658 | |
| 659 | size += envc + argc + 2; |
| 660 | size += 1; /* argc itself */ |
| 661 | size *= n; |
| 662 | |
| 663 | /* Allocate space and finalize stack alignment for entry now. */ |
| 664 | if (STACK_GROWS_DOWN) { |
| 665 | u_argc = QEMU_ALIGN_DOWN(sp - size, STACK_ALIGNMENT); |
| 666 | sp = u_argc; |
| 667 | } else { |
| 668 | u_argc = sp; |
| 669 | sp = QEMU_ALIGN_UP(sp + size, STACK_ALIGNMENT); |
| 670 | } |
| 671 | |
| 672 | u_argv = u_argc + n; |
| 673 | u_envp = u_argv + (argc + 1) * n; |
| 674 | u_auxv = u_envp + (envc + 1) * n; |
| 675 | info->saved_auxv = u_auxv; |
| 676 | info->argc = argc; |
| 677 | info->envc = envc; |
| 678 | info->argv = u_argv; |
| 679 | info->envp = u_envp; |
| 680 | |
| 681 | /* This is correct because Linux defines |
| 682 | * elf_addr_t as Elf32_Off / Elf64_Off |
| 683 | */ |
| 684 | #define NEW_AUX_ENT(id, val) do { \ |
| 685 | put_user_ual(id, u_auxv); u_auxv += n; \ |
| 686 | put_user_ual(val, u_auxv); u_auxv += n; \ |
| 687 | } while(0) |
| 688 | |
| 689 | #ifdef ARCH_DLINFO |
| 690 | /* |
| 691 | * ARCH_DLINFO must come first so platform specific code can enforce |
| 692 | * special alignment requirements on the AUXV if necessary (eg. PPC). |
| 693 | */ |
| 694 | ARCH_DLINFO; |
| 695 | #endif |
| 696 | /* There must be exactly DLINFO_ITEMS entries here, or the assert |
| 697 | * on info->auxv_len will trigger. |
| 698 | */ |
| 699 | NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->phdr_addr)); |
| 700 | NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr))); |
| 701 | NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum)); |
| 702 | NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE)); |
| 703 | NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0)); |
| 704 | NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0); |
| 705 | NEW_AUX_ENT(AT_ENTRY, info->entry); |
| 706 | NEW_AUX_ENT(AT_UID, (abi_ulong) getuid()); |
| 707 | NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid()); |
| 708 | NEW_AUX_ENT(AT_GID, (abi_ulong) getgid()); |
| 709 | NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid()); |
| 710 | NEW_AUX_ENT(AT_HWCAP, get_elf_hwcap(thread_cpu)); |
| 711 | NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK)); |
| 712 | NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes); |
| 713 | NEW_AUX_ENT(AT_SECURE, (abi_ulong) qemu_getauxval(AT_SECURE)); |
| 714 | NEW_AUX_ENT(AT_EXECFN, info->file_string); |
| 715 | |
| 716 | if (HAVE_ELF_HWCAP2) { |
| 717 | NEW_AUX_ENT(AT_HWCAP2, get_elf_hwcap2(thread_cpu)); |
| 718 | } |
| 719 | if (u_base_platform) { |
| 720 | NEW_AUX_ENT(AT_BASE_PLATFORM, u_base_platform); |
| 721 | } |
| 722 | if (u_platform) { |
| 723 | NEW_AUX_ENT(AT_PLATFORM, u_platform); |
| 724 | } |
| 725 | if (vdso_info) { |
| 726 | NEW_AUX_ENT(AT_SYSINFO_EHDR, vdso_info->load_addr); |
| 727 | } |
| 728 | NEW_AUX_ENT (AT_NULL, 0); |
| 729 | #undef NEW_AUX_ENT |
| 730 | |
| 731 | /* Check that our initial calculation of the auxv length matches how much |
| 732 | * we actually put into it. |
| 733 | */ |
| 734 | assert(info->auxv_len == u_auxv - info->saved_auxv); |
| 735 | |
| 736 | put_user_ual(argc, u_argc); |
| 737 | |
| 738 | p = info->arg_strings; |
| 739 | for (i = 0; i < argc; ++i) { |
| 740 | put_user_ual(p, u_argv); |
| 741 | u_argv += n; |
| 742 | p += target_strlen(p) + 1; |
| 743 | } |
| 744 | put_user_ual(0, u_argv); |
| 745 | |
| 746 | p = info->env_strings; |
| 747 | for (i = 0; i < envc; ++i) { |
| 748 | put_user_ual(p, u_envp); |
| 749 | u_envp += n; |
| 750 | p += target_strlen(p) + 1; |
| 751 | } |
| 752 | put_user_ual(0, u_envp); |
| 753 | |
| 754 | return sp; |
| 755 | } |
| 756 | |
| 757 | void linux_probe_guest_base(const char *image_name, |
| 758 | const PGBRange *image_range) |
| 759 | { |
| 760 | #ifdef COMMPAGE |
| 761 | const PGBRange * const commpage_range = &(PGBRange){ |
| 762 | COMMPAGE, COMMPAGE + TARGET_PAGE_SIZE - 1 |
| 763 | }; |
| 764 | #else |
| 765 | const PGBRange * const commpage_range = NULL; |
| 766 | #endif |
| 767 | |
| 768 | probe_guest_base(image_name, image_range, commpage_range); |
| 769 | |
| 770 | /* Reserve and initialize the commpage. */ |
| 771 | #if defined(COMMPAGE) || defined(HAVE_GUEST_COMMPAGE) |
| 772 | if (!init_guest_commpage()) { |
| 773 | /* We have already probed for the commpage being free. */ |
| 774 | g_assert_not_reached(); |
| 775 | } |
| 776 | #endif |
| 777 | } |
| 778 | |
| 779 | enum { |
| 780 | /* The string "GNU\0" as a magic number. */ |
| 781 | GNU0_MAGIC = const_le32('G' | 'N' << 8 | 'U' << 16), |
| 782 | NOTE_DATA_SZ = 1 * KiB, |
| 783 | NOTE_NAME_SZ = 4, |
| 784 | ELF_GNU_PROPERTY_ALIGN = ELF_CLASS == ELFCLASS32 ? 4 : 8, |
| 785 | }; |
| 786 | |
| 787 | /* |
| 788 | * Process a single gnu_property entry. |
| 789 | * Return false for error. |
| 790 | */ |
| 791 | static bool parse_elf_property(const uint32_t *data, int *off, int datasz, |
| 792 | struct image_info *info, bool have_prev_type, |
| 793 | uint32_t *prev_type, Error **errp) |
| 794 | { |
| 795 | uint32_t pr_type, pr_datasz, step; |
| 796 | |
| 797 | if (*off > datasz || !QEMU_IS_ALIGNED(*off, ELF_GNU_PROPERTY_ALIGN)) { |
| 798 | goto error_data; |
| 799 | } |
| 800 | datasz -= *off; |
| 801 | data += *off / sizeof(uint32_t); |
| 802 | |
| 803 | if (datasz < 2 * sizeof(uint32_t)) { |
| 804 | goto error_data; |
| 805 | } |
| 806 | pr_type = data[0]; |
| 807 | pr_datasz = data[1]; |
| 808 | data += 2; |
| 809 | datasz -= 2 * sizeof(uint32_t); |
| 810 | step = ROUND_UP(pr_datasz, ELF_GNU_PROPERTY_ALIGN); |
| 811 | if (step > datasz) { |
| 812 | goto error_data; |
| 813 | } |
| 814 | |
| 815 | /* Properties are supposed to be unique and sorted on pr_type. */ |
| 816 | if (have_prev_type && pr_type <= *prev_type) { |
| 817 | if (pr_type == *prev_type) { |
| 818 | error_setg(errp, "Duplicate property in PT_GNU_PROPERTY"); |
| 819 | } else { |
| 820 | error_setg(errp, "Unsorted property in PT_GNU_PROPERTY"); |
| 821 | } |
| 822 | return false; |
| 823 | } |
| 824 | *prev_type = pr_type; |
| 825 | |
| 826 | if (!arch_parse_elf_property(pr_type, pr_datasz, data, info, errp)) { |
| 827 | return false; |
| 828 | } |
| 829 | |
| 830 | *off += 2 * sizeof(uint32_t) + step; |
| 831 | return true; |
| 832 | |
| 833 | error_data: |
| 834 | error_setg(errp, "Ill-formed property in PT_GNU_PROPERTY"); |
| 835 | return false; |
| 836 | } |
| 837 | |
| 838 | /* Process NT_GNU_PROPERTY_TYPE_0. */ |
| 839 | static bool parse_elf_properties(const ImageSource *src, |
| 840 | struct image_info *info, |
| 841 | const struct elf_phdr *phdr, |
| 842 | Error **errp) |
| 843 | { |
| 844 | union { |
| 845 | struct elf_note nhdr; |
| 846 | uint32_t data[NOTE_DATA_SZ / sizeof(uint32_t)]; |
| 847 | } note; |
| 848 | |
| 849 | int n, off, datasz; |
| 850 | bool have_prev_type; |
| 851 | uint32_t prev_type; |
| 852 | |
| 853 | /* Unless the arch requires properties, ignore them. */ |
| 854 | if (!HAVE_ELF_GNU_PROPERTY) { |
| 855 | return true; |
| 856 | } |
| 857 | |
| 858 | /* If the properties are crazy large, that's too bad. */ |
| 859 | n = phdr->p_filesz; |
| 860 | if (n > sizeof(note)) { |
| 861 | error_setg(errp, "PT_GNU_PROPERTY too large"); |
| 862 | return false; |
| 863 | } |
| 864 | if (n < sizeof(note.nhdr)) { |
| 865 | error_setg(errp, "PT_GNU_PROPERTY too small"); |
| 866 | return false; |
| 867 | } |
| 868 | |
| 869 | if (!imgsrc_read(¬e, phdr->p_offset, n, src, errp)) { |
| 870 | return false; |
| 871 | } |
| 872 | |
| 873 | /* |
| 874 | * The contents of a valid PT_GNU_PROPERTY is a sequence of uint32_t. |
| 875 | * Swap most of them now, beyond the header and namesz. |
| 876 | */ |
| 877 | if (target_needs_bswap()) { |
| 878 | for (int i = 4; i < n / 4; i++) { |
| 879 | bswap32s(note.data + i); |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | /* |
| 884 | * Note that nhdr is 3 words, and that the "name" described by namesz |
| 885 | * immediately follows nhdr and is thus at the 4th word. Further, all |
| 886 | * of the inputs to the kernel's round_up are multiples of 4. |
| 887 | */ |
| 888 | if (tswap32(note.nhdr.n_type) != NT_GNU_PROPERTY_TYPE_0 || |
| 889 | tswap32(note.nhdr.n_namesz) != NOTE_NAME_SZ || |
| 890 | note.data[3] != GNU0_MAGIC) { |
| 891 | error_setg(errp, "Invalid note in PT_GNU_PROPERTY"); |
| 892 | return false; |
| 893 | } |
| 894 | off = sizeof(note.nhdr) + NOTE_NAME_SZ; |
| 895 | |
| 896 | datasz = tswap32(note.nhdr.n_descsz) + off; |
| 897 | if (datasz > n) { |
| 898 | error_setg(errp, "Invalid note size in PT_GNU_PROPERTY"); |
| 899 | return false; |
| 900 | } |
| 901 | |
| 902 | have_prev_type = false; |
| 903 | prev_type = 0; |
| 904 | while (1) { |
| 905 | if (off == datasz) { |
| 906 | return true; /* end, exit ok */ |
| 907 | } |
| 908 | if (!parse_elf_property(note.data, &off, datasz, info, |
| 909 | have_prev_type, &prev_type, errp)) { |
| 910 | return false; |
| 911 | } |
| 912 | have_prev_type = true; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | /** |
| 917 | * load_elf_image: Load an ELF image into the address space. |
| 918 | * @image_name: the filename of the image, to use in error messages. |
| 919 | * @src: the ImageSource from which to read. |
| 920 | * @info: info collected from the loaded image. |
| 921 | * @ehdr: the ELF header, not yet bswapped. |
| 922 | * @pinterp_name: record any PT_INTERP string found. |
| 923 | * |
| 924 | * On return: @info values will be filled in, as necessary or available. |
| 925 | */ |
| 926 | |
| 927 | static void load_elf_image(const char *image_name, const ImageSource *src, |
| 928 | struct image_info *info, struct elfhdr *ehdr, |
| 929 | char **pinterp_name) |
| 930 | { |
| 931 | g_autofree struct elf_phdr *phdr = NULL; |
| 932 | PGBRange range = { -1, 0 }; |
| 933 | abi_ulong load_addr, load_bias, error, align; |
| 934 | size_t reserve_size, align_size; |
| 935 | int i, prot_exec; |
| 936 | Error *err = NULL; |
| 937 | |
| 938 | /* |
| 939 | * First of all, some simple consistency checks. |
| 940 | * Note that we rely on the bswapped ehdr staying in bprm_buf, |
| 941 | * for later use by load_elf_binary and create_elf_tables. |
| 942 | */ |
| 943 | if (!imgsrc_read(ehdr, 0, sizeof(*ehdr), src, &err)) { |
| 944 | goto exit_errmsg; |
| 945 | } |
| 946 | if (!elf_check_ident(ehdr)) { |
| 947 | error_setg(&err, "Invalid ELF image for this architecture"); |
| 948 | goto exit_errmsg; |
| 949 | } |
| 950 | bswap_ehdr(ehdr); |
| 951 | if (!elf_check_ehdr(ehdr)) { |
| 952 | error_setg(&err, "Invalid ELF image for this architecture"); |
| 953 | goto exit_errmsg; |
| 954 | } |
| 955 | |
| 956 | phdr = imgsrc_read_alloc(ehdr->e_phoff, |
| 957 | ehdr->e_phnum * sizeof(struct elf_phdr), |
| 958 | src, &err); |
| 959 | if (phdr == NULL) { |
| 960 | goto exit_errmsg; |
| 961 | } |
| 962 | bswap_phdr(phdr, ehdr->e_phnum); |
| 963 | |
| 964 | info->nsegs = 0; |
| 965 | info->pt_dynamic_addr = 0; |
| 966 | |
| 967 | mmap_lock(); |
| 968 | |
| 969 | /* |
| 970 | * Find the maximum size of the image and allocate an appropriate |
| 971 | * amount of memory to handle that. Locate the interpreter, if any. |
| 972 | */ |
| 973 | align = 0; |
| 974 | info->exec_stack = EXSTACK_DEFAULT; |
| 975 | for (i = 0; i < ehdr->e_phnum; ++i) { |
| 976 | struct elf_phdr *eppnt = phdr + i; |
| 977 | if (eppnt->p_type == PT_LOAD) { |
| 978 | abi_ulong a = eppnt->p_vaddr & TARGET_PAGE_MASK; |
| 979 | if (a < range.lo) { |
| 980 | range.lo = a; |
| 981 | } |
| 982 | a = eppnt->p_vaddr + eppnt->p_memsz - 1; |
| 983 | if (a > range.hi) { |
| 984 | range.hi = a; |
| 985 | } |
| 986 | ++info->nsegs; |
| 987 | align |= eppnt->p_align; |
| 988 | } else if (eppnt->p_type == PT_INTERP && pinterp_name) { |
| 989 | g_autofree char *interp_name = NULL; |
| 990 | |
| 991 | if (*pinterp_name) { |
| 992 | error_setg(&err, "Multiple PT_INTERP entries"); |
| 993 | goto exit_errmsg; |
| 994 | } |
| 995 | |
| 996 | interp_name = imgsrc_read_alloc(eppnt->p_offset, eppnt->p_filesz, |
| 997 | src, &err); |
| 998 | if (interp_name == NULL) { |
| 999 | goto exit_errmsg; |
| 1000 | } |
| 1001 | if (interp_name[eppnt->p_filesz - 1] != 0) { |
| 1002 | error_setg(&err, "Invalid PT_INTERP entry"); |
| 1003 | goto exit_errmsg; |
| 1004 | } |
| 1005 | *pinterp_name = g_steal_pointer(&interp_name); |
| 1006 | } else if (eppnt->p_type == PT_GNU_PROPERTY) { |
| 1007 | if (!parse_elf_properties(src, info, eppnt, &err)) { |
| 1008 | goto exit_errmsg; |
| 1009 | } |
| 1010 | } else if (eppnt->p_type == PT_GNU_STACK) { |
| 1011 | info->exec_stack = eppnt->p_flags & PF_X; |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | load_addr = range.lo; |
| 1016 | |
| 1017 | align = pow2ceil(align); |
| 1018 | |
| 1019 | if (pinterp_name != NULL) { |
| 1020 | if (ehdr->e_type == ET_EXEC) { |
| 1021 | /* |
| 1022 | * Make sure that the low address does not conflict with |
| 1023 | * MMAP_MIN_ADDR or the QEMU application itself. |
| 1024 | */ |
| 1025 | linux_probe_guest_base(image_name, &range); |
| 1026 | } else { |
| 1027 | /* The binary is dynamic; we still need to select guest_base. */ |
| 1028 | linux_probe_guest_base(image_name, NULL); |
| 1029 | |
| 1030 | /* |
| 1031 | * Avoid collision with the loader by providing a different |
| 1032 | * default load address. |
| 1033 | */ |
| 1034 | load_addr += elf_et_dyn_base; |
| 1035 | |
| 1036 | /* |
| 1037 | * TODO: Better support for mmap alignment is desirable. |
| 1038 | * Since we do not have complete control over the guest |
| 1039 | * address space, we prefer the kernel to choose some address |
| 1040 | * rather than force the use of LOAD_ADDR via MAP_FIXED. |
| 1041 | */ |
| 1042 | if (align) { |
| 1043 | load_addr &= -align; |
| 1044 | } |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | /* |
| 1049 | * Reserve address space for all of this. |
| 1050 | * |
| 1051 | * In the case of ET_EXEC, we supply MAP_FIXED_NOREPLACE so that we get |
| 1052 | * exactly the address range that is required. Without reserved_va, |
| 1053 | * the guest address space is not isolated. We have attempted to avoid |
| 1054 | * conflict with the host program itself via probe_guest_base, but using |
| 1055 | * MAP_FIXED_NOREPLACE instead of MAP_FIXED provides an extra check. |
| 1056 | * |
| 1057 | * Otherwise this is ET_DYN, and we are searching for a location |
| 1058 | * that can hold the memory space required. If the image is |
| 1059 | * pre-linked, LOAD_ADDR will be non-zero, and the kernel should |
| 1060 | * honor that address if it happens to be free. |
| 1061 | * |
| 1062 | * In both cases, we will overwrite pages in this range with mappings |
| 1063 | * from the executable. |
| 1064 | */ |
| 1065 | reserve_size = range.hi - range.lo + 1; |
| 1066 | align_size = reserve_size; |
| 1067 | |
| 1068 | if (ehdr->e_type != ET_EXEC && align > qemu_real_host_page_size()) { |
| 1069 | align_size += align - 1; |
| 1070 | } |
| 1071 | |
| 1072 | load_addr = target_mmap(load_addr, align_size, PROT_NONE, |
| 1073 | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | |
| 1074 | (ehdr->e_type == ET_EXEC ? MAP_FIXED_NOREPLACE : 0), |
| 1075 | -1, 0); |
| 1076 | if (load_addr == -1) { |
| 1077 | goto exit_mmap; |
| 1078 | } |
| 1079 | |
| 1080 | if (align_size != reserve_size) { |
| 1081 | abi_ulong align_addr = ROUND_UP(load_addr, align); |
| 1082 | abi_ulong align_end = TARGET_PAGE_ALIGN(align_addr + reserve_size); |
| 1083 | abi_ulong load_end = TARGET_PAGE_ALIGN(load_addr + align_size); |
| 1084 | |
| 1085 | if (align_addr != load_addr) { |
| 1086 | target_munmap(load_addr, align_addr - load_addr); |
| 1087 | } |
| 1088 | if (align_end != load_end) { |
| 1089 | target_munmap(align_end, load_end - align_end); |
| 1090 | } |
| 1091 | load_addr = align_addr; |
| 1092 | } |
| 1093 | |
| 1094 | load_bias = load_addr - range.lo; |
| 1095 | |
| 1096 | if (elf_is_fdpic(ehdr)) { |
| 1097 | struct elf32_fdpic_loadseg *loadsegs = info->loadsegs = |
| 1098 | g_malloc(sizeof(*loadsegs) * info->nsegs); |
| 1099 | |
| 1100 | for (i = 0; i < ehdr->e_phnum; ++i) { |
| 1101 | switch (phdr[i].p_type) { |
| 1102 | case PT_DYNAMIC: |
| 1103 | info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias; |
| 1104 | break; |
| 1105 | case PT_LOAD: |
| 1106 | loadsegs->addr = phdr[i].p_vaddr + load_bias; |
| 1107 | loadsegs->p_vaddr = phdr[i].p_vaddr; |
| 1108 | loadsegs->p_memsz = phdr[i].p_memsz; |
| 1109 | ++loadsegs; |
| 1110 | break; |
| 1111 | } |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | info->load_bias = load_bias; |
| 1116 | info->code_offset = load_bias; |
| 1117 | info->data_offset = load_bias; |
| 1118 | info->load_addr = load_addr; |
| 1119 | info->entry = ehdr->e_entry + load_bias; |
| 1120 | /* |
| 1121 | * Fallback for AT_PHDR if the program headers do not fall within |
| 1122 | * any PT_LOAD segment (see the loop below, which overrides this with |
| 1123 | * the correct in-memory address when a containing segment is found). |
| 1124 | */ |
| 1125 | info->phdr_addr = load_addr + ehdr->e_phoff; |
| 1126 | info->start_code = -1; |
| 1127 | info->end_code = 0; |
| 1128 | info->start_data = -1; |
| 1129 | info->end_data = 0; |
| 1130 | /* Usual start for brk is after all sections of the main executable. */ |
| 1131 | info->brk = TARGET_PAGE_ALIGN(range.hi + load_bias); |
| 1132 | info->elf_flags = ehdr->e_flags; |
| 1133 | #ifdef TARGET_MIPS |
| 1134 | info->use_k0_tls = (ehdr->e_flags & EF_MIPS_MACH) == EF_MIPS_MACH_OCTEON; |
| 1135 | #endif |
| 1136 | |
| 1137 | prot_exec = PROT_EXEC; |
| 1138 | #ifdef TARGET_AARCH64 |
| 1139 | /* |
| 1140 | * If the BTI feature is present, this indicates that the executable |
| 1141 | * pages of the startup binary should be mapped with PROT_BTI, so that |
| 1142 | * branch targets are enforced. |
| 1143 | * |
| 1144 | * The startup binary is either the interpreter or the static executable. |
| 1145 | * The interpreter is responsible for all pages of a dynamic executable. |
| 1146 | * |
| 1147 | * Elf notes are backward compatible to older cpus. |
| 1148 | * Do not enable BTI unless it is supported. |
| 1149 | */ |
| 1150 | if ((info->note_flags & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) |
| 1151 | && (pinterp_name == NULL || *pinterp_name == 0) |
| 1152 | && cpu_isar_feature(aa64_bti, ARM_CPU(thread_cpu))) { |
| 1153 | prot_exec |= TARGET_PROT_BTI; |
| 1154 | } |
| 1155 | #endif |
| 1156 | |
| 1157 | for (i = 0; i < ehdr->e_phnum; i++) { |
| 1158 | struct elf_phdr *eppnt = phdr + i; |
| 1159 | if (eppnt->p_type == PT_LOAD) { |
| 1160 | abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em; |
| 1161 | int elf_prot = 0; |
| 1162 | |
| 1163 | if (eppnt->p_flags & PF_R) { |
| 1164 | elf_prot |= PROT_READ; |
| 1165 | } |
| 1166 | if (eppnt->p_flags & PF_W) { |
| 1167 | elf_prot |= PROT_WRITE; |
| 1168 | } |
| 1169 | if (eppnt->p_flags & PF_X) { |
| 1170 | elf_prot |= prot_exec; |
| 1171 | } |
| 1172 | |
| 1173 | vaddr = load_bias + eppnt->p_vaddr; |
| 1174 | vaddr_po = vaddr & ~TARGET_PAGE_MASK; |
| 1175 | vaddr_ps = vaddr & TARGET_PAGE_MASK; |
| 1176 | |
| 1177 | vaddr_ef = vaddr + eppnt->p_filesz; |
| 1178 | vaddr_em = vaddr + eppnt->p_memsz; |
| 1179 | |
| 1180 | /* |
| 1181 | * If this segment contains the program headers, record their |
| 1182 | * in-memory address for AT_PHDR. This matches the kernel, which |
| 1183 | * locates the headers via the containing PT_LOAD rather than |
| 1184 | * assuming load_addr + e_phoff (false when the phdrs are not |
| 1185 | * mapped 1:1 from file offset 0, e.g. relocated into their own |
| 1186 | * segment by a binary patcher). |
| 1187 | */ |
| 1188 | if (eppnt->p_offset <= ehdr->e_phoff && |
| 1189 | ehdr->e_phoff < eppnt->p_offset + eppnt->p_filesz) { |
| 1190 | info->phdr_addr = vaddr + (ehdr->e_phoff - eppnt->p_offset); |
| 1191 | } |
| 1192 | |
| 1193 | /* |
| 1194 | * Some segments may be completely empty, with a non-zero p_memsz |
| 1195 | * but no backing file segment. |
| 1196 | */ |
| 1197 | if (eppnt->p_filesz != 0) { |
| 1198 | error = imgsrc_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po, |
| 1199 | elf_prot, MAP_PRIVATE | MAP_FIXED, |
| 1200 | src, eppnt->p_offset - vaddr_po); |
| 1201 | if (error == -1) { |
| 1202 | goto exit_mmap; |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | /* If the load segment requests extra zeros (e.g. bss), map it. */ |
| 1207 | if (vaddr_ef < vaddr_em && |
| 1208 | !zero_bss(vaddr_ef, vaddr_em, elf_prot, &err)) { |
| 1209 | goto exit_errmsg; |
| 1210 | } |
| 1211 | |
| 1212 | /* Find the full program boundaries. */ |
| 1213 | if (elf_prot & PROT_EXEC) { |
| 1214 | if (vaddr < info->start_code) { |
| 1215 | info->start_code = vaddr; |
| 1216 | } |
| 1217 | if (vaddr_ef > info->end_code) { |
| 1218 | info->end_code = vaddr_ef; |
| 1219 | } |
| 1220 | } |
| 1221 | if (elf_prot & PROT_WRITE) { |
| 1222 | if (vaddr < info->start_data) { |
| 1223 | info->start_data = vaddr; |
| 1224 | } |
| 1225 | if (vaddr_ef > info->end_data) { |
| 1226 | info->end_data = vaddr_ef; |
| 1227 | } |
| 1228 | } |
| 1229 | #ifdef TARGET_MIPS |
| 1230 | } else if (eppnt->p_type == PT_MIPS_ABIFLAGS) { |
| 1231 | Mips_elf_abiflags_v0 abiflags; |
| 1232 | |
| 1233 | if (!imgsrc_read(&abiflags, eppnt->p_offset, sizeof(abiflags), |
| 1234 | src, &err)) { |
| 1235 | goto exit_errmsg; |
| 1236 | } |
| 1237 | bswap_mips_abiflags(&abiflags); |
| 1238 | info->fp_abi = abiflags.fp_abi; |
| 1239 | #endif |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | if (info->end_data == 0) { |
| 1244 | info->start_data = info->end_code; |
| 1245 | info->end_data = info->end_code; |
| 1246 | } |
| 1247 | |
| 1248 | if (qemu_log_enabled()) { |
| 1249 | load_symbols(ehdr, src, load_bias); |
| 1250 | } |
| 1251 | |
| 1252 | debuginfo_report_elf(image_name, src->fd, load_bias); |
| 1253 | |
| 1254 | mmap_unlock(); |
| 1255 | |
| 1256 | close(src->fd); |
| 1257 | return; |
| 1258 | |
| 1259 | exit_mmap: |
| 1260 | error_setg_errno(&err, errno, "Error mapping file"); |
| 1261 | goto exit_errmsg; |
| 1262 | exit_errmsg: |
| 1263 | error_reportf_err(err, "%s: ", image_name); |
| 1264 | exit(-1); |
| 1265 | } |
| 1266 | |
| 1267 | static void load_elf_interp(const char *filename, struct image_info *info, |
| 1268 | char bprm_buf[BPRM_BUF_SIZE]) |
| 1269 | { |
| 1270 | struct elfhdr ehdr; |
| 1271 | ImageSource src; |
| 1272 | int fd, retval; |
| 1273 | Error *err = NULL; |
| 1274 | |
| 1275 | fd = open(path(filename), O_RDONLY); |
| 1276 | if (fd < 0) { |
| 1277 | error_setg_file_open(&err, errno, filename); |
| 1278 | error_report_err(err); |
| 1279 | exit(-1); |
| 1280 | } |
| 1281 | |
| 1282 | retval = read(fd, bprm_buf, BPRM_BUF_SIZE); |
| 1283 | if (retval < 0) { |
| 1284 | error_setg_errno(&err, errno, "Error reading file header"); |
| 1285 | error_reportf_err(err, "%s: ", filename); |
| 1286 | exit(-1); |
| 1287 | } |
| 1288 | |
| 1289 | src.fd = fd; |
| 1290 | src.cache = bprm_buf; |
| 1291 | src.cache_size = retval; |
| 1292 | |
| 1293 | load_elf_image(filename, &src, info, &ehdr, NULL); |
| 1294 | } |
| 1295 | |
| 1296 | #ifndef HAVE_VDSO_IMAGE_INFO |
| 1297 | const VdsoImageInfo *get_vdso_image_info(uint32_t elf_flags) |
| 1298 | { |
| 1299 | #ifdef VDSO_HEADER |
| 1300 | #include VDSO_HEADER |
| 1301 | return &vdso_image_info; |
| 1302 | #else |
| 1303 | return NULL; |
| 1304 | #endif |
| 1305 | } |
| 1306 | #endif /* HAVE_VDSO_IMAGE_INFO */ |
| 1307 | |
| 1308 | static void load_elf_vdso(struct image_info *info, const VdsoImageInfo *vdso) |
| 1309 | { |
| 1310 | ImageSource src; |
| 1311 | struct elfhdr ehdr; |
| 1312 | abi_ulong load_bias, load_addr; |
| 1313 | |
| 1314 | src.fd = -1; |
| 1315 | src.cache = vdso->image; |
| 1316 | src.cache_size = vdso->image_size; |
| 1317 | |
| 1318 | load_elf_image("<internal-vdso>", &src, info, &ehdr, NULL); |
| 1319 | load_addr = info->load_addr; |
| 1320 | load_bias = info->load_bias; |
| 1321 | |
| 1322 | /* |
| 1323 | * We need to relocate the VDSO image. The one built into the kernel |
| 1324 | * is built for a fixed address. The one built for QEMU is not, since |
| 1325 | * that requires close control of the guest address space. |
| 1326 | * We pre-processed the image to locate all of the addresses that need |
| 1327 | * to be updated. |
| 1328 | */ |
| 1329 | for (unsigned i = 0, n = vdso->reloc_count; i < n; i++) { |
| 1330 | abi_ulong *addr = g2h_untagged(load_addr + vdso->relocs[i]); |
| 1331 | *addr = tswapal(tswapal(*addr) + load_bias); |
| 1332 | } |
| 1333 | |
| 1334 | /* Install signal trampolines, if present. */ |
| 1335 | if (vdso->sigreturn_ofs) { |
| 1336 | default_sigreturn = load_addr + vdso->sigreturn_ofs; |
| 1337 | } |
| 1338 | if (vdso->rt_sigreturn_ofs) { |
| 1339 | default_rt_sigreturn = load_addr + vdso->rt_sigreturn_ofs; |
| 1340 | } |
| 1341 | if (vdso->sigreturn_region_start_ofs) { |
| 1342 | vdso_sigreturn_region_start = |
| 1343 | load_addr + vdso->sigreturn_region_start_ofs; |
| 1344 | vdso_sigreturn_region_end = load_addr + vdso->sigreturn_region_end_ofs; |
| 1345 | } |
| 1346 | |
| 1347 | /* Remove write from VDSO segment. */ |
| 1348 | target_mprotect(info->start_data, info->end_data - info->start_data, |
| 1349 | PROT_READ | PROT_EXEC); |
| 1350 | } |
| 1351 | |
| 1352 | static int symfind(const void *s0, const void *s1) |
| 1353 | { |
| 1354 | struct elf_sym *sym = (struct elf_sym *)s1; |
| 1355 | __typeof(sym->st_value) addr = *(uint64_t *)s0; |
| 1356 | int result = 0; |
| 1357 | |
| 1358 | if (addr < sym->st_value) { |
| 1359 | result = -1; |
| 1360 | } else if (addr >= sym->st_value + sym->st_size) { |
| 1361 | result = 1; |
| 1362 | } |
| 1363 | return result; |
| 1364 | } |
| 1365 | |
| 1366 | static const char *lookup_symbolxx(struct syminfo *s, uint64_t orig_addr) |
| 1367 | { |
| 1368 | #if ELF_CLASS == ELFCLASS32 |
| 1369 | struct elf_sym *syms = s->disas_symtab.elf32; |
| 1370 | #else |
| 1371 | struct elf_sym *syms = s->disas_symtab.elf64; |
| 1372 | #endif |
| 1373 | |
| 1374 | // binary search |
| 1375 | struct elf_sym *sym; |
| 1376 | |
| 1377 | sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind); |
| 1378 | if (sym != NULL) { |
| 1379 | return s->disas_strtab + sym->st_name; |
| 1380 | } |
| 1381 | |
| 1382 | return ""; |
| 1383 | } |
| 1384 | |
| 1385 | /* FIXME: This should use elf_ops.h.inc */ |
| 1386 | static int symcmp(const void *s0, const void *s1) |
| 1387 | { |
| 1388 | struct elf_sym *sym0 = (struct elf_sym *)s0; |
| 1389 | struct elf_sym *sym1 = (struct elf_sym *)s1; |
| 1390 | return (sym0->st_value < sym1->st_value) |
| 1391 | ? -1 |
| 1392 | : ((sym0->st_value > sym1->st_value) ? 1 : 0); |
| 1393 | } |
| 1394 | |
| 1395 | /* Best attempt to load symbols from this ELF object. */ |
| 1396 | static void load_symbols(struct elfhdr *hdr, const ImageSource *src, |
| 1397 | abi_ulong load_bias) |
| 1398 | { |
| 1399 | int i, shnum, nsyms, sym_idx = 0, str_idx = 0; |
| 1400 | g_autofree struct elf_shdr *shdr = NULL; |
| 1401 | char *strings = NULL; |
| 1402 | struct elf_sym *syms = NULL; |
| 1403 | struct elf_sym *new_syms; |
| 1404 | uint64_t segsz; |
| 1405 | |
| 1406 | shnum = hdr->e_shnum; |
| 1407 | shdr = imgsrc_read_alloc(hdr->e_shoff, shnum * sizeof(struct elf_shdr), |
| 1408 | src, NULL); |
| 1409 | if (shdr == NULL) { |
| 1410 | return; |
| 1411 | } |
| 1412 | |
| 1413 | bswap_shdr(shdr, shnum); |
| 1414 | for (i = 0; i < shnum; ++i) { |
| 1415 | if (shdr[i].sh_type == SHT_SYMTAB) { |
| 1416 | sym_idx = i; |
| 1417 | str_idx = shdr[i].sh_link; |
| 1418 | goto found; |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | /* There will be no symbol table if the file was stripped. */ |
| 1423 | return; |
| 1424 | |
| 1425 | found: |
| 1426 | /* Now know where the strtab and symtab are. Snarf them. */ |
| 1427 | |
| 1428 | segsz = shdr[str_idx].sh_size; |
| 1429 | strings = g_try_malloc(segsz); |
| 1430 | if (!strings) { |
| 1431 | goto give_up; |
| 1432 | } |
| 1433 | if (!imgsrc_read(strings, shdr[str_idx].sh_offset, segsz, src, NULL)) { |
| 1434 | goto give_up; |
| 1435 | } |
| 1436 | |
| 1437 | segsz = shdr[sym_idx].sh_size; |
| 1438 | if (segsz / sizeof(struct elf_sym) > INT_MAX) { |
| 1439 | /* |
| 1440 | * Implausibly large symbol table: give up rather than ploughing |
| 1441 | * on with the number of symbols calculation overflowing. |
| 1442 | */ |
| 1443 | goto give_up; |
| 1444 | } |
| 1445 | nsyms = segsz / sizeof(struct elf_sym); |
| 1446 | syms = g_try_malloc(segsz); |
| 1447 | if (!syms) { |
| 1448 | goto give_up; |
| 1449 | } |
| 1450 | if (!imgsrc_read(syms, shdr[sym_idx].sh_offset, segsz, src, NULL)) { |
| 1451 | goto give_up; |
| 1452 | } |
| 1453 | |
| 1454 | for (i = 0; i < nsyms; ) { |
| 1455 | bswap_sym(syms + i); |
| 1456 | /* Throw away entries which we do not need. */ |
| 1457 | if (syms[i].st_shndx == SHN_UNDEF |
| 1458 | || syms[i].st_shndx >= SHN_LORESERVE |
| 1459 | || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) { |
| 1460 | if (i < --nsyms) { |
| 1461 | syms[i] = syms[nsyms]; |
| 1462 | } |
| 1463 | } else { |
| 1464 | #if defined(TARGET_ARM) || defined (TARGET_MIPS) |
| 1465 | /* The bottom address bit marks a Thumb or MIPS16 symbol. */ |
| 1466 | syms[i].st_value &= ~(target_ulong)1; |
| 1467 | #endif |
| 1468 | syms[i].st_value += load_bias; |
| 1469 | i++; |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | /* No "useful" symbol. */ |
| 1474 | if (nsyms == 0) { |
| 1475 | goto give_up; |
| 1476 | } |
| 1477 | |
| 1478 | /* |
| 1479 | * Attempt to free the storage associated with the local symbols |
| 1480 | * that we threw away. Whether or not this has any effect on the |
| 1481 | * memory allocation depends on the malloc implementation and how |
| 1482 | * many symbols we managed to discard. |
| 1483 | */ |
| 1484 | new_syms = g_try_renew(struct elf_sym, syms, nsyms); |
| 1485 | if (new_syms == NULL) { |
| 1486 | goto give_up; |
| 1487 | } |
| 1488 | syms = new_syms; |
| 1489 | |
| 1490 | qsort(syms, nsyms, sizeof(*syms), symcmp); |
| 1491 | |
| 1492 | { |
| 1493 | struct syminfo *s = g_new(struct syminfo, 1); |
| 1494 | |
| 1495 | s->disas_strtab = strings; |
| 1496 | s->disas_num_syms = nsyms; |
| 1497 | #if ELF_CLASS == ELFCLASS32 |
| 1498 | s->disas_symtab.elf32 = syms; |
| 1499 | #else |
| 1500 | s->disas_symtab.elf64 = syms; |
| 1501 | #endif |
| 1502 | s->lookup_symbol = lookup_symbolxx; |
| 1503 | s->next = syminfos; |
| 1504 | syminfos = s; |
| 1505 | } |
| 1506 | return; |
| 1507 | |
| 1508 | give_up: |
| 1509 | g_free(strings); |
| 1510 | g_free(syms); |
| 1511 | } |
| 1512 | |
| 1513 | uint32_t get_elf_eflags(int fd) |
| 1514 | { |
| 1515 | struct elfhdr ehdr; |
| 1516 | off_t offset; |
| 1517 | int ret; |
| 1518 | |
| 1519 | /* Read ELF header */ |
| 1520 | offset = lseek(fd, 0, SEEK_SET); |
| 1521 | if (offset == (off_t) -1) { |
| 1522 | return 0; |
| 1523 | } |
| 1524 | ret = read(fd, &ehdr, sizeof(ehdr)); |
| 1525 | if (ret < sizeof(ehdr)) { |
| 1526 | return 0; |
| 1527 | } |
| 1528 | offset = lseek(fd, offset, SEEK_SET); |
| 1529 | if (offset == (off_t) -1) { |
| 1530 | return 0; |
| 1531 | } |
| 1532 | |
| 1533 | /* Check ELF signature */ |
| 1534 | if (!elf_check_ident(&ehdr)) { |
| 1535 | return 0; |
| 1536 | } |
| 1537 | |
| 1538 | /* check header */ |
| 1539 | bswap_ehdr(&ehdr); |
| 1540 | if (!elf_check_ehdr(&ehdr)) { |
| 1541 | return 0; |
| 1542 | } |
| 1543 | |
| 1544 | /* return architecture id */ |
| 1545 | return ehdr.e_flags; |
| 1546 | } |
| 1547 | |
| 1548 | int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) |
| 1549 | { |
| 1550 | /* |
| 1551 | * We need a copy of the elf header for passing to create_elf_tables. |
| 1552 | * We will have overwritten the original when we re-use bprm->buf |
| 1553 | * while loading the interpreter. Allocate the storage for this now |
| 1554 | * and let elf_load_image do any swapping that may be required. |
| 1555 | */ |
| 1556 | struct elfhdr ehdr; |
| 1557 | struct image_info interp_info, vdso_info; |
| 1558 | char *elf_interpreter = NULL; |
| 1559 | char *scratch; |
| 1560 | |
| 1561 | memset(&interp_info, 0, sizeof(interp_info)); |
| 1562 | #ifdef TARGET_MIPS |
| 1563 | interp_info.fp_abi = MIPS_ABI_FP_UNKNOWN; |
| 1564 | #endif |
| 1565 | |
| 1566 | load_elf_image(bprm->filename, &bprm->src, info, &ehdr, &elf_interpreter); |
| 1567 | |
| 1568 | /* Do this so that we can load the interpreter, if need be. We will |
| 1569 | change some of these later */ |
| 1570 | bprm->p = setup_arg_pages(bprm, info); |
| 1571 | |
| 1572 | scratch = g_new0(char, TARGET_PAGE_SIZE); |
| 1573 | if (STACK_GROWS_DOWN) { |
| 1574 | bprm->p = copy_elf_strings(1, &bprm->filename, scratch, |
| 1575 | bprm->p, info->stack_limit); |
| 1576 | info->file_string = bprm->p; |
| 1577 | bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch, |
| 1578 | bprm->p, info->stack_limit); |
| 1579 | info->env_strings = bprm->p; |
| 1580 | bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch, |
| 1581 | bprm->p, info->stack_limit); |
| 1582 | info->arg_strings = bprm->p; |
| 1583 | } else { |
| 1584 | info->arg_strings = bprm->p; |
| 1585 | bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch, |
| 1586 | bprm->p, info->stack_limit); |
| 1587 | info->env_strings = bprm->p; |
| 1588 | bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch, |
| 1589 | bprm->p, info->stack_limit); |
| 1590 | info->file_string = bprm->p; |
| 1591 | bprm->p = copy_elf_strings(1, &bprm->filename, scratch, |
| 1592 | bprm->p, info->stack_limit); |
| 1593 | } |
| 1594 | |
| 1595 | g_free(scratch); |
| 1596 | |
| 1597 | if (!bprm->p) { |
| 1598 | fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG)); |
| 1599 | exit(-1); |
| 1600 | } |
| 1601 | |
| 1602 | if (elf_interpreter) { |
| 1603 | load_elf_interp(elf_interpreter, &interp_info, bprm->buf); |
| 1604 | |
| 1605 | /* |
| 1606 | * While unusual because of ELF_ET_DYN_BASE, if we are unlucky |
| 1607 | * with the mappings the interpreter can be loaded above but |
| 1608 | * near the main executable, which can leave very little room |
| 1609 | * for the heap. |
| 1610 | * If the current brk has less than 16MB, use the end of the |
| 1611 | * interpreter. |
| 1612 | */ |
| 1613 | if (interp_info.brk > info->brk && |
| 1614 | interp_info.load_bias - info->brk < 16 * MiB) { |
| 1615 | info->brk = interp_info.brk; |
| 1616 | } |
| 1617 | |
| 1618 | /* If the program interpreter is one of these two, then assume |
| 1619 | an iBCS2 image. Otherwise assume a native linux image. */ |
| 1620 | |
| 1621 | if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0 |
| 1622 | || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) { |
| 1623 | info->personality = PER_SVR4; |
| 1624 | |
| 1625 | /* Why this, you ask??? Well SVr4 maps page 0 as read-only, |
| 1626 | and some applications "depend" upon this behavior. Since |
| 1627 | we do not have the power to recompile these, we emulate |
| 1628 | the SVr4 behavior. Sigh. */ |
| 1629 | target_mmap(0, TARGET_PAGE_SIZE, PROT_READ | PROT_EXEC, |
| 1630 | MAP_FIXED_NOREPLACE | MAP_PRIVATE | MAP_ANONYMOUS, |
| 1631 | -1, 0); |
| 1632 | } |
| 1633 | #ifdef TARGET_MIPS |
| 1634 | info->interp_fp_abi = interp_info.fp_abi; |
| 1635 | #endif |
| 1636 | } |
| 1637 | |
| 1638 | /* |
| 1639 | * Load a vdso if available, which will amongst other things contain the |
| 1640 | * signal trampolines. Otherwise, allocate a separate page for them. |
| 1641 | */ |
| 1642 | const VdsoImageInfo *vdso = get_vdso_image_info(info->elf_flags); |
| 1643 | if (vdso) { |
| 1644 | load_elf_vdso(&vdso_info, vdso); |
| 1645 | info->vdso = vdso_info.load_bias; |
| 1646 | } else if (TARGET_ARCH_HAS_SIGTRAMP_PAGE) { |
| 1647 | abi_long tramp_page = target_mmap(0, TARGET_PAGE_SIZE, |
| 1648 | PROT_READ | PROT_WRITE, |
| 1649 | MAP_PRIVATE | MAP_ANON, -1, 0); |
| 1650 | if (tramp_page == -1) { |
| 1651 | return -errno; |
| 1652 | } |
| 1653 | |
| 1654 | setup_sigtramp(tramp_page); |
| 1655 | target_mprotect(tramp_page, TARGET_PAGE_SIZE, PROT_READ | PROT_EXEC); |
| 1656 | vdso_sigreturn_region_start = tramp_page; |
| 1657 | vdso_sigreturn_region_end = tramp_page + TARGET_PAGE_SIZE; |
| 1658 | } |
| 1659 | |
| 1660 | bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &ehdr, info, |
| 1661 | elf_interpreter ? &interp_info : NULL, |
| 1662 | vdso ? &vdso_info : NULL); |
| 1663 | info->start_stack = bprm->p; |
| 1664 | |
| 1665 | /* If we have an interpreter, set that as the program's entry point. |
| 1666 | Copy the load_bias as well, to help PPC64 interpret the entry |
| 1667 | point as a function descriptor. Do this after creating elf tables |
| 1668 | so that we copy the original program entry point into the AUXV. */ |
| 1669 | if (elf_interpreter) { |
| 1670 | info->load_bias = interp_info.load_bias; |
| 1671 | info->entry = interp_info.entry; |
| 1672 | g_free(elf_interpreter); |
| 1673 | } |
| 1674 | |
| 1675 | #ifdef HAVE_ELF_CORE_DUMP |
| 1676 | bprm->core_dump = &elf_core_dump; |
| 1677 | #endif |
| 1678 | |
| 1679 | return 0; |
| 1680 | } |
| 1681 | |
| 1682 | #ifdef HAVE_ELF_CORE_DUMP |
| 1683 | |
| 1684 | /* |
| 1685 | * Definitions to generate Intel SVR4-like core files. |
| 1686 | * These mostly have the same names as the SVR4 types with "target_elf_" |
| 1687 | * tacked on the front to prevent clashes with linux definitions, |
| 1688 | * and the typedef forms have been avoided. This is mostly like |
| 1689 | * the SVR4 structure, but more Linuxy, with things that Linux does |
| 1690 | * not support and which gdb doesn't really use excluded. |
| 1691 | * |
| 1692 | * Fields we don't dump (their contents is zero) in linux-user qemu |
| 1693 | * are marked with XXX. |
| 1694 | * |
| 1695 | * Core dump code is copied from linux kernel (fs/binfmt_elf.c). |
| 1696 | * |
| 1697 | * Porting ELF coredump for target is (quite) simple process. First you |
| 1698 | * define HAVE_ELF_CORE_DUMP in target ELF code (where init_thread() for |
| 1699 | * the target resides): |
| 1700 | * |
| 1701 | * #define HAVE_ELF_CORE_DUMP |
| 1702 | * |
| 1703 | * Next you define type of register set used for dumping: |
| 1704 | * typedef struct target_elf_gregset_t { ... } target_elf_gregset_t; |
| 1705 | * |
| 1706 | * Last step is to implement target specific function that copies registers |
| 1707 | * from given cpu into just specified register set. Prototype is: |
| 1708 | * |
| 1709 | * void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUArchState *env); |
| 1710 | * |
| 1711 | * Parameters: |
| 1712 | * regs - copy register values into here (allocated and zeroed by caller) |
| 1713 | * env - copy registers from here |
| 1714 | * |
| 1715 | * Example for ARM target is provided in this file. |
| 1716 | */ |
| 1717 | |
| 1718 | struct target_elf_siginfo { |
| 1719 | abi_int si_signo; /* signal number */ |
| 1720 | abi_int si_code; /* extra code */ |
| 1721 | abi_int si_errno; /* errno */ |
| 1722 | }; |
| 1723 | |
| 1724 | struct target_elf_prstatus { |
| 1725 | struct target_elf_siginfo pr_info; /* Info associated with signal */ |
| 1726 | abi_short pr_cursig; /* Current signal */ |
| 1727 | abi_ulong pr_sigpend; /* XXX */ |
| 1728 | abi_ulong pr_sighold; /* XXX */ |
| 1729 | target_pid_t pr_pid; |
| 1730 | target_pid_t pr_ppid; |
| 1731 | target_pid_t pr_pgrp; |
| 1732 | target_pid_t pr_sid; |
| 1733 | struct target_timeval pr_utime; /* XXX User time */ |
| 1734 | struct target_timeval pr_stime; /* XXX System time */ |
| 1735 | struct target_timeval pr_cutime; /* XXX Cumulative user time */ |
| 1736 | struct target_timeval pr_cstime; /* XXX Cumulative system time */ |
| 1737 | target_elf_gregset_t pr_reg; /* GP registers */ |
| 1738 | abi_int pr_fpvalid; /* XXX */ |
| 1739 | }; |
| 1740 | |
| 1741 | #define ELF_PRARGSZ (80) /* Number of chars for args */ |
| 1742 | |
| 1743 | struct target_elf_prpsinfo { |
| 1744 | char pr_state; /* numeric process state */ |
| 1745 | char pr_sname; /* char for pr_state */ |
| 1746 | char pr_zomb; /* zombie */ |
| 1747 | char pr_nice; /* nice val */ |
| 1748 | abi_ulong pr_flag; /* flags */ |
| 1749 | target_uid_t pr_uid; |
| 1750 | target_gid_t pr_gid; |
| 1751 | target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid; |
| 1752 | /* Lots missing */ |
| 1753 | char pr_fname[16] QEMU_NONSTRING; /* filename of executable */ |
| 1754 | char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */ |
| 1755 | }; |
| 1756 | |
| 1757 | static void bswap_prstatus(struct target_elf_prstatus *prstatus) |
| 1758 | { |
| 1759 | if (!target_needs_bswap()) { |
| 1760 | return; |
| 1761 | } |
| 1762 | |
| 1763 | prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo); |
| 1764 | prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code); |
| 1765 | prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno); |
| 1766 | prstatus->pr_cursig = tswap16(prstatus->pr_cursig); |
| 1767 | prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend); |
| 1768 | prstatus->pr_sighold = tswapal(prstatus->pr_sighold); |
| 1769 | prstatus->pr_pid = tswap32(prstatus->pr_pid); |
| 1770 | prstatus->pr_ppid = tswap32(prstatus->pr_ppid); |
| 1771 | prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp); |
| 1772 | prstatus->pr_sid = tswap32(prstatus->pr_sid); |
| 1773 | /* cpu times are not filled, so we skip them */ |
| 1774 | /* regs should be in correct format already */ |
| 1775 | prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid); |
| 1776 | } |
| 1777 | |
| 1778 | static void bswap_psinfo(struct target_elf_prpsinfo *psinfo) |
| 1779 | { |
| 1780 | if (!target_needs_bswap()) { |
| 1781 | return; |
| 1782 | } |
| 1783 | |
| 1784 | psinfo->pr_flag = tswapal(psinfo->pr_flag); |
| 1785 | psinfo->pr_uid = tswap16(psinfo->pr_uid); |
| 1786 | psinfo->pr_gid = tswap16(psinfo->pr_gid); |
| 1787 | psinfo->pr_pid = tswap32(psinfo->pr_pid); |
| 1788 | psinfo->pr_ppid = tswap32(psinfo->pr_ppid); |
| 1789 | psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp); |
| 1790 | psinfo->pr_sid = tswap32(psinfo->pr_sid); |
| 1791 | } |
| 1792 | |
| 1793 | static void bswap_note(struct elf_note *en) |
| 1794 | { |
| 1795 | if (!target_needs_bswap()) { |
| 1796 | return; |
| 1797 | } |
| 1798 | |
| 1799 | bswap32s(&en->n_namesz); |
| 1800 | bswap32s(&en->n_descsz); |
| 1801 | bswap32s(&en->n_type); |
| 1802 | } |
| 1803 | |
| 1804 | /* |
| 1805 | * Calculate file (dump) size of given memory region. |
| 1806 | */ |
| 1807 | static size_t vma_dump_size(vaddr start, vaddr end, int flags) |
| 1808 | { |
| 1809 | /* The area must be readable and dumpable. */ |
| 1810 | if (!(flags & PAGE_READ) || (flags & PAGE_DONTDUMP)) { |
| 1811 | return 0; |
| 1812 | } |
| 1813 | |
| 1814 | /* |
| 1815 | * Usually we don't dump executable pages as they contain |
| 1816 | * non-writable code that debugger can read directly from |
| 1817 | * target library etc. If there is no elf header, we dump it. |
| 1818 | */ |
| 1819 | if (!(flags & PAGE_WRITE_ORG) && |
| 1820 | (flags & PAGE_EXEC) && |
| 1821 | memcmp(g2h_untagged(start), ELFMAG, SELFMAG) == 0) { |
| 1822 | return 0; |
| 1823 | } |
| 1824 | |
| 1825 | return end - start; |
| 1826 | } |
| 1827 | |
| 1828 | static size_t size_note(const char *name, size_t datasz) |
| 1829 | { |
| 1830 | size_t namesz = strlen(name) + 1; |
| 1831 | |
| 1832 | namesz = ROUND_UP(namesz, 4); |
| 1833 | datasz = ROUND_UP(datasz, 4); |
| 1834 | |
| 1835 | return sizeof(struct elf_note) + namesz + datasz; |
| 1836 | } |
| 1837 | |
| 1838 | static void *fill_note(void **pptr, int type, const char *name, size_t datasz) |
| 1839 | { |
| 1840 | void *ptr = *pptr; |
| 1841 | struct elf_note *n = ptr; |
| 1842 | size_t namesz = strlen(name) + 1; |
| 1843 | |
| 1844 | n->n_namesz = namesz; |
| 1845 | n->n_descsz = datasz; |
| 1846 | n->n_type = type; |
| 1847 | bswap_note(n); |
| 1848 | |
| 1849 | ptr += sizeof(*n); |
| 1850 | memcpy(ptr, name, namesz); |
| 1851 | |
| 1852 | namesz = ROUND_UP(namesz, 4); |
| 1853 | datasz = ROUND_UP(datasz, 4); |
| 1854 | |
| 1855 | *pptr = ptr + namesz + datasz; |
| 1856 | return ptr + namesz; |
| 1857 | } |
| 1858 | |
| 1859 | static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine, |
| 1860 | uint32_t flags) |
| 1861 | { |
| 1862 | memcpy(elf->e_ident, ELFMAG, SELFMAG); |
| 1863 | |
| 1864 | elf->e_ident[EI_CLASS] = ELF_CLASS; |
| 1865 | elf->e_ident[EI_DATA] = ELF_DATA; |
| 1866 | elf->e_ident[EI_VERSION] = EV_CURRENT; |
| 1867 | elf->e_ident[EI_OSABI] = ELF_OSABI; |
| 1868 | |
| 1869 | elf->e_type = ET_CORE; |
| 1870 | elf->e_machine = machine; |
| 1871 | elf->e_version = EV_CURRENT; |
| 1872 | elf->e_phoff = sizeof(struct elfhdr); |
| 1873 | elf->e_flags = flags; |
| 1874 | elf->e_ehsize = sizeof(struct elfhdr); |
| 1875 | elf->e_phentsize = sizeof(struct elf_phdr); |
| 1876 | elf->e_phnum = segs; |
| 1877 | |
| 1878 | bswap_ehdr(elf); |
| 1879 | } |
| 1880 | |
| 1881 | static void fill_elf_note_phdr(struct elf_phdr *phdr, size_t sz, off_t offset) |
| 1882 | { |
| 1883 | phdr->p_type = PT_NOTE; |
| 1884 | phdr->p_offset = offset; |
| 1885 | phdr->p_filesz = sz; |
| 1886 | |
| 1887 | bswap_phdr(phdr, 1); |
| 1888 | } |
| 1889 | |
| 1890 | #ifdef HAVE_ELF_CORE_FPREGS |
| 1891 | static void fill_fpregset_note(void *data, CPUState *cpu) |
| 1892 | { |
| 1893 | /* Fill locally and copy: note memory is only aligned to 4. */ |
| 1894 | target_elf_fpregset_t fpregs = {}; |
| 1895 | |
| 1896 | elf_core_copy_fpregs(&fpregs, cpu_env(cpu)); |
| 1897 | memcpy(data, &fpregs, sizeof(fpregs)); |
| 1898 | } |
| 1899 | #endif |
| 1900 | |
| 1901 | static void fill_prstatus_note(void *data, CPUState *cpu, int signr) |
| 1902 | { |
| 1903 | /* |
| 1904 | * Because note memory is only aligned to 4, and target_elf_prstatus |
| 1905 | * may well have higher alignment requirements, fill locally and |
| 1906 | * memcpy to the destination afterward. |
| 1907 | */ |
| 1908 | struct target_elf_prstatus prstatus = { |
| 1909 | .pr_info.si_signo = signr, |
| 1910 | .pr_cursig = signr, |
| 1911 | .pr_pid = get_task_state(cpu)->ts_tid, |
| 1912 | .pr_ppid = getppid(), |
| 1913 | .pr_pgrp = getpgrp(), |
| 1914 | .pr_sid = getsid(0), |
| 1915 | }; |
| 1916 | |
| 1917 | elf_core_copy_regs(&prstatus.pr_reg, cpu_env(cpu)); |
| 1918 | bswap_prstatus(&prstatus); |
| 1919 | memcpy(data, &prstatus, sizeof(prstatus)); |
| 1920 | } |
| 1921 | |
| 1922 | static void fill_prpsinfo_note(void *data, const TaskState *ts) |
| 1923 | { |
| 1924 | /* |
| 1925 | * Because note memory is only aligned to 4, and target_elf_prpsinfo |
| 1926 | * may well have higher alignment requirements, fill locally and |
| 1927 | * memcpy to the destination afterward. |
| 1928 | */ |
| 1929 | struct target_elf_prpsinfo psinfo = { |
| 1930 | .pr_pid = getpid(), |
| 1931 | .pr_ppid = getppid(), |
| 1932 | .pr_pgrp = getpgrp(), |
| 1933 | .pr_sid = getsid(0), |
| 1934 | .pr_uid = getuid(), |
| 1935 | .pr_gid = getgid(), |
| 1936 | }; |
| 1937 | char *base_filename; |
| 1938 | size_t len; |
| 1939 | |
| 1940 | len = ts->info->env_strings - ts->info->arg_strings; |
| 1941 | len = MIN(len, ELF_PRARGSZ); |
| 1942 | memcpy(&psinfo.pr_psargs, g2h_untagged(ts->info->arg_strings), len); |
| 1943 | for (size_t i = 0; i < len; i++) { |
| 1944 | if (psinfo.pr_psargs[i] == 0) { |
| 1945 | psinfo.pr_psargs[i] = ' '; |
| 1946 | } |
| 1947 | } |
| 1948 | |
| 1949 | base_filename = g_path_get_basename(ts->bprm->filename); |
| 1950 | /* |
| 1951 | * Using strncpy here is fine: at max-length, |
| 1952 | * this field is not NUL-terminated. |
| 1953 | */ |
| 1954 | strncpy(psinfo.pr_fname, base_filename, sizeof(psinfo.pr_fname)); |
| 1955 | g_free(base_filename); |
| 1956 | |
| 1957 | bswap_psinfo(&psinfo); |
| 1958 | memcpy(data, &psinfo, sizeof(psinfo)); |
| 1959 | } |
| 1960 | |
| 1961 | static void fill_auxv_note(void *data, const TaskState *ts) |
| 1962 | { |
| 1963 | memcpy(data, g2h_untagged(ts->info->saved_auxv), ts->info->auxv_len); |
| 1964 | } |
| 1965 | |
| 1966 | /* |
| 1967 | * Constructs name of coredump file. We have following convention |
| 1968 | * for the name: |
| 1969 | * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core |
| 1970 | * |
| 1971 | * Returns the filename |
| 1972 | */ |
| 1973 | static char *core_dump_filename(const TaskState *ts) |
| 1974 | { |
| 1975 | g_autoptr(GDateTime) now = g_date_time_new_now_local(); |
| 1976 | g_autofree char *nowstr = g_date_time_format(now, "%Y%m%d-%H%M%S"); |
| 1977 | g_autofree char *base_filename = g_path_get_basename(ts->bprm->filename); |
| 1978 | |
| 1979 | return g_strdup_printf("qemu_%s_%s_%d.core", |
| 1980 | base_filename, nowstr, (int)getpid()); |
| 1981 | } |
| 1982 | |
| 1983 | static int dump_write(int fd, const void *ptr, size_t size) |
| 1984 | { |
| 1985 | const char *bufp = (const char *)ptr; |
| 1986 | ssize_t bytes_written, bytes_left; |
| 1987 | |
| 1988 | bytes_written = 0; |
| 1989 | bytes_left = size; |
| 1990 | |
| 1991 | /* |
| 1992 | * In normal conditions, single write(2) should do but |
| 1993 | * in case of socket etc. this mechanism is more portable. |
| 1994 | */ |
| 1995 | do { |
| 1996 | bytes_written = write(fd, bufp, bytes_left); |
| 1997 | if (bytes_written < 0) { |
| 1998 | if (errno == EINTR) |
| 1999 | continue; |
| 2000 | return (-1); |
| 2001 | } else if (bytes_written == 0) { /* eof */ |
| 2002 | return (-1); |
| 2003 | } |
| 2004 | bufp += bytes_written; |
| 2005 | bytes_left -= bytes_written; |
| 2006 | } while (bytes_left > 0); |
| 2007 | |
| 2008 | return (0); |
| 2009 | } |
| 2010 | |
| 2011 | static int wmr_page_unprotect_regions(void *opaque, vaddr start, |
| 2012 | vaddr end, int flags) |
| 2013 | { |
| 2014 | if ((flags & (PAGE_WRITE | PAGE_WRITE_ORG)) == PAGE_WRITE_ORG) { |
| 2015 | size_t step = MAX(TARGET_PAGE_SIZE, qemu_real_host_page_size()); |
| 2016 | |
| 2017 | while (1) { |
| 2018 | page_unprotect(NULL, start, 0); |
| 2019 | if (end - start <= step) { |
| 2020 | break; |
| 2021 | } |
| 2022 | start += step; |
| 2023 | } |
| 2024 | } |
| 2025 | return 0; |
| 2026 | } |
| 2027 | |
| 2028 | typedef struct { |
| 2029 | unsigned count; |
| 2030 | size_t size; |
| 2031 | } CountAndSizeRegions; |
| 2032 | |
| 2033 | static int wmr_count_and_size_regions(void *opaque, vaddr start, |
| 2034 | vaddr end, int flags) |
| 2035 | { |
| 2036 | CountAndSizeRegions *css = opaque; |
| 2037 | |
| 2038 | css->count++; |
| 2039 | css->size += vma_dump_size(start, end, flags); |
| 2040 | return 0; |
| 2041 | } |
| 2042 | |
| 2043 | typedef struct { |
| 2044 | struct elf_phdr *phdr; |
| 2045 | off_t offset; |
| 2046 | } FillRegionPhdr; |
| 2047 | |
| 2048 | static int wmr_fill_region_phdr(void *opaque, vaddr start, |
| 2049 | vaddr end, int flags) |
| 2050 | { |
| 2051 | FillRegionPhdr *d = opaque; |
| 2052 | struct elf_phdr *phdr = d->phdr; |
| 2053 | |
| 2054 | phdr->p_type = PT_LOAD; |
| 2055 | phdr->p_vaddr = start; |
| 2056 | phdr->p_paddr = 0; |
| 2057 | phdr->p_filesz = vma_dump_size(start, end, flags); |
| 2058 | phdr->p_offset = d->offset; |
| 2059 | d->offset += phdr->p_filesz; |
| 2060 | phdr->p_memsz = end - start; |
| 2061 | phdr->p_flags = (flags & PAGE_READ ? PF_R : 0) |
| 2062 | | (flags & PAGE_WRITE_ORG ? PF_W : 0) |
| 2063 | | (flags & PAGE_EXEC ? PF_X : 0); |
| 2064 | phdr->p_align = TARGET_PAGE_SIZE; |
| 2065 | |
| 2066 | bswap_phdr(phdr, 1); |
| 2067 | d->phdr = phdr + 1; |
| 2068 | return 0; |
| 2069 | } |
| 2070 | |
| 2071 | static int wmr_write_region(void *opaque, vaddr start, |
| 2072 | vaddr end, int flags) |
| 2073 | { |
| 2074 | int fd = *(int *)opaque; |
| 2075 | size_t size = vma_dump_size(start, end, flags); |
| 2076 | |
| 2077 | if (!size) { |
| 2078 | return 0; |
| 2079 | } |
| 2080 | return dump_write(fd, g2h_untagged(start), size); |
| 2081 | } |
| 2082 | |
| 2083 | /* |
| 2084 | * Write out ELF coredump. |
| 2085 | * |
| 2086 | * See documentation of ELF object file format in: |
| 2087 | * http://www.caldera.com/developers/devspecs/gabi41.pdf |
| 2088 | * |
| 2089 | * Coredump format in linux is following: |
| 2090 | * |
| 2091 | * 0 +----------------------+ \ |
| 2092 | * | ELF header | ET_CORE | |
| 2093 | * +----------------------+ | |
| 2094 | * | ELF program headers | |--- headers |
| 2095 | * | - NOTE section | | |
| 2096 | * | - PT_LOAD sections | | |
| 2097 | * +----------------------+ / |
| 2098 | * | NOTEs: | |
| 2099 | * | - NT_PRSTATUS | |
| 2100 | * | - NT_PRSINFO | |
| 2101 | * | - NT_AUXV | |
| 2102 | * +----------------------+ <-- aligned to target page |
| 2103 | * | Process memory dump | |
| 2104 | * : : |
| 2105 | * . . |
| 2106 | * : : |
| 2107 | * | | |
| 2108 | * +----------------------+ |
| 2109 | * |
| 2110 | * NT_PRSTATUS -> struct elf_prstatus (per thread) |
| 2111 | * NT_PRSINFO -> struct elf_prpsinfo |
| 2112 | * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()). |
| 2113 | * |
| 2114 | * Format follows System V format as close as possible. Current |
| 2115 | * version limitations are as follows: |
| 2116 | * - no floating point registers are dumped |
| 2117 | * |
| 2118 | * Function returns 0 in case of success, negative errno otherwise. |
| 2119 | * |
| 2120 | * TODO: make this work also during runtime: it should be |
| 2121 | * possible to force coredump from running process and then |
| 2122 | * continue processing. For example qemu could set up SIGUSR2 |
| 2123 | * handler (provided that target process haven't registered |
| 2124 | * handler for that) that does the dump when signal is received. |
| 2125 | */ |
| 2126 | #ifdef TARGET_SPARC |
| 2127 | #include "sparc/cpu_loop.h" |
| 2128 | #endif |
| 2129 | static int elf_core_dump(int signr, const CPUArchState *env) |
| 2130 | { |
| 2131 | const CPUState *cpu = env_cpu_const(env); |
| 2132 | const TaskState *ts = (const TaskState *)get_task_state((CPUState *)cpu); |
| 2133 | struct rlimit dumpsize; |
| 2134 | CountAndSizeRegions css; |
| 2135 | off_t offset, note_offset, data_offset; |
| 2136 | size_t note_size; |
| 2137 | int cpus, ret; |
| 2138 | int fd = -1; |
| 2139 | CPUState *cpu_iter; |
| 2140 | |
| 2141 | if (prctl(PR_GET_DUMPABLE) == 0) { |
| 2142 | return 0; |
| 2143 | } |
| 2144 | |
| 2145 | if (getrlimit(RLIMIT_CORE, &dumpsize) < 0 || dumpsize.rlim_cur == 0) { |
| 2146 | return 0; |
| 2147 | } |
| 2148 | |
| 2149 | cpu_list_lock(); |
| 2150 | mmap_lock(); |
| 2151 | |
| 2152 | #ifdef TARGET_SPARC |
| 2153 | CPU_FOREACH(cpu_iter) { |
| 2154 | flush_windows(cpu_env(cpu_iter)); |
| 2155 | } |
| 2156 | #endif |
| 2157 | |
| 2158 | /* By unprotecting, we merge vmas that might be split. */ |
| 2159 | walk_memory_regions(NULL, wmr_page_unprotect_regions); |
| 2160 | |
| 2161 | /* |
| 2162 | * Walk through target process memory mappings and |
| 2163 | * set up structure containing this information. |
| 2164 | */ |
| 2165 | memset(&css, 0, sizeof(css)); |
| 2166 | walk_memory_regions(&css, wmr_count_and_size_regions); |
| 2167 | |
| 2168 | cpus = 0; |
| 2169 | CPU_FOREACH(cpu_iter) { |
| 2170 | cpus++; |
| 2171 | } |
| 2172 | |
| 2173 | offset = sizeof(struct elfhdr); |
| 2174 | offset += (css.count + 1) * sizeof(struct elf_phdr); |
| 2175 | note_offset = offset; |
| 2176 | |
| 2177 | offset += size_note("CORE", ts->info->auxv_len); |
| 2178 | offset += size_note("CORE", sizeof(struct target_elf_prpsinfo)); |
| 2179 | offset += size_note("CORE", sizeof(struct target_elf_prstatus)) * cpus; |
| 2180 | #ifdef HAVE_ELF_CORE_FPREGS |
| 2181 | offset += size_note("CORE", sizeof(target_elf_fpregset_t)) * cpus; |
| 2182 | #endif |
| 2183 | note_size = offset - note_offset; |
| 2184 | data_offset = TARGET_PAGE_ALIGN(offset); |
| 2185 | |
| 2186 | /* Do not dump if the corefile size exceeds the limit. */ |
| 2187 | if (dumpsize.rlim_cur != RLIM_INFINITY |
| 2188 | && dumpsize.rlim_cur < data_offset + css.size) { |
| 2189 | errno = 0; |
| 2190 | goto out; |
| 2191 | } |
| 2192 | |
| 2193 | { |
| 2194 | g_autofree char *corefile = core_dump_filename(ts); |
| 2195 | fd = open(corefile, O_WRONLY | O_CREAT | O_TRUNC, |
| 2196 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
| 2197 | } |
| 2198 | if (fd < 0) { |
| 2199 | goto out; |
| 2200 | } |
| 2201 | |
| 2202 | /* |
| 2203 | * There is a fair amount of alignment padding within the notes |
| 2204 | * as well as preceeding the process memory. Allocate a zeroed |
| 2205 | * block to hold it all. Write all of the headers directly into |
| 2206 | * this buffer and then write it out as a block. |
| 2207 | */ |
| 2208 | { |
| 2209 | g_autofree void *header = g_malloc0(data_offset); |
| 2210 | FillRegionPhdr frp; |
| 2211 | void *hptr, *dptr; |
| 2212 | |
| 2213 | /* Create elf file header. */ |
| 2214 | hptr = header; |
| 2215 | fill_elf_header(hptr, css.count + 1, ELF_MACHINE, 0); |
| 2216 | hptr += sizeof(struct elfhdr); |
| 2217 | |
| 2218 | /* Create elf program headers. */ |
| 2219 | fill_elf_note_phdr(hptr, note_size, note_offset); |
| 2220 | hptr += sizeof(struct elf_phdr); |
| 2221 | |
| 2222 | frp.phdr = hptr; |
| 2223 | frp.offset = data_offset; |
| 2224 | walk_memory_regions(&frp, wmr_fill_region_phdr); |
| 2225 | hptr = frp.phdr; |
| 2226 | |
| 2227 | /* Create the notes. */ |
| 2228 | dptr = fill_note(&hptr, NT_AUXV, "CORE", ts->info->auxv_len); |
| 2229 | fill_auxv_note(dptr, ts); |
| 2230 | |
| 2231 | dptr = fill_note(&hptr, NT_PRPSINFO, "CORE", |
| 2232 | sizeof(struct target_elf_prpsinfo)); |
| 2233 | fill_prpsinfo_note(dptr, ts); |
| 2234 | |
| 2235 | CPU_FOREACH(cpu_iter) { |
| 2236 | dptr = fill_note(&hptr, NT_PRSTATUS, "CORE", |
| 2237 | sizeof(struct target_elf_prstatus)); |
| 2238 | fill_prstatus_note(dptr, cpu_iter, cpu_iter == cpu ? signr : 0); |
| 2239 | #ifdef HAVE_ELF_CORE_FPREGS |
| 2240 | dptr = fill_note(&hptr, NT_FPREGSET, "CORE", |
| 2241 | sizeof(target_elf_fpregset_t)); |
| 2242 | fill_fpregset_note(dptr, cpu_iter); |
| 2243 | #endif |
| 2244 | } |
| 2245 | |
| 2246 | if (dump_write(fd, header, data_offset) < 0) { |
| 2247 | goto out; |
| 2248 | } |
| 2249 | } |
| 2250 | |
| 2251 | /* |
| 2252 | * Finally write process memory into the corefile as well. |
| 2253 | */ |
| 2254 | if (walk_memory_regions(&fd, wmr_write_region) < 0) { |
| 2255 | goto out; |
| 2256 | } |
| 2257 | errno = 0; |
| 2258 | |
| 2259 | out: |
| 2260 | ret = -errno; |
| 2261 | mmap_unlock(); |
| 2262 | cpu_list_unlock(); |
| 2263 | if (fd >= 0) { |
| 2264 | close(fd); |
| 2265 | } |
| 2266 | return ret; |
| 2267 | } |
| 2268 | #endif /* HAVE_ELF_CORE_DUMP */ |