^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) static int find_map(void **start, void **end, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) FILE *maps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) char line[128];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) int found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) maps = fopen("/proc/self/maps", "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) if (!maps) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) fprintf(stderr, "cannot open maps\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) while (!found && fgets(line, sizeof(line), maps)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) int m = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /* We care only about private r-x mappings. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) if (2 != sscanf(line, "%p-%p r-xp %*x %*x:%*x %*u %n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) start, end, &m))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) if (m < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) if (!strncmp(&line[m], name, strlen(name)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) fclose(maps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) return !found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) }