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