From: Petr Machata Date: Thu, 8 Oct 2009 18:18:10 +0000 (+0200) Subject: dwarflint: More moving code around X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=87d8ca132e8c8f93192834c6d6a50f0d68bfabe5;p=thirdparty%2Felfutils.git dwarflint: More moving code around --- diff --git a/src/Makefile.am b/src/Makefile.am index 87ece1937..1b2bf1bf2 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -84,7 +84,9 @@ dwarflint_SOURCES = dwarfstrings.c \ dwarflint-hl.cc dwarflint-expected-at.cc dwarflint-expected.hh \ dwarflint-coverage.cc dwarflint-coverage.h \ dwarflint-readctx.c dwarflint-readctx.h \ - dwarflint-messages.cc dwarflint-messages.h dwarflint-where.h + dwarflint-messages.cc dwarflint-messages.h \ + dwarflint-where.c dwarflint-where.h \ + dwarflint-config.cc dwarflint-config.h readelf_SOURCES = readelf.c dwarfstrings.c diff --git a/src/dwarflint-config.cc b/src/dwarflint-config.cc new file mode 100644 index 000000000..81fe871e1 --- /dev/null +++ b/src/dwarflint-config.cc @@ -0,0 +1,14 @@ +#include "dwarflint-config.h" + +/* If true, we accept silently files without debuginfo. */ +bool tolerate_nodebug = false; + +/* True if no message is to be printed if the run is succesful. */ +bool be_quiet = false; /* -q */ +bool be_verbose = false; /* -v */ +bool be_strict = false; /* --strict */ +bool be_gnu = false; /* --gnu */ +bool be_tolerant = false; /* --tolerant */ +bool show_refs = false; /* --ref */ +bool do_high_level = true; /* ! --nohl */ +bool dump_die_offsets = false; /* --dump-offsets */ diff --git a/src/dwarflint-config.h b/src/dwarflint-config.h new file mode 100644 index 000000000..639277813 --- /dev/null +++ b/src/dwarflint-config.h @@ -0,0 +1,21 @@ +#ifdef __cplusplus +extern "C" +{ +#else +#include +#endif + + /* Whole-program options. */ + extern bool tolerate_nodebug; + extern bool be_quiet; /* -q */ + extern bool be_verbose; /* -v */ + extern bool be_strict; /* --strict */ + extern bool be_gnu; /* --gnu */ + extern bool be_tolerant; /* --tolerant */ + extern bool show_refs; /* --ref */ + extern bool do_high_level; /* ! --nohl */ + extern bool dump_die_offsets; /* --dump-offsets */ + +#ifdef __cplusplus +} +#endif diff --git a/src/dwarflint-main.cc b/src/dwarflint-main.cc index a9ecc2048..4cf4a202e 100644 --- a/src/dwarflint-main.cc +++ b/src/dwarflint-main.cc @@ -36,6 +36,7 @@ #include #include "dwarflint.h" +#include "dwarflint-config.h" /* Bug report address. */ const char *argp_program_bug_address = PACKAGE_BUGREPORT; @@ -79,19 +80,6 @@ Pedantic checking of DWARF stored in ELF files."); /* Strings for arguments in help texts. */ static const char args_doc[] = N_("FILE..."); -/* If true, we accept silently files without debuginfo. */ -bool tolerate_nodebug = false; - -/* True if no message is to be printed if the run is succesful. */ -bool be_quiet = false; /* -q */ -bool be_verbose = false; /* -v */ -bool be_strict = false; /* --strict */ -bool be_gnu = false; /* --gnu */ -bool be_tolerant = false; /* --tolerant */ -bool show_refs = false; /* --ref */ -bool do_high_level = true; /* ! --nohl */ -bool dump_die_offsets = false; /* --dump-offsets */ - /* Messages that are accepted (and made into warning). */ struct message_criteria warning_criteria; diff --git a/src/dwarflint-where.c b/src/dwarflint-where.c new file mode 100644 index 000000000..3d89cb443 --- /dev/null +++ b/src/dwarflint-where.c @@ -0,0 +1,181 @@ +#include "dwarflint-where.h" +#include "dwarflint-config.h" + +#include +#include +#include +#include +#include + +const char * +where_fmt (const struct where *wh, char *ptr) +{ + if (wh == NULL) + return ""; + + static char buf[256]; + + struct section_info + { + const char *name; + const char *addr1n; + const char *addr1f; + const char *addr2n; + const char *addr2f; + const char *addr3n; + const char *addr3f; + }; + + static struct section_info section_names[] = + { + [sec_info] = {".debug_info", "CU", "%"PRId64, + "DIE", "%#"PRIx64, NULL, NULL}, + + [sec_abbrev] = {".debug_abbrev", "section", "%"PRId64, + "abbreviation", "%"PRId64, "abbr. attribute", "%#"PRIx64}, + + [sec_aranges] = {".debug_aranges", "table", "%"PRId64, + "arange", "%#"PRIx64, NULL, NULL}, + + [sec_pubnames] = {".debug_pubnames", "pubname table", "%"PRId64, + "pubname", "%#"PRIx64, NULL, NULL}, + + [sec_pubtypes] = {".debug_pubtypes", "pubtype table", "%"PRId64, + "pubtype", "%#"PRIx64, NULL, NULL}, + + [sec_str] = {".debug_str", "offset", "%#"PRIx64, + NULL, NULL, NULL, NULL}, + + [sec_line] = {".debug_line", "table", "%"PRId64, + "offset", "%#"PRIx64, NULL, NULL}, + + [sec_loc] = {".debug_loc", "loclist", "%#"PRIx64, + "offset", "%#"PRIx64, NULL, NULL}, + + [sec_mac] = {".debug_mac", NULL, NULL, NULL, NULL, NULL, NULL}, + + [sec_ranges] = {".debug_ranges", "rangelist", "%#"PRIx64, + "offset", "%#"PRIx64, NULL, NULL}, + + [sec_locexpr] = {"location expression", "offset", "%#"PRIx64, + NULL, NULL, NULL, NULL}, + + [sec_rel] = {".rel", "relocation", "%"PRId64, + "offset", "%#"PRIx64, NULL, NULL}, + [sec_rela] = {".rela", "relocation", "%"PRId64, + "offset", "%#"PRIx64, NULL, NULL}, + }; + + static struct section_info special_formats[] = + { + [wf_cudie] = {".debug_info", "CU DIE", "%"PRId64, NULL, NULL, NULL, NULL} + }; + + assert (wh->section < sizeof (section_names) / sizeof (*section_names)); + struct section_info *inf + = (wh->formatting == wf_plain) + ? section_names + wh->section + : special_formats + wh->formatting; + + assert (inf->name); + + assert ((inf->addr1n == NULL) == (inf->addr1f == NULL)); + assert ((inf->addr2n == NULL) == (inf->addr2f == NULL)); + assert ((inf->addr3n == NULL) == (inf->addr3f == NULL)); + + assert ((wh->addr1 != (uint64_t)-1) ? inf->addr1n != NULL : true); + assert ((wh->addr2 != (uint64_t)-1) ? inf->addr2n != NULL : true); + assert ((wh->addr3 != (uint64_t)-1) ? inf->addr3n != NULL : true); + + assert ((wh->addr3 != (uint64_t)-1) ? (wh->addr2 != (uint64_t)-1) : true); + assert ((wh->addr2 != (uint64_t)-1) ? (wh->addr1 != (uint64_t)-1) : true); + + /* GCC insists on checking format parameters and emits a warning + when we don't use string literal. With -Werror this ends up + being hard error. So instead we walk around this warning by + using function pointer. */ + int (*x_asprintf)(char **strp, const char *fmt, ...) = asprintf; + +#define SETUP_ADDR(N) \ + char *addr##N##s; \ + if (wh->addr##N == (uint64_t)-1) \ + addr##N##s = NULL; \ + else if (x_asprintf (&addr##N##s, inf->addr##N##f, wh->addr##N) < 0) \ + addr##N##s = "(fmt error)" + + SETUP_ADDR (1); + SETUP_ADDR (2); + SETUP_ADDR (3); +#undef SETUP_ADDR + + char *orig = ptr; + bool is_reloc = wh->section == sec_rel || wh->section == sec_rela; + if (ptr == NULL) + { + ptr = stpcpy (buf, inf->name); + if (is_reloc) + { + struct where *ref = wh->ref; + assert (ref != NULL); + if (ref->section == sec_locexpr) + { + ref = ref->next; + assert (ref != NULL); + assert (ref->section != sec_locexpr); + } + ptr = stpcpy (ptr, section_names[ref->section].name); + } + + if (addr1s != NULL) + ptr = stpcpy (ptr, ": "); + } + + if (addr3s != NULL) + ptr = stpcpy (stpcpy (stpcpy (ptr, inf->addr3n), " "), addr3s); + else if (addr2s != NULL) + ptr = stpcpy (stpcpy (stpcpy (ptr, inf->addr2n), " "), addr2s); + else if (addr1s != NULL) + ptr = stpcpy (stpcpy (stpcpy (ptr, inf->addr1n), " "), addr1s); + + if (wh->ref != NULL && !is_reloc) + { + ptr = stpcpy (ptr, " ("); + ptr = (char *)where_fmt (wh->ref, ptr); + *ptr++ = ')'; + *ptr = 0; + } + + if (orig == NULL) + return buf; + else + return ptr; +} + +void +where_fmt_chain (const struct where *wh, const char *severity) +{ + if (wh != NULL && show_refs) + for (struct where *it = wh->next; it != NULL; it = it->next) + printf ("%s: %s: caused by this reference.\n", + severity, where_fmt (it, NULL)); +} + +void +where_reset_1 (struct where *wh, uint64_t addr) +{ + wh->addr1 = addr; + wh->addr2 = wh->addr3 = (uint64_t)-1; +} + +void +where_reset_2 (struct where *wh, uint64_t addr) +{ + wh->addr2 = addr; + wh->addr3 = (uint64_t)-1; +} + +void +where_reset_3 (struct where *wh, uint64_t addr) +{ + wh->addr3 = addr; +} diff --git a/src/dwarflint.c b/src/dwarflint.c index 7361e8261..e83647c6e 100644 --- a/src/dwarflint.c +++ b/src/dwarflint.c @@ -47,6 +47,7 @@ #include "dwarfstrings.h" #include "dwarflint.h" #include "dwarflint-readctx.h" +#include "dwarflint-config.h" #include "dwarf-opcodes.h" /* True if coverage analysis of .debug_ranges vs. ELF sections should @@ -228,179 +229,6 @@ static bool check_line_structural (struct elf_file *file, struct sec *sec, struct cu *cu_chain); -const char * -where_fmt (const struct where *wh, char *ptr) -{ - if (wh == NULL) - return ""; - - static char buf[256]; - - struct section_info - { - const char *name; - const char *addr1n; - const char *addr1f; - const char *addr2n; - const char *addr2f; - const char *addr3n; - const char *addr3f; - }; - - static struct section_info section_names[] = - { - [sec_info] = {".debug_info", "CU", "%"PRId64, - "DIE", "%#"PRIx64, NULL, NULL}, - - [sec_abbrev] = {".debug_abbrev", "section", "%"PRId64, - "abbreviation", "%"PRId64, "abbr. attribute", "%#"PRIx64}, - - [sec_aranges] = {".debug_aranges", "table", "%"PRId64, - "arange", "%#"PRIx64, NULL, NULL}, - - [sec_pubnames] = {".debug_pubnames", "pubname table", "%"PRId64, - "pubname", "%#"PRIx64, NULL, NULL}, - - [sec_pubtypes] = {".debug_pubtypes", "pubtype table", "%"PRId64, - "pubtype", "%#"PRIx64, NULL, NULL}, - - [sec_str] = {".debug_str", "offset", "%#"PRIx64, - NULL, NULL, NULL, NULL}, - - [sec_line] = {".debug_line", "table", "%"PRId64, - "offset", "%#"PRIx64, NULL, NULL}, - - [sec_loc] = {".debug_loc", "loclist", "%#"PRIx64, - "offset", "%#"PRIx64, NULL, NULL}, - - [sec_mac] = {".debug_mac", NULL, NULL, NULL, NULL, NULL, NULL}, - - [sec_ranges] = {".debug_ranges", "rangelist", "%#"PRIx64, - "offset", "%#"PRIx64, NULL, NULL}, - - [sec_locexpr] = {"location expression", "offset", "%#"PRIx64, - NULL, NULL, NULL, NULL}, - - [sec_rel] = {".rel", "relocation", "%"PRId64, - "offset", "%#"PRIx64, NULL, NULL}, - [sec_rela] = {".rela", "relocation", "%"PRId64, - "offset", "%#"PRIx64, NULL, NULL}, - }; - - static struct section_info special_formats[] = - { - [wf_cudie] = {".debug_info", "CU DIE", "%"PRId64, NULL, NULL, NULL, NULL} - }; - - assert (wh->section < sizeof (section_names) / sizeof (*section_names)); - struct section_info *inf - = (wh->formatting == wf_plain) - ? section_names + wh->section - : special_formats + wh->formatting; - - assert (inf->name); - - assert ((inf->addr1n == NULL) == (inf->addr1f == NULL)); - assert ((inf->addr2n == NULL) == (inf->addr2f == NULL)); - assert ((inf->addr3n == NULL) == (inf->addr3f == NULL)); - - assert ((wh->addr1 != (uint64_t)-1) ? inf->addr1n != NULL : true); - assert ((wh->addr2 != (uint64_t)-1) ? inf->addr2n != NULL : true); - assert ((wh->addr3 != (uint64_t)-1) ? inf->addr3n != NULL : true); - - assert ((wh->addr3 != (uint64_t)-1) ? (wh->addr2 != (uint64_t)-1) : true); - assert ((wh->addr2 != (uint64_t)-1) ? (wh->addr1 != (uint64_t)-1) : true); - - /* GCC insists on checking format parameters and emits a warning - when we don't use string literal. With -Werror this ends up - being hard error. So instead we walk around this warning by - using function pointer. */ - int (*x_asprintf)(char **strp, const char *fmt, ...) = asprintf; - -#define SETUP_ADDR(N) \ - char *addr##N##s; \ - if (wh->addr##N == (uint64_t)-1) \ - addr##N##s = NULL; \ - else if (x_asprintf (&addr##N##s, inf->addr##N##f, wh->addr##N) < 0) \ - addr##N##s = "(fmt error)" - - SETUP_ADDR (1); - SETUP_ADDR (2); - SETUP_ADDR (3); -#undef SETUP_ADDR - - char *orig = ptr; - bool is_reloc = wh->section == sec_rel || wh->section == sec_rela; - if (ptr == NULL) - { - ptr = stpcpy (buf, inf->name); - if (is_reloc) - { - struct where *ref = wh->ref; - assert (ref != NULL); - if (ref->section == sec_locexpr) - { - ref = ref->next; - assert (ref != NULL); - assert (ref->section != sec_locexpr); - } - ptr = stpcpy (ptr, section_names[ref->section].name); - } - - if (addr1s != NULL) - ptr = stpcpy (ptr, ": "); - } - - if (addr3s != NULL) - ptr = stpcpy (stpcpy (stpcpy (ptr, inf->addr3n), " "), addr3s); - else if (addr2s != NULL) - ptr = stpcpy (stpcpy (stpcpy (ptr, inf->addr2n), " "), addr2s); - else if (addr1s != NULL) - ptr = stpcpy (stpcpy (stpcpy (ptr, inf->addr1n), " "), addr1s); - - if (wh->ref != NULL && !is_reloc) - { - ptr = stpcpy (ptr, " ("); - ptr = (char *)where_fmt (wh->ref, ptr); - *ptr++ = ')'; - *ptr = 0; - } - - if (orig == NULL) - return buf; - else - return ptr; -} - -void -where_fmt_chain (const struct where *wh, const char *severity) -{ - if (wh != NULL && show_refs) - for (struct where *it = wh->next; it != NULL; it = it->next) - printf ("%s: %s: caused by this reference.\n", - severity, where_fmt (it, NULL)); -} - -void -where_reset_1 (struct where *wh, uint64_t addr) -{ - wh->addr1 = addr; - wh->addr2 = wh->addr3 = (uint64_t)-1; -} - -void -where_reset_2 (struct where *wh, uint64_t addr) -{ - wh->addr2 = addr; - wh->addr3 = (uint64_t)-1; -} - -void -where_reset_3 (struct where *wh, uint64_t addr) -{ - wh->addr3 = addr; -} - static bool address_aligned (uint64_t addr, uint64_t align) { diff --git a/src/dwarflint.h b/src/dwarflint.h index 22dd21904..bb815951f 100644 --- a/src/dwarflint.h +++ b/src/dwarflint.h @@ -51,17 +51,6 @@ extern "C" extern bool check_range_out_of_scope (struct hl_ctx *hlctx); extern void process_file (Elf *elf, const char *fname, bool only_one); - /* Whole-program options. */ - extern bool tolerate_nodebug; - extern bool be_quiet; /* -q */ - extern bool be_verbose; /* -v */ - extern bool be_strict; /* --strict */ - extern bool be_gnu; /* --gnu */ - extern bool be_tolerant; /* --tolerant */ - extern bool show_refs; /* --ref */ - extern bool do_high_level; /* ! --nohl */ - extern bool dump_die_offsets; /* --dump-offsets */ - struct relocation { uint64_t offset;