| 1 |
|
| 2 | |
| 3 | * Copyright (C) 2015 Josh Poimboeuf <[email protected]>
|
| 4 |
|
| 5 |
|
| 6 | #ifndef _WARN_H
|
| 7 | #define _WARN_H
|
| 8 |
|
| 9 | #include <stdlib.h>
|
| 10 | #include <string.h>
|
| 11 | #include <sys/types.h>
|
| 12 | #include <sys/stat.h>
|
| 13 | #include <fcntl.h>
|
| 14 | #include "elf.h"
|
| 15 |
|
| 16 | extern const char *objname;
|
| 17 |
|
| 18 | static inline char *offstr(struct section *sec, unsigned long offset)
|
| 19 | {
|
| 20 | struct symbol *func;
|
| 21 | char *name, *str;
|
| 22 | unsigned long name_off;
|
| 23 |
|
| 24 | func = find_containing_func(sec, offset);
|
| 25 | if (func) {
|
| 26 | name = func->name;
|
| 27 | name_off = offset - func->offset;
|
| 28 | } else {
|
| 29 | name = sec->name;
|
| 30 | name_off = offset;
|
| 31 | }
|
| 32 |
|
| 33 | str = malloc(strlen(name) + 20);
|
| 34 |
|
| 35 | if (func)
|
| 36 | sprintf(str, "%s()+0x%lx", name, name_off);
|
| 37 | else
|
| 38 | sprintf(str, "%s+0x%lx", name, name_off);
|
| 39 |
|
| 40 | return str;
|
| 41 | }
|
| 42 |
|
| 43 | #define WARN(format, ...) \
|
| 44 | fprintf(stderr, \
|
| 45 | "%s: warning: objtool: " format "\n", \
|
| 46 | objname, ##__VA_ARGS__)
|
| 47 |
|
| 48 | #define WARN_FUNC(format, sec, offset, ...) \
|
| 49 | ({ \
|
| 50 | char *_str = offstr(sec, offset); \
|
| 51 | WARN("%s: " format, _str, ##__VA_ARGS__); \
|
| 52 | free(_str); \
|
| 53 | })
|
| 54 |
|
| 55 | #define BT_FUNC(format, insn, ...) \
|
| 56 | ({ \
|
| 57 | struct instruction *_insn = (insn); \
|
| 58 | char *_str = offstr(_insn->sec, _insn->offset); \
|
| 59 | WARN(" %s: " format, _str, ##__VA_ARGS__); \
|
| 60 | free(_str); \
|
| 61 | })
|
| 62 |
|
| 63 | #define WARN_ELF(format, ...) \
|
| 64 | WARN(format ": %s", ##__VA_ARGS__, elf_errmsg(-1))
|
| 65 |
|
| 66 | #endif
|