Skip to content

Commit a910e5f

Browse files
committed
Caring about the debug str offsets offset in the string reader
1 parent 30118d0 commit a910e5f

File tree

2 files changed

+18
-27
lines changed

2 files changed

+18
-27
lines changed

src/parser/file/dwarf/v5/parser.c

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,28 @@ static inline char* dwarf5_stringFromSection(uint64_t offset,
5858
* @param debugStrOffsets the debug string offsets section
5959
* @return the optionally deducted string table offset
6060
*/
61-
static inline optional_uint64_t dwarf5_loadStringOffset(uint64_t index, struct lcs_section debugStrOffsets) {
61+
static inline optional_uint64_t dwarf5_loadStringOffset(uint64_t index, struct lcs_section debugStrOffsets, optional_uint64_t offset) {
6262
bool bit64;
6363
size_t counter = 0;
6464
const uint64_t size = dwarf_parseInitialSize(debugStrOffsets.content, &counter, &bit64);
6565
if (bit64) {
6666
if (index >= size / 8) {
6767
return (optional_uint64_t) { .has_value = false };
6868
}
69+
if (offset.has_value) {
70+
return (optional_uint64_t) { true, ((uint64_t*) (debugStrOffsets.content + offset.value))[index] };
71+
}
6972
return (optional_uint64_t) { true, ((uint64_t*) (debugStrOffsets.content + counter))[index] };
7073
} else if (index >= size / 4) {
7174
return (optional_uint64_t) { .has_value = false };
7275
}
76+
if (offset.has_value) {
77+
return (optional_uint64_t) { true, ((uint32_t*) (debugStrOffsets.content + offset.value))[index] };
78+
}
7379
return (optional_uint64_t) { true, ((uint32_t*) (debugStrOffsets.content + counter))[index] };
7480
}
7581

76-
char* dwarf5_readString(void* buffer,
77-
size_t* counter,
78-
uint64_t type,
79-
bool bit64,
80-
struct lcs_section debugLineStr,
81-
struct lcs_section debugStr,
82-
struct lcs_section debugStrOffsets) {
82+
char* dwarf5_readString(struct dwarf_parser* self, void* buffer, size_t* counter, uint64_t type) {
8383
if (type == DW_FORM_string) {
8484
char* toReturn = (buffer + *counter);
8585
*counter += strlen(toReturn) + 1;
@@ -92,7 +92,7 @@ char* dwarf5_readString(void* buffer,
9292
}
9393
uint64_t offset;
9494
if (type == DW_FORM_strp || type == DW_FORM_line_strp || type == DW_FORM_strp_sup) {
95-
if (bit64) {
95+
if (self->bit64) {
9696
offset = *((uint64_t*) (buffer + *counter));
9797
*counter += 8;
9898
} else {
@@ -128,13 +128,13 @@ char* dwarf5_readString(void* buffer,
128128
default: return NULL;
129129
}
130130
type = DW_FORM_strp;
131-
optional_uint64_t value = dwarf5_loadStringOffset(index, debugStrOffsets);
131+
optional_uint64_t value = dwarf5_loadStringOffset(index, self->debugStrOffsets, self->debugStrOffset);
132132
if (!value.has_value) {
133133
return NULL;
134134
}
135135
offset = value.value;
136136
}
137-
return dwarf5_stringFromSection(offset, type, debugLineStr, debugStr);
137+
return dwarf5_stringFromSection(offset, type, self->debugLineStr, self->debugStr);
138138
}
139139

140140
/**
@@ -255,7 +255,7 @@ static inline uint8_t* dwarf5_readMD5(void* buffer, size_t* counter) {
255255
return toReturn;
256256
}
257257

258-
bool dwarf5_consumeSome(void* buffer, size_t* counter, uint64_t type, bool bit64) {
258+
bool dwarf5_consumeSome(struct dwarf_parser* self, void* buffer, size_t* counter, uint64_t type) {
259259
switch (type) {
260260
case DW_FORM_block: {
261261
const uint64_t length = getULEB128(buffer, counter);
@@ -295,18 +295,15 @@ bool dwarf5_consumeSome(void* buffer, size_t* counter, uint64_t type, bool bit64
295295
case DW_FORM_strp:
296296
case DW_FORM_string:
297297
case DW_FORM_line_strp:
298-
(void) dwarf5_readString(buffer, counter, type, bit64,
299-
(struct lcs_section) { .content = NULL, .size = 0 },
300-
(struct lcs_section) { .content = NULL, .size = 0 },
301-
(struct lcs_section) { .content = NULL, .size = 0 });
298+
(void) dwarf5_readString(self, buffer, counter, type);
302299
break;
303300

304301
case DW_FORM_sdata: getLEB128(buffer, counter); break;
305302

306303
case DW_FORM_strx:
307304
case DW_FORM_udata: getULEB128(buffer, counter); break;
308305

309-
case DW_FORM_sec_offset: *counter += bit64 ? 8 : 4; break;
306+
case DW_FORM_sec_offset: *counter += self->bit64 ? 8 : 4; break;
310307

311308
default: return false;
312309
}
@@ -344,7 +341,7 @@ static inline optional_vector_fileAttribute_t dwarf5_parseFileAttributes(struct
344341
vector_iterate(pair_uint64_t, &entryFormats, {
345342
switch (element->first) {
346343
case DW_LNCT_path:
347-
attribute.path = dwarf5_readString(self->debugLine.content, counter, element->second, self->bit64, self->debugLineStr, self->debugStr, self->debugStrOffsets);
344+
attribute.path = dwarf5_readString(self, self->debugLine.content, counter, element->second);
348345
break;
349346

350347
case DW_LNCT_directory_index: {
@@ -374,7 +371,7 @@ static inline optional_vector_fileAttribute_t dwarf5_parseFileAttributes(struct
374371
break;
375372

376373
default:
377-
if (!dwarf5_consumeSome(self->debugLine.content, counter, element->second, self->bit64)) goto fail;
374+
if (!dwarf5_consumeSome(self, self->debugLine.content, counter, element->second)) goto fail;
378375
break;
379376
}
380377
})

src/parser/file/dwarf/v5/parser.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void dwarf5_parser_create(struct dwarf_parser* self);
5858
* @param bit64 whether to use the 64 bit format
5959
* @return whether the data was allowed and skipped successfully
6060
*/
61-
bool dwarf5_consumeSome(void* buffer, size_t* counter, uint64_t type, bool bit64);
61+
bool dwarf5_consumeSome(struct dwarf_parser* self, void* buffer, size_t* counter, uint64_t type);
6262

6363
/**
6464
* @brief Reads a string.
@@ -76,12 +76,6 @@ bool dwarf5_consumeSome(void* buffer, size_t* counter, uint64_t type, bool bit64
7676
* @return a pointer to the string which points into either the given data buffer or into one of the given sections;
7777
* `NULL` is returned if the given data type was not allowed
7878
*/
79-
char* dwarf5_readString(void* buffer,
80-
size_t* counter,
81-
uint64_t type,
82-
bool bit64,
83-
struct lcs_section debugLineStr,
84-
struct lcs_section debugStr,
85-
struct lcs_section debugStrOffsets);
79+
char* dwarf5_readString(struct dwarf_parser* self, void* buffer, size_t* counter, uint64_t type);
8680

8781
#endif /* dwarf_v5_parser_h */

0 commit comments

Comments
 (0)