Skip to content

Commit 99c3579

Browse files
committed
Issue #485: macho-parser: Use dwarf_init_path instead of dwarf_object_init_b to fix DWARF5 support (pending libdwarf release)
1 parent b370df0 commit 99c3579

File tree

1 file changed

+12
-157
lines changed

1 file changed

+12
-157
lines changed

src/parsers/macho-parser.cc

Lines changed: 12 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ using namespace kcov;
2727

2828
namespace
2929
{
30-
30+
static void dwarf_error_handler(Dwarf_Error error, Dwarf_Ptr userData)
31+
{
32+
char *msg = dwarf_errmsg(error);
33+
printf("Dwarf ERROR: %s\n", msg);
34+
}
3135
class MachoParser : public IFileParser
3236
{
3337
public:
@@ -127,20 +131,13 @@ class MachoParser : public IFileParser
127131
}
128132

129133
// Parse DWARF
130-
auto intfc = std::make_unique<Dwarf_Obj_Access_Interface_a>();
131-
132-
intfc->ai_object = static_cast<void*>(this);
133-
intfc->ai_methods = &dwarfCb_object_access_methods;
134134

135135
Dwarf_Debug dbg;
136136
Dwarf_Error err;
137-
auto ret = dwarf_object_init_b(
138-
intfc.get(),
139-
[](Dwarf_Error err, Dwarf_Ptr errarg) { panic("DWARF error: %s", dwarf_errmsg(err)); },
140-
nullptr,
141-
0,
142-
&dbg,
143-
&err);
137+
138+
constexpr auto kPathLen = 400;
139+
char pathbuf[kPathLen];
140+
auto ret = dwarf_init_path(name.c_str(),pathbuf,kPathLen,DW_GROUPNUMBER_ANY, dwarf_error_handler, 0, &dbg,&err);
144141
if (ret != DW_DLV_OK)
145142
{
146143
error("Failed to init DWARF: %s", dwarf_errmsg(err));
@@ -174,10 +171,12 @@ class MachoParser : public IFileParser
174171

175172
Dwarf_Small table_count = 0;
176173
Dwarf_Line_Context line_context = NULL;
174+
Dwarf_Unsigned version = 0;
177175

178-
ret = dwarf_srclines_b(cu_die, nullptr, &table_count, &line_context, &err);
176+
ret = dwarf_srclines_b(cu_die, &version, &table_count, &line_context, &err);
179177
if (ret != DW_DLV_OK)
180178
{
179+
printf("ERROR: %s\n", dwarf_errmsg(err));
181180
continue;
182181
}
183182

@@ -375,150 +374,6 @@ class MachoParser : public IFileParser
375374
}
376375

377376
// libdwarf callbacks for the class
378-
Dwarf_Unsigned dwarfCb_do_object_access_get_section_count() const
379-
{
380-
return m_dwarfSections.size();
381-
}
382-
383-
int dwarfCb_object_access_get_section_info(Dwarf_Unsigned section_index,
384-
Dwarf_Obj_Access_Section_a* ret_scn,
385-
int* error)
386-
{
387-
if (section_index >= m_dwarfSections.size())
388-
{
389-
*error = DW_DLE_MDE;
390-
return DW_DLV_ERROR;
391-
}
392-
393-
auto sec = m_dwarfSections[section_index];
394-
sec->sectname[1] = '.';
395-
ret_scn->as_size = sec->size;
396-
ret_scn->as_addr = sec->addr;
397-
ret_scn->as_name = sec->sectname + 1;
398-
if (strcmp(ret_scn->as_name, ".debug_pubnames__DWARF") == 0)
399-
{
400-
ret_scn->as_name = ".debug_pubnames";
401-
}
402-
403-
ret_scn->as_link = 0;
404-
ret_scn->as_entrysize = 0;
405-
406-
return DW_DLV_OK;
407-
}
408-
409-
int dwarfCb_object_access_load_section(Dwarf_Unsigned section_index,
410-
Dwarf_Small** section_data,
411-
int* error)
412-
{
413-
if (section_index >= m_dwarfSections.size())
414-
{
415-
*error = DW_DLE_MDE;
416-
return DW_DLV_ERROR;
417-
}
418-
419-
auto sec = m_dwarfSections[section_index];
420-
421-
// Does not handle FAT binaries, which has an arch offset
422-
m_readPtr = m_fileData + sec->offset;
423-
auto data = static_cast<uint8_t*>(xmalloc(sec->size));
424-
memcpy(data, m_readPtr, sec->size);
425-
426-
*section_data = data;
427-
m_readPtr += sec->size;
428-
429-
return DW_DLV_OK;
430-
}
431-
432-
Dwarf_Small dwarfCb_object_access_get_length_size() const
433-
{
434-
return 4;
435-
}
436-
437-
Dwarf_Small dwarfCb_object_access_get_pointer_size() const
438-
{
439-
return 8;
440-
}
441-
442-
Dwarf_Unsigned dwarfCb_object_access_get_file_size() const
443-
{
444-
return m_fileSize;
445-
}
446-
447-
448-
// --- The implementation of libdwarf callbacks --
449-
static Dwarf_Small staticDwarfCb_object_access_get_byte_order(void* obj_in)
450-
{
451-
// Little endian always
452-
return 0;
453-
}
454-
455-
static Dwarf_Unsigned staticDwarfCb_object_access_get_section_count(void* obj_in)
456-
{
457-
auto pThis = static_cast<MachoParser*>(obj_in);
458-
459-
return pThis->dwarfCb_do_object_access_get_section_count();
460-
}
461-
462-
static int staticDwarfCb_object_access_get_section_info(void* obj_in,
463-
Dwarf_Unsigned section_index,
464-
Dwarf_Obj_Access_Section_a* ret_scn,
465-
int* error)
466-
{
467-
auto pThis = static_cast<MachoParser*>(obj_in);
468-
469-
return pThis->dwarfCb_object_access_get_section_info(section_index, ret_scn, error);
470-
}
471-
472-
static int staticDwarfCb_object_access_load_section(void* obj_in,
473-
Dwarf_Unsigned section_index,
474-
Dwarf_Small** section_data,
475-
int* error)
476-
{
477-
auto pThis = static_cast<MachoParser*>(obj_in);
478-
479-
return pThis->dwarfCb_object_access_load_section(section_index, section_data, error);
480-
}
481-
482-
static int staticDwarfCb_object_relocate_a_section(void* obj_in,
483-
Dwarf_Unsigned section_index,
484-
Dwarf_Debug dbg,
485-
int* error)
486-
{
487-
return DW_DLV_NO_ENTRY;
488-
}
489-
490-
static Dwarf_Small staticDwarfCb_object_access_get_length_size(void* obj_in)
491-
{
492-
auto pThis = static_cast<MachoParser*>(obj_in);
493-
494-
return pThis->dwarfCb_object_access_get_length_size();
495-
}
496-
497-
static Dwarf_Small staticDwarfCb_object_access_get_pointer_size(void* obj_in)
498-
{
499-
auto pThis = static_cast<MachoParser*>(obj_in);
500-
501-
return pThis->dwarfCb_object_access_get_pointer_size();
502-
}
503-
504-
static Dwarf_Unsigned staticDwarfCb_object_access_get_file_size(void* obj_in)
505-
{
506-
auto pThis = static_cast<MachoParser*>(obj_in);
507-
508-
return pThis->dwarfCb_object_access_get_file_size();
509-
}
510-
511-
static constexpr Dwarf_Obj_Access_Methods_a dwarfCb_object_access_methods = {
512-
staticDwarfCb_object_access_get_section_info,
513-
staticDwarfCb_object_access_get_byte_order,
514-
staticDwarfCb_object_access_get_length_size,
515-
staticDwarfCb_object_access_get_pointer_size,
516-
staticDwarfCb_object_access_get_file_size,
517-
staticDwarfCb_object_access_get_section_count,
518-
staticDwarfCb_object_access_load_section,
519-
staticDwarfCb_object_relocate_a_section,
520-
};
521-
522377

523378
std::vector<ILineListener*> m_lineListeners;
524379
std::vector<IFileListener*> m_fileListeners;

0 commit comments

Comments
 (0)