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