xref: /openbmc/linux/scripts/mod/modpost.h (revision 7fc96d71)
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 
101 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
102 
103 void *do_nofail(void *ptr, const char *expr);
104 
105 struct buffer {
106 	char *p;
107 	int pos;
108 	int size;
109 };
110 
111 void __attribute__((format(printf, 2, 3)))
112 buf_printf(struct buffer *buf, const char *fmt, ...);
113 
114 void
115 buf_write(struct buffer *buf, const char *s, int len);
116 
117 struct module {
118 	struct list_head list;
119 	struct list_head exported_symbols;
120 	struct list_head unresolved_symbols;
121 	bool is_gpl_compatible;
122 	bool from_dump;		/* true if module was loaded from *.symvers */
123 	bool is_vmlinux;
124 	bool seen;
125 	bool has_init;
126 	bool has_cleanup;
127 	struct buffer dev_table_buf;
128 	char	     srcversion[25];
129 	// Missing namespace dependencies
130 	struct list_head missing_namespaces;
131 	// Actual imported namespaces
132 	struct list_head imported_namespaces;
133 	char name[];
134 };
135 
136 struct elf_info {
137 	size_t size;
138 	Elf_Ehdr     *hdr;
139 	Elf_Shdr     *sechdrs;
140 	Elf_Sym      *symtab_start;
141 	Elf_Sym      *symtab_stop;
142 	char         *strtab;
143 	char	     *modinfo;
144 	unsigned int modinfo_len;
145 
146 	/* support for 32bit section numbers */
147 
148 	unsigned int num_sections; /* max_secindex + 1 */
149 	unsigned int secindex_strings;
150 	/* if Nth symbol table entry has .st_shndx = SHN_XINDEX,
151 	 * take shndx from symtab_shndx_start[N] instead */
152 	Elf32_Word   *symtab_shndx_start;
153 	Elf32_Word   *symtab_shndx_stop;
154 };
155 
156 static inline int is_shndx_special(unsigned int i)
157 {
158 	return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
159 }
160 
161 /*
162  * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
163  * the way to -256..-1, to avoid conflicting with real section
164  * indices.
165  */
166 #define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1))
167 
168 /* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
169 static inline unsigned int get_secindex(const struct elf_info *info,
170 					const Elf_Sym *sym)
171 {
172 	if (is_shndx_special(sym->st_shndx))
173 		return SPECIAL(sym->st_shndx);
174 	if (sym->st_shndx != SHN_XINDEX)
175 		return sym->st_shndx;
176 	return info->symtab_shndx_start[sym - info->symtab_start];
177 }
178 
179 /* file2alias.c */
180 void handle_moddevtable(struct module *mod, struct elf_info *info,
181 			Elf_Sym *sym, const char *symname);
182 void add_moddevtable(struct buffer *buf, struct module *mod);
183 
184 /* sumversion.c */
185 void get_src_version(const char *modname, char sum[], unsigned sumlen);
186 
187 /* from modpost.c */
188 char *read_text_file(const char *filename);
189 char *get_line(char **stringp);
190 
191 enum loglevel {
192 	LOG_WARN,
193 	LOG_ERROR,
194 	LOG_FATAL
195 };
196 
197 void modpost_log(enum loglevel loglevel, const char *fmt, ...);
198 
199 /*
200  * warn - show the given message, then let modpost continue running, still
201  *        allowing modpost to exit successfully. This should be used when
202  *        we still allow to generate vmlinux and modules.
203  *
204  * error - show the given message, then let modpost continue running, but fail
205  *         in the end. This should be used when we should stop building vmlinux
206  *         or modules, but we can continue running modpost to catch as many
207  *         issues as possible.
208  *
209  * fatal - show the given message, and bail out immediately. This should be
210  *         used when there is no point to continue running modpost.
211  */
212 #define warn(fmt, args...)	modpost_log(LOG_WARN, fmt, ##args)
213 #define error(fmt, args...)	modpost_log(LOG_ERROR, fmt, ##args)
214 #define fatal(fmt, args...)	modpost_log(LOG_FATAL, fmt, ##args)
215