GNU Linux-libre 4.19.286-gnu1
[releases.git] / tools / perf / util / genelf.c
1 /*
2  * genelf.c
3  * Copyright (C) 2014, Google, Inc
4  *
5  * Contributed by:
6  *      Stephane Eranian <eranian@gmail.com>
7  *
8  * Released under the GPL v2. (and only v2, not any later version)
9  */
10
11 #include <sys/types.h>
12 #include <stdio.h>
13 #include <getopt.h>
14 #include <stddef.h>
15 #include <libelf.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <inttypes.h>
19 #include <limits.h>
20 #include <fcntl.h>
21 #include <err.h>
22 #ifdef HAVE_DWARF_SUPPORT
23 #include <dwarf.h>
24 #endif
25
26 #include "perf.h"
27 #include "genelf.h"
28 #include "../util/jitdump.h"
29
30 #ifndef NT_GNU_BUILD_ID
31 #define NT_GNU_BUILD_ID 3
32 #endif
33
34 #define JVMTI
35
36 #define BUILD_ID_URANDOM /* different uuid for each run */
37
38 // FIXME, remove this and fix the deprecation warnings before its removed and
39 // We'll break for good here...
40 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
41
42 #ifdef HAVE_LIBCRYPTO_SUPPORT
43
44 #define BUILD_ID_MD5
45 #undef BUILD_ID_SHA     /* does not seem to work well when linked with Java */
46 #undef BUILD_ID_URANDOM /* different uuid for each run */
47
48 #ifdef BUILD_ID_SHA
49 #include <openssl/sha.h>
50 #endif
51
52 #ifdef BUILD_ID_MD5
53 #include <openssl/md5.h>
54 #endif
55 #endif
56
57
58 typedef struct {
59   unsigned int namesz;  /* Size of entry's owner string */
60   unsigned int descsz;  /* Size of the note descriptor */
61   unsigned int type;    /* Interpretation of the descriptor */
62   char         name[0]; /* Start of the name+desc data */
63 } Elf_Note;
64
65 struct options {
66         char *output;
67         int fd;
68 };
69
70 static char shd_string_table[] = {
71         0,
72         '.', 't', 'e', 'x', 't', 0,                     /*  1 */
73         '.', 's', 'h', 's', 't', 'r', 't', 'a', 'b', 0, /*  7 */
74         '.', 's', 'y', 'm', 't', 'a', 'b', 0,           /* 17 */
75         '.', 's', 't', 'r', 't', 'a', 'b', 0,           /* 25 */
76         '.', 'n', 'o', 't', 'e', '.', 'g', 'n', 'u', '.', 'b', 'u', 'i', 'l', 'd', '-', 'i', 'd', 0, /* 33 */
77         '.', 'd', 'e', 'b', 'u', 'g', '_', 'l', 'i', 'n', 'e', 0, /* 52 */
78         '.', 'd', 'e', 'b', 'u', 'g', '_', 'i', 'n', 'f', 'o', 0, /* 64 */
79         '.', 'd', 'e', 'b', 'u', 'g', '_', 'a', 'b', 'b', 'r', 'e', 'v', 0, /* 76 */
80         '.', 'e', 'h', '_', 'f', 'r', 'a', 'm', 'e', '_', 'h', 'd', 'r', 0, /* 90 */
81         '.', 'e', 'h', '_', 'f', 'r', 'a', 'm', 'e', 0, /* 104 */
82 };
83
84 static struct buildid_note {
85         Elf_Note desc;          /* descsz: size of build-id, must be multiple of 4 */
86         char     name[4];       /* GNU\0 */
87         char     build_id[20];
88 } bnote;
89
90 static Elf_Sym symtab[]={
91         /* symbol 0 MUST be the undefined symbol */
92         { .st_name  = 0, /* index in sym_string table */
93           .st_info  = ELF_ST_TYPE(STT_NOTYPE),
94           .st_shndx = 0, /* for now */
95           .st_value = 0x0,
96           .st_other = ELF_ST_VIS(STV_DEFAULT),
97           .st_size  = 0,
98         },
99         { .st_name  = 1, /* index in sym_string table */
100           .st_info  = ELF_ST_BIND(STB_LOCAL) | ELF_ST_TYPE(STT_FUNC),
101           .st_shndx = 1,
102           .st_value = 0, /* for now */
103           .st_other = ELF_ST_VIS(STV_DEFAULT),
104           .st_size  = 0, /* for now */
105         }
106 };
107
108 #ifdef BUILD_ID_URANDOM
109 static void
110 gen_build_id(struct buildid_note *note,
111              unsigned long load_addr __maybe_unused,
112              const void *code __maybe_unused,
113              size_t csize __maybe_unused)
114 {
115         int fd;
116         size_t sz = sizeof(note->build_id);
117         ssize_t sret;
118
119         fd = open("/dev/urandom", O_RDONLY);
120         if (fd == -1)
121                 err(1, "cannot access /dev/urandom for buildid");
122
123         sret = read(fd, note->build_id, sz);
124
125         close(fd);
126
127         if (sret != (ssize_t)sz)
128                 memset(note->build_id, 0, sz);
129 }
130 #endif
131
132 #ifdef BUILD_ID_SHA
133 static void
134 gen_build_id(struct buildid_note *note,
135              unsigned long load_addr __maybe_unused,
136              const void *code,
137              size_t csize)
138 {
139         if (sizeof(note->build_id) < SHA_DIGEST_LENGTH)
140                 errx(1, "build_id too small for SHA1");
141
142         SHA1(code, csize, (unsigned char *)note->build_id);
143 }
144 #endif
145
146 #ifdef BUILD_ID_MD5
147 static void
148 gen_build_id(struct buildid_note *note, unsigned long load_addr, const void *code, size_t csize)
149 {
150         MD5_CTX context;
151
152         if (sizeof(note->build_id) < 16)
153                 errx(1, "build_id too small for MD5");
154
155         MD5_Init(&context);
156         MD5_Update(&context, &load_addr, sizeof(load_addr));
157         MD5_Update(&context, code, csize);
158         MD5_Final((unsigned char *)note->build_id, &context);
159 }
160 #endif
161
162 static int
163 jit_add_eh_frame_info(Elf *e, void* unwinding, uint64_t unwinding_header_size,
164                       uint64_t unwinding_size, uint64_t base_offset)
165 {
166         Elf_Data *d;
167         Elf_Scn *scn;
168         Elf_Shdr *shdr;
169         uint64_t unwinding_table_size = unwinding_size - unwinding_header_size;
170
171         /*
172          * setup eh_frame section
173          */
174         scn = elf_newscn(e);
175         if (!scn) {
176                 warnx("cannot create section");
177                 return -1;
178         }
179
180         d = elf_newdata(scn);
181         if (!d) {
182                 warnx("cannot get new data");
183                 return -1;
184         }
185
186         d->d_align = 8;
187         d->d_off = 0LL;
188         d->d_buf = unwinding;
189         d->d_type = ELF_T_BYTE;
190         d->d_size = unwinding_table_size;
191         d->d_version = EV_CURRENT;
192
193         shdr = elf_getshdr(scn);
194         if (!shdr) {
195                 warnx("cannot get section header");
196                 return -1;
197         }
198
199         shdr->sh_name = 104;
200         shdr->sh_type = SHT_PROGBITS;
201         shdr->sh_addr = base_offset;
202         shdr->sh_flags = SHF_ALLOC;
203         shdr->sh_entsize = 0;
204
205         /*
206          * setup eh_frame_hdr section
207          */
208         scn = elf_newscn(e);
209         if (!scn) {
210                 warnx("cannot create section");
211                 return -1;
212         }
213
214         d = elf_newdata(scn);
215         if (!d) {
216                 warnx("cannot get new data");
217                 return -1;
218         }
219
220         d->d_align = 4;
221         d->d_off = 0LL;
222         d->d_buf = unwinding + unwinding_table_size;
223         d->d_type = ELF_T_BYTE;
224         d->d_size = unwinding_header_size;
225         d->d_version = EV_CURRENT;
226
227         shdr = elf_getshdr(scn);
228         if (!shdr) {
229                 warnx("cannot get section header");
230                 return -1;
231         }
232
233         shdr->sh_name = 90;
234         shdr->sh_type = SHT_PROGBITS;
235         shdr->sh_addr = base_offset + unwinding_table_size;
236         shdr->sh_flags = SHF_ALLOC;
237         shdr->sh_entsize = 0;
238
239         return 0;
240 }
241
242 /*
243  * fd: file descriptor open for writing for the output file
244  * load_addr: code load address (could be zero, just used for buildid)
245  * sym: function name (for native code - used as the symbol)
246  * code: the native code
247  * csize: the code size in bytes
248  */
249 int
250 jit_write_elf(int fd, uint64_t load_addr, const char *sym,
251               const void *code, int csize,
252               void *debug __maybe_unused, int nr_debug_entries __maybe_unused,
253               void *unwinding, uint64_t unwinding_header_size, uint64_t unwinding_size)
254 {
255         Elf *e;
256         Elf_Data *d;
257         Elf_Scn *scn;
258         Elf_Ehdr *ehdr;
259         Elf_Phdr *phdr;
260         Elf_Shdr *shdr;
261         uint64_t eh_frame_base_offset;
262         char *strsym = NULL;
263         int symlen;
264         int retval = -1;
265
266         if (elf_version(EV_CURRENT) == EV_NONE) {
267                 warnx("ELF initialization failed");
268                 return -1;
269         }
270
271         e = elf_begin(fd, ELF_C_WRITE, NULL);
272         if (!e) {
273                 warnx("elf_begin failed");
274                 goto error;
275         }
276
277         /*
278          * setup ELF header
279          */
280         ehdr = elf_newehdr(e);
281         if (!ehdr) {
282                 warnx("cannot get ehdr");
283                 goto error;
284         }
285
286         ehdr->e_ident[EI_DATA] = GEN_ELF_ENDIAN;
287         ehdr->e_ident[EI_CLASS] = GEN_ELF_CLASS;
288         ehdr->e_machine = GEN_ELF_ARCH;
289         ehdr->e_type = ET_DYN;
290         ehdr->e_entry = GEN_ELF_TEXT_OFFSET;
291         ehdr->e_version = EV_CURRENT;
292         ehdr->e_shstrndx= unwinding ? 4 : 2; /* shdr index for section name */
293
294         /*
295          * setup program header
296          */
297         phdr = elf_newphdr(e, 1);
298         phdr[0].p_type = PT_LOAD;
299         phdr[0].p_offset = 0;
300         phdr[0].p_vaddr = 0;
301         phdr[0].p_paddr = 0;
302         phdr[0].p_filesz = csize;
303         phdr[0].p_memsz = csize;
304         phdr[0].p_flags = PF_X | PF_R;
305         phdr[0].p_align = 8;
306
307         /*
308          * setup text section
309          */
310         scn = elf_newscn(e);
311         if (!scn) {
312                 warnx("cannot create section");
313                 goto error;
314         }
315
316         d = elf_newdata(scn);
317         if (!d) {
318                 warnx("cannot get new data");
319                 goto error;
320         }
321
322         d->d_align = 16;
323         d->d_off = 0LL;
324         d->d_buf = (void *)code;
325         d->d_type = ELF_T_BYTE;
326         d->d_size = csize;
327         d->d_version = EV_CURRENT;
328
329         shdr = elf_getshdr(scn);
330         if (!shdr) {
331                 warnx("cannot get section header");
332                 goto error;
333         }
334
335         shdr->sh_name = 1;
336         shdr->sh_type = SHT_PROGBITS;
337         shdr->sh_addr = GEN_ELF_TEXT_OFFSET;
338         shdr->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
339         shdr->sh_entsize = 0;
340
341         /*
342          * Setup .eh_frame_hdr and .eh_frame
343          */
344         if (unwinding) {
345                 eh_frame_base_offset = ALIGN_8(GEN_ELF_TEXT_OFFSET + csize);
346                 retval = jit_add_eh_frame_info(e, unwinding,
347                                                unwinding_header_size, unwinding_size,
348                                                eh_frame_base_offset);
349                 if (retval)
350                         goto error;
351         }
352
353         /*
354          * setup section headers string table
355          */
356         scn = elf_newscn(e);
357         if (!scn) {
358                 warnx("cannot create section");
359                 goto error;
360         }
361
362         d = elf_newdata(scn);
363         if (!d) {
364                 warnx("cannot get new data");
365                 goto error;
366         }
367
368         d->d_align = 1;
369         d->d_off = 0LL;
370         d->d_buf = shd_string_table;
371         d->d_type = ELF_T_BYTE;
372         d->d_size = sizeof(shd_string_table);
373         d->d_version = EV_CURRENT;
374
375         shdr = elf_getshdr(scn);
376         if (!shdr) {
377                 warnx("cannot get section header");
378                 goto error;
379         }
380
381         shdr->sh_name = 7; /* offset of '.shstrtab' in shd_string_table */
382         shdr->sh_type = SHT_STRTAB;
383         shdr->sh_flags = 0;
384         shdr->sh_entsize = 0;
385
386         /*
387          * setup symtab section
388          */
389         symtab[1].st_size  = csize;
390         symtab[1].st_value = GEN_ELF_TEXT_OFFSET;
391
392         scn = elf_newscn(e);
393         if (!scn) {
394                 warnx("cannot create section");
395                 goto error;
396         }
397
398         d = elf_newdata(scn);
399         if (!d) {
400                 warnx("cannot get new data");
401                 goto error;
402         }
403
404         d->d_align = 8;
405         d->d_off = 0LL;
406         d->d_buf = symtab;
407         d->d_type = ELF_T_SYM;
408         d->d_size = sizeof(symtab);
409         d->d_version = EV_CURRENT;
410
411         shdr = elf_getshdr(scn);
412         if (!shdr) {
413                 warnx("cannot get section header");
414                 goto error;
415         }
416
417         shdr->sh_name = 17; /* offset of '.symtab' in shd_string_table */
418         shdr->sh_type = SHT_SYMTAB;
419         shdr->sh_flags = 0;
420         shdr->sh_entsize = sizeof(Elf_Sym);
421         shdr->sh_link = unwinding ? 6 : 4; /* index of .strtab section */
422
423         /*
424          * setup symbols string table
425          * 2 = 1 for 0 in 1st entry, 1 for the 0 at end of symbol for 2nd entry
426          */
427         symlen = 2 + strlen(sym);
428         strsym = calloc(1, symlen);
429         if (!strsym) {
430                 warnx("cannot allocate strsym");
431                 goto error;
432         }
433         strcpy(strsym + 1, sym);
434
435         scn = elf_newscn(e);
436         if (!scn) {
437                 warnx("cannot create section");
438                 goto error;
439         }
440
441         d = elf_newdata(scn);
442         if (!d) {
443                 warnx("cannot get new data");
444                 goto error;
445         }
446
447         d->d_align = 1;
448         d->d_off = 0LL;
449         d->d_buf = strsym;
450         d->d_type = ELF_T_BYTE;
451         d->d_size = symlen;
452         d->d_version = EV_CURRENT;
453
454         shdr = elf_getshdr(scn);
455         if (!shdr) {
456                 warnx("cannot get section header");
457                 goto error;
458         }
459
460         shdr->sh_name = 25; /* offset in shd_string_table */
461         shdr->sh_type = SHT_STRTAB;
462         shdr->sh_flags = 0;
463         shdr->sh_entsize = 0;
464
465         /*
466          * setup build-id section
467          */
468         scn = elf_newscn(e);
469         if (!scn) {
470                 warnx("cannot create section");
471                 goto error;
472         }
473
474         d = elf_newdata(scn);
475         if (!d) {
476                 warnx("cannot get new data");
477                 goto error;
478         }
479
480         /*
481          * build-id generation
482          */
483         gen_build_id(&bnote, load_addr, code, csize);
484         bnote.desc.namesz = sizeof(bnote.name); /* must include 0 termination */
485         bnote.desc.descsz = sizeof(bnote.build_id);
486         bnote.desc.type   = NT_GNU_BUILD_ID;
487         strcpy(bnote.name, "GNU");
488
489         d->d_align = 4;
490         d->d_off = 0LL;
491         d->d_buf = &bnote;
492         d->d_type = ELF_T_BYTE;
493         d->d_size = sizeof(bnote);
494         d->d_version = EV_CURRENT;
495
496         shdr = elf_getshdr(scn);
497         if (!shdr) {
498                 warnx("cannot get section header");
499                 goto error;
500         }
501
502         shdr->sh_name = 33; /* offset in shd_string_table */
503         shdr->sh_type = SHT_NOTE;
504         shdr->sh_addr = 0x0;
505         shdr->sh_flags = SHF_ALLOC;
506         shdr->sh_size = sizeof(bnote);
507         shdr->sh_entsize = 0;
508
509 #ifdef HAVE_DWARF_SUPPORT
510         if (debug && nr_debug_entries) {
511                 retval = jit_add_debug_info(e, load_addr, debug, nr_debug_entries);
512                 if (retval)
513                         goto error;
514         } else
515 #endif
516         {
517                 if (elf_update(e, ELF_C_WRITE) < 0) {
518                         warnx("elf_update 4 failed");
519                         goto error;
520                 }
521         }
522
523         retval = 0;
524 error:
525         (void)elf_end(e);
526
527         free(strsym);
528
529
530         return retval;
531 }
532
533 #ifndef JVMTI
534
535 static unsigned char x86_code[] = {
536     0xBB, 0x2A, 0x00, 0x00, 0x00, /* movl $42, %ebx */
537     0xB8, 0x01, 0x00, 0x00, 0x00, /* movl $1, %eax */
538     0xCD, 0x80            /* int $0x80 */
539 };
540
541 static struct options options;
542
543 int main(int argc, char **argv)
544 {
545         int c, fd, ret;
546
547         while ((c = getopt(argc, argv, "o:h")) != -1) {
548                 switch (c) {
549                 case 'o':
550                         options.output = optarg;
551                         break;
552                 case 'h':
553                         printf("Usage: genelf -o output_file [-h]\n");
554                         return 0;
555                 default:
556                         errx(1, "unknown option");
557                 }
558         }
559
560         fd = open(options.output, O_CREAT|O_TRUNC|O_RDWR, 0666);
561         if (fd == -1)
562                 err(1, "cannot create file %s", options.output);
563
564         ret = jit_write_elf(fd, "main", x86_code, sizeof(x86_code));
565         close(fd);
566
567         if (ret != 0)
568                 unlink(options.output);
569
570         return ret;
571 }
572 #endif