xref: /titanic_41/usr/src/tools/ctf/dwarf/common/dwarf_line.c (revision 07dc1947c362e187fb955d283b692f8769dd5def)
1 /*
2   Copyright (C) 2000-2006 Silicon Graphics, Inc.  All Rights Reserved.
3   Portions Copyright (C) 2007-2010 David Anderson. All Rights Reserved.
4 
5   This program is free software; you can redistribute it and/or modify it
6   under the terms of version 2.1 of the GNU Lesser General Public License
7   as published by the Free Software Foundation.
8 
9   This program is distributed in the hope that it would be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 
13   Further, this software is distributed without any warranty that it is
14   free of the rightful claim of any third person regarding infringement
15   or the like.  Any license provided herein, whether implied or
16   otherwise, applies only to this software file.  Patent licenses, if
17   any, provided herein do not apply to combinations of this program with
18   other software, or any other product whatsoever.
19 
20   You should have received a copy of the GNU Lesser General Public
21   License along with this program; if not, write the Free Software
22   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
23   USA.
24 
25   Contact information:  Silicon Graphics, Inc., 1500 Crittenden Lane,
26   Mountain View, CA 94043, or:
27 
28   http://www.sgi.com
29 
30   For further information regarding this notice, see:
31 
32   http://oss.sgi.com/projects/GenInfo/NoticeExplan
33 
34 */
35 /* The address of the Free Software Foundation is
36    Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
37    Boston, MA 02110-1301, USA.
38    SGI has moved from the Crittenden Lane address.
39 */
40 
41 
42 
43 
44 #include "config.h"
45 #include "dwarf_incl.h"
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include "dwarf_line.h"
49 
50 static int
is_path_separator(Dwarf_Small s)51 is_path_separator(Dwarf_Small s)
52 {
53     if(s == '/') {
54         return 1;
55     }
56 #ifdef HAVE_WINDOWS_PATH
57     if(s == '\\') {
58         return 1;
59     }
60 #endif
61     return 0;
62 }
63 
64 /* Return 0 if false, 1 if true.
65    If HAVE_WINDOWS_PATH is defined we
66    attempt to handle windows full paths:
67    \\something   or  C:cwdpath.c
68 */
69 static int
file_name_is_full_path(Dwarf_Small * fname)70 file_name_is_full_path(Dwarf_Small  *fname)
71 {
72     Dwarf_Small firstc = *fname;
73     if(is_path_separator(firstc)) {
74         /* Full path. */
75         return 1;
76     }
77     if(!firstc) {
78         return 0;
79     }
80 #ifdef HAVE_WINDOWS_PATH
81     if((firstc >= 'A' && firstc <= 'Z') ||
82        (firstc >= 'a' && firstc <= 'z')) {
83          Dwarf_Small secondc = fname[1];
84          if (secondc == ':') {
85              return 1;
86          }
87     }
88 #endif
89     return 0;
90 }
91 
92 /*
93     Although source files is supposed to return the
94     source files in the compilation-unit, it does
95     not look for any in the statement program.  In
96     other words, it ignores those defined using the
97     extended opcode DW_LNE_define_file.
98 */
99 int
dwarf_srcfiles(Dwarf_Die die,char *** srcfiles,Dwarf_Signed * srcfilecount,Dwarf_Error * error)100 dwarf_srcfiles(Dwarf_Die die,
101                char ***srcfiles,
102                Dwarf_Signed * srcfilecount, Dwarf_Error * error)
103 {
104     /* This pointer is used to scan the portion of the .debug_line
105        section for the current cu. */
106     Dwarf_Small *line_ptr;
107 
108     /* Pointer to a DW_AT_stmt_list attribute in case it exists in the
109        die. */
110     Dwarf_Attribute stmt_list_attr;
111 
112     /* Pointer to DW_AT_comp_dir attribute in die. */
113     Dwarf_Attribute comp_dir_attr;
114 
115     /* Pointer to name of compilation directory. */
116     Dwarf_Small *comp_dir = 0;
117 
118     /* Offset into .debug_line specified by a DW_AT_stmt_list
119        attribute. */
120     Dwarf_Unsigned line_offset = 0;
121 
122     /* This points to a block of char *'s, each of which points to a
123        file name. */
124     char **ret_files = 0;
125 
126     /* The Dwarf_Debug this die belongs to. */
127     Dwarf_Debug dbg = 0;
128 
129     /* Used to chain the file names. */
130     Dwarf_Chain curr_chain = NULL;
131     Dwarf_Chain prev_chain = NULL;
132     Dwarf_Chain head_chain = NULL;
133     Dwarf_Half attrform = 0;
134     int resattr = DW_DLV_ERROR;
135     int lres = DW_DLV_ERROR;
136     struct Line_Table_Prefix_s line_prefix;
137     int i = 0;
138     int res = DW_DLV_ERROR;
139 
140     /* ***** BEGIN CODE ***** */
141     /* Reset error. */
142     if (error != NULL)
143         *error = NULL;
144 
145     CHECK_DIE(die, DW_DLV_ERROR);
146     dbg = die->di_cu_context->cc_dbg;
147 
148     resattr = dwarf_attr(die, DW_AT_stmt_list, &stmt_list_attr, error);
149     if (resattr != DW_DLV_OK) {
150         return resattr;
151     }
152 
153     if (dbg->de_debug_line.dss_index == 0) {
154         _dwarf_error(dbg, error, DW_DLE_DEBUG_LINE_NULL);
155         return (DW_DLV_ERROR);
156     }
157 
158     res = _dwarf_load_section(dbg, &dbg->de_debug_line,error);
159     if (res != DW_DLV_OK) {
160         return res;
161     }
162 
163     lres = dwarf_whatform(stmt_list_attr,&attrform,error);
164     if (lres != DW_DLV_OK) {
165         return lres;
166     }
167     if (attrform != DW_FORM_data4 && attrform != DW_FORM_data8 &&
168         attrform != DW_FORM_sec_offset ) {
169         _dwarf_error(dbg, error, DW_DLE_LINE_OFFSET_BAD);
170         return (DW_DLV_ERROR);
171     }
172     lres = dwarf_global_formref(stmt_list_attr, &line_offset, error);
173     if (lres != DW_DLV_OK) {
174         return lres;
175     }
176     if (line_offset >= dbg->de_debug_line.dss_size) {
177         _dwarf_error(dbg, error, DW_DLE_LINE_OFFSET_BAD);
178         return (DW_DLV_ERROR);
179     }
180     line_ptr = dbg->de_debug_line.dss_data + line_offset;
181     dwarf_dealloc(dbg, stmt_list_attr, DW_DLA_ATTR);
182 
183     /*
184        If die has DW_AT_comp_dir attribute, get the string that names
185        the compilation directory. */
186     resattr = dwarf_attr(die, DW_AT_comp_dir, &comp_dir_attr, error);
187     if (resattr == DW_DLV_ERROR) {
188         return resattr;
189     }
190     if (resattr == DW_DLV_OK) {
191         int cres = DW_DLV_ERROR;
192         char *cdir = 0;
193 
194         cres = dwarf_formstring(comp_dir_attr, &cdir, error);
195         if (cres == DW_DLV_ERROR) {
196             return cres;
197         } else if (cres == DW_DLV_OK) {
198             comp_dir = (Dwarf_Small *) cdir;
199         }
200     }
201     if (resattr == DW_DLV_OK) {
202         dwarf_dealloc(dbg, comp_dir_attr, DW_DLA_ATTR);
203     }
204     dwarf_init_line_table_prefix(&line_prefix);
205     {
206         Dwarf_Small *line_ptr_out = 0;
207         int dres = dwarf_read_line_table_prefix(dbg,
208             line_ptr,
209             dbg->de_debug_line.dss_size,
210             &line_ptr_out,
211             &line_prefix,
212             NULL, NULL,error,
213             0);
214 
215         if (dres == DW_DLV_ERROR) {
216             dwarf_free_line_table_prefix(&line_prefix);
217             return dres;
218         }
219         if (dres == DW_DLV_NO_ENTRY) {
220             dwarf_free_line_table_prefix(&line_prefix);
221             return dres;
222         }
223 
224         line_ptr = line_ptr_out;
225     }
226 
227     for (i = 0; i < line_prefix.pf_files_count; ++i) {
228         struct Line_Table_File_Entry_s *fe =
229             line_prefix.pf_line_table_file_entries + i;
230         char *file_name = (char *) fe->lte_filename;
231         char *dir_name = 0;
232         char *full_name = 0;
233         Dwarf_Unsigned dir_index = fe->lte_directory_index;
234 
235         if (dir_index == 0) {
236             dir_name = (char *) comp_dir;
237         } else {
238             dir_name =
239                 (char *) line_prefix.pf_include_directories[
240                     fe->lte_directory_index - 1];
241         }
242 
243         /* dir_name can be NULL if there is no DW_AT_comp_dir */
244         if(dir_name == 0 || file_name_is_full_path((unsigned char *)file_name)) {
245             /* This is safe because dwarf_dealloc is careful to not
246                dealloc strings which are part of the raw .debug_* data.
247              */
248             full_name = file_name;
249         } else {
250             full_name = (char *) _dwarf_get_alloc(dbg, DW_DLA_STRING,
251                                                   strlen(dir_name) + 1 +
252                                                   strlen(file_name) +
253                                                   1);
254             if (full_name == NULL) {
255                 dwarf_free_line_table_prefix(&line_prefix);
256                 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
257                 return (DW_DLV_ERROR);
258             }
259 
260             /* This is not careful to avoid // in the output, Nothing
261                forces a 'canonical' name format here. Unclear if this
262                needs to be fixed. */
263             strcpy(full_name, dir_name);
264             strcat(full_name, "/");
265             strcat(full_name, file_name);
266         }
267         curr_chain =
268             (Dwarf_Chain) _dwarf_get_alloc(dbg, DW_DLA_CHAIN, 1);
269         if (curr_chain == NULL) {
270             dwarf_free_line_table_prefix(&line_prefix);
271             _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
272             return (DW_DLV_ERROR);
273         }
274         curr_chain->ch_item = full_name;
275         if (head_chain == NULL)
276             head_chain = prev_chain = curr_chain;
277         else {
278             prev_chain->ch_next = curr_chain;
279             prev_chain = curr_chain;
280         }
281     }
282 
283     curr_chain = (Dwarf_Chain) _dwarf_get_alloc(dbg, DW_DLA_CHAIN, 1);
284     if (curr_chain == NULL) {
285         dwarf_free_line_table_prefix(&line_prefix);
286         _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
287         return (DW_DLV_ERROR);
288     }
289 
290 
291 
292 
293     if (line_prefix.pf_files_count == 0) {
294         *srcfiles = NULL;
295         *srcfilecount = 0;
296         dwarf_free_line_table_prefix(&line_prefix);
297         return (DW_DLV_NO_ENTRY);
298     }
299 
300     ret_files = (char **)
301         _dwarf_get_alloc(dbg, DW_DLA_LIST, line_prefix.pf_files_count);
302     if (ret_files == NULL) {
303         _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
304         dwarf_free_line_table_prefix(&line_prefix);
305         return (DW_DLV_ERROR);
306     }
307 
308     curr_chain = head_chain;
309     for (i = 0; i < line_prefix.pf_files_count; i++) {
310         *(ret_files + i) = curr_chain->ch_item;
311         prev_chain = curr_chain;
312         curr_chain = curr_chain->ch_next;
313         dwarf_dealloc(dbg, prev_chain, DW_DLA_CHAIN);
314     }
315 
316     *srcfiles = ret_files;
317     *srcfilecount = line_prefix.pf_files_count;
318     dwarf_free_line_table_prefix(&line_prefix);
319     return (DW_DLV_OK);
320 }
321 
322 
323 /*
324         return DW_DLV_OK if ok. else DW_DLV_NO_ENTRY or DW_DLV_ERROR
325 */
326 int
_dwarf_internal_srclines(Dwarf_Die die,Dwarf_Line ** linebuf,Dwarf_Signed * count,Dwarf_Bool doaddrs,Dwarf_Bool dolines,Dwarf_Error * error)327 _dwarf_internal_srclines(Dwarf_Die die,
328     Dwarf_Line ** linebuf,
329     Dwarf_Signed * count,
330     Dwarf_Bool doaddrs,
331     Dwarf_Bool dolines, Dwarf_Error * error)
332 {
333     /* This pointer is used to scan the portion of the .debug_line
334        section for the current cu. */
335     Dwarf_Small *line_ptr = 0;
336 
337     /* This points to the last byte of the .debug_line portion for the
338        current cu. */
339     Dwarf_Small *line_ptr_end = 0;
340 
341     /* Pointer to a DW_AT_stmt_list attribute in case it exists in the
342        die. */
343     Dwarf_Attribute stmt_list_attr = 0;
344 
345     /* Pointer to DW_AT_comp_dir attribute in die. */
346     Dwarf_Attribute comp_dir_attr = 0;
347 
348     /* Pointer to name of compilation directory. */
349     Dwarf_Small *comp_dir = NULL;
350 
351     /* Offset into .debug_line specified by a DW_AT_stmt_list
352        attribute. */
353     Dwarf_Unsigned line_offset = 0;
354 
355     Dwarf_File_Entry file_entries = 0;
356 
357     /* These are the state machine state variables. */
358     Dwarf_Addr address = 0;
359     Dwarf_Word file = 1;
360     Dwarf_Word line = 1;
361     Dwarf_Word column = 0;
362 
363     /* Phony init. See below for true initialization. */
364     Dwarf_Bool is_stmt = false;
365 
366     Dwarf_Bool basic_block = false;
367     Dwarf_Bool prologue_end = false;
368     Dwarf_Bool epilogue_begin = false;
369     Dwarf_Small isa = 0;
370     Dwarf_Bool end_sequence = false;
371 
372     /* These pointers are used to build the list of files names by this
373        cu.  cur_file_entry points to the file name being added, and
374        prev_file_entry to the previous one. */
375     Dwarf_File_Entry cur_file_entry, prev_file_entry;
376 
377     Dwarf_Sword i = 0;
378     Dwarf_Sword file_entry_count = 0;
379 
380     /* This is the current opcode read from the statement program. */
381     Dwarf_Small opcode = 0;
382 
383     /* Pointer to a Dwarf_Line_Context_s structure that contains the
384        context such as file names and include directories for the set
385        of lines being generated. */
386     Dwarf_Line_Context line_context = 0;
387 
388     /* This is a pointer to the current line being added to the line
389        matrix. */
390     Dwarf_Line curr_line = 0;
391 
392     /* These variables are used to decode leb128 numbers. Leb128_num
393        holds the decoded number, and leb128_length is its length in
394        bytes. */
395     Dwarf_Word leb128_num = 0;
396     Dwarf_Word leb128_length = 0;
397     Dwarf_Sword advance_line = 0;
398 
399     /* This is the operand of the latest fixed_advance_pc extended
400        opcode. */
401     Dwarf_Half fixed_advance_pc = 0;
402 
403     /* Counts the number of lines in the line matrix. */
404     Dwarf_Sword line_count = 0;
405 
406     /* This is the length of an extended opcode instr.  */
407     Dwarf_Word instr_length = 0;
408     Dwarf_Small ext_opcode = 0;
409     struct Line_Table_Prefix_s prefix;
410 
411     /* Used to chain together pointers to line table entries that are
412        later used to create a block of Dwarf_Line entries. */
413     Dwarf_Chain chain_line = NULL;
414     Dwarf_Chain head_chain = NULL;
415     Dwarf_Chain curr_chain = NULL;
416 
417     /* This points to a block of Dwarf_Lines, a pointer to which is
418        returned in linebuf. */
419     Dwarf_Line *block_line = 0;
420 
421     /* The Dwarf_Debug this die belongs to. */
422     Dwarf_Debug dbg = 0;
423     int resattr = DW_DLV_ERROR;
424     int lres = DW_DLV_ERROR;
425     Dwarf_Half address_size = 0;
426 
427     int res = DW_DLV_ERROR;
428 
429     /* ***** BEGIN CODE ***** */
430     if (error != NULL)
431         *error = NULL;
432 
433     CHECK_DIE(die, DW_DLV_ERROR);
434     dbg = die->di_cu_context->cc_dbg;
435 
436     res = _dwarf_load_section(dbg, &dbg->de_debug_line,error);
437     if (res != DW_DLV_OK) {
438         return res;
439     }
440     address_size = _dwarf_get_address_size(dbg, die);
441     resattr = dwarf_attr(die, DW_AT_stmt_list, &stmt_list_attr, error);
442     if (resattr != DW_DLV_OK) {
443         return resattr;
444     }
445 
446     lres = dwarf_formudata(stmt_list_attr, &line_offset, error);
447     if (lres != DW_DLV_OK) {
448         return lres;
449     }
450 
451     if (line_offset >= dbg->de_debug_line.dss_size) {
452         _dwarf_error(dbg, error, DW_DLE_LINE_OFFSET_BAD);
453         return (DW_DLV_ERROR);
454     }
455     line_ptr = dbg->de_debug_line.dss_data + line_offset;
456     dwarf_dealloc(dbg, stmt_list_attr, DW_DLA_ATTR);
457 
458     /* If die has DW_AT_comp_dir attribute, get the string that names
459        the compilation directory. */
460     resattr = dwarf_attr(die, DW_AT_comp_dir, &comp_dir_attr, error);
461     if (resattr == DW_DLV_ERROR) {
462         return resattr;
463     }
464     if (resattr == DW_DLV_OK) {
465         int cres = DW_DLV_ERROR;
466         char *cdir = 0;
467 
468         cres = dwarf_formstring(comp_dir_attr, &cdir, error);
469         if (cres == DW_DLV_ERROR) {
470             return cres;
471         } else if (cres == DW_DLV_OK) {
472             comp_dir = (Dwarf_Small *) cdir;
473         }
474     }
475     if (resattr == DW_DLV_OK) {
476         dwarf_dealloc(dbg, comp_dir_attr, DW_DLA_ATTR);
477     }
478     dwarf_init_line_table_prefix(&prefix);
479 
480     {
481         Dwarf_Small *newlinep = 0;
482         int res = dwarf_read_line_table_prefix(dbg,
483             line_ptr,
484             dbg->de_debug_line.dss_size,
485             &newlinep,
486             &prefix,
487             NULL,NULL,
488             error,
489             0);
490 
491         if (res == DW_DLV_ERROR) {
492             dwarf_free_line_table_prefix(&prefix);
493             return res;
494         }
495         if (res == DW_DLV_NO_ENTRY) {
496             dwarf_free_line_table_prefix(&prefix);
497             return res;
498         }
499         line_ptr_end = prefix.pf_line_ptr_end;
500         line_ptr = newlinep;
501     }
502 
503 
504     /* Set up context structure for this set of lines. */
505     line_context = (Dwarf_Line_Context)
506         _dwarf_get_alloc(dbg, DW_DLA_LINE_CONTEXT, 1);
507     if (line_context == NULL) {
508         dwarf_free_line_table_prefix(&prefix);
509         _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
510         return (DW_DLV_ERROR);
511     }
512 
513     /* Fill out a Dwarf_File_Entry list as we use that to implement the
514        define_file operation. */
515     file_entries = prev_file_entry = NULL;
516     for (i = 0; i < prefix.pf_files_count; ++i) {
517         struct Line_Table_File_Entry_s *pfxfile =
518             prefix.pf_line_table_file_entries + i;
519 
520         cur_file_entry = (Dwarf_File_Entry)
521             _dwarf_get_alloc(dbg, DW_DLA_FILE_ENTRY, 1);
522         if (cur_file_entry == NULL) {
523             dwarf_free_line_table_prefix(&prefix);
524             _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
525             return (DW_DLV_ERROR);
526         }
527 
528         cur_file_entry->fi_file_name = pfxfile->lte_filename;
529         cur_file_entry->fi_dir_index = pfxfile->lte_directory_index;
530         cur_file_entry->fi_time_last_mod =
531             pfxfile->lte_last_modification_time;
532 
533         cur_file_entry->fi_file_length = pfxfile->lte_length_of_file;
534 
535         if (file_entries == NULL)
536             file_entries = cur_file_entry;
537         else
538             prev_file_entry->fi_next = cur_file_entry;
539         prev_file_entry = cur_file_entry;
540 
541         file_entry_count++;
542     }
543 
544 
545     /* Initialize the one state machine variable that depends on the
546        prefix.  */
547     is_stmt = prefix.pf_default_is_stmt;
548 
549 
550     /* Start of statement program.  */
551     while (line_ptr < line_ptr_end) {
552         int type;
553 
554         opcode = *(Dwarf_Small *) line_ptr;
555         line_ptr++;
556 
557 
558         /* 'type' is the output */
559         WHAT_IS_OPCODE(type, opcode, prefix.pf_opcode_base,
560                        prefix.pf_opcode_length_table, line_ptr,
561                        prefix.pf_std_op_count);
562 
563         if (type == LOP_DISCARD) {
564             int oc;
565             int opcnt = prefix.pf_opcode_length_table[opcode];
566 
567             for (oc = 0; oc < opcnt; oc++) {
568                 /*
569                  ** Read and discard operands we don't
570                  ** understand.
571                  ** arbitrary choice of unsigned read.
572                  ** signed read would work as well.
573                  */
574                 Dwarf_Unsigned utmp2;
575 
576                 DECODE_LEB128_UWORD(line_ptr, utmp2);
577             }
578         } else if (type == LOP_SPECIAL) {
579             /* This op code is a special op in the object, no matter
580                that it might fall into the standard op range in this
581                compile. That is, these are special opcodes between
582                opcode_base and MAX_LINE_OP_CODE.  (including
583                opcode_base and MAX_LINE_OP_CODE) */
584 
585             opcode = opcode - prefix.pf_opcode_base;
586             address = address + prefix.pf_minimum_instruction_length *
587                 (opcode / prefix.pf_line_range);
588             line =
589                 line + prefix.pf_line_base +
590                 opcode % prefix.pf_line_range;
591 
592             if (dolines) {
593                 curr_line =
594                     (Dwarf_Line) _dwarf_get_alloc(dbg, DW_DLA_LINE, 1);
595                 if (curr_line == NULL) {
596                     dwarf_free_line_table_prefix(&prefix);
597                     _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
598                     return (DW_DLV_ERROR);
599                 }
600 
601                 curr_line->li_address = address;
602                 curr_line->li_addr_line.li_l_data.li_file =
603                     (Dwarf_Sword) file;
604                 curr_line->li_addr_line.li_l_data.li_line =
605                     (Dwarf_Sword) line;
606                 curr_line->li_addr_line.li_l_data.li_column =
607                     (Dwarf_Half) column;
608                 curr_line->li_addr_line.li_l_data.li_is_stmt = is_stmt;
609                 curr_line->li_addr_line.li_l_data.li_basic_block =
610                     basic_block;
611                 curr_line->li_addr_line.li_l_data.li_end_sequence =
612                     curr_line->li_addr_line.li_l_data.
613                     li_epilogue_begin = epilogue_begin;
614                 curr_line->li_addr_line.li_l_data.li_prologue_end =
615                     prologue_end;
616                 curr_line->li_addr_line.li_l_data.li_isa = isa;
617                 curr_line->li_context = line_context;
618                 line_count++;
619 
620                 chain_line = (Dwarf_Chain)
621                     _dwarf_get_alloc(dbg, DW_DLA_CHAIN, 1);
622                 if (chain_line == NULL) {
623                     dwarf_free_line_table_prefix(&prefix);
624                     _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
625                     return (DW_DLV_ERROR);
626                 }
627                 chain_line->ch_item = curr_line;
628 
629                 if (head_chain == NULL)
630                     head_chain = curr_chain = chain_line;
631                 else {
632                     curr_chain->ch_next = chain_line;
633                     curr_chain = chain_line;
634                 }
635             }
636 
637             basic_block = false;
638         } else if (type == LOP_STANDARD) {
639             switch (opcode) {
640 
641             case DW_LNS_copy:{
642                     if (dolines) {
643 
644                         curr_line =
645                             (Dwarf_Line) _dwarf_get_alloc(dbg,
646                                                           DW_DLA_LINE,
647                                                           1);
648                         if (curr_line == NULL) {
649                             dwarf_free_line_table_prefix(&prefix);
650                             _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
651                             return (DW_DLV_ERROR);
652                         }
653 
654                         curr_line->li_address = address;
655                         curr_line->li_addr_line.li_l_data.li_file =
656                             (Dwarf_Sword) file;
657                         curr_line->li_addr_line.li_l_data.li_line =
658                             (Dwarf_Sword) line;
659                         curr_line->li_addr_line.li_l_data.li_column =
660                             (Dwarf_Half) column;
661                         curr_line->li_addr_line.li_l_data.li_is_stmt =
662                             is_stmt;
663                         curr_line->li_addr_line.li_l_data.
664                             li_basic_block = basic_block;
665                         curr_line->li_addr_line.li_l_data.
666                             li_end_sequence = end_sequence;
667                         curr_line->li_context = line_context;
668                         curr_line->li_addr_line.li_l_data.
669                             li_epilogue_begin = epilogue_begin;
670                         curr_line->li_addr_line.li_l_data.
671                             li_prologue_end = prologue_end;
672                         curr_line->li_addr_line.li_l_data.li_isa = isa;
673                         line_count++;
674 
675                         chain_line = (Dwarf_Chain)
676                             _dwarf_get_alloc(dbg, DW_DLA_CHAIN, 1);
677                         if (chain_line == NULL) {
678                             dwarf_free_line_table_prefix(&prefix);
679                             _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
680                             return (DW_DLV_ERROR);
681                         }
682                         chain_line->ch_item = curr_line;
683                         if (head_chain == NULL)
684                             head_chain = curr_chain = chain_line;
685                         else {
686                             curr_chain->ch_next = chain_line;
687                             curr_chain = chain_line;
688                         }
689                     }
690 
691                     basic_block = false;
692                     prologue_end = false;
693                     epilogue_begin = false;
694                     break;
695                 }
696 
697             case DW_LNS_advance_pc:{
698                     Dwarf_Unsigned utmp2;
699 
700                     DECODE_LEB128_UWORD(line_ptr, utmp2);
701                     leb128_num = (Dwarf_Word) utmp2;
702                     address =
703                         address +
704                         prefix.pf_minimum_instruction_length *
705                         leb128_num;
706                     break;
707                 }
708 
709             case DW_LNS_advance_line:{
710                     Dwarf_Signed stmp;
711 
712                     DECODE_LEB128_SWORD(line_ptr, stmp);
713                     advance_line = (Dwarf_Sword) stmp;
714                     line = line + advance_line;
715                     break;
716                 }
717 
718             case DW_LNS_set_file:{
719                     Dwarf_Unsigned utmp2;
720 
721                     DECODE_LEB128_UWORD(line_ptr, utmp2);
722                     file = (Dwarf_Word) utmp2;
723                     break;
724                 }
725 
726             case DW_LNS_set_column:{
727                     Dwarf_Unsigned utmp2;
728 
729                     DECODE_LEB128_UWORD(line_ptr, utmp2);
730                     column = (Dwarf_Word) utmp2;
731                     break;
732                 }
733 
734             case DW_LNS_negate_stmt:{
735 
736                     is_stmt = !is_stmt;
737                     break;
738                 }
739 
740             case DW_LNS_set_basic_block:{
741 
742                     basic_block = true;
743                     break;
744                 }
745 
746             case DW_LNS_const_add_pc:{
747                     opcode = MAX_LINE_OP_CODE - prefix.pf_opcode_base;
748                     address = address +
749                         prefix.pf_minimum_instruction_length * (opcode /
750                             prefix.
751                             pf_line_range);
752                     break;
753                 }
754 
755             case DW_LNS_fixed_advance_pc:{
756 
757                     READ_UNALIGNED(dbg, fixed_advance_pc, Dwarf_Half,
758                                    line_ptr, sizeof(Dwarf_Half));
759                     line_ptr += sizeof(Dwarf_Half);
760                     address = address + fixed_advance_pc;
761                     break;
762                 }
763 
764                 /* New in DWARF3 */
765             case DW_LNS_set_prologue_end:{
766 
767                     prologue_end = true;
768                     break;
769 
770 
771                 }
772                 /* New in DWARF3 */
773             case DW_LNS_set_epilogue_begin:{
774                     epilogue_begin = true;
775                     break;
776                 }
777 
778                 /* New in DWARF3 */
779             case DW_LNS_set_isa:{
780                     Dwarf_Unsigned utmp2;
781 
782                     DECODE_LEB128_UWORD(line_ptr, utmp2);
783                     isa = utmp2;
784                     if (isa != utmp2) {
785                         /* The value of the isa did not fit in our
786                            local so we record it wrong. declare an
787                            error. */
788                         dwarf_free_line_table_prefix(&prefix);
789 
790                         _dwarf_error(dbg, error,
791                                      DW_DLE_LINE_NUM_OPERANDS_BAD);
792                         return (DW_DLV_ERROR);
793                     }
794                     break;
795                 }
796             }
797 
798         } else if (type == LOP_EXTENDED) {
799             Dwarf_Unsigned utmp3;
800 
801             DECODE_LEB128_UWORD(line_ptr, utmp3);
802             instr_length = (Dwarf_Word) utmp3;
803             /* Dwarf_Small is a ubyte and the extended opcode is a
804                ubyte, though not stated as clearly in the 2.0.0 spec as
805                one might hope. */
806             ext_opcode = *(Dwarf_Small *) line_ptr;
807             line_ptr++;
808             switch (ext_opcode) {
809 
810             case DW_LNE_end_sequence:{
811                     end_sequence = true;
812 
813                     if (dolines) {
814                         curr_line = (Dwarf_Line)
815                             _dwarf_get_alloc(dbg, DW_DLA_LINE, 1);
816                         if (curr_line == NULL) {
817                             dwarf_free_line_table_prefix(&prefix);
818                             _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
819                             return (DW_DLV_ERROR);
820                         }
821 
822                         curr_line->li_address = address;
823                         curr_line->li_addr_line.li_l_data.li_file =
824                             (Dwarf_Sword) file;
825                         curr_line->li_addr_line.li_l_data.li_line =
826                             (Dwarf_Sword) line;
827                         curr_line->li_addr_line.li_l_data.li_column =
828                             (Dwarf_Half) column;
829                         curr_line->li_addr_line.li_l_data.li_is_stmt =
830                             prefix.pf_default_is_stmt;
831                         curr_line->li_addr_line.li_l_data.
832                             li_basic_block = basic_block;
833                         curr_line->li_addr_line.li_l_data.
834                             li_end_sequence = end_sequence;
835                         curr_line->li_context = line_context;
836                         curr_line->li_addr_line.li_l_data.
837                             li_epilogue_begin = epilogue_begin;
838                         curr_line->li_addr_line.li_l_data.
839                             li_prologue_end = prologue_end;
840                         curr_line->li_addr_line.li_l_data.li_isa = isa;
841                         line_count++;
842 
843                         chain_line = (Dwarf_Chain)
844                             _dwarf_get_alloc(dbg, DW_DLA_CHAIN, 1);
845                         if (chain_line == NULL) {
846                             dwarf_free_line_table_prefix(&prefix);
847                             _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
848                             return (DW_DLV_ERROR);
849                         }
850                         chain_line->ch_item = curr_line;
851 
852                         if (head_chain == NULL)
853                             head_chain = curr_chain = chain_line;
854                         else {
855                             curr_chain->ch_next = chain_line;
856                             curr_chain = chain_line;
857                         }
858                     }
859 
860                     address = 0;
861                     file = 1;
862                     line = 1;
863                     column = 0;
864                     is_stmt = prefix.pf_default_is_stmt;
865                     basic_block = false;
866                     end_sequence = false;
867                     prologue_end = false;
868                     epilogue_begin = false;
869 
870 
871                     break;
872                 }
873 
874             case DW_LNE_set_address:{
875                     {
876                         READ_UNALIGNED(dbg, address, Dwarf_Addr,
877                                        line_ptr, address_size);
878                         if (doaddrs) {
879                             curr_line =
880                                 (Dwarf_Line) _dwarf_get_alloc(dbg,
881                                                               DW_DLA_LINE,
882                                                               1);
883                             if (curr_line == NULL) {
884                                 dwarf_free_line_table_prefix(&prefix);
885                                 _dwarf_error(dbg, error,
886                                              DW_DLE_ALLOC_FAIL);
887                                 return (DW_DLV_ERROR);
888                             }
889 
890                             curr_line->li_address = address;
891                             curr_line->li_addr_line.li_offset =
892                                 line_ptr - dbg->de_debug_line.dss_data;
893 
894                             line_count++;
895 
896                             chain_line = (Dwarf_Chain)
897                                 _dwarf_get_alloc(dbg, DW_DLA_CHAIN, 1);
898                             if (chain_line == NULL) {
899                                 dwarf_free_line_table_prefix(&prefix);
900                                 _dwarf_error(dbg, error,
901                                              DW_DLE_ALLOC_FAIL);
902                                 return (DW_DLV_ERROR);
903                             }
904                             chain_line->ch_item = curr_line;
905 
906                             if (head_chain == NULL)
907                                 head_chain = curr_chain = chain_line;
908                             else {
909                                 curr_chain->ch_next = chain_line;
910                                 curr_chain = chain_line;
911                             }
912                         }
913 
914                         line_ptr += address_size;
915                     }
916 
917                     break;
918                 }
919 
920             case DW_LNE_define_file:{
921 
922                     if (dolines) {
923                         cur_file_entry = (Dwarf_File_Entry)
924                             _dwarf_get_alloc(dbg, DW_DLA_FILE_ENTRY, 1);
925                         if (cur_file_entry == NULL) {
926                             dwarf_free_line_table_prefix(&prefix);
927                             _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
928                             return (DW_DLV_ERROR);
929                         }
930 
931                         cur_file_entry->fi_file_name =
932                             (Dwarf_Small *) line_ptr;
933                         line_ptr =
934                             line_ptr + strlen((char *) line_ptr) + 1;
935 
936                         cur_file_entry->fi_dir_index = (Dwarf_Sword)
937                             _dwarf_decode_u_leb128(line_ptr,
938                                                    &leb128_length);
939                         line_ptr = line_ptr + leb128_length;
940 
941                         cur_file_entry->fi_time_last_mod =
942                             _dwarf_decode_u_leb128(line_ptr,
943                                                    &leb128_length);
944                         line_ptr = line_ptr + leb128_length;
945 
946                         cur_file_entry->fi_file_length =
947                             _dwarf_decode_u_leb128(line_ptr,
948                                                    &leb128_length);
949                         line_ptr = line_ptr + leb128_length;
950 
951                         if (file_entries == NULL)
952                             file_entries = cur_file_entry;
953                         else
954                             prev_file_entry->fi_next = cur_file_entry;
955                         prev_file_entry = cur_file_entry;
956 
957                         file_entry_count++;
958                     }
959                     break;
960                 }
961 
962             default:{
963                  /* This is an extended op code we do not know about,
964                     other than we know now many bytes it is
965                     and the op code and the bytes of operand. */
966                  Dwarf_Unsigned remaining_bytes = instr_length -1;
967                  if(instr_length < 1 || remaining_bytes > DW_LNE_LEN_MAX) {
968                       dwarf_free_line_table_prefix(&prefix);
969                       _dwarf_error(dbg, error,
970                                  DW_DLE_LINE_EXT_OPCODE_BAD);
971                       return (DW_DLV_ERROR);
972                  }
973                  line_ptr += remaining_bytes;
974                  break;
975                 }
976             }
977 
978         }
979     }
980 
981     block_line = (Dwarf_Line *)
982         _dwarf_get_alloc(dbg, DW_DLA_LIST, line_count);
983     if (block_line == NULL) {
984         dwarf_free_line_table_prefix(&prefix);
985         _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
986         return (DW_DLV_ERROR);
987     }
988 
989     curr_chain = head_chain;
990     for (i = 0; i < line_count; i++) {
991         *(block_line + i) = curr_chain->ch_item;
992         head_chain = curr_chain;
993         curr_chain = curr_chain->ch_next;
994         dwarf_dealloc(dbg, head_chain, DW_DLA_CHAIN);
995     }
996 
997     line_context->lc_file_entries = file_entries;
998     line_context->lc_file_entry_count = file_entry_count;
999     line_context->lc_include_directories_count =
1000         prefix.pf_include_directories_count;
1001     if (prefix.pf_include_directories_count > 0) {
1002         /* This gets a pointer to the *first* include dir. The others
1003            follow directly with the standard DWARF2/3 NUL byte
1004            following the last. */
1005         line_context->lc_include_directories =
1006             prefix.pf_include_directories[0];
1007     }
1008 
1009     line_context->lc_line_count = line_count;
1010     line_context->lc_compilation_directory = comp_dir;
1011     line_context->lc_version_number = prefix.pf_version;
1012     line_context->lc_dbg = dbg;
1013     *count = line_count;
1014 
1015     *linebuf = block_line;
1016     dwarf_free_line_table_prefix(&prefix);
1017     return (DW_DLV_OK);
1018 }
1019 
1020 int
dwarf_srclines(Dwarf_Die die,Dwarf_Line ** linebuf,Dwarf_Signed * linecount,Dwarf_Error * error)1021 dwarf_srclines(Dwarf_Die die,
1022                Dwarf_Line ** linebuf,
1023                Dwarf_Signed * linecount, Dwarf_Error * error)
1024 {
1025     Dwarf_Signed count = 0;
1026     int res  = _dwarf_internal_srclines(die, linebuf, &count,
1027         /* addrlist= */ false,
1028         /* linelist= */ true, error);
1029     if (res != DW_DLV_OK) {
1030         return res;
1031     }
1032     *linecount = count;
1033     return res;
1034 }
1035 
1036 
1037 
1038 /* Every line table entry (except DW_DLE_end_sequence,
1039    which is returned using dwarf_lineendsequence())
1040    potentially has the begin-statement
1041    flag marked 'on'.   This returns thru *return_bool,
1042    the begin-statement flag.
1043 */
1044 
1045 int
dwarf_linebeginstatement(Dwarf_Line line,Dwarf_Bool * return_bool,Dwarf_Error * error)1046 dwarf_linebeginstatement(Dwarf_Line line,
1047     Dwarf_Bool * return_bool, Dwarf_Error * error)
1048 {
1049     if (line == NULL || return_bool == 0) {
1050         _dwarf_error(NULL, error, DW_DLE_DWARF_LINE_NULL);
1051         return (DW_DLV_ERROR);
1052     }
1053 
1054     *return_bool = (line->li_addr_line.li_l_data.li_is_stmt);
1055     return DW_DLV_OK;
1056 }
1057 
1058 /* At the end of any contiguous line-table there may be
1059    a DW_LNE_end_sequence operator.
1060    This returns non-zero thru *return_bool
1061    if and only if this 'line' entry was a DW_LNE_end_sequence.
1062 
1063    Within a compilation unit or function there may be multiple
1064    line tables, each ending with a DW_LNE_end_sequence.
1065    Each table describes a contiguous region.
1066    Because compilers may split function code up in arbitrary ways
1067    compilers may need to emit multiple contigous regions (ie
1068    line tables) for a single function.
1069    See the DWARF3 spec section 6.2.
1070 */
1071 int
dwarf_lineendsequence(Dwarf_Line line,Dwarf_Bool * return_bool,Dwarf_Error * error)1072 dwarf_lineendsequence(Dwarf_Line line,
1073     Dwarf_Bool * return_bool, Dwarf_Error * error)
1074 {
1075     if (line == NULL) {
1076         _dwarf_error(NULL, error, DW_DLE_DWARF_LINE_NULL);
1077         return (DW_DLV_ERROR);
1078     }
1079 
1080     *return_bool = (line->li_addr_line.li_l_data.li_end_sequence);
1081     return DW_DLV_OK;
1082 }
1083 
1084 
1085 /* Each 'line' entry has a line-number.
1086    If the entry is a DW_LNE_end_sequence the line-number is
1087    meaningless (see dwarf_lineendsequence(), just above).
1088 */
1089 int
dwarf_lineno(Dwarf_Line line,Dwarf_Unsigned * ret_lineno,Dwarf_Error * error)1090 dwarf_lineno(Dwarf_Line line,
1091     Dwarf_Unsigned * ret_lineno, Dwarf_Error * error)
1092 {
1093     if (line == NULL || ret_lineno == 0) {
1094         _dwarf_error(NULL, error, DW_DLE_DWARF_LINE_NULL);
1095         return (DW_DLV_ERROR);
1096     }
1097 
1098     *ret_lineno = (line->li_addr_line.li_l_data.li_line);
1099     return DW_DLV_OK;
1100 }
1101 
1102 /* Each 'line' entry has a file-number, and index into the file table.
1103    If the entry is a DW_LNE_end_sequence the index is
1104    meaningless (see dwarf_lineendsequence(), just above).
1105    The file number returned is an index into the file table
1106    produced by dwarf_srcfiles(), but care is required: the
1107    li_file begins with 1 for real files, so that the li_file returned here
1108    is 1 greater than its index into the dwarf_srcfiles() output array.
1109    And entries from DW_LNE_define_file don't appear in
1110    the dwarf_srcfiles() output so file indexes from here may exceed
1111    the size of the dwarf_srcfiles() output array size.
1112 */
1113 int
dwarf_line_srcfileno(Dwarf_Line line,Dwarf_Unsigned * ret_fileno,Dwarf_Error * error)1114 dwarf_line_srcfileno(Dwarf_Line line,
1115     Dwarf_Unsigned * ret_fileno, Dwarf_Error * error)
1116 {
1117     if (line == NULL || ret_fileno == 0) {
1118         _dwarf_error(NULL, error, DW_DLE_DWARF_LINE_NULL);
1119         return (DW_DLV_ERROR);
1120     }
1121     /* li_file must be <= line->li_context->lc_file_entry_count else it
1122        is trash. li_file 0 means not attributable to any source file
1123        per dwarf2/3 spec. */
1124 
1125     *ret_fileno = (line->li_addr_line.li_l_data.li_file);
1126     return DW_DLV_OK;
1127 }
1128 
1129 
1130 /* Each 'line' entry has a line-address.
1131    If the entry is a DW_LNE_end_sequence the adddress
1132    is one-beyond the last address this contigous region
1133    covers, so the address is not inside the region,
1134    but is just outside it.
1135 */
1136 int
dwarf_lineaddr(Dwarf_Line line,Dwarf_Addr * ret_lineaddr,Dwarf_Error * error)1137 dwarf_lineaddr(Dwarf_Line line,
1138     Dwarf_Addr * ret_lineaddr, Dwarf_Error * error)
1139 {
1140     if (line == NULL || ret_lineaddr == 0) {
1141         _dwarf_error(NULL, error, DW_DLE_DWARF_LINE_NULL);
1142         return (DW_DLV_ERROR);
1143     }
1144 
1145     *ret_lineaddr = (line->li_address);
1146     return DW_DLV_OK;
1147 }
1148 
1149 
1150 /* Each 'line' entry has a column-within-line (offset
1151    within the line) where the
1152    source text begins.
1153    If the entry is a DW_LNE_end_sequence the line-number is
1154    meaningless (see dwarf_lineendsequence(), just above).
1155    Lines of text begin at column 1.  The value 0
1156    means the line begins at the left edge of the line.
1157    (See the DWARF3 spec, section 6.2.2).
1158 */
1159 int
dwarf_lineoff(Dwarf_Line line,Dwarf_Signed * ret_lineoff,Dwarf_Error * error)1160 dwarf_lineoff(Dwarf_Line line,
1161     Dwarf_Signed * ret_lineoff, Dwarf_Error * error)
1162 {
1163     if (line == NULL || ret_lineoff == 0) {
1164         _dwarf_error(NULL, error, DW_DLE_DWARF_LINE_NULL);
1165         return (DW_DLV_ERROR);
1166     }
1167 
1168     *ret_lineoff =
1169         (line->li_addr_line.li_l_data.li_column ==
1170          0 ? -1 : line->li_addr_line.li_l_data.li_column);
1171     return DW_DLV_OK;
1172 }
1173 
1174 
1175 int
dwarf_linesrc(Dwarf_Line line,char ** ret_linesrc,Dwarf_Error * error)1176 dwarf_linesrc(Dwarf_Line line, char **ret_linesrc, Dwarf_Error * error)
1177 {
1178     Dwarf_Signed i = 0;
1179     Dwarf_File_Entry file_entry;
1180     Dwarf_Small *name_buffer = 0;
1181     Dwarf_Small *include_directories = 0;
1182     Dwarf_Small include_direc_full_path = 0;
1183     Dwarf_Small file_name_full_path = 0;
1184     Dwarf_Debug dbg = 0;
1185     unsigned int comp_dir_len = 0;
1186 
1187     if (line == NULL) {
1188         _dwarf_error(NULL, error, DW_DLE_DWARF_LINE_NULL);
1189         return (DW_DLV_ERROR);
1190     }
1191 
1192     if (line->li_context == NULL) {
1193         _dwarf_error(NULL, error, DW_DLE_LINE_CONTEXT_NULL);
1194         return (DW_DLV_ERROR);
1195     }
1196     dbg = line->li_context->lc_dbg;
1197 
1198     if (line->li_addr_line.li_l_data.li_file >
1199         line->li_context->lc_file_entry_count) {
1200         _dwarf_error(dbg, error, DW_DLE_LINE_FILE_NUM_BAD);
1201         return (DW_DLV_ERROR);
1202     }
1203 
1204     if (line->li_addr_line.li_l_data.li_file == 0) {
1205         /* No file name known: see dwarf2/3 spec. */
1206         _dwarf_error(dbg, error, DW_DLE_NO_FILE_NAME);
1207         return (DW_DLV_ERROR);
1208     }
1209     file_entry = line->li_context->lc_file_entries;
1210     /* ASSERT: li_file > 0, dwarf correctness issue, see line table
1211        definition of dwarf2/3 spec. */
1212     /* Example: if li_file is 2 and lc_file_entry_count is 3,
1213        file_entry is file 3 (1 based), aka 2( 0 based) file_entry->next
1214        is file 2 (1 based), aka 1( 0 based) file_entry->next->next is
1215        file 1 (1 based), aka 0( 0 based) file_entry->next->next->next
1216        is NULL.
1217 
1218        and this loop finds the file_entry we need (2 (1 based) in this
1219        case). Because lc_file_entries are in reverse order and
1220        effectively zero based as a count whereas li_file is 1 based. */
1221     for (i = line->li_addr_line.li_l_data.li_file - 1; i > 0; i--)
1222         file_entry = file_entry->fi_next;
1223 
1224     if (file_entry->fi_file_name == NULL) {
1225         _dwarf_error(dbg, error, DW_DLE_NO_FILE_NAME);
1226         return (DW_DLV_ERROR);
1227     }
1228 
1229     file_name_full_path = file_name_is_full_path(file_entry->fi_file_name);
1230     if (file_name_full_path) {
1231         *ret_linesrc = ((char *) file_entry->fi_file_name);
1232         return DW_DLV_OK;
1233     }
1234 
1235     if (file_entry->fi_dir_index == 0) {
1236 
1237         /* dir_index of 0 means that the compilation was in the
1238            'current directory of compilation' */
1239         if (line->li_context->lc_compilation_directory == NULL) {
1240             /* we don't actually *have* a current directory of
1241                compilation: DW_AT_comp_dir was not present Rather than
1242                emitting DW_DLE_NO_COMP_DIR lets just make an empty name
1243                here. In other words, do the best we can with what we do
1244                have instead of reporting an error. _dwarf_error(dbg,
1245                error, DW_DLE_NO_COMP_DIR); return(DW_DLV_ERROR); */
1246             comp_dir_len = 0;
1247         } else {
1248             comp_dir_len = strlen((char *)
1249                                   (line->li_context->
1250                                    lc_compilation_directory));
1251         }
1252 
1253         name_buffer =
1254             _dwarf_get_alloc(line->li_context->lc_dbg, DW_DLA_STRING,
1255                              comp_dir_len + 1 +
1256                              strlen((char *) file_entry->fi_file_name) +
1257                              1);
1258         if (name_buffer == NULL) {
1259             _dwarf_error(line->li_context->lc_dbg, error,
1260                          DW_DLE_ALLOC_FAIL);
1261             return (DW_DLV_ERROR);
1262         }
1263 
1264         if (comp_dir_len > 0) {
1265             /* if comp_dir_len is 0 we do not want to put a / in front
1266                of the fi_file_name as we just don't know anything. */
1267             strcpy((char *) name_buffer,
1268                    (char *) (line->li_context->
1269                              lc_compilation_directory));
1270             strcat((char *) name_buffer, "/");
1271         }
1272         strcat((char *) name_buffer, (char *) file_entry->fi_file_name);
1273         *ret_linesrc = ((char *) name_buffer);
1274         return DW_DLV_OK;
1275     }
1276 
1277     if (file_entry->fi_dir_index >
1278         line->li_context->lc_include_directories_count) {
1279         _dwarf_error(dbg, error, DW_DLE_INCL_DIR_NUM_BAD);
1280         return (DW_DLV_ERROR);
1281     }
1282 
1283     include_directories = line->li_context->lc_include_directories;
1284     for (i = file_entry->fi_dir_index - 1; i > 0; i--)
1285         include_directories += strlen((char *) include_directories) + 1;
1286 
1287     if (line->li_context->lc_compilation_directory) {
1288         comp_dir_len = strlen((char *)
1289             (line->li_context->lc_compilation_directory));
1290     } else {
1291         /* No DW_AT_comp_dir present. Do the best we can without it. */
1292         comp_dir_len = 0;
1293     }
1294 
1295     include_direc_full_path = file_name_is_full_path(include_directories);
1296     name_buffer = _dwarf_get_alloc(dbg, DW_DLA_STRING,
1297         (include_direc_full_path ?  0 : comp_dir_len + 1) +
1298             strlen((char *)include_directories) + 1 +
1299             strlen((char *)file_entry->fi_file_name) + 1);
1300     if (name_buffer == NULL) {
1301         _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
1302         return (DW_DLV_ERROR);
1303     }
1304 
1305     if (!include_direc_full_path) {
1306         if (comp_dir_len > 0) {
1307             strcpy((char *)name_buffer,
1308                 (char *)line->li_context->lc_compilation_directory);
1309             /* Who provides the / needed after the compilation
1310                directory? */
1311             if (!is_path_separator(name_buffer[comp_dir_len - 1])) {
1312                 /* Here we provide the / separator. It
1313                    should work ok for Windows */
1314                 /* Overwrite previous nul terminator with needed / */
1315                 name_buffer[comp_dir_len] = '/';
1316                 name_buffer[comp_dir_len + 1] = 0;
1317             }
1318         }
1319     } else {
1320         strcpy((char *) name_buffer, "");
1321     }
1322     strcat((char *) name_buffer, (char *) include_directories);
1323     strcat((char *) name_buffer, "/");
1324     strcat((char *) name_buffer, (char *) file_entry->fi_file_name);
1325     *ret_linesrc = ((char *) name_buffer);
1326     return DW_DLV_OK;
1327 }
1328 
1329 /* Every line table entry potentially has the basic-block-start
1330    flag marked 'on'.   This returns thru *return_bool,
1331    the basic-block-start flag.
1332 */
1333 int
dwarf_lineblock(Dwarf_Line line,Dwarf_Bool * return_bool,Dwarf_Error * error)1334 dwarf_lineblock(Dwarf_Line line,
1335     Dwarf_Bool * return_bool, Dwarf_Error * error)
1336 {
1337     if (line == NULL) {
1338         _dwarf_error(NULL, error, DW_DLE_DWARF_LINE_NULL);
1339         return (DW_DLV_ERROR);
1340     }
1341     *return_bool = (line->li_addr_line.li_l_data.li_basic_block);
1342     return DW_DLV_OK;
1343 }
1344 
1345 
1346 #if 0                           /* Ignore this.  This needs major
1347                                    re-work. */
1348 /*
1349     This routine works by looking for exact matches between
1350     the current line address and pc, and crossovers from
1351     from less than pc value to greater than.  At each line
1352     that satisfies the above, it records a pointer to the
1353     line, and the difference between the address and pc.
1354     It then scans these pointers and picks out those with
1355     the smallest difference between pc and address.
1356 */
1357 int
1358 dwarf_pclines(Dwarf_Debug dbg,
1359               Dwarf_Addr pc,
1360               Dwarf_Line ** linebuf,
1361               Dwarf_Signed slide,
1362               Dwarf_Signed * linecount, Dwarf_Error * error)
1363 {
1364     /*
1365        Scans the line matrix for the current cu to which a pointer
1366        exists in dbg. */
1367     Dwarf_Line line;
1368     Dwarf_Line prev_line;
1369 
1370     /*
1371        These flags are for efficiency reasons. Check_line is true
1372        initially, but set false when the address of the current line is
1373        greater than pc.  It is set true only when the address of the
1374        current line falls below pc.  This assumes that addresses within
1375        the same segment increase, and we are only interested in the
1376        switch from a less than pc address to a greater than. First_line
1377        is set true initially, but set false after the first line is
1378        scanned.  This is to prevent looking at the address of previous
1379        line when slide is DW_DLS_BACKWARD, and the first line is being
1380        scanned. */
1381     Dwarf_Bool check_line, first_line;
1382 
1383     /*
1384        Diff tracks the smallest difference a line address and the input
1385        pc value. */
1386     Dwarf_Signed diff, i;
1387 
1388     /*
1389        For the slide = DW_DLS_BACKWARD case, pc_less is the value of
1390        the address of the line immediately preceding the first line
1391        that has value greater than pc. For the slide = DW_DLS_FORWARD
1392        case, pc_more is the values of address for the first line that
1393        is greater than pc. Diff is the difference between either of the
1394        these values and pc. */
1395     Dwarf_Addr pc_less, pc_more;
1396 
1397     /*
1398        Pc_line_buf points to a chain of pointers to lines of which
1399        those with a diff equal to the smallest difference will be
1400        returned. */
1401     Dwarf_Line *pc_line_buf, *pc_line;
1402 
1403     /*
1404        Chain_count counts the number of lines in the above chain for
1405        which the diff is equal to the smallest difference This is the
1406        number returned by this routine. */
1407     Dwarf_Signed chain_count;
1408 
1409     chain_head = NULL;
1410 
1411     check_line = true;
1412     first_line = true;
1413     diff = MAX_LINE_DIFF;
1414 
1415     for (i = 0; i < dbg->de_cu_line_count; i++) {
1416 
1417         line = *(dbg->de_cu_line_ptr + i);
1418         prev_line = first_line ? NULL : *(dbg->de_cu_line_ptr + i - 1);
1419 
1420         if (line->li_address == pc) {
1421             chain_ptr = (struct chain *)
1422                 _dwarf_get_alloc(dbg, DW_DLA_CHAIN, 1);
1423             if (chain_ptr == NULL) {
1424                 _dwarf_error(NULL, error, DW_DLE_ALLOC_FAIL);
1425                 return (DW_DLV_ERROR);
1426             }
1427 
1428             chain_ptr->line = line;
1429             chain_ptr->diff = diff = 0;
1430             chain_ptr->next = chain_head;
1431             chain_head = chain_ptr;
1432         } else
1433             /*
1434                Look for crossover from less than pc address to greater
1435                than. */
1436         if (check_line && line->li_address > pc &&
1437                 (first_line ? 0 : prev_line->li_address) < pc)
1438 
1439             if (slide == DW_DLS_BACKWARD && !first_line) {
1440                 pc_less = prev_line->li_address;
1441                 if (pc - pc_less <= diff) {
1442                     chain_ptr = (struct chain *)
1443                         _dwarf_get_alloc(dbg, DW_DLA_CHAIN, 1);
1444                     if (chain_ptr == NULL) {
1445                         _dwarf_error(NULL, error, DW_DLE_ALLOC_FAIL);
1446                         return (DW_DLV_ERROR);
1447                     }
1448 
1449                     chain_ptr->line = prev_line;
1450                     chain_ptr->diff = diff = pc - pc_less;
1451                     chain_ptr->next = chain_head;
1452                     chain_head = chain_ptr;
1453                 }
1454                 check_line = false;
1455             } else if (slide == DW_DLS_FORWARD) {
1456                 pc_more = line->li_address;
1457                 if (pc_more - pc <= diff) {
1458                     chain_ptr = (struct chain *)
1459                         _dwarf_get_alloc(dbg, DW_DLA_CHAIN, 1);
1460                     if (chain_ptr == NULL) {
1461                         _dwarf_error(NULL, error, DW_DLE_ALLOC_FAIL);
1462                         return (DW_DLV_ERROR);
1463                     }
1464 
1465                     chain_ptr->line = line;
1466                     chain_ptr->diff = diff = pc_more - pc;
1467                     chain_ptr->next = chain_head;
1468                     chain_head = chain_ptr;
1469                 }
1470                 check_line = false;
1471             } else
1472                 /* Check addresses only when they go */
1473                 /* below pc.  */
1474             if (line->li_address < pc)
1475                 check_line = true;
1476 
1477         first_line = false;
1478     }
1479 
1480     chain_count = 0;
1481     for (chain_ptr = chain_head; chain_ptr != NULL;
1482          chain_ptr = chain_ptr->next)
1483         if (chain_ptr->diff == diff)
1484             chain_count++;
1485 
1486     pc_line_buf = pc_line = (Dwarf_Line)
1487         _dwarf_get_alloc(dbg, DW_DLA_LIST, chain_count);
1488     for (chain_ptr = chain_head; chain_ptr != NULL;
1489          chain_ptr = chain_ptr->next)
1490         if (chain_ptr->diff == diff) {
1491             *pc_line = chain_ptr->line;
1492             pc_line++;
1493         }
1494 
1495     for (chain_ptr = chain_head; chain_ptr != NULL;) {
1496         chain_head = chain_ptr;
1497         chain_ptr = chain_ptr->next;
1498         dwarf_dealloc(dbg, chain_head, DW_DLA_CHAIN);
1499     }
1500 
1501     *linebuf = pc_line_buf;
1502     return (chain_count);
1503 }
1504 #endif
1505 
1506 
1507 
1508 /*
1509    It's impossible for callers of dwarf_srclines() to get to and
1510    free all the resources (in particular, the li_context and its
1511    lc_file_entries).
1512    So this function, new July 2005, does it.
1513 */
1514 
1515 void
dwarf_srclines_dealloc(Dwarf_Debug dbg,Dwarf_Line * linebuf,Dwarf_Signed count)1516 dwarf_srclines_dealloc(Dwarf_Debug dbg, Dwarf_Line * linebuf,
1517     Dwarf_Signed count)
1518 {
1519 
1520     Dwarf_Signed i = 0;
1521     struct Dwarf_Line_Context_s *context = 0;
1522 
1523     if (count > 0) {
1524         /* All these entries share a single context */
1525         context = linebuf[0]->li_context;
1526     }
1527     for (i = 0; i < count; ++i) {
1528         dwarf_dealloc(dbg, linebuf[i], DW_DLA_LINE);
1529     }
1530     dwarf_dealloc(dbg, linebuf, DW_DLA_LIST);
1531 
1532     if (context) {
1533         Dwarf_File_Entry fe = context->lc_file_entries;
1534 
1535         while (fe) {
1536             Dwarf_File_Entry fenext = fe->fi_next;
1537 
1538             dwarf_dealloc(dbg, fe, DW_DLA_FILE_ENTRY);
1539             fe = fenext;
1540         }
1541         dwarf_dealloc(dbg, context, DW_DLA_LINE_CONTEXT);
1542     }
1543 
1544     return;
1545 }
1546 
1547 /* Operand counts per standard operand.
1548    The initial zero is for DW_LNS_copy.
1549    This is an economical way to verify we understand the table
1550    of standard-opcode-lengths in the line table prologue.  */
1551 #define STANDARD_OPERAND_COUNT_DWARF2 9
1552 #define STANDARD_OPERAND_COUNT_DWARF3 12
1553 static unsigned char
1554   dwarf_standard_opcode_operand_count[STANDARD_OPERAND_COUNT_DWARF3] = {
1555     /* DWARF2 */
1556     0,
1557     1, 1, 1, 1,
1558     0, 0, 0,
1559     1,
1560     /* Following are new for DWARF3. */
1561     0, 0, 1
1562 };
1563 
1564 /* We have a normal standard opcode base, but
1565    an arm compiler emitted a non-standard table!
1566    This could lead to problems...
1567    ARM C/C++ Compiler, RVCT4.0 [Build 4
1568    00] seems to get the table wrong .  */
1569 static unsigned char
1570 dwarf_arm_standard_opcode_operand_count[STANDARD_OPERAND_COUNT_DWARF3] = {
1571     /* DWARF2 */
1572     0,
1573     1, 1, 1, 1,
1574     0, 0, 0,
1575     0,  /* <<< --- this is wrong */
1576     /* Following are new for DWARF3. */
1577     0, 0, 1
1578 };
1579 
1580 static void
print_header_issue(Dwarf_Debug dbg,char * specific_msg,Dwarf_Small * data_start,int * err_count_out)1581 print_header_issue(Dwarf_Debug dbg,
1582     char *specific_msg,
1583     Dwarf_Small *data_start,
1584     int *err_count_out)
1585 {
1586     if(!err_count_out)
1587         return;
1588     printf("*** DWARF CHECK: "
1589         "line table header: %s",
1590         specific_msg);
1591     if( data_start >= dbg->de_debug_line.dss_data &&
1592         (data_start < (dbg->de_debug_line.dss_data +
1593         dbg->de_debug_line.dss_size))) {
1594         Dwarf_Unsigned off = data_start - dbg->de_debug_line.dss_data;
1595         printf(" at .debug_line section offset 0x%" DW_PR_DUx
1596             "  ( %" DW_PR_DUu " ) ",
1597             off,off);
1598     } else {
1599         printf(" (unknown section location) ");
1600     }
1601     printf("***\n");
1602     *err_count_out += 1;
1603 }
1604 
1605 
1606 
1607 /* Common line table prefix reading code.
1608    Returns DW_DLV_OK, DW_DLV_ERROR.
1609    DW_DLV_NO_ENTRY cannot be returned, but callers should
1610    assume it is possible.
1611 
1612    The prefix_out area must be initialized properly before calling this.
1613 
1614    Has the side effect of allocating arrays which
1615    must be freed (see the Line_Table_Prefix_s struct which
1616    holds the pointers to space we allocate here).
1617 
1618    bogus_bytes_ptr and bogus_bytes are output values which
1619    let a print-program notify the user of some surprising bytes
1620    after a line table header and before the line table instructions.
1621    These can be ignored unless one is printing.
1622    And are ignored if NULL passed as the pointer.
1623 */
1624 
1625 /* err_count_out may be NULL, in which case we
1626    make no attempt to count checking-type errors.
1627    Checking-type errors do not stop us, we just report them.
1628 */
1629 int
dwarf_read_line_table_prefix(Dwarf_Debug dbg,Dwarf_Small * data_start,Dwarf_Unsigned data_length,Dwarf_Small ** updated_data_start_out,struct Line_Table_Prefix_s * prefix_out,Dwarf_Small ** bogus_bytes_ptr,Dwarf_Unsigned * bogus_bytes,Dwarf_Error * err,int * err_count_out)1630 dwarf_read_line_table_prefix(Dwarf_Debug dbg,
1631     Dwarf_Small * data_start,
1632     Dwarf_Unsigned data_length,
1633     Dwarf_Small ** updated_data_start_out,
1634     struct Line_Table_Prefix_s *prefix_out,
1635     Dwarf_Small ** bogus_bytes_ptr,
1636     Dwarf_Unsigned *bogus_bytes,
1637     Dwarf_Error * err,
1638     int *err_count_out)
1639 {
1640     Dwarf_Small *line_ptr = data_start;
1641     Dwarf_Unsigned total_length = 0;
1642     int local_length_size = 0;
1643     int local_extension_size = 0;
1644     Dwarf_Unsigned prologue_length = 0;
1645     Dwarf_Half version = 0;
1646     Dwarf_Unsigned directories_count = 0;
1647     Dwarf_Unsigned directories_malloc = 0;
1648     Dwarf_Unsigned files_count = 0;
1649     Dwarf_Unsigned files_malloc = 0;
1650     Dwarf_Small *line_ptr_end = 0;
1651     Dwarf_Small *lp_begin = 0;
1652     if(bogus_bytes_ptr) *bogus_bytes_ptr = 0;
1653     if(bogus_bytes) *bogus_bytes= 0;
1654 
1655     prefix_out->pf_line_ptr_start = line_ptr;
1656     /* READ_AREA_LENGTH updates line_ptr for consumed bytes */
1657     READ_AREA_LENGTH(dbg, total_length, Dwarf_Unsigned,
1658                      line_ptr, local_length_size, local_extension_size);
1659 
1660 
1661     line_ptr_end = line_ptr + total_length;
1662     prefix_out->pf_line_ptr_end = line_ptr_end;
1663     prefix_out->pf_length_field_length = local_length_size +
1664         local_extension_size;
1665     /* ASSERT: prefix_out->pf_length_field_length == line_ptr
1666        -prefix_out->pf_line_ptr_start; */
1667     if (line_ptr_end > dbg->de_debug_line.dss_data +
1668         dbg->de_debug_line.dss_size) {
1669         _dwarf_error(dbg, err, DW_DLE_DEBUG_LINE_LENGTH_BAD);
1670         return (DW_DLV_ERROR);
1671     }
1672     if (line_ptr_end > data_start + data_length) {
1673         _dwarf_error(dbg, err, DW_DLE_DEBUG_LINE_LENGTH_BAD);
1674         return (DW_DLV_ERROR);
1675     }
1676     prefix_out->pf_total_length = total_length;
1677 
1678     READ_UNALIGNED(dbg, version, Dwarf_Half,
1679                    line_ptr, sizeof(Dwarf_Half));
1680     prefix_out->pf_version = version;
1681     line_ptr += sizeof(Dwarf_Half);
1682     if (version != CURRENT_VERSION_STAMP &&
1683         version != CURRENT_VERSION_STAMP3) {
1684         _dwarf_error(dbg, err, DW_DLE_VERSION_STAMP_ERROR);
1685         return (DW_DLV_ERROR);
1686     }
1687 
1688     READ_UNALIGNED(dbg, prologue_length, Dwarf_Unsigned,
1689                    line_ptr, local_length_size);
1690     prefix_out->pf_prologue_length = prologue_length;
1691     line_ptr += local_length_size;
1692     prefix_out->pf_line_prologue_start = line_ptr;
1693 
1694     prefix_out->pf_minimum_instruction_length =
1695         *(unsigned char *) line_ptr;
1696     line_ptr = line_ptr + sizeof(Dwarf_Small);
1697 
1698     prefix_out->pf_default_is_stmt = *(unsigned char *) line_ptr;
1699     line_ptr = line_ptr + sizeof(Dwarf_Small);
1700 
1701     prefix_out->pf_line_base = *(signed char *) line_ptr;
1702     line_ptr = line_ptr + sizeof(Dwarf_Sbyte);
1703 
1704     prefix_out->pf_line_range = *(unsigned char *) line_ptr;
1705     line_ptr = line_ptr + sizeof(Dwarf_Small);
1706 
1707     prefix_out->pf_opcode_base = *(unsigned char *) line_ptr;
1708     line_ptr = line_ptr + sizeof(Dwarf_Small);
1709 
1710     /* Set up the array of standard opcode lengths. */
1711     /* We think this works ok even for cross-endian processing of
1712        objects.  It might be wrong, we might need to specially process
1713        the array of ubyte into host order.  */
1714     prefix_out->pf_opcode_length_table = line_ptr;
1715 
1716     /* pf_opcode_base is one greater than the size of the array. */
1717     line_ptr += prefix_out->pf_opcode_base - 1;
1718 
1719     {
1720         /* Determine (as best we can) whether the
1721            pf_opcode_length_table holds 9 or 12 standard-conforming
1722            entries.  gcc4 upped to DWARF3's 12 without updating the
1723            version number.   */
1724         int operand_ck_fail = true;
1725 
1726         if (prefix_out->pf_opcode_base >= STANDARD_OPERAND_COUNT_DWARF3) {
1727             int mismatch = memcmp(dwarf_standard_opcode_operand_count,
1728                                   prefix_out->pf_opcode_length_table,
1729                                   STANDARD_OPERAND_COUNT_DWARF3);
1730             if(mismatch) {
1731                  if(err_count_out) {
1732                      print_header_issue(dbg,"standard-operands did not match",
1733                          data_start,err_count_out);
1734                  }
1735                  mismatch = memcmp(dwarf_arm_standard_opcode_operand_count,
1736                                   prefix_out->pf_opcode_length_table,
1737                                   STANDARD_OPERAND_COUNT_DWARF3);
1738                  if(!mismatch && err_count_out) {
1739                      print_header_issue(dbg,"arm (incorrect) operands in use",
1740                          data_start,err_count_out);
1741                  }
1742             }
1743             if (!mismatch) {
1744                 if (version == 2) {
1745                     if(err_count_out) {
1746                         print_header_issue(dbg,
1747                         "standard DWARF3 operands matched, but is DWARF2 linetable",
1748                             data_start,err_count_out);
1749                     }
1750                 }
1751                 operand_ck_fail = false;
1752                 prefix_out->pf_std_op_count =
1753                     STANDARD_OPERAND_COUNT_DWARF3;
1754             }
1755         }
1756         if (operand_ck_fail) {
1757             if (prefix_out->pf_opcode_base >=
1758                 STANDARD_OPERAND_COUNT_DWARF2) {
1759 
1760                 int mismatch =
1761                     memcmp(dwarf_standard_opcode_operand_count,
1762                            prefix_out->pf_opcode_length_table,
1763                            STANDARD_OPERAND_COUNT_DWARF2);
1764                 if(mismatch) {
1765                     if(err_count_out) {
1766                         print_header_issue(dbg,"standard-operands-lengths did not match",
1767                             data_start,err_count_out);
1768                     }
1769                     mismatch = memcmp(dwarf_arm_standard_opcode_operand_count,
1770                                   prefix_out->pf_opcode_length_table,
1771                                   STANDARD_OPERAND_COUNT_DWARF2);
1772                     if(!mismatch && err_count_out) {
1773                         print_header_issue(dbg,"arm (incorrect) operand in use",
1774                             data_start,err_count_out);
1775                     }
1776                 }
1777 
1778                 if (!mismatch) {
1779                     operand_ck_fail = false;
1780                     prefix_out->pf_std_op_count =
1781                         STANDARD_OPERAND_COUNT_DWARF2;
1782                 }
1783             }
1784         }
1785         if (operand_ck_fail) {
1786             /* Here we are not sure what the pf_std_op_count is. */
1787             _dwarf_error(dbg, err, DW_DLE_LINE_NUM_OPERANDS_BAD);
1788             return (DW_DLV_ERROR);
1789         }
1790     }
1791     /* At this point we no longer need to check operand counts. */
1792 
1793 
1794     directories_count = 0;
1795     directories_malloc = 5;
1796     prefix_out->pf_include_directories = malloc(sizeof(Dwarf_Small *) *
1797                                                 directories_malloc);
1798     if (prefix_out->pf_include_directories == NULL) {
1799         _dwarf_error(dbg, err, DW_DLE_ALLOC_FAIL);
1800         return (DW_DLV_ERROR);
1801     }
1802     memset(prefix_out->pf_include_directories, 0,
1803            sizeof(Dwarf_Small *) * directories_malloc);
1804 
1805     while ((*(char *) line_ptr) != '\0') {
1806         if (directories_count >= directories_malloc) {
1807             Dwarf_Unsigned expand = 2 * directories_malloc;
1808             Dwarf_Unsigned bytesalloc = sizeof(Dwarf_Small *) * expand;
1809             Dwarf_Small **newdirs =
1810                 realloc(prefix_out->pf_include_directories,
1811                         bytesalloc);
1812 
1813             if (!newdirs) {
1814                 _dwarf_error(dbg, err, DW_DLE_ALLOC_FAIL);
1815                 return (DW_DLV_ERROR);
1816             }
1817             /* Doubled size, zero out second half. */
1818             memset(newdirs + directories_malloc, 0,
1819                    sizeof(Dwarf_Small *) * directories_malloc);
1820             directories_malloc = expand;
1821             prefix_out->pf_include_directories = newdirs;
1822         }
1823         prefix_out->pf_include_directories[directories_count] =
1824             line_ptr;
1825         line_ptr = line_ptr + strlen((char *) line_ptr) + 1;
1826         directories_count++;
1827     }
1828     prefix_out->pf_include_directories_count = directories_count;
1829     line_ptr++;
1830 
1831     files_count = 0;
1832     files_malloc = 5;
1833     prefix_out->pf_line_table_file_entries =
1834         malloc(sizeof(struct Line_Table_File_Entry_s) * files_malloc);
1835     if (prefix_out->pf_line_table_file_entries == NULL) {
1836         _dwarf_error(dbg, err, DW_DLE_ALLOC_FAIL);
1837         return (DW_DLV_ERROR);
1838     }
1839     memset(prefix_out->pf_line_table_file_entries, 0,
1840            sizeof(struct Line_Table_File_Entry_s) * files_malloc);
1841 
1842     while (*(char *) line_ptr != '\0') {
1843         Dwarf_Unsigned utmp;
1844         Dwarf_Unsigned dir_index = 0;
1845         Dwarf_Unsigned lastmod = 0;
1846         Dwarf_Unsigned file_length = 0;
1847         struct Line_Table_File_Entry_s *curline;
1848         Dwarf_Word leb128_length = 0;
1849 
1850 
1851         if (files_count >= files_malloc) {
1852             Dwarf_Unsigned expand = 2 * files_malloc;
1853             struct Line_Table_File_Entry_s *newfiles =
1854                 realloc(prefix_out->pf_line_table_file_entries,
1855                         sizeof(struct Line_Table_File_Entry_s) *
1856                         expand);
1857             if (!newfiles) {
1858                 _dwarf_error(dbg, err, DW_DLE_ALLOC_FAIL);
1859                 return (DW_DLV_ERROR);
1860             }
1861             memset(newfiles + files_malloc, 0,
1862                    sizeof(struct Line_Table_File_Entry_s) *
1863                    files_malloc);
1864             files_malloc = expand;
1865             prefix_out->pf_line_table_file_entries = newfiles;
1866         }
1867         curline = prefix_out->pf_line_table_file_entries + files_count;
1868 
1869         curline->lte_filename = line_ptr;
1870         line_ptr = line_ptr + strlen((char *) line_ptr) + 1;
1871 
1872         DECODE_LEB128_UWORD(line_ptr, utmp);
1873         dir_index = (Dwarf_Sword) utmp;
1874         if (dir_index > directories_count) {
1875             _dwarf_error(dbg, err, DW_DLE_DIR_INDEX_BAD);
1876             return (DW_DLV_ERROR);
1877         }
1878         curline->lte_directory_index = dir_index;
1879 
1880         lastmod = _dwarf_decode_u_leb128(line_ptr, &leb128_length);
1881         line_ptr = line_ptr + leb128_length;
1882         curline->lte_last_modification_time = lastmod;
1883 
1884         /* Skip over file length. */
1885         file_length = _dwarf_decode_u_leb128(line_ptr, &leb128_length);
1886         line_ptr = line_ptr + leb128_length;
1887         curline->lte_length_of_file = file_length;
1888 
1889         ++files_count;
1890 
1891     }
1892     prefix_out->pf_files_count = files_count;
1893     /* Skip trailing nul byte */
1894     ++line_ptr;
1895 
1896 
1897     lp_begin = prefix_out->pf_line_prologue_start +
1898                      prefix_out->pf_prologue_length;
1899     if (line_ptr != lp_begin) {
1900         if(line_ptr > lp_begin) {
1901             _dwarf_error(dbg, err, DW_DLE_LINE_PROLOG_LENGTH_BAD);
1902             return (DW_DLV_ERROR);
1903         } else {
1904             /* Bug in compiler. These
1905              * bytes are really part of the instruction
1906              * stream.  The prefix_out->pf_prologue_length is
1907              * wrong (12 too high).  */
1908             if(bogus_bytes_ptr) {
1909                *bogus_bytes_ptr = line_ptr;
1910             }
1911             if(bogus_bytes) {
1912                /* How far off things are. We expect the
1913                   value 12 ! */
1914                *bogus_bytes = (lp_begin - line_ptr);
1915             }
1916         }
1917         /* Ignore the lp_begin calc. Assume line_ptr right.
1918            Making up for compiler bug. */
1919         lp_begin = line_ptr;
1920 
1921     }
1922 
1923     *updated_data_start_out = lp_begin;
1924     return DW_DLV_OK;
1925 }
1926 
1927 
1928 /* Initialize the Line_Table_Prefix_s struct.
1929    memset is not guaranteed a portable initializer, but works
1930    fine for current architectures.   AFAIK.
1931 */
1932 void
dwarf_init_line_table_prefix(struct Line_Table_Prefix_s * pf)1933 dwarf_init_line_table_prefix(struct Line_Table_Prefix_s *pf)
1934 {
1935     memset(pf, 0, sizeof(*pf));
1936 }
1937 
1938 /* Free any malloc'd area.  of the Line_Table_Prefix_s struct. */
1939 void
dwarf_free_line_table_prefix(struct Line_Table_Prefix_s * pf)1940 dwarf_free_line_table_prefix(struct Line_Table_Prefix_s *pf)
1941 {
1942     if (pf->pf_include_directories) {
1943         free(pf->pf_include_directories);
1944         pf->pf_include_directories = 0;
1945     }
1946     if (pf->pf_line_table_file_entries) {
1947         free(pf->pf_line_table_file_entries);
1948         pf->pf_line_table_file_entries = 0;
1949     }
1950     return;
1951 }
1952