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