GNU Linux-libre 4.14.332-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 builid");
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_Shdr *shdr;
260         uint64_t eh_frame_base_offset;
261         char *strsym = NULL;
262         int symlen;
263         int retval = -1;
264
265         if (elf_version(EV_CURRENT) == EV_NONE) {
266                 warnx("ELF initialization failed");
267                 return -1;
268         }
269
270         e = elf_begin(fd, ELF_C_WRITE, NULL);
271         if (!e) {
272                 warnx("elf_begin failed");
273                 goto error;
274         }
275
276         /*
277          * setup ELF header
278          */
279         ehdr = elf_newehdr(e);
280         if (!ehdr) {
281                 warnx("cannot get ehdr");
282                 goto error;
283         }
284
285         ehdr->e_ident[EI_DATA] = GEN_ELF_ENDIAN;
286         ehdr->e_ident[EI_CLASS] = GEN_ELF_CLASS;
287         ehdr->e_machine = GEN_ELF_ARCH;
288         ehdr->e_type = ET_DYN;
289         ehdr->e_entry = GEN_ELF_TEXT_OFFSET;
290         ehdr->e_version = EV_CURRENT;
291         ehdr->e_shstrndx= unwinding ? 4 : 2; /* shdr index for section name */
292
293         /*
294          * setup text section
295          */
296         scn = elf_newscn(e);
297         if (!scn) {
298                 warnx("cannot create section");
299                 goto error;
300         }
301
302         d = elf_newdata(scn);
303         if (!d) {
304                 warnx("cannot get new data");
305                 goto error;
306         }
307
308         d->d_align = 16;
309         d->d_off = 0LL;
310         d->d_buf = (void *)code;
311         d->d_type = ELF_T_BYTE;
312         d->d_size = csize;
313         d->d_version = EV_CURRENT;
314
315         shdr = elf_getshdr(scn);
316         if (!shdr) {
317                 warnx("cannot get section header");
318                 goto error;
319         }
320
321         shdr->sh_name = 1;
322         shdr->sh_type = SHT_PROGBITS;
323         shdr->sh_addr = GEN_ELF_TEXT_OFFSET;
324         shdr->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
325         shdr->sh_entsize = 0;
326
327         /*
328          * Setup .eh_frame_hdr and .eh_frame
329          */
330         if (unwinding) {
331                 eh_frame_base_offset = ALIGN_8(GEN_ELF_TEXT_OFFSET + csize);
332                 retval = jit_add_eh_frame_info(e, unwinding,
333                                                unwinding_header_size, unwinding_size,
334                                                eh_frame_base_offset);
335                 if (retval)
336                         goto error;
337         }
338
339         /*
340          * setup section headers string table
341          */
342         scn = elf_newscn(e);
343         if (!scn) {
344                 warnx("cannot create section");
345                 goto error;
346         }
347
348         d = elf_newdata(scn);
349         if (!d) {
350                 warnx("cannot get new data");
351                 goto error;
352         }
353
354         d->d_align = 1;
355         d->d_off = 0LL;
356         d->d_buf = shd_string_table;
357         d->d_type = ELF_T_BYTE;
358         d->d_size = sizeof(shd_string_table);
359         d->d_version = EV_CURRENT;
360
361         shdr = elf_getshdr(scn);
362         if (!shdr) {
363                 warnx("cannot get section header");
364                 goto error;
365         }
366
367         shdr->sh_name = 7; /* offset of '.shstrtab' in shd_string_table */
368         shdr->sh_type = SHT_STRTAB;
369         shdr->sh_flags = 0;
370         shdr->sh_entsize = 0;
371
372         /*
373          * setup symtab section
374          */
375         symtab[1].st_size  = csize;
376         symtab[1].st_value = GEN_ELF_TEXT_OFFSET;
377
378         scn = elf_newscn(e);
379         if (!scn) {
380                 warnx("cannot create section");
381                 goto error;
382         }
383
384         d = elf_newdata(scn);
385         if (!d) {
386                 warnx("cannot get new data");
387                 goto error;
388         }
389
390         d->d_align = 8;
391         d->d_off = 0LL;
392         d->d_buf = symtab;
393         d->d_type = ELF_T_SYM;
394         d->d_size = sizeof(symtab);
395         d->d_version = EV_CURRENT;
396
397         shdr = elf_getshdr(scn);
398         if (!shdr) {
399                 warnx("cannot get section header");
400                 goto error;
401         }
402
403         shdr->sh_name = 17; /* offset of '.symtab' in shd_string_table */
404         shdr->sh_type = SHT_SYMTAB;
405         shdr->sh_flags = 0;
406         shdr->sh_entsize = sizeof(Elf_Sym);
407         shdr->sh_link = unwinding ? 6 : 4; /* index of .strtab section */
408
409         /*
410          * setup symbols string table
411          * 2 = 1 for 0 in 1st entry, 1 for the 0 at end of symbol for 2nd entry
412          */
413         symlen = 2 + strlen(sym);
414         strsym = calloc(1, symlen);
415         if (!strsym) {
416                 warnx("cannot allocate strsym");
417                 goto error;
418         }
419         strcpy(strsym + 1, sym);
420
421         scn = elf_newscn(e);
422         if (!scn) {
423                 warnx("cannot create section");
424                 goto error;
425         }
426
427         d = elf_newdata(scn);
428         if (!d) {
429                 warnx("cannot get new data");
430                 goto error;
431         }
432
433         d->d_align = 1;
434         d->d_off = 0LL;
435         d->d_buf = strsym;
436         d->d_type = ELF_T_BYTE;
437         d->d_size = symlen;
438         d->d_version = EV_CURRENT;
439
440         shdr = elf_getshdr(scn);
441         if (!shdr) {
442                 warnx("cannot get section header");
443                 goto error;
444         }
445
446         shdr->sh_name = 25; /* offset in shd_string_table */
447         shdr->sh_type = SHT_STRTAB;
448         shdr->sh_flags = 0;
449         shdr->sh_entsize = 0;
450
451         /*
452          * setup build-id section
453          */
454         scn = elf_newscn(e);
455         if (!scn) {
456                 warnx("cannot create section");
457                 goto error;
458         }
459
460         d = elf_newdata(scn);
461         if (!d) {
462                 warnx("cannot get new data");
463                 goto error;
464         }
465
466         /*
467          * build-id generation
468          */
469         gen_build_id(&bnote, load_addr, code, csize);
470         bnote.desc.namesz = sizeof(bnote.name); /* must include 0 termination */
471         bnote.desc.descsz = sizeof(bnote.build_id);
472         bnote.desc.type   = NT_GNU_BUILD_ID;
473         strcpy(bnote.name, "GNU");
474
475         d->d_align = 4;
476         d->d_off = 0LL;
477         d->d_buf = &bnote;
478         d->d_type = ELF_T_BYTE;
479         d->d_size = sizeof(bnote);
480         d->d_version = EV_CURRENT;
481
482         shdr = elf_getshdr(scn);
483         if (!shdr) {
484                 warnx("cannot get section header");
485                 goto error;
486         }
487
488         shdr->sh_name = 33; /* offset in shd_string_table */
489         shdr->sh_type = SHT_NOTE;
490         shdr->sh_addr = 0x0;
491         shdr->sh_flags = SHF_ALLOC;
492         shdr->sh_size = sizeof(bnote);
493         shdr->sh_entsize = 0;
494
495 #ifdef HAVE_DWARF_SUPPORT
496         if (debug && nr_debug_entries) {
497                 retval = jit_add_debug_info(e, load_addr, debug, nr_debug_entries);
498                 if (retval)
499                         goto error;
500         } else
501 #endif
502         {
503                 if (elf_update(e, ELF_C_WRITE) < 0) {
504                         warnx("elf_update 4 failed");
505                         goto error;
506                 }
507         }
508
509         retval = 0;
510 error:
511         (void)elf_end(e);
512
513         free(strsym);
514
515
516         return retval;
517 }
518
519 #ifndef JVMTI
520
521 static unsigned char x86_code[] = {
522     0xBB, 0x2A, 0x00, 0x00, 0x00, /* movl $42, %ebx */
523     0xB8, 0x01, 0x00, 0x00, 0x00, /* movl $1, %eax */
524     0xCD, 0x80            /* int $0x80 */
525 };
526
527 static struct options options;
528
529 int main(int argc, char **argv)
530 {
531         int c, fd, ret;
532
533         while ((c = getopt(argc, argv, "o:h")) != -1) {
534                 switch (c) {
535                 case 'o':
536                         options.output = optarg;
537                         break;
538                 case 'h':
539                         printf("Usage: genelf -o output_file [-h]\n");
540                         return 0;
541                 default:
542                         errx(1, "unknown option");
543                 }
544         }
545
546         fd = open(options.output, O_CREAT|O_TRUNC|O_RDWR, 0666);
547         if (fd == -1)
548                 err(1, "cannot create file %s", options.output);
549
550         ret = jit_write_elf(fd, "main", x86_code, sizeof(x86_code));
551         close(fd);
552
553         if (ret != 0)
554                 unlink(options.output);
555
556         return ret;
557 }
558 #endif