Greg Hartman | 76d05dc | 2016-11-23 15:51:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * common.h - Common internal operations performed by the module subsystem |
| 3 | * |
| 4 | * Created on: Aug 11, 2008 |
| 5 | * Author: Stefan Bucur <stefanb@zytor.com> |
| 6 | */ |
| 7 | |
| 8 | #ifndef COMMON_H_ |
| 9 | #define COMMON_H_ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | |
| 13 | #include <sys/module.h> |
| 14 | #include <linux/list.h> |
| 15 | |
| 16 | #include "elfutils.h" |
| 17 | |
| 18 | // Performs an operation and jumps to a given label if an error occurs |
| 19 | #define CHECKED(res, expr, error) \ |
| 20 | do { \ |
| 21 | (res) = (expr); \ |
| 22 | if ((res) < 0) \ |
| 23 | goto error; \ |
| 24 | } while (0) |
| 25 | |
| 26 | #define MIN(x,y) (((x) < (y)) ? (x) : (y)) |
| 27 | #define MAX(x,y) (((x) > (y)) ? (x) : (y)) |
| 28 | |
| 29 | static inline Elf_Sym *symbol_get_entry(struct elf_module *module, int entry) |
| 30 | { |
| 31 | char *sym_table = (char *)module->sym_table; |
| 32 | int index = entry * module->syment_size; |
| 33 | |
| 34 | return (Elf_Sym *)(sym_table + index); |
| 35 | } |
| 36 | |
| 37 | //#define ELF_DEBUG |
| 38 | |
| 39 | #ifdef ELF_DEBUG |
| 40 | #define DBG_PRINT(fmt, args...) fprintf(stderr, "[ELF] " fmt, ##args) |
| 41 | #else |
| 42 | #define DBG_PRINT(fmt, args...) // Expand to nothing |
| 43 | #endif |
| 44 | |
| 45 | // User-space debugging routines |
| 46 | #ifdef ELF_DEBUG |
| 47 | extern void print_elf_ehdr(Elf_Ehdr *ehdr); |
| 48 | extern void print_elf_symbols(struct elf_module *module); |
| 49 | #endif //ELF_DEBUG |
| 50 | |
| 51 | |
| 52 | /* |
| 53 | * Image files manipulation routines |
| 54 | */ |
| 55 | |
| 56 | extern int image_load(struct elf_module *module); |
| 57 | extern int image_unload(struct elf_module *module); |
| 58 | extern int image_read(void *buff, size_t size, struct elf_module *module); |
| 59 | extern int image_skip(size_t size, struct elf_module *module); |
| 60 | extern int image_seek(Elf_Off offset, struct elf_module *module); |
| 61 | |
| 62 | extern struct module_dep *module_dep_alloc(struct elf_module *module); |
| 63 | |
| 64 | extern int check_header_common(Elf_Ehdr *elf_hdr); |
| 65 | |
| 66 | extern int enforce_dependency(struct elf_module *req, struct elf_module *dep); |
| 67 | extern int clear_dependency(struct elf_module *req, struct elf_module *dep); |
| 68 | |
| 69 | extern int check_symbols(struct elf_module *module); |
| 70 | |
| 71 | |
| 72 | #endif /* COMMON_H_ */ |