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