79661656

Date: 2025-06-11 08:55:22
Score: 0.5
Natty:
Report link

Alright, thanks to @G.M. i could come up with an answer, if anyone is interested i will share it.
It resumes the steps from the document he shared on GNU GCC freestanding environnements:

main.c:

#include "app.h"
#include <gcov.h>
#include <stdio.h>
#include <stdlib.h>

extern const struct gcov_info *const __gcov_info_start[];
extern const struct gcov_info *const __gcov_info_end[];

static void dump(const void *d, unsigned n, void *arg) {
    (void)arg;
    fwrite(d, 1, n, stderr);
}

static void filename(const char *f, void *arg) {
    __gcov_filename_to_gcfn(f, dump, arg);
}

static void *allocate(unsigned length, void *arg) {
    (void)arg;
    return malloc(length);
}

static void dump_gcov_info(void) {
    const struct gcov_info *const *info = __gcov_info_start;
    const struct gcov_info *const *end = __gcov_info_end;

    __asm__ ("" : "+r" (info));

    while (info != end) {
        void *arg = NULL;
        __gcov_info_to_gcda(*info, filename, dump, allocate, arg);
        ++info;
    }
}

int main(void) {
    application();
    dump_gcov_info();
    return 0;
}

app.c:


#include "app.h"
#include <stdio.h>

void application(void) {
    int x = 1;

    if (x == 1) {
        printf("Works\n");
    }

    if (x == 2) {
        printf("Doesn't work\n");
    }
}

The app.h file is empty, just the application() function prototype.

gcc --coverage -fprofile-info-section -c app.c
gcc --coverage -fprofile-info-section -c main.c
ld --verbose | sed '1,/^===/d' | sed '/^===/d' > linkcmds
  .gcov_info      :
  {
    PROVIDE (__gcov_info_start = .);
    KEEP (*(.gcov_info))
    PROVIDE (__gcov_info_end = .);
  }
gcc --coverage main.o app.o -T linkcmds # This will output an executable file "a.out"
./a.out 2>gcda.txt
static void dump(const void *d, unsigned n, void *arg) {
    (void)arg;
    fwrite(d, 1, n, stderr);
}
gcov-tool merge-stream gcda.txt
gcov -bc app.c

-> File 'app.c'
Lines executed:85.71% of 7
Branches executed:100.00% of 4
Taken at least once:50.00% of 4
Calls executed:50.00% of 2
Creating 'app.c.gcov'

Lines executed:85.71% of 7
Reasons:
  • Blacklisted phrase (0.5): thanks
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Léo Fresnay