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