xref: /freebsd/contrib/elftoolchain/libdwarf/libdwarf_lineno.c (revision 545ddfbe7d4fe8adfb862903b24eac1d5896c1ef)
1 /*-
2  * Copyright (c) 2009,2010 Kai Wang
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include "_libdwarf.h"
28 
29 ELFTC_VCSID("$Id: libdwarf_lineno.c 3100 2014-10-25 20:34:29Z jkoshy $");
30 
31 static int
32 _dwarf_lineno_add_file(Dwarf_LineInfo li, uint8_t **p, const char *compdir,
33     Dwarf_Error *error, Dwarf_Debug dbg)
34 {
35 	Dwarf_LineFile lf;
36 	const char *dirname;
37 	uint8_t *src;
38 	int slen;
39 
40 	src = *p;
41 
42 	if ((lf = malloc(sizeof(struct _Dwarf_LineFile))) == NULL) {
43 		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
44 		return (DW_DLE_MEMORY);
45 	}
46 
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) {
52 		free(lf);
53 		DWARF_SET_ERROR(dbg, error, DW_DLE_DIR_INDEX_BAD);
54 		return (DW_DLE_DIR_INDEX_BAD);
55 	}
56 
57 	/* Make full pathname if need. */
58 	if (*lf->lf_fname != '/') {
59 		dirname = compdir;
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) {
65 				free(lf);
66 				DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
67 				return (DW_DLE_MEMORY);
68 			}
69 			snprintf(lf->lf_fullpath, slen, "%s/%s", dirname,
70 			    lf->lf_fname);
71 		}
72 	}
73 
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);
77 	li->li_lflen++;
78 
79 	*p = src;
80 
81 	return (DW_DLE_NONE);
82 }
83 
84 static int
85 _dwarf_lineno_run_program(Dwarf_CU cu, Dwarf_LineInfo li, uint8_t *p,
86     uint8_t *pe, const char *compdir, Dwarf_Error *error)
87 {
88 	Dwarf_Debug dbg;
89 	Dwarf_Line ln, tln;
90 	uint64_t address, file, line, column, opsize;
91 	int is_stmt, basic_block, end_sequence;
92 	int ret;
93 
94 #define	RESET_REGISTERS						\
95 	do {							\
96 		address	       = 0;				\
97 		file	       = 1;				\
98 		line	       = 1;				\
99 		column	       = 0;				\
100 		is_stmt	       = li->li_defstmt;		\
101 		basic_block    = 0;				\
102 		end_sequence   = 0;				\
103 	} while(0)
104 
105 #define	APPEND_ROW						\
106 	do {							\
107 		ln = malloc(sizeof(struct _Dwarf_Line));	\
108 		if (ln == NULL) {				\
109 			ret = DW_DLE_MEMORY;			\
110 			DWARF_SET_ERROR(dbg, error, ret);	\
111 			goto prog_fail;				\
112 		}						\
113 		ln->ln_li     = li;				\
114 		ln->ln_addr   = address;			\
115 		ln->ln_symndx = 0;				\
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);\
123 		li->li_lnlen++;					\
124 	} while(0)
125 
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)
128 
129 	dbg = cu->cu_dbg;
130 
131 	/*
132 	 * Set registers to their default values.
133 	 */
134 	RESET_REGISTERS;
135 
136 	/*
137 	 * Start line number program.
138 	 */
139 	while (p < pe) {
140 		if (*p == 0) {
141 
142 			/*
143 			 * Extended Opcodes.
144 			 */
145 
146 			p++;
147 			opsize = _dwarf_decode_uleb128(&p);
148 			switch (*p) {
149 			case DW_LNE_end_sequence:
150 				p++;
151 				end_sequence = 1;
152 				APPEND_ROW;
153 				RESET_REGISTERS;
154 				break;
155 			case DW_LNE_set_address:
156 				p++;
157 				address = dbg->decode(&p, cu->cu_pointer_size);
158 				break;
159 			case DW_LNE_define_file:
160 				p++;
161 				ret = _dwarf_lineno_add_file(li, &p, compdir,
162 				    error, dbg);
163 				if (ret != DW_DLE_NONE)
164 					goto prog_fail;
165 				break;
166 			default:
167 				/* Unrecognized extened opcodes. */
168 				p += opsize;
169 			}
170 
171 		} else if (*p > 0 && *p < li->li_opbase) {
172 
173 			/*
174 			 * Standard Opcodes.
175 			 */
176 
177 			switch (*p++) {
178 			case DW_LNS_copy:
179 				APPEND_ROW;
180 				basic_block = 0;
181 				break;
182 			case DW_LNS_advance_pc:
183 				address += _dwarf_decode_uleb128(&p) *
184 				    li->li_minlen;
185 				break;
186 			case DW_LNS_advance_line:
187 				line += _dwarf_decode_sleb128(&p);
188 				break;
189 			case DW_LNS_set_file:
190 				file = _dwarf_decode_uleb128(&p);
191 				break;
192 			case DW_LNS_set_column:
193 				column = _dwarf_decode_uleb128(&p);
194 				break;
195 			case DW_LNS_negate_stmt:
196 				is_stmt = !is_stmt;
197 				break;
198 			case DW_LNS_set_basic_block:
199 				basic_block = 1;
200 				break;
201 			case DW_LNS_const_add_pc:
202 				address += ADDRESS(255);
203 				break;
204 			case DW_LNS_fixed_advance_pc:
205 				address += dbg->decode(&p, 2);
206 				break;
207 			case DW_LNS_set_prologue_end:
208 				break;
209 			case DW_LNS_set_epilogue_begin:
210 				break;
211 			case DW_LNS_set_isa:
212 				(void) _dwarf_decode_uleb128(&p);
213 				break;
214 			default:
215 				/* Unrecognized extened opcodes. What to do? */
216 				break;
217 			}
218 
219 		} else {
220 
221 			/*
222 			 * Special Opcodes.
223 			 */
224 
225 			line += LINE(*p);
226 			address += ADDRESS(*p);
227 			APPEND_ROW;
228 			basic_block = 0;
229 			p++;
230 		}
231 	}
232 
233 	return (DW_DLE_NONE);
234 
235 prog_fail:
236 
237 	STAILQ_FOREACH_SAFE(ln, &li->li_lnlist, ln_next, tln) {
238 		STAILQ_REMOVE(&li->li_lnlist, ln, _Dwarf_Line, ln_next);
239 		free(ln);
240 	}
241 
242 	return (ret);
243 
244 #undef	RESET_REGISTERS
245 #undef	APPEND_ROW
246 #undef	LINE
247 #undef	ADDRESS
248 }
249 
250 int
251 _dwarf_lineno_init(Dwarf_Die die, uint64_t offset, Dwarf_Error *error)
252 {
253 	Dwarf_Debug dbg;
254 	Dwarf_Section *ds;
255 	Dwarf_CU cu;
256 	Dwarf_Attribute at;
257 	Dwarf_LineInfo li;
258 	Dwarf_LineFile lf, tlf;
259 	const char *compdir;
260 	uint64_t length, hdroff, endoff;
261 	uint8_t *p;
262 	int dwarf_size, i, ret;
263 
264 	cu = die->die_cu;
265 	assert(cu != NULL);
266 
267 	dbg = cu->cu_dbg;
268 	assert(dbg != NULL);
269 
270 	if ((ds = _dwarf_find_section(dbg, ".debug_line")) == NULL)
271 		return (DW_DLE_NONE);
272 
273 	/*
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.
276 	 */
277 	compdir = NULL;
278 	at = _dwarf_attr_find(die, DW_AT_comp_dir);
279 	if (at != NULL) {
280 		switch (at->at_form) {
281 		case DW_FORM_strp:
282 			compdir = at->u[1].s;
283 			break;
284 		case DW_FORM_string:
285 			compdir = at->u[0].s;
286 			break;
287 		default:
288 			break;
289 		}
290 	}
291 
292 	length = dbg->read(ds->ds_data, &offset, 4);
293 	if (length == 0xffffffff) {
294 		dwarf_size = 8;
295 		length = dbg->read(ds->ds_data, &offset, 8);
296 	} else
297 		dwarf_size = 4;
298 
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);
302 	}
303 
304 	if ((li = calloc(1, sizeof(struct _Dwarf_LineInfo))) == NULL) {
305 		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
306 		return (DW_DLE_MEMORY);
307 	}
308 
309 	/*
310 	 * Read in line number program header.
311 	 */
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);
316 	hdroff = offset;
317 	li->li_minlen = dbg->read(ds->ds_data, &offset, 1);
318 	li->li_defstmt = dbg->read(ds->ds_data, &offset, 1);
319 	li->li_lbase = dbg->read(ds->ds_data, &offset, 1);
320 	li->li_lrange = dbg->read(ds->ds_data, &offset, 1);
321 	li->li_opbase = dbg->read(ds->ds_data, &offset, 1);
322 	STAILQ_INIT(&li->li_lflist);
323 	STAILQ_INIT(&li->li_lnlist);
324 
325 	if ((int)li->li_hdrlen - 5 < li->li_opbase - 1) {
326 		ret = DW_DLE_DEBUG_LINE_LENGTH_BAD;
327 		DWARF_SET_ERROR(dbg, error, ret);
328 		goto fail_cleanup;
329 	}
330 
331 	if ((li->li_oplen = malloc(li->li_opbase)) == NULL) {
332 		ret = DW_DLE_MEMORY;
333 		DWARF_SET_ERROR(dbg, error, ret);
334 		goto fail_cleanup;
335 	}
336 
337 	/*
338 	 * Read in std opcode arg length list. Note that the first
339 	 * element is not used.
340 	 */
341 	for (i = 1; i < li->li_opbase; i++)
342 		li->li_oplen[i] = dbg->read(ds->ds_data, &offset, 1);
343 
344 	/*
345 	 * Check how many strings in the include dir string array.
346 	 */
347 	length = 0;
348 	p = ds->ds_data + offset;
349 	while (*p != '\0') {
350 		while (*p++ != '\0')
351 			;
352 		length++;
353 	}
354 	li->li_inclen = length;
355 
356 	/* Sanity check. */
357 	if (p - ds->ds_data > (int) ds->ds_size) {
358 		ret = DW_DLE_DEBUG_LINE_LENGTH_BAD;
359 		DWARF_SET_ERROR(dbg, error, ret);
360 		goto fail_cleanup;
361 	}
362 
363 	if (length != 0) {
364 		if ((li->li_incdirs = malloc(length * sizeof(char *))) ==
365 		    NULL) {
366 			ret = DW_DLE_MEMORY;
367 			DWARF_SET_ERROR(dbg, error, ret);
368 			goto fail_cleanup;
369 		}
370 	}
371 
372 	/* Fill in include dir array. */
373 	i = 0;
374 	p = ds->ds_data + offset;
375 	while (*p != '\0') {
376 		li->li_incdirs[i++] = (char *) p;
377 		while (*p++ != '\0')
378 			;
379 	}
380 
381 	p++;
382 
383 	/*
384 	 * Process file list.
385 	 */
386 	while (*p != '\0') {
387 		ret = _dwarf_lineno_add_file(li, &p, compdir, error, dbg);
388 		if (ret != DW_DLE_NONE)
389 			goto fail_cleanup;
390 		if (p - ds->ds_data > (int) ds->ds_size) {
391 			ret = DW_DLE_DEBUG_LINE_LENGTH_BAD;
392 			DWARF_SET_ERROR(dbg, error, ret);
393 			goto fail_cleanup;
394 		}
395 	}
396 
397 	p++;
398 
399 	/* Sanity check. */
400 	if (p - ds->ds_data - hdroff != li->li_hdrlen) {
401 		ret = DW_DLE_DEBUG_LINE_LENGTH_BAD;
402 		DWARF_SET_ERROR(dbg, error, ret);
403 		goto fail_cleanup;
404 	}
405 
406 	/*
407 	 * Process line number program.
408 	 */
409 	ret = _dwarf_lineno_run_program(cu, li, p, ds->ds_data + endoff, compdir,
410 	    error);
411 	if (ret != DW_DLE_NONE)
412 		goto fail_cleanup;
413 
414 	cu->cu_lineinfo = li;
415 
416 	return (DW_DLE_NONE);
417 
418 fail_cleanup:
419 
420 	STAILQ_FOREACH_SAFE(lf, &li->li_lflist, lf_next, tlf) {
421 		STAILQ_REMOVE(&li->li_lflist, lf, _Dwarf_LineFile, lf_next);
422 		if (lf->lf_fullpath)
423 			free(lf->lf_fullpath);
424 		free(lf);
425 	}
426 
427 	if (li->li_oplen)
428 		free(li->li_oplen);
429 	if (li->li_incdirs)
430 		free(li->li_incdirs);
431 	free(li);
432 
433 	return (ret);
434 }
435 
436 void
437 _dwarf_lineno_cleanup(Dwarf_LineInfo li)
438 {
439 	Dwarf_LineFile lf, tlf;
440 	Dwarf_Line ln, tln;
441 
442 	if (li == NULL)
443 		return;
444 	STAILQ_FOREACH_SAFE(lf, &li->li_lflist, lf_next, tlf) {
445 		STAILQ_REMOVE(&li->li_lflist, lf,
446 		    _Dwarf_LineFile, lf_next);
447 		if (lf->lf_fullpath)
448 			free(lf->lf_fullpath);
449 		free(lf);
450 	}
451 	STAILQ_FOREACH_SAFE(ln, &li->li_lnlist, ln_next, tln) {
452 		STAILQ_REMOVE(&li->li_lnlist, ln, _Dwarf_Line,
453 		    ln_next);
454 		free(ln);
455 	}
456 	if (li->li_oplen)
457 		free(li->li_oplen);
458 	if (li->li_incdirs)
459 		free(li->li_incdirs);
460 	if (li->li_lnarray)
461 		free(li->li_lnarray);
462 	if (li->li_lfnarray)
463 		free(li->li_lfnarray);
464 	free(li);
465 }
466 
467 static int
468 _dwarf_lineno_gen_program(Dwarf_P_Debug dbg, Dwarf_P_Section ds,
469     Dwarf_Rel_Section drs, Dwarf_Error * error)
470 {
471 	Dwarf_LineInfo li;
472 	Dwarf_Line ln;
473 	Dwarf_Unsigned address, file, line, spc;
474 	Dwarf_Unsigned addr0, maddr;
475 	Dwarf_Signed line0, column;
476 	int is_stmt, basic_block;
477 	int need_copy;
478 	int ret;
479 
480 #define	RESET_REGISTERS						\
481 	do {							\
482 		address	       = 0;				\
483 		file	       = 1;				\
484 		line	       = 1;				\
485 		column	       = 0;				\
486 		is_stmt	       = li->li_defstmt;		\
487 		basic_block    = 0;				\
488 	} while(0)
489 
490 	li = dbg->dbgp_lineinfo;
491 	maddr = (255 - li->li_opbase) / li->li_lrange;
492 
493 	RESET_REGISTERS;
494 
495 	STAILQ_FOREACH(ln, &li->li_lnlist, ln_next) {
496 		if (ln->ln_symndx > 0) {
497 			/*
498 			 * Generate DW_LNE_set_address extended op.
499 			 */
500 			RCHECK(WRITE_VALUE(0, 1));
501 			RCHECK(WRITE_ULEB128(dbg->dbg_pointer_size + 1));
502 			RCHECK(WRITE_VALUE(DW_LNE_set_address, 1));
503 			RCHECK(_dwarf_reloc_entry_add(dbg, drs, ds,
504 			    dwarf_drt_data_reloc, dbg->dbg_pointer_size,
505 			    ds->ds_size, ln->ln_symndx, ln->ln_addr,
506 			    NULL, error));
507 			address = ln->ln_addr;
508 			continue;
509 		} else if (ln->ln_endseq) {
510 			addr0 = (ln->ln_addr - address) / li->li_minlen;
511 			if (addr0 != 0) {
512 				RCHECK(WRITE_VALUE(DW_LNS_advance_pc, 1));
513 				RCHECK(WRITE_ULEB128(addr0));
514 			}
515 
516 			/*
517 			 * Generate DW_LNE_end_sequence.
518 			 */
519 			RCHECK(WRITE_VALUE(0, 1));
520 			RCHECK(WRITE_ULEB128(1));
521 			RCHECK(WRITE_VALUE(DW_LNE_end_sequence, 1));
522 			RESET_REGISTERS;
523 			continue;
524 		}
525 
526 		/*
527 		 * Generate standard opcodes for file, column, is_stmt or
528 		 * basic_block changes.
529 		 */
530 		if (ln->ln_fileno != file) {
531 			RCHECK(WRITE_VALUE(DW_LNS_set_file, 1));
532 			RCHECK(WRITE_ULEB128(ln->ln_fileno));
533 			file = ln->ln_fileno;
534 		}
535 		if (ln->ln_column != column) {
536 			RCHECK(WRITE_VALUE(DW_LNS_set_column, 1));
537 			RCHECK(WRITE_ULEB128(ln->ln_column));
538 			column = ln->ln_column;
539 		}
540 		if (ln->ln_stmt != is_stmt) {
541 			RCHECK(WRITE_VALUE(DW_LNS_negate_stmt, 1));
542 			is_stmt = ln->ln_stmt;
543 		}
544 		if (ln->ln_bblock && !basic_block) {
545 			RCHECK(WRITE_VALUE(DW_LNS_set_basic_block, 1));
546 			basic_block = 1;
547 		}
548 
549 		/*
550 		 * Calculate address and line number change.
551 		 */
552 		addr0 = (ln->ln_addr - address) / li->li_minlen;
553 		line0 = ln->ln_lineno - line;
554 
555 		if (addr0 == 0 && line0 == 0)
556 			continue;
557 
558 		/*
559 		 * Check if line delta is with the range and if the special
560 		 * opcode can be used.
561 		 */
562 		assert(li->li_lbase <= 0);
563 		if (line0 >= li->li_lbase &&
564 		    line0 <= li->li_lbase + li->li_lrange - 1) {
565 			spc = (line0 - li->li_lbase) +
566 			    (li->li_lrange * addr0) + li->li_opbase;
567 			if (spc <= 255) {
568 				RCHECK(WRITE_VALUE(spc, 1));
569 				basic_block = 0;
570 				goto next_line;
571 			}
572 		}
573 
574 		/* Generate DW_LNS_advance_line for line number change. */
575 		if (line0 != 0) {
576 			RCHECK(WRITE_VALUE(DW_LNS_advance_line, 1));
577 			RCHECK(WRITE_SLEB128(line0));
578 			line0 = 0;
579 			need_copy = 1;
580 		} else
581 			need_copy = basic_block;
582 
583 		if (addr0 != 0) {
584 			/* See if it can be handled by DW_LNS_const_add_pc. */
585 			spc = (line0 - li->li_lbase) +
586 			    (li->li_lrange * (addr0 - maddr)) + li->li_opbase;
587 			if (addr0 >= maddr && spc <= 255) {
588 				RCHECK(WRITE_VALUE(DW_LNS_const_add_pc, 1));
589 				RCHECK(WRITE_VALUE(spc, 1));
590 			} else {
591 				/* Otherwise we use DW_LNS_advance_pc. */
592 				RCHECK(WRITE_VALUE(DW_LNS_advance_pc, 1));
593 				RCHECK(WRITE_ULEB128(addr0));
594 			}
595 		}
596 
597 		if (need_copy) {
598 			RCHECK(WRITE_VALUE(DW_LNS_copy, 1));
599 			basic_block = 0;
600 		}
601 
602 	next_line:
603 		address = ln->ln_addr;
604 		line = ln->ln_lineno;
605 	}
606 
607 	return (DW_DLE_NONE);
608 
609 gen_fail:
610 	return (ret);
611 
612 #undef	RESET_REGISTERS
613 }
614 
615 static uint8_t
616 _dwarf_get_minlen(Dwarf_P_Debug dbg)
617 {
618 
619 	assert(dbg != NULL);
620 
621 	switch (dbg->dbgp_isa) {
622 	case DW_ISA_ARM:
623 		return (2);
624 	case DW_ISA_X86:
625 	case DW_ISA_X86_64:
626 		return (1);
627 	default:
628 		return (4);
629 	}
630 }
631 
632 static uint8_t oplen[] = {0, 1, 1, 1, 1, 0, 0, 0, 1};
633 
634 int
635 _dwarf_lineno_gen(Dwarf_P_Debug dbg, Dwarf_Error *error)
636 {
637 	Dwarf_LineInfo li;
638 	Dwarf_LineFile lf;
639 	Dwarf_P_Section ds;
640 	Dwarf_Rel_Section drs;
641 	Dwarf_Unsigned offset;
642 	int i, ret;
643 
644 	assert(dbg != NULL && dbg->dbgp_lineinfo != NULL);
645 
646 	li = dbg->dbgp_lineinfo;
647 	if (STAILQ_EMPTY(&li->li_lnlist))
648 		return (DW_DLE_NONE);
649 
650 	li->li_length = 0;
651 	li->li_version = 2;
652 	li->li_hdrlen = 0;
653 	li->li_minlen = _dwarf_get_minlen(dbg);
654 	li->li_defstmt = 1;
655 	li->li_lbase = -5;
656 	li->li_lrange = 14;
657 	li->li_opbase = 10;
658 
659 	/* Create .debug_line section. */
660 	if ((ret = _dwarf_section_init(dbg, &ds, ".debug_line", 0, error)) !=
661 	    DW_DLE_NONE)
662 		return (ret);
663 
664 	/* Create relocation section for .debug_line */
665 	if ((ret = _dwarf_reloc_section_init(dbg, &drs, ds, error)) !=
666 	    DW_DLE_NONE)
667 		goto gen_fail1;
668 
669 	/* Length placeholder. (We only use 32-bit DWARF format) */
670 	RCHECK(WRITE_VALUE(0, 4));
671 
672 	/* Write line number dwarf version. (DWARF2) */
673 	RCHECK(WRITE_VALUE(li->li_version, 2));
674 
675 	/* Header length placeholder. */
676 	offset = ds->ds_size;
677 	RCHECK(WRITE_VALUE(li->li_hdrlen, 4));
678 
679 	/* Write minimum instruction length. */
680 	RCHECK(WRITE_VALUE(li->li_minlen, 1));
681 
682 	/*
683 	 * Write initial value for is_stmt. XXX Which default value we
684 	 * should use?
685 	 */
686 	RCHECK(WRITE_VALUE(li->li_defstmt, 1));
687 
688 	/*
689 	 * Write line_base and line_range. FIXME These value needs to be
690 	 * fine tuned.
691 	 */
692 	RCHECK(WRITE_VALUE(li->li_lbase, 1));
693 	RCHECK(WRITE_VALUE(li->li_lrange, 1));
694 
695 	/* Write opcode_base. (DWARF2) */
696 	RCHECK(WRITE_VALUE(li->li_opbase, 1));
697 
698 	/* Write standard op length array. */
699 	RCHECK(WRITE_BLOCK(oplen, sizeof(oplen) / sizeof(oplen[0])));
700 
701 	/* Write the list of include directories. */
702 	for (i = 0; (Dwarf_Unsigned) i < li->li_inclen; i++)
703 		RCHECK(WRITE_STRING(li->li_incdirs[i]));
704 	RCHECK(WRITE_VALUE(0, 1));
705 
706 	/* Write the list of filenames. */
707 	STAILQ_FOREACH(lf, &li->li_lflist, lf_next) {
708 		RCHECK(WRITE_STRING(lf->lf_fname));
709 		RCHECK(WRITE_ULEB128(lf->lf_dirndx));
710 		RCHECK(WRITE_ULEB128(lf->lf_mtime));
711 		RCHECK(WRITE_ULEB128(lf->lf_size));
712 	}
713 	RCHECK(WRITE_VALUE(0, 1));
714 
715 	/* Fill in the header length. */
716 	li->li_hdrlen = ds->ds_size - offset - 4;
717 	dbg->write(ds->ds_data, &offset, li->li_hdrlen, 4);
718 
719 	/* Generate the line number program. */
720 	RCHECK(_dwarf_lineno_gen_program(dbg, ds, drs, error));
721 
722 	/* Fill in the length of this line info. */
723 	li->li_length = ds->ds_size - 4;
724 	offset = 0;
725 	dbg->write(ds->ds_data, &offset, li->li_length, 4);
726 
727 	/* Notify the creation of .debug_line ELF section. */
728 	RCHECK(_dwarf_section_callback(dbg, ds, SHT_PROGBITS, 0, 0, 0, error));
729 
730 	/* Finalize relocation section for .debug_line. */
731 	RCHECK(_dwarf_reloc_section_finalize(dbg, drs, error));
732 
733 	return (DW_DLE_NONE);
734 
735 gen_fail:
736 	_dwarf_reloc_section_free(dbg, &drs);
737 
738 gen_fail1:
739 	_dwarf_section_free(dbg, &ds);
740 
741 	return (ret);
742 }
743 
744 void
745 _dwarf_lineno_pro_cleanup(Dwarf_P_Debug dbg)
746 {
747 	Dwarf_LineInfo li;
748 	Dwarf_LineFile lf, tlf;
749 	Dwarf_Line ln, tln;
750 	int i;
751 
752 	assert(dbg != NULL && dbg->dbg_mode == DW_DLC_WRITE);
753 	if (dbg->dbgp_lineinfo == NULL)
754 		return;
755 
756 	li = dbg->dbgp_lineinfo;
757 	STAILQ_FOREACH_SAFE(lf, &li->li_lflist, lf_next, tlf) {
758 		STAILQ_REMOVE(&li->li_lflist, lf, _Dwarf_LineFile,
759 		    lf_next);
760 		if (lf->lf_fname)
761 			free(lf->lf_fname);
762 		free(lf);
763 	}
764 	STAILQ_FOREACH_SAFE(ln, &li->li_lnlist, ln_next, tln) {
765 		STAILQ_REMOVE(&li->li_lnlist, ln, _Dwarf_Line, ln_next);
766 		free(ln);
767 	}
768 	if (li->li_incdirs) {
769 		for (i = 0; (Dwarf_Unsigned) i < li->li_inclen; i++)
770 			free(li->li_incdirs[i]);
771 		free(li->li_incdirs);
772 	}
773 	free(li);
774 	dbg->dbgp_lineinfo = NULL;
775 }
776