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 3164 2015-02-19 01:20:12Z 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, opsize;
91 int is_stmt, basic_block, end_sequence;
94 #define RESET_REGISTERS \
100 is_stmt = li->li_defstmt; \
107 ln = malloc(sizeof(struct _Dwarf_Line)); \
109 ret = DW_DLE_MEMORY; \
110 DWARF_SET_ERROR(dbg, error, ret); \
114 ln->ln_addr = address; \
116 ln->ln_fileno = file; \
117 ln->ln_lineno = line; \
118 ln->ln_column = column; \
119 ln->ln_bblock = basic_block; \
120 ln->ln_stmt = is_stmt; \
121 ln->ln_endseq = end_sequence; \
122 STAILQ_INSERT_TAIL(&li->li_lnlist, ln, ln_next);\
126 #define LINE(x) (li->li_lbase + (((x) - li->li_opbase) % li->li_lrange))
127 #define ADDRESS(x) ((((x) - li->li_opbase) / li->li_lrange) * li->li_minlen)
132 * Set registers to their default values.
137 * Start line number program.
147 opsize = _dwarf_decode_uleb128(&p);
149 case DW_LNE_end_sequence:
155 case DW_LNE_set_address:
157 address = dbg->decode(&p, cu->cu_pointer_size);
159 case DW_LNE_define_file:
161 ret = _dwarf_lineno_add_file(li, &p, compdir,
163 if (ret != DW_DLE_NONE)
167 /* Unrecognized extened opcodes. */
171 } else if (*p > 0 && *p < li->li_opbase) {
182 case DW_LNS_advance_pc:
183 address += _dwarf_decode_uleb128(&p) *
186 case DW_LNS_advance_line:
187 line += _dwarf_decode_sleb128(&p);
189 case DW_LNS_set_file:
190 file = _dwarf_decode_uleb128(&p);
192 case DW_LNS_set_column:
193 column = _dwarf_decode_uleb128(&p);
195 case DW_LNS_negate_stmt:
198 case DW_LNS_set_basic_block:
201 case DW_LNS_const_add_pc:
202 address += ADDRESS(255);
204 case DW_LNS_fixed_advance_pc:
205 address += dbg->decode(&p, 2);
207 case DW_LNS_set_prologue_end:
209 case DW_LNS_set_epilogue_begin:
212 (void) _dwarf_decode_uleb128(&p);
215 /* Unrecognized extened opcodes. What to do? */
226 address += ADDRESS(*p);
233 return (DW_DLE_NONE);
237 STAILQ_FOREACH_SAFE(ln, &li->li_lnlist, ln_next, tln) {
238 STAILQ_REMOVE(&li->li_lnlist, ln, _Dwarf_Line, ln_next);
244 #undef RESET_REGISTERS
251 _dwarf_lineno_init(Dwarf_Die die, uint64_t offset, Dwarf_Error *error)
258 Dwarf_LineFile lf, tlf;
260 uint64_t length, hdroff, endoff;
262 int dwarf_size, i, ret;
270 if ((ds = _dwarf_find_section(dbg, ".debug_line")) == NULL)
271 return (DW_DLE_NONE);
274 * Try to find out the dir where the CU was compiled. Later we
275 * will use the dir to create full pathnames, if need.
278 at = _dwarf_attr_find(die, DW_AT_comp_dir);
280 switch (at->at_form) {
282 compdir = at->u[1].s;
285 compdir = at->u[0].s;
292 length = dbg->read(ds->ds_data, &offset, 4);
293 if (length == 0xffffffff) {
295 length = dbg->read(ds->ds_data, &offset, 8);
299 if (length > ds->ds_size - offset) {
300 DWARF_SET_ERROR(dbg, error, DW_DLE_DEBUG_LINE_LENGTH_BAD);
301 return (DW_DLE_DEBUG_LINE_LENGTH_BAD);
304 if ((li = calloc(1, sizeof(struct _Dwarf_LineInfo))) == NULL) {
305 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
306 return (DW_DLE_MEMORY);
310 * Read in line number program header.
312 li->li_length = length;
313 endoff = offset + length;
314 li->li_version = dbg->read(ds->ds_data, &offset, 2); /* FIXME: verify version */
315 li->li_hdrlen = dbg->read(ds->ds_data, &offset, dwarf_size);
317 li->li_minlen = dbg->read(ds->ds_data, &offset, 1);
318 if (li->li_version == 4)
319 li->li_maxop = dbg->read(ds->ds_data, &offset, 1);
320 li->li_defstmt = dbg->read(ds->ds_data, &offset, 1);
321 li->li_lbase = dbg->read(ds->ds_data, &offset, 1);
322 li->li_lrange = dbg->read(ds->ds_data, &offset, 1);
323 li->li_opbase = dbg->read(ds->ds_data, &offset, 1);
324 STAILQ_INIT(&li->li_lflist);
325 STAILQ_INIT(&li->li_lnlist);
327 if ((int)li->li_hdrlen - 5 < li->li_opbase - 1) {
328 ret = DW_DLE_DEBUG_LINE_LENGTH_BAD;
329 DWARF_SET_ERROR(dbg, error, ret);
333 if ((li->li_oplen = malloc(li->li_opbase)) == NULL) {
335 DWARF_SET_ERROR(dbg, error, ret);
340 * Read in std opcode arg length list. Note that the first
341 * element is not used.
343 for (i = 1; i < li->li_opbase; i++)
344 li->li_oplen[i] = dbg->read(ds->ds_data, &offset, 1);
347 * Check how many strings in the include dir string array.
350 p = ds->ds_data + offset;
356 li->li_inclen = length;
359 if (p - ds->ds_data > (int) ds->ds_size) {
360 ret = DW_DLE_DEBUG_LINE_LENGTH_BAD;
361 DWARF_SET_ERROR(dbg, error, ret);
366 if ((li->li_incdirs = malloc(length * sizeof(char *))) ==
369 DWARF_SET_ERROR(dbg, error, ret);
374 /* Fill in include dir array. */
376 p = ds->ds_data + offset;
378 li->li_incdirs[i++] = (char *) p;
389 ret = _dwarf_lineno_add_file(li, &p, compdir, error, dbg);
390 if (ret != DW_DLE_NONE)
392 if (p - ds->ds_data > (int) ds->ds_size) {
393 ret = DW_DLE_DEBUG_LINE_LENGTH_BAD;
394 DWARF_SET_ERROR(dbg, error, ret);
402 if (p - ds->ds_data - hdroff != li->li_hdrlen) {
403 ret = DW_DLE_DEBUG_LINE_LENGTH_BAD;
404 DWARF_SET_ERROR(dbg, error, ret);
409 * Process line number program.
411 ret = _dwarf_lineno_run_program(cu, li, p, ds->ds_data + endoff, compdir,
413 if (ret != DW_DLE_NONE)
416 cu->cu_lineinfo = li;
418 return (DW_DLE_NONE);
422 STAILQ_FOREACH_SAFE(lf, &li->li_lflist, lf_next, tlf) {
423 STAILQ_REMOVE(&li->li_lflist, lf, _Dwarf_LineFile, lf_next);
425 free(lf->lf_fullpath);
432 free(li->li_incdirs);
439 _dwarf_lineno_cleanup(Dwarf_LineInfo li)
441 Dwarf_LineFile lf, tlf;
446 STAILQ_FOREACH_SAFE(lf, &li->li_lflist, lf_next, tlf) {
447 STAILQ_REMOVE(&li->li_lflist, lf,
448 _Dwarf_LineFile, lf_next);
450 free(lf->lf_fullpath);
453 STAILQ_FOREACH_SAFE(ln, &li->li_lnlist, ln_next, tln) {
454 STAILQ_REMOVE(&li->li_lnlist, ln, _Dwarf_Line,
461 free(li->li_incdirs);
463 free(li->li_lnarray);
465 free(li->li_lfnarray);
470 _dwarf_lineno_gen_program(Dwarf_P_Debug dbg, Dwarf_P_Section ds,
471 Dwarf_Rel_Section drs, Dwarf_Error * error)
475 Dwarf_Unsigned address, file, line, spc;
476 Dwarf_Unsigned addr0, maddr;
477 Dwarf_Signed line0, column;
478 int is_stmt, basic_block;
482 #define RESET_REGISTERS \
488 is_stmt = li->li_defstmt; \
492 li = dbg->dbgp_lineinfo;
493 maddr = (255 - li->li_opbase) / li->li_lrange;
497 STAILQ_FOREACH(ln, &li->li_lnlist, ln_next) {
498 if (ln->ln_symndx > 0) {
500 * Generate DW_LNE_set_address extended op.
502 RCHECK(WRITE_VALUE(0, 1));
503 RCHECK(WRITE_ULEB128(dbg->dbg_pointer_size + 1));
504 RCHECK(WRITE_VALUE(DW_LNE_set_address, 1));
505 RCHECK(_dwarf_reloc_entry_add(dbg, drs, ds,
506 dwarf_drt_data_reloc, dbg->dbg_pointer_size,
507 ds->ds_size, ln->ln_symndx, ln->ln_addr,
509 address = ln->ln_addr;
511 } else if (ln->ln_endseq) {
512 addr0 = (ln->ln_addr - address) / li->li_minlen;
514 RCHECK(WRITE_VALUE(DW_LNS_advance_pc, 1));
515 RCHECK(WRITE_ULEB128(addr0));
519 * Generate DW_LNE_end_sequence.
521 RCHECK(WRITE_VALUE(0, 1));
522 RCHECK(WRITE_ULEB128(1));
523 RCHECK(WRITE_VALUE(DW_LNE_end_sequence, 1));
529 * Generate standard opcodes for file, column, is_stmt or
530 * basic_block changes.
532 if (ln->ln_fileno != file) {
533 RCHECK(WRITE_VALUE(DW_LNS_set_file, 1));
534 RCHECK(WRITE_ULEB128(ln->ln_fileno));
535 file = ln->ln_fileno;
537 if (ln->ln_column != column) {
538 RCHECK(WRITE_VALUE(DW_LNS_set_column, 1));
539 RCHECK(WRITE_ULEB128(ln->ln_column));
540 column = ln->ln_column;
542 if (ln->ln_stmt != is_stmt) {
543 RCHECK(WRITE_VALUE(DW_LNS_negate_stmt, 1));
544 is_stmt = ln->ln_stmt;
546 if (ln->ln_bblock && !basic_block) {
547 RCHECK(WRITE_VALUE(DW_LNS_set_basic_block, 1));
552 * Calculate address and line number change.
554 addr0 = (ln->ln_addr - address) / li->li_minlen;
555 line0 = ln->ln_lineno - line;
557 if (addr0 == 0 && line0 == 0)
561 * Check if line delta is with the range and if the special
562 * opcode can be used.
564 assert(li->li_lbase <= 0);
565 if (line0 >= li->li_lbase &&
566 line0 <= li->li_lbase + li->li_lrange - 1) {
567 spc = (line0 - li->li_lbase) +
568 (li->li_lrange * addr0) + li->li_opbase;
570 RCHECK(WRITE_VALUE(spc, 1));
576 /* Generate DW_LNS_advance_line for line number change. */
578 RCHECK(WRITE_VALUE(DW_LNS_advance_line, 1));
579 RCHECK(WRITE_SLEB128(line0));
583 need_copy = basic_block;
586 /* See if it can be handled by DW_LNS_const_add_pc. */
587 spc = (line0 - li->li_lbase) +
588 (li->li_lrange * (addr0 - maddr)) + li->li_opbase;
589 if (addr0 >= maddr && spc <= 255) {
590 RCHECK(WRITE_VALUE(DW_LNS_const_add_pc, 1));
591 RCHECK(WRITE_VALUE(spc, 1));
593 /* Otherwise we use DW_LNS_advance_pc. */
594 RCHECK(WRITE_VALUE(DW_LNS_advance_pc, 1));
595 RCHECK(WRITE_ULEB128(addr0));
600 RCHECK(WRITE_VALUE(DW_LNS_copy, 1));
605 address = ln->ln_addr;
606 line = ln->ln_lineno;
609 return (DW_DLE_NONE);
614 #undef RESET_REGISTERS
618 _dwarf_get_minlen(Dwarf_P_Debug dbg)
623 switch (dbg->dbgp_isa) {
634 static uint8_t oplen[] = {0, 1, 1, 1, 1, 0, 0, 0, 1};
637 _dwarf_lineno_gen(Dwarf_P_Debug dbg, Dwarf_Error *error)
642 Dwarf_Rel_Section drs;
643 Dwarf_Unsigned offset;
646 assert(dbg != NULL && dbg->dbgp_lineinfo != NULL);
648 li = dbg->dbgp_lineinfo;
649 if (STAILQ_EMPTY(&li->li_lnlist))
650 return (DW_DLE_NONE);
655 li->li_minlen = _dwarf_get_minlen(dbg);
661 /* Create .debug_line section. */
662 if ((ret = _dwarf_section_init(dbg, &ds, ".debug_line", 0, error)) !=
666 /* Create relocation section for .debug_line */
667 if ((ret = _dwarf_reloc_section_init(dbg, &drs, ds, error)) !=
671 /* Length placeholder. (We only use 32-bit DWARF format) */
672 RCHECK(WRITE_VALUE(0, 4));
674 /* Write line number dwarf version. (DWARF2) */
675 RCHECK(WRITE_VALUE(li->li_version, 2));
677 /* Header length placeholder. */
678 offset = ds->ds_size;
679 RCHECK(WRITE_VALUE(li->li_hdrlen, 4));
681 /* Write minimum instruction length. */
682 RCHECK(WRITE_VALUE(li->li_minlen, 1));
685 * Write initial value for is_stmt. XXX Which default value we
688 RCHECK(WRITE_VALUE(li->li_defstmt, 1));
691 * Write line_base and line_range. FIXME These value needs to be
694 RCHECK(WRITE_VALUE(li->li_lbase, 1));
695 RCHECK(WRITE_VALUE(li->li_lrange, 1));
697 /* Write opcode_base. (DWARF2) */
698 RCHECK(WRITE_VALUE(li->li_opbase, 1));
700 /* Write standard op length array. */
701 RCHECK(WRITE_BLOCK(oplen, sizeof(oplen) / sizeof(oplen[0])));
703 /* Write the list of include directories. */
704 for (i = 0; (Dwarf_Unsigned) i < li->li_inclen; i++)
705 RCHECK(WRITE_STRING(li->li_incdirs[i]));
706 RCHECK(WRITE_VALUE(0, 1));
708 /* Write the list of filenames. */
709 STAILQ_FOREACH(lf, &li->li_lflist, lf_next) {
710 RCHECK(WRITE_STRING(lf->lf_fname));
711 RCHECK(WRITE_ULEB128(lf->lf_dirndx));
712 RCHECK(WRITE_ULEB128(lf->lf_mtime));
713 RCHECK(WRITE_ULEB128(lf->lf_size));
715 RCHECK(WRITE_VALUE(0, 1));
717 /* Fill in the header length. */
718 li->li_hdrlen = ds->ds_size - offset - 4;
719 dbg->write(ds->ds_data, &offset, li->li_hdrlen, 4);
721 /* Generate the line number program. */
722 RCHECK(_dwarf_lineno_gen_program(dbg, ds, drs, error));
724 /* Fill in the length of this line info. */
725 li->li_length = ds->ds_size - 4;
727 dbg->write(ds->ds_data, &offset, li->li_length, 4);
729 /* Notify the creation of .debug_line ELF section. */
730 RCHECK(_dwarf_section_callback(dbg, ds, SHT_PROGBITS, 0, 0, 0, error));
732 /* Finalize relocation section for .debug_line. */
733 RCHECK(_dwarf_reloc_section_finalize(dbg, drs, error));
735 return (DW_DLE_NONE);
738 _dwarf_reloc_section_free(dbg, &drs);
741 _dwarf_section_free(dbg, &ds);
747 _dwarf_lineno_pro_cleanup(Dwarf_P_Debug dbg)
750 Dwarf_LineFile lf, tlf;
754 assert(dbg != NULL && dbg->dbg_mode == DW_DLC_WRITE);
755 if (dbg->dbgp_lineinfo == NULL)
758 li = dbg->dbgp_lineinfo;
759 STAILQ_FOREACH_SAFE(lf, &li->li_lflist, lf_next, tlf) {
760 STAILQ_REMOVE(&li->li_lflist, lf, _Dwarf_LineFile,
766 STAILQ_FOREACH_SAFE(ln, &li->li_lnlist, ln_next, tln) {
767 STAILQ_REMOVE(&li->li_lnlist, ln, _Dwarf_Line, ln_next);
770 if (li->li_incdirs) {
771 for (i = 0; (Dwarf_Unsigned) i < li->li_inclen; i++)
772 free(li->li_incdirs[i]);
773 free(li->li_incdirs);
776 dbg->dbgp_lineinfo = NULL;