xref: /openbmc/linux/tools/perf/util/genelf_debug.c (revision 539050f9)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * genelf_debug.c
4  * Copyright (C) 2015, Google, Inc
5  *
6  * Contributed by:
7  * 	Stephane Eranian <eranian@google.com>
8  *
9  * based on GPLv2 source code from Oprofile
10  * @remark Copyright 2007 OProfile authors
11  * @author Philippe Elie
12  */
13 #include <linux/compiler.h>
14 #include <linux/zalloc.h>
15 #include <sys/types.h>
16 #include <stdio.h>
17 #include <getopt.h>
18 #include <stddef.h>
19 #include <libelf.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <inttypes.h>
23 #include <limits.h>
24 #include <fcntl.h>
25 #include <err.h>
26 #include <dwarf.h>
27 
28 #include "genelf.h"
29 #include "../util/jitdump.h"
30 
31 #define BUFFER_EXT_DFL_SIZE	(4 * 1024)
32 
33 typedef uint32_t uword;
34 typedef uint16_t uhalf;
35 typedef int32_t  sword;
36 typedef int16_t  shalf;
37 typedef uint8_t  ubyte;
38 typedef int8_t   sbyte;
39 
40 struct buffer_ext {
41 	size_t cur_pos;
42 	size_t max_sz;
43 	void *data;
44 };
45 
46 static void
47 buffer_ext_dump(struct buffer_ext *be, const char *msg)
48 {
49 	size_t i;
50 	warnx("DUMP for %s", msg);
51 	for (i = 0 ; i < be->cur_pos; i++)
52 		warnx("%4zu 0x%02x", i, (((char *)be->data)[i]) & 0xff);
53 }
54 
55 static inline int
56 buffer_ext_add(struct buffer_ext *be, void *addr, size_t sz)
57 {
58 	void *tmp;
59 	size_t be_sz = be->max_sz;
60 
61 retry:
62 	if ((be->cur_pos + sz) < be_sz) {
63 		memcpy(be->data + be->cur_pos, addr, sz);
64 		be->cur_pos += sz;
65 		return 0;
66 	}
67 
68 	if (!be_sz)
69 		be_sz = BUFFER_EXT_DFL_SIZE;
70 	else
71 		be_sz <<= 1;
72 
73 	tmp = realloc(be->data, be_sz);
74 	if (!tmp)
75 		return -1;
76 
77 	be->data   = tmp;
78 	be->max_sz = be_sz;
79 
80 	goto retry;
81 }
82 
83 static void
84 buffer_ext_init(struct buffer_ext *be)
85 {
86 	be->data = NULL;
87 	be->cur_pos = 0;
88 	be->max_sz = 0;
89 }
90 
91 static void
92 buffer_ext_exit(struct buffer_ext *be)
93 {
94 	zfree(&be->data);
95 }
96 
97 static inline size_t
98 buffer_ext_size(struct buffer_ext *be)
99 {
100 	return be->cur_pos;
101 }
102 
103 static inline void *
104 buffer_ext_addr(struct buffer_ext *be)
105 {
106 	return be->data;
107 }
108 
109 struct debug_line_header {
110 	// Not counting this field
111 	uword total_length;
112 	// version number (2 currently)
113 	uhalf version;
114 	// relative offset from next field to
115 	// program statement
116 	uword prolog_length;
117 	ubyte minimum_instruction_length;
118 	ubyte default_is_stmt;
119 	// line_base - see DWARF 2 specs
120 	sbyte line_base;
121 	// line_range - see DWARF 2 specs
122 	ubyte line_range;
123 	// number of opcode + 1
124 	ubyte opcode_base;
125 	/* follow the array of opcode args nr: ubytes [nr_opcode_base] */
126 	/* follow the search directories index, zero terminated string
127 	 * terminated by an empty string.
128 	 */
129 	/* follow an array of { filename, LEB128, LEB128, LEB128 }, first is
130 	 * the directory index entry, 0 means current directory, then mtime
131 	 * and filesize, last entry is followed by en empty string.
132 	 */
133 	/* follow the first program statement */
134 } __packed;
135 
136 /* DWARF 2 spec talk only about one possible compilation unit header while
137  * binutils can handle two flavours of dwarf 2, 32 and 64 bits, this is not
138  * related to the used arch, an ELF 32 can hold more than 4 Go of debug
139  * information. For now we handle only DWARF 2 32 bits comp unit. It'll only
140  * become a problem if we generate more than 4GB of debug information.
141  */
142 struct compilation_unit_header {
143 	uword total_length;
144 	uhalf version;
145 	uword debug_abbrev_offset;
146 	ubyte pointer_size;
147 } __packed;
148 
149 #define DW_LNS_num_opcode (DW_LNS_set_isa + 1)
150 
151 /* field filled at run time are marked with -1 */
152 static struct debug_line_header const default_debug_line_header = {
153 	.total_length = -1,
154 	.version = 2,
155 	.prolog_length = -1,
156 	.minimum_instruction_length = 1,	/* could be better when min instruction size != 1 */
157 	.default_is_stmt = 1,	/* we don't take care about basic block */
158 	.line_base = -5,	/* sensible value for line base ... */
159 	.line_range = -14,     /* ... and line range are guessed statically */
160 	.opcode_base = DW_LNS_num_opcode
161 };
162 
163 static ubyte standard_opcode_length[] =
164 {
165 	0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1
166 };
167 #if 0
168 {
169 	[DW_LNS_advance_pc]   = 1,
170 	[DW_LNS_advance_line] = 1,
171 	[DW_LNS_set_file] =  1,
172 	[DW_LNS_set_column] = 1,
173 	[DW_LNS_fixed_advance_pc] = 1,
174 	[DW_LNS_set_isa] = 1,
175 };
176 #endif
177 
178 /* field filled at run time are marked with -1 */
179 static struct compilation_unit_header default_comp_unit_header = {
180 	.total_length = -1,
181 	.version = 2,
182 	.debug_abbrev_offset = 0,     /* we reuse the same abbrev entries for all comp unit */
183 	.pointer_size = sizeof(void *)
184 };
185 
186 static void emit_uword(struct buffer_ext *be, uword data)
187 {
188 	buffer_ext_add(be, &data, sizeof(uword));
189 }
190 
191 static void emit_string(struct buffer_ext *be, const char *s)
192 {
193 	buffer_ext_add(be, (void *)s, strlen(s) + 1);
194 }
195 
196 static void emit_unsigned_LEB128(struct buffer_ext *be,
197 				 unsigned long data)
198 {
199 	do {
200 		ubyte cur = data & 0x7F;
201 		data >>= 7;
202 		if (data)
203 			cur |= 0x80;
204 		buffer_ext_add(be, &cur, 1);
205 	} while (data);
206 }
207 
208 static void emit_signed_LEB128(struct buffer_ext *be, long data)
209 {
210 	int more = 1;
211 	int negative = data < 0;
212 	int size = sizeof(long) * CHAR_BIT;
213 	while (more) {
214 		ubyte cur = data & 0x7F;
215 		data >>= 7;
216 		if (negative)
217 			data |= - (1 << (size - 7));
218 		if ((data == 0 && !(cur & 0x40)) ||
219 		    (data == -1l && (cur & 0x40)))
220 			more = 0;
221 		else
222 			cur |= 0x80;
223 		buffer_ext_add(be, &cur, 1);
224 	}
225 }
226 
227 static void emit_extended_opcode(struct buffer_ext *be, ubyte opcode,
228 				 void *data, size_t data_len)
229 {
230 	buffer_ext_add(be, (char *)"", 1);
231 
232 	emit_unsigned_LEB128(be, data_len + 1);
233 
234 	buffer_ext_add(be, &opcode, 1);
235 	buffer_ext_add(be, data, data_len);
236 }
237 
238 static void emit_opcode(struct buffer_ext *be, ubyte opcode)
239 {
240 	buffer_ext_add(be, &opcode, 1);
241 }
242 
243 static void emit_opcode_signed(struct buffer_ext  *be,
244 			       ubyte opcode, long data)
245 {
246 	buffer_ext_add(be, &opcode, 1);
247 	emit_signed_LEB128(be, data);
248 }
249 
250 static void emit_opcode_unsigned(struct buffer_ext *be, ubyte opcode,
251 				 unsigned long data)
252 {
253 	buffer_ext_add(be, &opcode, 1);
254 	emit_unsigned_LEB128(be, data);
255 }
256 
257 static void emit_advance_pc(struct buffer_ext *be, unsigned long delta_pc)
258 {
259 	emit_opcode_unsigned(be, DW_LNS_advance_pc, delta_pc);
260 }
261 
262 static void emit_advance_lineno(struct buffer_ext  *be, long delta_lineno)
263 {
264 	emit_opcode_signed(be, DW_LNS_advance_line, delta_lineno);
265 }
266 
267 static void emit_lne_end_of_sequence(struct buffer_ext *be)
268 {
269 	emit_extended_opcode(be, DW_LNE_end_sequence, NULL, 0);
270 }
271 
272 static void emit_set_file(struct buffer_ext *be, unsigned long idx)
273 {
274 	emit_opcode_unsigned(be, DW_LNS_set_file, idx);
275 }
276 
277 static void emit_lne_define_filename(struct buffer_ext *be,
278 				     const char *filename)
279 {
280 	buffer_ext_add(be, (void *)"", 1);
281 
282 	/* LNE field, strlen(filename) + zero termination, 3 bytes for: the dir entry, timestamp, filesize */
283 	emit_unsigned_LEB128(be, strlen(filename) + 5);
284 	emit_opcode(be, DW_LNE_define_file);
285 	emit_string(be, filename);
286 	/* directory index 0=do not know */
287         emit_unsigned_LEB128(be, 0);
288 	/* last modification date on file 0=do not know */
289         emit_unsigned_LEB128(be, 0);
290 	/* filesize 0=do not know */
291         emit_unsigned_LEB128(be, 0);
292 }
293 
294 static void emit_lne_set_address(struct buffer_ext *be,
295 				 void *address)
296 {
297 	emit_extended_opcode(be, DW_LNE_set_address, &address, sizeof(unsigned long));
298 }
299 
300 static ubyte get_special_opcode(struct debug_entry *ent,
301 				unsigned int last_line,
302 				unsigned long last_vma)
303 {
304 	unsigned int temp;
305 	unsigned long delta_addr;
306 
307 	/*
308 	 * delta from line_base
309 	 */
310 	temp = (ent->lineno - last_line) - default_debug_line_header.line_base;
311 
312 	if (temp >= default_debug_line_header.line_range)
313 		return 0;
314 
315 	/*
316 	 * delta of addresses
317 	 */
318 	delta_addr = (ent->addr - last_vma) / default_debug_line_header.minimum_instruction_length;
319 
320 	/* This is not sufficient to ensure opcode will be in [0-256] but
321 	 * sufficient to ensure when summing with the delta lineno we will
322 	 * not overflow the unsigned long opcode */
323 
324 	if (delta_addr <= 256 / default_debug_line_header.line_range) {
325 		unsigned long opcode = temp +
326 			(delta_addr * default_debug_line_header.line_range) +
327 			default_debug_line_header.opcode_base;
328 
329 		return opcode <= 255 ? opcode : 0;
330 	}
331 	return 0;
332 }
333 
334 static void emit_lineno_info(struct buffer_ext *be,
335 			     struct debug_entry *ent, size_t nr_entry,
336 			     unsigned long code_addr)
337 {
338 	size_t i;
339 
340 	/*
341 	 * Machine state at start of a statement program
342 	 * address = 0
343 	 * file    = 1
344 	 * line    = 1
345 	 * column  = 0
346 	 * is_stmt = default_is_stmt as given in the debug_line_header
347 	 * basic block = 0
348 	 * end sequence = 0
349 	 */
350 
351 	/* start state of the state machine we take care of */
352 	unsigned long last_vma = 0;
353 	char const  *cur_filename = NULL;
354 	unsigned long cur_file_idx = 0;
355 	int last_line = 1;
356 
357 	emit_lne_set_address(be, (void *)code_addr);
358 
359 	for (i = 0; i < nr_entry; i++, ent = debug_entry_next(ent)) {
360 		int need_copy = 0;
361 		ubyte special_opcode;
362 
363 		/*
364 		 * check if filename changed, if so add it
365 		 */
366 		if (!cur_filename || strcmp(cur_filename, ent->name)) {
367 			emit_lne_define_filename(be, ent->name);
368 			cur_filename = ent->name;
369 			emit_set_file(be, ++cur_file_idx);
370 			need_copy = 1;
371 		}
372 
373 		special_opcode = get_special_opcode(ent, last_line, last_vma);
374 		if (special_opcode != 0) {
375 			last_line = ent->lineno;
376 			last_vma  = ent->addr;
377 			emit_opcode(be, special_opcode);
378 		} else {
379 			/*
380 			 * lines differ, emit line delta
381 			 */
382 			if (last_line != ent->lineno) {
383 				emit_advance_lineno(be, ent->lineno - last_line);
384 				last_line = ent->lineno;
385 				need_copy = 1;
386 			}
387 			/*
388 			 * addresses differ, emit address delta
389 			 */
390 			if (last_vma != ent->addr) {
391 				emit_advance_pc(be, ent->addr - last_vma);
392 				last_vma = ent->addr;
393 				need_copy = 1;
394 			}
395 			/*
396 			 * add new row to matrix
397 			 */
398 			if (need_copy)
399 				emit_opcode(be, DW_LNS_copy);
400 		}
401 	}
402 }
403 
404 static void add_debug_line(struct buffer_ext *be,
405 	struct debug_entry *ent, size_t nr_entry,
406 	unsigned long code_addr)
407 {
408 	struct debug_line_header * dbg_header;
409 	size_t old_size;
410 
411 	old_size = buffer_ext_size(be);
412 
413 	buffer_ext_add(be, (void *)&default_debug_line_header,
414 		 sizeof(default_debug_line_header));
415 
416 	buffer_ext_add(be, &standard_opcode_length,  sizeof(standard_opcode_length));
417 
418 	// empty directory entry
419 	buffer_ext_add(be, (void *)"", 1);
420 
421 	// empty filename directory
422 	buffer_ext_add(be, (void *)"", 1);
423 
424 	dbg_header = buffer_ext_addr(be) + old_size;
425 	dbg_header->prolog_length = (buffer_ext_size(be) - old_size) -
426 		offsetof(struct debug_line_header, minimum_instruction_length);
427 
428 	emit_lineno_info(be, ent, nr_entry, code_addr);
429 
430 	emit_lne_end_of_sequence(be);
431 
432 	dbg_header = buffer_ext_addr(be) + old_size;
433 	dbg_header->total_length = (buffer_ext_size(be) - old_size) -
434 		offsetof(struct debug_line_header, version);
435 }
436 
437 static void
438 add_debug_abbrev(struct buffer_ext *be)
439 {
440         emit_unsigned_LEB128(be, 1);
441         emit_unsigned_LEB128(be, DW_TAG_compile_unit);
442         emit_unsigned_LEB128(be, DW_CHILDREN_yes);
443         emit_unsigned_LEB128(be, DW_AT_stmt_list);
444         emit_unsigned_LEB128(be, DW_FORM_data4);
445         emit_unsigned_LEB128(be, 0);
446         emit_unsigned_LEB128(be, 0);
447         emit_unsigned_LEB128(be, 0);
448 }
449 
450 static void
451 add_compilation_unit(struct buffer_ext *be,
452 		     size_t offset_debug_line)
453 {
454 	struct compilation_unit_header *comp_unit_header;
455 	size_t old_size = buffer_ext_size(be);
456 
457 	buffer_ext_add(be, &default_comp_unit_header,
458 		       sizeof(default_comp_unit_header));
459 
460 	emit_unsigned_LEB128(be, 1);
461 	emit_uword(be, offset_debug_line);
462 
463 	comp_unit_header = buffer_ext_addr(be) + old_size;
464 	comp_unit_header->total_length = (buffer_ext_size(be) - old_size) -
465 		offsetof(struct compilation_unit_header, version);
466 }
467 
468 static int
469 jit_process_debug_info(uint64_t code_addr,
470 		       void *debug, int nr_debug_entries,
471 		       struct buffer_ext *dl,
472 		       struct buffer_ext *da,
473 		       struct buffer_ext *di)
474 {
475 	struct debug_entry *ent = debug;
476 	int i;
477 
478 	for (i = 0; i < nr_debug_entries; i++) {
479 		ent->addr = ent->addr - code_addr;
480 		ent = debug_entry_next(ent);
481 	}
482 	add_compilation_unit(di, buffer_ext_size(dl));
483 	add_debug_line(dl, debug, nr_debug_entries, GEN_ELF_TEXT_OFFSET);
484 	add_debug_abbrev(da);
485 	if (0) buffer_ext_dump(da, "abbrev");
486 
487 	return 0;
488 }
489 
490 int
491 jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries)
492 {
493 	Elf_Data *d;
494 	Elf_Scn *scn;
495 	Elf_Shdr *shdr;
496 	struct buffer_ext dl, di, da;
497 	int ret = -1;
498 
499 	buffer_ext_init(&dl);
500 	buffer_ext_init(&di);
501 	buffer_ext_init(&da);
502 
503 	if (jit_process_debug_info(code_addr, debug, nr_debug_entries, &dl, &da, &di))
504 		goto out;
505 
506 	/*
507 	 * setup .debug_line section
508 	 */
509 	scn = elf_newscn(e);
510 	if (!scn) {
511 		warnx("cannot create section");
512 		goto out;
513 	}
514 
515 	d = elf_newdata(scn);
516 	if (!d) {
517 		warnx("cannot get new data");
518 		goto out;
519 	}
520 
521 	d->d_align = 1;
522 	d->d_off = 0LL;
523 	d->d_buf = buffer_ext_addr(&dl);
524 	d->d_type = ELF_T_BYTE;
525 	d->d_size = buffer_ext_size(&dl);
526 	d->d_version = EV_CURRENT;
527 
528 	shdr = elf_getshdr(scn);
529 	if (!shdr) {
530 		warnx("cannot get section header");
531 		goto out;
532 	}
533 
534 	shdr->sh_name = 52; /* .debug_line */
535 	shdr->sh_type = SHT_PROGBITS;
536 	shdr->sh_addr = 0; /* must be zero or == sh_offset -> dynamic object */
537 	shdr->sh_flags = 0;
538 	shdr->sh_entsize = 0;
539 
540 	/*
541 	 * setup .debug_info section
542 	 */
543 	scn = elf_newscn(e);
544 	if (!scn) {
545 		warnx("cannot create section");
546 		goto out;
547 	}
548 
549 	d = elf_newdata(scn);
550 	if (!d) {
551 		warnx("cannot get new data");
552 		goto out;
553 	}
554 
555 	d->d_align = 1;
556 	d->d_off = 0LL;
557 	d->d_buf = buffer_ext_addr(&di);
558 	d->d_type = ELF_T_BYTE;
559 	d->d_size = buffer_ext_size(&di);
560 	d->d_version = EV_CURRENT;
561 
562 	shdr = elf_getshdr(scn);
563 	if (!shdr) {
564 		warnx("cannot get section header");
565 		goto out;
566 	}
567 
568 	shdr->sh_name = 64; /* .debug_info */
569 	shdr->sh_type = SHT_PROGBITS;
570 	shdr->sh_addr = 0; /* must be zero or == sh_offset -> dynamic object */
571 	shdr->sh_flags = 0;
572 	shdr->sh_entsize = 0;
573 
574 	/*
575 	 * setup .debug_abbrev section
576 	 */
577 	scn = elf_newscn(e);
578 	if (!scn) {
579 		warnx("cannot create section");
580 		goto out;
581 	}
582 
583 	d = elf_newdata(scn);
584 	if (!d) {
585 		warnx("cannot get new data");
586 		goto out;
587 	}
588 
589 	d->d_align = 1;
590 	d->d_off = 0LL;
591 	d->d_buf = buffer_ext_addr(&da);
592 	d->d_type = ELF_T_BYTE;
593 	d->d_size = buffer_ext_size(&da);
594 	d->d_version = EV_CURRENT;
595 
596 	shdr = elf_getshdr(scn);
597 	if (!shdr) {
598 		warnx("cannot get section header");
599 		goto out;
600 	}
601 
602 	shdr->sh_name = 76; /* .debug_info */
603 	shdr->sh_type = SHT_PROGBITS;
604 	shdr->sh_addr = 0; /* must be zero or == sh_offset -> dynamic object */
605 	shdr->sh_flags = 0;
606 	shdr->sh_entsize = 0;
607 
608 	/*
609 	 * now we update the ELF image with all the sections
610 	 */
611 	if (elf_update(e, ELF_C_WRITE) < 0)
612 		warnx("elf_update debug failed");
613 	else
614 		ret = 0;
615 
616 out:
617 	buffer_ext_exit(&dl);
618 	buffer_ext_exit(&di);
619 	buffer_ext_exit(&da);
620 	return ret;
621 }
622