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