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