| 1 |
/* Copyright (c) 2019-2019, David Anderson |
| 2 |
All rights reserved. |
| 3 |
|
| 4 |
Redistribution and use in source and binary forms, with |
| 5 |
or without modification, are permitted provided that the |
| 6 |
following conditions are met: |
| 7 |
|
| 8 |
Redistributions of source code must retain the above |
| 9 |
copyright notice, this list of conditions and the following |
| 10 |
disclaimer. |
| 11 |
|
| 12 |
Redistributions in binary form must reproduce the above |
| 13 |
copyright notice, this list of conditions and the following |
| 14 |
disclaimer in the documentation and/or other materials |
| 15 |
provided with the distribution. |
| 16 |
|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 18 |
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 19 |
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 22 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 24 |
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 26 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 28 |
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 29 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
*/ |
| 31 |
|
| 32 |
#include <config.h> |
| 33 |
|
| 34 |
#include <stdio.h> /* printf() for tests */ |
| 35 |
#include <string.h> /* strcmp() */ |
| 36 |
|
| 37 |
#include "dwarf.h" |
| 38 |
#include "libdwarf.h" |
| 39 |
#include "dwarf_base_types.h" |
| 40 |
#include "dwarf_string.h" |
| 41 |
#include "libdwarfp.h" |
| 42 |
#include "dwarf_pro_incl.h" |
| 43 |
#include "dwarf_pro_opaque.h" |
| 44 |
|
| 45 |
/* in the producer_init extras string. |
| 46 |
Handles hex and decimal. Not octal. |
| 47 |
Used a very small number of times, so performance |
| 48 |
not an issue. */ |
| 49 |
|
| 50 |
/* err will be used...shortly */ |
| 51 |
static int |
| 52 |
translatetosigned(char *s,Dwarf_Signed *v, int *err) |
| 53 |
{ |
| 54 |
unsigned char *cp = (unsigned char *)s; |
| 55 |
unsigned char *digits = (unsigned char *)s; |
| 56 |
int signmult = 1; |
| 57 |
Dwarf_Signed l = 0; |
| 58 |
|
| 59 |
(void)err; |
| 60 |
if (*cp == '0' && |
| 61 |
(*(cp+1) == 'x'|| (*(cp+1) == 'X'))) { |
| 62 |
digits += 2; |
| 63 |
cp = digits; |
| 64 |
for ( ; *cp; cp++) { |
| 65 |
l = l << 4; |
| 66 |
switch (*cp) { |
| 67 |
case '0': |
| 68 |
case '1': |
| 69 |
case '2': |
| 70 |
case '3': |
| 71 |
case '4': |
| 72 |
case '5': |
| 73 |
case '6': |
| 74 |
case '7': |
| 75 |
case '8': |
| 76 |
case '9': |
| 77 |
l += (*cp - '0'); |
| 78 |
break; |
| 79 |
case 'a': |
| 80 |
case 'A': |
| 81 |
l += 10; |
| 82 |
break; |
| 83 |
case 'b': |
| 84 |
case 'B': |
| 85 |
l += 11; |
| 86 |
break; |
| 87 |
case 'c': |
| 88 |
case 'C': |
| 89 |
l += 12; |
| 90 |
break; |
| 91 |
case 'd': |
| 92 |
case 'D': |
| 93 |
l += 13; |
| 94 |
break; |
| 95 |
case 'e': |
| 96 |
case 'E': |
| 97 |
l += 14; |
| 98 |
break; |
| 99 |
case 'f': |
| 100 |
case 'F': |
| 101 |
l += 15; |
| 102 |
break; |
| 103 |
default: |
| 104 |
#ifdef TESTING |
| 105 |
printf("ERROR in hex string \"%s\" " |
| 106 |
"bad character 0x%x, line %d %s\n", |
| 107 |
s,*cp,__LINE__,__FILE__); |
| 108 |
#endif |
| 109 |
*err = DW_DLE_HEX_STRING_ERROR; |
| 110 |
return DW_DLV_ERROR; |
| 111 |
} |
| 112 |
} |
| 113 |
*v = l; |
| 114 |
return DW_DLV_OK; |
| 115 |
} else if (*cp == '-') { |
| 116 |
signmult = -1; |
| 117 |
digits ++; |
| 118 |
} |
| 119 |
|
| 120 |
cp = digits; |
| 121 |
for ( ; *cp; cp++) { |
| 122 |
l = l * 10; |
| 123 |
switch (*cp) { |
| 124 |
case '9': |
| 125 |
case '8': |
| 126 |
case '7': |
| 127 |
case '6': |
| 128 |
case '5': |
| 129 |
case '4': |
| 130 |
case '3': |
| 131 |
case '2': |
| 132 |
case '1': |
| 133 |
case '0': |
| 134 |
l += (*cp - '0'); |
| 135 |
break; |
| 136 |
default: |
| 137 |
#ifdef TESTING |
| 138 |
printf("ERROR in decimal string \"%s\", " |
| 139 |
"bad character 0x%x, line %d %s\n", |
| 140 |
s,*cp,__LINE__,__FILE__); |
| 141 |
#endif |
| 142 |
*err = DW_DLE_DECIMAL_STRING_ERROR; |
| 143 |
return DW_DLV_ERROR; |
| 144 |
} |
| 145 |
} |
| 146 |
*v = signmult * l; |
| 147 |
return DW_DLV_OK; |
| 148 |
} |
| 149 |
|
| 150 |
static int |
| 151 |
update_named_field(Dwarf_P_Debug dbg, |
| 152 |
dwarfstring *cmsname, |
| 153 |
dwarfstring *cmsvalue, |
| 154 |
int *err) |
| 155 |
{ |
| 156 |
char *name = dwarfstring_string(cmsname); |
| 157 |
char *value = dwarfstring_string(cmsvalue); |
| 158 |
Dwarf_Signed v = 0; |
| 159 |
int res; |
| 160 |
|
| 161 |
res = translatetosigned(value,&v,err); |
| 162 |
if (res != DW_DLV_OK) { |
| 163 |
return res; |
| 164 |
} |
| 165 |
if ( dwarfstring_strlen(cmsvalue) == 0) { |
| 166 |
return DW_DLV_NO_ENTRY; |
| 167 |
} |
| 168 |
|
| 169 |
/* The value in the string is a number, |
| 170 |
but always quite a small number. */ |
| 171 |
if (!strcmp(name,"default_is_stmt")) { |
| 172 |
dbg->de_line_inits.pi_default_is_stmt = (unsigned)v; |
| 173 |
} else if (!strcmp(name,"minimum_instruction_length")) { |
| 174 |
dbg->de_line_inits.pi_minimum_instruction_length = |
| 175 |
(unsigned)v; |
| 176 |
} else if (!strcmp(name,"maximum_operations_per_instruction")) { |
| 177 |
dbg->de_line_inits.pi_maximum_operations_per_instruction = |
| 178 |
(unsigned)v; |
| 179 |
} else if (!strcmp(name,"opcode_base")) { |
| 180 |
dbg->de_line_inits.pi_opcode_base = (unsigned)v; |
| 181 |
} else if (!strcmp(name,"line_base")) { |
| 182 |
dbg->de_line_inits.pi_line_base = (int)v; |
| 183 |
} else if (!strcmp(name,"line_range")) { |
| 184 |
dbg->de_line_inits.pi_line_range = (int)v; |
| 185 |
} else if (!strcmp(name,"linetable_version")) { |
| 186 |
dbg->de_line_inits.pi_linetable_version = (unsigned)v; |
| 187 |
dbg->de_output_version = (unsigned char)v; |
| 188 |
} else if (!strcmp(name,"segment_selector_size")) { |
| 189 |
dbg->de_line_inits.pi_segment_selector_size = (unsigned)v; |
| 190 |
} else if (!strcmp(name,"segment_size")) { |
| 191 |
dbg->de_line_inits.pi_segment_size = (unsigned)v; |
| 192 |
} else if (!strcmp(name,"address_size")) { |
| 193 |
dbg->de_line_inits.pi_address_size = (unsigned)v; |
| 194 |
dbg->de_pointer_size = (unsigned char)v; |
| 195 |
} else { |
| 196 |
#ifdef TESTING |
| 197 |
printf("ERROR due to unknown string \"%s\", line %d %s\n", |
| 198 |
name,__LINE__,__FILE__); |
| 199 |
#endif |
| 200 |
*err = DW_DLE_PRO_INIT_EXTRAS_UNKNOWN; |
| 201 |
return DW_DLV_ERROR; |
| 202 |
} |
| 203 |
return DW_DLV_OK; |
| 204 |
} |
| 205 |
static int |
| 206 |
update_named_value(Dwarf_P_Debug dbg, dwarfstring*cms, |
| 207 |
int *err) |
| 208 |
{ |
| 209 |
char * str = dwarfstring_string(cms); |
| 210 |
char *cp = str; |
| 211 |
char * value_start = 0; |
| 212 |
dwarfstring cmsname; |
| 213 |
dwarfstring cmsvalue; |
| 214 |
unsigned slen = 0; |
| 215 |
int res = 0; |
| 216 |
|
| 217 |
dwarfstring_constructor(&cmsname); |
| 218 |
dwarfstring_constructor(&cmsvalue); |
| 219 |
for ( ; *cp && *cp != '=' && *cp != ' '; cp++) { } |
| 220 |
if (! *cp) { |
| 221 |
/* Ignore this, it's empty or has no =value clause */ |
| 222 |
dwarfstring_destructor(&cmsname); |
| 223 |
dwarfstring_destructor(&cmsvalue); |
| 224 |
/* FIXME *err */ |
| 225 |
return DW_DLV_NO_ENTRY; |
| 226 |
} |
| 227 |
if (*cp == ' ') { |
| 228 |
/* Trailing spaces, no = is an input bug. */ |
| 229 |
dwarfstring_destructor(&cmsname); |
| 230 |
dwarfstring_destructor(&cmsvalue); |
| 231 |
#ifdef TESTING |
| 232 |
printf("ERROR due to trailing spaces before = " |
| 233 |
"in \"%s\", line %d %s\n", |
| 234 |
cp,__LINE__,__FILE__); |
| 235 |
#endif |
| 236 |
*err = DW_DLE_PRO_INIT_EXTRAS_ERR; |
| 237 |
return DW_DLV_ERROR; |
| 238 |
} |
| 239 |
slen = cp - str; |
| 240 |
dwarfstring_append_length(&cmsname,str,(size_t)slen); |
| 241 |
cp++; |
| 242 |
value_start = cp; |
| 243 |
for ( ; *cp && *cp != ' '; cp++) { } |
| 244 |
slen = (unsigned int)(cp - value_start); |
| 245 |
if (slen) { |
| 246 |
dwarfstring_append_length(&cmsvalue,value_start,(size_t)slen); |
| 247 |
} else { |
| 248 |
dwarfstring_destructor(&cmsname); |
| 249 |
dwarfstring_destructor(&cmsvalue); |
| 250 |
return DW_DLV_NO_ENTRY; |
| 251 |
} |
| 252 |
res = update_named_field(dbg,&cmsname,&cmsvalue,err); |
| 253 |
dwarfstring_destructor(&cmsname); |
| 254 |
dwarfstring_destructor(&cmsvalue); |
| 255 |
return res; |
| 256 |
} |
| 257 |
|
| 258 |
static int |
| 259 |
find_next_comma(const char *base,const char **nextcomma) |
| 260 |
{ |
| 261 |
const char *cp = base; |
| 262 |
for ( ; *cp ; ++cp) { |
| 263 |
if (*cp == ',') { |
| 264 |
*nextcomma = cp; |
| 265 |
return DW_DLV_OK; |
| 266 |
} |
| 267 |
} |
| 268 |
/* Encountered end of string, should not happen as |
| 269 |
we ensured a last string. */ |
| 270 |
*nextcomma = cp; |
| 271 |
return DW_DLV_OK; |
| 272 |
} |
| 273 |
|
| 274 |
/* Publicly visible in in libdwarf to enable easy testing |
| 275 |
of the code here. */ |
| 276 |
int |
| 277 |
_dwarf_log_extra_flagstrings(Dwarf_P_Debug dbg, |
| 278 |
const char *extra, |
| 279 |
int *err) |
| 280 |
{ |
| 281 |
int res = 0; |
| 282 |
const char *nextcharloc = 0; |
| 283 |
const char *nextcomma = 0; |
| 284 |
dwarfstring cms; |
| 285 |
dwarfstring input; |
| 286 |
|
| 287 |
if (!extra || !*extra) { |
| 288 |
/* Nothing to do. */ |
| 289 |
return DW_DLV_NO_ENTRY; |
| 290 |
} |
| 291 |
|
| 292 |
dwarfstring_constructor(&cms); |
| 293 |
dwarfstring_constructor(&input); |
| 294 |
dwarfstring_append(&input,(char *)extra); |
| 295 |
/* Adding a final , simplifies logic here. */ |
| 296 |
dwarfstring_append(&input,(char *)","); |
| 297 |
nextcharloc = dwarfstring_string(&input); |
| 298 |
while (1) { |
| 299 |
dwarfstring_reset(&cms); |
| 300 |
find_next_comma(nextcharloc,&nextcomma); |
| 301 |
{ |
| 302 |
unsigned len = (unsigned)(nextcomma - nextcharloc); |
| 303 |
if (len > 0) { |
| 304 |
dwarfstring_append_length(&cms,(char *)nextcharloc, |
| 305 |
len); |
| 306 |
res = update_named_value(dbg,&cms,err); |
| 307 |
if (res == DW_DLV_ERROR) { |
| 308 |
dwarfstring_destructor(&cms); |
| 309 |
dwarfstring_destructor(&input); |
| 310 |
return res; |
| 311 |
} |
| 312 |
} else {/* else empty, */ |
| 313 |
} |
| 314 |
if (!(nextcomma[1])) { |
| 315 |
/* done */ |
| 316 |
break; |
| 317 |
} |
| 318 |
nextcharloc = nextcomma+1; |
| 319 |
} |
| 320 |
} |
| 321 |
dwarfstring_destructor(&cms); |
| 322 |
dwarfstring_destructor(&input); |
| 323 |
return DW_DLV_OK; |
| 324 |
} |
| 325 |
|
| 326 |
/* ===== end Initialization using string=value,string2=valu2 (etc) */ |