warn.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #include <stdlib.h>
  20. #include <string.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <fcntl.h>
  24. #include "elf.h"
  25. extern const char *objname;
  26. static inline char *offstr(struct section *sec, unsigned long offset)
  27. {
  28. struct symbol *func;
  29. char *name, *str;
  30. unsigned long name_off;
  31. func = find_containing_func(sec, offset);
  32. if (func) {
  33. name = func->name;
  34. name_off = offset - func->offset;
  35. } else {
  36. name = sec->name;
  37. name_off = offset;
  38. }
  39. str = malloc(strlen(name) + 20);
  40. if (func)
  41. sprintf(str, "%s()+0x%lx", name, name_off);
  42. else
  43. sprintf(str, "%s+0x%lx", name, name_off);
  44. return str;
  45. }
  46. #define WARN(format, ...) \
  47. fprintf(stderr, \
  48. "%s: warning: objtool: " format "\n", \
  49. objname, ##__VA_ARGS__)
  50. #define WARN_FUNC(format, sec, offset, ...) \
  51. ({ \
  52. char *_str = offstr(sec, offset); \
  53. WARN("%s: " format, _str, ##__VA_ARGS__); \
  54. free(_str); \
  55. })
  56. #define WARN_ELF(format, ...) \
  57. WARN(format ": %s", ##__VA_ARGS__, elf_errmsg(-1))
  58. #endif /* _WARN_H */