1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <byteswap.h>
3 #include <stdbool.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdarg.h>
7 #include <string.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <sys/mman.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <elf.h>
14 #include "../../include/linux/module_symbol.h"
15 
16 #include <list_types.h>
17 #include "elfconfig.h"
18 
19 /* On BSD-alike OSes elf.h defines these according to host's word size */
20 #undef ELF_ST_BIND
21 #undef ELF_ST_TYPE
22 #undef ELF_R_SYM
23 #undef ELF_R_TYPE
24 
25 #if KERNEL_ELFCLASS == ELFCLASS32
26 
27 #define Elf_Ehdr    Elf32_Ehdr
28 #define Elf_Shdr    Elf32_Shdr
29 #define Elf_Sym     Elf32_Sym
30 #define Elf_Addr    Elf32_Addr
31 #define Elf_Section Elf32_Half
32 #define ELF_ST_BIND ELF32_ST_BIND
33 #define ELF_ST_TYPE ELF32_ST_TYPE
34 
35 #define Elf_Rel     Elf32_Rel
36 #define Elf_Rela    Elf32_Rela
37 #define ELF_R_SYM   ELF32_R_SYM
38 #define ELF_R_TYPE  ELF32_R_TYPE
39 #else
40 
41 #define Elf_Ehdr    Elf64_Ehdr
42 #define Elf_Shdr    Elf64_Shdr
43 #define Elf_Sym     Elf64_Sym
44 #define Elf_Addr    Elf64_Addr
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 #define bswap(x) \
56 ({ \
57 	_Static_assert(sizeof(x) == 1 || sizeof(x) == 2 || \
58 		       sizeof(x) == 4 || sizeof(x) == 8, "bug"); \
59 	(typeof(x))(sizeof(x) == 2 ? bswap_16(x) : \
60 		    sizeof(x) == 4 ? bswap_32(x) : \
61 		    sizeof(x) == 8 ? bswap_64(x) : \
62 		    x); \
63 })
64 
65 #define TO_NATIVE(x)	\
66 	(target_is_big_endian == host_is_big_endian ? x : bswap(x))
67 
68 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
69 
70 struct buffer {
71 	char *p;
72 	int pos;
73 	int size;
74 };
75 
76 void __attribute__((format(printf, 2, 3)))
77 buf_printf(struct buffer *buf, const char *fmt, ...);
78 
79 void
80 buf_write(struct buffer *buf, const char *s, int len);
81 
82 struct module {
83 	struct list_head list;
84 	struct list_head exported_symbols;
85 	struct list_head unresolved_symbols;
86 	bool is_gpl_compatible;
87 	bool from_dump;		/* true if module was loaded from *.symvers */
88 	bool is_vmlinux;
89 	bool seen;
90 	bool has_init;
91 	bool has_cleanup;
92 	bool is_protected;  /* true if module exports are protected */
93 	struct buffer dev_table_buf;
94 	char	     srcversion[25];
95 	// Missing namespace dependencies
96 	struct list_head missing_namespaces;
97 	// Actual imported namespaces
98 	struct list_head imported_namespaces;
99 	char name[];
100 };
101 
102 struct elf_info {
103 	size_t size;
104 	Elf_Ehdr     *hdr;
105 	Elf_Shdr     *sechdrs;
106 	Elf_Sym      *symtab_start;
107 	Elf_Sym      *symtab_stop;
108 	unsigned int export_symbol_secndx;	/* .export_symbol section */
109 	char         *strtab;
110 	char	     *modinfo;
111 	unsigned int modinfo_len;
112 
113 	/* support for 32bit section numbers */
114 
115 	unsigned int num_sections; /* max_secindex + 1 */
116 	unsigned int secindex_strings;
117 	/* if Nth symbol table entry has .st_shndx = SHN_XINDEX,
118 	 * take shndx from symtab_shndx_start[N] instead */
119 	Elf32_Word   *symtab_shndx_start;
120 	Elf32_Word   *symtab_shndx_stop;
121 
122 	struct symsearch *symsearch;
123 };
124 
125 /* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
get_secindex(const struct elf_info * info,const Elf_Sym * sym)126 static inline unsigned int get_secindex(const struct elf_info *info,
127 					const Elf_Sym *sym)
128 {
129 	unsigned int index = sym->st_shndx;
130 
131 	/*
132 	 * Elf{32,64}_Sym::st_shndx is 2 byte. Big section numbers are available
133 	 * in the .symtab_shndx section.
134 	 */
135 	if (index == SHN_XINDEX)
136 		return info->symtab_shndx_start[sym - info->symtab_start];
137 
138 	/*
139 	 * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
140 	 * the way to UINT_MAX-255..UINT_MAX, to avoid conflicting with real
141 	 * section indices.
142 	 */
143 	if (index >= SHN_LORESERVE && index <= SHN_HIRESERVE)
144 		return index - SHN_HIRESERVE - 1;
145 
146 	return index;
147 }
148 
149 /*
150  * If there's no name there, ignore it; likewise, ignore it if it's
151  * one of the magic symbols emitted used by current tools.
152  *
153  * Internal symbols created by tools should be ignored by modpost.
154  */
is_valid_name(struct elf_info * elf,Elf_Sym * sym)155 static inline bool is_valid_name(struct elf_info *elf, Elf_Sym *sym)
156 {
157 	const char *name = elf->strtab + sym->st_name;
158 
159 	if (!name || !strlen(name))
160 		return false;
161 	return !is_mapping_symbol(name);
162 }
163 
164 /* symsearch.c */
165 void symsearch_init(struct elf_info *elf);
166 void symsearch_finish(struct elf_info *elf);
167 Elf_Sym *symsearch_find_nearest(struct elf_info *elf, Elf_Addr addr,
168 				unsigned int secndx, bool allow_negative,
169 				Elf_Addr min_distance);
170 
171 /* file2alias.c */
172 void handle_moddevtable(struct module *mod, struct elf_info *info,
173 			Elf_Sym *sym, const char *symname);
174 void add_moddevtable(struct buffer *buf, struct module *mod);
175 
176 /* sumversion.c */
177 void get_src_version(const char *modname, char sum[], unsigned sumlen);
178 
179 /* from modpost.c */
180 extern bool target_is_big_endian;
181 extern bool host_is_big_endian;
182 char *read_text_file(const char *filename);
183 char *get_line(char **stringp);
184 void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym);
185 
186 void __attribute__((format(printf, 2, 3)))
187 modpost_log(bool is_error, const char *fmt, ...);
188 
189 /*
190  * warn - show the given message, then let modpost continue running, still
191  *        allowing modpost to exit successfully. This should be used when
192  *        we still allow to generate vmlinux and modules.
193  *
194  * error - show the given message, then let modpost continue running, but fail
195  *         in the end. This should be used when we should stop building vmlinux
196  *         or modules, but we can continue running modpost to catch as many
197  *         issues as possible.
198  *
199  * fatal - show the given message, and bail out immediately. This should be
200  *         used when there is no point to continue running modpost.
201  */
202 #define warn(fmt, args...)	modpost_log(false, fmt, ##args)
203 #define error(fmt, args...)	modpost_log(true, fmt, ##args)
204 #define fatal(fmt, args...)	do { error(fmt, ##args); exit(1); } while (1)
205