Deal with some conversion warnings

This commit is contained in:
Jeremy 2023-09-18 22:41:34 -04:00
parent 0b32df64e4
commit ccd0ea5e17
No known key found for this signature in database
GPG Key ID: 19AA8270105E8EB4
3 changed files with 29 additions and 18 deletions

View File

@ -49,14 +49,18 @@ namespace detail {
uintptr_t image_base; uintptr_t image_base;
if(optional_header_magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) { if(optional_header_magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
// 32 bit // 32 bit
image_base = pe_byteswap_if_needed( image_base = to<uintptr_t>(
pe_byteswap_if_needed(
load_bytes<DWORD>(file, nt_header_offset + 0x18 + 0x1c) // optional header + 0x1c load_bytes<DWORD>(file, nt_header_offset + 0x18 + 0x1c) // optional header + 0x1c
)
); );
} else { } else {
// 64 bit // 64 bit
// I get an "error: 'QWORD' was not declared in this scope" for some reason when using QWORD // I get an "error: 'QWORD' was not declared in this scope" for some reason when using QWORD
image_base = pe_byteswap_if_needed( image_base = to<uintptr_t>(
pe_byteswap_if_needed(
load_bytes<unsigned __int64>(file, nt_header_offset + 0x18 + 0x18) // optional header + 0x18 load_bytes<unsigned __int64>(file, nt_header_offset + 0x18 + 0x18) // optional header + 0x18
)
); );
} }
fclose(file); fclose(file);

View File

@ -26,7 +26,6 @@
#include <sys/stat.h> #include <sys/stat.h>
#endif #endif
namespace cpptrace { namespace cpptrace {
namespace detail { namespace detail {
inline std::vector<std::string> split(const std::string& str, const std::string& delims) { inline std::vector<std::string> split(const std::string& str, const std::string& delims) {
@ -336,6 +335,18 @@ namespace detail {
return path.substr(pos + 1); return path.substr(pos + 1);
} }
} }
// A way to cast to unsigned long long without "warning: useless cast to type"
template<typename T>
unsigned long long to_ull(T t) {
return static_cast<unsigned long long>(t);
}
// A way to cast to U without "warning: useless cast to type"
template<typename U, typename V>
unsigned long long to(V v) {
return static_cast<U>(v);
}
} }
} }

View File

@ -44,7 +44,7 @@ namespace libdwarf {
constexpr bool trace_dwarf = false; constexpr bool trace_dwarf = false;
static void err_handler(Dwarf_Error err, Dwarf_Ptr errarg) { static void err_handler(Dwarf_Error err, Dwarf_Ptr errarg) {
printf("libdwarf error reading %s: %lu %s\n", "xx", (unsigned long)dwarf_errno(err), dwarf_errmsg(err)); printf("libdwarf error reading %s: %llu %s\n", "xx", to_ull(dwarf_errno(err)), dwarf_errmsg(err));
if(errarg) { if(errarg) {
printf("Error: errarg is nonnull but it should be null\n"); printf("Error: errarg is nonnull but it should be null\n");
} }
@ -487,7 +487,7 @@ namespace libdwarf {
fprintf( fprintf(
stderr, stderr,
"%08llx %s %s\n", "%08llx %s %s\n",
(unsigned long long) get_global_offset(), to_ull(get_global_offset()),
get_tag_name(), get_tag_name(),
get_name().c_str() get_name().c_str()
); );
@ -668,7 +668,7 @@ namespace libdwarf {
fprintf( fprintf(
stderr, stderr,
"-------------> %08llx %s %s\n", "-------------> %08llx %s %s\n",
(unsigned long long) die.get_global_offset(), to_ull(die.get_global_offset()),
die.get_tag_name(), die.get_tag_name(),
die.get_name().c_str() die.get_name().c_str()
); );
@ -684,7 +684,7 @@ namespace libdwarf {
stderr, stderr,
"%s %08llx %s\n", "%s %08llx %s\n",
die.get_tag() == DW_TAG_namespace ? "pc maybe in die (namespace)" : "pc in die", die.get_tag() == DW_TAG_namespace ? "pc maybe in die (namespace)" : "pc in die",
(unsigned long long) die.get_global_offset(), to_ull(die.get_global_offset()),
die.get_tag_name() die.get_tag_name()
); );
} }
@ -828,7 +828,6 @@ namespace libdwarf {
Dwarf_Unsigned version; Dwarf_Unsigned version;
Dwarf_Small table_count; Dwarf_Small table_count;
Dwarf_Line_Context ctxt; Dwarf_Line_Context ctxt;
Dwarf_Bool is_found = false;
(void)dwversion; (void)dwversion;
auto off = die.get_global_offset(); auto off = die.get_global_offset();
auto it = line_contexts.find(off); auto it = line_contexts.find(off);
@ -877,12 +876,9 @@ namespace libdwarf {
last_pc_line = j_line; last_pc_line = j_line;
} }
} }
is_found = true;
handle_line(last_pc_line, frame); handle_line(last_pc_line, frame);
break; break;
} else if(prev_line && pc > prev_lineaddr && } else if(prev_line && pc > prev_lineaddr && pc < lineaddr) {
pc < lineaddr) {
is_found = true;
handle_line(prev_line, frame); handle_line(prev_line, frame);
break; break;
} }
@ -927,9 +923,9 @@ namespace libdwarf {
fprintf( fprintf(
stderr, stderr,
"pc in die %08llx %s (now searching for %08llx)\n", "pc in die %08llx %s (now searching for %08llx)\n",
(unsigned long long) cu_die.get_global_offset(), to_ull(cu_die.get_global_offset()),
cu_die.get_tag_name(), cu_die.get_tag_name(),
pc to_ull(pc)
); );
} }
retrieve_line_info(cu_die, pc, dwversion, frame); // no offset for line info retrieve_line_info(cu_die, pc, dwversion, frame); // no offset for line info
@ -984,7 +980,7 @@ namespace libdwarf {
) { ) {
if(dump_dwarf) { if(dump_dwarf) {
fprintf(stderr, "%s\n", obj_path.c_str()); fprintf(stderr, "%s\n", obj_path.c_str());
fprintf(stderr, "%llx\n", pc); fprintf(stderr, "%llx\n", to_ull(pc));
} }
// Check for .debug_aranges for fast lookup // Check for .debug_aranges for fast lookup
Dwarf_Arange *aranges; Dwarf_Arange *aranges;
@ -1028,7 +1024,7 @@ namespace libdwarf {
stderr, stderr,
"Starting resolution for %s %08llx %s\n", "Starting resolution for %s %08llx %s\n",
obj_path.c_str(), obj_path.c_str(),
(unsigned long long)frame_info.obj_address, to_ull(frame_info.obj_address),
frame_info.symbol.c_str() frame_info.symbol.c_str()
); );
} }