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