warn.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef _WARN_H
  18. #define _WARN_H
  19. extern const char *objname;
  20. static inline char *offstr(struct section *sec, unsigned long offset)
  21. {
  22. struct symbol *func;
  23. char *name, *str;
  24. unsigned long name_off;
  25. func = find_containing_func(sec, offset);
  26. if (func) {
  27. name = func->name;
  28. name_off = offset - func->offset;
  29. } else {
  30. name = sec->name;
  31. name_off = offset;
  32. }
  33. str = malloc(strlen(name) + 20);
  34. if (func)
  35. sprintf(str, "%s()+0x%lx", name, name_off);
  36. else
  37. sprintf(str, "%s+0x%lx", name, name_off);
  38. return str;
  39. }
  40. #define WARN(format, ...) \
  41. fprintf(stderr, \
  42. "%s: warning: objtool: " format "\n", \
  43. objname, ##__VA_ARGS__)
  44. #define WARN_FUNC(format, sec, offset, ...) \
  45. ({ \
  46. char *_str = offstr(sec, offset); \
  47. WARN("%s: " format, _str, ##__VA_ARGS__); \
  48. free(_str); \
  49. })
  50. #endif /* _WARN_H */