2 * Copyright (c) 2009,2010 Kai Wang
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include "_libdwarf.h"
29 ELFTC_VCSID("$Id: libdwarf_lineno.c 2972 2013-12-23 06:46:04Z kaiwang27 $");
32 _dwarf_lineno_add_file(Dwarf_LineInfo li, uint8_t **p, const char *compdir,
33 Dwarf_Error *error, Dwarf_Debug dbg)
42 if ((lf = malloc(sizeof(struct _Dwarf_LineFile))) == NULL) {
43 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
44 return (DW_DLE_MEMORY);
47 lf->lf_fullpath = NULL;
48 lf->lf_fname = (char *) src;
49 src += strlen(lf->lf_fname) + 1;
50 lf->lf_dirndx = _dwarf_decode_uleb128(&src);
51 if (lf->lf_dirndx > li->li_inclen) {
53 DWARF_SET_ERROR(dbg, error, DW_DLE_DIR_INDEX_BAD);
54 return (DW_DLE_DIR_INDEX_BAD);
57 /* Make full pathname if need. */
58 if (*lf->lf_fname != '/') {
60 if (lf->lf_dirndx > 0)
61 dirname = li->li_incdirs[lf->lf_dirndx - 1];
62 if (dirname != NULL) {
63 slen = strlen(dirname) + strlen(lf->lf_fname) + 2;
64 if ((lf->lf_fullpath = malloc(slen)) == NULL) {
66 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
67 return (DW_DLE_MEMORY);
69 snprintf(lf->lf_fullpath, slen, "%s/%s", dirname,
74 lf->lf_mtime = _dwarf_decode_uleb128(&src);
75 lf->lf_size = _dwarf_decode_uleb128(&src);
76 STAILQ_INSERT_TAIL(&li->li_lflist, lf, lf_next);
85 _dwarf_lineno_run_program(Dwarf_CU cu, Dwarf_LineInfo li, uint8_t *p,
86 uint8_t *pe, const char *compdir, Dwarf_Error *error)
90 uint64_t address, file, line, column, isa, opsize;
91 int is_stmt, basic_block, end_sequence;
92 int prologue_end, epilogue_begin;
95 #define RESET_REGISTERS \
101 is_stmt = li->li_defstmt; \
105 epilogue_begin = 0; \
110 ln = malloc(sizeof(struct _Dwarf_Line)); \
112 ret = DW_DLE_MEMORY; \
113 DWARF_SET_ERROR(dbg, error, ret); \
117 ln->ln_addr = address; \
119 ln->ln_fileno = file; \
120 ln->ln_lineno = line; \
121 ln->ln_column = column; \
122 ln->ln_bblock = basic_block; \
123 ln->ln_stmt = is_stmt; \
124 ln->ln_endseq = end_sequence; \
125 STAILQ_INSERT_TAIL(&li->li_lnlist, ln, ln_next);\
129 #define LINE(x) (li->li_lbase + (((x) - li->li_opbase) % li->li_lrange))
130 #define ADDRESS(x) ((((x) - li->li_opbase) / li->li_lrange) * li->li_minlen)
135 * Set registers to their default values.
140 * Start line number program.
150 opsize = _dwarf_decode_uleb128(&p);
152 case DW_LNE_end_sequence:
158 case DW_LNE_set_address:
160 address = dbg->decode(&p, cu->cu_pointer_size);
162 case DW_LNE_define_file:
164 ret = _dwarf_lineno_add_file(li, &p, compdir,
166 if (ret != DW_DLE_NONE)
170 /* Unrecognized extened opcodes. */
174 } else if (*p > 0 && *p < li->li_opbase) {
187 case DW_LNS_advance_pc:
188 address += _dwarf_decode_uleb128(&p) *
191 case DW_LNS_advance_line:
192 line += _dwarf_decode_sleb128(&p);
194 case DW_LNS_set_file:
195 file = _dwarf_decode_uleb128(&p);
197 case DW_LNS_set_column:
198 column = _dwarf_decode_uleb128(&p);
200 case DW_LNS_negate_stmt:
203 case DW_LNS_set_basic_block:
206 case DW_LNS_const_add_pc:
207 address += ADDRESS(255);
209 case DW_LNS_fixed_advance_pc:
210 address += dbg->decode(&p, 2);
212 case DW_LNS_set_prologue_end:
215 case DW_LNS_set_epilogue_begin:
219 isa = _dwarf_decode_uleb128(&p);
222 /* Unrecognized extened opcodes. What to do? */
233 address += ADDRESS(*p);
242 return (DW_DLE_NONE);
246 STAILQ_FOREACH_SAFE(ln, &li->li_lnlist, ln_next, tln) {
247 STAILQ_REMOVE(&li->li_lnlist, ln, _Dwarf_Line, ln_next);
253 #undef RESET_REGISTERS
260 _dwarf_lineno_init(Dwarf_Die die, uint64_t offset, Dwarf_Error *error)
267 Dwarf_LineFile lf, tlf;
269 uint64_t length, hdroff, endoff;
271 int dwarf_size, i, ret;
279 if ((ds = _dwarf_find_section(dbg, ".debug_line")) == NULL)
280 return (DW_DLE_NONE);
283 * Try to find out the dir where the CU was compiled. Later we
284 * will use the dir to create full pathnames, if need.
287 at = _dwarf_attr_find(die, DW_AT_comp_dir);
289 switch (at->at_form) {
291 compdir = at->u[1].s;
294 compdir = at->u[0].s;
301 length = dbg->read(ds->ds_data, &offset, 4);
302 if (length == 0xffffffff) {
304 length = dbg->read(ds->ds_data, &offset, 8);
308 if (length > ds->ds_size - offset) {
309 DWARF_SET_ERROR(dbg, error, DW_DLE_DEBUG_LINE_LENGTH_BAD);
310 return (DW_DLE_DEBUG_LINE_LENGTH_BAD);
313 if ((li = calloc(1, sizeof(struct _Dwarf_LineInfo))) == NULL) {
314 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
315 return (DW_DLE_MEMORY);
319 * Read in line number program header.
321 li->li_length = length;
322 endoff = offset + length;
323 li->li_version = dbg->read(ds->ds_data, &offset, 2); /* FIXME: verify version */
324 li->li_hdrlen = dbg->read(ds->ds_data, &offset, dwarf_size);
326 li->li_minlen = dbg->read(ds->ds_data, &offset, 1);
327 li->li_defstmt = dbg->read(ds->ds_data, &offset, 1);
328 li->li_lbase = dbg->read(ds->ds_data, &offset, 1);
329 li->li_lrange = dbg->read(ds->ds_data, &offset, 1);
330 li->li_opbase = dbg->read(ds->ds_data, &offset, 1);
331 STAILQ_INIT(&li->li_lflist);
332 STAILQ_INIT(&li->li_lnlist);
334 if ((int)li->li_hdrlen - 5 < li->li_opbase - 1) {
335 ret = DW_DLE_DEBUG_LINE_LENGTH_BAD;
336 DWARF_SET_ERROR(dbg, error, ret);
340 if ((li->li_oplen = malloc(li->li_opbase)) == NULL) {
342 DWARF_SET_ERROR(dbg, error, ret);
347 * Read in std opcode arg length list. Note that the first
348 * element is not used.
350 for (i = 1; i < li->li_opbase; i++)
351 li->li_oplen[i] = dbg->read(ds->ds_data, &offset, 1);
354 * Check how many strings in the include dir string array.
357 p = ds->ds_data + offset;
363 li->li_inclen = length;
366 if (p - ds->ds_data > (int) ds->ds_size) {
367 ret = DW_DLE_DEBUG_LINE_LENGTH_BAD;
368 DWARF_SET_ERROR(dbg, error, ret);
373 if ((li->li_incdirs = malloc(length * sizeof(char *))) ==
376 DWARF_SET_ERROR(dbg, error, ret);
381 /* Fill in include dir array. */
383 p = ds->ds_data + offset;
385 li->li_incdirs[i++] = (char *) p;
396 ret = _dwarf_lineno_add_file(li, &p, compdir, error, dbg);
397 if (ret != DW_DLE_NONE)
399 if (p - ds->ds_data > (int) ds->ds_size) {
400 ret = DW_DLE_DEBUG_LINE_LENGTH_BAD;
401 DWARF_SET_ERROR(dbg, error, ret);
409 if (p - ds->ds_data - hdroff != li->li_hdrlen) {
410 ret = DW_DLE_DEBUG_LINE_LENGTH_BAD;
411 DWARF_SET_ERROR(dbg, error, ret);
416 * Process line number program.
418 ret = _dwarf_lineno_run_program(cu, li, p, ds->ds_data + endoff, compdir,
420 if (ret != DW_DLE_NONE)
423 cu->cu_lineinfo = li;
425 return (DW_DLE_NONE);
429 STAILQ_FOREACH_SAFE(lf, &li->li_lflist, lf_next, tlf) {
430 STAILQ_REMOVE(&li->li_lflist, lf, _Dwarf_LineFile, lf_next);
432 free(lf->lf_fullpath);
439 free(li->li_incdirs);
446 _dwarf_lineno_cleanup(Dwarf_LineInfo li)
448 Dwarf_LineFile lf, tlf;
453 STAILQ_FOREACH_SAFE(lf, &li->li_lflist, lf_next, tlf) {
454 STAILQ_REMOVE(&li->li_lflist, lf,
455 _Dwarf_LineFile, lf_next);
457 free(lf->lf_fullpath);
460 STAILQ_FOREACH_SAFE(ln, &li->li_lnlist, ln_next, tln) {
461 STAILQ_REMOVE(&li->li_lnlist, ln, _Dwarf_Line,
468 free(li->li_incdirs);
470 free(li->li_lnarray);
472 free(li->li_lfnarray);
477 _dwarf_lineno_gen_program(Dwarf_P_Debug dbg, Dwarf_P_Section ds,
478 Dwarf_Rel_Section drs, Dwarf_Error * error)
482 Dwarf_Unsigned address, file, line, spc;
483 Dwarf_Unsigned addr0, maddr;
484 Dwarf_Signed line0, column;
485 int is_stmt, basic_block, end_sequence;
489 #define RESET_REGISTERS \
495 is_stmt = li->li_defstmt; \
500 li = dbg->dbgp_lineinfo;
501 maddr = (255 - li->li_opbase) / li->li_lrange;
505 STAILQ_FOREACH(ln, &li->li_lnlist, ln_next) {
506 if (ln->ln_symndx > 0) {
508 * Generate DW_LNE_set_address extended op.
510 RCHECK(WRITE_VALUE(0, 1));
511 RCHECK(WRITE_ULEB128(dbg->dbg_pointer_size + 1));
512 RCHECK(WRITE_VALUE(DW_LNE_set_address, 1));
513 RCHECK(_dwarf_reloc_entry_add(dbg, drs, ds,
514 dwarf_drt_data_reloc, dbg->dbg_pointer_size,
515 ds->ds_size, ln->ln_symndx, ln->ln_addr,
517 address = ln->ln_addr;
519 } else if (ln->ln_endseq) {
520 addr0 = (ln->ln_addr - address) / li->li_minlen;
522 RCHECK(WRITE_VALUE(DW_LNS_advance_pc, 1));
523 RCHECK(WRITE_ULEB128(addr0));
527 * Generate DW_LNE_end_sequence.
529 RCHECK(WRITE_VALUE(0, 1));
530 RCHECK(WRITE_ULEB128(1));
531 RCHECK(WRITE_VALUE(DW_LNE_end_sequence, 1));
537 * Generate standard opcodes for file, column, is_stmt or
538 * basic_block changes.
540 if (ln->ln_fileno != file) {
541 RCHECK(WRITE_VALUE(DW_LNS_set_file, 1));
542 RCHECK(WRITE_ULEB128(ln->ln_fileno));
543 file = ln->ln_fileno;
545 if (ln->ln_column != column) {
546 RCHECK(WRITE_VALUE(DW_LNS_set_column, 1));
547 RCHECK(WRITE_ULEB128(ln->ln_column));
548 column = ln->ln_column;
550 if (ln->ln_stmt != is_stmt) {
551 RCHECK(WRITE_VALUE(DW_LNS_negate_stmt, 1));
552 is_stmt = ln->ln_stmt;
554 if (ln->ln_bblock && !basic_block) {
555 RCHECK(WRITE_VALUE(DW_LNS_set_basic_block, 1));
560 * Calculate address and line number change.
562 addr0 = (ln->ln_addr - address) / li->li_minlen;
563 line0 = ln->ln_lineno - line;
565 if (addr0 == 0 && line0 == 0)
569 * Check if line delta is with the range and if the special
570 * opcode can be used.
572 assert(li->li_lbase <= 0);
573 if (line0 >= li->li_lbase &&
574 line0 <= li->li_lbase + li->li_lrange - 1) {
575 spc = (line0 - li->li_lbase) +
576 (li->li_lrange * addr0) + li->li_opbase;
578 RCHECK(WRITE_VALUE(spc, 1));
584 /* Generate DW_LNS_advance_line for line number change. */
586 RCHECK(WRITE_VALUE(DW_LNS_advance_line, 1));
587 RCHECK(WRITE_SLEB128(line0));
591 need_copy = basic_block;
594 /* See if it can be handled by DW_LNS_const_add_pc. */
595 spc = (line0 - li->li_lbase) +
596 (li->li_lrange * (addr0 - maddr)) + li->li_opbase;
597 if (addr0 >= maddr && spc <= 255) {
598 RCHECK(WRITE_VALUE(DW_LNS_const_add_pc, 1));
599 RCHECK(WRITE_VALUE(spc, 1));
601 /* Otherwise we use DW_LNS_advance_pc. */
602 RCHECK(WRITE_VALUE(DW_LNS_advance_pc, 1));
603 RCHECK(WRITE_ULEB128(addr0));
608 RCHECK(WRITE_VALUE(DW_LNS_copy, 1));
613 address = ln->ln_addr;
614 line = ln->ln_lineno;
617 return (DW_DLE_NONE);
622 #undef RESET_REGISTERS
626 _dwarf_get_minlen(Dwarf_P_Debug dbg)
631 switch (dbg->dbgp_isa) {
642 static uint8_t oplen[] = {0, 1, 1, 1, 1, 0, 0, 0, 1};
645 _dwarf_lineno_gen(Dwarf_P_Debug dbg, Dwarf_Error *error)
650 Dwarf_Rel_Section drs;
651 Dwarf_Unsigned offset;
654 assert(dbg != NULL && dbg->dbgp_lineinfo != NULL);
656 li = dbg->dbgp_lineinfo;
657 if (STAILQ_EMPTY(&li->li_lnlist))
658 return (DW_DLE_NONE);
663 li->li_minlen = _dwarf_get_minlen(dbg);
669 /* Create .debug_line section. */
670 if ((ret = _dwarf_section_init(dbg, &ds, ".debug_line", 0, error)) !=
674 /* Create relocation section for .debug_line */
675 if ((ret = _dwarf_reloc_section_init(dbg, &drs, ds, error)) !=
679 /* Length placeholder. (We only use 32-bit DWARF format) */
680 RCHECK(WRITE_VALUE(0, 4));
682 /* Write line number dwarf version. (DWARF2) */
683 RCHECK(WRITE_VALUE(li->li_version, 2));
685 /* Header length placeholder. */
686 offset = ds->ds_size;
687 RCHECK(WRITE_VALUE(li->li_hdrlen, 4));
689 /* Write minimum instruction length. */
690 RCHECK(WRITE_VALUE(li->li_minlen, 1));
693 * Write initial value for is_stmt. XXX Which default value we
696 RCHECK(WRITE_VALUE(li->li_defstmt, 1));
699 * Write line_base and line_range. FIXME These value needs to be
702 RCHECK(WRITE_VALUE(li->li_lbase, 1));
703 RCHECK(WRITE_VALUE(li->li_lrange, 1));
705 /* Write opcode_base. (DWARF2) */
706 RCHECK(WRITE_VALUE(li->li_opbase, 1));
708 /* Write standard op length array. */
709 RCHECK(WRITE_BLOCK(oplen, sizeof(oplen) / sizeof(oplen[0])));
711 /* Write the list of include directories. */
712 for (i = 0; (Dwarf_Unsigned) i < li->li_inclen; i++)
713 RCHECK(WRITE_STRING(li->li_incdirs[i]));
714 RCHECK(WRITE_VALUE(0, 1));
716 /* Write the list of filenames. */
717 STAILQ_FOREACH(lf, &li->li_lflist, lf_next) {
718 RCHECK(WRITE_STRING(lf->lf_fname));
719 RCHECK(WRITE_ULEB128(lf->lf_dirndx));
720 RCHECK(WRITE_ULEB128(lf->lf_mtime));
721 RCHECK(WRITE_ULEB128(lf->lf_size));
723 RCHECK(WRITE_VALUE(0, 1));
725 /* Fill in the header length. */
726 li->li_hdrlen = ds->ds_size - offset - 4;
727 dbg->write(ds->ds_data, &offset, li->li_hdrlen, 4);
729 /* Generate the line number program. */
730 RCHECK(_dwarf_lineno_gen_program(dbg, ds, drs, error));
732 /* Fill in the length of this line info. */
733 li->li_length = ds->ds_size - 4;
735 dbg->write(ds->ds_data, &offset, li->li_length, 4);
737 /* Notify the creation of .debug_line ELF section. */
738 RCHECK(_dwarf_section_callback(dbg, ds, SHT_PROGBITS, 0, 0, 0, error));
740 /* Finalize relocation section for .debug_line. */
741 RCHECK(_dwarf_reloc_section_finalize(dbg, drs, error));
743 return (DW_DLE_NONE);
746 _dwarf_reloc_section_free(dbg, &drs);
749 _dwarf_section_free(dbg, &ds);
755 _dwarf_lineno_pro_cleanup(Dwarf_P_Debug dbg)
758 Dwarf_LineFile lf, tlf;
762 assert(dbg != NULL && dbg->dbg_mode == DW_DLC_WRITE);
763 if (dbg->dbgp_lineinfo == NULL)
766 li = dbg->dbgp_lineinfo;
767 STAILQ_FOREACH_SAFE(lf, &li->li_lflist, lf_next, tlf) {
768 STAILQ_REMOVE(&li->li_lflist, lf, _Dwarf_LineFile,
774 STAILQ_FOREACH_SAFE(ln, &li->li_lnlist, ln_next, tln) {
775 STAILQ_REMOVE(&li->li_lnlist, ln, _Dwarf_Line, ln_next);
778 if (li->li_incdirs) {
779 for (i = 0; (Dwarf_Unsigned) i < li->li_inclen; i++)
780 free(li->li_incdirs[i]);
781 free(li->li_incdirs);
784 dbg->dbgp_lineinfo = NULL;