3 #define PERBIT(x) x##32
4 #define ElfPERBIT(x) Elf32_##x
5 #define ELFPERBIT(x) ELF32_##x
6 /* 32-bit unsigned integer */
7 #define Elf32_Uint Elf32_Word
9 #elif defined(ELF64BIT)
11 #define PERBIT(x) x##64
12 #define ElfPERBIT(x) Elf64_##x
13 #define ELFPERBIT(x) ELF64_##x
14 /* 64-bit unsigned integer */
15 #define Elf64_Uint Elf64_Xword
18 # error "Undefined ELF word length"
21 static void *PERBIT(get_section
)(struct elf_file
*module
,
23 ElfPERBIT(Shdr
) **sechdr
,
24 unsigned long *secsize
)
26 void *data
= module
->data
;
27 unsigned long len
= module
->len
;
28 int conv
= module
->conv
;
31 ElfPERBIT(Shdr
) *sechdrs
;
32 ElfPERBIT(Off
) e_shoff
;
33 ElfPERBIT(Half
) e_shnum
, e_shstrndx
;
34 ElfPERBIT(Off
) secoffset
;
41 if (len
<= 0 || len
< sizeof(*hdr
))
45 e_shoff
= END(hdr
->e_shoff
, conv
);
46 e_shnum
= END(hdr
->e_shnum
, conv
);
47 e_shstrndx
= END(hdr
->e_shstrndx
, conv
);
49 if (len
< e_shoff
+ e_shnum
* sizeof(sechdrs
[0]))
52 sechdrs
= data
+ e_shoff
;
54 if (len
< END(sechdrs
[e_shstrndx
].sh_offset
, conv
))
57 /* Find section by name; return header, pointer and size. */
58 secnames
= data
+ END(sechdrs
[e_shstrndx
].sh_offset
, conv
);
59 for (i
= 1; i
< e_shnum
; i
++) {
60 if (streq(secnames
+ END(sechdrs
[i
].sh_name
, conv
), secname
)) {
61 *secsize
= END(sechdrs
[i
].sh_size
, conv
);
62 secoffset
= END(sechdrs
[i
].sh_offset
, conv
);
64 *sechdr
= sechdrs
+ i
;
65 if (len
< secoffset
+ *secsize
)
67 return data
+ secoffset
;
73 /* Load the given section: NULL on error. */
74 static void *PERBIT(load_section
)(struct elf_file
*module
,
76 unsigned long *secsize
)
78 return PERBIT(get_section
)(module
, secname
, NULL
, secsize
);
81 static struct string_table
*PERBIT(load_strings
)(struct elf_file
*module
,
83 struct string_table
*tbl
,
89 strings
= PERBIT(load_section
)(module
, secname
, &size
);
91 /* Skip any zero padding. */
97 for (; strings
; strings
= next_string(strings
, &size
))
98 tbl
= NOFAIL(strtbl_add(strings
, tbl
));
103 static struct string_table
*PERBIT(load_symbols
)(struct elf_file
*module
,
106 struct string_table
*symtbl
= NULL
;
109 static const char crc
[] = "__crc_";
110 static const int crc_len
= sizeof(crc
) - 1;
111 unsigned int num_syms
, i
;
113 ElfPERBIT(Sym
) *syms
;
118 strings
= PERBIT(load_section
)(module
, ".strtab", &size
);
119 syms
= PERBIT(load_section
)(module
, ".symtab", &size
);
120 if (!strings
|| !syms
)
122 num_syms
= size
/ sizeof(syms
[0]);
123 *versions
= NOFAIL(calloc(sizeof(**versions
), num_syms
));
126 for (i
= 1; i
< num_syms
; i
++) {
128 name
= strings
+ END(syms
[i
].st_name
, conv
);
129 if (strncmp(name
, crc
, crc_len
) != 0)
132 symtbl
= NOFAIL(strtbl_add(name
, symtbl
));
133 (*versions
)[symtbl
->cnt
- 1] = END(syms
[i
].st_value
,
137 /* Either this module does not export any symbols, or
138 * it was compiled without CONFIG_MODVERSIONS. If the
139 * latter, we will print a warning in load_dep_syms,
140 * so just silently fallback to __ksymtab_strings in
150 return PERBIT(load_strings
)(module
, "__ksymtab_strings", symtbl
,
154 static char *PERBIT(get_aliases
)(struct elf_file
*module
, unsigned long *size
)
156 return PERBIT(load_section
)(module
, ".modalias", size
);
159 static char *PERBIT(get_modinfo
)(struct elf_file
*module
, unsigned long *size
)
161 return PERBIT(load_section
)(module
, ".modinfo", size
);
165 #define STT_REGISTER 13 /* Global register reserved to app. */
168 static struct string_table
*PERBIT(load_dep_syms
)(struct elf_file
*module
,
169 struct string_table
**types
,
172 unsigned int i
, num_syms
;
173 unsigned int j
, num_symvers
, versions_size
;
176 ElfPERBIT(Sym
) *syms
;
177 ElfPERBIT(Ehdr
) *hdr
;
178 struct PERBIT(modver_info
) **symvers
;
179 int handle_register_symbols
;
180 struct string_table
*names
;
186 num_symvers
= versions_size
= 0;
191 struct PERBIT(modver_info
) *symvers_sec
;
193 symvers_sec
= module
->ops
->load_section(module
, "__versions",
196 warn("%s is built without modversions",
200 if (size
% sizeof(symvers
[0]) != 0) {
201 warn("invalid __versions section size in %s",
206 num_symvers
= size
/ sizeof(symvers_sec
[0]);
207 /* symvers is used to keep track of each visited entry.
208 * The table also contains the fake struct_module /
209 * module_layout symbol which we don't want to miss.
211 symvers
= NOFAIL(malloc(num_symvers
*
212 sizeof(symvers
[0])));
213 for (j
= 0; j
< num_symvers
; j
++)
214 symvers
[j
] = &symvers_sec
[j
];
220 strings
= PERBIT(load_section
)(module
, ".strtab", &size
);
221 syms
= PERBIT(load_section
)(module
, ".symtab", &size
);
222 if (!strings
|| !syms
) {
223 warn("Couldn't find symtab and strtab in module %s\n",
228 num_syms
= size
/ sizeof(syms
[0]);
232 versions_size
= num_syms
;
233 *versions
= NOFAIL(calloc(sizeof(**versions
), versions_size
));
236 handle_register_symbols
=
237 (END(hdr
->e_machine
, conv
) == EM_SPARC
||
238 END(hdr
->e_machine
, conv
) == EM_SPARCV9
);
240 for (i
= 1; i
< num_syms
; i
++) {
241 if (END(syms
[i
].st_shndx
, conv
) == SHN_UNDEF
) {
242 /* Look for symbol */
246 name
= strings
+ END(syms
[i
].st_name
, conv
);
248 /* Not really undefined: sparc gcc 3.3 creates
249 U references when you have global asm
250 variables, to avoid anyone else misusing
252 if (handle_register_symbols
253 && (ELFPERBIT(ST_TYPE
)(END(syms
[i
].st_info
, conv
))
257 weak
= (ELFPERBIT(ST_BIND
)(END(syms
[i
].st_info
, conv
))
259 names
= NOFAIL(strtbl_add(name
, names
));
260 *types
= NOFAIL(strtbl_add(weak
? weak_sym
: undef_sym
,
265 /* Not optimal, but the number of required symbols
266 * is usually not huge and this is only called by
269 for (j
= 0; j
< num_symvers
; j
++) {
270 struct PERBIT(modver_info
) *info
= symvers
[j
];
274 if (streq(name
, info
->name
)) {
275 (*versions
)[names
->cnt
- 1] =
276 END(info
->crc
, conv
);
283 /* add struct_module / module_layout */
284 for (j
= 0; j
< num_symvers
; j
++) {
285 struct PERBIT(modver_info
) *info
= symvers
[j
];
289 if ((names
? names
->cnt
: 0) >= versions_size
) {
291 *versions
= NOFAIL(realloc(*versions
, versions_size
));
293 names
= NOFAIL(strtbl_add(info
->name
, names
));
294 *types
= NOFAIL(strtbl_add(undef_sym
, *types
));
295 (*versions
)[names
->cnt
- 1] = END(info
->crc
, conv
);
302 static void *PERBIT(deref_sym
)(ElfPERBIT(Ehdr
) *hdr
,
303 ElfPERBIT(Shdr
) *sechdrs
,
305 unsigned int *secsize
,
308 /* In BSS? Happens for empty device tables on
309 * recent GCC versions. */
310 if (END(sechdrs
[END(sym
->st_shndx
, conv
)].sh_type
,conv
) == SHT_NOBITS
)
314 *secsize
= END(sym
->st_size
, conv
);
316 + END(sechdrs
[END(sym
->st_shndx
, conv
)].sh_offset
, conv
)
317 + END(sym
->st_value
, conv
);
320 /* FIXME: Check size, unless we end up using aliases anyway --RR */
321 static void PERBIT(fetch_tables
)(struct elf_file
*module
,
322 struct module_tables
*tables
)
327 ElfPERBIT(Ehdr
) *hdr
;
328 ElfPERBIT(Sym
) *syms
;
329 ElfPERBIT(Shdr
) *sechdrs
;
335 sechdrs
= (void *)hdr
+ END(hdr
->e_shoff
, conv
);
336 strings
= PERBIT(load_section
)(module
, ".strtab", &size
);
337 syms
= PERBIT(load_section
)(module
, ".symtab", &size
);
339 /* Don't warn again: we already have above */
340 if (!strings
|| !syms
)
343 memset(tables
, 0x00, sizeof(struct module_tables
));
345 for (i
= 0; i
< size
/ sizeof(syms
[0]); i
++) {
346 char *name
= strings
+ END(syms
[i
].st_name
, conv
);
348 if (!tables
->pci_table
&& streq(name
, "__mod_pci_device_table")) {
349 tables
->pci_size
= PERBIT(PCI_DEVICE_SIZE
);
350 tables
->pci_table
= PERBIT(deref_sym
)(hdr
, sechdrs
, &syms
[i
],
353 else if (!tables
->usb_table
&& streq(name
, "__mod_usb_device_table")) {
354 tables
->usb_size
= PERBIT(USB_DEVICE_SIZE
);
355 tables
->usb_table
= PERBIT(deref_sym
)(hdr
, sechdrs
, &syms
[i
],
358 else if (!tables
->ccw_table
&& streq(name
, "__mod_ccw_device_table")) {
359 tables
->ccw_size
= PERBIT(CCW_DEVICE_SIZE
);
360 tables
->ccw_table
= PERBIT(deref_sym
)(hdr
, sechdrs
, &syms
[i
],
363 else if (!tables
->ieee1394_table
&& streq(name
, "__mod_ieee1394_device_table")) {
364 tables
->ieee1394_size
= PERBIT(IEEE1394_DEVICE_SIZE
);
365 tables
->ieee1394_table
= PERBIT(deref_sym
)(hdr
, sechdrs
, &syms
[i
],
368 else if (!tables
->pnp_table
&& streq(name
, "__mod_pnp_device_table")) {
369 tables
->pnp_size
= PERBIT(PNP_DEVICE_SIZE
);
370 tables
->pnp_table
= PERBIT(deref_sym
)(hdr
, sechdrs
, &syms
[i
],
373 else if (!tables
->pnp_card_table
&& streq(name
, "__mod_pnp_card_device_table")) {
374 tables
->pnp_card_size
= PERBIT(PNP_CARD_DEVICE_SIZE
);
375 tables
->pnp_card_table
= PERBIT(deref_sym
)(hdr
, sechdrs
, &syms
[i
],
377 tables
->pnp_card_offset
= PERBIT(PNP_CARD_DEVICE_OFFSET
);
379 else if (!tables
->input_table
&& streq(name
, "__mod_input_device_table")) {
380 tables
->input_size
= PERBIT(INPUT_DEVICE_SIZE
);
381 tables
->input_table
= PERBIT(deref_sym
)(hdr
, sechdrs
, &syms
[i
],
382 &tables
->input_table_size
,
385 else if (!tables
->serio_table
&& streq(name
, "__mod_serio_device_table")) {
386 tables
->serio_size
= PERBIT(SERIO_DEVICE_SIZE
);
387 tables
->serio_table
= PERBIT(deref_sym
)(hdr
, sechdrs
, &syms
[i
],
390 else if (!tables
->of_table
&& streq(name
, "__mod_of_device_table")) {
391 tables
->of_size
= PERBIT(OF_DEVICE_SIZE
);
392 tables
->of_table
= PERBIT(deref_sym
)(hdr
, sechdrs
, &syms
[i
],
399 * strip_section - tell the kernel to ignore the named section
401 static void PERBIT(strip_section
)(struct elf_file
*module
, const char *secname
)
404 ElfPERBIT(Shdr
) *sechdr
;
405 unsigned long secsize
;
407 p
= PERBIT(get_section
)(module
, secname
, &sechdr
, &secsize
);
409 ElfPERBIT(Uint
) mask
;
410 mask
= ~((ElfPERBIT(Uint
))SHF_ALLOC
);
411 sechdr
->sh_flags
&= END(mask
, module
->conv
);
415 static int PERBIT(dump_modversions
)(struct elf_file
*module
)
417 unsigned long secsize
;
418 struct PERBIT(modver_info
) *info
;
421 info
= module
->ops
->load_section(module
, "__versions", &secsize
);
423 return 0; /* not a kernel module */
424 if (secsize
% sizeof(*info
) != 0)
425 return -1; /* invalid section size */
427 for (n
= 0; n
< secsize
/ sizeof(*info
); n
++) {
428 #if defined(ELF32BIT)
429 printf("0x%08lx\t%s\n", (unsigned long)
430 #else /* defined(ELF64BIT) */
431 printf("0x%08llx\t%s\n", (unsigned long long)
433 END(info
[n
].crc
, module
->conv
),
434 skip_dot(info
[n
].name
));
439 struct module_ops
PERBIT(mod_ops
) = {
440 .load_section
= PERBIT(load_section
),
441 .load_strings
= PERBIT(load_strings
),
442 .load_symbols
= PERBIT(load_symbols
),
443 .load_dep_syms
= PERBIT(load_dep_syms
),
444 .fetch_tables
= PERBIT(fetch_tables
),
445 .get_aliases
= PERBIT(get_aliases
),
446 .get_modinfo
= PERBIT(get_modinfo
),
447 .strip_section
= PERBIT(strip_section
),
448 .dump_modvers
= PERBIT(dump_modversions
),