| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * genelf.c |
|---|
| 3 | 4 | * Copyright (C) 2014, Google, Inc |
|---|
| 4 | 5 | * |
|---|
| 5 | 6 | * Contributed by: |
|---|
| 6 | 7 | * Stephane Eranian <eranian@gmail.com> |
|---|
| 7 | | - * |
|---|
| 8 | | - * Released under the GPL v2. (and only v2, not any later version) |
|---|
| 9 | 8 | */ |
|---|
| 10 | 9 | |
|---|
| 11 | 10 | #include <sys/types.h> |
|---|
| 12 | | -#include <stdio.h> |
|---|
| 13 | | -#include <getopt.h> |
|---|
| 14 | 11 | #include <stddef.h> |
|---|
| 15 | 12 | #include <libelf.h> |
|---|
| 16 | 13 | #include <string.h> |
|---|
| 17 | 14 | #include <stdlib.h> |
|---|
| 15 | +#include <unistd.h> |
|---|
| 18 | 16 | #include <inttypes.h> |
|---|
| 19 | | -#include <limits.h> |
|---|
| 20 | 17 | #include <fcntl.h> |
|---|
| 21 | 18 | #include <err.h> |
|---|
| 22 | 19 | #ifdef HAVE_DWARF_SUPPORT |
|---|
| 23 | 20 | #include <dwarf.h> |
|---|
| 24 | 21 | #endif |
|---|
| 25 | 22 | |
|---|
| 26 | | -#include "perf.h" |
|---|
| 27 | 23 | #include "genelf.h" |
|---|
| 28 | 24 | #include "../util/jitdump.h" |
|---|
| 25 | +#include <linux/compiler.h> |
|---|
| 29 | 26 | |
|---|
| 30 | 27 | #ifndef NT_GNU_BUILD_ID |
|---|
| 31 | 28 | #define NT_GNU_BUILD_ID 3 |
|---|
| 32 | 29 | #endif |
|---|
| 33 | 30 | |
|---|
| 34 | | -#define JVMTI |
|---|
| 35 | | - |
|---|
| 36 | 31 | #define BUILD_ID_URANDOM /* different uuid for each run */ |
|---|
| 37 | 32 | |
|---|
| 38 | | -#ifdef HAVE_LIBCRYPTO |
|---|
| 33 | +// FIXME, remove this and fix the deprecation warnings before its removed and |
|---|
| 34 | +// We'll break for good here... |
|---|
| 35 | +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
|---|
| 36 | + |
|---|
| 37 | +#ifdef HAVE_LIBCRYPTO_SUPPORT |
|---|
| 39 | 38 | |
|---|
| 40 | 39 | #define BUILD_ID_MD5 |
|---|
| 41 | 40 | #undef BUILD_ID_SHA /* does not seem to work well when linked with Java */ |
|---|
| .. | .. |
|---|
| 252 | 251 | Elf_Data *d; |
|---|
| 253 | 252 | Elf_Scn *scn; |
|---|
| 254 | 253 | Elf_Ehdr *ehdr; |
|---|
| 254 | + Elf_Phdr *phdr; |
|---|
| 255 | 255 | Elf_Shdr *shdr; |
|---|
| 256 | 256 | uint64_t eh_frame_base_offset; |
|---|
| 257 | 257 | char *strsym = NULL; |
|---|
| .. | .. |
|---|
| 285 | 285 | ehdr->e_entry = GEN_ELF_TEXT_OFFSET; |
|---|
| 286 | 286 | ehdr->e_version = EV_CURRENT; |
|---|
| 287 | 287 | ehdr->e_shstrndx= unwinding ? 4 : 2; /* shdr index for section name */ |
|---|
| 288 | + |
|---|
| 289 | + /* |
|---|
| 290 | + * setup program header |
|---|
| 291 | + */ |
|---|
| 292 | + phdr = elf_newphdr(e, 1); |
|---|
| 293 | + phdr[0].p_type = PT_LOAD; |
|---|
| 294 | + phdr[0].p_offset = 0; |
|---|
| 295 | + phdr[0].p_vaddr = 0; |
|---|
| 296 | + phdr[0].p_paddr = 0; |
|---|
| 297 | + phdr[0].p_filesz = csize; |
|---|
| 298 | + phdr[0].p_memsz = csize; |
|---|
| 299 | + phdr[0].p_flags = PF_X | PF_R; |
|---|
| 300 | + phdr[0].p_align = 8; |
|---|
| 288 | 301 | |
|---|
| 289 | 302 | /* |
|---|
| 290 | 303 | * setup text section |
|---|
| .. | .. |
|---|
| 511 | 524 | |
|---|
| 512 | 525 | return retval; |
|---|
| 513 | 526 | } |
|---|
| 514 | | - |
|---|
| 515 | | -#ifndef JVMTI |
|---|
| 516 | | - |
|---|
| 517 | | -static unsigned char x86_code[] = { |
|---|
| 518 | | - 0xBB, 0x2A, 0x00, 0x00, 0x00, /* movl $42, %ebx */ |
|---|
| 519 | | - 0xB8, 0x01, 0x00, 0x00, 0x00, /* movl $1, %eax */ |
|---|
| 520 | | - 0xCD, 0x80 /* int $0x80 */ |
|---|
| 521 | | -}; |
|---|
| 522 | | - |
|---|
| 523 | | -static struct options options; |
|---|
| 524 | | - |
|---|
| 525 | | -int main(int argc, char **argv) |
|---|
| 526 | | -{ |
|---|
| 527 | | - int c, fd, ret; |
|---|
| 528 | | - |
|---|
| 529 | | - while ((c = getopt(argc, argv, "o:h")) != -1) { |
|---|
| 530 | | - switch (c) { |
|---|
| 531 | | - case 'o': |
|---|
| 532 | | - options.output = optarg; |
|---|
| 533 | | - break; |
|---|
| 534 | | - case 'h': |
|---|
| 535 | | - printf("Usage: genelf -o output_file [-h]\n"); |
|---|
| 536 | | - return 0; |
|---|
| 537 | | - default: |
|---|
| 538 | | - errx(1, "unknown option"); |
|---|
| 539 | | - } |
|---|
| 540 | | - } |
|---|
| 541 | | - |
|---|
| 542 | | - fd = open(options.output, O_CREAT|O_TRUNC|O_RDWR, 0666); |
|---|
| 543 | | - if (fd == -1) |
|---|
| 544 | | - err(1, "cannot create file %s", options.output); |
|---|
| 545 | | - |
|---|
| 546 | | - ret = jit_write_elf(fd, "main", x86_code, sizeof(x86_code)); |
|---|
| 547 | | - close(fd); |
|---|
| 548 | | - |
|---|
| 549 | | - if (ret != 0) |
|---|
| 550 | | - unlink(options.output); |
|---|
| 551 | | - |
|---|
| 552 | | - return ret; |
|---|
| 553 | | -} |
|---|
| 554 | | -#endif |
|---|