-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathinsmod.c
3727 lines (3064 loc) · 88.2 KB
/
insmod.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi: set sw=4 ts=4: */
/*
* Mini insmod implementation for busybox
*
* This version of insmod supports x86, ARM, SH3/4, powerpc, m68k,
* and MIPS.
*
*
* Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
* Copyright (C) 1999-2002 Erik Andersen <[email protected]>
* Written by Erik Andersen and Ron Alder <[email protected]>
*
* Modified by Bryan Rittmeyer <[email protected]> to support SH4
* and (theoretically) SH3. I have only tested SH4 in little endian mode.
*
* Modified by Alcove, Julien Gaulmin <[email protected]> and
* Nicolas Ferre <[email protected]> to support ARM7TDMI. Only
* very minor changes required to also work with StrongArm and presumably
* all ARM based systems.
*
* Magnus Damm <[email protected]> 22-May-2002.
* The plt and got code are now using the same structs.
* Added generic linked list code to fully support PowerPC.
* Replaced the mess in arch_apply_relocation() with architecture blocks.
* The arch_create_got() function got cleaned up with architecture blocks.
* These blocks should be easy maintain and sync with obj_xxx.c in modutils.
*
* Magnus Damm <[email protected]> added PowerPC support 20-Feb-2001.
* PowerPC specific code stolen from modutils-2.3.16,
* written by Paul Mackerras, Copyright 1996, 1997 Linux International.
* I've only tested the code on mpc8xx platforms in big-endian mode.
* Did some cleanup and added BB_USE_xxx_ENTRIES...
*
* Quinn Jensen <[email protected]> added MIPS support 23-Feb-2001.
* based on modutils-2.4.2
* MIPS specific support for Elf loading and relocation.
* Copyright 1996, 1997 Linux International.
* Contributed by Ralf Baechle <[email protected]>
*
* Based almost entirely on the Linux modutils-2.3.11 implementation.
* Copyright 1996, 1997 Linux International.
* New implementation contributed by Richard Henderson <[email protected]>
* Based on original work by Bjorn Ekwall <[email protected]>
* Restructured (and partly rewritten) by:
* Björn Ekwall <[email protected]> February 1999
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <errno.h>
#include <unistd.h>
#include <dirent.h>
#include <ctype.h>
#include <assert.h>
#include <string.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/utsname.h>
#include "busybox.h"
#ifdef BB_FEATURE_NEW_MODULE_INTERFACE
# undef BB_FEATURE_OLD_MODULE_INTERFACE
# define new_sys_init_module init_module
#else
# define old_sys_init_module init_module
#endif
#ifdef BB_FEATURE_INSMOD_LOADINKMEM
#define LOADBITS 0
#else
#define LOADBITS 1
#endif
#if defined(__arm__)
#define BB_USE_PLT_ENTRIES
#define BB_PLT_ENTRY_SIZE 8
#define BB_USE_GOT_ENTRIES
#define BB_GOT_ENTRY_SIZE 8
#define BB_USE_SINGLE
#define MATCH_MACHINE(x) (x == EM_ARM)
#define SHT_RELM SHT_REL
#define Elf32_RelM Elf32_Rel
#define ELFCLASSM ELFCLASS32
#endif
#if defined(__i386__)
#define BB_USE_GOT_ENTRIES
#define BB_GOT_ENTRY_SIZE 4
#define BB_USE_SINGLE
#ifndef EM_486
#define MATCH_MACHINE(x) (x == EM_386)
#else
#define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
#endif
#define SHT_RELM SHT_REL
#define Elf32_RelM Elf32_Rel
#define ELFCLASSM ELFCLASS32
#endif
#if defined(__mc68000__)
#define BB_USE_GOT_ENTRIES
#define BB_GOT_ENTRY_SIZE 4
#define BB_USE_SINGLE
#define MATCH_MACHINE(x) (x == EM_68K)
#define SHT_RELM SHT_RELA
#define Elf32_RelM Elf32_Rela
#endif
#if defined(__mips__)
/* Account for ELF spec changes. */
#ifndef EM_MIPS_RS3_LE
#ifdef EM_MIPS_RS4_BE
#define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
#else
#define EM_MIPS_RS3_LE 10
#endif
#endif /* !EM_MIPS_RS3_LE */
#define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
#define SHT_RELM SHT_REL
#define Elf32_RelM Elf32_Rel
#define ELFCLASSM ELFCLASS32
#define ARCHDATAM "__dbe_table"
#endif
#if defined(__powerpc__)
#define BB_USE_PLT_ENTRIES
#define BB_PLT_ENTRY_SIZE 16
#define BB_USE_PLT_LIST
#define BB_LIST_ARCHTYPE ElfW(Addr)
#define BB_USE_LIST
#define MATCH_MACHINE(x) (x == EM_PPC)
#define SHT_RELM SHT_RELA
#define Elf32_RelM Elf32_Rela
#define ELFCLASSM ELFCLASS32
#define ARCHDATAM "__ftr_fixup"
#endif
#if defined(__sh__)
#define BB_USE_GOT_ENTRIES
#define BB_GOT_ENTRY_SIZE 4
#define BB_USE_SINGLE
#define MATCH_MACHINE(x) (x == EM_SH)
#define SHT_RELM SHT_RELA
#define Elf32_RelM Elf32_Rela
#define ELFCLASSM ELFCLASS32
/* the SH changes have only been tested on the SH4 in =little endian= mode */
/* I'm not sure about big endian, so let's warn: */
#if (defined(__SH4__) || defined(__SH3__)) && defined(__BIG_ENDIAN__)
#error insmod.c may require changes for use on big endian SH4/SH3
#endif
/* it may or may not work on the SH1/SH2... So let's error on those
also */
#if (defined(__sh__) && (!(defined(__SH3__) || defined(__SH4__))))
#error insmod.c may require changes for non-SH3/SH4 use
#endif
#endif
#ifndef SHT_RELM
#error Sorry, but insmod.c does not yet support this architecture...
#endif
//----------------------------------------------------------------------------
//--------modutils module.h, lines 45-242
//----------------------------------------------------------------------------
/* Definitions for the Linux module syscall interface.
Copyright 1996, 1997 Linux International.
Contributed by Richard Henderson <[email protected]>
This file is part of the Linux modutils.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#ifndef MODUTILS_MODULE_H
#define MODUTILS_MODULE_H
/* This file contains the structures used by the 2.0 and 2.1 kernels.
We do not use the kernel headers directly because we do not wish
to be dependant on a particular kernel version to compile insmod. */
/*======================================================================*/
/* The structures used by Linux 2.0. */
/* The symbol format used by get_kernel_syms(2). */
struct old_kernel_sym
{
unsigned long value;
char name[60];
};
struct old_module_ref
{
unsigned long module; /* kernel addresses */
unsigned long next;
};
struct old_module_symbol
{
unsigned long addr;
unsigned long name;
};
struct old_symbol_table
{
int size; /* total, including string table!!! */
int n_symbols;
int n_refs;
struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
struct old_module_ref ref[0]; /* actual size defined by n_refs */
};
struct old_mod_routines
{
unsigned long init;
unsigned long cleanup;
};
struct old_module
{
unsigned long next;
unsigned long ref; /* the list of modules that refer to me */
unsigned long symtab;
unsigned long name;
int size; /* size of module in pages */
unsigned long addr; /* address of module */
int state;
unsigned long cleanup; /* cleanup routine */
};
/* Sent to init_module(2) or'ed into the code size parameter. */
static const int OLD_MOD_AUTOCLEAN = 0x40000000; /* big enough, but no sign problems... */
int get_kernel_syms(struct old_kernel_sym *);
int old_sys_init_module(const char *name, char *code, unsigned codesize,
struct old_mod_routines *, struct old_symbol_table *);
/*======================================================================*/
/* For sizeof() which are related to the module platform and not to the
environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
#define tgt_sizeof_char sizeof(char)
#define tgt_sizeof_short sizeof(short)
#define tgt_sizeof_int sizeof(int)
#define tgt_sizeof_long sizeof(long)
#define tgt_sizeof_char_p sizeof(char *)
#define tgt_sizeof_void_p sizeof(void *)
#define tgt_long long
#if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
#undef tgt_sizeof_long
#undef tgt_sizeof_char_p
#undef tgt_sizeof_void_p
#undef tgt_long
static const int tgt_sizeof_long = 8;
static const int tgt_sizeof_char_p = 8;
static const int tgt_sizeof_void_p = 8;
#define tgt_long long long
#endif
/*======================================================================*/
/* The structures used in Linux 2.1. */
/* Note: new_module_symbol does not use tgt_long intentionally */
struct new_module_symbol
{
unsigned long value;
unsigned long name;
};
struct new_module_persist;
struct new_module_ref
{
unsigned tgt_long dep; /* kernel addresses */
unsigned tgt_long ref;
unsigned tgt_long next_ref;
};
struct new_module
{
unsigned tgt_long size_of_struct; /* == sizeof(module) */
unsigned tgt_long next;
unsigned tgt_long name;
unsigned tgt_long size;
tgt_long usecount;
unsigned tgt_long flags; /* AUTOCLEAN et al */
unsigned nsyms;
unsigned ndeps;
unsigned tgt_long syms;
unsigned tgt_long deps;
unsigned tgt_long refs;
unsigned tgt_long init;
unsigned tgt_long cleanup;
unsigned tgt_long ex_table_start;
unsigned tgt_long ex_table_end;
#ifdef __alpha__
unsigned tgt_long gp;
#endif
/* Everything after here is extension. */
unsigned tgt_long persist_start;
unsigned tgt_long persist_end;
unsigned tgt_long can_unload;
unsigned tgt_long runsize;
#ifdef BB_FEATURE_NEW_MODULE_INTERFACE
const char *kallsyms_start; /* All symbols for kernel debugging */
const char *kallsyms_end;
const char *archdata_start; /* arch specific data for module */
const char *archdata_end;
const char *kernel_data; /* Reserved for kernel internal use */
#endif
};
#ifdef ARCHDATAM
#define ARCHDATA_SEC_NAME ARCHDATAM
#else
#define ARCHDATA_SEC_NAME "__archdata"
#endif
#define KALLSYMS_SEC_NAME "__kallsyms"
struct new_module_info
{
unsigned long addr;
unsigned long size;
unsigned long flags;
long usecount;
};
/* Bits of module.flags. */
static const int NEW_MOD_RUNNING = 1;
static const int NEW_MOD_DELETED = 2;
static const int NEW_MOD_AUTOCLEAN = 4;
static const int NEW_MOD_VISITED = 8;
static const int NEW_MOD_USED_ONCE = 16;
int new_sys_init_module(const char *name, const struct new_module *);
int query_module(const char *name, int which, void *buf, size_t bufsize,
size_t *ret);
/* Values for query_module's which. */
static const int QM_MODULES = 1;
static const int QM_DEPS = 2;
static const int QM_REFS = 3;
static const int QM_SYMBOLS = 4;
static const int QM_INFO = 5;
/*======================================================================*/
/* The system calls unchanged between 2.0 and 2.1. */
unsigned long create_module(const char *, size_t);
int delete_module(const char *);
#endif /* module.h */
//----------------------------------------------------------------------------
//--------end of modutils module.h
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//--------modutils obj.h, lines 253-462
//----------------------------------------------------------------------------
/* Elf object file loading and relocation routines.
Copyright 1996, 1997 Linux International.
Contributed by Richard Henderson <[email protected]>
This file is part of the Linux modutils.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#ifndef MODUTILS_OBJ_H
static const int MODUTILS_OBJ_H = 1;
/* The relocatable object is manipulated using elfin types. */
#include <stdio.h>
#include <elf.h>
#include <endian.h>
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define ELFDATAM ELFDATA2LSB
#elif __BYTE_ORDER == __BIG_ENDIAN
#define ELFDATAM ELFDATA2MSB
#endif
#ifndef ElfW
# if ELFCLASSM == ELFCLASS32
# define ElfW(x) Elf32_ ## x
# define ELFW(x) ELF32_ ## x
# else
# define ElfW(x) Elf64_ ## x
# define ELFW(x) ELF64_ ## x
# endif
#endif
/* For some reason this is missing from libc5. */
#ifndef ELF32_ST_INFO
# define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
#endif
#ifndef ELF64_ST_INFO
# define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
#endif
struct obj_string_patch;
struct obj_symbol_patch;
struct obj_section
{
ElfW(Shdr) header;
const char *name;
char *contents;
struct obj_section *load_next;
int idx;
};
struct obj_symbol
{
struct obj_symbol *next; /* hash table link */
const char *name;
unsigned long value;
unsigned long size;
int secidx; /* the defining section index/module */
int info;
int ksymidx; /* for export to the kernel symtab */
int referenced; /* actually used in the link */
};
/* Hardcode the hash table size. We shouldn't be needing so many
symbols that we begin to degrade performance, and we get a big win
by giving the compiler a constant divisor. */
#define HASH_BUCKETS 521
struct obj_file
{
ElfW(Ehdr) header;
ElfW(Addr) baseaddr;
struct obj_section **sections;
struct obj_section *load_order;
struct obj_section **load_order_search_start;
struct obj_string_patch *string_patches;
struct obj_symbol_patch *symbol_patches;
int (*symbol_cmp)(const char *, const char *);
unsigned long (*symbol_hash)(const char *);
unsigned long local_symtab_size;
struct obj_symbol **local_symtab;
struct obj_symbol *symtab[HASH_BUCKETS];
};
enum obj_reloc
{
obj_reloc_ok,
obj_reloc_overflow,
obj_reloc_dangerous,
obj_reloc_unhandled
};
struct obj_string_patch
{
struct obj_string_patch *next;
int reloc_secidx;
ElfW(Addr) reloc_offset;
ElfW(Addr) string_offset;
};
struct obj_symbol_patch
{
struct obj_symbol_patch *next;
int reloc_secidx;
ElfW(Addr) reloc_offset;
struct obj_symbol *sym;
};
/* Generic object manipulation routines. */
static unsigned long obj_elf_hash(const char *);
static unsigned long obj_elf_hash_n(const char *, unsigned long len);
static struct obj_symbol *obj_find_symbol (struct obj_file *f,
const char *name);
static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
struct obj_symbol *sym);
#ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
static void obj_set_symbol_compare(struct obj_file *f,
int (*cmp)(const char *, const char *),
unsigned long (*hash)(const char *));
#endif
static struct obj_section *obj_find_section (struct obj_file *f,
const char *name);
static void obj_insert_section_load_order (struct obj_file *f,
struct obj_section *sec);
static struct obj_section *obj_create_alloced_section (struct obj_file *f,
const char *name,
unsigned long align,
unsigned long size);
static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
const char *name,
unsigned long align,
unsigned long size);
static void *obj_extend_section (struct obj_section *sec, unsigned long more);
static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
const char *string);
#ifdef BB_FEATURE_NEW_MODULE_INTERFACE
static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
struct obj_symbol *sym);
#endif
static int obj_check_undefineds(struct obj_file *f);
static void obj_allocate_commons(struct obj_file *f);
static unsigned long obj_load_size (struct obj_file *f);
static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
static struct obj_file *obj_load(FILE *f, int loadprogbits);
static int obj_create_image (struct obj_file *f, char *image);
/* Architecture specific manipulation routines. */
static struct obj_file *arch_new_file (void);
static struct obj_section *arch_new_section (void);
static struct obj_symbol *arch_new_symbol (void);
static enum obj_reloc arch_apply_relocation (struct obj_file *f,
struct obj_section *targsec,
struct obj_section *symsec,
struct obj_symbol *sym,
ElfW(RelM) *rel, ElfW(Addr) value);
static void arch_create_got (struct obj_file *f);
#ifdef BB_FEATURE_NEW_MODULE_INTERFACE
static int arch_init_module (struct obj_file *f, struct new_module *);
#endif
#endif /* obj.h */
//----------------------------------------------------------------------------
//--------end of modutils obj.h
//----------------------------------------------------------------------------
#define _PATH_MODULES "/lib/modules"
static const int STRVERSIONLEN = 32;
/*======================================================================*/
static int flag_force_load = 0;
static int flag_autoclean = 0;
static int flag_verbose = 0;
static int flag_quiet = 0;
static int flag_export = 1;
/*======================================================================*/
#if defined(BB_USE_LIST)
struct arch_list_entry
{
struct arch_list_entry *next;
BB_LIST_ARCHTYPE addend;
int offset;
int inited : 1;
};
#endif
#if defined(BB_USE_SINGLE)
struct arch_single_entry
{
int offset;
int inited : 1;
int allocated : 1;
};
#endif
#if defined(__mips__)
struct mips_hi16
{
struct mips_hi16 *next;
Elf32_Addr *addr;
Elf32_Addr value;
};
#endif
struct arch_file {
struct obj_file root;
#if defined(BB_USE_PLT_ENTRIES)
struct obj_section *plt;
#endif
#if defined(BB_USE_GOT_ENTRIES)
struct obj_section *got;
#endif
#if defined(__mips__)
struct mips_hi16 *mips_hi16_list;
#endif
};
struct arch_symbol {
struct obj_symbol root;
#if defined(BB_USE_PLT_ENTRIES)
#if defined(BB_USE_PLT_LIST)
struct arch_list_entry *pltent;
#else
struct arch_single_entry pltent;
#endif
#endif
#if defined(BB_USE_GOT_ENTRIES)
struct arch_single_entry gotent;
#endif
};
struct external_module {
const char *name;
ElfW(Addr) addr;
int used;
size_t nsyms;
struct new_module_symbol *syms;
};
static struct new_module_symbol *ksyms;
static size_t nksyms;
static struct external_module *ext_modules;
static int n_ext_modules;
static int n_ext_modules_used;
extern int delete_module(const char *);
static char m_filename[FILENAME_MAX];
static char m_fullName[FILENAME_MAX];
/*======================================================================*/
static int check_module_name_match(const char *filename, struct stat *statbuf,
void *userdata)
{
char *fullname = (char *) userdata;
if (fullname[0] == '\0')
return (FALSE);
else {
char *tmp, *tmp1 = strdup(filename);
tmp = get_last_path_component(tmp1);
if (strcmp(tmp, fullname) == 0) {
free(tmp1);
/* Stop searching if we find a match */
safe_strncpy(m_filename, filename, sizeof(m_filename));
return (TRUE);
}
free(tmp1);
}
return (FALSE);
}
/*======================================================================*/
static struct obj_file *arch_new_file(void)
{
struct arch_file *f;
f = xmalloc(sizeof(*f));
memset(f, 0, sizeof(*f));
return &f->root;
}
static struct obj_section *arch_new_section(void)
{
return xmalloc(sizeof(struct obj_section));
}
static struct obj_symbol *arch_new_symbol(void)
{
struct arch_symbol *sym;
sym = xmalloc(sizeof(*sym));
memset(sym, 0, sizeof(*sym));
return &sym->root;
}
static enum obj_reloc
arch_apply_relocation(struct obj_file *f,
struct obj_section *targsec,
struct obj_section *symsec,
struct obj_symbol *sym,
ElfW(RelM) *rel, ElfW(Addr) v)
{
struct arch_file *ifile = (struct arch_file *) f;
enum obj_reloc ret = obj_reloc_ok;
ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
#if defined(BB_USE_GOT_ENTRIES) || defined(BB_USE_PLT_ENTRIES)
struct arch_symbol *isym = (struct arch_symbol *) sym;
#endif
#if defined(BB_USE_GOT_ENTRIES)
ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
#endif
#if defined(BB_USE_PLT_ENTRIES)
ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
unsigned long *ip;
#if defined(BB_USE_PLT_LIST)
struct arch_list_entry *pe;
#else
struct arch_single_entry *pe;
#endif
#endif
switch (ELF32_R_TYPE(rel->r_info)) {
#if defined(__arm__)
case R_ARM_NONE:
break;
case R_ARM_ABS32:
*loc += v;
break;
case R_ARM_GOT32:
goto bb_use_got;
case R_ARM_GOTPC:
/* relative reloc, always to _GLOBAL_OFFSET_TABLE_
* (which is .got) similar to branch,
* but is full 32 bits relative */
assert(got);
*loc += got - dot;
break;
case R_ARM_PC24:
case R_ARM_PLT32:
goto bb_use_plt;
case R_ARM_GOTOFF: /* address relative to the got */
assert(got);
*loc += v - got;
break;
#elif defined(__i386__)
case R_386_NONE:
break;
case R_386_32:
*loc += v;
break;
case R_386_PLT32:
case R_386_PC32:
*loc += v - dot;
break;
case R_386_GLOB_DAT:
case R_386_JMP_SLOT:
*loc = v;
break;
case R_386_RELATIVE:
*loc += f->baseaddr;
break;
case R_386_GOTPC:
assert(got != 0);
*loc += got - dot;
break;
case R_386_GOT32:
goto bb_use_got;
case R_386_GOTOFF:
assert(got != 0);
*loc += v - got;
break;
#elif defined(__mc68000__)
case R_68K_NONE:
break;
case R_68K_32:
*loc += v;
break;
case R_68K_8:
if (v > 0xff) {
ret = obj_reloc_overflow;
}
*(char *)loc = v;
break;
case R_68K_16:
if (v > 0xffff) {
ret = obj_reloc_overflow;
}
*(short *)loc = v;
break;
case R_68K_PC8:
v -= dot;
if ((Elf32_Sword)v > 0x7f ||
(Elf32_Sword)v < -(Elf32_Sword)0x80) {
ret = obj_reloc_overflow;
}
*(char *)loc = v;
break;
case R_68K_PC16:
v -= dot;
if ((Elf32_Sword)v > 0x7fff ||
(Elf32_Sword)v < -(Elf32_Sword)0x8000) {
ret = obj_reloc_overflow;
}
*(short *)loc = v;
break;
case R_68K_PC32:
*(int *)loc = v - dot;
break;
case R_68K_GLOB_DAT:
case R_68K_JMP_SLOT:
*loc = v;
break;
case R_68K_RELATIVE:
*(int *)loc += f->baseaddr;
break;
case R_68K_GOT32:
goto bb_use_got;
case R_68K_GOTOFF:
assert(got != 0);
*loc += v - got;
break;
#elif defined(__mips__)
case R_MIPS_NONE:
break;
case R_MIPS_32:
*loc += v;
break;
case R_MIPS_26:
if (v % 4)
ret = obj_reloc_dangerous;
if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
ret = obj_reloc_overflow;
*loc =
(*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
0x03ffffff);
break;
case R_MIPS_HI16:
{
struct mips_hi16 *n;
/* We cannot relocate this one now because we don't know the value
of the carry we need to add. Save the information, and let LO16
do the actual relocation. */
n = (struct mips_hi16 *) xmalloc(sizeof *n);
n->addr = loc;
n->value = v;
n->next = ifile->mips_hi16_list;
ifile->mips_hi16_list = n;
break;
}
case R_MIPS_LO16:
{
unsigned long insnlo = *loc;
Elf32_Addr val, vallo;
/* Sign extend the addend we extract from the lo insn. */
vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
if (ifile->mips_hi16_list != NULL) {
struct mips_hi16 *l;
l = ifile->mips_hi16_list;
while (l != NULL) {
struct mips_hi16 *next;
unsigned long insn;
/* The value for the HI16 had best be the same. */
assert(v == l->value);
/* Do the HI16 relocation. Note that we actually don't
need to know anything about the LO16 itself, except where
to find the low 16 bits of the addend needed by the LO16. */
insn = *l->addr;
val =
((insn & 0xffff) << 16) +
vallo;
val += v;
/* Account for the sign extension that will happen in the
low bits. */
val =
((val >> 16) +
((val & 0x8000) !=
0)) & 0xffff;
insn = (insn & ~0xffff) | val;
*l->addr = insn;
next = l->next;
free(l);
l = next;