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