Merge tag 'trace-v5.19' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux-2.6-microblaze.git] / scripts / mod / modpost.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <stdbool.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <stdarg.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <sys/mman.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <elf.h>
13
14 #include "list.h"
15 #include "elfconfig.h"
16
17 /* On BSD-alike OSes elf.h defines these according to host's word size */
18 #undef ELF_ST_BIND
19 #undef ELF_ST_TYPE
20 #undef ELF_R_SYM
21 #undef ELF_R_TYPE
22
23 #if KERNEL_ELFCLASS == ELFCLASS32
24
25 #define Elf_Ehdr    Elf32_Ehdr
26 #define Elf_Shdr    Elf32_Shdr
27 #define Elf_Sym     Elf32_Sym
28 #define Elf_Addr    Elf32_Addr
29 #define Elf_Sword   Elf64_Sword
30 #define Elf_Section Elf32_Half
31 #define ELF_ST_BIND ELF32_ST_BIND
32 #define ELF_ST_TYPE ELF32_ST_TYPE
33
34 #define Elf_Rel     Elf32_Rel
35 #define Elf_Rela    Elf32_Rela
36 #define ELF_R_SYM   ELF32_R_SYM
37 #define ELF_R_TYPE  ELF32_R_TYPE
38 #else
39
40 #define Elf_Ehdr    Elf64_Ehdr
41 #define Elf_Shdr    Elf64_Shdr
42 #define Elf_Sym     Elf64_Sym
43 #define Elf_Addr    Elf64_Addr
44 #define Elf_Sword   Elf64_Sxword
45 #define Elf_Section Elf64_Half
46 #define ELF_ST_BIND ELF64_ST_BIND
47 #define ELF_ST_TYPE ELF64_ST_TYPE
48
49 #define Elf_Rel     Elf64_Rel
50 #define Elf_Rela    Elf64_Rela
51 #define ELF_R_SYM   ELF64_R_SYM
52 #define ELF_R_TYPE  ELF64_R_TYPE
53 #endif
54
55 /* The 64-bit MIPS ELF ABI uses an unusual reloc format. */
56 typedef struct
57 {
58         Elf32_Word    r_sym;    /* Symbol index */
59         unsigned char r_ssym;   /* Special symbol for 2nd relocation */
60         unsigned char r_type3;  /* 3rd relocation type */
61         unsigned char r_type2;  /* 2nd relocation type */
62         unsigned char r_type1;  /* 1st relocation type */
63 } _Elf64_Mips_R_Info;
64
65 typedef union
66 {
67         Elf64_Xword             r_info_number;
68         _Elf64_Mips_R_Info      r_info_fields;
69 } _Elf64_Mips_R_Info_union;
70
71 #define ELF64_MIPS_R_SYM(i) \
72   ((__extension__ (_Elf64_Mips_R_Info_union)(i)).r_info_fields.r_sym)
73
74 #define ELF64_MIPS_R_TYPE(i) \
75   ((__extension__ (_Elf64_Mips_R_Info_union)(i)).r_info_fields.r_type1)
76
77 #if KERNEL_ELFDATA != HOST_ELFDATA
78
79 static inline void __endian(const void *src, void *dest, unsigned int size)
80 {
81         unsigned int i;
82         for (i = 0; i < size; i++)
83                 ((unsigned char*)dest)[i] = ((unsigned char*)src)[size - i-1];
84 }
85
86 #define TO_NATIVE(x)                                            \
87 ({                                                              \
88         typeof(x) __x;                                          \
89         __endian(&(x), &(__x), sizeof(__x));                    \
90         __x;                                                    \
91 })
92
93 #else /* endianness matches */
94
95 #define TO_NATIVE(x) (x)
96
97 #endif
98
99 #define NOFAIL(ptr)   do_nofail((ptr), #ptr)
100 void *do_nofail(void *ptr, const char *expr);
101
102 struct buffer {
103         char *p;
104         int pos;
105         int size;
106 };
107
108 void __attribute__((format(printf, 2, 3)))
109 buf_printf(struct buffer *buf, const char *fmt, ...);
110
111 void
112 buf_write(struct buffer *buf, const char *s, int len);
113
114 struct module {
115         struct list_head list;
116         struct list_head exported_symbols;
117         struct list_head unresolved_symbols;
118         bool is_gpl_compatible;
119         bool from_dump;         /* true if module was loaded from *.symvers */
120         bool is_vmlinux;
121         bool seen;
122         bool has_init;
123         bool has_cleanup;
124         struct buffer dev_table_buf;
125         char         srcversion[25];
126         // Missing namespace dependencies
127         struct list_head missing_namespaces;
128         // Actual imported namespaces
129         struct list_head imported_namespaces;
130         char name[];
131 };
132
133 struct elf_info {
134         size_t size;
135         Elf_Ehdr     *hdr;
136         Elf_Shdr     *sechdrs;
137         Elf_Sym      *symtab_start;
138         Elf_Sym      *symtab_stop;
139         char         *strtab;
140         char         *modinfo;
141         unsigned int modinfo_len;
142
143         /* support for 32bit section numbers */
144
145         unsigned int num_sections; /* max_secindex + 1 */
146         unsigned int secindex_strings;
147         /* if Nth symbol table entry has .st_shndx = SHN_XINDEX,
148          * take shndx from symtab_shndx_start[N] instead */
149         Elf32_Word   *symtab_shndx_start;
150         Elf32_Word   *symtab_shndx_stop;
151 };
152
153 static inline int is_shndx_special(unsigned int i)
154 {
155         return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
156 }
157
158 /*
159  * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
160  * the way to -256..-1, to avoid conflicting with real section
161  * indices.
162  */
163 #define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1))
164
165 /* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
166 static inline unsigned int get_secindex(const struct elf_info *info,
167                                         const Elf_Sym *sym)
168 {
169         if (is_shndx_special(sym->st_shndx))
170                 return SPECIAL(sym->st_shndx);
171         if (sym->st_shndx != SHN_XINDEX)
172                 return sym->st_shndx;
173         return info->symtab_shndx_start[sym - info->symtab_start];
174 }
175
176 /* file2alias.c */
177 void handle_moddevtable(struct module *mod, struct elf_info *info,
178                         Elf_Sym *sym, const char *symname);
179 void add_moddevtable(struct buffer *buf, struct module *mod);
180
181 /* sumversion.c */
182 void get_src_version(const char *modname, char sum[], unsigned sumlen);
183
184 /* from modpost.c */
185 char *read_text_file(const char *filename);
186 char *get_line(char **stringp);
187
188 enum loglevel {
189         LOG_WARN,
190         LOG_ERROR,
191         LOG_FATAL
192 };
193
194 void modpost_log(enum loglevel loglevel, const char *fmt, ...);
195
196 /*
197  * warn - show the given message, then let modpost continue running, still
198  *        allowing modpost to exit successfully. This should be used when
199  *        we still allow to generate vmlinux and modules.
200  *
201  * error - show the given message, then let modpost continue running, but fail
202  *         in the end. This should be used when we should stop building vmlinux
203  *         or modules, but we can continue running modpost to catch as many
204  *         issues as possible.
205  *
206  * fatal - show the given message, and bail out immediately. This should be
207  *         used when there is no point to continue running modpost.
208  */
209 #define warn(fmt, args...)      modpost_log(LOG_WARN, fmt, ##args)
210 #define error(fmt, args...)     modpost_log(LOG_ERROR, fmt, ##args)
211 #define fatal(fmt, args...)     modpost_log(LOG_FATAL, fmt, ##args)