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 #include "../../include/linux/module_symbol.h"
14
15 #include "list.h"
16 #include "elfconfig.h"
17
18 /* On BSD-alike OSes elf.h defines these according to host's word size */
19 #undef ELF_ST_BIND
20 #undef ELF_ST_TYPE
21 #undef ELF_R_SYM
22 #undef ELF_R_TYPE
23
24 #if KERNEL_ELFCLASS == ELFCLASS32
25
26 #define Elf_Ehdr Elf32_Ehdr
27 #define Elf_Shdr Elf32_Shdr
28 #define Elf_Sym Elf32_Sym
29 #define Elf_Addr Elf32_Addr
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_Section Elf64_Half
45 #define ELF_ST_BIND ELF64_ST_BIND
46 #define ELF_ST_TYPE ELF64_ST_TYPE
47
48 #define Elf_Rel Elf64_Rel
49 #define Elf_Rela Elf64_Rela
50 #define ELF_R_SYM ELF64_R_SYM
51 #define ELF_R_TYPE ELF64_R_TYPE
52 #endif
53
54 #if KERNEL_ELFDATA != HOST_ELFDATA
55
__endian(const void * src,void * dest,unsigned int size)56 static inline void __endian(const void *src, void *dest, unsigned int size)
57 {
58 unsigned int i;
59 for (i = 0; i < size; i++)
60 ((unsigned char*)dest)[i] = ((unsigned char*)src)[size - i-1];
61 }
62
63 #define TO_NATIVE(x) \
64 ({ \
65 typeof(x) __x; \
66 __endian(&(x), &(__x), sizeof(__x)); \
67 __x; \
68 })
69
70 #else /* endianness matches */
71
72 #define TO_NATIVE(x) (x)
73
74 #endif
75
76 #define NOFAIL(ptr) do_nofail((ptr), #ptr)
77
78 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
79
80 void *do_nofail(void *ptr, const char *expr);
81
82 struct buffer {
83 char *p;
84 int pos;
85 int size;
86 };
87
88 void __attribute__((format(printf, 2, 3)))
89 buf_printf(struct buffer *buf, const char *fmt, ...);
90
91 void
92 buf_write(struct buffer *buf, const char *s, int len);
93
94 struct module {
95 struct list_head list;
96 struct list_head exported_symbols;
97 struct list_head unresolved_symbols;
98 bool is_gpl_compatible;
99 bool from_dump; /* true if module was loaded from *.symvers */
100 bool is_vmlinux;
101 bool seen;
102 bool has_init;
103 bool has_cleanup;
104 struct buffer dev_table_buf;
105 char srcversion[25];
106 // Missing namespace dependencies
107 struct list_head missing_namespaces;
108 // Actual imported namespaces
109 struct list_head imported_namespaces;
110 char name[];
111 };
112
113 struct elf_info {
114 size_t size;
115 Elf_Ehdr *hdr;
116 Elf_Shdr *sechdrs;
117 Elf_Sym *symtab_start;
118 Elf_Sym *symtab_stop;
119 unsigned int export_symbol_secndx; /* .export_symbol section */
120 char *strtab;
121 char *modinfo;
122 unsigned int modinfo_len;
123
124 /* support for 32bit section numbers */
125
126 unsigned int num_sections; /* max_secindex + 1 */
127 unsigned int secindex_strings;
128 /* if Nth symbol table entry has .st_shndx = SHN_XINDEX,
129 * take shndx from symtab_shndx_start[N] instead */
130 Elf32_Word *symtab_shndx_start;
131 Elf32_Word *symtab_shndx_stop;
132
133 struct symsearch *symsearch;
134 };
135
136 /* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
get_secindex(const struct elf_info * info,const Elf_Sym * sym)137 static inline unsigned int get_secindex(const struct elf_info *info,
138 const Elf_Sym *sym)
139 {
140 unsigned int index = sym->st_shndx;
141
142 /*
143 * Elf{32,64}_Sym::st_shndx is 2 byte. Big section numbers are available
144 * in the .symtab_shndx section.
145 */
146 if (index == SHN_XINDEX)
147 return info->symtab_shndx_start[sym - info->symtab_start];
148
149 /*
150 * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
151 * the way to UINT_MAX-255..UINT_MAX, to avoid conflicting with real
152 * section indices.
153 */
154 if (index >= SHN_LORESERVE && index <= SHN_HIRESERVE)
155 return index - SHN_HIRESERVE - 1;
156
157 return index;
158 }
159
160 /*
161 * If there's no name there, ignore it; likewise, ignore it if it's
162 * one of the magic symbols emitted used by current tools.
163 *
164 * Internal symbols created by tools should be ignored by modpost.
165 */
is_valid_name(struct elf_info * elf,Elf_Sym * sym)166 static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
167 {
168 const char *name = elf->strtab + sym->st_name;
169
170 if (!name || !strlen(name))
171 return 0;
172 return !is_mapping_symbol(name);
173 }
174
175 /* symsearch.c */
176 void symsearch_init(struct elf_info *elf);
177 void symsearch_finish(struct elf_info *elf);
178 Elf_Sym *symsearch_find_nearest(struct elf_info *elf, Elf_Addr addr,
179 unsigned int secndx, bool allow_negative,
180 Elf_Addr min_distance);
181
182 /* file2alias.c */
183 void handle_moddevtable(struct module *mod, struct elf_info *info,
184 Elf_Sym *sym, const char *symname);
185 void add_moddevtable(struct buffer *buf, struct module *mod);
186
187 /* sumversion.c */
188 void get_src_version(const char *modname, char sum[], unsigned sumlen);
189
190 /* from modpost.c */
191 char *read_text_file(const char *filename);
192 char *get_line(char **stringp);
193 void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym);
194
195 enum loglevel {
196 LOG_WARN,
197 LOG_ERROR,
198 LOG_FATAL
199 };
200
201 void modpost_log(enum loglevel loglevel, const char *fmt, ...);
202
203 /*
204 * warn - show the given message, then let modpost continue running, still
205 * allowing modpost to exit successfully. This should be used when
206 * we still allow to generate vmlinux and modules.
207 *
208 * error - show the given message, then let modpost continue running, but fail
209 * in the end. This should be used when we should stop building vmlinux
210 * or modules, but we can continue running modpost to catch as many
211 * issues as possible.
212 *
213 * fatal - show the given message, and bail out immediately. This should be
214 * used when there is no point to continue running modpost.
215 */
216 #define warn(fmt, args...) modpost_log(LOG_WARN, fmt, ##args)
217 #define error(fmt, args...) modpost_log(LOG_ERROR, fmt, ##args)
218 #define fatal(fmt, args...) modpost_log(LOG_FATAL, fmt, ##args)
219