find-map.c 592 B

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