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