That's not possible with simply rewriting libgcov. You'd have to rewrite the corresponding gcc code that injects the counter increment instruction inbetween every line.
After instrumentation your code will execute essentially as:
crt0:
__gcov_init(main_locals);
main();
__gcov_exit(); // dump the counters to files
int main() {
static GcovStruct_t local;
local.Counter[0]++;
for (i=0;i<3;i++) {
local.Counter[1]++;
printf("Hello");
local.Counter[2]++;
}
local.Counter[3]++;
}
There maybe something to do though, as you can use
<prompt> gcc -S -fprofile-arcs -ftest-coverage
to get the intermediate .s file:
movq .LPBX1(%rip), %rax
addq $1, %rax
movq %rax, .LPBX1(%rip)
This could be almost trivial to modify by search & replace into:
movq .LPBX1(%rip), %rax -> leaq .LPBX1(%rip), %rax
addq $1, %rax -> pushq %rax
call __init_add_line_number_to_list
movq %rax, .LPBX1(%rip) -> -- remove this --
Then you'd require the newly introduced routine to increment the qword pointer and to insert that address to some extra structure that would be processed by the gcov tools that you'd modify next.