blob: e2437d98f1231e5d73ddbce2d3da286e6c8280ba [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/* SPDX-License-Identifier: GPL-2.0 */
2#include <stdio.h>
3#include <stdlib.h>
4#include <stdarg.h>
5#include <string.h>
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <sys/mman.h>
9#include <fcntl.h>
10#include <unistd.h>
11#if !(defined(__APPLE__) || defined(__CYGWIN__))
12#include <elf.h>
13#else
14#include "elf.h"
15#endif
16
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_Sword Elf64_Sword
32#define Elf_Section Elf32_Half
33#define ELF_ST_BIND ELF32_ST_BIND
34#define ELF_ST_TYPE ELF32_ST_TYPE
35
36#define Elf_Rel Elf32_Rel
37#define Elf_Rela Elf32_Rela
38#define ELF_R_SYM ELF32_R_SYM
39#define ELF_R_TYPE ELF32_R_TYPE
40#else
41
42#define Elf_Ehdr Elf64_Ehdr
43#define Elf_Shdr Elf64_Shdr
44#define Elf_Sym Elf64_Sym
45#define Elf_Addr Elf64_Addr
46#define Elf_Sword Elf64_Sxword
47#define Elf_Section Elf64_Half
48#define ELF_ST_BIND ELF64_ST_BIND
49#define ELF_ST_TYPE ELF64_ST_TYPE
50
51#define Elf_Rel Elf64_Rel
52#define Elf_Rela Elf64_Rela
53#define ELF_R_SYM ELF64_R_SYM
54#define ELF_R_TYPE ELF64_R_TYPE
55#endif
56
57/* The 64-bit MIPS ELF ABI uses an unusual reloc format. */
58typedef struct
59{
60 Elf32_Word r_sym; /* Symbol index */
61 unsigned char r_ssym; /* Special symbol for 2nd relocation */
62 unsigned char r_type3; /* 3rd relocation type */
63 unsigned char r_type2; /* 2nd relocation type */
64 unsigned char r_type1; /* 1st relocation type */
65} _Elf64_Mips_R_Info;
66
67typedef union
68{
69 Elf64_Xword r_info_number;
70 _Elf64_Mips_R_Info r_info_fields;
71} _Elf64_Mips_R_Info_union;
72
73#define ELF64_MIPS_R_SYM(i) \
74 ((__extension__ (_Elf64_Mips_R_Info_union)(i)).r_info_fields.r_sym)
75
76#define ELF64_MIPS_R_TYPE(i) \
77 ((__extension__ (_Elf64_Mips_R_Info_union)(i)).r_info_fields.r_type1)
78
79#if KERNEL_ELFDATA != HOST_ELFDATA
80
81static inline void __endian(const void *src, void *dest, unsigned int size)
82{
83 unsigned int i;
84 for (i = 0; i < size; i++)
85 ((unsigned char*)dest)[i] = ((unsigned char*)src)[size - i-1];
86}
87
88#define TO_NATIVE(x) \
89({ \
90 typeof(x) __x; \
91 __endian(&(x), &(__x), sizeof(__x)); \
92 __x; \
93})
94
95#else /* endianness matches */
96
97#define TO_NATIVE(x) (x)
98
99#endif
100
101#define NOFAIL(ptr) do_nofail((ptr), #ptr)
102void *do_nofail(void *ptr, const char *expr);
103
104struct buffer {
105 char *p;
106 int pos;
107 int size;
108};
109
110void __attribute__((format(printf, 2, 3)))
111buf_printf(struct buffer *buf, const char *fmt, ...);
112
113void
114buf_write(struct buffer *buf, const char *s, int len);
115
116struct namespace_list {
117 struct namespace_list *next;
118 char namespace[0];
119};
120
121struct module {
122 struct module *next;
123 const char *name;
124 int gpl_compatible;
125 struct symbol *unres;
126 int seen;
127 int skip;
128 int has_init;
129 int has_cleanup;
130 struct buffer dev_table_buf;
131 char srcversion[25];
132 int is_dot_o;
133 // Required namespace dependencies
134 struct namespace_list *required_namespaces;
135 // Actual imported namespaces
136 struct namespace_list *imported_namespaces;
137};
138
139struct elf_info {
140 unsigned long size;
141 Elf_Ehdr *hdr;
142 Elf_Shdr *sechdrs;
143 Elf_Sym *symtab_start;
144 Elf_Sym *symtab_stop;
145 Elf_Section export_sec;
146 Elf_Section export_unused_sec;
147 Elf_Section export_gpl_sec;
148 Elf_Section export_unused_gpl_sec;
149 Elf_Section export_gpl_future_sec;
150 char *strtab;
151 char *modinfo;
152 unsigned int modinfo_len;
153
154 /* support for 32bit section numbers */
155
156 unsigned int num_sections; /* max_secindex + 1 */
157 unsigned int secindex_strings;
158 /* if Nth symbol table entry has .st_shndx = SHN_XINDEX,
159 * take shndx from symtab_shndx_start[N] instead */
160 Elf32_Word *symtab_shndx_start;
161 Elf32_Word *symtab_shndx_stop;
162};
163
164static inline int is_shndx_special(unsigned int i)
165{
166 return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
167}
168
169/*
170 * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
171 * the way to -256..-1, to avoid conflicting with real section
172 * indices.
173 */
174#define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1))
175
176/* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
177static inline unsigned int get_secindex(const struct elf_info *info,
178 const Elf_Sym *sym)
179{
180 if (is_shndx_special(sym->st_shndx))
181 return SPECIAL(sym->st_shndx);
182 if (sym->st_shndx != SHN_XINDEX)
183 return sym->st_shndx;
184 return info->symtab_shndx_start[sym - info->symtab_start];
185}
186
187/* file2alias.c */
188extern unsigned int cross_build;
189void handle_moddevtable(struct module *mod, struct elf_info *info,
190 Elf_Sym *sym, const char *symname);
191void add_moddevtable(struct buffer *buf, struct module *mod);
192
193/* sumversion.c */
194void maybe_frob_rcs_version(const char *modfilename,
195 char *version,
196 void *modinfo,
197 unsigned long modinfo_offset);
198void get_src_version(const char *modname, char sum[], unsigned sumlen);
199
200/* from modpost.c */
201void *grab_file(const char *filename, unsigned long *size);
202char* get_next_line(unsigned long *pos, void *file, unsigned long size);
203void release_file(void *file, unsigned long size);
204
205void fatal(const char *fmt, ...);
206void warn(const char *fmt, ...);
207void merror(const char *fmt, ...);