Home | History | Annotate | Download | only in gcov-src
      1 /* Routines required for instrumenting a program.  */
      2 /* Compile this one with gcc.  */
      3 /* Copyright (C) 1989-2014 Free Software Foundation, Inc.
      4 
      5 This file is part of GCC.
      6 
      7 GCC is free software; you can redistribute it and/or modify it under
      8 the terms of the GNU General Public License as published by the Free
      9 Software Foundation; either version 3, or (at your option) any later
     10 version.
     11 
     12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
     13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
     14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     15 for more details.
     16 
     17 Under Section 7 of GPL version 3, you are granted additional
     18 permissions described in the GCC Runtime Library Exception, version
     19 3.1, as published by the Free Software Foundation.
     20 
     21 You should have received a copy of the GNU General Public License and
     22 a copy of the GCC Runtime Library Exception along with this program;
     23 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     24 <http://www.gnu.org/licenses/>.  */
     25 
     26 #include "libgcov.h"
     27 
     28 #if defined(inhibit_libc)
     29 /* If libc and its header files are not available, provide dummy functions.  */
     30 
     31 #if defined(L_gcov)
     32 void __gcov_init (struct gcov_info *p __attribute__ ((unused))) {}
     33 #endif
     34 
     35 #else /* inhibit_libc */
     36 
     37 #if !defined(__KERNEL__)
     38 #include <string.h>
     39 #if GCOV_LOCKED
     40 #include <fcntl.h>
     41 #include <errno.h>
     42 #include <sys/stat.h>
     43 #endif
     44 #endif /* __KERNEL__ */
     45 
     46 #ifdef L_gcov
     47 #include "gcov-io.c"
     48 
     49 /* Unique identifier assigned to each module (object file).  */
     50 static gcov_unsigned_t gcov_cur_module_id = 0;
     51 
     52 
     53 /* Dynamic call graph build and form module groups.  */
     54 int __gcov_compute_module_groups (char **zero_counts) ATTRIBUTE_HIDDEN;
     55 void __gcov_finalize_dyn_callgraph (void) ATTRIBUTE_HIDDEN;
     56 
     57 /* The following functions can be called from outside of this file.  */
     58 extern void gcov_clear (void) ATTRIBUTE_HIDDEN;
     59 extern void gcov_exit (void) ATTRIBUTE_HIDDEN;
     60 extern void set_gcov_dump_complete (void) ATTRIBUTE_HIDDEN;
     61 extern void reset_gcov_dump_complete (void) ATTRIBUTE_HIDDEN;
     62 extern int get_gcov_dump_complete (void) ATTRIBUTE_HIDDEN;
     63 extern void set_gcov_list (struct gcov_info *) ATTRIBUTE_HIDDEN;
     64 __attribute__((weak)) void __coverage_callback (gcov_type, int);
     65 
     66 #if !defined(IN_GCOV_TOOL) && !defined(__KERNEL__)
     67 extern gcov_unsigned_t __gcov_sampling_period;
     68 extern gcov_unsigned_t __gcov_has_sampling;
     69 static int gcov_sampling_period_initialized = 0;
     70 
     71 /* Create a strong reference to these symbols so that they are
     72    unconditionally pulled into the instrumented binary, even when
     73    the only reference is a weak reference. This is necessary because
     74    we are using weak references to enable references from code that
     75    may not be linked with libgcov. These are the only symbols that
     76    should be accessed via link references from application code!
     77 
     78    A subtlety of the linker is that it will only resolve weak references
     79    defined within archive libraries when there is a strong reference to
     80    something else defined within the same object file. Since these functions
     81    are defined within their own object files, they would not automatically
     82    get resolved. Since there are symbols within the main L_gcov
     83    section that are strongly referenced during -fprofile-generate and
     84    -ftest-coverage builds, these dummy symbols will always need to be
     85    resolved.  */
     86 void (*__gcov_dummy_ref1)(void) = &__gcov_reset;
     87 void (*__gcov_dummy_ref2)(void) = &__gcov_dump;
     88 extern char *__gcov_get_profile_prefix (void);
     89 char *(*__gcov_dummy_ref3)(void) = &__gcov_get_profile_prefix;
     90 extern void __gcov_set_sampling_period (unsigned int period);
     91 char *(*__gcov_dummy_ref4)(void) = &__gcov_set_sampling_period;
     92 extern unsigned int __gcov_sampling_enabled (void);
     93 char *(*__gcov_dummy_ref5)(void) = &__gcov_sampling_enabled;
     94 extern void __gcov_flush (void);
     95 char *(*__gcov_dummy_ref6)(void) = &__gcov_flush;
     96 extern unsigned int __gcov_profiling_for_test_coverage (void);
     97 char *(*__gcov_dummy_ref7)(void) = &__gcov_profiling_for_test_coverage;
     98 #endif
     99 
    100 /* Default callback function for profile instrumentation callback.  */
    101 __attribute__((weak)) void
    102 __coverage_callback (gcov_type funcdef_no __attribute__ ((unused)),
    103                      int edge_no __attribute__ ((unused)))
    104 {
    105    /* nothing */
    106 }
    107 
    108 struct gcov_fn_buffer
    109 {
    110   struct gcov_fn_buffer *next;
    111   unsigned fn_ix;
    112   struct gcov_fn_info info;
    113   /* note gcov_fn_info ends in a trailing array.  */
    114 };
    115 
    116 struct gcov_summary_buffer
    117 {
    118   struct gcov_summary_buffer *next;
    119   struct gcov_summary summary;
    120 };
    121 
    122 /* Chain of per-object gcov structures.  */
    123 extern struct gcov_info *__gcov_list;
    124 
    125 /* Set the head of gcov_list.  */
    126 void
    127 set_gcov_list (struct gcov_info *head)
    128 {
    129   __gcov_list = head;
    130 }
    131 
    132 /* Flag if the current function being read was marked as having fixed-up
    133    zero counters.  */
    134 static int __gcov_curr_fn_fixed_up;
    135 
    136 /* Set function fixed up flag.  */
    137 void
    138 set_gcov_fn_fixed_up (int fixed_up)
    139 {
    140   __gcov_curr_fn_fixed_up = fixed_up;
    141 }
    142 
    143 /* Return function fixed up flag.  */
    144 int
    145 get_gcov_fn_fixed_up (void)
    146 {
    147   return __gcov_curr_fn_fixed_up;
    148 }
    149 
    150 /* Size of the longest file name. */
    151 /* We need to expose this static variable when compiling for gcov-tool.  */
    152 #ifndef IN_GCOV_TOOL
    153 static
    154 #endif
    155 size_t gcov_max_filename = 0;
    156 
    157 /* Flag when the profile has already been dumped via __gcov_dump().  */
    158 static int gcov_dump_complete;
    159 
    160 /* A global function that get the vaule of gcov_dump_complete.  */
    161 
    162 int
    163 get_gcov_dump_complete (void)
    164 {
    165   return gcov_dump_complete;
    166 }
    167 
    168 /* A global functino that set the vaule of gcov_dump_complete. Will
    169    be used in __gcov_dump() in libgcov-interface.c.  */
    170 
    171 void
    172 set_gcov_dump_complete (void)
    173 {
    174   gcov_dump_complete = 1;
    175 }
    176 
    177 /* A global functino that set the vaule of gcov_dump_complete. Will
    178    be used in __gcov_reset() in libgcov-interface.c.  */
    179 
    180 void
    181 reset_gcov_dump_complete (void)
    182 {
    183   gcov_dump_complete = 0;
    184 }
    185 
    186 /* A utility function for outputing errors.  */
    187 static int gcov_error (const char *, ...);
    188 
    189 static struct gcov_fn_buffer *
    190 free_fn_data (const struct gcov_info *gi_ptr, struct gcov_fn_buffer *buffer,
    191               unsigned limit)
    192 {
    193   struct gcov_fn_buffer *next;
    194   unsigned ix, n_ctr = 0;
    195 
    196   if (!buffer)
    197     return 0;
    198   next = buffer->next;
    199 
    200   for (ix = 0; ix != limit; ix++)
    201     if (gi_ptr->merge[ix])
    202       xfree (buffer->info.ctrs[n_ctr++].values);
    203   xfree (buffer);
    204   return next;
    205 }
    206 
    207 static struct gcov_fn_buffer **
    208 buffer_fn_data (const char *filename, const struct gcov_info *gi_ptr,
    209                 struct gcov_fn_buffer **end_ptr, unsigned fn_ix)
    210 {
    211   unsigned n_ctrs = 0, ix = 0;
    212   struct gcov_fn_buffer *fn_buffer;
    213   unsigned len;
    214 
    215   for (ix = GCOV_COUNTERS; ix--;)
    216     if (gi_ptr->merge[ix])
    217       n_ctrs++;
    218 
    219   len = sizeof (*fn_buffer) + sizeof (fn_buffer->info.ctrs[0]) * n_ctrs;
    220   fn_buffer = (struct gcov_fn_buffer *) xmalloc (len);
    221 
    222   if (!fn_buffer)
    223     goto fail;
    224 
    225   fn_buffer->next = 0;
    226   fn_buffer->fn_ix = fn_ix;
    227   fn_buffer->info.ident = gcov_read_unsigned ();
    228   fn_buffer->info.lineno_checksum = gcov_read_unsigned ();
    229   fn_buffer->info.cfg_checksum = gcov_read_unsigned ();
    230 
    231   for (n_ctrs = ix = 0; ix != GCOV_COUNTERS; ix++)
    232     {
    233       gcov_unsigned_t length;
    234       gcov_type *values;
    235 
    236       if (!gi_ptr->merge[ix])
    237         continue;
    238 
    239       if (gcov_read_unsigned () != GCOV_TAG_FOR_COUNTER (ix))
    240         {
    241           len = 0;
    242           goto fail;
    243         }
    244 
    245       length = GCOV_TAG_COUNTER_NUM (gcov_read_unsigned ());
    246       len = length * sizeof (gcov_type);
    247       values = (gcov_type *) xmalloc (len);
    248       if (!values)
    249         goto fail;
    250 
    251       fn_buffer->info.ctrs[n_ctrs].num = length;
    252       fn_buffer->info.ctrs[n_ctrs].values = values;
    253 
    254       while (length--)
    255         *values++ = gcov_read_counter ();
    256       n_ctrs++;
    257     }
    258 
    259   *end_ptr = fn_buffer;
    260   return &fn_buffer->next;
    261 
    262 fail:
    263   gcov_error ("profiling:%s:Function %u %s %u \n", filename, fn_ix,
    264               len ? "cannot allocate" : "counter mismatch", len ? len : ix);
    265 
    266   return (struct gcov_fn_buffer **)free_fn_data (gi_ptr, fn_buffer, ix);
    267 }
    268 
    269 /* Determine whether a counter is active.  */
    270 
    271 static inline int
    272 gcov_counter_active (const struct gcov_info *info, unsigned int type)
    273 {
    274   return (info->merge[type] != 0);
    275 }
    276 
    277 /* Add an unsigned value to the current crc */
    278 
    279 static gcov_unsigned_t
    280 crc32_unsigned (gcov_unsigned_t crc32, gcov_unsigned_t value)
    281 {
    282   unsigned ix;
    283 
    284   for (ix = 32; ix--; value <<= 1)
    285     {
    286       unsigned feedback;
    287 
    288       feedback = (value ^ crc32) & 0x80000000 ? 0x04c11db7 : 0;
    289       crc32 <<= 1;
    290       crc32 ^= feedback;
    291     }
    292 
    293   return crc32;
    294 }
    295 
    296 /* Check if VERSION of the info block PTR matches libgcov one.
    297    Return 1 on success, or zero in case of versions mismatch.
    298    If FILENAME is not NULL, its value used for reporting purposes
    299    instead of value from the info block.  */
    300 
    301 static int
    302 gcov_version (struct gcov_info *ptr, gcov_unsigned_t version,
    303               const char *filename)
    304 {
    305   if (version != GCOV_VERSION)
    306     {
    307       char v[4], e[4];
    308 
    309       GCOV_UNSIGNED2STRING (v, version);
    310       GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
    311 
    312       if (filename)
    313         gcov_error ("profiling:%s:Version mismatch - expected %.4s got %.4s\n",
    314                    filename? filename : ptr->filename, e, v);
    315       else
    316         gcov_error ("profiling:Version mismatch - expected %.4s got %.4s\n", e, v);
    317 
    318       return 0;
    319     }
    320   return 1;
    321 }
    322 
    323 /* Insert counter VALUE into HISTOGRAM.  */
    324 
    325 static void
    326 gcov_histogram_insert(gcov_bucket_type *histogram, gcov_type value)
    327 {
    328   unsigned i;
    329 
    330   i = gcov_histo_index(value);
    331   histogram[i].num_counters++;
    332   histogram[i].cum_value += value;
    333   if (value < histogram[i].min_value)
    334     histogram[i].min_value = value;
    335 }
    336 
    337 /* Computes a histogram of the arc counters to place in the summary SUM.  */
    338 
    339 static void
    340 gcov_compute_histogram (struct gcov_summary *sum)
    341 {
    342   struct gcov_info *gi_ptr;
    343   const struct gcov_fn_info *gfi_ptr;
    344   const struct gcov_ctr_info *ci_ptr;
    345   struct gcov_ctr_summary *cs_ptr;
    346   unsigned t_ix, f_ix, ctr_info_ix, ix;
    347   int h_ix;
    348 
    349   /* This currently only applies to arc counters.  */
    350   t_ix = GCOV_COUNTER_ARCS;
    351 
    352   /* First check if there are any counts recorded for this counter.  */
    353   cs_ptr = &(sum->ctrs[t_ix]);
    354   if (!cs_ptr->num)
    355     return;
    356 
    357   for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
    358     {
    359       cs_ptr->histogram[h_ix].num_counters = 0;
    360       cs_ptr->histogram[h_ix].min_value = cs_ptr->run_max;
    361       cs_ptr->histogram[h_ix].cum_value = 0;
    362     }
    363 
    364   /* Walk through all the per-object structures and record each of
    365      the count values in histogram.  */
    366   for (gi_ptr = __gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
    367     {
    368       if (!gi_ptr->merge[t_ix])
    369         continue;
    370 
    371       /* Find the appropriate index into the gcov_ctr_info array
    372          for the counter we are currently working on based on the
    373          existence of the merge function pointer for this object.  */
    374       for (ix = 0, ctr_info_ix = 0; ix < t_ix; ix++)
    375         {
    376           if (gi_ptr->merge[ix])
    377             ctr_info_ix++;
    378         }
    379       for (f_ix = 0; f_ix != gi_ptr->n_functions; f_ix++)
    380         {
    381           gfi_ptr = gi_ptr->functions[f_ix];
    382 
    383           if (!gfi_ptr || gfi_ptr->key != gi_ptr)
    384             continue;
    385 
    386           ci_ptr = &gfi_ptr->ctrs[ctr_info_ix];
    387           for (ix = 0; ix < ci_ptr->num; ix++)
    388             gcov_histogram_insert (cs_ptr->histogram, ci_ptr->values[ix]);
    389         }
    390     }
    391 }
    392 
    393 /* gcda filename.  */
    394 static char *gi_filename;
    395 /* buffer for the fn_data from another program.  */
    396 static struct gcov_fn_buffer *fn_buffer;
    397 /* buffer for summary from other programs to be written out. */
    398 static struct gcov_summary_buffer *sum_buffer;
    399 /* If application calls fork or exec multiple times, we end up storing
    400    profile repeadely.  We should not account this as multiple runs or
    401    functions executed once may mistakely become cold.  */
    402 static int run_accounted = 0;
    403 
    404 /* This funtions computes the program level summary and the histo-gram.
    405    It computes and returns CRC32 and stored summary in THIS_PRG.  */
    406 
    407 #if !IN_GCOV_TOOL
    408 static
    409 #endif
    410 gcov_unsigned_t
    411 gcov_exit_compute_summary (struct gcov_summary *this_prg)
    412 {
    413   struct gcov_info *gi_ptr;
    414   const struct gcov_fn_info *gfi_ptr;
    415   struct gcov_ctr_summary *cs_ptr;
    416   const struct gcov_ctr_info *ci_ptr;
    417   int f_ix;
    418   unsigned t_ix;
    419   gcov_unsigned_t c_num;
    420   gcov_unsigned_t crc32 = 0;
    421 
    422   /* Find the totals for this execution.  */
    423   memset (this_prg, 0, sizeof (*this_prg));
    424   for (gi_ptr = __gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
    425     {
    426       crc32 = crc32_unsigned (crc32, gi_ptr->stamp);
    427       crc32 = crc32_unsigned (crc32, gi_ptr->n_functions);
    428 
    429       for (f_ix = 0; (unsigned)f_ix != gi_ptr->n_functions; f_ix++)
    430         {
    431           gfi_ptr = gi_ptr->functions[f_ix];
    432 
    433           if (gfi_ptr && gfi_ptr->key != gi_ptr)
    434             gfi_ptr = 0;
    435 
    436           crc32 = crc32_unsigned (crc32, gfi_ptr ? gfi_ptr->cfg_checksum : 0);
    437           crc32 = crc32_unsigned (crc32,
    438                                   gfi_ptr ? gfi_ptr->lineno_checksum : 0);
    439           if (!gfi_ptr)
    440             continue;
    441 
    442           ci_ptr = gfi_ptr->ctrs;
    443           for (t_ix = 0; t_ix != GCOV_COUNTERS_SUMMABLE; t_ix++)
    444             {
    445               if (!gi_ptr->merge[t_ix])
    446                 continue;
    447 
    448               cs_ptr = &(this_prg->ctrs[t_ix]);
    449               cs_ptr->num += ci_ptr->num;
    450               crc32 = crc32_unsigned (crc32, ci_ptr->num);
    451 
    452               for (c_num = 0; c_num < ci_ptr->num; c_num++)
    453                 {
    454                   cs_ptr->sum_all += ci_ptr->values[c_num];
    455                   if (cs_ptr->run_max < ci_ptr->values[c_num])
    456                     cs_ptr->run_max = ci_ptr->values[c_num];
    457                 }
    458               ci_ptr++;
    459             }
    460         }
    461     }
    462   gcov_compute_histogram (this_prg);
    463   return crc32;
    464 }
    465 
    466 /* A struct that bundles all the related information about the
    467    gcda filename.  */
    468 struct gcov_filename_aux{
    469   char *gi_filename_up;
    470   int gcov_prefix_strip;
    471   size_t prefix_length;
    472 };
    473 
    474 /* Including system dependent components. */
    475 #if !defined (__KERNEL__)
    476 #include "libgcov-driver-system.c"
    477 #else
    478 #include "libgcov-driver-kernel.c"
    479 #endif
    480 
    481 static int
    482 scan_build_info (struct gcov_info *gi_ptr)
    483 {
    484   gcov_unsigned_t i, length;
    485   gcov_unsigned_t num_strings = 0;
    486   char **build_info_strings;
    487 
    488   length = gcov_read_unsigned ();
    489   build_info_strings = gcov_read_build_info (length, &num_strings);
    490   if (!build_info_strings)
    491     {
    492       gcov_error ("profiling:%s:Error reading build info\n", gi_filename);
    493       return -1;
    494     }
    495   if (!gi_ptr->build_info)
    496     {
    497       gcov_error ("profiling:%s:Mismatched build info sections, expected "
    498                   "none, found %u strings)\n", gi_filename, num_strings);
    499       return -1;
    500     }
    501 
    502   for (i = 0; i < num_strings; i++)
    503     {
    504       if (strcmp (build_info_strings[i], gi_ptr->build_info[i]))
    505         {
    506           gcov_error ("profiling:%s:Mismatched build info string "
    507                       "(expected %s, read %s)\n",
    508                       gi_filename, gi_ptr->build_info[i],
    509                       build_info_strings[i]);
    510           return -1;
    511         }
    512       xfree (build_info_strings[i]);
    513     }
    514   xfree (build_info_strings);
    515   return 0;
    516 }
    517 
    518 #if !defined(__KERNEL__)
    519 /* Scan through the current open gcda file corresponding to GI_PTR
    520    to locate the end position just before function data should be rewritten,
    521    returned in SUMMARY_END_POS_P. E.g. scan past the last summary and other
    522    sections that won't be rewritten, like the build info.  Return 0 on success,
    523    -1 on error.  */
    524 static int
    525 gcov_scan_to_function_data (struct gcov_info *gi_ptr,
    526                             gcov_position_t *summary_end_pos_p)
    527 {
    528   gcov_unsigned_t tag, version, stamp;
    529   tag = gcov_read_unsigned ();
    530   if (tag != GCOV_DATA_MAGIC)
    531     {
    532       gcov_error ("profiling:%s:Not a gcov data file\n", gi_filename);
    533       return -1;
    534     }
    535 
    536   version = gcov_read_unsigned ();
    537   if (!gcov_version (gi_ptr, version, gi_filename))
    538     return -1;
    539 
    540   stamp = gcov_read_unsigned ();
    541   if (stamp != gi_ptr->stamp)
    542     /* Read from a different compilation.  Overwrite the file.  */
    543     return -1;
    544 
    545   /* Look for program summary.  */
    546   while (1)
    547     {
    548       struct gcov_summary tmp;
    549 
    550       *summary_end_pos_p = gcov_position ();
    551       tag = gcov_read_unsigned ();
    552       if (tag != GCOV_TAG_PROGRAM_SUMMARY)
    553         break;
    554 
    555       gcov_read_unsigned ();
    556       gcov_read_summary (&tmp);
    557       if (gcov_is_error ())
    558         return -1;
    559     }
    560 
    561   /* If there is a build info section, scan past it as well.  */
    562   if (tag == GCOV_TAG_BUILD_INFO)
    563     {
    564       if (scan_build_info (gi_ptr) < 0)
    565         return -1;
    566 
    567       *summary_end_pos_p = gcov_position ();
    568       tag = gcov_read_unsigned ();
    569     }
    570   /* The next section should be the function counters.  */
    571   gcc_assert (tag == GCOV_TAG_FUNCTION);
    572 
    573   return 0;
    574 }
    575 #endif /* __KERNEL__ */
    576 
    577 /* This function merges counters in GI_PTR to an existing gcda file.
    578    Return 0 on success.
    579    Return -1 on error. In this case, caller will goto read_fatal.  */
    580 
    581 static int
    582 gcov_exit_merge_gcda (struct gcov_info *gi_ptr,
    583                       struct gcov_summary *prg_p,
    584                       struct gcov_summary *this_prg,
    585                       gcov_position_t *summary_pos_p,
    586                       gcov_position_t *eof_pos_p,
    587 		      gcov_unsigned_t crc32)
    588 {
    589   gcov_unsigned_t tag, length;
    590   unsigned t_ix;
    591   int f_ix;
    592   int error = 0;
    593   struct gcov_fn_buffer **fn_tail = &fn_buffer;
    594   struct gcov_summary_buffer **sum_tail = &sum_buffer;
    595   int *zero_fixup_flags = NULL;
    596 
    597   length = gcov_read_unsigned ();
    598   if (!gcov_version (gi_ptr, length, gi_filename))
    599     return -1;
    600 
    601   length = gcov_read_unsigned ();
    602   if (length != gi_ptr->stamp)
    603     /* Read from a different compilation. Overwrite the file.  */
    604     return 0;
    605 
    606   /* Look for program summary.  */
    607   for (f_ix = 0;;)
    608     {
    609       struct gcov_summary tmp;
    610 
    611       *eof_pos_p = gcov_position ();
    612       tag = gcov_read_unsigned ();
    613       if (tag != GCOV_TAG_PROGRAM_SUMMARY)
    614         break;
    615 
    616       f_ix--;
    617       length = gcov_read_unsigned ();
    618       gcov_read_summary (&tmp);
    619       if ((error = gcov_is_error ()))
    620         goto read_error;
    621       if (*summary_pos_p)
    622         {
    623           /* Save all summaries after the one that will be
    624              merged into below. These will need to be rewritten
    625              as histogram merging may change the number of non-zero
    626              histogram entries that will be emitted, and thus the
    627              size of the merged summary.  */
    628           (*sum_tail) = (struct gcov_summary_buffer *)
    629               xmalloc (sizeof(struct gcov_summary_buffer));
    630           (*sum_tail)->summary = tmp;
    631           (*sum_tail)->next = 0;
    632           sum_tail = &((*sum_tail)->next);
    633           goto next_summary;
    634         }
    635       if (tmp.checksum != crc32)
    636         goto next_summary;
    637 
    638       for (t_ix = 0; t_ix != GCOV_COUNTERS_SUMMABLE; t_ix++)
    639         if (tmp.ctrs[t_ix].num != this_prg->ctrs[t_ix].num)
    640           goto next_summary;
    641       *prg_p = tmp;
    642       *summary_pos_p = *eof_pos_p;
    643 
    644     next_summary:;
    645     }
    646 
    647   if (tag == GCOV_TAG_BUILD_INFO)
    648     {
    649       if (scan_build_info (gi_ptr) < 0)
    650         return -1;
    651 
    652       /* Since the stamps matched if we got here, this should be from
    653          the same compilation and the build info strings should match.  */
    654       tag = gcov_read_unsigned ();
    655     }
    656 
    657   if (tag == GCOV_TAG_COMDAT_ZERO_FIXUP)
    658     {
    659       gcov_unsigned_t num_fns = 0;
    660       length = gcov_read_unsigned ();
    661       zero_fixup_flags = gcov_read_comdat_zero_fixup (length, &num_fns);
    662       if (!zero_fixup_flags)
    663         {
    664           gcov_error ("profiling:%s:Error reading zero fixup flags\n",
    665                       gi_filename);
    666           return -1;
    667         }
    668 
    669       tag = gcov_read_unsigned ();
    670     }
    671 
    672   /* Merge execution counts for each function.  */
    673   for (f_ix = 0; (unsigned)f_ix != gi_ptr->n_functions;
    674        f_ix++, tag = gcov_read_unsigned ())
    675     {
    676       const struct gcov_ctr_info *ci_ptr;
    677       const struct gcov_fn_info *gfi_ptr = gi_ptr->functions[f_ix];
    678 
    679       if (tag != GCOV_TAG_FUNCTION)
    680         goto read_mismatch;
    681 
    682       length = gcov_read_unsigned ();
    683       if (!length)
    684         /* This function did not appear in the other program.
    685            We have nothing to merge.  */
    686         continue;
    687 
    688       if (length != GCOV_TAG_FUNCTION_LENGTH)
    689         goto read_mismatch;
    690 
    691       if (!gfi_ptr || gfi_ptr->key != gi_ptr)
    692         {
    693           /* This function appears in the other program.  We
    694              need to buffer the information in order to write
    695              it back out -- we'll be inserting data before
    696              this point, so cannot simply keep the data in the
    697              file.  */
    698           fn_tail = buffer_fn_data (gi_filename,
    699                                     gi_ptr, fn_tail, f_ix);
    700           if (!fn_tail)
    701             goto read_mismatch;
    702           continue;
    703         }
    704 
    705       if (zero_fixup_flags)
    706         set_gcov_fn_fixed_up (zero_fixup_flags[f_ix]);
    707 
    708       length = gcov_read_unsigned ();
    709       if (length != gfi_ptr->ident)
    710         goto read_mismatch;
    711 
    712       length = gcov_read_unsigned ();
    713       if (length != gfi_ptr->lineno_checksum)
    714         goto read_mismatch;
    715 
    716       length = gcov_read_unsigned ();
    717       if (length != gfi_ptr->cfg_checksum)
    718         goto read_mismatch;
    719 
    720       ci_ptr = gfi_ptr->ctrs;
    721       for (t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
    722         {
    723           gcov_merge_fn merge = gi_ptr->merge[t_ix];
    724 
    725           if (!merge)
    726             continue;
    727 
    728           tag = gcov_read_unsigned ();
    729           length = gcov_read_unsigned ();
    730           if (tag != GCOV_TAG_FOR_COUNTER (t_ix)
    731               || length != GCOV_TAG_COUNTER_LENGTH (ci_ptr->num))
    732             goto read_mismatch;
    733           (*merge) (ci_ptr->values, ci_ptr->num);
    734           ci_ptr++;
    735         }
    736       if ((error = gcov_is_error ()))
    737         goto read_error;
    738     }
    739   xfree (zero_fixup_flags);
    740 
    741   if (tag && tag != GCOV_TAG_MODULE_INFO)
    742     {
    743     read_mismatch:;
    744       gcov_error ("profiling:%s:Merge mismatch for %s %u\n",
    745                   gi_filename, f_ix >= 0 ? "function" : "summary",
    746                   f_ix < 0 ? -1 - f_ix : f_ix);
    747       return -1;
    748     }
    749   return 0;
    750 
    751 read_error:
    752   gcov_error ("profiling:%s:%s merging\n", gi_filename,
    753               error < 0 ? "Overflow": "Error");
    754   return -1;
    755 }
    756 
    757 #if !defined(__KERNEL__)
    758 /* Write NUM_FNS ZERO_COUNTS fixup flags to a gcda file starting from its
    759    current location.  */
    760 
    761 static void
    762 gcov_write_comdat_zero_fixup (char *zero_counts, unsigned num_fns)
    763 {
    764   unsigned f_ix;
    765   gcov_unsigned_t len = GCOV_TAG_COMDAT_ZERO_FIXUP_LENGTH (num_fns);
    766   gcov_unsigned_t bitvector = 0, b_ix = 0;
    767   gcov_write_tag_length (GCOV_TAG_COMDAT_ZERO_FIXUP, len);
    768 
    769   gcov_write_unsigned (num_fns);
    770   for (f_ix = 0; f_ix != num_fns; f_ix++)
    771     {
    772       if (zero_counts[f_ix])
    773         bitvector |= 1 << b_ix;
    774       if (++b_ix == 32)
    775         {
    776           gcov_write_unsigned (bitvector);
    777           b_ix = 0;
    778           bitvector = 0;
    779         }
    780     }
    781   if (b_ix > 0)
    782     gcov_write_unsigned (bitvector);
    783 }
    784 #endif /* __KERNEL__ */
    785 
    786 /* Write build_info strings from GI_PTR to a gcda file starting from its current
    787    location.  */
    788 
    789 static void
    790 gcov_write_build_info (struct gcov_info *gi_ptr)
    791 {
    792   gcov_unsigned_t num = 0;
    793   gcov_unsigned_t len = 1;
    794 
    795   if (!gi_ptr->build_info)
    796     return;
    797 
    798   /* Count the number of strings, which is terminated with an empty string.  */
    799   while (gi_ptr->build_info[num][0])
    800     num++;
    801 
    802   len += gcov_compute_string_array_len (gi_ptr->build_info, num);
    803   gcov_write_tag_length (GCOV_TAG_BUILD_INFO, len);
    804   gcov_write_unsigned (num);
    805   gcov_write_string_array (gi_ptr->build_info, num);
    806 }
    807 
    808 /* Write counters in GI_PTR to a gcda file starting from its current
    809    location.  */
    810 
    811 static void
    812 gcov_write_func_counters (struct gcov_info *gi_ptr)
    813 {
    814   unsigned f_ix;
    815 
    816   /* Write execution counts for each function.  */
    817   for (f_ix = 0; f_ix != gi_ptr->n_functions; f_ix++)
    818     {
    819       unsigned buffered = 0;
    820       const struct gcov_fn_info *gfi_ptr;
    821       const struct gcov_ctr_info *ci_ptr;
    822       gcov_unsigned_t length;
    823       unsigned t_ix;
    824 
    825       if (fn_buffer && fn_buffer->fn_ix == f_ix)
    826         {
    827           /* Buffered data from another program.  */
    828           buffered = 1;
    829           gfi_ptr = &fn_buffer->info;
    830           length = GCOV_TAG_FUNCTION_LENGTH;
    831         }
    832       else
    833         {
    834           gfi_ptr = gi_ptr->functions[f_ix];
    835           if (gfi_ptr && gfi_ptr->key == gi_ptr)
    836             length = GCOV_TAG_FUNCTION_LENGTH;
    837           else
    838             length = 0;
    839         }
    840 
    841       gcov_write_tag_length (GCOV_TAG_FUNCTION, length);
    842       if (!length)
    843         continue;
    844 
    845       gcov_write_unsigned (gfi_ptr->ident);
    846       gcov_write_unsigned (gfi_ptr->lineno_checksum);
    847       gcov_write_unsigned (gfi_ptr->cfg_checksum);
    848 
    849       ci_ptr = gfi_ptr->ctrs;
    850       for (t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
    851         {
    852           gcov_unsigned_t n_counts;
    853           gcov_type *c_ptr;
    854 
    855           if (!gi_ptr->merge[t_ix])
    856             continue;
    857 
    858           n_counts = ci_ptr->num;
    859           gcov_write_tag_length (GCOV_TAG_FOR_COUNTER (t_ix),
    860                                  GCOV_TAG_COUNTER_LENGTH (n_counts));
    861           c_ptr = ci_ptr->values;
    862           while (n_counts--)
    863             gcov_write_counter (*c_ptr++);
    864           ci_ptr++;
    865         }
    866 #if !defined(__KERNEL__)
    867       if (buffered)
    868         fn_buffer = free_fn_data (gi_ptr, fn_buffer, GCOV_COUNTERS);
    869 #endif /* __KERNEL__ */
    870     }
    871 
    872   gi_ptr->eof_pos = gcov_position ();
    873   gcov_write_unsigned (0);
    874 }
    875 
    876 /* Write counters in GI_PTR and the summary in PRG to a gcda file.  In
    877    the case of appending to an existing file, SUMMARY_POS will be non-zero.
    878    We will write the file starting from SUMMAY_POS.  */
    879 
    880 static void
    881 gcov_exit_write_gcda (struct gcov_info *gi_ptr,
    882                       const struct gcov_summary *prg_p,
    883                       const gcov_position_t eof_pos,
    884                       const gcov_position_t summary_pos)
    885 
    886 {
    887   struct gcov_summary_buffer *next_sum_buffer;
    888 
    889   /* Write out the data.  */
    890   if (!eof_pos)
    891     {
    892       gcov_write_tag_length (GCOV_DATA_MAGIC, GCOV_VERSION);
    893       gcov_write_unsigned (gi_ptr->stamp);
    894     }
    895 
    896   if (summary_pos)
    897      gcov_seek (summary_pos);
    898   gcc_assert (!summary_pos || summary_pos == gcov_position ());
    899 
    900   /* Generate whole program statistics.  */
    901   gcov_write_summary (GCOV_TAG_PROGRAM_SUMMARY, prg_p);
    902 
    903   /* Rewrite all the summaries that were after the summary we merged
    904      into.  This is necessary as the merged summary may have a different
    905      size due to the number of non-zero histogram entries changing after
    906      merging.  */
    907 
    908   while (sum_buffer)
    909     {
    910       gcov_write_summary (GCOV_TAG_PROGRAM_SUMMARY, &sum_buffer->summary);
    911       next_sum_buffer = sum_buffer->next;
    912       xfree (sum_buffer);
    913       sum_buffer = next_sum_buffer;
    914     }
    915 
    916   gcov_write_build_info (gi_ptr);
    917 
    918   /* Write the counters.  */
    919   gcov_write_func_counters (gi_ptr);
    920 }
    921 
    922 /* Helper function for merging summary.
    923    Return -1 on error. Return 0 on success.  */
    924 
    925 static int
    926 gcov_exit_merge_summary (const struct gcov_info *gi_ptr, struct gcov_summary *prg,
    927                          struct gcov_summary *this_prg, gcov_unsigned_t crc32,
    928 			 struct gcov_summary *all_prg __attribute__ ((unused)))
    929 {
    930   struct gcov_ctr_summary *cs_prg, *cs_tprg;
    931   unsigned t_ix;
    932 #if !GCOV_LOCKED
    933   /* summary for all instances of program.  */
    934   struct gcov_ctr_summary *cs_all;
    935 #endif
    936 
    937   /* Merge the summaries.  */
    938   for (t_ix = 0; t_ix < GCOV_COUNTERS_SUMMABLE; t_ix++)
    939     {
    940       cs_prg = &(prg->ctrs[t_ix]);
    941       cs_tprg = &(this_prg->ctrs[t_ix]);
    942 
    943       if (gi_ptr->merge[t_ix])
    944         {
    945 	  int first = !cs_prg->runs;
    946 
    947 	  if (!run_accounted)
    948 	    cs_prg->runs++;
    949           if (first)
    950             cs_prg->num = cs_tprg->num;
    951           cs_prg->sum_all += cs_tprg->sum_all;
    952           if (cs_prg->run_max < cs_tprg->run_max)
    953             cs_prg->run_max = cs_tprg->run_max;
    954           cs_prg->sum_max += cs_tprg->run_max;
    955           if (first)
    956             memcpy (cs_prg->histogram, cs_tprg->histogram,
    957                    sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
    958           else
    959             gcov_histogram_merge (cs_prg->histogram, cs_tprg->histogram);
    960         }
    961       else if (cs_prg->runs)
    962         {
    963           gcov_error ("profiling:%s:Merge mismatch for summary.\n",
    964                       gi_filename);
    965           return -1;
    966         }
    967 #if !GCOV_LOCKED
    968       cs_all = &all_prg->ctrs[t_ix];
    969       if (!cs_all->runs && cs_prg->runs)
    970         {
    971           cs_all->num = cs_prg->num;
    972           cs_all->runs = cs_prg->runs;
    973           cs_all->sum_all = cs_prg->sum_all;
    974           cs_all->run_max = cs_prg->run_max;
    975           cs_all->sum_max = cs_prg->sum_max;
    976         }
    977       else if (!all_prg->checksum
    978                /* Don't compare the histograms, which may have slight
    979                   variations depending on the order they were updated
    980                   due to the truncating integer divides used in the
    981                   merge.  */
    982                && (cs_all->num != cs_prg->num
    983                    || cs_all->runs != cs_prg->runs
    984                    || cs_all->sum_all != cs_prg->sum_all
    985                    || cs_all->run_max != cs_prg->run_max
    986                    || cs_all->sum_max != cs_prg->sum_max))
    987              {
    988                gcov_error ("profiling:%s:Data file mismatch - some "
    989                            "data files may have been concurrently "
    990                            "updated without locking support\n", gi_filename);
    991                all_prg->checksum = ~0u;
    992              }
    993 #endif
    994     }
    995 
    996   prg->checksum = crc32;
    997 
    998   return 0;
    999 }
   1000 
   1001 __attribute__((weak)) gcov_unsigned_t __gcov_lipo_sampling_period;
   1002 
   1003 /* Sort N entries in VALUE_ARRAY in descending order.
   1004    Each entry in VALUE_ARRAY has two values. The sorting
   1005    is based on the second value.  */
   1006 
   1007 GCOV_LINKAGE  void
   1008 gcov_sort_n_vals (gcov_type *value_array, int n)
   1009 {
   1010   int j, k;
   1011   for (j = 2; j < n; j += 2)
   1012     {
   1013       gcov_type cur_ent[2];
   1014       cur_ent[0] = value_array[j];
   1015       cur_ent[1] = value_array[j + 1];
   1016       k = j - 2;
   1017       while (k >= 0 && value_array[k + 1] < cur_ent[1])
   1018         {
   1019           value_array[k + 2] = value_array[k];
   1020           value_array[k + 3] = value_array[k+1];
   1021           k -= 2;
   1022         }
   1023       value_array[k + 2] = cur_ent[0];
   1024       value_array[k + 3] = cur_ent[1];
   1025     }
   1026 }
   1027 
   1028 /* Sort the profile counters for all indirect call sites. Counters
   1029    for each call site are allocated in array COUNTERS.  */
   1030 
   1031 static void
   1032 gcov_sort_icall_topn_counter (const struct gcov_ctr_info *counters)
   1033 {
   1034   int i;
   1035   gcov_type *values;
   1036   int n = counters->num;
   1037   gcc_assert (!(n % GCOV_ICALL_TOPN_NCOUNTS));
   1038 
   1039   values = counters->values;
   1040 
   1041   for (i = 0; i < n; i += GCOV_ICALL_TOPN_NCOUNTS)
   1042     {
   1043       gcov_type *value_array = &values[i + 1];
   1044       gcov_sort_n_vals (value_array, GCOV_ICALL_TOPN_NCOUNTS - 1);
   1045     }
   1046 }
   1047 
   1048 static void
   1049 gcov_sort_topn_counter_arrays (const struct gcov_info *gi_ptr)
   1050 {
   1051   unsigned int i;
   1052   int f_ix;
   1053   const struct gcov_fn_info *gfi_ptr;
   1054   const struct gcov_ctr_info *ci_ptr;
   1055 
   1056   for (f_ix = 0; (unsigned)f_ix != gi_ptr->n_functions; f_ix++)
   1057     {
   1058       gfi_ptr = gi_ptr->functions[f_ix];
   1059       ci_ptr = gfi_ptr->ctrs;
   1060       for (i = 0; i < GCOV_COUNTERS; i++)
   1061         {
   1062           if (!gcov_counter_active (gi_ptr, i))
   1063             continue;
   1064           if (i == GCOV_COUNTER_ICALL_TOPNV)
   1065             {
   1066               gcov_sort_icall_topn_counter (ci_ptr);
   1067               break;
   1068             }
   1069           ci_ptr++;
   1070         }
   1071      }
   1072 }
   1073 
   1074 /* Scaling LIPO sampled profile counters.  */
   1075 static void
   1076 gcov_scaling_lipo_counters (const struct gcov_info *gi_ptr)
   1077 {
   1078   unsigned int i,j,k;
   1079   int f_ix;
   1080   const struct gcov_fn_info *gfi_ptr;
   1081   const struct gcov_ctr_info *ci_ptr;
   1082 
   1083   if (__gcov_lipo_sampling_period <= 1)
   1084     return;
   1085 
   1086   for (f_ix = 0; (unsigned)f_ix != gi_ptr->n_functions; f_ix++)
   1087     {
   1088       gfi_ptr = gi_ptr->functions[f_ix];
   1089       ci_ptr = gfi_ptr->ctrs;
   1090       for (i = 0; i < GCOV_COUNTERS; i++)
   1091         {
   1092           if (!gcov_counter_active (gi_ptr, i))
   1093             continue;
   1094           if (i == GCOV_COUNTER_ICALL_TOPNV)
   1095             {
   1096               for (j = 0; j < ci_ptr->num; j += GCOV_ICALL_TOPN_NCOUNTS)
   1097                 for (k = 2; k < GCOV_ICALL_TOPN_NCOUNTS; k += 2)
   1098                   ci_ptr->values[j+k] *= __gcov_lipo_sampling_period;
   1099             }
   1100           if (i == GCOV_COUNTER_DIRECT_CALL)
   1101             {
   1102               for (j = 0; j < ci_ptr->num; j += 2)
   1103                 ci_ptr->values[j+1] *= __gcov_lipo_sampling_period;
   1104             }
   1105           ci_ptr++;
   1106         }
   1107     }
   1108 }
   1109 
   1110 /* Open a gcda file specified by GI_FILENAME.
   1111    Return -1 on error.  Return 0 on success.  */
   1112 
   1113 static int
   1114 gcov_exit_open_gcda_file (struct gcov_info *gi_ptr, struct gcov_filename_aux *gf)
   1115 {
   1116   int gcov_prefix_strip;
   1117   size_t prefix_length;
   1118   char *gi_filename_up;
   1119 
   1120   gcov_prefix_strip = gf->gcov_prefix_strip;
   1121   gi_filename_up = gf->gi_filename_up;
   1122   prefix_length = gf->prefix_length;
   1123 
   1124   gcov_strip_leading_dirs (prefix_length, gcov_prefix_strip, gi_ptr->filename,
   1125                            gi_filename_up);
   1126 
   1127   return gcov_open_by_filename (gi_filename);
   1128 }
   1129 
   1130 /* Dump the coverage counts for one gcov_info object. We merge with existing
   1131    counts when possible, to avoid growing the .da files ad infinitum. We use
   1132    this program's checksum to make sure we only accumulate whole program
   1133    statistics to the correct summary. An object file might be embedded
   1134    in two separate programs, and we must keep the two program
   1135    summaries separate.  */
   1136 
   1137 static void
   1138 gcov_exit_dump_gcov (struct gcov_info *gi_ptr, struct gcov_filename_aux *gf,
   1139 		     gcov_unsigned_t crc32, struct gcov_summary *all_prg,
   1140                      struct gcov_summary *this_prg)
   1141 {
   1142 /* We have to make the decl static as kernel has limited stack size.
   1143    If we put prg to stack, we will running into nasty stack overflow.  */
   1144 #if defined(__KERNEL__)
   1145   static
   1146 #endif
   1147   struct gcov_summary prg; /* summary for this object over all program.  */
   1148   int error;
   1149   gcov_unsigned_t tag = 0;
   1150   gcov_position_t summary_pos = 0;
   1151   gcov_position_t eof_pos = 0;
   1152 
   1153   fn_buffer = 0;
   1154   sum_buffer = 0;
   1155 
   1156   gcov_sort_topn_counter_arrays (gi_ptr);
   1157   gcov_scaling_lipo_counters (gi_ptr);
   1158 
   1159   error = gcov_exit_open_gcda_file (gi_ptr, gf);
   1160   if (error == -1)
   1161     return;
   1162 
   1163 #if !defined(__KERNEL__)
   1164   tag = gcov_read_unsigned ();
   1165 #endif
   1166   if (tag)
   1167     {
   1168       /* Merge data from file.  */
   1169       if (tag != GCOV_DATA_MAGIC)
   1170         {
   1171           gcov_error ("profiling:%s:Not a gcov data file\n", gi_filename);
   1172           goto read_fatal;
   1173         }
   1174       error = gcov_exit_merge_gcda (gi_ptr, &prg, this_prg, &summary_pos, &eof_pos,
   1175 				    crc32);
   1176       if (error == -1)
   1177         goto read_fatal;
   1178     }
   1179 
   1180   gcov_rewrite ();
   1181 
   1182   if (!summary_pos)
   1183     {
   1184       memset (&prg, 0, sizeof (prg));
   1185       summary_pos = eof_pos;
   1186     }
   1187 
   1188   error = gcov_exit_merge_summary (gi_ptr, &prg, this_prg, crc32, all_prg);
   1189   if (error == -1)
   1190     goto read_fatal;
   1191 
   1192   gcov_exit_write_gcda (gi_ptr, &prg, eof_pos, summary_pos);
   1193   /* fall through */
   1194 
   1195 read_fatal:;
   1196 #if !defined(__KERNEL__)
   1197   while (fn_buffer)
   1198     fn_buffer = free_fn_data (gi_ptr, fn_buffer, GCOV_COUNTERS);
   1199 #else
   1200 
   1201       /* In LIPO mode, dump the primary module info.  */
   1202       if (gi_ptr->mod_info && gi_ptr->mod_info->is_primary)
   1203         {
   1204           /* Overwrite the zero word at the of the file.  */
   1205           gcov_seek (gi_ptr->eof_pos);
   1206           gcov_write_module_info (gi_ptr, 1);
   1207           /* Write the end marker  */
   1208           gcov_write_unsigned (0);
   1209         }
   1210 #endif
   1211 
   1212   if ((error = gcov_close ()))
   1213     gcov_error (error  < 0 ?
   1214                 "profiling:%s:Overflow writing\n" :
   1215                 "profiling:%s:Error writing\n",
   1216                 gi_filename);
   1217 }
   1218 
   1219 #if !defined (__KERNEL__)
   1220 /* Write imported files (auxiliary modules) for primary module GI_PTR
   1221    into file GI_FILENAME.  */
   1222 
   1223 static void
   1224 gcov_write_import_file (char *gi_filename, struct gcov_info *gi_ptr)
   1225 {
   1226   char  *gi_imports_filename;
   1227   const char *gcov_suffix;
   1228   FILE *imports_file;
   1229   size_t prefix_length, suffix_length;
   1230 
   1231   gcov_suffix = getenv ("GCOV_IMPORTS_SUFFIX");
   1232   if (!gcov_suffix || !strlen (gcov_suffix))
   1233     gcov_suffix = ".imports";
   1234   suffix_length = strlen (gcov_suffix);
   1235   prefix_length = strlen (gi_filename);
   1236   gi_imports_filename = (char *) alloca (prefix_length + suffix_length + 1);
   1237   memset (gi_imports_filename, 0, prefix_length + suffix_length + 1);
   1238   memcpy (gi_imports_filename, gi_filename, prefix_length);
   1239   memcpy (gi_imports_filename + prefix_length, gcov_suffix, suffix_length);
   1240   imports_file = fopen (gi_imports_filename, "w");
   1241   if (imports_file)
   1242     {
   1243       const struct dyn_imp_mod **imp_mods;
   1244       unsigned i, imp_len;
   1245       imp_mods = gcov_get_sorted_import_module_array (gi_ptr, &imp_len);
   1246       if (imp_mods)
   1247         {
   1248           for (i = 0; i < imp_len; i++)
   1249             {
   1250               fprintf (imports_file, "%s\n",
   1251                        imp_mods[i]->imp_mod->mod_info->source_filename);
   1252               fprintf (imports_file, "%s%s\n",
   1253                        imp_mods[i]->imp_mod->mod_info->da_filename, GCOV_DATA_SUFFIX);
   1254             }
   1255           xfree (imp_mods);
   1256         }
   1257       fclose (imports_file);
   1258     }
   1259 }
   1260 
   1261 static void
   1262 gcov_dump_module_info (struct gcov_filename_aux *gf)
   1263 {
   1264   struct gcov_info *gi_ptr;
   1265 
   1266   unsigned max_module_id = 0;
   1267   for (gi_ptr = __gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
   1268     {
   1269       unsigned mod_id = gi_ptr->mod_info->ident;
   1270       if (max_module_id < mod_id)
   1271         max_module_id = mod_id;
   1272     }
   1273   char **zero_counts = (char **) xcalloc (max_module_id, sizeof (char *));
   1274   for (gi_ptr = __gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
   1275     {
   1276       unsigned mod_id = gi_ptr->mod_info->ident;
   1277       zero_counts[mod_id-1] = (char *) xcalloc (gi_ptr->n_functions,
   1278                                                sizeof (char));
   1279     }
   1280 
   1281   /* Compute the module groups and record whether there were any
   1282      counter fixups applied that require rewriting the counters.  */
   1283   int changed = __gcov_compute_module_groups (zero_counts);
   1284 
   1285   /* Now write out module group info.  */
   1286   for (gi_ptr = __gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
   1287     {
   1288       int error;
   1289 
   1290       if (gcov_exit_open_gcda_file (gi_ptr, gf) == -1)
   1291 	continue;
   1292 
   1293       if (changed)
   1294         {
   1295           /* Scan file to find the start of the function section, which is
   1296              where we will start re-writing the counters.  */
   1297           gcov_position_t summary_end_pos;
   1298           if (gcov_scan_to_function_data (gi_ptr, &summary_end_pos) == -1)
   1299             gcov_error ("profiling:%s:Error scanning summaries\n",
   1300                         gi_filename);
   1301           else
   1302             {
   1303               gcov_position_t eof_pos = gi_ptr->eof_pos;
   1304               gcov_rewrite ();
   1305               gcov_seek (summary_end_pos);
   1306 
   1307               unsigned mod_id = gi_ptr->mod_info->ident;
   1308               gcov_write_comdat_zero_fixup (zero_counts[mod_id-1],
   1309                                             gi_ptr->n_functions);
   1310               gcov_position_t zero_fixup_eof_pos = gcov_position ();
   1311 
   1312               gcov_write_func_counters (gi_ptr);
   1313               gcc_assert (eof_pos + (zero_fixup_eof_pos - summary_end_pos)
   1314                           == gi_ptr->eof_pos);
   1315             }
   1316         }
   1317       else
   1318         gcov_rewrite ();
   1319 
   1320       /* Overwrite the zero word at the of the file.  */
   1321       gcov_seek (gi_ptr->eof_pos);
   1322 
   1323       gcov_write_module_infos (gi_ptr);
   1324       /* Write the end marker  */
   1325       gcov_write_unsigned (0);
   1326       gcov_truncate ();
   1327 
   1328       if ((error = gcov_close ()))
   1329         gcov_error (error  < 0 ?  "profiling:%s:Overflow writing\n" :
   1330                                   "profiling:%s:Error writing\n",
   1331                                   gi_filename);
   1332       gcov_write_import_file (gi_filename, gi_ptr);
   1333       free (zero_counts[gi_ptr->mod_info->ident-1]);
   1334     }
   1335 
   1336   free (zero_counts);
   1337 
   1338   __gcov_finalize_dyn_callgraph ();
   1339 }
   1340 
   1341 /* Dump all the coverage counts for the program. It first computes program
   1342    summary and then traverses gcov_list list and dumps the gcov_info
   1343    objects one by one.  */
   1344 
   1345 void
   1346 gcov_exit (void)
   1347 {
   1348   struct gcov_info *gi_ptr;
   1349   struct gcov_filename_aux gf;
   1350   gcov_unsigned_t crc32;
   1351   int dump_module_info = 0;
   1352   struct gcov_summary all_prg;
   1353   struct gcov_summary this_prg;
   1354 
   1355   /* Prevent the counters from being dumped a second time on exit when the
   1356      application already wrote out the profile using __gcov_dump().  */
   1357   if (gcov_dump_complete)
   1358     return;
   1359 
   1360   crc32 = gcov_exit_compute_summary (&this_prg);
   1361 
   1362   allocate_filename_struct (&gf);
   1363 #if !GCOV_LOCKED
   1364   memset (&all_prg, 0, sizeof (all_prg));
   1365 #endif
   1366 
   1367   /* Now merge each file.  */
   1368   for (gi_ptr = __gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
   1369     {
   1370       gcov_exit_dump_gcov (gi_ptr, &gf, crc32, &all_prg, &this_prg);
   1371 
   1372       /* The IS_PRIMARY field is overloaded to indicate if this module
   1373        is FDO/LIPO.  */
   1374       if (gi_ptr->mod_info)
   1375         dump_module_info |= gi_ptr->mod_info->is_primary;
   1376     }
   1377   run_accounted = 1;
   1378 
   1379   if (dump_module_info)
   1380     gcov_dump_module_info (&gf);
   1381 
   1382   if (gi_filename)
   1383     xfree (gi_filename);
   1384 }
   1385 
   1386 /* Add a new object file onto the bb chain.  Invoked automatically
   1387   when running an object file's global ctors.  */
   1388 
   1389 void
   1390 __gcov_init (struct gcov_info *info)
   1391 {
   1392 #ifndef IN_GCOV_TOOL
   1393    if (!gcov_sampling_period_initialized)
   1394     {
   1395       const char* env_value_str = getenv ("GCOV_SAMPLING_PERIOD");
   1396       if (env_value_str)
   1397         {
   1398           int env_value_int = atoi(env_value_str);
   1399           if (env_value_int >= 1)
   1400             __gcov_sampling_period = env_value_int;
   1401         }
   1402       env_value_str = getenv ("GCOV_LIPO_SAMPLING_PERIOD");
   1403       if (env_value_str)
   1404         {
   1405           int env_value_int = atoi(env_value_str);
   1406           if (env_value_int >= 0)
   1407             __gcov_lipo_sampling_period = env_value_int;
   1408         }
   1409       gcov_sampling_period_initialized = 1;
   1410     }
   1411 #endif
   1412 
   1413   if (!info->version || !info->n_functions)
   1414     return;
   1415   if (gcov_version (info, info->version, 0))
   1416     {
   1417       size_t filename_length = strlen(info->filename);
   1418 
   1419       /* Refresh the longest file name information */
   1420       if (filename_length > gcov_max_filename)
   1421         gcov_max_filename = filename_length;
   1422 
   1423       /* Assign the module ID (starting at 1).  */
   1424       info->mod_info->ident = (++gcov_cur_module_id);
   1425       gcc_assert (EXTRACT_MODULE_ID_FROM_GLOBAL_ID (GEN_FUNC_GLOBAL_ID (
   1426                                                        info->mod_info->ident, 0))
   1427                   == info->mod_info->ident);
   1428 
   1429       if (!__gcov_list)
   1430         atexit (gcov_exit);
   1431 
   1432       info->next = __gcov_list;
   1433       __gcov_list = info;
   1434     }
   1435   info->version = 0;
   1436 }
   1437 
   1438 #else /* __KERNEL__ */
   1439 
   1440 static struct gcov_filename_aux gf;
   1441 static gcov_unsigned_t crc32;
   1442 static struct gcov_summary all_prg;
   1443 static struct gcov_summary this_prg;
   1444 void
   1445 gcov_kernel_dump_gcov_init (void)
   1446 {
   1447   crc32 = gcov_exit_compute_summary (&this_prg);
   1448   allocate_filename_struct (&gf);
   1449   memset (&all_prg, 0, sizeof (all_prg));
   1450 }
   1451 
   1452 void
   1453 gcov_kernel_dump_one_gcov(struct gcov_info *info)
   1454 {
   1455   gcov_exit_dump_gcov (info, &gf, crc32, &all_prg, &this_prg);
   1456 }
   1457 
   1458 #endif /* __KERNEL__ */
   1459 
   1460 /* Reset all counters to zero.  */
   1461 
   1462 void
   1463 gcov_clear (void)
   1464 {
   1465   const struct gcov_info *gi_ptr;
   1466 
   1467   for (gi_ptr = __gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
   1468     {
   1469       unsigned f_ix;
   1470 
   1471       for (f_ix = 0; f_ix < gi_ptr->n_functions; f_ix++)
   1472         {
   1473           unsigned t_ix;
   1474           const struct gcov_fn_info *gfi_ptr = gi_ptr->functions[f_ix];
   1475           const struct gcov_ctr_info *ci_ptr;
   1476 
   1477           if (!gfi_ptr || gfi_ptr->key != gi_ptr)
   1478             continue;
   1479           ci_ptr = gfi_ptr->ctrs;
   1480           for (t_ix = 0; t_ix != GCOV_COUNTERS; t_ix++)
   1481             {
   1482               if (!gi_ptr->merge[t_ix])
   1483                 continue;
   1484 
   1485               memset (ci_ptr->values, 0, sizeof (gcov_type) * ci_ptr->num);
   1486               ci_ptr++;
   1487             }
   1488         }
   1489     }
   1490 }
   1491 
   1492 /* Write out MOD_INFO into the gcda file. IS_PRIMARY is a flag
   1493    indicating if the module is the primary module in the group.  */
   1494 
   1495 void
   1496 gcov_write_module_info (const struct gcov_info *mod_info,
   1497                         unsigned is_primary)
   1498 {
   1499   gcov_unsigned_t len = 0, filename_len = 0, src_filename_len = 0, i;
   1500   gcov_unsigned_t num_strings;
   1501   gcov_unsigned_t *aligned_fname;
   1502   struct gcov_module_info  *module_info = mod_info->mod_info;
   1503   filename_len = (strlen (module_info->da_filename) +
   1504 		  sizeof (gcov_unsigned_t)) / sizeof (gcov_unsigned_t);
   1505   src_filename_len = (strlen (module_info->source_filename) +
   1506 		      sizeof (gcov_unsigned_t)) / sizeof (gcov_unsigned_t);
   1507   len = filename_len + src_filename_len;
   1508   len += 2; /* each name string is led by a length.  */
   1509 
   1510   num_strings = module_info->num_quote_paths + module_info->num_bracket_paths
   1511     + module_info->num_system_paths
   1512     + module_info->num_cpp_defines + module_info->num_cpp_includes
   1513     + module_info->num_cl_args;
   1514   len += gcov_compute_string_array_len (module_info->string_array,
   1515                                         num_strings);
   1516 
   1517   len += 11; /* 11 more fields */
   1518 
   1519   gcov_write_tag_length (GCOV_TAG_MODULE_INFO, len);
   1520   gcov_write_unsigned (module_info->ident);
   1521   gcov_write_unsigned (is_primary);
   1522   gcov_write_unsigned (module_info->flags);
   1523   gcov_write_unsigned (module_info->lang);
   1524   gcov_write_unsigned (module_info->ggc_memory);
   1525   gcov_write_unsigned (module_info->num_quote_paths);
   1526   gcov_write_unsigned (module_info->num_bracket_paths);
   1527   gcov_write_unsigned (module_info->num_system_paths);
   1528   gcov_write_unsigned (module_info->num_cpp_defines);
   1529   gcov_write_unsigned (module_info->num_cpp_includes);
   1530   gcov_write_unsigned (module_info->num_cl_args);
   1531 
   1532   /* Now write the filenames */
   1533   aligned_fname = (gcov_unsigned_t *) alloca ((filename_len + src_filename_len + 2) *
   1534 					      sizeof (gcov_unsigned_t));
   1535   memset (aligned_fname, 0,
   1536           (filename_len + src_filename_len + 2) * sizeof (gcov_unsigned_t));
   1537   aligned_fname[0] = filename_len;
   1538   strcpy ((char*) (aligned_fname + 1), module_info->da_filename);
   1539   aligned_fname[filename_len + 1] = src_filename_len;
   1540   strcpy ((char*) (aligned_fname + filename_len + 2), module_info->source_filename);
   1541 
   1542   for (i = 0; i < (filename_len + src_filename_len + 2); i++)
   1543     gcov_write_unsigned (aligned_fname[i]);
   1544 
   1545   /* Now write the string array.  */
   1546   gcov_write_string_array (module_info->string_array, num_strings);
   1547 }
   1548 
   1549 #endif /* L_gcov */
   1550 #endif /* inhibit_libc */
   1551