Contents of /tags/mkinitrd-6_1_7/busybox/modutils/modutils-24.c
Parent Directory | Revision Log
Revision 909 -
(show annotations)
(download)
Fri Oct 16 13:40:14 2009 UTC (14 years, 11 months ago) by niro
File MIME type: text/plain
File size: 97324 byte(s)
Fri Oct 16 13:40:14 2009 UTC (14 years, 11 months ago) by niro
File MIME type: text/plain
File size: 97324 byte(s)
tagged 'mkinitrd-6_1_7'
1 | /* vi: set sw=4 ts=4: */ |
2 | /* |
3 | * Mini insmod implementation for busybox |
4 | * |
5 | * This version of insmod supports ARM, CRIS, H8/300, x86, ia64, x86_64, |
6 | * m68k, MIPS, PowerPC, S390, SH3/4/5, Sparc, v850e, and x86_64. |
7 | * |
8 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
9 | * and Ron Alder <alder@lineo.com> |
10 | * |
11 | * Rodney Radford <rradford@mindspring.com> 17-Aug-2004. |
12 | * Added x86_64 support. |
13 | * |
14 | * Miles Bader <miles@gnu.org> added NEC V850E support. |
15 | * |
16 | * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4 |
17 | * and (theoretically) SH3. I have only tested SH4 in little endian mode. |
18 | * |
19 | * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and |
20 | * Nicolas Ferre <nicolas.ferre@alcove.fr> to support ARM7TDMI. Only |
21 | * very minor changes required to also work with StrongArm and presumably |
22 | * all ARM based systems. |
23 | * |
24 | * Yoshinori Sato <ysato@users.sourceforge.jp> 19-May-2004. |
25 | * added Renesas H8/300 support. |
26 | * |
27 | * Paul Mundt <lethal@linux-sh.org> 08-Aug-2003. |
28 | * Integrated support for sh64 (SH-5), from preliminary modutils |
29 | * patches from Benedict Gaster <benedict.gaster@superh.com>. |
30 | * Currently limited to support for 32bit ABI. |
31 | * |
32 | * Magnus Damm <damm@opensource.se> 22-May-2002. |
33 | * The plt and got code are now using the same structs. |
34 | * Added generic linked list code to fully support PowerPC. |
35 | * Replaced the mess in arch_apply_relocation() with architecture blocks. |
36 | * The arch_create_got() function got cleaned up with architecture blocks. |
37 | * These blocks should be easy maintain and sync with obj_xxx.c in modutils. |
38 | * |
39 | * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001. |
40 | * PowerPC specific code stolen from modutils-2.3.16, |
41 | * written by Paul Mackerras, Copyright 1996, 1997 Linux International. |
42 | * I've only tested the code on mpc8xx platforms in big-endian mode. |
43 | * Did some cleanup and added USE_xxx_ENTRIES... |
44 | * |
45 | * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001. |
46 | * based on modutils-2.4.2 |
47 | * MIPS specific support for Elf loading and relocation. |
48 | * Copyright 1996, 1997 Linux International. |
49 | * Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu> |
50 | * |
51 | * Based almost entirely on the Linux modutils-2.3.11 implementation. |
52 | * Copyright 1996, 1997 Linux International. |
53 | * New implementation contributed by Richard Henderson <rth@tamu.edu> |
54 | * Based on original work by Bjorn Ekwall <bj0rn@blox.se> |
55 | * Restructured (and partly rewritten) by: |
56 | * Björn Ekwall <bj0rn@blox.se> February 1999 |
57 | * |
58 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
59 | */ |
60 | |
61 | #include "libbb.h" |
62 | #include "modutils.h" |
63 | #include <libgen.h> |
64 | #include <sys/utsname.h> |
65 | |
66 | #if ENABLE_FEATURE_INSMOD_LOADINKMEM |
67 | #define LOADBITS 0 |
68 | #else |
69 | #define LOADBITS 1 |
70 | #endif |
71 | |
72 | /* Alpha */ |
73 | #if defined(__alpha__) |
74 | #define MATCH_MACHINE(x) (x == EM_ALPHA) |
75 | #define SHT_RELM SHT_RELA |
76 | #define Elf64_RelM Elf64_Rela |
77 | #define ELFCLASSM ELFCLASS64 |
78 | #endif |
79 | |
80 | /* ARM support */ |
81 | #if defined(__arm__) |
82 | #define MATCH_MACHINE(x) (x == EM_ARM) |
83 | #define SHT_RELM SHT_REL |
84 | #define Elf32_RelM Elf32_Rel |
85 | #define ELFCLASSM ELFCLASS32 |
86 | #define USE_PLT_ENTRIES |
87 | #define PLT_ENTRY_SIZE 8 |
88 | #define USE_GOT_ENTRIES |
89 | #define GOT_ENTRY_SIZE 8 |
90 | #define USE_SINGLE |
91 | #endif |
92 | |
93 | /* blackfin */ |
94 | #if defined(BFIN) |
95 | #define MATCH_MACHINE(x) (x == EM_BLACKFIN) |
96 | #define SHT_RELM SHT_RELA |
97 | #define Elf32_RelM Elf32_Rela |
98 | #define ELFCLASSM ELFCLASS32 |
99 | #endif |
100 | |
101 | /* CRIS */ |
102 | #if defined(__cris__) |
103 | #define MATCH_MACHINE(x) (x == EM_CRIS) |
104 | #define SHT_RELM SHT_RELA |
105 | #define Elf32_RelM Elf32_Rela |
106 | #define ELFCLASSM ELFCLASS32 |
107 | #ifndef EM_CRIS |
108 | #define EM_CRIS 76 |
109 | #define R_CRIS_NONE 0 |
110 | #define R_CRIS_32 3 |
111 | #endif |
112 | #endif |
113 | |
114 | /* H8/300 */ |
115 | #if defined(__H8300H__) || defined(__H8300S__) |
116 | #define MATCH_MACHINE(x) (x == EM_H8_300) |
117 | #define SHT_RELM SHT_RELA |
118 | #define Elf32_RelM Elf32_Rela |
119 | #define ELFCLASSM ELFCLASS32 |
120 | #define USE_SINGLE |
121 | #define SYMBOL_PREFIX "_" |
122 | #endif |
123 | |
124 | /* PA-RISC / HP-PA */ |
125 | #if defined(__hppa__) |
126 | #define MATCH_MACHINE(x) (x == EM_PARISC) |
127 | #define SHT_RELM SHT_RELA |
128 | #if defined(__LP64__) |
129 | #define Elf64_RelM Elf64_Rela |
130 | #define ELFCLASSM ELFCLASS64 |
131 | #else |
132 | #define Elf32_RelM Elf32_Rela |
133 | #define ELFCLASSM ELFCLASS32 |
134 | #endif |
135 | #endif |
136 | |
137 | /* x86 */ |
138 | #if defined(__i386__) |
139 | #ifndef EM_486 |
140 | #define MATCH_MACHINE(x) (x == EM_386) |
141 | #else |
142 | #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486) |
143 | #endif |
144 | #define SHT_RELM SHT_REL |
145 | #define Elf32_RelM Elf32_Rel |
146 | #define ELFCLASSM ELFCLASS32 |
147 | #define USE_GOT_ENTRIES |
148 | #define GOT_ENTRY_SIZE 4 |
149 | #define USE_SINGLE |
150 | #endif |
151 | |
152 | /* IA64, aka Itanium */ |
153 | #if defined(__ia64__) |
154 | #define MATCH_MACHINE(x) (x == EM_IA_64) |
155 | #define SHT_RELM SHT_RELA |
156 | #define Elf64_RelM Elf64_Rela |
157 | #define ELFCLASSM ELFCLASS64 |
158 | #endif |
159 | |
160 | /* m68k */ |
161 | #if defined(__mc68000__) |
162 | #define MATCH_MACHINE(x) (x == EM_68K) |
163 | #define SHT_RELM SHT_RELA |
164 | #define Elf32_RelM Elf32_Rela |
165 | #define ELFCLASSM ELFCLASS32 |
166 | #define USE_GOT_ENTRIES |
167 | #define GOT_ENTRY_SIZE 4 |
168 | #define USE_SINGLE |
169 | #endif |
170 | |
171 | /* Microblaze */ |
172 | #if defined(__microblaze__) |
173 | #define USE_SINGLE |
174 | #include <linux/elf-em.h> |
175 | #define MATCH_MACHINE(x) (x == EM_XILINX_MICROBLAZE) |
176 | #define SHT_RELM SHT_RELA |
177 | #define Elf32_RelM Elf32_Rela |
178 | #define ELFCLASSM ELFCLASS32 |
179 | #endif |
180 | |
181 | /* MIPS */ |
182 | #if defined(__mips__) |
183 | #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE) |
184 | #define SHT_RELM SHT_REL |
185 | #define Elf32_RelM Elf32_Rel |
186 | #define ELFCLASSM ELFCLASS32 |
187 | /* Account for ELF spec changes. */ |
188 | #ifndef EM_MIPS_RS3_LE |
189 | #ifdef EM_MIPS_RS4_BE |
190 | #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE |
191 | #else |
192 | #define EM_MIPS_RS3_LE 10 |
193 | #endif |
194 | #endif /* !EM_MIPS_RS3_LE */ |
195 | #define ARCHDATAM "__dbe_table" |
196 | #endif |
197 | |
198 | /* Nios II */ |
199 | #if defined(__nios2__) |
200 | #define MATCH_MACHINE(x) (x == EM_ALTERA_NIOS2) |
201 | #define SHT_RELM SHT_RELA |
202 | #define Elf32_RelM Elf32_Rela |
203 | #define ELFCLASSM ELFCLASS32 |
204 | #endif |
205 | |
206 | /* PowerPC */ |
207 | #if defined(__powerpc64__) |
208 | #define MATCH_MACHINE(x) (x == EM_PPC64) |
209 | #define SHT_RELM SHT_RELA |
210 | #define Elf64_RelM Elf64_Rela |
211 | #define ELFCLASSM ELFCLASS64 |
212 | #elif defined(__powerpc__) |
213 | #define MATCH_MACHINE(x) (x == EM_PPC) |
214 | #define SHT_RELM SHT_RELA |
215 | #define Elf32_RelM Elf32_Rela |
216 | #define ELFCLASSM ELFCLASS32 |
217 | #define USE_PLT_ENTRIES |
218 | #define PLT_ENTRY_SIZE 16 |
219 | #define USE_PLT_LIST |
220 | #define LIST_ARCHTYPE ElfW(Addr) |
221 | #define USE_LIST |
222 | #define ARCHDATAM "__ftr_fixup" |
223 | #endif |
224 | |
225 | /* S390 */ |
226 | #if defined(__s390__) |
227 | #define MATCH_MACHINE(x) (x == EM_S390) |
228 | #define SHT_RELM SHT_RELA |
229 | #define Elf32_RelM Elf32_Rela |
230 | #define ELFCLASSM ELFCLASS32 |
231 | #define USE_PLT_ENTRIES |
232 | #define PLT_ENTRY_SIZE 8 |
233 | #define USE_GOT_ENTRIES |
234 | #define GOT_ENTRY_SIZE 8 |
235 | #define USE_SINGLE |
236 | #endif |
237 | |
238 | /* SuperH */ |
239 | #if defined(__sh__) |
240 | #define MATCH_MACHINE(x) (x == EM_SH) |
241 | #define SHT_RELM SHT_RELA |
242 | #define Elf32_RelM Elf32_Rela |
243 | #define ELFCLASSM ELFCLASS32 |
244 | #define USE_GOT_ENTRIES |
245 | #define GOT_ENTRY_SIZE 4 |
246 | #define USE_SINGLE |
247 | /* the SH changes have only been tested in =little endian= mode */ |
248 | /* I'm not sure about big endian, so let's warn: */ |
249 | #if defined(__sh__) && BB_BIG_ENDIAN |
250 | # error insmod.c may require changes for use on big endian SH |
251 | #endif |
252 | /* it may or may not work on the SH1/SH2... Error on those also */ |
253 | #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && (defined(__sh__)) |
254 | #error insmod.c may require changes for SH1 or SH2 use |
255 | #endif |
256 | #endif |
257 | |
258 | /* Sparc */ |
259 | #if defined(__sparc__) |
260 | #define MATCH_MACHINE(x) (x == EM_SPARC) |
261 | #define SHT_RELM SHT_RELA |
262 | #define Elf32_RelM Elf32_Rela |
263 | #define ELFCLASSM ELFCLASS32 |
264 | #endif |
265 | |
266 | /* v850e */ |
267 | #if defined(__v850e__) |
268 | #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850) |
269 | #define SHT_RELM SHT_RELA |
270 | #define Elf32_RelM Elf32_Rela |
271 | #define ELFCLASSM ELFCLASS32 |
272 | #define USE_PLT_ENTRIES |
273 | #define PLT_ENTRY_SIZE 8 |
274 | #define USE_SINGLE |
275 | #ifndef EM_CYGNUS_V850 /* grumble */ |
276 | #define EM_CYGNUS_V850 0x9080 |
277 | #endif |
278 | #define SYMBOL_PREFIX "_" |
279 | #endif |
280 | |
281 | /* X86_64 */ |
282 | #if defined(__x86_64__) |
283 | #define MATCH_MACHINE(x) (x == EM_X86_64) |
284 | #define SHT_RELM SHT_RELA |
285 | #define USE_GOT_ENTRIES |
286 | #define GOT_ENTRY_SIZE 8 |
287 | #define USE_SINGLE |
288 | #define Elf64_RelM Elf64_Rela |
289 | #define ELFCLASSM ELFCLASS64 |
290 | #endif |
291 | |
292 | #ifndef SHT_RELM |
293 | #error Sorry, but insmod.c does not yet support this architecture... |
294 | #endif |
295 | |
296 | |
297 | //---------------------------------------------------------------------------- |
298 | //--------modutils module.h, lines 45-242 |
299 | //---------------------------------------------------------------------------- |
300 | |
301 | /* Definitions for the Linux module syscall interface. |
302 | Copyright 1996, 1997 Linux International. |
303 | |
304 | Contributed by Richard Henderson <rth@tamu.edu> |
305 | |
306 | This file is part of the Linux modutils. |
307 | |
308 | This program is free software; you can redistribute it and/or modify it |
309 | under the terms of the GNU General Public License as published by the |
310 | Free Software Foundation; either version 2 of the License, or (at your |
311 | option) any later version. |
312 | |
313 | This program is distributed in the hope that it will be useful, but |
314 | WITHOUT ANY WARRANTY; without even the implied warranty of |
315 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
316 | General Public License for more details. |
317 | |
318 | You should have received a copy of the GNU General Public License |
319 | along with this program; if not, write to the Free Software Foundation, |
320 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
321 | |
322 | |
323 | #ifndef MODUTILS_MODULE_H |
324 | |
325 | /*======================================================================*/ |
326 | /* For sizeof() which are related to the module platform and not to the |
327 | environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */ |
328 | |
329 | #define tgt_sizeof_char sizeof(char) |
330 | #define tgt_sizeof_short sizeof(short) |
331 | #define tgt_sizeof_int sizeof(int) |
332 | #define tgt_sizeof_long sizeof(long) |
333 | #define tgt_sizeof_char_p sizeof(char *) |
334 | #define tgt_sizeof_void_p sizeof(void *) |
335 | #define tgt_long long |
336 | |
337 | #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64) |
338 | #undef tgt_sizeof_long |
339 | #undef tgt_sizeof_char_p |
340 | #undef tgt_sizeof_void_p |
341 | #undef tgt_long |
342 | enum { |
343 | tgt_sizeof_long = 8, |
344 | tgt_sizeof_char_p = 8, |
345 | tgt_sizeof_void_p = 8 |
346 | }; |
347 | #define tgt_long long long |
348 | #endif |
349 | |
350 | /*======================================================================*/ |
351 | /* The structures used in Linux 2.1. */ |
352 | |
353 | /* Note: new_module_symbol does not use tgt_long intentionally */ |
354 | struct new_module_symbol { |
355 | unsigned long value; |
356 | unsigned long name; |
357 | }; |
358 | |
359 | struct new_module_persist; |
360 | |
361 | struct new_module_ref { |
362 | unsigned tgt_long dep; /* kernel addresses */ |
363 | unsigned tgt_long ref; |
364 | unsigned tgt_long next_ref; |
365 | }; |
366 | |
367 | struct new_module { |
368 | unsigned tgt_long size_of_struct; /* == sizeof(module) */ |
369 | unsigned tgt_long next; |
370 | unsigned tgt_long name; |
371 | unsigned tgt_long size; |
372 | |
373 | tgt_long usecount; |
374 | unsigned tgt_long flags; /* AUTOCLEAN et al */ |
375 | |
376 | unsigned nsyms; |
377 | unsigned ndeps; |
378 | |
379 | unsigned tgt_long syms; |
380 | unsigned tgt_long deps; |
381 | unsigned tgt_long refs; |
382 | unsigned tgt_long init; |
383 | unsigned tgt_long cleanup; |
384 | unsigned tgt_long ex_table_start; |
385 | unsigned tgt_long ex_table_end; |
386 | #ifdef __alpha__ |
387 | unsigned tgt_long gp; |
388 | #endif |
389 | /* Everything after here is extension. */ |
390 | unsigned tgt_long persist_start; |
391 | unsigned tgt_long persist_end; |
392 | unsigned tgt_long can_unload; |
393 | unsigned tgt_long runsize; |
394 | const char *kallsyms_start; /* All symbols for kernel debugging */ |
395 | const char *kallsyms_end; |
396 | const char *archdata_start; /* arch specific data for module */ |
397 | const char *archdata_end; |
398 | const char *kernel_data; /* Reserved for kernel internal use */ |
399 | }; |
400 | |
401 | #ifdef ARCHDATAM |
402 | #define ARCHDATA_SEC_NAME ARCHDATAM |
403 | #else |
404 | #define ARCHDATA_SEC_NAME "__archdata" |
405 | #endif |
406 | #define KALLSYMS_SEC_NAME "__kallsyms" |
407 | |
408 | |
409 | struct new_module_info { |
410 | unsigned long addr; |
411 | unsigned long size; |
412 | unsigned long flags; |
413 | long usecount; |
414 | }; |
415 | |
416 | /* Bits of module.flags. */ |
417 | enum { |
418 | NEW_MOD_RUNNING = 1, |
419 | NEW_MOD_DELETED = 2, |
420 | NEW_MOD_AUTOCLEAN = 4, |
421 | NEW_MOD_VISITED = 8, |
422 | NEW_MOD_USED_ONCE = 16 |
423 | }; |
424 | |
425 | int init_module(const char *name, const struct new_module *); |
426 | int query_module(const char *name, int which, void *buf, |
427 | size_t bufsize, size_t *ret); |
428 | |
429 | /* Values for query_module's which. */ |
430 | enum { |
431 | QM_MODULES = 1, |
432 | QM_DEPS = 2, |
433 | QM_REFS = 3, |
434 | QM_SYMBOLS = 4, |
435 | QM_INFO = 5 |
436 | }; |
437 | |
438 | /*======================================================================*/ |
439 | /* The system calls unchanged between 2.0 and 2.1. */ |
440 | |
441 | unsigned long create_module(const char *, size_t); |
442 | int delete_module(const char *module, unsigned int flags); |
443 | |
444 | |
445 | #endif /* module.h */ |
446 | |
447 | //---------------------------------------------------------------------------- |
448 | //--------end of modutils module.h |
449 | //---------------------------------------------------------------------------- |
450 | |
451 | |
452 | |
453 | //---------------------------------------------------------------------------- |
454 | //--------modutils obj.h, lines 253-462 |
455 | //---------------------------------------------------------------------------- |
456 | |
457 | /* Elf object file loading and relocation routines. |
458 | Copyright 1996, 1997 Linux International. |
459 | |
460 | Contributed by Richard Henderson <rth@tamu.edu> |
461 | |
462 | This file is part of the Linux modutils. |
463 | |
464 | This program is free software; you can redistribute it and/or modify it |
465 | under the terms of the GNU General Public License as published by the |
466 | Free Software Foundation; either version 2 of the License, or (at your |
467 | option) any later version. |
468 | |
469 | This program is distributed in the hope that it will be useful, but |
470 | WITHOUT ANY WARRANTY; without even the implied warranty of |
471 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
472 | General Public License for more details. |
473 | |
474 | You should have received a copy of the GNU General Public License |
475 | along with this program; if not, write to the Free Software Foundation, |
476 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
477 | |
478 | |
479 | #ifndef MODUTILS_OBJ_H |
480 | |
481 | /* The relocatable object is manipulated using elfin types. */ |
482 | |
483 | #include <elf.h> |
484 | #include <endian.h> |
485 | |
486 | #ifndef ElfW |
487 | # if ELFCLASSM == ELFCLASS32 |
488 | # define ElfW(x) Elf32_ ## x |
489 | # define ELFW(x) ELF32_ ## x |
490 | # else |
491 | # define ElfW(x) Elf64_ ## x |
492 | # define ELFW(x) ELF64_ ## x |
493 | # endif |
494 | #endif |
495 | |
496 | /* For some reason this is missing from some ancient C libraries.... */ |
497 | #ifndef ELF32_ST_INFO |
498 | # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf)) |
499 | #endif |
500 | |
501 | #ifndef ELF64_ST_INFO |
502 | # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf)) |
503 | #endif |
504 | |
505 | #define ELF_ST_BIND(info) ELFW(ST_BIND)(info) |
506 | #define ELF_ST_TYPE(info) ELFW(ST_TYPE)(info) |
507 | #define ELF_ST_INFO(bind, type) ELFW(ST_INFO)(bind, type) |
508 | #define ELF_R_TYPE(val) ELFW(R_TYPE)(val) |
509 | #define ELF_R_SYM(val) ELFW(R_SYM)(val) |
510 | |
511 | struct obj_string_patch; |
512 | struct obj_symbol_patch; |
513 | |
514 | struct obj_section |
515 | { |
516 | ElfW(Shdr) header; |
517 | const char *name; |
518 | char *contents; |
519 | struct obj_section *load_next; |
520 | int idx; |
521 | }; |
522 | |
523 | struct obj_symbol |
524 | { |
525 | struct obj_symbol *next; /* hash table link */ |
526 | const char *name; |
527 | unsigned long value; |
528 | unsigned long size; |
529 | int secidx; /* the defining section index/module */ |
530 | int info; |
531 | int ksymidx; /* for export to the kernel symtab */ |
532 | int referenced; /* actually used in the link */ |
533 | }; |
534 | |
535 | /* Hardcode the hash table size. We shouldn't be needing so many |
536 | symbols that we begin to degrade performance, and we get a big win |
537 | by giving the compiler a constant divisor. */ |
538 | |
539 | #define HASH_BUCKETS 521 |
540 | |
541 | struct obj_file { |
542 | ElfW(Ehdr) header; |
543 | ElfW(Addr) baseaddr; |
544 | struct obj_section **sections; |
545 | struct obj_section *load_order; |
546 | struct obj_section **load_order_search_start; |
547 | struct obj_string_patch *string_patches; |
548 | struct obj_symbol_patch *symbol_patches; |
549 | int (*symbol_cmp)(const char *, const char *); |
550 | unsigned long (*symbol_hash)(const char *); |
551 | unsigned long local_symtab_size; |
552 | struct obj_symbol **local_symtab; |
553 | struct obj_symbol *symtab[HASH_BUCKETS]; |
554 | }; |
555 | |
556 | enum obj_reloc { |
557 | obj_reloc_ok, |
558 | obj_reloc_overflow, |
559 | obj_reloc_dangerous, |
560 | obj_reloc_unhandled |
561 | }; |
562 | |
563 | struct obj_string_patch { |
564 | struct obj_string_patch *next; |
565 | int reloc_secidx; |
566 | ElfW(Addr) reloc_offset; |
567 | ElfW(Addr) string_offset; |
568 | }; |
569 | |
570 | struct obj_symbol_patch { |
571 | struct obj_symbol_patch *next; |
572 | int reloc_secidx; |
573 | ElfW(Addr) reloc_offset; |
574 | struct obj_symbol *sym; |
575 | }; |
576 | |
577 | |
578 | /* Generic object manipulation routines. */ |
579 | |
580 | static unsigned long obj_elf_hash(const char *); |
581 | |
582 | static unsigned long obj_elf_hash_n(const char *, unsigned long len); |
583 | |
584 | static struct obj_symbol *obj_find_symbol(struct obj_file *f, |
585 | const char *name); |
586 | |
587 | static ElfW(Addr) obj_symbol_final_value(struct obj_file *f, |
588 | struct obj_symbol *sym); |
589 | |
590 | #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING |
591 | static void obj_set_symbol_compare(struct obj_file *f, |
592 | int (*cmp)(const char *, const char *), |
593 | unsigned long (*hash)(const char *)); |
594 | #endif |
595 | |
596 | static struct obj_section *obj_find_section(struct obj_file *f, |
597 | const char *name); |
598 | |
599 | static void obj_insert_section_load_order(struct obj_file *f, |
600 | struct obj_section *sec); |
601 | |
602 | static struct obj_section *obj_create_alloced_section(struct obj_file *f, |
603 | const char *name, |
604 | unsigned long align, |
605 | unsigned long size); |
606 | |
607 | static struct obj_section *obj_create_alloced_section_first(struct obj_file *f, |
608 | const char *name, |
609 | unsigned long align, |
610 | unsigned long size); |
611 | |
612 | static void *obj_extend_section(struct obj_section *sec, unsigned long more); |
613 | |
614 | static void obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset, |
615 | const char *string); |
616 | |
617 | static void obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset, |
618 | struct obj_symbol *sym); |
619 | |
620 | static void obj_check_undefineds(struct obj_file *f); |
621 | |
622 | static void obj_allocate_commons(struct obj_file *f); |
623 | |
624 | static unsigned long obj_load_size(struct obj_file *f); |
625 | |
626 | static int obj_relocate(struct obj_file *f, ElfW(Addr) base); |
627 | |
628 | static struct obj_file *obj_load(FILE *f, int loadprogbits); |
629 | |
630 | static int obj_create_image(struct obj_file *f, char *image); |
631 | |
632 | /* Architecture specific manipulation routines. */ |
633 | |
634 | static struct obj_file *arch_new_file(void); |
635 | |
636 | static struct obj_section *arch_new_section(void); |
637 | |
638 | static struct obj_symbol *arch_new_symbol(void); |
639 | |
640 | static enum obj_reloc arch_apply_relocation(struct obj_file *f, |
641 | struct obj_section *targsec, |
642 | /*struct obj_section *symsec,*/ |
643 | struct obj_symbol *sym, |
644 | ElfW(RelM) *rel, ElfW(Addr) value); |
645 | |
646 | static void arch_create_got(struct obj_file *f); |
647 | #if ENABLE_FEATURE_CHECK_TAINTED_MODULE |
648 | static int obj_gpl_license(struct obj_file *f, const char **license); |
649 | #endif /* FEATURE_CHECK_TAINTED_MODULE */ |
650 | #endif /* obj.h */ |
651 | //---------------------------------------------------------------------------- |
652 | //--------end of modutils obj.h |
653 | //---------------------------------------------------------------------------- |
654 | |
655 | |
656 | /* SPFX is always a string, so it can be concatenated to string constants. */ |
657 | #ifdef SYMBOL_PREFIX |
658 | #define SPFX SYMBOL_PREFIX |
659 | #else |
660 | #define SPFX "" |
661 | #endif |
662 | |
663 | enum { STRVERSIONLEN = 64 }; |
664 | |
665 | /*======================================================================*/ |
666 | |
667 | #define flag_force_load (option_mask32 & INSMOD_OPT_FORCE) |
668 | #define flag_autoclean (option_mask32 & INSMOD_OPT_KERNELD) |
669 | #define flag_verbose (option_mask32 & INSMOD_OPT_VERBOSE) |
670 | #define flag_quiet (option_mask32 & INSMOD_OPT_SILENT) |
671 | #define flag_noexport (option_mask32 & INSMOD_OPT_NO_EXPORT) |
672 | #define flag_print_load_map (option_mask32 & INSMOD_OPT_PRINT_MAP) |
673 | |
674 | /*======================================================================*/ |
675 | |
676 | #if defined(USE_LIST) |
677 | |
678 | struct arch_list_entry |
679 | { |
680 | struct arch_list_entry *next; |
681 | LIST_ARCHTYPE addend; |
682 | int offset; |
683 | int inited : 1; |
684 | }; |
685 | |
686 | #endif |
687 | |
688 | #if defined(USE_SINGLE) |
689 | |
690 | struct arch_single_entry |
691 | { |
692 | int offset; |
693 | int inited : 1; |
694 | int allocated : 1; |
695 | }; |
696 | |
697 | #endif |
698 | |
699 | #if defined(__mips__) |
700 | struct mips_hi16 |
701 | { |
702 | struct mips_hi16 *next; |
703 | ElfW(Addr) *addr; |
704 | ElfW(Addr) value; |
705 | }; |
706 | #endif |
707 | |
708 | struct arch_file { |
709 | struct obj_file root; |
710 | #if defined(USE_PLT_ENTRIES) |
711 | struct obj_section *plt; |
712 | #endif |
713 | #if defined(USE_GOT_ENTRIES) |
714 | struct obj_section *got; |
715 | #endif |
716 | #if defined(__mips__) |
717 | struct mips_hi16 *mips_hi16_list; |
718 | #endif |
719 | }; |
720 | |
721 | struct arch_symbol { |
722 | struct obj_symbol root; |
723 | #if defined(USE_PLT_ENTRIES) |
724 | #if defined(USE_PLT_LIST) |
725 | struct arch_list_entry *pltent; |
726 | #else |
727 | struct arch_single_entry pltent; |
728 | #endif |
729 | #endif |
730 | #if defined(USE_GOT_ENTRIES) |
731 | struct arch_single_entry gotent; |
732 | #endif |
733 | }; |
734 | |
735 | |
736 | struct external_module { |
737 | const char *name; |
738 | ElfW(Addr) addr; |
739 | int used; |
740 | size_t nsyms; |
741 | struct new_module_symbol *syms; |
742 | }; |
743 | |
744 | static struct new_module_symbol *ksyms; |
745 | static size_t nksyms; |
746 | |
747 | static struct external_module *ext_modules; |
748 | static int n_ext_modules; |
749 | static int n_ext_modules_used; |
750 | |
751 | /*======================================================================*/ |
752 | |
753 | |
754 | static struct obj_file *arch_new_file(void) |
755 | { |
756 | struct arch_file *f; |
757 | f = xzalloc(sizeof(*f)); |
758 | return &f->root; /* it's a first member */ |
759 | } |
760 | |
761 | static struct obj_section *arch_new_section(void) |
762 | { |
763 | return xzalloc(sizeof(struct obj_section)); |
764 | } |
765 | |
766 | static struct obj_symbol *arch_new_symbol(void) |
767 | { |
768 | struct arch_symbol *sym; |
769 | sym = xzalloc(sizeof(*sym)); |
770 | return &sym->root; |
771 | } |
772 | |
773 | static enum obj_reloc |
774 | arch_apply_relocation(struct obj_file *f, |
775 | struct obj_section *targsec, |
776 | /*struct obj_section *symsec,*/ |
777 | struct obj_symbol *sym, |
778 | ElfW(RelM) *rel, ElfW(Addr) v) |
779 | { |
780 | #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \ |
781 | || defined(__sh__) || defined(__s390__) || defined(__x86_64__) \ |
782 | || defined(__powerpc__) || defined(__mips__) |
783 | struct arch_file *ifile = (struct arch_file *) f; |
784 | #endif |
785 | enum obj_reloc ret = obj_reloc_ok; |
786 | ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset); |
787 | #if defined(__arm__) || defined(__H8300H__) || defined(__H8300S__) \ |
788 | || defined(__i386__) || defined(__mc68000__) || defined(__microblaze__) \ |
789 | || defined(__mips__) || defined(__nios2__) || defined(__powerpc__) \ |
790 | || defined(__s390__) || defined(__sh__) || defined(__x86_64__) |
791 | ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset; |
792 | #endif |
793 | #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES) |
794 | struct arch_symbol *isym = (struct arch_symbol *) sym; |
795 | #endif |
796 | #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \ |
797 | || defined(__sh__) || defined(__s390__) |
798 | #if defined(USE_GOT_ENTRIES) |
799 | ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0; |
800 | #endif |
801 | #endif |
802 | #if defined(USE_PLT_ENTRIES) |
803 | ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0; |
804 | unsigned long *ip; |
805 | # if defined(USE_PLT_LIST) |
806 | struct arch_list_entry *pe; |
807 | # else |
808 | struct arch_single_entry *pe; |
809 | # endif |
810 | #endif |
811 | |
812 | switch (ELF_R_TYPE(rel->r_info)) { |
813 | |
814 | #if defined(__arm__) |
815 | |
816 | case R_ARM_NONE: |
817 | break; |
818 | |
819 | case R_ARM_ABS32: |
820 | *loc += v; |
821 | break; |
822 | |
823 | case R_ARM_GOT32: |
824 | goto bb_use_got; |
825 | |
826 | case R_ARM_GOTPC: |
827 | /* relative reloc, always to _GLOBAL_OFFSET_TABLE_ |
828 | * (which is .got) similar to branch, |
829 | * but is full 32 bits relative */ |
830 | |
831 | *loc += got - dot; |
832 | break; |
833 | |
834 | case R_ARM_PC24: |
835 | case R_ARM_PLT32: |
836 | goto bb_use_plt; |
837 | |
838 | case R_ARM_GOTOFF: /* address relative to the got */ |
839 | *loc += v - got; |
840 | break; |
841 | |
842 | #elif defined(__cris__) |
843 | |
844 | case R_CRIS_NONE: |
845 | break; |
846 | |
847 | case R_CRIS_32: |
848 | /* CRIS keeps the relocation value in the r_addend field and |
849 | * should not use whats in *loc at all |
850 | */ |
851 | *loc = v; |
852 | break; |
853 | |
854 | #elif defined(__H8300H__) || defined(__H8300S__) |
855 | |
856 | case R_H8_DIR24R8: |
857 | loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1); |
858 | *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v); |
859 | break; |
860 | case R_H8_DIR24A8: |
861 | *loc += v; |
862 | break; |
863 | case R_H8_DIR32: |
864 | case R_H8_DIR32A16: |
865 | *loc += v; |
866 | break; |
867 | case R_H8_PCREL16: |
868 | v -= dot + 2; |
869 | if ((ElfW(Sword))v > 0x7fff || |
870 | (ElfW(Sword))v < -(ElfW(Sword))0x8000) |
871 | ret = obj_reloc_overflow; |
872 | else |
873 | *(unsigned short *)loc = v; |
874 | break; |
875 | case R_H8_PCREL8: |
876 | v -= dot + 1; |
877 | if ((ElfW(Sword))v > 0x7f || |
878 | (ElfW(Sword))v < -(ElfW(Sword))0x80) |
879 | ret = obj_reloc_overflow; |
880 | else |
881 | *(unsigned char *)loc = v; |
882 | break; |
883 | |
884 | #elif defined(__i386__) |
885 | |
886 | case R_386_NONE: |
887 | break; |
888 | |
889 | case R_386_32: |
890 | *loc += v; |
891 | break; |
892 | |
893 | case R_386_PLT32: |
894 | case R_386_PC32: |
895 | case R_386_GOTOFF: |
896 | *loc += v - dot; |
897 | break; |
898 | |
899 | case R_386_GLOB_DAT: |
900 | case R_386_JMP_SLOT: |
901 | *loc = v; |
902 | break; |
903 | |
904 | case R_386_RELATIVE: |
905 | *loc += f->baseaddr; |
906 | break; |
907 | |
908 | case R_386_GOTPC: |
909 | *loc += got - dot; |
910 | break; |
911 | |
912 | case R_386_GOT32: |
913 | goto bb_use_got; |
914 | break; |
915 | |
916 | #elif defined(__microblaze__) |
917 | case R_MICROBLAZE_NONE: |
918 | case R_MICROBLAZE_64_NONE: |
919 | case R_MICROBLAZE_32_SYM_OP_SYM: |
920 | case R_MICROBLAZE_32_PCREL: |
921 | break; |
922 | |
923 | case R_MICROBLAZE_64_PCREL: { |
924 | /* dot is the address of the current instruction. |
925 | * v is the target symbol address. |
926 | * So we need to extract the offset in the code, |
927 | * adding v, then subtrating the current address |
928 | * of this instruction. |
929 | * Ex: "IMM 0xFFFE bralid 0x0000" = "bralid 0xFFFE0000" |
930 | */ |
931 | |
932 | /* Get split offset stored in code */ |
933 | unsigned int temp = (loc[0] & 0xFFFF) << 16 | |
934 | (loc[1] & 0xFFFF); |
935 | |
936 | /* Adjust relative offset. -4 adjustment required |
937 | * because dot points to the IMM insn, but branch |
938 | * is computed relative to the branch instruction itself. |
939 | */ |
940 | temp += v - dot - 4; |
941 | |
942 | /* Store back into code */ |
943 | loc[0] = (loc[0] & 0xFFFF0000) | temp >> 16; |
944 | loc[1] = (loc[1] & 0xFFFF0000) | (temp & 0xFFFF); |
945 | |
946 | break; |
947 | } |
948 | |
949 | case R_MICROBLAZE_32: |
950 | *loc += v; |
951 | break; |
952 | |
953 | case R_MICROBLAZE_64: { |
954 | /* Get split pointer stored in code */ |
955 | unsigned int temp1 = (loc[0] & 0xFFFF) << 16 | |
956 | (loc[1] & 0xFFFF); |
957 | |
958 | /* Add reloc offset */ |
959 | temp1+=v; |
960 | |
961 | /* Store back into code */ |
962 | loc[0] = (loc[0] & 0xFFFF0000) | temp1 >> 16; |
963 | loc[1] = (loc[1] & 0xFFFF0000) | (temp1 & 0xFFFF); |
964 | |
965 | break; |
966 | } |
967 | |
968 | case R_MICROBLAZE_32_PCREL_LO: |
969 | case R_MICROBLAZE_32_LO: |
970 | case R_MICROBLAZE_SRO32: |
971 | case R_MICROBLAZE_SRW32: |
972 | ret = obj_reloc_unhandled; |
973 | break; |
974 | |
975 | #elif defined(__mc68000__) |
976 | |
977 | case R_68K_NONE: |
978 | break; |
979 | |
980 | case R_68K_32: |
981 | *loc += v; |
982 | break; |
983 | |
984 | case R_68K_8: |
985 | if (v > 0xff) { |
986 | ret = obj_reloc_overflow; |
987 | } |
988 | *(char *)loc = v; |
989 | break; |
990 | |
991 | case R_68K_16: |
992 | if (v > 0xffff) { |
993 | ret = obj_reloc_overflow; |
994 | } |
995 | *(short *)loc = v; |
996 | break; |
997 | |
998 | case R_68K_PC8: |
999 | v -= dot; |
1000 | if ((ElfW(Sword))v > 0x7f || |
1001 | (ElfW(Sword))v < -(ElfW(Sword))0x80) { |
1002 | ret = obj_reloc_overflow; |
1003 | } |
1004 | *(char *)loc = v; |
1005 | break; |
1006 | |
1007 | case R_68K_PC16: |
1008 | v -= dot; |
1009 | if ((ElfW(Sword))v > 0x7fff || |
1010 | (ElfW(Sword))v < -(ElfW(Sword))0x8000) { |
1011 | ret = obj_reloc_overflow; |
1012 | } |
1013 | *(short *)loc = v; |
1014 | break; |
1015 | |
1016 | case R_68K_PC32: |
1017 | *(int *)loc = v - dot; |
1018 | break; |
1019 | |
1020 | case R_68K_GLOB_DAT: |
1021 | case R_68K_JMP_SLOT: |
1022 | *loc = v; |
1023 | break; |
1024 | |
1025 | case R_68K_RELATIVE: |
1026 | *(int *)loc += f->baseaddr; |
1027 | break; |
1028 | |
1029 | case R_68K_GOT32: |
1030 | goto bb_use_got; |
1031 | |
1032 | # ifdef R_68K_GOTOFF |
1033 | case R_68K_GOTOFF: |
1034 | *loc += v - got; |
1035 | break; |
1036 | # endif |
1037 | |
1038 | #elif defined(__mips__) |
1039 | |
1040 | case R_MIPS_NONE: |
1041 | break; |
1042 | |
1043 | case R_MIPS_32: |
1044 | *loc += v; |
1045 | break; |
1046 | |
1047 | case R_MIPS_26: |
1048 | if (v % 4) |
1049 | ret = obj_reloc_dangerous; |
1050 | if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000)) |
1051 | ret = obj_reloc_overflow; |
1052 | *loc = |
1053 | (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) & |
1054 | 0x03ffffff); |
1055 | break; |
1056 | |
1057 | case R_MIPS_HI16: |
1058 | { |
1059 | struct mips_hi16 *n; |
1060 | |
1061 | /* We cannot relocate this one now because we don't know the value |
1062 | of the carry we need to add. Save the information, and let LO16 |
1063 | do the actual relocation. */ |
1064 | n = xmalloc(sizeof *n); |
1065 | n->addr = loc; |
1066 | n->value = v; |
1067 | n->next = ifile->mips_hi16_list; |
1068 | ifile->mips_hi16_list = n; |
1069 | break; |
1070 | } |
1071 | |
1072 | case R_MIPS_LO16: |
1073 | { |
1074 | unsigned long insnlo = *loc; |
1075 | ElfW(Addr) val, vallo; |
1076 | |
1077 | /* Sign extend the addend we extract from the lo insn. */ |
1078 | vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000; |
1079 | |
1080 | if (ifile->mips_hi16_list != NULL) { |
1081 | struct mips_hi16 *l; |
1082 | |
1083 | l = ifile->mips_hi16_list; |
1084 | while (l != NULL) { |
1085 | struct mips_hi16 *next; |
1086 | unsigned long insn; |
1087 | |
1088 | /* Do the HI16 relocation. Note that we actually don't |
1089 | need to know anything about the LO16 itself, except where |
1090 | to find the low 16 bits of the addend needed by the LO16. */ |
1091 | insn = *l->addr; |
1092 | val = |
1093 | ((insn & 0xffff) << 16) + |
1094 | vallo; |
1095 | val += v; |
1096 | |
1097 | /* Account for the sign extension that will happen in the |
1098 | low bits. */ |
1099 | val = |
1100 | ((val >> 16) + |
1101 | ((val & 0x8000) != |
1102 | 0)) & 0xffff; |
1103 | |
1104 | insn = (insn & ~0xffff) | val; |
1105 | *l->addr = insn; |
1106 | |
1107 | next = l->next; |
1108 | free(l); |
1109 | l = next; |
1110 | } |
1111 | |
1112 | ifile->mips_hi16_list = NULL; |
1113 | } |
1114 | |
1115 | /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */ |
1116 | val = v + vallo; |
1117 | insnlo = (insnlo & ~0xffff) | (val & 0xffff); |
1118 | *loc = insnlo; |
1119 | break; |
1120 | } |
1121 | |
1122 | #elif defined(__nios2__) |
1123 | |
1124 | case R_NIOS2_NONE: |
1125 | break; |
1126 | |
1127 | case R_NIOS2_BFD_RELOC_32: |
1128 | *loc += v; |
1129 | break; |
1130 | |
1131 | case R_NIOS2_BFD_RELOC_16: |
1132 | if (v > 0xffff) { |
1133 | ret = obj_reloc_overflow; |
1134 | } |
1135 | *(short *)loc = v; |
1136 | break; |
1137 | |
1138 | case R_NIOS2_BFD_RELOC_8: |
1139 | if (v > 0xff) { |
1140 | ret = obj_reloc_overflow; |
1141 | } |
1142 | *(char *)loc = v; |
1143 | break; |
1144 | |
1145 | case R_NIOS2_S16: |
1146 | { |
1147 | Elf32_Addr word; |
1148 | |
1149 | if ((Elf32_Sword)v > 0x7fff || |
1150 | (Elf32_Sword)v < -(Elf32_Sword)0x8000) { |
1151 | ret = obj_reloc_overflow; |
1152 | } |
1153 | |
1154 | word = *loc; |
1155 | *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | |
1156 | (word & 0x3f); |
1157 | } |
1158 | break; |
1159 | |
1160 | case R_NIOS2_U16: |
1161 | { |
1162 | Elf32_Addr word; |
1163 | |
1164 | if (v > 0xffff) { |
1165 | ret = obj_reloc_overflow; |
1166 | } |
1167 | |
1168 | word = *loc; |
1169 | *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | |
1170 | (word & 0x3f); |
1171 | } |
1172 | break; |
1173 | |
1174 | case R_NIOS2_PCREL16: |
1175 | { |
1176 | Elf32_Addr word; |
1177 | |
1178 | v -= dot + 4; |
1179 | if ((Elf32_Sword)v > 0x7fff || |
1180 | (Elf32_Sword)v < -(Elf32_Sword)0x8000) { |
1181 | ret = obj_reloc_overflow; |
1182 | } |
1183 | |
1184 | word = *loc; |
1185 | *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f); |
1186 | } |
1187 | break; |
1188 | |
1189 | case R_NIOS2_GPREL: |
1190 | { |
1191 | Elf32_Addr word, gp; |
1192 | /* get _gp */ |
1193 | gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp")); |
1194 | v-=gp; |
1195 | if ((Elf32_Sword)v > 0x7fff || |
1196 | (Elf32_Sword)v < -(Elf32_Sword)0x8000) { |
1197 | ret = obj_reloc_overflow; |
1198 | } |
1199 | |
1200 | word = *loc; |
1201 | *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f); |
1202 | } |
1203 | break; |
1204 | |
1205 | case R_NIOS2_CALL26: |
1206 | if (v & 3) |
1207 | ret = obj_reloc_dangerous; |
1208 | if ((v >> 28) != (dot >> 28)) |
1209 | ret = obj_reloc_overflow; |
1210 | *loc = (*loc & 0x3f) | ((v >> 2) << 6); |
1211 | break; |
1212 | |
1213 | case R_NIOS2_IMM5: |
1214 | { |
1215 | Elf32_Addr word; |
1216 | |
1217 | if (v > 0x1f) { |
1218 | ret = obj_reloc_overflow; |
1219 | } |
1220 | |
1221 | word = *loc & ~0x7c0; |
1222 | *loc = word | ((v & 0x1f) << 6); |
1223 | } |
1224 | break; |
1225 | |
1226 | case R_NIOS2_IMM6: |
1227 | { |
1228 | Elf32_Addr word; |
1229 | |
1230 | if (v > 0x3f) { |
1231 | ret = obj_reloc_overflow; |
1232 | } |
1233 | |
1234 | word = *loc & ~0xfc0; |
1235 | *loc = word | ((v & 0x3f) << 6); |
1236 | } |
1237 | break; |
1238 | |
1239 | case R_NIOS2_IMM8: |
1240 | { |
1241 | Elf32_Addr word; |
1242 | |
1243 | if (v > 0xff) { |
1244 | ret = obj_reloc_overflow; |
1245 | } |
1246 | |
1247 | word = *loc & ~0x3fc0; |
1248 | *loc = word | ((v & 0xff) << 6); |
1249 | } |
1250 | break; |
1251 | |
1252 | case R_NIOS2_HI16: |
1253 | { |
1254 | Elf32_Addr word; |
1255 | |
1256 | word = *loc; |
1257 | *loc = ((((word >> 22) << 16) | ((v >>16) & 0xffff)) << 6) | |
1258 | (word & 0x3f); |
1259 | } |
1260 | break; |
1261 | |
1262 | case R_NIOS2_LO16: |
1263 | { |
1264 | Elf32_Addr word; |
1265 | |
1266 | word = *loc; |
1267 | *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | |
1268 | (word & 0x3f); |
1269 | } |
1270 | break; |
1271 | |
1272 | case R_NIOS2_HIADJ16: |
1273 | { |
1274 | Elf32_Addr word1, word2; |
1275 | |
1276 | word1 = *loc; |
1277 | word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff; |
1278 | *loc = ((((word1 >> 22) << 16) | word2) << 6) | |
1279 | (word1 & 0x3f); |
1280 | } |
1281 | break; |
1282 | |
1283 | #elif defined(__powerpc64__) |
1284 | /* PPC64 needs a 2.6 kernel, 2.4 module relocation irrelevant */ |
1285 | |
1286 | #elif defined(__powerpc__) |
1287 | |
1288 | case R_PPC_ADDR16_HA: |
1289 | *(unsigned short *)loc = (v + 0x8000) >> 16; |
1290 | break; |
1291 | |
1292 | case R_PPC_ADDR16_HI: |
1293 | *(unsigned short *)loc = v >> 16; |
1294 | break; |
1295 | |
1296 | case R_PPC_ADDR16_LO: |
1297 | *(unsigned short *)loc = v; |
1298 | break; |
1299 | |
1300 | case R_PPC_REL24: |
1301 | goto bb_use_plt; |
1302 | |
1303 | case R_PPC_REL32: |
1304 | *loc = v - dot; |
1305 | break; |
1306 | |
1307 | case R_PPC_ADDR32: |
1308 | *loc = v; |
1309 | break; |
1310 | |
1311 | #elif defined(__s390__) |
1312 | |
1313 | case R_390_32: |
1314 | *(unsigned int *) loc += v; |
1315 | break; |
1316 | case R_390_16: |
1317 | *(unsigned short *) loc += v; |
1318 | break; |
1319 | case R_390_8: |
1320 | *(unsigned char *) loc += v; |
1321 | break; |
1322 | |
1323 | case R_390_PC32: |
1324 | *(unsigned int *) loc += v - dot; |
1325 | break; |
1326 | case R_390_PC16DBL: |
1327 | *(unsigned short *) loc += (v - dot) >> 1; |
1328 | break; |
1329 | case R_390_PC16: |
1330 | *(unsigned short *) loc += v - dot; |
1331 | break; |
1332 | |
1333 | case R_390_PLT32: |
1334 | case R_390_PLT16DBL: |
1335 | /* find the plt entry and initialize it. */ |
1336 | pe = (struct arch_single_entry *) &isym->pltent; |
1337 | if (pe->inited == 0) { |
1338 | ip = (unsigned long *)(ifile->plt->contents + pe->offset); |
1339 | ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */ |
1340 | ip[1] = 0x100607f1; |
1341 | if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL) |
1342 | ip[2] = v - 2; |
1343 | else |
1344 | ip[2] = v; |
1345 | pe->inited = 1; |
1346 | } |
1347 | |
1348 | /* Insert relative distance to target. */ |
1349 | v = plt + pe->offset - dot; |
1350 | if (ELF_R_TYPE(rel->r_info) == R_390_PLT32) |
1351 | *(unsigned int *) loc = (unsigned int) v; |
1352 | else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL) |
1353 | *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1); |
1354 | break; |
1355 | |
1356 | case R_390_GLOB_DAT: |
1357 | case R_390_JMP_SLOT: |
1358 | *loc = v; |
1359 | break; |
1360 | |
1361 | case R_390_RELATIVE: |
1362 | *loc += f->baseaddr; |
1363 | break; |
1364 | |
1365 | case R_390_GOTPC: |
1366 | *(unsigned long *) loc += got - dot; |
1367 | break; |
1368 | |
1369 | case R_390_GOT12: |
1370 | case R_390_GOT16: |
1371 | case R_390_GOT32: |
1372 | if (!isym->gotent.inited) |
1373 | { |
1374 | isym->gotent.inited = 1; |
1375 | *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v; |
1376 | } |
1377 | if (ELF_R_TYPE(rel->r_info) == R_390_GOT12) |
1378 | *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff; |
1379 | else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16) |
1380 | *(unsigned short *) loc += isym->gotent.offset; |
1381 | else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32) |
1382 | *(unsigned int *) loc += isym->gotent.offset; |
1383 | break; |
1384 | |
1385 | # ifndef R_390_GOTOFF32 |
1386 | # define R_390_GOTOFF32 R_390_GOTOFF |
1387 | # endif |
1388 | case R_390_GOTOFF32: |
1389 | *loc += v - got; |
1390 | break; |
1391 | |
1392 | #elif defined(__sh__) |
1393 | |
1394 | case R_SH_NONE: |
1395 | break; |
1396 | |
1397 | case R_SH_DIR32: |
1398 | *loc += v; |
1399 | break; |
1400 | |
1401 | case R_SH_REL32: |
1402 | *loc += v - dot; |
1403 | break; |
1404 | |
1405 | case R_SH_PLT32: |
1406 | *loc = v - dot; |
1407 | break; |
1408 | |
1409 | case R_SH_GLOB_DAT: |
1410 | case R_SH_JMP_SLOT: |
1411 | *loc = v; |
1412 | break; |
1413 | |
1414 | case R_SH_RELATIVE: |
1415 | *loc = f->baseaddr + rel->r_addend; |
1416 | break; |
1417 | |
1418 | case R_SH_GOTPC: |
1419 | *loc = got - dot + rel->r_addend; |
1420 | break; |
1421 | |
1422 | case R_SH_GOT32: |
1423 | goto bb_use_got; |
1424 | |
1425 | case R_SH_GOTOFF: |
1426 | *loc = v - got; |
1427 | break; |
1428 | |
1429 | # if defined(__SH5__) |
1430 | case R_SH_IMM_MEDLOW16: |
1431 | case R_SH_IMM_LOW16: |
1432 | { |
1433 | ElfW(Addr) word; |
1434 | |
1435 | if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16) |
1436 | v >>= 16; |
1437 | |
1438 | /* |
1439 | * movi and shori have the format: |
1440 | * |
1441 | * | op | imm | reg | reserved | |
1442 | * 31..26 25..10 9.. 4 3 .. 0 |
1443 | * |
1444 | * so we simply mask and or in imm. |
1445 | */ |
1446 | word = *loc & ~0x3fffc00; |
1447 | word |= (v & 0xffff) << 10; |
1448 | |
1449 | *loc = word; |
1450 | |
1451 | break; |
1452 | } |
1453 | |
1454 | case R_SH_IMM_MEDLOW16_PCREL: |
1455 | case R_SH_IMM_LOW16_PCREL: |
1456 | { |
1457 | ElfW(Addr) word; |
1458 | |
1459 | word = *loc & ~0x3fffc00; |
1460 | |
1461 | v -= dot; |
1462 | |
1463 | if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL) |
1464 | v >>= 16; |
1465 | |
1466 | word |= (v & 0xffff) << 10; |
1467 | |
1468 | *loc = word; |
1469 | |
1470 | break; |
1471 | } |
1472 | # endif /* __SH5__ */ |
1473 | |
1474 | #elif defined(__v850e__) |
1475 | |
1476 | case R_V850_NONE: |
1477 | break; |
1478 | |
1479 | case R_V850_32: |
1480 | /* We write two shorts instead of a long because even |
1481 | 32-bit insns only need half-word alignment, but |
1482 | 32-bit data needs to be long-word aligned. */ |
1483 | v += ((unsigned short *)loc)[0]; |
1484 | v += ((unsigned short *)loc)[1] << 16; |
1485 | ((unsigned short *)loc)[0] = v & 0xffff; |
1486 | ((unsigned short *)loc)[1] = (v >> 16) & 0xffff; |
1487 | break; |
1488 | |
1489 | case R_V850_22_PCREL: |
1490 | goto bb_use_plt; |
1491 | |
1492 | #elif defined(__x86_64__) |
1493 | |
1494 | case R_X86_64_NONE: |
1495 | break; |
1496 | |
1497 | case R_X86_64_64: |
1498 | *loc += v; |
1499 | break; |
1500 | |
1501 | case R_X86_64_32: |
1502 | *(unsigned int *) loc += v; |
1503 | if (v > 0xffffffff) |
1504 | { |
1505 | ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */ |
1506 | /* error("Possibly is module compiled without -mcmodel=kernel!"); */ |
1507 | } |
1508 | break; |
1509 | |
1510 | case R_X86_64_32S: |
1511 | *(signed int *) loc += v; |
1512 | break; |
1513 | |
1514 | case R_X86_64_16: |
1515 | *(unsigned short *) loc += v; |
1516 | break; |
1517 | |
1518 | case R_X86_64_8: |
1519 | *(unsigned char *) loc += v; |
1520 | break; |
1521 | |
1522 | case R_X86_64_PC32: |
1523 | *(unsigned int *) loc += v - dot; |
1524 | break; |
1525 | |
1526 | case R_X86_64_PC16: |
1527 | *(unsigned short *) loc += v - dot; |
1528 | break; |
1529 | |
1530 | case R_X86_64_PC8: |
1531 | *(unsigned char *) loc += v - dot; |
1532 | break; |
1533 | |
1534 | case R_X86_64_GLOB_DAT: |
1535 | case R_X86_64_JUMP_SLOT: |
1536 | *loc = v; |
1537 | break; |
1538 | |
1539 | case R_X86_64_RELATIVE: |
1540 | *loc += f->baseaddr; |
1541 | break; |
1542 | |
1543 | case R_X86_64_GOT32: |
1544 | case R_X86_64_GOTPCREL: |
1545 | goto bb_use_got; |
1546 | # if 0 |
1547 | if (!isym->gotent.reloc_done) |
1548 | { |
1549 | isym->gotent.reloc_done = 1; |
1550 | *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v; |
1551 | } |
1552 | /* XXX are these really correct? */ |
1553 | if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL) |
1554 | *(unsigned int *) loc += v + isym->gotent.offset; |
1555 | else |
1556 | *loc += isym->gotent.offset; |
1557 | break; |
1558 | # endif |
1559 | |
1560 | #else |
1561 | # warning "no idea how to handle relocations on your arch" |
1562 | #endif |
1563 | |
1564 | default: |
1565 | printf("Warning: unhandled reloc %d\n",(int)ELF_R_TYPE(rel->r_info)); |
1566 | ret = obj_reloc_unhandled; |
1567 | break; |
1568 | |
1569 | #if defined(USE_PLT_ENTRIES) |
1570 | |
1571 | bb_use_plt: |
1572 | |
1573 | /* find the plt entry and initialize it if necessary */ |
1574 | |
1575 | #if defined(USE_PLT_LIST) |
1576 | for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;) |
1577 | pe = pe->next; |
1578 | #else |
1579 | pe = &isym->pltent; |
1580 | #endif |
1581 | |
1582 | if (! pe->inited) { |
1583 | ip = (unsigned long *) (ifile->plt->contents + pe->offset); |
1584 | |
1585 | /* generate some machine code */ |
1586 | |
1587 | #if defined(__arm__) |
1588 | ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */ |
1589 | ip[1] = v; /* sym@ */ |
1590 | #endif |
1591 | #if defined(__powerpc__) |
1592 | ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */ |
1593 | ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */ |
1594 | ip[2] = 0x7d6903a6; /* mtctr r11 */ |
1595 | ip[3] = 0x4e800420; /* bctr */ |
1596 | #endif |
1597 | #if defined(__v850e__) |
1598 | /* We have to trash a register, so we assume that any control |
1599 | transfer more than 21-bits away must be a function call |
1600 | (so we can use a call-clobbered register). */ |
1601 | ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */ |
1602 | ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */ |
1603 | #endif |
1604 | pe->inited = 1; |
1605 | } |
1606 | |
1607 | /* relative distance to target */ |
1608 | v -= dot; |
1609 | /* if the target is too far away.... */ |
1610 | #if defined(__arm__) || defined(__powerpc__) |
1611 | if ((int)v < -0x02000000 || (int)v >= 0x02000000) |
1612 | #elif defined(__v850e__) |
1613 | if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000) |
1614 | #endif |
1615 | /* go via the plt */ |
1616 | v = plt + pe->offset - dot; |
1617 | |
1618 | #if defined(__v850e__) |
1619 | if (v & 1) |
1620 | #else |
1621 | if (v & 3) |
1622 | #endif |
1623 | ret = obj_reloc_dangerous; |
1624 | |
1625 | /* merge the offset into the instruction. */ |
1626 | #if defined(__arm__) |
1627 | /* Convert to words. */ |
1628 | v >>= 2; |
1629 | |
1630 | *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff); |
1631 | #endif |
1632 | #if defined(__powerpc__) |
1633 | *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc); |
1634 | #endif |
1635 | #if defined(__v850e__) |
1636 | /* We write two shorts instead of a long because even 32-bit insns |
1637 | only need half-word alignment, but the 32-bit data write needs |
1638 | to be long-word aligned. */ |
1639 | ((unsigned short *)loc)[0] = |
1640 | (*(unsigned short *)loc & 0xffc0) /* opcode + reg */ |
1641 | | ((v >> 16) & 0x3f); /* offs high part */ |
1642 | ((unsigned short *)loc)[1] = |
1643 | (v & 0xffff); /* offs low part */ |
1644 | #endif |
1645 | break; |
1646 | #endif /* USE_PLT_ENTRIES */ |
1647 | |
1648 | #if defined(USE_GOT_ENTRIES) |
1649 | bb_use_got: |
1650 | |
1651 | /* needs an entry in the .got: set it, once */ |
1652 | if (!isym->gotent.inited) { |
1653 | isym->gotent.inited = 1; |
1654 | *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v; |
1655 | } |
1656 | /* make the reloc with_respect_to_.got */ |
1657 | #if defined(__sh__) |
1658 | *loc += isym->gotent.offset + rel->r_addend; |
1659 | #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__) |
1660 | *loc += isym->gotent.offset; |
1661 | #endif |
1662 | break; |
1663 | |
1664 | #endif /* USE_GOT_ENTRIES */ |
1665 | } |
1666 | |
1667 | return ret; |
1668 | } |
1669 | |
1670 | |
1671 | #if defined(USE_LIST) |
1672 | |
1673 | static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list, |
1674 | int offset, int size) |
1675 | { |
1676 | struct arch_list_entry *pe; |
1677 | |
1678 | for (pe = *list; pe != NULL; pe = pe->next) { |
1679 | if (pe->addend == rel->r_addend) { |
1680 | break; |
1681 | } |
1682 | } |
1683 | |
1684 | if (pe == NULL) { |
1685 | pe = xmalloc(sizeof(struct arch_list_entry)); |
1686 | pe->next = *list; |
1687 | pe->addend = rel->r_addend; |
1688 | pe->offset = offset; |
1689 | pe->inited = 0; |
1690 | *list = pe; |
1691 | return size; |
1692 | } |
1693 | return 0; |
1694 | } |
1695 | |
1696 | #endif |
1697 | |
1698 | #if defined(USE_SINGLE) |
1699 | |
1700 | static int arch_single_init(/*ElfW(RelM) *rel,*/ struct arch_single_entry *single, |
1701 | int offset, int size) |
1702 | { |
1703 | if (single->allocated == 0) { |
1704 | single->allocated = 1; |
1705 | single->offset = offset; |
1706 | single->inited = 0; |
1707 | return size; |
1708 | } |
1709 | return 0; |
1710 | } |
1711 | |
1712 | #endif |
1713 | |
1714 | #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES) |
1715 | |
1716 | static struct obj_section *arch_xsect_init(struct obj_file *f, const char *name, |
1717 | int offset, int size) |
1718 | { |
1719 | struct obj_section *myrelsec = obj_find_section(f, name); |
1720 | |
1721 | if (offset == 0) { |
1722 | offset += size; |
1723 | } |
1724 | |
1725 | if (myrelsec) { |
1726 | obj_extend_section(myrelsec, offset); |
1727 | } else { |
1728 | myrelsec = obj_create_alloced_section(f, name, |
1729 | size, offset); |
1730 | } |
1731 | |
1732 | return myrelsec; |
1733 | } |
1734 | |
1735 | #endif |
1736 | |
1737 | static void arch_create_got(struct obj_file *f) |
1738 | { |
1739 | #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES) |
1740 | struct arch_file *ifile = (struct arch_file *) f; |
1741 | int i; |
1742 | #if defined(USE_GOT_ENTRIES) |
1743 | int got_offset = 0, got_needed = 0, got_allocate; |
1744 | #endif |
1745 | #if defined(USE_PLT_ENTRIES) |
1746 | int plt_offset = 0, plt_needed = 0, plt_allocate; |
1747 | #endif |
1748 | struct obj_section *relsec, *symsec, *strsec; |
1749 | ElfW(RelM) *rel, *relend; |
1750 | ElfW(Sym) *symtab, *extsym; |
1751 | const char *strtab, *name; |
1752 | struct arch_symbol *intsym; |
1753 | |
1754 | for (i = 0; i < f->header.e_shnum; ++i) { |
1755 | relsec = f->sections[i]; |
1756 | if (relsec->header.sh_type != SHT_RELM) |
1757 | continue; |
1758 | |
1759 | symsec = f->sections[relsec->header.sh_link]; |
1760 | strsec = f->sections[symsec->header.sh_link]; |
1761 | |
1762 | rel = (ElfW(RelM) *) relsec->contents; |
1763 | relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM))); |
1764 | symtab = (ElfW(Sym) *) symsec->contents; |
1765 | strtab = (const char *) strsec->contents; |
1766 | |
1767 | for (; rel < relend; ++rel) { |
1768 | extsym = &symtab[ELF_R_SYM(rel->r_info)]; |
1769 | |
1770 | #if defined(USE_GOT_ENTRIES) |
1771 | got_allocate = 0; |
1772 | #endif |
1773 | #if defined(USE_PLT_ENTRIES) |
1774 | plt_allocate = 0; |
1775 | #endif |
1776 | |
1777 | switch (ELF_R_TYPE(rel->r_info)) { |
1778 | #if defined(__arm__) |
1779 | case R_ARM_PC24: |
1780 | case R_ARM_PLT32: |
1781 | plt_allocate = 1; |
1782 | break; |
1783 | |
1784 | case R_ARM_GOTOFF: |
1785 | case R_ARM_GOTPC: |
1786 | got_needed = 1; |
1787 | continue; |
1788 | |
1789 | case R_ARM_GOT32: |
1790 | got_allocate = 1; |
1791 | break; |
1792 | |
1793 | #elif defined(__i386__) |
1794 | case R_386_GOTPC: |
1795 | case R_386_GOTOFF: |
1796 | got_needed = 1; |
1797 | continue; |
1798 | |
1799 | case R_386_GOT32: |
1800 | got_allocate = 1; |
1801 | break; |
1802 | |
1803 | #elif defined(__powerpc__) |
1804 | case R_PPC_REL24: |
1805 | plt_allocate = 1; |
1806 | break; |
1807 | |
1808 | #elif defined(__mc68000__) |
1809 | case R_68K_GOT32: |
1810 | got_allocate = 1; |
1811 | break; |
1812 | |
1813 | #ifdef R_68K_GOTOFF |
1814 | case R_68K_GOTOFF: |
1815 | got_needed = 1; |
1816 | continue; |
1817 | #endif |
1818 | |
1819 | #elif defined(__sh__) |
1820 | case R_SH_GOT32: |
1821 | got_allocate = 1; |
1822 | break; |
1823 | |
1824 | case R_SH_GOTPC: |
1825 | case R_SH_GOTOFF: |
1826 | got_needed = 1; |
1827 | continue; |
1828 | |
1829 | #elif defined(__v850e__) |
1830 | case R_V850_22_PCREL: |
1831 | plt_needed = 1; |
1832 | break; |
1833 | |
1834 | #endif |
1835 | default: |
1836 | continue; |
1837 | } |
1838 | |
1839 | if (extsym->st_name != 0) { |
1840 | name = strtab + extsym->st_name; |
1841 | } else { |
1842 | name = f->sections[extsym->st_shndx]->name; |
1843 | } |
1844 | intsym = (struct arch_symbol *) obj_find_symbol(f, name); |
1845 | #if defined(USE_GOT_ENTRIES) |
1846 | if (got_allocate) { |
1847 | got_offset += arch_single_init( |
1848 | /*rel,*/ &intsym->gotent, |
1849 | got_offset, GOT_ENTRY_SIZE); |
1850 | |
1851 | got_needed = 1; |
1852 | } |
1853 | #endif |
1854 | #if defined(USE_PLT_ENTRIES) |
1855 | if (plt_allocate) { |
1856 | #if defined(USE_PLT_LIST) |
1857 | plt_offset += arch_list_add( |
1858 | rel, &intsym->pltent, |
1859 | plt_offset, PLT_ENTRY_SIZE); |
1860 | #else |
1861 | plt_offset += arch_single_init( |
1862 | /*rel,*/ &intsym->pltent, |
1863 | plt_offset, PLT_ENTRY_SIZE); |
1864 | #endif |
1865 | plt_needed = 1; |
1866 | } |
1867 | #endif |
1868 | } |
1869 | } |
1870 | |
1871 | #if defined(USE_GOT_ENTRIES) |
1872 | if (got_needed) { |
1873 | ifile->got = arch_xsect_init(f, ".got", got_offset, |
1874 | GOT_ENTRY_SIZE); |
1875 | } |
1876 | #endif |
1877 | |
1878 | #if defined(USE_PLT_ENTRIES) |
1879 | if (plt_needed) { |
1880 | ifile->plt = arch_xsect_init(f, ".plt", plt_offset, |
1881 | PLT_ENTRY_SIZE); |
1882 | } |
1883 | #endif |
1884 | |
1885 | #endif /* defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES) */ |
1886 | } |
1887 | |
1888 | /*======================================================================*/ |
1889 | |
1890 | /* Standard ELF hash function. */ |
1891 | static unsigned long obj_elf_hash_n(const char *name, unsigned long n) |
1892 | { |
1893 | unsigned long h = 0; |
1894 | unsigned long g; |
1895 | unsigned char ch; |
1896 | |
1897 | while (n > 0) { |
1898 | ch = *name++; |
1899 | h = (h << 4) + ch; |
1900 | g = (h & 0xf0000000); |
1901 | if (g != 0) { |
1902 | h ^= g >> 24; |
1903 | h &= ~g; |
1904 | } |
1905 | n--; |
1906 | } |
1907 | return h; |
1908 | } |
1909 | |
1910 | static unsigned long obj_elf_hash(const char *name) |
1911 | { |
1912 | return obj_elf_hash_n(name, strlen(name)); |
1913 | } |
1914 | |
1915 | #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING |
1916 | /* String comparison for non-co-versioned kernel and module. */ |
1917 | |
1918 | static int ncv_strcmp(const char *a, const char *b) |
1919 | { |
1920 | size_t alen = strlen(a), blen = strlen(b); |
1921 | |
1922 | if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R') |
1923 | return strncmp(a, b, alen); |
1924 | else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R') |
1925 | return strncmp(a, b, blen); |
1926 | else |
1927 | return strcmp(a, b); |
1928 | } |
1929 | |
1930 | /* String hashing for non-co-versioned kernel and module. Here |
1931 | we are simply forced to drop the crc from the hash. */ |
1932 | |
1933 | static unsigned long ncv_symbol_hash(const char *str) |
1934 | { |
1935 | size_t len = strlen(str); |
1936 | if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R') |
1937 | len -= 10; |
1938 | return obj_elf_hash_n(str, len); |
1939 | } |
1940 | |
1941 | static void |
1942 | obj_set_symbol_compare(struct obj_file *f, |
1943 | int (*cmp) (const char *, const char *), |
1944 | unsigned long (*hash) (const char *)) |
1945 | { |
1946 | if (cmp) |
1947 | f->symbol_cmp = cmp; |
1948 | if (hash) { |
1949 | struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next; |
1950 | int i; |
1951 | |
1952 | f->symbol_hash = hash; |
1953 | |
1954 | memcpy(tmptab, f->symtab, sizeof(tmptab)); |
1955 | memset(f->symtab, 0, sizeof(f->symtab)); |
1956 | |
1957 | for (i = 0; i < HASH_BUCKETS; ++i) |
1958 | for (sym = tmptab[i]; sym; sym = next) { |
1959 | unsigned long h = hash(sym->name) % HASH_BUCKETS; |
1960 | next = sym->next; |
1961 | sym->next = f->symtab[h]; |
1962 | f->symtab[h] = sym; |
1963 | } |
1964 | } |
1965 | } |
1966 | |
1967 | #endif /* FEATURE_INSMOD_VERSION_CHECKING */ |
1968 | |
1969 | static struct obj_symbol * |
1970 | obj_add_symbol(struct obj_file *f, const char *name, |
1971 | unsigned long symidx, int info, |
1972 | int secidx, ElfW(Addr) value, |
1973 | unsigned long size) |
1974 | { |
1975 | struct obj_symbol *sym; |
1976 | unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS; |
1977 | int n_type = ELF_ST_TYPE(info); |
1978 | int n_binding = ELF_ST_BIND(info); |
1979 | |
1980 | for (sym = f->symtab[hash]; sym; sym = sym->next) { |
1981 | if (f->symbol_cmp(sym->name, name) == 0) { |
1982 | int o_secidx = sym->secidx; |
1983 | int o_info = sym->info; |
1984 | int o_type = ELF_ST_TYPE(o_info); |
1985 | int o_binding = ELF_ST_BIND(o_info); |
1986 | |
1987 | /* A redefinition! Is it legal? */ |
1988 | |
1989 | if (secidx == SHN_UNDEF) |
1990 | return sym; |
1991 | else if (o_secidx == SHN_UNDEF) |
1992 | goto found; |
1993 | else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) { |
1994 | /* Cope with local and global symbols of the same name |
1995 | in the same object file, as might have been created |
1996 | by ld -r. The only reason locals are now seen at this |
1997 | level at all is so that we can do semi-sensible things |
1998 | with parameters. */ |
1999 | |
2000 | struct obj_symbol *nsym, **p; |
2001 | |
2002 | nsym = arch_new_symbol(); |
2003 | nsym->next = sym->next; |
2004 | nsym->ksymidx = -1; |
2005 | |
2006 | /* Excise the old (local) symbol from the hash chain. */ |
2007 | for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next) |
2008 | continue; |
2009 | *p = sym = nsym; |
2010 | goto found; |
2011 | } else if (n_binding == STB_LOCAL) { |
2012 | /* Another symbol of the same name has already been defined. |
2013 | Just add this to the local table. */ |
2014 | sym = arch_new_symbol(); |
2015 | sym->next = NULL; |
2016 | sym->ksymidx = -1; |
2017 | f->local_symtab[symidx] = sym; |
2018 | goto found; |
2019 | } else if (n_binding == STB_WEAK) |
2020 | return sym; |
2021 | else if (o_binding == STB_WEAK) |
2022 | goto found; |
2023 | /* Don't unify COMMON symbols with object types the programmer |
2024 | doesn't expect. */ |
2025 | else if (secidx == SHN_COMMON |
2026 | && (o_type == STT_NOTYPE || o_type == STT_OBJECT)) |
2027 | return sym; |
2028 | else if (o_secidx == SHN_COMMON |
2029 | && (n_type == STT_NOTYPE || n_type == STT_OBJECT)) |
2030 | goto found; |
2031 | else { |
2032 | /* Don't report an error if the symbol is coming from |
2033 | the kernel or some external module. */ |
2034 | if (secidx <= SHN_HIRESERVE) |
2035 | bb_error_msg("%s multiply defined", name); |
2036 | return sym; |
2037 | } |
2038 | } |
2039 | } |
2040 | |
2041 | /* Completely new symbol. */ |
2042 | sym = arch_new_symbol(); |
2043 | sym->next = f->symtab[hash]; |
2044 | f->symtab[hash] = sym; |
2045 | sym->ksymidx = -1; |
2046 | if (ELF_ST_BIND(info) == STB_LOCAL && symidx != (unsigned long)(-1)) { |
2047 | if (symidx >= f->local_symtab_size) |
2048 | bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld", |
2049 | name, (long) symidx, (long) f->local_symtab_size); |
2050 | else |
2051 | f->local_symtab[symidx] = sym; |
2052 | } |
2053 | |
2054 | found: |
2055 | sym->name = name; |
2056 | sym->value = value; |
2057 | sym->size = size; |
2058 | sym->secidx = secidx; |
2059 | sym->info = info; |
2060 | |
2061 | return sym; |
2062 | } |
2063 | |
2064 | static struct obj_symbol * |
2065 | obj_find_symbol(struct obj_file *f, const char *name) |
2066 | { |
2067 | struct obj_symbol *sym; |
2068 | unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS; |
2069 | |
2070 | for (sym = f->symtab[hash]; sym; sym = sym->next) |
2071 | if (f->symbol_cmp(sym->name, name) == 0) |
2072 | return sym; |
2073 | |
2074 | return NULL; |
2075 | } |
2076 | |
2077 | static ElfW(Addr) obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym) |
2078 | { |
2079 | if (sym) { |
2080 | if (sym->secidx >= SHN_LORESERVE) |
2081 | return sym->value; |
2082 | |
2083 | return sym->value + f->sections[sym->secidx]->header.sh_addr; |
2084 | } else { |
2085 | /* As a special case, a NULL sym has value zero. */ |
2086 | return 0; |
2087 | } |
2088 | } |
2089 | |
2090 | static struct obj_section *obj_find_section(struct obj_file *f, const char *name) |
2091 | { |
2092 | int i, n = f->header.e_shnum; |
2093 | |
2094 | for (i = 0; i < n; ++i) |
2095 | if (strcmp(f->sections[i]->name, name) == 0) |
2096 | return f->sections[i]; |
2097 | |
2098 | return NULL; |
2099 | } |
2100 | |
2101 | static int obj_load_order_prio(struct obj_section *a) |
2102 | { |
2103 | unsigned long af, ac; |
2104 | |
2105 | af = a->header.sh_flags; |
2106 | |
2107 | ac = 0; |
2108 | if (a->name[0] != '.' || strlen(a->name) != 10 || |
2109 | strcmp(a->name + 5, ".init")) |
2110 | ac |= 32; |
2111 | if (af & SHF_ALLOC) |
2112 | ac |= 16; |
2113 | if (!(af & SHF_WRITE)) |
2114 | ac |= 8; |
2115 | if (af & SHF_EXECINSTR) |
2116 | ac |= 4; |
2117 | if (a->header.sh_type != SHT_NOBITS) |
2118 | ac |= 2; |
2119 | |
2120 | return ac; |
2121 | } |
2122 | |
2123 | static void |
2124 | obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec) |
2125 | { |
2126 | struct obj_section **p; |
2127 | int prio = obj_load_order_prio(sec); |
2128 | for (p = f->load_order_search_start; *p; p = &(*p)->load_next) |
2129 | if (obj_load_order_prio(*p) < prio) |
2130 | break; |
2131 | sec->load_next = *p; |
2132 | *p = sec; |
2133 | } |
2134 | |
2135 | static struct obj_section *obj_create_alloced_section(struct obj_file *f, |
2136 | const char *name, |
2137 | unsigned long align, |
2138 | unsigned long size) |
2139 | { |
2140 | int newidx = f->header.e_shnum++; |
2141 | struct obj_section *sec; |
2142 | |
2143 | f->sections = xrealloc_vector(f->sections, 2, newidx); |
2144 | f->sections[newidx] = sec = arch_new_section(); |
2145 | |
2146 | sec->header.sh_type = SHT_PROGBITS; |
2147 | sec->header.sh_flags = SHF_WRITE | SHF_ALLOC; |
2148 | sec->header.sh_size = size; |
2149 | sec->header.sh_addralign = align; |
2150 | sec->name = name; |
2151 | sec->idx = newidx; |
2152 | if (size) |
2153 | sec->contents = xzalloc(size); |
2154 | |
2155 | obj_insert_section_load_order(f, sec); |
2156 | |
2157 | return sec; |
2158 | } |
2159 | |
2160 | static struct obj_section *obj_create_alloced_section_first(struct obj_file *f, |
2161 | const char *name, |
2162 | unsigned long align, |
2163 | unsigned long size) |
2164 | { |
2165 | int newidx = f->header.e_shnum++; |
2166 | struct obj_section *sec; |
2167 | |
2168 | f->sections = xrealloc_vector(f->sections, 2, newidx); |
2169 | f->sections[newidx] = sec = arch_new_section(); |
2170 | |
2171 | sec->header.sh_type = SHT_PROGBITS; |
2172 | sec->header.sh_flags = SHF_WRITE | SHF_ALLOC; |
2173 | sec->header.sh_size = size; |
2174 | sec->header.sh_addralign = align; |
2175 | sec->name = name; |
2176 | sec->idx = newidx; |
2177 | if (size) |
2178 | sec->contents = xzalloc(size); |
2179 | |
2180 | sec->load_next = f->load_order; |
2181 | f->load_order = sec; |
2182 | if (f->load_order_search_start == &f->load_order) |
2183 | f->load_order_search_start = &sec->load_next; |
2184 | |
2185 | return sec; |
2186 | } |
2187 | |
2188 | static void *obj_extend_section(struct obj_section *sec, unsigned long more) |
2189 | { |
2190 | unsigned long oldsize = sec->header.sh_size; |
2191 | if (more) { |
2192 | sec->header.sh_size += more; |
2193 | sec->contents = xrealloc(sec->contents, sec->header.sh_size); |
2194 | } |
2195 | return sec->contents + oldsize; |
2196 | } |
2197 | |
2198 | |
2199 | /* Conditionally add the symbols from the given symbol set to the |
2200 | new module. */ |
2201 | |
2202 | static int |
2203 | add_symbols_from( struct obj_file *f, |
2204 | int idx, struct new_module_symbol *syms, size_t nsyms) |
2205 | { |
2206 | struct new_module_symbol *s; |
2207 | size_t i; |
2208 | int used = 0; |
2209 | #ifdef SYMBOL_PREFIX |
2210 | char *name_buf = 0; |
2211 | size_t name_alloced_size = 0; |
2212 | #endif |
2213 | #if ENABLE_FEATURE_CHECK_TAINTED_MODULE |
2214 | int gpl; |
2215 | |
2216 | gpl = obj_gpl_license(f, NULL) == 0; |
2217 | #endif |
2218 | for (i = 0, s = syms; i < nsyms; ++i, ++s) { |
2219 | /* Only add symbols that are already marked external. |
2220 | If we override locals we may cause problems for |
2221 | argument initialization. We will also create a false |
2222 | dependency on the module. */ |
2223 | struct obj_symbol *sym; |
2224 | char *name; |
2225 | |
2226 | /* GPL licensed modules can use symbols exported with |
2227 | * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the |
2228 | * exported names. Non-GPL modules never see any GPLONLY_ |
2229 | * symbols so they cannot fudge it by adding the prefix on |
2230 | * their references. |
2231 | */ |
2232 | if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) { |
2233 | #if ENABLE_FEATURE_CHECK_TAINTED_MODULE |
2234 | if (gpl) |
2235 | s->name += 8; |
2236 | else |
2237 | #endif |
2238 | continue; |
2239 | } |
2240 | name = (char *)s->name; |
2241 | |
2242 | #ifdef SYMBOL_PREFIX |
2243 | /* Prepend SYMBOL_PREFIX to the symbol's name (the |
2244 | kernel exports `C names', but module object files |
2245 | reference `linker names'). */ |
2246 | size_t extra = sizeof SYMBOL_PREFIX; |
2247 | size_t name_size = strlen(name) + extra; |
2248 | if (name_size > name_alloced_size) { |
2249 | name_alloced_size = name_size * 2; |
2250 | name_buf = alloca(name_alloced_size); |
2251 | } |
2252 | strcpy(name_buf, SYMBOL_PREFIX); |
2253 | strcpy(name_buf + extra - 1, name); |
2254 | name = name_buf; |
2255 | #endif /* SYMBOL_PREFIX */ |
2256 | |
2257 | sym = obj_find_symbol(f, name); |
2258 | if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) { |
2259 | #ifdef SYMBOL_PREFIX |
2260 | /* Put NAME_BUF into more permanent storage. */ |
2261 | name = xmalloc(name_size); |
2262 | strcpy(name, name_buf); |
2263 | #endif |
2264 | sym = obj_add_symbol(f, name, -1, |
2265 | ELF_ST_INFO(STB_GLOBAL, |
2266 | STT_NOTYPE), |
2267 | idx, s->value, 0); |
2268 | /* Did our symbol just get installed? If so, mark the |
2269 | module as "used". */ |
2270 | if (sym->secidx == idx) |
2271 | used = 1; |
2272 | } |
2273 | } |
2274 | |
2275 | return used; |
2276 | } |
2277 | |
2278 | static void add_kernel_symbols(struct obj_file *f) |
2279 | { |
2280 | struct external_module *m; |
2281 | int i, nused = 0; |
2282 | |
2283 | /* Add module symbols first. */ |
2284 | |
2285 | for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m) { |
2286 | if (m->nsyms |
2287 | && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms, m->nsyms) |
2288 | ) { |
2289 | m->used = 1; |
2290 | ++nused; |
2291 | } |
2292 | } |
2293 | |
2294 | n_ext_modules_used = nused; |
2295 | |
2296 | /* And finally the symbols from the kernel proper. */ |
2297 | |
2298 | if (nksyms) |
2299 | add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms); |
2300 | } |
2301 | |
2302 | static char *get_modinfo_value(struct obj_file *f, const char *key) |
2303 | { |
2304 | struct obj_section *sec; |
2305 | char *p, *v, *n, *ep; |
2306 | size_t klen = strlen(key); |
2307 | |
2308 | sec = obj_find_section(f, ".modinfo"); |
2309 | if (sec == NULL) |
2310 | return NULL; |
2311 | p = sec->contents; |
2312 | ep = p + sec->header.sh_size; |
2313 | while (p < ep) { |
2314 | v = strchr(p, '='); |
2315 | n = strchr(p, '\0'); |
2316 | if (v) { |
2317 | if (p + klen == v && strncmp(p, key, klen) == 0) |
2318 | return v + 1; |
2319 | } else { |
2320 | if (p + klen == n && strcmp(p, key) == 0) |
2321 | return n; |
2322 | } |
2323 | p = n + 1; |
2324 | } |
2325 | |
2326 | return NULL; |
2327 | } |
2328 | |
2329 | |
2330 | /*======================================================================*/ |
2331 | /* Functions relating to module loading after 2.1.18. */ |
2332 | |
2333 | /* From Linux-2.6 sources */ |
2334 | /* You can use " around spaces, but can't escape ". */ |
2335 | /* Hyphens and underscores equivalent in parameter names. */ |
2336 | static char *next_arg(char *args, char **param, char **val) |
2337 | { |
2338 | unsigned int i, equals = 0; |
2339 | int in_quote = 0, quoted = 0; |
2340 | char *next; |
2341 | |
2342 | if (*args == '"') { |
2343 | args++; |
2344 | in_quote = 1; |
2345 | quoted = 1; |
2346 | } |
2347 | |
2348 | for (i = 0; args[i]; i++) { |
2349 | if (args[i] == ' ' && !in_quote) |
2350 | break; |
2351 | if (equals == 0) { |
2352 | if (args[i] == '=') |
2353 | equals = i; |
2354 | } |
2355 | if (args[i] == '"') |
2356 | in_quote = !in_quote; |
2357 | } |
2358 | |
2359 | *param = args; |
2360 | if (!equals) |
2361 | *val = NULL; |
2362 | else { |
2363 | args[equals] = '\0'; |
2364 | *val = args + equals + 1; |
2365 | |
2366 | /* Don't include quotes in value. */ |
2367 | if (**val == '"') { |
2368 | (*val)++; |
2369 | if (args[i-1] == '"') |
2370 | args[i-1] = '\0'; |
2371 | } |
2372 | if (quoted && args[i-1] == '"') |
2373 | args[i-1] = '\0'; |
2374 | } |
2375 | |
2376 | if (args[i]) { |
2377 | args[i] = '\0'; |
2378 | next = args + i + 1; |
2379 | } else |
2380 | next = args + i; |
2381 | |
2382 | /* Chew up trailing spaces. */ |
2383 | return skip_whitespace(next); |
2384 | } |
2385 | |
2386 | static void |
2387 | new_process_module_arguments(struct obj_file *f, const char *options) |
2388 | { |
2389 | char *xoptions, *pos; |
2390 | char *param, *val; |
2391 | |
2392 | xoptions = pos = xstrdup(skip_whitespace(options)); |
2393 | while (*pos) { |
2394 | unsigned long charssize = 0; |
2395 | char *tmp, *contents, *loc, *pinfo, *p; |
2396 | struct obj_symbol *sym; |
2397 | int min, max, n, len; |
2398 | |
2399 | pos = next_arg(pos, ¶m, &val); |
2400 | |
2401 | tmp = xasprintf("parm_%s", param); |
2402 | pinfo = get_modinfo_value(f, tmp); |
2403 | free(tmp); |
2404 | if (pinfo == NULL) |
2405 | bb_error_msg_and_die("invalid parameter %s", param); |
2406 | |
2407 | #ifdef SYMBOL_PREFIX |
2408 | tmp = xasprintf(SYMBOL_PREFIX "%s", param); |
2409 | sym = obj_find_symbol(f, tmp); |
2410 | free(tmp); |
2411 | #else |
2412 | sym = obj_find_symbol(f, param); |
2413 | #endif |
2414 | |
2415 | /* Also check that the parameter was not resolved from the kernel. */ |
2416 | if (sym == NULL || sym->secidx > SHN_HIRESERVE) |
2417 | bb_error_msg_and_die("symbol for parameter %s not found", param); |
2418 | |
2419 | /* Number of parameters */ |
2420 | if (isdigit(*pinfo)) { |
2421 | min = strtoul(pinfo, &pinfo, 10); |
2422 | if (*pinfo == '-') |
2423 | max = strtoul(pinfo + 1, &pinfo, 10); |
2424 | else |
2425 | max = min; |
2426 | } else |
2427 | min = max = 1; |
2428 | |
2429 | contents = f->sections[sym->secidx]->contents; |
2430 | loc = contents + sym->value; |
2431 | |
2432 | if (*pinfo == 'c') { |
2433 | if (!isdigit(*(pinfo + 1))) { |
2434 | bb_error_msg_and_die("parameter type 'c' for %s must be followed by" |
2435 | " the maximum size", param); |
2436 | } |
2437 | charssize = strtoul(pinfo + 1, (char **) NULL, 10); |
2438 | } |
2439 | |
2440 | if (val == NULL) { |
2441 | if (*pinfo != 'b') |
2442 | bb_error_msg_and_die("argument expected for parameter %s", param); |
2443 | val = (char *) "1"; |
2444 | } |
2445 | |
2446 | /* Parse parameter values */ |
2447 | n = 0; |
2448 | p = val; |
2449 | while (*p != 0) { |
2450 | if (++n > max) |
2451 | bb_error_msg_and_die("too many values for %s (max %d)", param, max); |
2452 | |
2453 | switch (*pinfo) { |
2454 | case 's': |
2455 | len = strcspn(p, ","); |
2456 | p[len] = 0; |
2457 | obj_string_patch(f, sym->secidx, |
2458 | loc - contents, p); |
2459 | loc += tgt_sizeof_char_p; |
2460 | p += len; |
2461 | break; |
2462 | case 'c': |
2463 | len = strcspn(p, ","); |
2464 | p[len] = 0; |
2465 | if (len >= charssize) |
2466 | bb_error_msg_and_die("string too long for %s (max %ld)", param, |
2467 | charssize - 1); |
2468 | strcpy((char *) loc, p); |
2469 | loc += charssize; |
2470 | p += len; |
2471 | break; |
2472 | case 'b': |
2473 | *loc++ = strtoul(p, &p, 0); |
2474 | break; |
2475 | case 'h': |
2476 | *(short *) loc = strtoul(p, &p, 0); |
2477 | loc += tgt_sizeof_short; |
2478 | break; |
2479 | case 'i': |
2480 | *(int *) loc = strtoul(p, &p, 0); |
2481 | loc += tgt_sizeof_int; |
2482 | break; |
2483 | case 'l': |
2484 | *(long *) loc = strtoul(p, &p, 0); |
2485 | loc += tgt_sizeof_long; |
2486 | break; |
2487 | default: |
2488 | bb_error_msg_and_die("unknown parameter type '%c' for %s", |
2489 | *pinfo, param); |
2490 | } |
2491 | |
2492 | p = skip_whitespace(p); |
2493 | if (*p != ',') |
2494 | break; |
2495 | p = skip_whitespace(p + 1); |
2496 | } |
2497 | |
2498 | if (n < min) |
2499 | bb_error_msg_and_die("parameter %s requires at least %d arguments", param, min); |
2500 | if (*p != '\0') |
2501 | bb_error_msg_and_die("invalid argument syntax for %s", param); |
2502 | } |
2503 | |
2504 | free(xoptions); |
2505 | } |
2506 | |
2507 | #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING |
2508 | static int new_is_module_checksummed(struct obj_file *f) |
2509 | { |
2510 | const char *p = get_modinfo_value(f, "using_checksums"); |
2511 | if (p) |
2512 | return xatoi(p); |
2513 | return 0; |
2514 | } |
2515 | |
2516 | /* Get the module's kernel version in the canonical integer form. */ |
2517 | |
2518 | static int |
2519 | new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN]) |
2520 | { |
2521 | char *p, *q; |
2522 | int a, b, c; |
2523 | |
2524 | p = get_modinfo_value(f, "kernel_version"); |
2525 | if (p == NULL) |
2526 | return -1; |
2527 | safe_strncpy(str, p, STRVERSIONLEN); |
2528 | |
2529 | a = strtoul(p, &p, 10); |
2530 | if (*p != '.') |
2531 | return -1; |
2532 | b = strtoul(p + 1, &p, 10); |
2533 | if (*p != '.') |
2534 | return -1; |
2535 | c = strtoul(p + 1, &q, 10); |
2536 | if (p + 1 == q) |
2537 | return -1; |
2538 | |
2539 | return a << 16 | b << 8 | c; |
2540 | } |
2541 | |
2542 | #endif /* FEATURE_INSMOD_VERSION_CHECKING */ |
2543 | |
2544 | |
2545 | /* Fetch the loaded modules, and all currently exported symbols. */ |
2546 | |
2547 | static void new_get_kernel_symbols(void) |
2548 | { |
2549 | char *module_names, *mn; |
2550 | struct external_module *modules, *m; |
2551 | struct new_module_symbol *syms, *s; |
2552 | size_t ret, bufsize, nmod, nsyms, i, j; |
2553 | |
2554 | /* Collect the loaded modules. */ |
2555 | |
2556 | bufsize = 256; |
2557 | module_names = xmalloc(bufsize); |
2558 | |
2559 | retry_modules_load: |
2560 | if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) { |
2561 | if (errno == ENOSPC && bufsize < ret) { |
2562 | bufsize = ret; |
2563 | module_names = xrealloc(module_names, bufsize); |
2564 | goto retry_modules_load; |
2565 | } |
2566 | bb_perror_msg_and_die("QM_MODULES"); |
2567 | } |
2568 | |
2569 | n_ext_modules = nmod = ret; |
2570 | |
2571 | /* Collect the modules' symbols. */ |
2572 | |
2573 | if (nmod) { |
2574 | ext_modules = modules = xzalloc(nmod * sizeof(*modules)); |
2575 | for (i = 0, mn = module_names, m = modules; |
2576 | i < nmod; ++i, ++m, mn += strlen(mn) + 1) { |
2577 | struct new_module_info info; |
2578 | |
2579 | if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) { |
2580 | if (errno == ENOENT) { |
2581 | /* The module was removed out from underneath us. */ |
2582 | continue; |
2583 | } |
2584 | bb_perror_msg_and_die("query_module: QM_INFO: %s", mn); |
2585 | } |
2586 | |
2587 | bufsize = 1024; |
2588 | syms = xmalloc(bufsize); |
2589 | retry_mod_sym_load: |
2590 | if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) { |
2591 | switch (errno) { |
2592 | case ENOSPC: |
2593 | bufsize = ret; |
2594 | syms = xrealloc(syms, bufsize); |
2595 | goto retry_mod_sym_load; |
2596 | case ENOENT: |
2597 | /* The module was removed out from underneath us. */ |
2598 | continue; |
2599 | default: |
2600 | bb_perror_msg_and_die("query_module: QM_SYMBOLS: %s", mn); |
2601 | } |
2602 | } |
2603 | nsyms = ret; |
2604 | |
2605 | m->name = mn; |
2606 | m->addr = info.addr; |
2607 | m->nsyms = nsyms; |
2608 | m->syms = syms; |
2609 | |
2610 | for (j = 0, s = syms; j < nsyms; ++j, ++s) { |
2611 | s->name += (unsigned long) syms; |
2612 | } |
2613 | } |
2614 | } |
2615 | |
2616 | /* Collect the kernel's symbols. */ |
2617 | |
2618 | syms = xmalloc(bufsize = 16 * 1024); |
2619 | retry_kern_sym_load: |
2620 | if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) { |
2621 | if (errno == ENOSPC && bufsize < ret) { |
2622 | bufsize = ret; |
2623 | syms = xrealloc(syms, bufsize); |
2624 | goto retry_kern_sym_load; |
2625 | } |
2626 | bb_perror_msg_and_die("kernel: QM_SYMBOLS"); |
2627 | } |
2628 | nksyms = nsyms = ret; |
2629 | ksyms = syms; |
2630 | |
2631 | for (j = 0, s = syms; j < nsyms; ++j, ++s) { |
2632 | s->name += (unsigned long) syms; |
2633 | } |
2634 | } |
2635 | |
2636 | |
2637 | /* Return the kernel symbol checksum version, or zero if not used. */ |
2638 | |
2639 | static int new_is_kernel_checksummed(void) |
2640 | { |
2641 | struct new_module_symbol *s; |
2642 | size_t i; |
2643 | |
2644 | /* Using_Versions is not the first symbol, but it should be in there. */ |
2645 | |
2646 | for (i = 0, s = ksyms; i < nksyms; ++i, ++s) |
2647 | if (strcmp((char *) s->name, "Using_Versions") == 0) |
2648 | return s->value; |
2649 | |
2650 | return 0; |
2651 | } |
2652 | |
2653 | |
2654 | static void new_create_this_module(struct obj_file *f, const char *m_name) |
2655 | { |
2656 | struct obj_section *sec; |
2657 | |
2658 | sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long, |
2659 | sizeof(struct new_module)); |
2660 | /* done by obj_create_alloced_section_first: */ |
2661 | /*memset(sec->contents, 0, sizeof(struct new_module));*/ |
2662 | |
2663 | obj_add_symbol(f, SPFX "__this_module", -1, |
2664 | ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0, |
2665 | sizeof(struct new_module)); |
2666 | |
2667 | obj_string_patch(f, sec->idx, offsetof(struct new_module, name), |
2668 | m_name); |
2669 | } |
2670 | |
2671 | #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS |
2672 | /* add an entry to the __ksymtab section, creating it if necessary */ |
2673 | static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym) |
2674 | { |
2675 | struct obj_section *sec; |
2676 | ElfW(Addr) ofs; |
2677 | |
2678 | /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section. |
2679 | * If __ksymtab is defined but not marked alloc, x out the first character |
2680 | * (no obj_delete routine) and create a new __ksymtab with the correct |
2681 | * characteristics. |
2682 | */ |
2683 | sec = obj_find_section(f, "__ksymtab"); |
2684 | if (sec && !(sec->header.sh_flags & SHF_ALLOC)) { |
2685 | *((char *)(sec->name)) = 'x'; /* override const */ |
2686 | sec = NULL; |
2687 | } |
2688 | if (!sec) |
2689 | sec = obj_create_alloced_section(f, "__ksymtab", |
2690 | tgt_sizeof_void_p, 0); |
2691 | if (!sec) |
2692 | return; |
2693 | sec->header.sh_flags |= SHF_ALLOC; |
2694 | /* Empty section might be byte-aligned */ |
2695 | sec->header.sh_addralign = tgt_sizeof_void_p; |
2696 | ofs = sec->header.sh_size; |
2697 | obj_symbol_patch(f, sec->idx, ofs, sym); |
2698 | obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name); |
2699 | obj_extend_section(sec, 2 * tgt_sizeof_char_p); |
2700 | } |
2701 | #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */ |
2702 | |
2703 | static int new_create_module_ksymtab(struct obj_file *f) |
2704 | { |
2705 | struct obj_section *sec; |
2706 | int i; |
2707 | |
2708 | /* We must always add the module references. */ |
2709 | |
2710 | if (n_ext_modules_used) { |
2711 | struct new_module_ref *dep; |
2712 | struct obj_symbol *tm; |
2713 | |
2714 | sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p, |
2715 | (sizeof(struct new_module_ref) |
2716 | * n_ext_modules_used)); |
2717 | if (!sec) |
2718 | return 0; |
2719 | |
2720 | tm = obj_find_symbol(f, SPFX "__this_module"); |
2721 | dep = (struct new_module_ref *) sec->contents; |
2722 | for (i = 0; i < n_ext_modules; ++i) |
2723 | if (ext_modules[i].used) { |
2724 | dep->dep = ext_modules[i].addr; |
2725 | obj_symbol_patch(f, sec->idx, |
2726 | (char *) &dep->ref - sec->contents, tm); |
2727 | dep->next_ref = 0; |
2728 | ++dep; |
2729 | } |
2730 | } |
2731 | |
2732 | if (!flag_noexport && !obj_find_section(f, "__ksymtab")) { |
2733 | size_t nsyms; |
2734 | int *loaded; |
2735 | |
2736 | sec = obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p, 0); |
2737 | |
2738 | /* We don't want to export symbols residing in sections that |
2739 | aren't loaded. There are a number of these created so that |
2740 | we make sure certain module options don't appear twice. */ |
2741 | |
2742 | loaded = alloca(sizeof(int) * (i = f->header.e_shnum)); |
2743 | while (--i >= 0) |
2744 | loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0; |
2745 | |
2746 | for (nsyms = i = 0; i < HASH_BUCKETS; ++i) { |
2747 | struct obj_symbol *sym; |
2748 | for (sym = f->symtab[i]; sym; sym = sym->next) |
2749 | if (ELF_ST_BIND(sym->info) != STB_LOCAL |
2750 | && sym->secidx <= SHN_HIRESERVE |
2751 | && (sym->secidx >= SHN_LORESERVE |
2752 | || loaded[sym->secidx])) { |
2753 | ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p; |
2754 | |
2755 | obj_symbol_patch(f, sec->idx, ofs, sym); |
2756 | obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, |
2757 | sym->name); |
2758 | |
2759 | nsyms++; |
2760 | } |
2761 | } |
2762 | |
2763 | obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p); |
2764 | } |
2765 | |
2766 | return 1; |
2767 | } |
2768 | |
2769 | |
2770 | static int |
2771 | new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size) |
2772 | { |
2773 | struct new_module *module; |
2774 | struct obj_section *sec; |
2775 | void *image; |
2776 | int ret; |
2777 | tgt_long m_addr; |
2778 | |
2779 | sec = obj_find_section(f, ".this"); |
2780 | if (!sec || !sec->contents) { |
2781 | bb_perror_msg_and_die("corrupt module %s?", m_name); |
2782 | } |
2783 | module = (struct new_module *) sec->contents; |
2784 | m_addr = sec->header.sh_addr; |
2785 | |
2786 | module->size_of_struct = sizeof(*module); |
2787 | module->size = m_size; |
2788 | module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0; |
2789 | |
2790 | sec = obj_find_section(f, "__ksymtab"); |
2791 | if (sec && sec->header.sh_size) { |
2792 | module->syms = sec->header.sh_addr; |
2793 | module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p); |
2794 | } |
2795 | |
2796 | if (n_ext_modules_used) { |
2797 | sec = obj_find_section(f, ".kmodtab"); |
2798 | module->deps = sec->header.sh_addr; |
2799 | module->ndeps = n_ext_modules_used; |
2800 | } |
2801 | |
2802 | module->init = |
2803 | obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module")); |
2804 | module->cleanup = |
2805 | obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module")); |
2806 | |
2807 | sec = obj_find_section(f, "__ex_table"); |
2808 | if (sec) { |
2809 | module->ex_table_start = sec->header.sh_addr; |
2810 | module->ex_table_end = sec->header.sh_addr + sec->header.sh_size; |
2811 | } |
2812 | |
2813 | sec = obj_find_section(f, ".text.init"); |
2814 | if (sec) { |
2815 | module->runsize = sec->header.sh_addr - m_addr; |
2816 | } |
2817 | sec = obj_find_section(f, ".data.init"); |
2818 | if (sec) { |
2819 | if (!module->runsize || |
2820 | module->runsize > sec->header.sh_addr - m_addr) |
2821 | module->runsize = sec->header.sh_addr - m_addr; |
2822 | } |
2823 | sec = obj_find_section(f, ARCHDATA_SEC_NAME); |
2824 | if (sec && sec->header.sh_size) { |
2825 | module->archdata_start = (void*)sec->header.sh_addr; |
2826 | module->archdata_end = module->archdata_start + sec->header.sh_size; |
2827 | } |
2828 | sec = obj_find_section(f, KALLSYMS_SEC_NAME); |
2829 | if (sec && sec->header.sh_size) { |
2830 | module->kallsyms_start = (void*)sec->header.sh_addr; |
2831 | module->kallsyms_end = module->kallsyms_start + sec->header.sh_size; |
2832 | } |
2833 | |
2834 | /* Whew! All of the initialization is complete. Collect the final |
2835 | module image and give it to the kernel. */ |
2836 | |
2837 | image = xmalloc(m_size); |
2838 | obj_create_image(f, image); |
2839 | |
2840 | ret = init_module(m_name, (struct new_module *) image); |
2841 | if (ret) |
2842 | bb_perror_msg("init_module: %s", m_name); |
2843 | |
2844 | free(image); |
2845 | |
2846 | return ret == 0; |
2847 | } |
2848 | |
2849 | |
2850 | /*======================================================================*/ |
2851 | |
2852 | static void |
2853 | obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset, |
2854 | const char *string) |
2855 | { |
2856 | struct obj_string_patch *p; |
2857 | struct obj_section *strsec; |
2858 | size_t len = strlen(string) + 1; |
2859 | char *loc; |
2860 | |
2861 | p = xmalloc(sizeof(*p)); |
2862 | p->next = f->string_patches; |
2863 | p->reloc_secidx = secidx; |
2864 | p->reloc_offset = offset; |
2865 | f->string_patches = p; |
2866 | |
2867 | strsec = obj_find_section(f, ".kstrtab"); |
2868 | if (strsec == NULL) { |
2869 | strsec = obj_create_alloced_section(f, ".kstrtab", 1, len); |
2870 | p->string_offset = 0; |
2871 | loc = strsec->contents; |
2872 | } else { |
2873 | p->string_offset = strsec->header.sh_size; |
2874 | loc = obj_extend_section(strsec, len); |
2875 | } |
2876 | memcpy(loc, string, len); |
2877 | } |
2878 | |
2879 | static void |
2880 | obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset, |
2881 | struct obj_symbol *sym) |
2882 | { |
2883 | struct obj_symbol_patch *p; |
2884 | |
2885 | p = xmalloc(sizeof(*p)); |
2886 | p->next = f->symbol_patches; |
2887 | p->reloc_secidx = secidx; |
2888 | p->reloc_offset = offset; |
2889 | p->sym = sym; |
2890 | f->symbol_patches = p; |
2891 | } |
2892 | |
2893 | static void obj_check_undefineds(struct obj_file *f) |
2894 | { |
2895 | unsigned i; |
2896 | |
2897 | for (i = 0; i < HASH_BUCKETS; ++i) { |
2898 | struct obj_symbol *sym; |
2899 | for (sym = f->symtab[i]; sym; sym = sym->next) |
2900 | if (sym->secidx == SHN_UNDEF) { |
2901 | if (ELF_ST_BIND(sym->info) == STB_WEAK) { |
2902 | sym->secidx = SHN_ABS; |
2903 | sym->value = 0; |
2904 | } else { |
2905 | if (!flag_quiet) |
2906 | bb_error_msg_and_die("unresolved symbol %s", sym->name); |
2907 | } |
2908 | } |
2909 | } |
2910 | } |
2911 | |
2912 | static void obj_allocate_commons(struct obj_file *f) |
2913 | { |
2914 | struct common_entry { |
2915 | struct common_entry *next; |
2916 | struct obj_symbol *sym; |
2917 | } *common_head = NULL; |
2918 | |
2919 | unsigned long i; |
2920 | |
2921 | for (i = 0; i < HASH_BUCKETS; ++i) { |
2922 | struct obj_symbol *sym; |
2923 | for (sym = f->symtab[i]; sym; sym = sym->next) |
2924 | if (sym->secidx == SHN_COMMON) { |
2925 | /* Collect all COMMON symbols and sort them by size so as to |
2926 | minimize space wasted by alignment requirements. */ |
2927 | { |
2928 | struct common_entry **p, *n; |
2929 | for (p = &common_head; *p; p = &(*p)->next) |
2930 | if (sym->size <= (*p)->sym->size) |
2931 | break; |
2932 | |
2933 | n = alloca(sizeof(*n)); |
2934 | n->next = *p; |
2935 | n->sym = sym; |
2936 | *p = n; |
2937 | } |
2938 | } |
2939 | } |
2940 | |
2941 | for (i = 1; i < f->local_symtab_size; ++i) { |
2942 | struct obj_symbol *sym = f->local_symtab[i]; |
2943 | if (sym && sym->secidx == SHN_COMMON) { |
2944 | struct common_entry **p, *n; |
2945 | for (p = &common_head; *p; p = &(*p)->next) |
2946 | if (sym == (*p)->sym) |
2947 | break; |
2948 | else if (sym->size < (*p)->sym->size) { |
2949 | n = alloca(sizeof(*n)); |
2950 | n->next = *p; |
2951 | n->sym = sym; |
2952 | *p = n; |
2953 | break; |
2954 | } |
2955 | } |
2956 | } |
2957 | |
2958 | if (common_head) { |
2959 | /* Find the bss section. */ |
2960 | for (i = 0; i < f->header.e_shnum; ++i) |
2961 | if (f->sections[i]->header.sh_type == SHT_NOBITS) |
2962 | break; |
2963 | |
2964 | /* If for some reason there hadn't been one, create one. */ |
2965 | if (i == f->header.e_shnum) { |
2966 | struct obj_section *sec; |
2967 | |
2968 | f->header.e_shnum++; |
2969 | f->sections = xrealloc_vector(f->sections, 2, i); |
2970 | f->sections[i] = sec = arch_new_section(); |
2971 | |
2972 | sec->header.sh_type = SHT_PROGBITS; |
2973 | sec->header.sh_flags = SHF_WRITE | SHF_ALLOC; |
2974 | sec->name = ".bss"; |
2975 | sec->idx = i; |
2976 | } |
2977 | |
2978 | /* Allocate the COMMONS. */ |
2979 | { |
2980 | ElfW(Addr) bss_size = f->sections[i]->header.sh_size; |
2981 | ElfW(Addr) max_align = f->sections[i]->header.sh_addralign; |
2982 | struct common_entry *c; |
2983 | |
2984 | for (c = common_head; c; c = c->next) { |
2985 | ElfW(Addr) align = c->sym->value; |
2986 | |
2987 | if (align > max_align) |
2988 | max_align = align; |
2989 | if (bss_size & (align - 1)) |
2990 | bss_size = (bss_size | (align - 1)) + 1; |
2991 | |
2992 | c->sym->secidx = i; |
2993 | c->sym->value = bss_size; |
2994 | |
2995 | bss_size += c->sym->size; |
2996 | } |
2997 | |
2998 | f->sections[i]->header.sh_size = bss_size; |
2999 | f->sections[i]->header.sh_addralign = max_align; |
3000 | } |
3001 | } |
3002 | |
3003 | /* For the sake of patch relocation and parameter initialization, |
3004 | allocate zeroed data for NOBITS sections now. Note that after |
3005 | this we cannot assume NOBITS are really empty. */ |
3006 | for (i = 0; i < f->header.e_shnum; ++i) { |
3007 | struct obj_section *s = f->sections[i]; |
3008 | if (s->header.sh_type == SHT_NOBITS) { |
3009 | s->contents = NULL; |
3010 | if (s->header.sh_size != 0) |
3011 | s->contents = xzalloc(s->header.sh_size); |
3012 | s->header.sh_type = SHT_PROGBITS; |
3013 | } |
3014 | } |
3015 | } |
3016 | |
3017 | static unsigned long obj_load_size(struct obj_file *f) |
3018 | { |
3019 | unsigned long dot = 0; |
3020 | struct obj_section *sec; |
3021 | |
3022 | /* Finalize the positions of the sections relative to one another. */ |
3023 | |
3024 | for (sec = f->load_order; sec; sec = sec->load_next) { |
3025 | ElfW(Addr) align; |
3026 | |
3027 | align = sec->header.sh_addralign; |
3028 | if (align && (dot & (align - 1))) |
3029 | dot = (dot | (align - 1)) + 1; |
3030 | |
3031 | sec->header.sh_addr = dot; |
3032 | dot += sec->header.sh_size; |
3033 | } |
3034 | |
3035 | return dot; |
3036 | } |
3037 | |
3038 | static int obj_relocate(struct obj_file *f, ElfW(Addr) base) |
3039 | { |
3040 | int i, n = f->header.e_shnum; |
3041 | int ret = 1; |
3042 | |
3043 | /* Finalize the addresses of the sections. */ |
3044 | |
3045 | f->baseaddr = base; |
3046 | for (i = 0; i < n; ++i) |
3047 | f->sections[i]->header.sh_addr += base; |
3048 | |
3049 | /* And iterate over all of the relocations. */ |
3050 | |
3051 | for (i = 0; i < n; ++i) { |
3052 | struct obj_section *relsec, *symsec, *targsec, *strsec; |
3053 | ElfW(RelM) * rel, *relend; |
3054 | ElfW(Sym) * symtab; |
3055 | const char *strtab; |
3056 | |
3057 | relsec = f->sections[i]; |
3058 | if (relsec->header.sh_type != SHT_RELM) |
3059 | continue; |
3060 | |
3061 | symsec = f->sections[relsec->header.sh_link]; |
3062 | targsec = f->sections[relsec->header.sh_info]; |
3063 | strsec = f->sections[symsec->header.sh_link]; |
3064 | |
3065 | rel = (ElfW(RelM) *) relsec->contents; |
3066 | relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM))); |
3067 | symtab = (ElfW(Sym) *) symsec->contents; |
3068 | strtab = (const char *) strsec->contents; |
3069 | |
3070 | for (; rel < relend; ++rel) { |
3071 | ElfW(Addr) value = 0; |
3072 | struct obj_symbol *intsym = NULL; |
3073 | unsigned long symndx; |
3074 | ElfW(Sym) * extsym = 0; |
3075 | const char *errmsg; |
3076 | |
3077 | /* Attempt to find a value to use for this relocation. */ |
3078 | |
3079 | symndx = ELF_R_SYM(rel->r_info); |
3080 | if (symndx) { |
3081 | /* Note we've already checked for undefined symbols. */ |
3082 | |
3083 | extsym = &symtab[symndx]; |
3084 | if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) { |
3085 | /* Local symbols we look up in the local table to be sure |
3086 | we get the one that is really intended. */ |
3087 | intsym = f->local_symtab[symndx]; |
3088 | } else { |
3089 | /* Others we look up in the hash table. */ |
3090 | const char *name; |
3091 | if (extsym->st_name) |
3092 | name = strtab + extsym->st_name; |
3093 | else |
3094 | name = f->sections[extsym->st_shndx]->name; |
3095 | intsym = obj_find_symbol(f, name); |
3096 | } |
3097 | |
3098 | value = obj_symbol_final_value(f, intsym); |
3099 | intsym->referenced = 1; |
3100 | } |
3101 | #if SHT_RELM == SHT_RELA |
3102 | #if defined(__alpha__) && defined(AXP_BROKEN_GAS) |
3103 | /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */ |
3104 | if (!extsym || !extsym->st_name || |
3105 | ELF_ST_BIND(extsym->st_info) != STB_LOCAL) |
3106 | #endif |
3107 | value += rel->r_addend; |
3108 | #endif |
3109 | |
3110 | /* Do it! */ |
3111 | switch (arch_apply_relocation |
3112 | (f, targsec, /*symsec,*/ intsym, rel, value) |
3113 | ) { |
3114 | case obj_reloc_ok: |
3115 | break; |
3116 | |
3117 | case obj_reloc_overflow: |
3118 | errmsg = "Relocation overflow"; |
3119 | goto bad_reloc; |
3120 | case obj_reloc_dangerous: |
3121 | errmsg = "Dangerous relocation"; |
3122 | goto bad_reloc; |
3123 | case obj_reloc_unhandled: |
3124 | errmsg = "Unhandled relocation"; |
3125 | bad_reloc: |
3126 | if (extsym) { |
3127 | bb_error_msg("%s of type %ld for %s", errmsg, |
3128 | (long) ELF_R_TYPE(rel->r_info), |
3129 | strtab + extsym->st_name); |
3130 | } else { |
3131 | bb_error_msg("%s of type %ld", errmsg, |
3132 | (long) ELF_R_TYPE(rel->r_info)); |
3133 | } |
3134 | ret = 0; |
3135 | break; |
3136 | } |
3137 | } |
3138 | } |
3139 | |
3140 | /* Finally, take care of the patches. */ |
3141 | |
3142 | if (f->string_patches) { |
3143 | struct obj_string_patch *p; |
3144 | struct obj_section *strsec; |
3145 | ElfW(Addr) strsec_base; |
3146 | strsec = obj_find_section(f, ".kstrtab"); |
3147 | strsec_base = strsec->header.sh_addr; |
3148 | |
3149 | for (p = f->string_patches; p; p = p->next) { |
3150 | struct obj_section *targsec = f->sections[p->reloc_secidx]; |
3151 | *(ElfW(Addr) *) (targsec->contents + p->reloc_offset) |
3152 | = strsec_base + p->string_offset; |
3153 | } |
3154 | } |
3155 | |
3156 | if (f->symbol_patches) { |
3157 | struct obj_symbol_patch *p; |
3158 | |
3159 | for (p = f->symbol_patches; p; p = p->next) { |
3160 | struct obj_section *targsec = f->sections[p->reloc_secidx]; |
3161 | *(ElfW(Addr) *) (targsec->contents + p->reloc_offset) |
3162 | = obj_symbol_final_value(f, p->sym); |
3163 | } |
3164 | } |
3165 | |
3166 | return ret; |
3167 | } |
3168 | |
3169 | static int obj_create_image(struct obj_file *f, char *image) |
3170 | { |
3171 | struct obj_section *sec; |
3172 | ElfW(Addr) base = f->baseaddr; |
3173 | |
3174 | for (sec = f->load_order; sec; sec = sec->load_next) { |
3175 | char *secimg; |
3176 | |
3177 | if (sec->contents == 0 || sec->header.sh_size == 0) |
3178 | continue; |
3179 | |
3180 | secimg = image + (sec->header.sh_addr - base); |
3181 | |
3182 | /* Note that we allocated data for NOBITS sections earlier. */ |
3183 | memcpy(secimg, sec->contents, sec->header.sh_size); |
3184 | } |
3185 | |
3186 | return 1; |
3187 | } |
3188 | |
3189 | /*======================================================================*/ |
3190 | |
3191 | static struct obj_file *obj_load(FILE *fp, int loadprogbits UNUSED_PARAM) |
3192 | { |
3193 | struct obj_file *f; |
3194 | ElfW(Shdr) * section_headers; |
3195 | size_t shnum, i; |
3196 | char *shstrtab; |
3197 | |
3198 | /* Read the file header. */ |
3199 | |
3200 | f = arch_new_file(); |
3201 | f->symbol_cmp = strcmp; |
3202 | f->symbol_hash = obj_elf_hash; |
3203 | f->load_order_search_start = &f->load_order; |
3204 | |
3205 | fseek(fp, 0, SEEK_SET); |
3206 | if (fread(&f->header, sizeof(f->header), 1, fp) != 1) { |
3207 | bb_perror_msg_and_die("error reading ELF header"); |
3208 | } |
3209 | |
3210 | if (f->header.e_ident[EI_MAG0] != ELFMAG0 |
3211 | || f->header.e_ident[EI_MAG1] != ELFMAG1 |
3212 | || f->header.e_ident[EI_MAG2] != ELFMAG2 |
3213 | || f->header.e_ident[EI_MAG3] != ELFMAG3) { |
3214 | bb_error_msg_and_die("not an ELF file"); |
3215 | } |
3216 | if (f->header.e_ident[EI_CLASS] != ELFCLASSM |
3217 | || f->header.e_ident[EI_DATA] != (BB_BIG_ENDIAN |
3218 | ? ELFDATA2MSB : ELFDATA2LSB) |
3219 | || f->header.e_ident[EI_VERSION] != EV_CURRENT |
3220 | || !MATCH_MACHINE(f->header.e_machine)) { |
3221 | bb_error_msg_and_die("ELF file not for this architecture"); |
3222 | } |
3223 | if (f->header.e_type != ET_REL) { |
3224 | bb_error_msg_and_die("ELF file not a relocatable object"); |
3225 | } |
3226 | |
3227 | /* Read the section headers. */ |
3228 | |
3229 | if (f->header.e_shentsize != sizeof(ElfW(Shdr))) { |
3230 | bb_error_msg_and_die("section header size mismatch: %lu != %lu", |
3231 | (unsigned long) f->header.e_shentsize, |
3232 | (unsigned long) sizeof(ElfW(Shdr))); |
3233 | } |
3234 | |
3235 | shnum = f->header.e_shnum; |
3236 | /* Growth of ->sections vector will be done by |
3237 | * xrealloc_vector(..., 2, ...), therefore we must allocate |
3238 | * at least 2^2 = 4 extra elements here. */ |
3239 | f->sections = xzalloc(sizeof(f->sections[0]) * (shnum + 4)); |
3240 | |
3241 | section_headers = alloca(sizeof(ElfW(Shdr)) * shnum); |
3242 | fseek(fp, f->header.e_shoff, SEEK_SET); |
3243 | if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) { |
3244 | bb_perror_msg_and_die("error reading ELF section headers"); |
3245 | } |
3246 | |
3247 | /* Read the section data. */ |
3248 | |
3249 | for (i = 0; i < shnum; ++i) { |
3250 | struct obj_section *sec; |
3251 | |
3252 | f->sections[i] = sec = arch_new_section(); |
3253 | |
3254 | sec->header = section_headers[i]; |
3255 | sec->idx = i; |
3256 | |
3257 | if (sec->header.sh_size) { |
3258 | switch (sec->header.sh_type) { |
3259 | case SHT_NULL: |
3260 | case SHT_NOTE: |
3261 | case SHT_NOBITS: |
3262 | /* ignore */ |
3263 | break; |
3264 | |
3265 | case SHT_PROGBITS: |
3266 | #if LOADBITS |
3267 | if (!loadprogbits) { |
3268 | sec->contents = NULL; |
3269 | break; |
3270 | } |
3271 | #endif |
3272 | case SHT_SYMTAB: |
3273 | case SHT_STRTAB: |
3274 | case SHT_RELM: |
3275 | sec->contents = NULL; |
3276 | if (sec->header.sh_size > 0) { |
3277 | sec->contents = xzalloc(sec->header.sh_size); |
3278 | fseek(fp, sec->header.sh_offset, SEEK_SET); |
3279 | if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) { |
3280 | bb_perror_msg_and_die("error reading ELF section data"); |
3281 | } |
3282 | } |
3283 | break; |
3284 | |
3285 | #if SHT_RELM == SHT_REL |
3286 | case SHT_RELA: |
3287 | bb_error_msg_and_die("RELA relocations not supported on this architecture"); |
3288 | #else |
3289 | case SHT_REL: |
3290 | bb_error_msg_and_die("REL relocations not supported on this architecture"); |
3291 | #endif |
3292 | default: |
3293 | if (sec->header.sh_type >= SHT_LOPROC) { |
3294 | /* Assume processor specific section types are debug |
3295 | info and can safely be ignored. If this is ever not |
3296 | the case (Hello MIPS?), don't put ifdefs here but |
3297 | create an arch_load_proc_section(). */ |
3298 | break; |
3299 | } |
3300 | |
3301 | bb_error_msg_and_die("can't handle sections of type %ld", |
3302 | (long) sec->header.sh_type); |
3303 | } |
3304 | } |
3305 | } |
3306 | |
3307 | /* Do what sort of interpretation as needed by each section. */ |
3308 | |
3309 | shstrtab = f->sections[f->header.e_shstrndx]->contents; |
3310 | |
3311 | for (i = 0; i < shnum; ++i) { |
3312 | struct obj_section *sec = f->sections[i]; |
3313 | sec->name = shstrtab + sec->header.sh_name; |
3314 | } |
3315 | |
3316 | for (i = 0; i < shnum; ++i) { |
3317 | struct obj_section *sec = f->sections[i]; |
3318 | |
3319 | /* .modinfo should be contents only but gcc has no attribute for that. |
3320 | * The kernel may have marked .modinfo as ALLOC, ignore this bit. |
3321 | */ |
3322 | if (strcmp(sec->name, ".modinfo") == 0) |
3323 | sec->header.sh_flags &= ~SHF_ALLOC; |
3324 | |
3325 | if (sec->header.sh_flags & SHF_ALLOC) |
3326 | obj_insert_section_load_order(f, sec); |
3327 | |
3328 | switch (sec->header.sh_type) { |
3329 | case SHT_SYMTAB: |
3330 | { |
3331 | unsigned long nsym, j; |
3332 | char *strtab; |
3333 | ElfW(Sym) * sym; |
3334 | |
3335 | if (sec->header.sh_entsize != sizeof(ElfW(Sym))) { |
3336 | bb_error_msg_and_die("symbol size mismatch: %lu != %lu", |
3337 | (unsigned long) sec->header.sh_entsize, |
3338 | (unsigned long) sizeof(ElfW(Sym))); |
3339 | } |
3340 | |
3341 | nsym = sec->header.sh_size / sizeof(ElfW(Sym)); |
3342 | strtab = f->sections[sec->header.sh_link]->contents; |
3343 | sym = (ElfW(Sym) *) sec->contents; |
3344 | |
3345 | /* Allocate space for a table of local symbols. */ |
3346 | j = f->local_symtab_size = sec->header.sh_info; |
3347 | f->local_symtab = xzalloc(j * sizeof(struct obj_symbol *)); |
3348 | |
3349 | /* Insert all symbols into the hash table. */ |
3350 | for (j = 1, ++sym; j < nsym; ++j, ++sym) { |
3351 | ElfW(Addr) val = sym->st_value; |
3352 | const char *name; |
3353 | if (sym->st_name) |
3354 | name = strtab + sym->st_name; |
3355 | else if (sym->st_shndx < shnum) |
3356 | name = f->sections[sym->st_shndx]->name; |
3357 | else |
3358 | continue; |
3359 | #if defined(__SH5__) |
3360 | /* |
3361 | * For sh64 it is possible that the target of a branch |
3362 | * requires a mode switch (32 to 16 and back again). |
3363 | * |
3364 | * This is implied by the lsb being set in the target |
3365 | * address for SHmedia mode and clear for SHcompact. |
3366 | */ |
3367 | val |= sym->st_other & 4; |
3368 | #endif |
3369 | obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx, |
3370 | val, sym->st_size); |
3371 | } |
3372 | } |
3373 | break; |
3374 | |
3375 | case SHT_RELM: |
3376 | if (sec->header.sh_entsize != sizeof(ElfW(RelM))) { |
3377 | bb_error_msg_and_die("relocation entry size mismatch: %lu != %lu", |
3378 | (unsigned long) sec->header.sh_entsize, |
3379 | (unsigned long) sizeof(ElfW(RelM))); |
3380 | } |
3381 | break; |
3382 | /* XXX Relocation code from modutils-2.3.19 is not here. |
3383 | * Why? That's about 20 lines of code from obj/obj_load.c, |
3384 | * which gets done in a second pass through the sections. |
3385 | * This BusyBox insmod does similar work in obj_relocate(). */ |
3386 | } |
3387 | } |
3388 | |
3389 | return f; |
3390 | } |
3391 | |
3392 | #if ENABLE_FEATURE_INSMOD_LOADINKMEM |
3393 | /* |
3394 | * load the unloaded sections directly into the memory allocated by |
3395 | * kernel for the module |
3396 | */ |
3397 | |
3398 | static int obj_load_progbits(FILE *fp, struct obj_file *f, char *imagebase) |
3399 | { |
3400 | ElfW(Addr) base = f->baseaddr; |
3401 | struct obj_section* sec; |
3402 | |
3403 | for (sec = f->load_order; sec; sec = sec->load_next) { |
3404 | |
3405 | /* section already loaded? */ |
3406 | if (sec->contents != NULL) |
3407 | continue; |
3408 | |
3409 | if (sec->header.sh_size == 0) |
3410 | continue; |
3411 | |
3412 | sec->contents = imagebase + (sec->header.sh_addr - base); |
3413 | fseek(fp, sec->header.sh_offset, SEEK_SET); |
3414 | if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) { |
3415 | bb_perror_msg("error reading ELF section data"); |
3416 | return 0; |
3417 | } |
3418 | |
3419 | } |
3420 | return 1; |
3421 | } |
3422 | #endif |
3423 | |
3424 | static void hide_special_symbols(struct obj_file *f) |
3425 | { |
3426 | static const char *const specials[] = { |
3427 | SPFX "cleanup_module", |
3428 | SPFX "init_module", |
3429 | SPFX "kernel_version", |
3430 | NULL |
3431 | }; |
3432 | |
3433 | struct obj_symbol *sym; |
3434 | const char *const *p; |
3435 | |
3436 | for (p = specials; *p; ++p) { |
3437 | sym = obj_find_symbol(f, *p); |
3438 | if (sym != NULL) |
3439 | sym->info = ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info)); |
3440 | } |
3441 | } |
3442 | |
3443 | |
3444 | #if ENABLE_FEATURE_CHECK_TAINTED_MODULE |
3445 | static int obj_gpl_license(struct obj_file *f, const char **license) |
3446 | { |
3447 | struct obj_section *sec; |
3448 | /* This list must match *exactly* the list of allowable licenses in |
3449 | * linux/include/linux/module.h. Checking for leading "GPL" will not |
3450 | * work, somebody will use "GPL sucks, this is proprietary". |
3451 | */ |
3452 | static const char *const gpl_licenses[] = { |
3453 | "GPL", |
3454 | "GPL v2", |
3455 | "GPL and additional rights", |
3456 | "Dual BSD/GPL", |
3457 | "Dual MPL/GPL" |
3458 | }; |
3459 | |
3460 | sec = obj_find_section(f, ".modinfo"); |
3461 | if (sec) { |
3462 | const char *value, *ptr, *endptr; |
3463 | ptr = sec->contents; |
3464 | endptr = ptr + sec->header.sh_size; |
3465 | while (ptr < endptr) { |
3466 | value = strchr(ptr, '='); |
3467 | if (value && strncmp(ptr, "license", value-ptr) == 0) { |
3468 | unsigned i; |
3469 | if (license) |
3470 | *license = value+1; |
3471 | for (i = 0; i < ARRAY_SIZE(gpl_licenses); ++i) { |
3472 | if (strcmp(value+1, gpl_licenses[i]) == 0) |
3473 | return 0; |
3474 | } |
3475 | return 2; |
3476 | } |
3477 | ptr = strchr(ptr, '\0'); |
3478 | if (ptr) |
3479 | ptr++; |
3480 | else |
3481 | ptr = endptr; |
3482 | } |
3483 | } |
3484 | return 1; |
3485 | } |
3486 | |
3487 | #define TAINT_FILENAME "/proc/sys/kernel/tainted" |
3488 | #define TAINT_PROPRIETORY_MODULE (1 << 0) |
3489 | #define TAINT_FORCED_MODULE (1 << 1) |
3490 | #define TAINT_UNSAFE_SMP (1 << 2) |
3491 | #define TAINT_URL "http://www.tux.org/lkml/#export-tainted" |
3492 | |
3493 | static void set_tainted(int fd, const char *m_name, |
3494 | int kernel_has_tainted, int taint, const char *text1, const char *text2) |
3495 | { |
3496 | static smallint printed_info; |
3497 | |
3498 | char buf[80]; |
3499 | int oldval; |
3500 | |
3501 | if (fd < 0 && !kernel_has_tainted) |
3502 | return; /* New modutils on old kernel */ |
3503 | printf("Warning: loading %s will taint the kernel: %s%s\n", |
3504 | m_name, text1, text2); |
3505 | if (!printed_info) { |
3506 | printf(" See %s for information about tainted modules\n", TAINT_URL); |
3507 | printed_info = 1; |
3508 | } |
3509 | if (fd >= 0) { |
3510 | read(fd, buf, sizeof(buf)-1); |
3511 | buf[sizeof(buf)-1] = '\0'; |
3512 | oldval = strtoul(buf, NULL, 10); |
3513 | sprintf(buf, "%d\n", oldval | taint); |
3514 | write(fd, buf, strlen(buf)); |
3515 | } |
3516 | } |
3517 | |
3518 | /* Check if loading this module will taint the kernel. */ |
3519 | static void check_tainted_module(struct obj_file *f, const char *m_name) |
3520 | { |
3521 | static const char tainted_file[] ALIGN1 = TAINT_FILENAME; |
3522 | |
3523 | int fd, kernel_has_tainted; |
3524 | const char *ptr; |
3525 | |
3526 | kernel_has_tainted = 1; |
3527 | fd = open(tainted_file, O_RDWR); |
3528 | if (fd < 0) { |
3529 | if (errno == ENOENT) |
3530 | kernel_has_tainted = 0; |
3531 | else if (errno == EACCES) |
3532 | kernel_has_tainted = 1; |
3533 | else { |
3534 | perror(tainted_file); |
3535 | kernel_has_tainted = 0; |
3536 | } |
3537 | } |
3538 | |
3539 | switch (obj_gpl_license(f, &ptr)) { |
3540 | case 0: |
3541 | break; |
3542 | case 1: |
3543 | set_tainted(fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", ""); |
3544 | break; |
3545 | case 2: |
3546 | /* The module has a non-GPL license so we pretend that the |
3547 | * kernel always has a taint flag to get a warning even on |
3548 | * kernels without the proc flag. |
3549 | */ |
3550 | set_tainted(fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr); |
3551 | break; |
3552 | default: |
3553 | set_tainted(fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", ""); |
3554 | break; |
3555 | } |
3556 | |
3557 | if (flag_force_load) |
3558 | set_tainted(fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", ""); |
3559 | |
3560 | if (fd >= 0) |
3561 | close(fd); |
3562 | } |
3563 | #else /* FEATURE_CHECK_TAINTED_MODULE */ |
3564 | #define check_tainted_module(x, y) do { } while (0); |
3565 | #endif /* FEATURE_CHECK_TAINTED_MODULE */ |
3566 | |
3567 | #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS |
3568 | /* add module source, timestamp, kernel version and a symbol for the |
3569 | * start of some sections. this info is used by ksymoops to do better |
3570 | * debugging. |
3571 | */ |
3572 | #if !ENABLE_FEATURE_INSMOD_VERSION_CHECKING |
3573 | #define get_module_version(f, str) get_module_version(str) |
3574 | #endif |
3575 | static int |
3576 | get_module_version(struct obj_file *f, char str[STRVERSIONLEN]) |
3577 | { |
3578 | #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING |
3579 | return new_get_module_version(f, str); |
3580 | #else /* FEATURE_INSMOD_VERSION_CHECKING */ |
3581 | strncpy(str, "???", sizeof(str)); |
3582 | return -1; |
3583 | #endif /* FEATURE_INSMOD_VERSION_CHECKING */ |
3584 | } |
3585 | |
3586 | /* add module source, timestamp, kernel version and a symbol for the |
3587 | * start of some sections. this info is used by ksymoops to do better |
3588 | * debugging. |
3589 | */ |
3590 | static void |
3591 | add_ksymoops_symbols(struct obj_file *f, const char *filename, |
3592 | const char *m_name) |
3593 | { |
3594 | static const char symprefix[] ALIGN1 = "__insmod_"; |
3595 | static const char section_names[][8] = { |
3596 | ".text", |
3597 | ".rodata", |
3598 | ".data", |
3599 | ".bss", |
3600 | ".sbss" |
3601 | }; |
3602 | |
3603 | struct obj_section *sec; |
3604 | struct obj_symbol *sym; |
3605 | char *name, *absolute_filename; |
3606 | char str[STRVERSIONLEN]; |
3607 | unsigned i; |
3608 | int l, lm_name, lfilename, use_ksymtab, version; |
3609 | struct stat statbuf; |
3610 | |
3611 | /* WARNING: was using realpath, but replaced by readlink to stop using |
3612 | * lots of stack. But here it seems to be able to cause problems? */ |
3613 | absolute_filename = xmalloc_readlink(filename); |
3614 | if (!absolute_filename) |
3615 | absolute_filename = xstrdup(filename); |
3616 | |
3617 | lm_name = strlen(m_name); |
3618 | lfilename = strlen(absolute_filename); |
3619 | |
3620 | /* add to ksymtab if it already exists or there is no ksymtab and other symbols |
3621 | * are not to be exported. otherwise leave ksymtab alone for now, the |
3622 | * "export all symbols" compatibility code will export these symbols later. |
3623 | */ |
3624 | use_ksymtab = obj_find_section(f, "__ksymtab") || flag_noexport; |
3625 | |
3626 | sec = obj_find_section(f, ".this"); |
3627 | if (sec) { |
3628 | /* tag the module header with the object name, last modified |
3629 | * timestamp and module version. worst case for module version |
3630 | * is 0xffffff, decimal 16777215. putting all three fields in |
3631 | * one symbol is less readable but saves kernel space. |
3632 | */ |
3633 | l = sizeof(symprefix) + /* "__insmod_" */ |
3634 | lm_name + /* module name */ |
3635 | 2 + /* "_O" */ |
3636 | lfilename + /* object filename */ |
3637 | 2 + /* "_M" */ |
3638 | 2 * sizeof(statbuf.st_mtime) + /* mtime in hex */ |
3639 | 2 + /* "_V" */ |
3640 | 8 + /* version in dec */ |
3641 | 1; /* nul */ |
3642 | name = xmalloc(l); |
3643 | if (stat(absolute_filename, &statbuf) != 0) |
3644 | statbuf.st_mtime = 0; |
3645 | version = get_module_version(f, str); /* -1 if not found */ |
3646 | snprintf(name, l, "%s%s_O%s_M%0*lX_V%d", |
3647 | symprefix, m_name, absolute_filename, |
3648 | (int)(2 * sizeof(statbuf.st_mtime)), statbuf.st_mtime, |
3649 | version); |
3650 | sym = obj_add_symbol(f, name, -1, |
3651 | ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE), |
3652 | sec->idx, sec->header.sh_addr, 0); |
3653 | if (use_ksymtab) |
3654 | new_add_ksymtab(f, sym); |
3655 | } |
3656 | free(absolute_filename); |
3657 | #ifdef _NOT_SUPPORTED_ |
3658 | /* record where the persistent data is going, same address as previous symbol */ |
3659 | |
3660 | if (f->persist) { |
3661 | l = sizeof(symprefix) + /* "__insmod_" */ |
3662 | lm_name + /* module name */ |
3663 | 2 + /* "_P" */ |
3664 | strlen(f->persist) + /* data store */ |
3665 | 1; /* nul */ |
3666 | name = xmalloc(l); |
3667 | snprintf(name, l, "%s%s_P%s", |
3668 | symprefix, m_name, f->persist); |
3669 | sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE), |
3670 | sec->idx, sec->header.sh_addr, 0); |
3671 | if (use_ksymtab) |
3672 | new_add_ksymtab(f, sym); |
3673 | } |
3674 | #endif /* _NOT_SUPPORTED_ */ |
3675 | /* tag the desired sections if size is non-zero */ |
3676 | |
3677 | for (i = 0; i < ARRAY_SIZE(section_names); ++i) { |
3678 | sec = obj_find_section(f, section_names[i]); |
3679 | if (sec && sec->header.sh_size) { |
3680 | l = sizeof(symprefix) + /* "__insmod_" */ |
3681 | lm_name + /* module name */ |
3682 | 2 + /* "_S" */ |
3683 | strlen(sec->name) + /* section name */ |
3684 | 2 + /* "_L" */ |
3685 | 8 + /* length in dec */ |
3686 | 1; /* nul */ |
3687 | name = xmalloc(l); |
3688 | snprintf(name, l, "%s%s_S%s_L%ld", |
3689 | symprefix, m_name, sec->name, |
3690 | (long)sec->header.sh_size); |
3691 | sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE), |
3692 | sec->idx, sec->header.sh_addr, 0); |
3693 | if (use_ksymtab) |
3694 | new_add_ksymtab(f, sym); |
3695 | } |
3696 | } |
3697 | } |
3698 | #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */ |
3699 | |
3700 | #if ENABLE_FEATURE_INSMOD_LOAD_MAP |
3701 | static void print_load_map(struct obj_file *f) |
3702 | { |
3703 | struct obj_section *sec; |
3704 | #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL |
3705 | struct obj_symbol **all, **p; |
3706 | int i, nsyms, *loaded; |
3707 | struct obj_symbol *sym; |
3708 | #endif |
3709 | /* Report on the section layout. */ |
3710 | |
3711 | printf("Sections: Size %-*s Align\n", |
3712 | (int) (2 * sizeof(void *)), "Address"); |
3713 | |
3714 | for (sec = f->load_order; sec; sec = sec->load_next) { |
3715 | int a; |
3716 | unsigned long tmp; |
3717 | |
3718 | for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a) |
3719 | tmp >>= 1; |
3720 | if (a == -1) |
3721 | a = 0; |
3722 | |
3723 | printf("%-15s %08lx %0*lx 2**%d\n", |
3724 | sec->name, |
3725 | (long)sec->header.sh_size, |
3726 | (int) (2 * sizeof(void *)), |
3727 | (long)sec->header.sh_addr, |
3728 | a); |
3729 | } |
3730 | #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL |
3731 | /* Quick reference which section indices are loaded. */ |
3732 | |
3733 | i = f->header.e_shnum; |
3734 | loaded = alloca(sizeof(int) * i); |
3735 | while (--i >= 0) |
3736 | loaded[i] = ((f->sections[i]->header.sh_flags & SHF_ALLOC) != 0); |
3737 | |
3738 | /* Collect the symbols we'll be listing. */ |
3739 | |
3740 | for (nsyms = i = 0; i < HASH_BUCKETS; ++i) |
3741 | for (sym = f->symtab[i]; sym; sym = sym->next) |
3742 | if (sym->secidx <= SHN_HIRESERVE |
3743 | && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])) |
3744 | ++nsyms; |
3745 | |
3746 | all = alloca(nsyms * sizeof(struct obj_symbol *)); |
3747 | |
3748 | for (i = 0, p = all; i < HASH_BUCKETS; ++i) |
3749 | for (sym = f->symtab[i]; sym; sym = sym->next) |
3750 | if (sym->secidx <= SHN_HIRESERVE |
3751 | && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])) |
3752 | *p++ = sym; |
3753 | |
3754 | /* And list them. */ |
3755 | printf("\nSymbols:\n"); |
3756 | for (p = all; p < all + nsyms; ++p) { |
3757 | char type = '?'; |
3758 | unsigned long value; |
3759 | |
3760 | sym = *p; |
3761 | if (sym->secidx == SHN_ABS) { |
3762 | type = 'A'; |
3763 | value = sym->value; |
3764 | } else if (sym->secidx == SHN_UNDEF) { |
3765 | type = 'U'; |
3766 | value = 0; |
3767 | } else { |
3768 | sec = f->sections[sym->secidx]; |
3769 | |
3770 | if (sec->header.sh_type == SHT_NOBITS) |
3771 | type = 'B'; |
3772 | else if (sec->header.sh_flags & SHF_ALLOC) { |
3773 | if (sec->header.sh_flags & SHF_EXECINSTR) |
3774 | type = 'T'; |
3775 | else if (sec->header.sh_flags & SHF_WRITE) |
3776 | type = 'D'; |
3777 | else |
3778 | type = 'R'; |
3779 | } |
3780 | value = sym->value + sec->header.sh_addr; |
3781 | } |
3782 | |
3783 | if (ELF_ST_BIND(sym->info) == STB_LOCAL) |
3784 | type = tolower(type); |
3785 | |
3786 | printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value, |
3787 | type, sym->name); |
3788 | } |
3789 | #endif |
3790 | } |
3791 | #else /* !FEATURE_INSMOD_LOAD_MAP */ |
3792 | static void print_load_map(struct obj_file *f UNUSED_PARAM) |
3793 | { |
3794 | } |
3795 | #endif |
3796 | |
3797 | int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options UNUSED_PARAM) |
3798 | { |
3799 | int k_crcs; |
3800 | unsigned long m_size; |
3801 | ElfW(Addr) m_addr; |
3802 | struct obj_file *f; |
3803 | struct utsname uts; |
3804 | int exit_status = EXIT_FAILURE; |
3805 | int m_has_modinfo; |
3806 | char *m_name; |
3807 | #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING |
3808 | char m_strversion[STRVERSIONLEN]; |
3809 | int m_version, m_crcs; |
3810 | #endif |
3811 | FILE *fp; |
3812 | |
3813 | uname(&uts); |
3814 | fp = fopen_for_read(m_filename); |
3815 | if (fp == NULL) |
3816 | return EXIT_FAILURE; |
3817 | |
3818 | m_name = xstrdup(bb_basename(m_filename)); |
3819 | *strrchr(m_name, '.') = 0; |
3820 | |
3821 | f = obj_load(fp, LOADBITS); |
3822 | |
3823 | if (get_modinfo_value(f, "kernel_version") == NULL) |
3824 | m_has_modinfo = 0; |
3825 | else |
3826 | m_has_modinfo = 1; |
3827 | |
3828 | #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING |
3829 | /* Version correspondence? */ |
3830 | if (!flag_quiet) { |
3831 | if (m_has_modinfo) { |
3832 | m_version = new_get_module_version(f, m_strversion); |
3833 | if (m_version == -1) { |
3834 | bb_error_msg_and_die("cannot find the kernel version the module was " |
3835 | "compiled for"); |
3836 | } |
3837 | } |
3838 | |
3839 | if (strncmp(uts.release, m_strversion, STRVERSIONLEN) != 0) { |
3840 | bb_error_msg("%skernel-module version mismatch\n" |
3841 | "\t%s was compiled for kernel version %s\n" |
3842 | "\twhile this kernel is version %s", |
3843 | flag_force_load ? "warning: " : "", |
3844 | m_name, m_strversion, uts.release); |
3845 | if (!flag_force_load) |
3846 | goto out; |
3847 | } |
3848 | } |
3849 | k_crcs = 0; |
3850 | #endif /* FEATURE_INSMOD_VERSION_CHECKING */ |
3851 | |
3852 | if (query_module(NULL, 0, NULL, 0, NULL)) |
3853 | bb_error_msg_and_die("not configured to support old kernels"); |
3854 | new_get_kernel_symbols(); |
3855 | k_crcs = new_is_kernel_checksummed(); |
3856 | |
3857 | #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING |
3858 | m_crcs = 0; |
3859 | if (m_has_modinfo) |
3860 | m_crcs = new_is_module_checksummed(f); |
3861 | |
3862 | if (m_crcs != k_crcs) |
3863 | obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash); |
3864 | #endif /* FEATURE_INSMOD_VERSION_CHECKING */ |
3865 | |
3866 | /* Let the module know about the kernel symbols. */ |
3867 | add_kernel_symbols(f); |
3868 | |
3869 | /* Allocate common symbols, symbol tables, and string tables. */ |
3870 | |
3871 | new_create_this_module(f, m_name); |
3872 | obj_check_undefineds(f); |
3873 | obj_allocate_commons(f); |
3874 | check_tainted_module(f, m_name); |
3875 | |
3876 | /* done with the module name, on to the optional var=value arguments */ |
3877 | new_process_module_arguments(f, options); |
3878 | |
3879 | arch_create_got(f); |
3880 | hide_special_symbols(f); |
3881 | |
3882 | #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS |
3883 | add_ksymoops_symbols(f, m_filename, m_name); |
3884 | #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */ |
3885 | |
3886 | new_create_module_ksymtab(f); |
3887 | |
3888 | /* Find current size of the module */ |
3889 | m_size = obj_load_size(f); |
3890 | |
3891 | m_addr = create_module(m_name, m_size); |
3892 | if (m_addr == (ElfW(Addr))(-1)) switch (errno) { |
3893 | case EEXIST: |
3894 | bb_error_msg_and_die("a module named %s already exists", m_name); |
3895 | case ENOMEM: |
3896 | bb_error_msg_and_die("can't allocate kernel memory for module; needed %lu bytes", |
3897 | m_size); |
3898 | default: |
3899 | bb_perror_msg_and_die("create_module: %s", m_name); |
3900 | } |
3901 | |
3902 | #if !LOADBITS |
3903 | /* |
3904 | * the PROGBITS section was not loaded by the obj_load |
3905 | * now we can load them directly into the kernel memory |
3906 | */ |
3907 | if (!obj_load_progbits(fp, f, (char*)m_addr)) { |
3908 | delete_module(m_name, 0); |
3909 | goto out; |
3910 | } |
3911 | #endif |
3912 | |
3913 | if (!obj_relocate(f, m_addr)) { |
3914 | delete_module(m_name, 0); |
3915 | goto out; |
3916 | } |
3917 | |
3918 | if (!new_init_module(m_name, f, m_size)) { |
3919 | delete_module(m_name, 0); |
3920 | goto out; |
3921 | } |
3922 | |
3923 | if (flag_print_load_map) |
3924 | print_load_map(f); |
3925 | |
3926 | exit_status = EXIT_SUCCESS; |
3927 | |
3928 | out: |
3929 | if (fp) |
3930 | fclose(fp); |
3931 | free(m_name); |
3932 | |
3933 | return exit_status; |
3934 | } |