summaryrefslogtreecommitdiff
path: root/targets/instr_own.c
blob: 5c30141ac984c6a0af8b696abbcfbe4ef5efd189 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// A program with its own memcpy and strlen, as one linked with a libc
// compiled by the pass has: the runtime calls them too, and must not trace
// itself. Exits with the length of the string it copied.
#include <stddef.h>

void *memcpy(void *to, const void *from, size_t n) {
    char *d = to;
    const char *s = from;
    for (size_t i = 0; i < n; i++) d[i] = s[i];
    return to;
}

size_t strlen(const char *s) {
    size_t n = 0;
    while (s[n]) n++;
    return n;
}

// Through pointers: the compiler would do a known memcpy inline.
void *(*volatile copy)(void *, const void *, size_t) = memcpy;
size_t (*volatile length)(const char *) = strlen;

int main(void) {
    char to[8], from[8] = "abcdefg";
    copy(to, from, sizeof from);
    return (int)length(to);
}