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