1 /* SPDX-License-Identifier: GPL-2.0 */
15 #include "elfconfig.h"
17 /* On BSD-alike OSes elf.h defines these according to host's word size */
23 #if KERNEL_ELFCLASS == ELFCLASS32
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
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
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
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
55 /* The 64-bit MIPS ELF ABI uses an unusual reloc format. */
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 */
67 Elf64_Xword r_info_number;
68 _Elf64_Mips_R_Info r_info_fields;
69 } _Elf64_Mips_R_Info_union;
71 #define ELF64_MIPS_R_SYM(i) \
72 ((__extension__ (_Elf64_Mips_R_Info_union)(i)).r_info_fields.r_sym)
74 #define ELF64_MIPS_R_TYPE(i) \
75 ((__extension__ (_Elf64_Mips_R_Info_union)(i)).r_info_fields.r_type1)
77 #if KERNEL_ELFDATA != HOST_ELFDATA
79 static inline void __endian(const void *src, void *dest, unsigned int size)
82 for (i = 0; i < size; i++)
83 ((unsigned char*)dest)[i] = ((unsigned char*)src)[size - i-1];
86 #define TO_NATIVE(x) \
89 __endian(&(x), &(__x), sizeof(__x)); \
93 #else /* endianness matches */
95 #define TO_NATIVE(x) (x)
99 #define NOFAIL(ptr) do_nofail((ptr), #ptr)
101 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
103 void *do_nofail(void *ptr, const char *expr);
111 void __attribute__((format(printf, 2, 3)))
112 buf_printf(struct buffer *buf, const char *fmt, ...);
115 buf_write(struct buffer *buf, const char *s, int len);
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 */
127 struct buffer dev_table_buf;
129 // Missing namespace dependencies
130 struct list_head missing_namespaces;
131 // Actual imported namespaces
132 struct list_head imported_namespaces;
140 Elf_Sym *symtab_start;
141 Elf_Sym *symtab_stop;
144 unsigned int modinfo_len;
146 /* support for 32bit section numbers */
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;
156 static inline int is_shndx_special(unsigned int i)
158 return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
162 * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
163 * the way to -256..-1, to avoid conflicting with real section
166 #define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1))
168 /* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
169 static inline unsigned int get_secindex(const struct elf_info *info,
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];
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);
185 void get_src_version(const char *modname, char sum[], unsigned sumlen);
188 char *read_text_file(const char *filename);
189 char *get_line(char **stringp);
197 void modpost_log(enum loglevel loglevel, const char *fmt, ...);
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.
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.
209 * fatal - show the given message, and bail out immediately. This should be
210 * used when there is no point to continue running modpost.
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)