Skip to content

Commit

Permalink
[gcov] Fix big-endian problems
Browse files Browse the repository at this point in the history
In a big-endian .gcda file, the first four bytes are "gcda" instead of "adcg".
All 32-bit values are in big-endian.

With this change, libclang_rt.profile can hopefully produce gcov
compatible output.
  • Loading branch information
MaskRay committed May 12, 2020
1 parent 4c684b9 commit f98709a
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions compiler-rt/lib/profile/GCDAProfiling.c
Expand Up @@ -67,6 +67,8 @@ typedef unsigned long long uint64_t;
/* #define DEBUG_GCDAPROFILING */

enum {
GCOV_DATA_MAGIC = 0x67636461, // "gcda"

GCOV_TAG_FUNCTION = 0x01000000,
GCOV_TAG_COUNTER_ARCS = 0x01a10000,
// GCOV_TAG_OBJECT_SUMMARY superseded GCOV_TAG_PROGRAM_SUMMARY in GCC 9.
Expand Down Expand Up @@ -423,7 +425,7 @@ void llvm_gcda_start_file(const char *orig_filename, const char version[4],
: (version[0] - '0') * 10 + version[2] - '0';
#endif

write_bytes("adcg", 4);
write_32bit_value(GCOV_DATA_MAGIC);
write_bytes(version, 4);
write_32bit_value(checksum);

Expand Down Expand Up @@ -472,7 +474,7 @@ void llvm_gcda_emit_function(uint32_t ident, uint32_t func_checksum,
if (!output_file) return;

/* function tag */
write_bytes("\0\0\0\1", 4);
write_32bit_value(GCOV_TAG_FUNCTION);
write_32bit_value(len);
write_32bit_value(ident);
write_32bit_value(func_checksum);
Expand All @@ -493,7 +495,7 @@ void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {

if (val != (uint32_t)-1) {
/* There are counters present in the file. Merge them. */
if (val != 0x01a10000) {
if (val != GCOV_TAG_COUNTER_ARCS) {
fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
"corrupt arc tag (0x%08x)\n",
filename, val);
Expand All @@ -516,7 +518,7 @@ void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
cur_pos = save_cur_pos;

/* Counter #1 (arcs) tag */
write_bytes("\0\0\xa1\1", 4);
write_32bit_value(GCOV_TAG_COUNTER_ARCS);
write_32bit_value(num_counters * 2);
for (i = 0; i < num_counters; ++i) {
counters[i] += (old_ctrs ? old_ctrs[i] : 0);
Expand Down

0 comments on commit f98709a

Please sign in to comment.