Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
58e01fca | 2 | #include <stdbool.h> |
1da177e4 LT |
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 | ||
325eba05 | 14 | #include "list.h" |
1da177e4 LT |
15 | #include "elfconfig.h" |
16 | ||
3c0561e0 PF |
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 | ||
1da177e4 LT |
23 | #if KERNEL_ELFCLASS == ELFCLASS32 |
24 | ||
62070fa4 SR |
25 | #define Elf_Ehdr Elf32_Ehdr |
26 | #define Elf_Shdr Elf32_Shdr | |
1da177e4 | 27 | #define Elf_Sym Elf32_Sym |
b39927cf | 28 | #define Elf_Addr Elf32_Addr |
9ad21c3f | 29 | #define Elf_Sword Elf64_Sword |
4f4c4ee1 | 30 | #define Elf_Section Elf32_Half |
1da177e4 LT |
31 | #define ELF_ST_BIND ELF32_ST_BIND |
32 | #define ELF_ST_TYPE ELF32_ST_TYPE | |
33 | ||
2c1a51f3 | 34 | #define Elf_Rel Elf32_Rel |
b39927cf SR |
35 | #define Elf_Rela Elf32_Rela |
36 | #define ELF_R_SYM ELF32_R_SYM | |
37 | #define ELF_R_TYPE ELF32_R_TYPE | |
1da177e4 LT |
38 | #else |
39 | ||
62070fa4 SR |
40 | #define Elf_Ehdr Elf64_Ehdr |
41 | #define Elf_Shdr Elf64_Shdr | |
1da177e4 | 42 | #define Elf_Sym Elf64_Sym |
b39927cf | 43 | #define Elf_Addr Elf64_Addr |
9ad21c3f | 44 | #define Elf_Sword Elf64_Sxword |
4f4c4ee1 | 45 | #define Elf_Section Elf64_Half |
1da177e4 LT |
46 | #define ELF_ST_BIND ELF64_ST_BIND |
47 | #define ELF_ST_TYPE ELF64_ST_TYPE | |
48 | ||
2c1a51f3 | 49 | #define Elf_Rel Elf64_Rel |
b39927cf SR |
50 | #define Elf_Rela Elf64_Rela |
51 | #define ELF_R_SYM ELF64_R_SYM | |
52 | #define ELF_R_TYPE ELF64_R_TYPE | |
1da177e4 LT |
53 | #endif |
54 | ||
eae07ac6 AN |
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 | ||
ae4ac123 AN |
74 | #define ELF64_MIPS_R_TYPE(i) \ |
75 | ((__extension__ (_Elf64_Mips_R_Info_union)(i)).r_info_fields.r_type1) | |
76 | ||
1da177e4 LT |
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 | ||
1da177e4 LT |
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 { | |
325eba05 | 115 | struct list_head list; |
f841536e | 116 | struct list_head exported_symbols; |
8a69152b | 117 | struct list_head unresolved_symbols; |
5066743e | 118 | bool is_gpl_compatible; |
58e01fca MY |
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; | |
1da177e4 LT |
124 | struct buffer dev_table_buf; |
125 | char srcversion[25]; | |
bbc55bde | 126 | // Missing namespace dependencies |
ab489d60 | 127 | struct list_head missing_namespaces; |
cb9b55d2 | 128 | // Actual imported namespaces |
ab489d60 | 129 | struct list_head imported_namespaces; |
a82f794c | 130 | char name[]; |
1da177e4 LT |
131 | }; |
132 | ||
133 | struct elf_info { | |
3b09efc4 | 134 | size_t size; |
1da177e4 LT |
135 | Elf_Ehdr *hdr; |
136 | Elf_Shdr *sechdrs; | |
137 | Elf_Sym *symtab_start; | |
138 | Elf_Sym *symtab_stop; | |
7d02b490 | 139 | char *strtab; |
1da177e4 LT |
140 | char *modinfo; |
141 | unsigned int modinfo_len; | |
1ce53adf DV |
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; | |
1da177e4 LT |
151 | }; |
152 | ||
1ce53adf DV |
153 | static inline int is_shndx_special(unsigned int i) |
154 | { | |
155 | return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE; | |
156 | } | |
157 | ||
6845756b AK |
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. | |
1ce53adf | 162 | */ |
6845756b | 163 | #define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1)) |
1ce53adf DV |
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 | { | |
6845756b AK |
169 | if (is_shndx_special(sym->st_shndx)) |
170 | return SPECIAL(sym->st_shndx); | |
1ce53adf DV |
171 | if (sym->st_shndx != SHN_XINDEX) |
172 | return sym->st_shndx; | |
6845756b | 173 | return info->symtab_shndx_start[sym - info->symtab_start]; |
1ce53adf DV |
174 | } |
175 | ||
cb80514d | 176 | /* file2alias.c */ |
1da177e4 LT |
177 | void handle_moddevtable(struct module *mod, struct elf_info *info, |
178 | Elf_Sym *sym, const char *symname); | |
1da177e4 LT |
179 | void add_moddevtable(struct buffer *buf, struct module *mod); |
180 | ||
cb80514d | 181 | /* sumversion.c */ |
1da177e4 LT |
182 | void get_src_version(const char *modname, char sum[], unsigned sumlen); |
183 | ||
cb80514d | 184 | /* from modpost.c */ |
ac5100f5 MY |
185 | char *read_text_file(const char *filename); |
186 | char *get_line(char **stringp); | |
cb80514d | 187 | |
93c95e52 JY |
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 | ||
0fd3fbad MY |
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 | */ | |
93c95e52 | 209 | #define warn(fmt, args...) modpost_log(LOG_WARN, fmt, ##args) |
bc72d723 | 210 | #define error(fmt, args...) modpost_log(LOG_ERROR, fmt, ##args) |
93c95e52 | 211 | #define fatal(fmt, args...) modpost_log(LOG_FATAL, fmt, ##args) |