tools/elf4rom/src/dwarflinemanager.cpp
changeset 34 92d87f2e53c2
equal deleted inserted replaced
33:1af5c1be89f8 34:92d87f2e53c2
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * This program is free software: you can redistribute it and/or modify
       
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, either version 3 of the License, or
       
     8 * (at your option) any later version.
       
     9 *
       
    10 * This program is distributed in the hope that it will be useful,
       
    11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13 * GNU Lesser General Public License for more details.
       
    14 * 
       
    15 * You should have received a copy of the GNU Lesser General Public License
       
    16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    17 */
       
    18 
       
    19 #include <iostream>
       
    20 #include "dwarfmanager.h"
       
    21 #include "inputfile.h"
       
    22 
       
    23 const string DwarfLineManager::iLineSectionName(".debug_line");
       
    24 
       
    25 inline static size_t process_extended_line_op (FileShdrPair & aPair, Dwarf_Byte_Ptr data)
       
    26 {
       
    27 	size_t bytes_read;
       
    28 	size_t len = (size_t)ULEB128(data, bytes_read);
       
    29 
       
    30 	if (len == 0){
       
    31 		cerr << "badly formed extended line op encountered!\n";
       
    32 		// a length of 0 indicates a badly formed op and will force everything to be ignored until 'end_of_sequence'.
       
    33 		return 0;
       
    34 	}
       
    35 
       
    36 	len += bytes_read;
       
    37 	unsigned char op_code = *data++;
       
    38   	switch (op_code){
       
    39     case DW_LNE_end_sequence:
       
    40       	break;
       
    41     case DW_LNE_set_address: {
       
    42     	size_t size = len - bytes_read - 1;
       
    43 		LinearAddr addr = GetValue(data, size);
       
    44 		LinearAddr relocatedAddress = aPair.iXIPFileDetails.Relocate(addr);
       
    45 		if (addr != relocatedAddress)
       
    46 			WriteValue(data, relocatedAddress, size);
       
    47 		break;
       
    48     	}
       
    49     case DW_LNE_define_file:
       
    50       	ULEB128(data, bytes_read);
       
    51       	ULEB128(data, bytes_read);
       
    52       	ULEB128(data, bytes_read);
       
    53       	break;     
       
    54     default:
       
    55 
       
    56     	break;
       
    57     }
       
    58 
       
    59   return len;
       
    60 }
       
    61 
       
    62 void DwarfLineManager::ProcessSection(FileShdrPair & aPair, Dwarf_Byte_Ptr aStart, Dwarf_Byte_Ptr aEnd){
       
    63 	Dwarf_Byte_Ptr start = aStart;
       
    64 	Dwarf_Byte_Ptr end = aEnd;
       
    65 	size_t bytes_read = 0;
       
    66 	while (start < end){
       
    67 		Dwarf_Byte_Ptr hdrptr = start;		
       
    68 		size_t offset_size, initial_length_size;
       
    69 		
       
    70 		Dwarf_Word length = READ_UNALIGNED4(hdrptr); 
       
    71 		hdrptr += 4;
       
    72 
       
    73 		if (length >= 0xfffffff0u) {
       
    74 			cerr << "Error: 64 bit DWARF not supported\n";
       
    75 			exit(EXIT_FAILURE);
       
    76 		} else {	
       
    77 			offset_size = 4;
       
    78 			initial_length_size = 4;
       
    79 		}
       
    80 		
       
    81 		Dwarf_Byte_Ptr end_of_sequence = start + length + initial_length_size;
       
    82 
       
    83 		Dwarf_Half version = READ_UNALIGNED2(hdrptr);
       
    84 		hdrptr += 2;
       
    85 
       
    86 		if (version != 2 && version != 3){
       
    87 			static bool warned = false;
       
    88 			if (!warned){
       
    89 				cerr << "Only DWARF 2 and 3 aranges are currently supported\n";
       
    90 				warned = true;
       
    91 			}
       
    92 			return;
       
    93 		}
       
    94 		
       
    95 		hdrptr += offset_size;
       
    96 #if 0
       
    97 		// Don't need the next four fields 
       
    98 		Dwarf_Ubyte min_insn_length = *hdrptr++;   
       
    99 		Dwarf_Ubyte default_is_stmt = *hdrptr++;
       
   100 		Dwarf_Ubyte line_base = *hdrptr++;
       
   101 		Dwarf_Ubyte line_range = *hdrptr++;
       
   102 #endif
       
   103 		hdrptr +=4;
       
   104 		Dwarf_Ubyte opcode_base = *hdrptr++;
       
   105 
       
   106 	    /* Skip the contents of the Opcodes table.  */
       
   107 		Dwarf_Byte_Ptr standard_opcodes = hdrptr;
       
   108 		start = standard_opcodes + opcode_base - 1;
       
   109 
       
   110 		/* skip the contents of the Directory table.  */
       
   111 		while (*start != 0){
       
   112 			start += strlen ((char *) start) + 1;
       
   113 		}
       
   114 		/* Skip the NUL at the end of the table.  */
       
   115 		start++;
       
   116 
       
   117 		/* skip the contents of the File Name table.  */
       
   118 		while (*start != 0){
       
   119 			start += strlen ((char *) start) + 1;
       
   120 
       
   121 			ULEB128(start, bytes_read);
       
   122 			ULEB128(start, bytes_read);
       
   123 			ULEB128(start, bytes_read);
       
   124 		}
       
   125 		/* Skip the NUL at the end of the table.  */
       
   126 		start++;
       
   127 	   
       
   128 		while (start < end_of_sequence){
       
   129 			unsigned char op_code = *start++;
       
   130 
       
   131 			if (op_code >= opcode_base){
       
   132 				continue;
       
   133 			} else {
       
   134 				switch (op_code){
       
   135 // missing from dwarf.h - first byte of extended op codes is '0x0'
       
   136 #define DW_LNS_extended_op 0x0
       
   137 				case DW_LNS_extended_op:
       
   138 					size_t n = process_extended_line_op (aPair, start);
       
   139 					// if we don't understand the extended op skip to the end of the sequence :-(
       
   140 					if (n == 0)
       
   141 						start = end_of_sequence;
       
   142 					else 
       
   143 						start += n;
       
   144 					break;
       
   145 
       
   146 				case DW_LNS_copy:
       
   147 					break;
       
   148 
       
   149 				case DW_LNS_advance_pc:
       
   150 				case DW_LNS_advance_line:
       
   151 				case DW_LNS_set_file:
       
   152 				case DW_LNS_set_column:
       
   153 					ULEB128(start, bytes_read);
       
   154 					break;
       
   155 
       
   156 				case DW_LNS_negate_stmt:
       
   157 				case DW_LNS_set_basic_block:
       
   158 				case DW_LNS_const_add_pc:
       
   159 					break;
       
   160 
       
   161 				case DW_LNS_fixed_advance_pc:
       
   162 					start += 2;
       
   163 					break;
       
   164 
       
   165 				case DW_LNS_set_prologue_end:
       
   166 				case DW_LNS_set_epilogue_begin:
       
   167 					break;
       
   168 
       
   169 				case DW_LNS_set_isa:
       
   170 					ULEB128(start, bytes_read);
       
   171 					break;
       
   172 
       
   173 				default:
       
   174 					for (int i = standard_opcodes[op_code - 1]; i > 0 ; --i){
       
   175 						ULEB128(start, bytes_read);
       
   176 					}
       
   177 					break;
       
   178 				}
       
   179 			}
       
   180 		}
       
   181 		// !! eek ARM seems to require header word aligned - at least for Dwarf 2
       
   182 		if (aPair.iXIPFileDetails.iRVCTProduced && (version == 2))
       
   183 			start = (Dwarf_Byte_Ptr)(((unsigned long)start + 3) & ~3);
       
   184 	}
       
   185 }