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