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 | 4f4bcfc | 2001-04-25 17:22:32 +0000 | [diff] [blame] | 4 | * This version of insmod now supports x86, ARM, SH3/4, powerpc, and MIPS. |
Erik Andersen | 0210432 | 1999-12-17 18:57:34 +0000 | [diff] [blame] | 5 | * |
Eric Andersen | 8ec10a9 | 2001-01-27 09:33:39 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999,2000,2001 by Lineo, inc. |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 7 | * Written by Erik Andersen <andersen@lineo.com> |
| 8 | * and Ron Alder <alder@lineo.com> |
| 9 | * |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 10 | * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4 |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 11 | * and (theoretically) SH3. I have only tested SH4 in little endian mode. |
| 12 | * |
| 13 | * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and |
| 14 | * Nicolas Ferre <nicolas.ferre@alcove.fr> to support ARM7TDMI. Only |
| 15 | * very minor changes required to also work with StrongArm and presumably |
| 16 | * all ARM based systems. |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 17 | * |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 18 | * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001. |
| 19 | * PowerPC specific code stolen from modutils-2.3.16, |
| 20 | * written by Paul Mackerras, Copyright 1996, 1997 Linux International. |
| 21 | * I've only tested the code on mpc8xx platforms in big-endian mode. |
| 22 | * Did some cleanup and added BB_USE_xxx_ENTRIES... |
| 23 | * |
Eric Andersen | 2bf658d | 2001-02-24 20:01:53 +0000 | [diff] [blame] | 24 | * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001. |
| 25 | * based on modutils-2.4.2 |
| 26 | * MIPS specific support for Elf loading and relocation. |
| 27 | * Copyright 1996, 1997 Linux International. |
| 28 | * Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu> |
| 29 | * |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 30 | * Based almost entirely on the Linux modutils-2.3.11 implementation. |
| 31 | * Copyright 1996, 1997 Linux International. |
| 32 | * New implementation contributed by Richard Henderson <rth@tamu.edu> |
| 33 | * Based on original work by Bjorn Ekwall <bj0rn@blox.se> |
| 34 | * Restructured (and partly rewritten) by: |
| 35 | * Björn Ekwall <bj0rn@blox.se> February 1999 |
Erik Andersen | 0210432 | 1999-12-17 18:57:34 +0000 | [diff] [blame] | 36 | * |
| 37 | * This program is free software; you can redistribute it and/or modify |
| 38 | * it under the terms of the GNU General Public License as published by |
| 39 | * the Free Software Foundation; either version 2 of the License, or |
| 40 | * (at your option) any later version. |
| 41 | * |
| 42 | * This program is distributed in the hope that it will be useful, |
| 43 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 44 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 45 | * General Public License for more details. |
| 46 | * |
| 47 | * You should have received a copy of the GNU General Public License |
| 48 | * along with this program; if not, write to the Free Software |
| 49 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 50 | * |
| 51 | */ |
| 52 | |
Erik Andersen | 0210432 | 1999-12-17 18:57:34 +0000 | [diff] [blame] | 53 | #include <stdlib.h> |
| 54 | #include <stdio.h> |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 55 | #include <stddef.h> |
Erik Andersen | 0210432 | 1999-12-17 18:57:34 +0000 | [diff] [blame] | 56 | #include <errno.h> |
| 57 | #include <unistd.h> |
| 58 | #include <dirent.h> |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 59 | #include <ctype.h> |
| 60 | #include <assert.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 61 | #include <string.h> |
Eric Andersen | 999bf72 | 2000-07-09 06:59:58 +0000 | [diff] [blame] | 62 | #include <getopt.h> |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 63 | #include <sys/utsname.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 64 | #include "busybox.h" |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 65 | |
Eric Andersen | 64c8b17 | 2001-04-05 07:33:10 +0000 | [diff] [blame] | 66 | #ifdef BB_FEATURE_NEW_MODULE_INTERFACE |
| 67 | # define new_sys_init_module init_module |
| 68 | #else |
| 69 | # define old_sys_init_module init_module |
| 70 | #endif |
| 71 | |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 72 | #ifdef BB_FEATURE_INSMOD_LOADINKMEM |
| 73 | #define LOADBITS 0 |
| 74 | #else |
| 75 | #define LOADBITS 1 |
| 76 | #endif |
| 77 | |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 78 | #if defined(__powerpc__) |
| 79 | #define BB_USE_PLT_ENTRIES |
| 80 | #define BB_PLT_ENTRY_SIZE 16 |
| 81 | #endif |
| 82 | |
| 83 | #if defined(__arm__) |
| 84 | #define BB_USE_PLT_ENTRIES |
| 85 | #define BB_PLT_ENTRY_SIZE 8 |
| 86 | #define BB_USE_GOT_ENTRIES |
| 87 | #define BB_GOT_ENTRY_SIZE 8 |
| 88 | #endif |
| 89 | |
| 90 | #if defined(__sh__) |
| 91 | #define BB_USE_GOT_ENTRIES |
| 92 | #define BB_GOT_ENTRY_SIZE 4 |
| 93 | #endif |
| 94 | |
| 95 | #if defined(__i386__) |
| 96 | #define BB_USE_GOT_ENTRIES |
| 97 | #define BB_GOT_ENTRY_SIZE 4 |
| 98 | #endif |
| 99 | |
Eric Andersen | 2bf658d | 2001-02-24 20:01:53 +0000 | [diff] [blame] | 100 | #if defined(__mips__) |
| 101 | // neither used |
| 102 | #endif |
| 103 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 104 | //---------------------------------------------------------------------------- |
| 105 | //--------modutils module.h, lines 45-242 |
| 106 | //---------------------------------------------------------------------------- |
| 107 | |
| 108 | /* Definitions for the Linux module syscall interface. |
| 109 | Copyright 1996, 1997 Linux International. |
| 110 | |
| 111 | Contributed by Richard Henderson <rth@tamu.edu> |
| 112 | |
| 113 | This file is part of the Linux modutils. |
| 114 | |
| 115 | This program is free software; you can redistribute it and/or modify it |
| 116 | under the terms of the GNU General Public License as published by the |
| 117 | Free Software Foundation; either version 2 of the License, or (at your |
| 118 | option) any later version. |
| 119 | |
| 120 | This program is distributed in the hope that it will be useful, but |
| 121 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 122 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 123 | General Public License for more details. |
| 124 | |
| 125 | You should have received a copy of the GNU General Public License |
| 126 | along with this program; if not, write to the Free Software Foundation, |
| 127 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 128 | |
| 129 | |
| 130 | #ifndef MODUTILS_MODULE_H |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 131 | static const int MODUTILS_MODULE_H = 1; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 132 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 133 | #ident "$Id: insmod.c,v 1.68 2001/07/17 01:12:36 andersen Exp $" |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 134 | |
| 135 | /* This file contains the structures used by the 2.0 and 2.1 kernels. |
| 136 | We do not use the kernel headers directly because we do not wish |
| 137 | to be dependant on a particular kernel version to compile insmod. */ |
| 138 | |
| 139 | |
| 140 | /*======================================================================*/ |
| 141 | /* The structures used by Linux 2.0. */ |
| 142 | |
| 143 | /* The symbol format used by get_kernel_syms(2). */ |
| 144 | struct old_kernel_sym |
| 145 | { |
| 146 | unsigned long value; |
| 147 | char name[60]; |
| 148 | }; |
| 149 | |
| 150 | struct old_module_ref |
| 151 | { |
| 152 | unsigned long module; /* kernel addresses */ |
| 153 | unsigned long next; |
| 154 | }; |
| 155 | |
| 156 | struct old_module_symbol |
| 157 | { |
| 158 | unsigned long addr; |
| 159 | unsigned long name; |
| 160 | }; |
| 161 | |
| 162 | struct old_symbol_table |
| 163 | { |
| 164 | int size; /* total, including string table!!! */ |
| 165 | int n_symbols; |
| 166 | int n_refs; |
| 167 | struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */ |
| 168 | struct old_module_ref ref[0]; /* actual size defined by n_refs */ |
| 169 | }; |
| 170 | |
| 171 | struct old_mod_routines |
| 172 | { |
| 173 | unsigned long init; |
| 174 | unsigned long cleanup; |
| 175 | }; |
| 176 | |
| 177 | struct old_module |
| 178 | { |
| 179 | unsigned long next; |
| 180 | unsigned long ref; /* the list of modules that refer to me */ |
| 181 | unsigned long symtab; |
| 182 | unsigned long name; |
| 183 | int size; /* size of module in pages */ |
| 184 | unsigned long addr; /* address of module */ |
| 185 | int state; |
| 186 | unsigned long cleanup; /* cleanup routine */ |
| 187 | }; |
| 188 | |
| 189 | /* Sent to init_module(2) or'ed into the code size parameter. */ |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 190 | static const int OLD_MOD_AUTOCLEAN = 0x40000000; /* big enough, but no sign problems... */ |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 191 | |
| 192 | int get_kernel_syms(struct old_kernel_sym *); |
| 193 | int old_sys_init_module(const char *name, char *code, unsigned codesize, |
| 194 | struct old_mod_routines *, struct old_symbol_table *); |
| 195 | |
| 196 | /*======================================================================*/ |
| 197 | /* For sizeof() which are related to the module platform and not to the |
| 198 | environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */ |
| 199 | |
| 200 | #define tgt_sizeof_char sizeof(char) |
| 201 | #define tgt_sizeof_short sizeof(short) |
| 202 | #define tgt_sizeof_int sizeof(int) |
| 203 | #define tgt_sizeof_long sizeof(long) |
| 204 | #define tgt_sizeof_char_p sizeof(char *) |
| 205 | #define tgt_sizeof_void_p sizeof(void *) |
| 206 | #define tgt_long long |
| 207 | |
| 208 | #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64) |
| 209 | #undef tgt_sizeof_long |
| 210 | #undef tgt_sizeof_char_p |
| 211 | #undef tgt_sizeof_void_p |
| 212 | #undef tgt_long |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 213 | static const int tgt_sizeof_long = 8; |
| 214 | static const int tgt_sizeof_char_p = 8; |
| 215 | static const int tgt_sizeof_void_p = 8; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 216 | #define tgt_long long long |
| 217 | #endif |
| 218 | |
| 219 | /*======================================================================*/ |
| 220 | /* The structures used in Linux 2.1. */ |
| 221 | |
| 222 | /* Note: new_module_symbol does not use tgt_long intentionally */ |
| 223 | struct new_module_symbol |
| 224 | { |
| 225 | unsigned long value; |
| 226 | unsigned long name; |
| 227 | }; |
| 228 | |
| 229 | struct new_module_persist; |
| 230 | |
| 231 | struct new_module_ref |
| 232 | { |
| 233 | unsigned tgt_long dep; /* kernel addresses */ |
| 234 | unsigned tgt_long ref; |
| 235 | unsigned tgt_long next_ref; |
| 236 | }; |
| 237 | |
| 238 | struct new_module |
| 239 | { |
| 240 | unsigned tgt_long size_of_struct; /* == sizeof(module) */ |
| 241 | unsigned tgt_long next; |
| 242 | unsigned tgt_long name; |
| 243 | unsigned tgt_long size; |
| 244 | |
| 245 | tgt_long usecount; |
| 246 | unsigned tgt_long flags; /* AUTOCLEAN et al */ |
| 247 | |
| 248 | unsigned nsyms; |
| 249 | unsigned ndeps; |
| 250 | |
| 251 | unsigned tgt_long syms; |
| 252 | unsigned tgt_long deps; |
| 253 | unsigned tgt_long refs; |
| 254 | unsigned tgt_long init; |
| 255 | unsigned tgt_long cleanup; |
| 256 | unsigned tgt_long ex_table_start; |
| 257 | unsigned tgt_long ex_table_end; |
| 258 | #ifdef __alpha__ |
| 259 | unsigned tgt_long gp; |
| 260 | #endif |
| 261 | /* Everything after here is extension. */ |
| 262 | unsigned tgt_long persist_start; |
| 263 | unsigned tgt_long persist_end; |
| 264 | unsigned tgt_long can_unload; |
| 265 | unsigned tgt_long runsize; |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 266 | #ifdef BB_FEATURE_NEW_MODULE_INTERFACE |
| 267 | const char *kallsyms_start; /* All symbols for kernel debugging */ |
| 268 | const char *kallsyms_end; |
| 269 | const char *archdata_start; /* arch specific data for module */ |
| 270 | const char *archdata_end; |
| 271 | const char *kernel_data; /* Reserved for kernel internal use */ |
| 272 | #endif |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 273 | }; |
| 274 | |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 275 | #define ARCHDATA_SEC_NAME "__archdata" |
| 276 | #define KALLSYMS_SEC_NAME "__kallsyms" |
| 277 | |
| 278 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 279 | struct new_module_info |
| 280 | { |
| 281 | unsigned long addr; |
| 282 | unsigned long size; |
| 283 | unsigned long flags; |
| 284 | long usecount; |
| 285 | }; |
| 286 | |
| 287 | /* Bits of module.flags. */ |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 288 | static const int NEW_MOD_RUNNING = 1; |
| 289 | static const int NEW_MOD_DELETED = 2; |
| 290 | static const int NEW_MOD_AUTOCLEAN = 4; |
| 291 | static const int NEW_MOD_VISITED = 8; |
| 292 | static const int NEW_MOD_USED_ONCE = 16; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 293 | |
Eric Andersen | 64c8b17 | 2001-04-05 07:33:10 +0000 | [diff] [blame] | 294 | int new_sys_init_module(const char *name, const struct new_module *); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 295 | int query_module(const char *name, int which, void *buf, size_t bufsize, |
| 296 | size_t *ret); |
| 297 | |
| 298 | /* Values for query_module's which. */ |
| 299 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 300 | static const int QM_MODULES = 1; |
| 301 | static const int QM_DEPS = 2; |
| 302 | static const int QM_REFS = 3; |
| 303 | static const int QM_SYMBOLS = 4; |
| 304 | static const int QM_INFO = 5; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 305 | |
| 306 | /*======================================================================*/ |
| 307 | /* The system calls unchanged between 2.0 and 2.1. */ |
| 308 | |
| 309 | unsigned long create_module(const char *, size_t); |
| 310 | int delete_module(const char *); |
| 311 | |
| 312 | |
| 313 | #endif /* module.h */ |
| 314 | |
| 315 | //---------------------------------------------------------------------------- |
| 316 | //--------end of modutils module.h |
| 317 | //---------------------------------------------------------------------------- |
| 318 | |
| 319 | |
| 320 | |
| 321 | //---------------------------------------------------------------------------- |
| 322 | //--------modutils obj.h, lines 253-462 |
| 323 | //---------------------------------------------------------------------------- |
| 324 | |
| 325 | /* Elf object file loading and relocation routines. |
| 326 | Copyright 1996, 1997 Linux International. |
| 327 | |
| 328 | Contributed by Richard Henderson <rth@tamu.edu> |
| 329 | |
| 330 | This file is part of the Linux modutils. |
| 331 | |
| 332 | This program is free software; you can redistribute it and/or modify it |
| 333 | under the terms of the GNU General Public License as published by the |
| 334 | Free Software Foundation; either version 2 of the License, or (at your |
| 335 | option) any later version. |
| 336 | |
| 337 | This program is distributed in the hope that it will be useful, but |
| 338 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 339 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 340 | General Public License for more details. |
| 341 | |
| 342 | You should have received a copy of the GNU General Public License |
| 343 | along with this program; if not, write to the Free Software Foundation, |
| 344 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 345 | |
| 346 | |
| 347 | #ifndef MODUTILS_OBJ_H |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 348 | static const int MODUTILS_OBJ_H = 1; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 349 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 350 | #ident "$Id: insmod.c,v 1.68 2001/07/17 01:12:36 andersen Exp $" |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 351 | |
| 352 | /* The relocatable object is manipulated using elfin types. */ |
| 353 | |
| 354 | #include <stdio.h> |
| 355 | #include <elf.h> |
| 356 | |
| 357 | |
| 358 | /* Machine-specific elf macros for i386 et al. */ |
| 359 | |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 360 | /* the SH changes have only been tested on the SH4 in =little endian= mode */ |
| 361 | /* I'm not sure about big endian, so let's warn: */ |
| 362 | |
| 363 | #if (defined(__SH4__) || defined(__SH3__)) && defined(__BIG_ENDIAN__) |
| 364 | #error insmod.c may require changes for use on big endian SH4/SH3 |
| 365 | #endif |
| 366 | |
| 367 | /* it may or may not work on the SH1/SH2... So let's error on those |
| 368 | also */ |
| 369 | #if (defined(__sh__) && (!(defined(__SH3__) || defined(__SH4__)))) |
| 370 | #error insmod.c may require changes for non-SH3/SH4 use |
| 371 | #endif |
| 372 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 373 | #define ELFCLASSM ELFCLASS32 |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 374 | |
Eric Andersen | b4a26e6 | 2001-06-19 15:00:52 +0000 | [diff] [blame] | 375 | #if (defined(__mc68000__)) |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 376 | #define ELFDATAM ELFDATA2MSB |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 377 | #endif |
| 378 | |
| 379 | |
| 380 | |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 381 | #if defined(__sh__) |
| 382 | |
| 383 | #define MATCH_MACHINE(x) (x == EM_SH) |
| 384 | #define SHT_RELM SHT_RELA |
| 385 | #define Elf32_RelM Elf32_Rela |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 386 | #define ELFDATAM ELFDATA2LSB |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 387 | |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 388 | #elif defined(__arm__) |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 389 | |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 390 | #define MATCH_MACHINE(x) (x == EM_ARM) |
| 391 | #define SHT_RELM SHT_REL |
| 392 | #define Elf32_RelM Elf32_Rel |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 393 | #define ELFDATAM ELFDATA2LSB |
| 394 | |
| 395 | #elif defined(__powerpc__) |
| 396 | |
| 397 | #define MATCH_MACHINE(x) (x == EM_PPC) |
| 398 | #define SHT_RELM SHT_RELA |
| 399 | #define Elf32_RelM Elf32_Rela |
Eric Andersen | 5a9d441 | 2001-05-24 14:16:28 +0000 | [diff] [blame] | 400 | #define ELFDATAM ELFDATA2MSB |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 401 | |
Eric Andersen | 2bf658d | 2001-02-24 20:01:53 +0000 | [diff] [blame] | 402 | #elif defined(__mips__) |
| 403 | |
Eric Andersen | 82bb8a2 | 2001-03-22 19:01:16 +0000 | [diff] [blame] | 404 | /* Account for ELF spec changes. */ |
| 405 | #ifndef EM_MIPS_RS3_LE |
| 406 | #ifdef EM_MIPS_RS4_BE |
| 407 | #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE |
| 408 | #else |
| 409 | #define EM_MIPS_RS3_LE 10 |
| 410 | #endif |
| 411 | #endif /* !EM_MIPS_RS3_LE */ |
| 412 | |
Eric Andersen | 2bf658d | 2001-02-24 20:01:53 +0000 | [diff] [blame] | 413 | #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE) |
| 414 | #define SHT_RELM SHT_REL |
| 415 | #define Elf32_RelM Elf32_Rel |
| 416 | #ifdef __MIPSEB__ |
| 417 | #define ELFDATAM ELFDATA2MSB |
| 418 | #endif |
| 419 | #ifdef __MIPSEL__ |
| 420 | #define ELFDATAM ELFDATA2LSB |
| 421 | #endif |
| 422 | |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 423 | #elif defined(__i386__) |
| 424 | |
| 425 | /* presumably we can use these for anything but the SH and ARM*/ |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 426 | /* this is the previous behavior, but it does result in |
| 427 | insmod.c being broken on anything except i386 */ |
Pavel Roskin | 43f3e61 | 2000-09-28 20:52:55 +0000 | [diff] [blame] | 428 | #ifndef EM_486 |
| 429 | #define MATCH_MACHINE(x) (x == EM_386) |
| 430 | #else |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 431 | #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486) |
Pavel Roskin | 43f3e61 | 2000-09-28 20:52:55 +0000 | [diff] [blame] | 432 | #endif |
| 433 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 434 | #define SHT_RELM SHT_REL |
| 435 | #define Elf32_RelM Elf32_Rel |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 436 | #define ELFDATAM ELFDATA2LSB |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 437 | |
Eric Andersen | b4a26e6 | 2001-06-19 15:00:52 +0000 | [diff] [blame] | 438 | #elif defined(__mc68000__) |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 439 | |
| 440 | #define MATCH_MACHINE(x) (x == EM_68K) |
| 441 | #define SHT_RELM SHT_RELA |
| 442 | #define Elf32_RelM Elf32_Rela |
| 443 | |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 444 | #else |
Eric Andersen | d5cad14 | 2001-01-26 02:23:57 +0000 | [diff] [blame] | 445 | #error Sorry, but insmod.c does not yet support this architecture... |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 446 | #endif |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 447 | |
| 448 | #ifndef ElfW |
| 449 | # if ELFCLASSM == ELFCLASS32 |
| 450 | # define ElfW(x) Elf32_ ## x |
| 451 | # define ELFW(x) ELF32_ ## x |
| 452 | # else |
| 453 | # define ElfW(x) Elf64_ ## x |
| 454 | # define ELFW(x) ELF64_ ## x |
| 455 | # endif |
| 456 | #endif |
| 457 | |
| 458 | /* For some reason this is missing from libc5. */ |
| 459 | #ifndef ELF32_ST_INFO |
| 460 | # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf)) |
| 461 | #endif |
| 462 | |
| 463 | #ifndef ELF64_ST_INFO |
| 464 | # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf)) |
| 465 | #endif |
| 466 | |
| 467 | struct obj_string_patch; |
| 468 | struct obj_symbol_patch; |
| 469 | |
| 470 | struct obj_section |
| 471 | { |
| 472 | ElfW(Shdr) header; |
| 473 | const char *name; |
| 474 | char *contents; |
| 475 | struct obj_section *load_next; |
| 476 | int idx; |
| 477 | }; |
| 478 | |
| 479 | struct obj_symbol |
| 480 | { |
| 481 | struct obj_symbol *next; /* hash table link */ |
| 482 | const char *name; |
| 483 | unsigned long value; |
| 484 | unsigned long size; |
| 485 | int secidx; /* the defining section index/module */ |
| 486 | int info; |
| 487 | int ksymidx; /* for export to the kernel symtab */ |
| 488 | int referenced; /* actually used in the link */ |
| 489 | }; |
| 490 | |
| 491 | /* Hardcode the hash table size. We shouldn't be needing so many |
| 492 | symbols that we begin to degrade performance, and we get a big win |
| 493 | by giving the compiler a constant divisor. */ |
| 494 | |
| 495 | #define HASH_BUCKETS 521 |
| 496 | |
| 497 | struct obj_file |
| 498 | { |
| 499 | ElfW(Ehdr) header; |
| 500 | ElfW(Addr) baseaddr; |
| 501 | struct obj_section **sections; |
| 502 | struct obj_section *load_order; |
| 503 | struct obj_section **load_order_search_start; |
| 504 | struct obj_string_patch *string_patches; |
| 505 | struct obj_symbol_patch *symbol_patches; |
| 506 | int (*symbol_cmp)(const char *, const char *); |
| 507 | unsigned long (*symbol_hash)(const char *); |
| 508 | unsigned long local_symtab_size; |
| 509 | struct obj_symbol **local_symtab; |
| 510 | struct obj_symbol *symtab[HASH_BUCKETS]; |
| 511 | }; |
| 512 | |
| 513 | enum obj_reloc |
| 514 | { |
| 515 | obj_reloc_ok, |
| 516 | obj_reloc_overflow, |
| 517 | obj_reloc_dangerous, |
| 518 | obj_reloc_unhandled |
| 519 | }; |
| 520 | |
| 521 | struct obj_string_patch |
| 522 | { |
| 523 | struct obj_string_patch *next; |
| 524 | int reloc_secidx; |
| 525 | ElfW(Addr) reloc_offset; |
| 526 | ElfW(Addr) string_offset; |
| 527 | }; |
| 528 | |
| 529 | struct obj_symbol_patch |
| 530 | { |
| 531 | struct obj_symbol_patch *next; |
| 532 | int reloc_secidx; |
| 533 | ElfW(Addr) reloc_offset; |
| 534 | struct obj_symbol *sym; |
| 535 | }; |
| 536 | |
| 537 | |
| 538 | /* Generic object manipulation routines. */ |
| 539 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 540 | static unsigned long obj_elf_hash(const char *); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 541 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 542 | static unsigned long obj_elf_hash_n(const char *, unsigned long len); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 543 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 544 | static struct obj_symbol *obj_find_symbol (struct obj_file *f, |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 545 | const char *name); |
| 546 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 547 | static ElfW(Addr) obj_symbol_final_value(struct obj_file *f, |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 548 | struct obj_symbol *sym); |
| 549 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 550 | static void obj_set_symbol_compare(struct obj_file *f, |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 551 | int (*cmp)(const char *, const char *), |
| 552 | unsigned long (*hash)(const char *)); |
| 553 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 554 | static struct obj_section *obj_find_section (struct obj_file *f, |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 555 | const char *name); |
| 556 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 557 | static void obj_insert_section_load_order (struct obj_file *f, |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 558 | struct obj_section *sec); |
| 559 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 560 | static struct obj_section *obj_create_alloced_section (struct obj_file *f, |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 561 | const char *name, |
| 562 | unsigned long align, |
| 563 | unsigned long size); |
| 564 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 565 | 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] | 566 | const char *name, |
| 567 | unsigned long align, |
| 568 | unsigned long size); |
| 569 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 570 | static void *obj_extend_section (struct obj_section *sec, unsigned long more); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 571 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 572 | 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] | 573 | const char *string); |
| 574 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 575 | 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] | 576 | struct obj_symbol *sym); |
| 577 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 578 | static int obj_check_undefineds(struct obj_file *f); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 579 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 580 | static void obj_allocate_commons(struct obj_file *f); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 581 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 582 | static unsigned long obj_load_size (struct obj_file *f); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 583 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 584 | static int obj_relocate (struct obj_file *f, ElfW(Addr) base); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 585 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 586 | static struct obj_file *obj_load(FILE *f, int loadprogbits); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 587 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 588 | static int obj_create_image (struct obj_file *f, char *image); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 589 | |
| 590 | /* Architecture specific manipulation routines. */ |
| 591 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 592 | static struct obj_file *arch_new_file (void); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 593 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 594 | static struct obj_section *arch_new_section (void); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 595 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 596 | static struct obj_symbol *arch_new_symbol (void); |
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 enum obj_reloc arch_apply_relocation (struct obj_file *f, |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 599 | struct obj_section *targsec, |
| 600 | struct obj_section *symsec, |
| 601 | struct obj_symbol *sym, |
| 602 | ElfW(RelM) *rel, ElfW(Addr) value); |
| 603 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 604 | static int arch_create_got (struct obj_file *f); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 605 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 606 | static int arch_init_module (struct obj_file *f, struct new_module *); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 607 | |
| 608 | #endif /* obj.h */ |
| 609 | //---------------------------------------------------------------------------- |
| 610 | //--------end of modutils obj.h |
| 611 | //---------------------------------------------------------------------------- |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
Erik Andersen | 0210432 | 1999-12-17 18:57:34 +0000 | [diff] [blame] | 616 | |
Erik Andersen | d387d01 | 1999-12-21 02:55:11 +0000 | [diff] [blame] | 617 | #define _PATH_MODULES "/lib/modules" |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 618 | static const int STRVERSIONLEN = 32; |
Erik Andersen | d387d01 | 1999-12-21 02:55:11 +0000 | [diff] [blame] | 619 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 620 | /*======================================================================*/ |
| 621 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 622 | static int flag_force_load = 0; |
| 623 | static int flag_autoclean = 0; |
| 624 | static int flag_verbose = 0; |
| 625 | static int flag_export = 1; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 626 | |
| 627 | |
| 628 | /*======================================================================*/ |
| 629 | |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 630 | /* previously, these were named i386_* but since we could be |
| 631 | compiling for the sh, I've renamed them to the more general |
| 632 | arch_* These structures are the same between the x86 and SH, |
| 633 | and we can't support anything else right now anyway. In the |
| 634 | future maybe they should be #if defined'd */ |
| 635 | |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 636 | /* Done ;-) */ |
| 637 | |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 638 | |
| 639 | |
| 640 | #if defined(BB_USE_PLT_ENTRIES) |
| 641 | struct arch_plt_entry |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 642 | { |
| 643 | int offset; |
| 644 | int allocated:1; |
| 645 | int inited:1; /* has been set up */ |
| 646 | }; |
| 647 | #endif |
| 648 | |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 649 | #if defined(BB_USE_GOT_ENTRIES) |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 650 | struct arch_got_entry { |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 651 | int offset; |
| 652 | unsigned offset_done:1; |
| 653 | unsigned reloc_done:1; |
| 654 | }; |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 655 | #endif |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 656 | |
Eric Andersen | 2bf658d | 2001-02-24 20:01:53 +0000 | [diff] [blame] | 657 | #if defined(__mips__) |
| 658 | struct mips_hi16 |
| 659 | { |
| 660 | struct mips_hi16 *next; |
| 661 | Elf32_Addr *addr; |
| 662 | Elf32_Addr value; |
| 663 | }; |
| 664 | #endif |
| 665 | |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 666 | struct arch_file { |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 667 | struct obj_file root; |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 668 | #if defined(BB_USE_PLT_ENTRIES) |
| 669 | struct obj_section *plt; |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 670 | #endif |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 671 | #if defined(BB_USE_GOT_ENTRIES) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 672 | struct obj_section *got; |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 673 | #endif |
Eric Andersen | 2bf658d | 2001-02-24 20:01:53 +0000 | [diff] [blame] | 674 | #if defined(__mips__) |
| 675 | struct mips_hi16 *mips_hi16_list; |
| 676 | #endif |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 677 | }; |
| 678 | |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 679 | struct arch_symbol { |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 680 | struct obj_symbol root; |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 681 | #if defined(BB_USE_PLT_ENTRIES) |
| 682 | struct arch_plt_entry pltent; |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 683 | #endif |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 684 | #if defined(BB_USE_GOT_ENTRIES) |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 685 | struct arch_got_entry gotent; |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 686 | #endif |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 687 | }; |
| 688 | |
| 689 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 690 | struct external_module { |
| 691 | const char *name; |
| 692 | ElfW(Addr) addr; |
| 693 | int used; |
| 694 | size_t nsyms; |
| 695 | struct new_module_symbol *syms; |
| 696 | }; |
| 697 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 698 | static struct new_module_symbol *ksyms; |
| 699 | static size_t nksyms; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 700 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 701 | static struct external_module *ext_modules; |
| 702 | static int n_ext_modules; |
| 703 | static int n_ext_modules_used; |
Erik Andersen | 0210432 | 1999-12-17 18:57:34 +0000 | [diff] [blame] | 704 | extern int delete_module(const char *); |
Eric Andersen | a2a978a | 2001-04-05 06:08:14 +0000 | [diff] [blame] | 705 | |
Eric Andersen | 14d3543 | 2001-05-14 17:07:32 +0000 | [diff] [blame] | 706 | static char m_filename[FILENAME_MAX + 1]; |
| 707 | static char m_fullName[FILENAME_MAX + 1]; |
Erik Andersen | 0210432 | 1999-12-17 18:57:34 +0000 | [diff] [blame] | 708 | |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 709 | |
Erik Andersen | 0210432 | 1999-12-17 18:57:34 +0000 | [diff] [blame] | 710 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 711 | /*======================================================================*/ |
Erik Andersen | 0210432 | 1999-12-17 18:57:34 +0000 | [diff] [blame] | 712 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 713 | |
Eric Andersen | 14d3543 | 2001-05-14 17:07:32 +0000 | [diff] [blame] | 714 | static int check_module_name_match(const char *filename, struct stat *statbuf, |
| 715 | void *userdata) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 716 | { |
Eric Andersen | 14d3543 | 2001-05-14 17:07:32 +0000 | [diff] [blame] | 717 | char *fullname = (char *) userdata; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 718 | |
Eric Andersen | 14d3543 | 2001-05-14 17:07:32 +0000 | [diff] [blame] | 719 | if (fullname[0] == '\0') |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 720 | return (FALSE); |
| 721 | else { |
Eric Andersen | 14d3543 | 2001-05-14 17:07:32 +0000 | [diff] [blame] | 722 | char *tmp, *tmp1 = strdup(filename); |
| 723 | tmp = get_last_path_component(tmp1); |
| 724 | if (strcmp(tmp, fullname) == 0) { |
| 725 | free(tmp1); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 726 | /* Stop searching if we find a match */ |
Eric Andersen | 14d3543 | 2001-05-14 17:07:32 +0000 | [diff] [blame] | 727 | safe_strncpy(m_filename, filename, sizeof(m_filename)); |
| 728 | return (TRUE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 729 | } |
Eric Andersen | 14d3543 | 2001-05-14 17:07:32 +0000 | [diff] [blame] | 730 | free(tmp1); |
Erik Andersen | d387d01 | 1999-12-21 02:55:11 +0000 | [diff] [blame] | 731 | } |
Eric Andersen | 14d3543 | 2001-05-14 17:07:32 +0000 | [diff] [blame] | 732 | return (FALSE); |
Erik Andersen | d387d01 | 1999-12-21 02:55:11 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Erik Andersen | 0210432 | 1999-12-17 18:57:34 +0000 | [diff] [blame] | 735 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 736 | /*======================================================================*/ |
| 737 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 738 | static struct obj_file *arch_new_file(void) |
Erik Andersen | 0210432 | 1999-12-17 18:57:34 +0000 | [diff] [blame] | 739 | { |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 740 | struct arch_file *f; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 741 | f = xmalloc(sizeof(*f)); |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 742 | |
| 743 | #if defined(BB_USE_PLT_ENTRIES) |
| 744 | f->plt = NULL; |
| 745 | #endif |
| 746 | #if defined(BB_USE_GOT_ENTRIES) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 747 | f->got = NULL; |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 748 | #endif |
Eric Andersen | 2bf658d | 2001-02-24 20:01:53 +0000 | [diff] [blame] | 749 | #if defined(__mips__) |
| 750 | f->mips_hi16_list = NULL; |
| 751 | #endif |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 752 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 753 | return &f->root; |
| 754 | } |
| 755 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 756 | static struct obj_section *arch_new_section(void) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 757 | { |
| 758 | return xmalloc(sizeof(struct obj_section)); |
| 759 | } |
| 760 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 761 | static struct obj_symbol *arch_new_symbol(void) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 762 | { |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 763 | struct arch_symbol *sym; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 764 | sym = xmalloc(sizeof(*sym)); |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 765 | |
| 766 | #if defined(BB_USE_PLT_ENTRIES) |
| 767 | memset(&sym->pltent, 0, sizeof(sym->pltent)); |
| 768 | #endif |
| 769 | #if defined(BB_USE_GOT_ENTRIES) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 770 | memset(&sym->gotent, 0, sizeof(sym->gotent)); |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 771 | #endif |
| 772 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 773 | return &sym->root; |
| 774 | } |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 775 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 776 | static enum obj_reloc |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 777 | arch_apply_relocation(struct obj_file *f, |
| 778 | struct obj_section *targsec, |
| 779 | struct obj_section *symsec, |
| 780 | struct obj_symbol *sym, |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 781 | ElfW(RelM) *rel, ElfW(Addr) v) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 782 | { |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 783 | struct arch_file *ifile = (struct arch_file *) f; |
Eric Andersen | 82bb8a2 | 2001-03-22 19:01:16 +0000 | [diff] [blame] | 784 | #if !(defined(__mips__)) |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 785 | struct arch_symbol *isym = (struct arch_symbol *) sym; |
Eric Andersen | 82bb8a2 | 2001-03-22 19:01:16 +0000 | [diff] [blame] | 786 | #endif |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 787 | |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 788 | ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset); |
| 789 | ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset; |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 790 | #if defined(BB_USE_GOT_ENTRIES) |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 791 | ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0; |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 792 | #endif |
| 793 | #if defined(BB_USE_PLT_ENTRIES) |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 794 | ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0; |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 795 | struct arch_plt_entry *pe; |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 796 | unsigned long *ip; |
| 797 | #endif |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 798 | enum obj_reloc ret = obj_reloc_ok; |
| 799 | |
| 800 | switch (ELF32_R_TYPE(rel->r_info)) { |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 801 | |
| 802 | /* even though these constants seem to be the same for |
| 803 | the i386 and the sh, we "#if define" them for clarity |
| 804 | and in case that ever changes */ |
| 805 | #if defined(__sh__) |
| 806 | case R_SH_NONE: |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 807 | #elif defined(__arm__) |
| 808 | case R_ARM_NONE: |
| 809 | #elif defined(__i386__) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 810 | case R_386_NONE: |
Eric Andersen | b4a26e6 | 2001-06-19 15:00:52 +0000 | [diff] [blame] | 811 | #elif defined(__mc68000__) |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 812 | case R_68K_NONE: |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 813 | #elif defined(__powerpc__) |
| 814 | case R_PPC_NONE: |
Eric Andersen | 2bf658d | 2001-02-24 20:01:53 +0000 | [diff] [blame] | 815 | #elif defined(__mips__) |
| 816 | case R_MIPS_NONE: |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 817 | #endif |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 818 | break; |
| 819 | |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 820 | #if defined(__sh__) |
| 821 | case R_SH_DIR32: |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 822 | #elif defined(__arm__) |
| 823 | case R_ARM_ABS32: |
| 824 | #elif defined(__i386__) |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 825 | case R_386_32: |
Eric Andersen | b4a26e6 | 2001-06-19 15:00:52 +0000 | [diff] [blame] | 826 | #elif defined(__mc68000__) |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 827 | case R_68K_32: |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 828 | #elif defined(__powerpc__) |
| 829 | case R_PPC_ADDR32: |
Eric Andersen | 2bf658d | 2001-02-24 20:01:53 +0000 | [diff] [blame] | 830 | #elif defined(__mips__) |
| 831 | case R_MIPS_32: |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 832 | #endif |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 833 | *loc += v; |
| 834 | break; |
Eric Andersen | b4a26e6 | 2001-06-19 15:00:52 +0000 | [diff] [blame] | 835 | #if defined(__mc68000__) |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 836 | case R_68K_8: |
| 837 | if (v > 0xff) |
| 838 | ret = obj_reloc_overflow; |
| 839 | *(char *)loc = v; |
| 840 | break; |
| 841 | case R_68K_16: |
| 842 | if (v > 0xffff) |
| 843 | ret = obj_reloc_overflow; |
| 844 | *(short *)loc = v; |
| 845 | break; |
Eric Andersen | b4a26e6 | 2001-06-19 15:00:52 +0000 | [diff] [blame] | 846 | #endif /* __mc68000__ */ |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 847 | |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 848 | #if defined(__powerpc__) |
| 849 | case R_PPC_ADDR16_HA: |
| 850 | *(unsigned short *)loc = (v + 0x8000) >> 16; |
| 851 | break; |
| 852 | |
| 853 | case R_PPC_ADDR16_HI: |
| 854 | *(unsigned short *)loc = v >> 16; |
| 855 | break; |
| 856 | |
| 857 | case R_PPC_ADDR16_LO: |
| 858 | *(unsigned short *)loc = v; |
| 859 | break; |
| 860 | #endif |
| 861 | |
Eric Andersen | 2bf658d | 2001-02-24 20:01:53 +0000 | [diff] [blame] | 862 | #if defined(__mips__) |
| 863 | case R_MIPS_26: |
| 864 | if (v % 4) |
| 865 | ret = obj_reloc_dangerous; |
| 866 | if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000)) |
| 867 | ret = obj_reloc_overflow; |
| 868 | *loc = |
| 869 | (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) & |
| 870 | 0x03ffffff); |
| 871 | break; |
| 872 | |
| 873 | case R_MIPS_HI16: |
| 874 | { |
| 875 | struct mips_hi16 *n; |
| 876 | |
| 877 | /* We cannot relocate this one now because we don't know the value |
| 878 | of the carry we need to add. Save the information, and let LO16 |
| 879 | do the actual relocation. */ |
| 880 | n = (struct mips_hi16 *) xmalloc(sizeof *n); |
| 881 | n->addr = loc; |
| 882 | n->value = v; |
| 883 | n->next = ifile->mips_hi16_list; |
| 884 | ifile->mips_hi16_list = n; |
| 885 | break; |
| 886 | } |
| 887 | |
| 888 | case R_MIPS_LO16: |
| 889 | { |
| 890 | unsigned long insnlo = *loc; |
| 891 | Elf32_Addr val, vallo; |
| 892 | |
| 893 | /* Sign extend the addend we extract from the lo insn. */ |
| 894 | vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000; |
| 895 | |
| 896 | if (ifile->mips_hi16_list != NULL) { |
| 897 | struct mips_hi16 *l; |
| 898 | |
| 899 | l = ifile->mips_hi16_list; |
| 900 | while (l != NULL) { |
| 901 | struct mips_hi16 *next; |
| 902 | unsigned long insn; |
| 903 | |
| 904 | /* The value for the HI16 had best be the same. */ |
| 905 | assert(v == l->value); |
| 906 | |
| 907 | /* Do the HI16 relocation. Note that we actually don't |
| 908 | need to know anything about the LO16 itself, except where |
| 909 | to find the low 16 bits of the addend needed by the LO16. */ |
| 910 | insn = *l->addr; |
| 911 | val = |
| 912 | ((insn & 0xffff) << 16) + |
| 913 | vallo; |
| 914 | val += v; |
| 915 | |
| 916 | /* Account for the sign extension that will happen in the |
| 917 | low bits. */ |
| 918 | val = |
| 919 | ((val >> 16) + |
| 920 | ((val & 0x8000) != |
| 921 | 0)) & 0xffff; |
| 922 | |
| 923 | insn = (insn & ~0xffff) | val; |
| 924 | *l->addr = insn; |
| 925 | |
| 926 | next = l->next; |
| 927 | free(l); |
| 928 | l = next; |
| 929 | } |
| 930 | |
| 931 | ifile->mips_hi16_list = NULL; |
| 932 | } |
| 933 | |
| 934 | /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */ |
| 935 | val = v + vallo; |
| 936 | insnlo = (insnlo & ~0xffff) | (val & 0xffff); |
| 937 | *loc = insnlo; |
| 938 | break; |
| 939 | } |
| 940 | #endif |
| 941 | |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 942 | #if defined(__arm__) |
| 943 | #elif defined(__sh__) |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 944 | case R_SH_REL32: |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 945 | *loc += v - dot; |
| 946 | break; |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 947 | #elif defined(__i386__) |
| 948 | case R_386_PLT32: |
| 949 | case R_386_PC32: |
| 950 | *loc += v - dot; |
| 951 | break; |
Eric Andersen | b4a26e6 | 2001-06-19 15:00:52 +0000 | [diff] [blame] | 952 | #elif defined(__mc68000__) |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 953 | case R_68K_PC8: |
| 954 | v -= dot; |
| 955 | if ((Elf32_Sword)v > 0x7f || (Elf32_Sword)v < -(Elf32_Sword)0x80) |
| 956 | ret = obj_reloc_overflow; |
| 957 | *(char *)loc = v; |
| 958 | break; |
| 959 | case R_68K_PC16: |
| 960 | v -= dot; |
| 961 | if ((Elf32_Sword)v > 0x7fff || (Elf32_Sword)v < -(Elf32_Sword)0x8000) |
| 962 | ret = obj_reloc_overflow; |
| 963 | *(short *)loc = v; |
| 964 | break; |
| 965 | case R_68K_PC32: |
| 966 | *(int *)loc = v - dot; |
| 967 | break; |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 968 | #elif defined(__powerpc__) |
| 969 | case R_PPC_REL32: |
| 970 | *loc = v - dot; |
| 971 | break; |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 972 | #endif |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 973 | |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 974 | #if defined(__sh__) |
| 975 | case R_SH_PLT32: |
| 976 | *loc = v - dot; |
| 977 | break; |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 978 | #elif defined(__i386__) |
| 979 | #endif |
| 980 | |
| 981 | #if defined(BB_USE_PLT_ENTRIES) |
| 982 | |
| 983 | #if defined(__arm__) |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 984 | case R_ARM_PC24: |
| 985 | case R_ARM_PLT32: |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 986 | #endif |
| 987 | #if defined(__powerpc__) |
| 988 | case R_PPC_REL24: |
| 989 | #endif |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 990 | /* find the plt entry and initialize it if necessary */ |
| 991 | assert(isym != NULL); |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 992 | |
| 993 | pe = (struct arch_plt_entry*) &isym->pltent; |
| 994 | |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 995 | if (! pe->inited) { |
| 996 | ip = (unsigned long *) (ifile->plt->contents + pe->offset); |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 997 | |
| 998 | /* generate some machine code */ |
| 999 | |
| 1000 | #if defined(__arm__) |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1001 | ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */ |
| 1002 | ip[1] = v; /* sym@ */ |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 1003 | #endif |
| 1004 | #if defined(__powerpc__) |
| 1005 | ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */ |
| 1006 | ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */ |
| 1007 | ip[2] = 0x7d6903a6; /* mtctr r11 */ |
| 1008 | ip[3] = 0x4e800420; /* bctr */ |
| 1009 | #endif |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1010 | pe->inited = 1; |
| 1011 | } |
| 1012 | |
| 1013 | /* relative distance to target */ |
| 1014 | v -= dot; |
| 1015 | /* if the target is too far away.... */ |
| 1016 | if ((int)v < -0x02000000 || (int)v >= 0x02000000) { |
| 1017 | /* go via the plt */ |
| 1018 | v = plt + pe->offset - dot; |
| 1019 | } |
| 1020 | if (v & 3) |
| 1021 | ret = obj_reloc_dangerous; |
| 1022 | |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 1023 | /* merge the offset into the instruction. */ |
| 1024 | #if defined(__arm__) |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1025 | /* Convert to words. */ |
| 1026 | v >>= 2; |
| 1027 | |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1028 | *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff); |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 1029 | #endif |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 1030 | #if defined(__powerpc__) |
| 1031 | *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc); |
| 1032 | #endif |
| 1033 | break; |
| 1034 | #endif /* BB_USE_PLT_ENTRIES */ |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 1035 | |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1036 | #if defined(__arm__) |
| 1037 | #elif defined(__sh__) |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 1038 | case R_SH_GLOB_DAT: |
| 1039 | case R_SH_JMP_SLOT: |
| 1040 | *loc = v; |
| 1041 | break; |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1042 | #elif defined(__i386__) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1043 | case R_386_GLOB_DAT: |
| 1044 | case R_386_JMP_SLOT: |
| 1045 | *loc = v; |
| 1046 | break; |
Eric Andersen | b4a26e6 | 2001-06-19 15:00:52 +0000 | [diff] [blame] | 1047 | #elif defined(__mc68000__) |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 1048 | case R_68K_GLOB_DAT: |
| 1049 | case R_68K_JMP_SLOT: |
| 1050 | *loc = v; |
| 1051 | break; |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 1052 | #endif |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1053 | |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1054 | #if defined(__arm__) |
| 1055 | #elif defined(__sh__) |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 1056 | case R_SH_RELATIVE: |
| 1057 | *loc += f->baseaddr + rel->r_addend; |
| 1058 | break; |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1059 | #elif defined(__i386__) |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 1060 | case R_386_RELATIVE: |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1061 | *loc += f->baseaddr; |
| 1062 | break; |
Eric Andersen | b4a26e6 | 2001-06-19 15:00:52 +0000 | [diff] [blame] | 1063 | #elif defined(__mc68000__) |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 1064 | case R_68K_RELATIVE: |
| 1065 | *(int *)loc += f->baseaddr; |
| 1066 | break; |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 1067 | #endif |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1068 | |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 1069 | #if defined(BB_USE_GOT_ENTRIES) |
| 1070 | |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 1071 | #if !defined(__68k__) |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 1072 | #if defined(__sh__) |
| 1073 | case R_SH_GOTPC: |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1074 | #elif defined(__arm__) |
| 1075 | case R_ARM_GOTPC: |
| 1076 | #elif defined(__i386__) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1077 | case R_386_GOTPC: |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 1078 | #endif |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1079 | assert(got != 0); |
| 1080 | #if defined(__sh__) |
| 1081 | *loc += got - dot + rel->r_addend;; |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 1082 | #elif defined(__i386__) || defined(__arm__) || defined(__m68k_) |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1083 | *loc += got - dot; |
| 1084 | #endif |
| 1085 | break; |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 1086 | #endif // __68k__ |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1087 | |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 1088 | #if defined(__sh__) |
| 1089 | case R_SH_GOT32: |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1090 | #elif defined(__arm__) |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 1091 | case R_ARM_GOT32: |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1092 | #elif defined(__i386__) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1093 | case R_386_GOT32: |
Eric Andersen | b4a26e6 | 2001-06-19 15:00:52 +0000 | [diff] [blame] | 1094 | #elif defined(__mc68000__) |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 1095 | case R_68K_GOT32: |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1096 | #endif |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1097 | assert(isym != NULL); |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1098 | /* needs an entry in the .got: set it, once */ |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1099 | if (!isym->gotent.reloc_done) { |
| 1100 | isym->gotent.reloc_done = 1; |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1101 | *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1102 | } |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1103 | /* make the reloc with_respect_to_.got */ |
| 1104 | #if defined(__sh__) |
| 1105 | *loc += isym->gotent.offset + rel->r_addend; |
Eric Andersen | b4a26e6 | 2001-06-19 15:00:52 +0000 | [diff] [blame] | 1106 | #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1107 | *loc += isym->gotent.offset; |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 1108 | #endif |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1109 | break; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1110 | |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1111 | /* address relative to the got */ |
Eric Andersen | b4a26e6 | 2001-06-19 15:00:52 +0000 | [diff] [blame] | 1112 | #if !defined(__mc68000__) |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 1113 | #if defined(__sh__) |
| 1114 | case R_SH_GOTOFF: |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1115 | #elif defined(__arm__) |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 1116 | case R_ARM_GOTOFF: |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1117 | #elif defined(__i386__) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1118 | case R_386_GOTOFF: |
Eric Andersen | b4a26e6 | 2001-06-19 15:00:52 +0000 | [diff] [blame] | 1119 | #elif defined(__mc68000__) |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 1120 | case R_68K_GOTOFF: |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 1121 | #endif |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1122 | assert(got != 0); |
| 1123 | *loc += v - got; |
| 1124 | break; |
Eric Andersen | b4a26e6 | 2001-06-19 15:00:52 +0000 | [diff] [blame] | 1125 | #endif // __mc68000__ |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1126 | |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 1127 | #endif /* BB_USE_GOT_ENTRIES */ |
| 1128 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1129 | default: |
Eric Andersen | 2112554 | 2000-12-13 16:41:29 +0000 | [diff] [blame] | 1130 | printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info)); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1131 | ret = obj_reloc_unhandled; |
| 1132 | break; |
| 1133 | } |
| 1134 | |
| 1135 | return ret; |
| 1136 | } |
| 1137 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 1138 | static int arch_create_got(struct obj_file *f) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1139 | { |
Eric Andersen | 2bf658d | 2001-02-24 20:01:53 +0000 | [diff] [blame] | 1140 | #if defined(BB_USE_GOT_ENTRIES) || defined(BB_USE_PLT_ENTRIES) |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 1141 | struct arch_file *ifile = (struct arch_file *) f; |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 1142 | int i; |
| 1143 | #if defined(BB_USE_GOT_ENTRIES) |
| 1144 | int got_offset = 0, gotneeded = 0; |
| 1145 | #endif |
| 1146 | #if defined(BB_USE_PLT_ENTRIES) |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1147 | int plt_offset = 0, pltneeded = 0; |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 1148 | #endif |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1149 | struct obj_section *relsec, *symsec, *strsec; |
| 1150 | ElfW(RelM) *rel, *relend; |
| 1151 | ElfW(Sym) *symtab, *extsym; |
| 1152 | const char *strtab, *name; |
| 1153 | struct arch_symbol *intsym; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1154 | |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1155 | for (i = 0; i < f->header.e_shnum; ++i) { |
| 1156 | relsec = f->sections[i]; |
| 1157 | if (relsec->header.sh_type != SHT_RELM) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1158 | continue; |
| 1159 | |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1160 | symsec = f->sections[relsec->header.sh_link]; |
| 1161 | strsec = f->sections[symsec->header.sh_link]; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1162 | |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1163 | rel = (ElfW(RelM) *) relsec->contents; |
| 1164 | relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM))); |
| 1165 | symtab = (ElfW(Sym) *) symsec->contents; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1166 | strtab = (const char *) strsec->contents; |
| 1167 | |
| 1168 | for (; rel < relend; ++rel) { |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1169 | extsym = &symtab[ELF32_R_SYM(rel->r_info)]; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1170 | |
| 1171 | switch (ELF32_R_TYPE(rel->r_info)) { |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1172 | #if defined(__arm__) |
| 1173 | case R_ARM_GOT32: |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 1174 | break; |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1175 | #elif defined(__sh__) |
Eric Andersen | fe4208f | 2000-09-24 03:44:29 +0000 | [diff] [blame] | 1176 | case R_SH_GOT32: |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 1177 | break; |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1178 | #elif defined(__i386__) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1179 | case R_386_GOT32: |
| 1180 | break; |
Eric Andersen | b4a26e6 | 2001-06-19 15:00:52 +0000 | [diff] [blame] | 1181 | #elif defined(__mc68000__) |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 1182 | case R_68K_GOT32: |
| 1183 | break; |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 1184 | #endif |
| 1185 | |
| 1186 | #if defined(__powerpc__) |
| 1187 | case R_PPC_REL24: |
| 1188 | pltneeded = 1; |
| 1189 | break; |
| 1190 | #endif |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1191 | |
| 1192 | #if defined(__arm__) |
| 1193 | case R_ARM_PC24: |
| 1194 | case R_ARM_PLT32: |
| 1195 | pltneeded = 1; |
| 1196 | break; |
| 1197 | |
| 1198 | case R_ARM_GOTPC: |
| 1199 | case R_ARM_GOTOFF: |
| 1200 | gotneeded = 1; |
| 1201 | if (got_offset == 0) |
| 1202 | got_offset = 4; |
| 1203 | #elif defined(__sh__) |
| 1204 | case R_SH_GOTPC: |
| 1205 | case R_SH_GOTOFF: |
| 1206 | gotneeded = 1; |
| 1207 | #elif defined(__i386__) |
| 1208 | case R_386_GOTPC: |
| 1209 | case R_386_GOTOFF: |
| 1210 | gotneeded = 1; |
| 1211 | #endif |
| 1212 | |
| 1213 | default: |
| 1214 | continue; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1217 | if (extsym->st_name != 0) { |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1218 | name = strtab + extsym->st_name; |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1219 | } else { |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1220 | name = f->sections[extsym->st_shndx]->name; |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1221 | } |
| 1222 | intsym = (struct arch_symbol *) obj_find_symbol(f, name); |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 1223 | #if defined(BB_USE_GOT_ENTRIES) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1224 | if (!intsym->gotent.offset_done) { |
| 1225 | intsym->gotent.offset_done = 1; |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1226 | intsym->gotent.offset = got_offset; |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 1227 | got_offset += BB_GOT_ENTRY_SIZE; |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1228 | } |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 1229 | #endif |
| 1230 | #if defined(BB_USE_PLT_ENTRIES) |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1231 | if (pltneeded && intsym->pltent.allocated == 0) { |
| 1232 | intsym->pltent.allocated = 1; |
| 1233 | intsym->pltent.offset = plt_offset; |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 1234 | plt_offset += BB_PLT_ENTRY_SIZE; |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1235 | intsym->pltent.inited = 0; |
| 1236 | pltneeded = 0; |
| 1237 | } |
| 1238 | #endif |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1239 | } |
| 1240 | } |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1241 | |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 1242 | #if defined(BB_USE_GOT_ENTRIES) |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1243 | if (got_offset) { |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 1244 | struct obj_section* myrelsec = obj_find_section(f, ".got"); |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1245 | |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 1246 | if (myrelsec) { |
| 1247 | obj_extend_section(myrelsec, got_offset); |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1248 | } else { |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 1249 | myrelsec = obj_create_alloced_section(f, ".got", |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 1250 | BB_GOT_ENTRY_SIZE, |
| 1251 | got_offset); |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 1252 | assert(myrelsec); |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 1255 | ifile->got = myrelsec; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1256 | } |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 1257 | #endif |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1258 | |
Eric Andersen | 90fe7fe | 2001-02-20 20:47:08 +0000 | [diff] [blame] | 1259 | #if defined(BB_USE_PLT_ENTRIES) |
| 1260 | if (plt_offset) |
| 1261 | ifile->plt = obj_create_alloced_section(f, ".plt", |
| 1262 | BB_PLT_ENTRY_SIZE, |
| 1263 | plt_offset); |
| 1264 | #endif |
Eric Andersen | 2bf658d | 2001-02-24 20:01:53 +0000 | [diff] [blame] | 1265 | #endif |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1266 | return 1; |
| 1267 | } |
| 1268 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 1269 | static int arch_init_module(struct obj_file *f, struct new_module *mod) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1270 | { |
| 1271 | return 1; |
| 1272 | } |
| 1273 | |
| 1274 | |
| 1275 | /*======================================================================*/ |
| 1276 | |
| 1277 | /* Standard ELF hash function. */ |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 1278 | 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] | 1279 | { |
| 1280 | unsigned long h = 0; |
| 1281 | unsigned long g; |
| 1282 | unsigned char ch; |
| 1283 | |
| 1284 | while (n > 0) { |
| 1285 | ch = *name++; |
| 1286 | h = (h << 4) + ch; |
| 1287 | if ((g = (h & 0xf0000000)) != 0) { |
| 1288 | h ^= g >> 24; |
| 1289 | h &= ~g; |
| 1290 | } |
| 1291 | n--; |
| 1292 | } |
| 1293 | return h; |
| 1294 | } |
| 1295 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 1296 | static unsigned long obj_elf_hash(const char *name) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1297 | { |
| 1298 | return obj_elf_hash_n(name, strlen(name)); |
| 1299 | } |
| 1300 | |
| 1301 | #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING |
| 1302 | /* Get the kernel version in the canonical integer form. */ |
| 1303 | |
| 1304 | static int get_kernel_version(char str[STRVERSIONLEN]) |
| 1305 | { |
| 1306 | struct utsname uts_info; |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 1307 | int kv; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1308 | |
| 1309 | if (uname(&uts_info) < 0) |
| 1310 | return -1; |
| 1311 | strncpy(str, uts_info.release, STRVERSIONLEN); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1312 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 1313 | kv = get_kernel_revision(); |
| 1314 | if(kv==0) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1315 | return -1; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | /* String comparison for non-co-versioned kernel and module. */ |
| 1319 | |
| 1320 | static int ncv_strcmp(const char *a, const char *b) |
| 1321 | { |
| 1322 | size_t alen = strlen(a), blen = strlen(b); |
| 1323 | |
| 1324 | if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R') |
| 1325 | return strncmp(a, b, alen); |
| 1326 | else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R') |
| 1327 | return strncmp(a, b, blen); |
| 1328 | else |
| 1329 | return strcmp(a, b); |
| 1330 | } |
| 1331 | |
| 1332 | /* String hashing for non-co-versioned kernel and module. Here |
| 1333 | we are simply forced to drop the crc from the hash. */ |
| 1334 | |
| 1335 | static unsigned long ncv_symbol_hash(const char *str) |
| 1336 | { |
| 1337 | size_t len = strlen(str); |
| 1338 | if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R') |
| 1339 | len -= 10; |
| 1340 | return obj_elf_hash_n(str, len); |
| 1341 | } |
| 1342 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 1343 | static void |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1344 | obj_set_symbol_compare(struct obj_file *f, |
| 1345 | int (*cmp) (const char *, const char *), |
| 1346 | unsigned long (*hash) (const char *)) |
| 1347 | { |
| 1348 | if (cmp) |
| 1349 | f->symbol_cmp = cmp; |
| 1350 | if (hash) { |
| 1351 | struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next; |
| 1352 | int i; |
| 1353 | |
| 1354 | f->symbol_hash = hash; |
| 1355 | |
| 1356 | memcpy(tmptab, f->symtab, sizeof(tmptab)); |
| 1357 | memset(f->symtab, 0, sizeof(f->symtab)); |
| 1358 | |
| 1359 | for (i = 0; i < HASH_BUCKETS; ++i) |
| 1360 | for (sym = tmptab[i]; sym; sym = next) { |
| 1361 | unsigned long h = hash(sym->name) % HASH_BUCKETS; |
| 1362 | next = sym->next; |
| 1363 | sym->next = f->symtab[h]; |
| 1364 | f->symtab[h] = sym; |
| 1365 | } |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */ |
| 1370 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 1371 | static struct obj_symbol * |
| 1372 | obj_add_symbol(struct obj_file *f, const char *name, |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1373 | unsigned long symidx, int info, |
| 1374 | int secidx, ElfW(Addr) value, |
| 1375 | unsigned long size) |
| 1376 | { |
| 1377 | struct obj_symbol *sym; |
| 1378 | unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS; |
| 1379 | int n_type = ELFW(ST_TYPE) (info); |
| 1380 | int n_binding = ELFW(ST_BIND) (info); |
| 1381 | |
| 1382 | for (sym = f->symtab[hash]; sym; sym = sym->next) |
| 1383 | if (f->symbol_cmp(sym->name, name) == 0) { |
| 1384 | int o_secidx = sym->secidx; |
| 1385 | int o_info = sym->info; |
| 1386 | int o_type = ELFW(ST_TYPE) (o_info); |
| 1387 | int o_binding = ELFW(ST_BIND) (o_info); |
| 1388 | |
| 1389 | /* A redefinition! Is it legal? */ |
| 1390 | |
| 1391 | if (secidx == SHN_UNDEF) |
| 1392 | return sym; |
| 1393 | else if (o_secidx == SHN_UNDEF) |
| 1394 | goto found; |
| 1395 | else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) { |
| 1396 | /* Cope with local and global symbols of the same name |
| 1397 | in the same object file, as might have been created |
| 1398 | by ld -r. The only reason locals are now seen at this |
| 1399 | level at all is so that we can do semi-sensible things |
| 1400 | with parameters. */ |
| 1401 | |
| 1402 | struct obj_symbol *nsym, **p; |
| 1403 | |
| 1404 | nsym = arch_new_symbol(); |
| 1405 | nsym->next = sym->next; |
| 1406 | nsym->ksymidx = -1; |
| 1407 | |
| 1408 | /* Excise the old (local) symbol from the hash chain. */ |
| 1409 | for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next) |
| 1410 | continue; |
| 1411 | *p = sym = nsym; |
| 1412 | goto found; |
| 1413 | } else if (n_binding == STB_LOCAL) { |
| 1414 | /* Another symbol of the same name has already been defined. |
| 1415 | Just add this to the local table. */ |
| 1416 | sym = arch_new_symbol(); |
| 1417 | sym->next = NULL; |
| 1418 | sym->ksymidx = -1; |
| 1419 | f->local_symtab[symidx] = sym; |
| 1420 | goto found; |
| 1421 | } else if (n_binding == STB_WEAK) |
| 1422 | return sym; |
| 1423 | else if (o_binding == STB_WEAK) |
| 1424 | goto found; |
| 1425 | /* Don't unify COMMON symbols with object types the programmer |
| 1426 | doesn't expect. */ |
| 1427 | else if (secidx == SHN_COMMON |
| 1428 | && (o_type == STT_NOTYPE || o_type == STT_OBJECT)) |
| 1429 | return sym; |
| 1430 | else if (o_secidx == SHN_COMMON |
| 1431 | && (n_type == STT_NOTYPE || n_type == STT_OBJECT)) |
| 1432 | goto found; |
| 1433 | else { |
| 1434 | /* Don't report an error if the symbol is coming from |
| 1435 | the kernel or some external module. */ |
| 1436 | if (secidx <= SHN_HIRESERVE) |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 1437 | error_msg("%s multiply defined", name); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1438 | return sym; |
| 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | /* Completely new symbol. */ |
| 1443 | sym = arch_new_symbol(); |
| 1444 | sym->next = f->symtab[hash]; |
| 1445 | f->symtab[hash] = sym; |
| 1446 | sym->ksymidx = -1; |
| 1447 | |
Eric Andersen | 66ca948 | 2001-06-28 21:36:06 +0000 | [diff] [blame] | 1448 | if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) { |
| 1449 | if (symidx >= f->local_symtab_size) |
| 1450 | error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld", |
| 1451 | name, (long) symidx, (long) f->local_symtab_size); |
| 1452 | else |
| 1453 | f->local_symtab[symidx] = sym; |
| 1454 | } |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1455 | |
| 1456 | found: |
| 1457 | sym->name = name; |
| 1458 | sym->value = value; |
| 1459 | sym->size = size; |
| 1460 | sym->secidx = secidx; |
| 1461 | sym->info = info; |
| 1462 | |
| 1463 | return sym; |
| 1464 | } |
| 1465 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 1466 | static struct obj_symbol * |
| 1467 | obj_find_symbol(struct obj_file *f, const char *name) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1468 | { |
| 1469 | struct obj_symbol *sym; |
| 1470 | unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS; |
| 1471 | |
| 1472 | for (sym = f->symtab[hash]; sym; sym = sym->next) |
| 1473 | if (f->symbol_cmp(sym->name, name) == 0) |
| 1474 | return sym; |
| 1475 | |
| 1476 | return NULL; |
| 1477 | } |
| 1478 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 1479 | static ElfW(Addr) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1480 | obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym) |
| 1481 | { |
| 1482 | if (sym) { |
| 1483 | if (sym->secidx >= SHN_LORESERVE) |
| 1484 | return sym->value; |
| 1485 | |
| 1486 | return sym->value + f->sections[sym->secidx]->header.sh_addr; |
| 1487 | } else { |
| 1488 | /* As a special case, a NULL sym has value zero. */ |
| 1489 | return 0; |
| 1490 | } |
| 1491 | } |
| 1492 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 1493 | 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] | 1494 | { |
| 1495 | int i, n = f->header.e_shnum; |
| 1496 | |
| 1497 | for (i = 0; i < n; ++i) |
| 1498 | if (strcmp(f->sections[i]->name, name) == 0) |
| 1499 | return f->sections[i]; |
| 1500 | |
| 1501 | return NULL; |
| 1502 | } |
| 1503 | |
| 1504 | static int obj_load_order_prio(struct obj_section *a) |
| 1505 | { |
| 1506 | unsigned long af, ac; |
| 1507 | |
| 1508 | af = a->header.sh_flags; |
| 1509 | |
| 1510 | ac = 0; |
| 1511 | if (a->name[0] != '.' || strlen(a->name) != 10 || |
| 1512 | strcmp(a->name + 5, ".init")) |
| 1513 | ac |= 32; |
| 1514 | if (af & SHF_ALLOC) |
| 1515 | ac |= 16; |
| 1516 | if (!(af & SHF_WRITE)) |
| 1517 | ac |= 8; |
| 1518 | if (af & SHF_EXECINSTR) |
| 1519 | ac |= 4; |
| 1520 | if (a->header.sh_type != SHT_NOBITS) |
| 1521 | ac |= 2; |
| 1522 | |
| 1523 | return ac; |
| 1524 | } |
| 1525 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 1526 | static void |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1527 | obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec) |
| 1528 | { |
| 1529 | struct obj_section **p; |
| 1530 | int prio = obj_load_order_prio(sec); |
| 1531 | for (p = f->load_order_search_start; *p; p = &(*p)->load_next) |
| 1532 | if (obj_load_order_prio(*p) < prio) |
| 1533 | break; |
| 1534 | sec->load_next = *p; |
| 1535 | *p = sec; |
| 1536 | } |
| 1537 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 1538 | static struct obj_section *obj_create_alloced_section(struct obj_file *f, |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1539 | const char *name, |
| 1540 | unsigned long align, |
| 1541 | unsigned long size) |
| 1542 | { |
| 1543 | int newidx = f->header.e_shnum++; |
| 1544 | struct obj_section *sec; |
| 1545 | |
| 1546 | f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec)); |
| 1547 | f->sections[newidx] = sec = arch_new_section(); |
| 1548 | |
| 1549 | memset(sec, 0, sizeof(*sec)); |
| 1550 | sec->header.sh_type = SHT_PROGBITS; |
| 1551 | sec->header.sh_flags = SHF_WRITE | SHF_ALLOC; |
| 1552 | sec->header.sh_size = size; |
| 1553 | sec->header.sh_addralign = align; |
| 1554 | sec->name = name; |
| 1555 | sec->idx = newidx; |
| 1556 | if (size) |
| 1557 | sec->contents = xmalloc(size); |
| 1558 | |
| 1559 | obj_insert_section_load_order(f, sec); |
| 1560 | |
| 1561 | return sec; |
| 1562 | } |
| 1563 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 1564 | 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] | 1565 | const char *name, |
| 1566 | unsigned long align, |
| 1567 | unsigned long size) |
| 1568 | { |
| 1569 | int newidx = f->header.e_shnum++; |
| 1570 | struct obj_section *sec; |
| 1571 | |
| 1572 | f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec)); |
| 1573 | f->sections[newidx] = sec = arch_new_section(); |
| 1574 | |
| 1575 | memset(sec, 0, sizeof(*sec)); |
| 1576 | sec->header.sh_type = SHT_PROGBITS; |
| 1577 | sec->header.sh_flags = SHF_WRITE | SHF_ALLOC; |
| 1578 | sec->header.sh_size = size; |
| 1579 | sec->header.sh_addralign = align; |
| 1580 | sec->name = name; |
| 1581 | sec->idx = newidx; |
| 1582 | if (size) |
| 1583 | sec->contents = xmalloc(size); |
| 1584 | |
| 1585 | sec->load_next = f->load_order; |
| 1586 | f->load_order = sec; |
| 1587 | if (f->load_order_search_start == &f->load_order) |
| 1588 | f->load_order_search_start = &sec->load_next; |
| 1589 | |
| 1590 | return sec; |
| 1591 | } |
| 1592 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 1593 | static void *obj_extend_section(struct obj_section *sec, unsigned long more) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1594 | { |
| 1595 | unsigned long oldsize = sec->header.sh_size; |
Eric Andersen | 7f3b86e | 2001-04-26 19:29:58 +0000 | [diff] [blame] | 1596 | if (more) { |
| 1597 | sec->contents = xrealloc(sec->contents, sec->header.sh_size += more); |
| 1598 | } |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1599 | return sec->contents + oldsize; |
| 1600 | } |
| 1601 | |
| 1602 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1603 | /* Conditionally add the symbols from the given symbol set to the |
| 1604 | new module. */ |
| 1605 | |
| 1606 | static int |
| 1607 | add_symbols_from( |
| 1608 | struct obj_file *f, |
| 1609 | int idx, struct new_module_symbol *syms, size_t nsyms) |
| 1610 | { |
| 1611 | struct new_module_symbol *s; |
| 1612 | size_t i; |
| 1613 | int used = 0; |
| 1614 | |
| 1615 | for (i = 0, s = syms; i < nsyms; ++i, ++s) { |
| 1616 | |
| 1617 | /* Only add symbols that are already marked external. If we |
| 1618 | override locals we may cause problems for argument initialization. |
| 1619 | We will also create a false dependency on the module. */ |
| 1620 | struct obj_symbol *sym; |
| 1621 | |
| 1622 | sym = obj_find_symbol(f, (char *) s->name); |
| 1623 | if (sym && !ELFW(ST_BIND) (sym->info) == STB_LOCAL) { |
| 1624 | sym = obj_add_symbol(f, (char *) s->name, -1, |
| 1625 | ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE), |
| 1626 | idx, s->value, 0); |
| 1627 | /* Did our symbol just get installed? If so, mark the |
| 1628 | module as "used". */ |
| 1629 | if (sym->secidx == idx) |
| 1630 | used = 1; |
| 1631 | } |
| 1632 | } |
| 1633 | |
| 1634 | return used; |
| 1635 | } |
| 1636 | |
| 1637 | static void add_kernel_symbols(struct obj_file *f) |
| 1638 | { |
| 1639 | struct external_module *m; |
Pavel Roskin | ff5a903 | 2000-07-14 16:23:32 +0000 | [diff] [blame] | 1640 | int i, nused = 0; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1641 | |
| 1642 | /* Add module symbols first. */ |
| 1643 | |
| 1644 | for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m) |
| 1645 | if (m->nsyms |
| 1646 | && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms, |
| 1647 | m->nsyms)) m->used = 1, ++nused; |
| 1648 | |
| 1649 | n_ext_modules_used = nused; |
| 1650 | |
| 1651 | /* And finally the symbols from the kernel proper. */ |
| 1652 | |
| 1653 | if (nksyms) |
| 1654 | add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms); |
| 1655 | } |
| 1656 | |
| 1657 | static char *get_modinfo_value(struct obj_file *f, const char *key) |
| 1658 | { |
| 1659 | struct obj_section *sec; |
| 1660 | char *p, *v, *n, *ep; |
| 1661 | size_t klen = strlen(key); |
| 1662 | |
| 1663 | sec = obj_find_section(f, ".modinfo"); |
| 1664 | if (sec == NULL) |
| 1665 | return NULL; |
| 1666 | p = sec->contents; |
| 1667 | ep = p + sec->header.sh_size; |
| 1668 | while (p < ep) { |
| 1669 | v = strchr(p, '='); |
| 1670 | n = strchr(p, '\0'); |
| 1671 | if (v) { |
Pavel Roskin | ff5a903 | 2000-07-14 16:23:32 +0000 | [diff] [blame] | 1672 | if (p + klen == v && strncmp(p, key, klen) == 0) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1673 | return v + 1; |
| 1674 | } else { |
Pavel Roskin | ff5a903 | 2000-07-14 16:23:32 +0000 | [diff] [blame] | 1675 | if (p + klen == n && strcmp(p, key) == 0) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1676 | return n; |
| 1677 | } |
| 1678 | p = n + 1; |
| 1679 | } |
| 1680 | |
| 1681 | return NULL; |
| 1682 | } |
| 1683 | |
| 1684 | |
| 1685 | /*======================================================================*/ |
| 1686 | /* Functions relating to module loading in pre 2.1 kernels. */ |
| 1687 | |
| 1688 | static int |
| 1689 | old_process_module_arguments(struct obj_file *f, int argc, char **argv) |
| 1690 | { |
| 1691 | while (argc > 0) { |
| 1692 | char *p, *q; |
| 1693 | struct obj_symbol *sym; |
| 1694 | int *loc; |
| 1695 | |
| 1696 | p = *argv; |
Eric Andersen | ef40aa8 | 2000-06-26 11:16:22 +0000 | [diff] [blame] | 1697 | if ((q = strchr(p, '=')) == NULL) { |
| 1698 | argc--; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1699 | continue; |
Eric Andersen | ef40aa8 | 2000-06-26 11:16:22 +0000 | [diff] [blame] | 1700 | } |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1701 | *q++ = '\0'; |
| 1702 | |
| 1703 | sym = obj_find_symbol(f, p); |
| 1704 | |
| 1705 | /* Also check that the parameter was not resolved from the kernel. */ |
| 1706 | if (sym == NULL || sym->secidx > SHN_HIRESERVE) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 1707 | error_msg("symbol for parameter %s not found", p); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1708 | return 0; |
| 1709 | } |
| 1710 | |
| 1711 | loc = (int *) (f->sections[sym->secidx]->contents + sym->value); |
| 1712 | |
| 1713 | /* Do C quoting if we begin with a ". */ |
| 1714 | if (*q == '"') { |
| 1715 | char *r, *str; |
| 1716 | |
| 1717 | str = alloca(strlen(q)); |
| 1718 | for (r = str, q++; *q != '"'; ++q, ++r) { |
| 1719 | if (*q == '\0') { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 1720 | error_msg("improperly terminated string argument for %s", p); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1721 | return 0; |
| 1722 | } else if (*q == '\\') |
| 1723 | switch (*++q) { |
| 1724 | case 'a': |
| 1725 | *r = '\a'; |
| 1726 | break; |
| 1727 | case 'b': |
| 1728 | *r = '\b'; |
| 1729 | break; |
| 1730 | case 'e': |
| 1731 | *r = '\033'; |
| 1732 | break; |
| 1733 | case 'f': |
| 1734 | *r = '\f'; |
| 1735 | break; |
| 1736 | case 'n': |
| 1737 | *r = '\n'; |
| 1738 | break; |
| 1739 | case 'r': |
| 1740 | *r = '\r'; |
| 1741 | break; |
| 1742 | case 't': |
| 1743 | *r = '\t'; |
| 1744 | break; |
| 1745 | |
| 1746 | case '0': |
| 1747 | case '1': |
| 1748 | case '2': |
| 1749 | case '3': |
| 1750 | case '4': |
| 1751 | case '5': |
| 1752 | case '6': |
| 1753 | case '7': |
| 1754 | { |
| 1755 | int c = *q - '0'; |
| 1756 | if (q[1] >= '0' && q[1] <= '7') { |
| 1757 | c = (c * 8) + *++q - '0'; |
| 1758 | if (q[1] >= '0' && q[1] <= '7') |
| 1759 | c = (c * 8) + *++q - '0'; |
| 1760 | } |
| 1761 | *r = c; |
| 1762 | } |
| 1763 | break; |
| 1764 | |
| 1765 | default: |
| 1766 | *r = *q; |
| 1767 | break; |
| 1768 | } else |
| 1769 | *r = *q; |
| 1770 | } |
| 1771 | *r = '\0'; |
| 1772 | obj_string_patch(f, sym->secidx, sym->value, str); |
| 1773 | } else if (*q >= '0' && *q <= '9') { |
| 1774 | do |
| 1775 | *loc++ = strtoul(q, &q, 0); |
| 1776 | while (*q++ == ','); |
| 1777 | } else { |
| 1778 | char *contents = f->sections[sym->secidx]->contents; |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 1779 | char *myloc = contents + sym->value; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1780 | char *r; /* To search for commas */ |
| 1781 | |
| 1782 | /* Break the string with comas */ |
| 1783 | while ((r = strchr(q, ',')) != (char *) NULL) { |
| 1784 | *r++ = '\0'; |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 1785 | obj_string_patch(f, sym->secidx, myloc - contents, q); |
| 1786 | myloc += sizeof(char *); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1787 | q = r; |
| 1788 | } |
| 1789 | |
| 1790 | /* last part */ |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 1791 | obj_string_patch(f, sym->secidx, myloc - contents, q); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | argc--, argv++; |
| 1795 | } |
| 1796 | |
| 1797 | return 1; |
| 1798 | } |
| 1799 | |
| 1800 | #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING |
| 1801 | static int old_is_module_checksummed(struct obj_file *f) |
| 1802 | { |
| 1803 | return obj_find_symbol(f, "Using_Versions") != NULL; |
| 1804 | } |
| 1805 | /* Get the module's kernel version in the canonical integer form. */ |
| 1806 | |
| 1807 | static int |
| 1808 | old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN]) |
| 1809 | { |
| 1810 | struct obj_symbol *sym; |
| 1811 | char *p, *q; |
| 1812 | int a, b, c; |
| 1813 | |
| 1814 | sym = obj_find_symbol(f, "kernel_version"); |
| 1815 | if (sym == NULL) |
| 1816 | return -1; |
| 1817 | |
| 1818 | p = f->sections[sym->secidx]->contents + sym->value; |
| 1819 | strncpy(str, p, STRVERSIONLEN); |
| 1820 | |
| 1821 | a = strtoul(p, &p, 10); |
| 1822 | if (*p != '.') |
| 1823 | return -1; |
| 1824 | b = strtoul(p + 1, &p, 10); |
| 1825 | if (*p != '.') |
| 1826 | return -1; |
| 1827 | c = strtoul(p + 1, &q, 10); |
| 1828 | if (p + 1 == q) |
| 1829 | return -1; |
| 1830 | |
| 1831 | return a << 16 | b << 8 | c; |
| 1832 | } |
| 1833 | |
| 1834 | #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */ |
| 1835 | |
Eric Andersen | f5d5e77 | 2001-01-24 23:34:48 +0000 | [diff] [blame] | 1836 | #ifdef BB_FEATURE_OLD_MODULE_INTERFACE |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1837 | |
| 1838 | /* Fetch all the symbols and divvy them up as appropriate for the modules. */ |
| 1839 | |
Eric Andersen | 8c185f9 | 2000-09-22 00:38:07 +0000 | [diff] [blame] | 1840 | static int old_get_kernel_symbols(const char *m_name) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1841 | { |
| 1842 | struct old_kernel_sym *ks, *k; |
| 1843 | struct new_module_symbol *s; |
| 1844 | struct external_module *mod; |
| 1845 | int nks, nms, nmod, i; |
| 1846 | |
| 1847 | nks = get_kernel_syms(NULL); |
Eric Andersen | f227815 | 2001-04-24 21:41:41 +0000 | [diff] [blame] | 1848 | if (nks <= 0) { |
Eric Andersen | 4f4bcfc | 2001-04-25 17:22:32 +0000 | [diff] [blame] | 1849 | if (nks) |
| 1850 | perror_msg("get_kernel_syms: %s", m_name); |
| 1851 | else |
| 1852 | error_msg("No kernel symbols"); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1853 | return 0; |
| 1854 | } |
| 1855 | |
| 1856 | ks = k = xmalloc(nks * sizeof(*ks)); |
| 1857 | |
| 1858 | if (get_kernel_syms(ks) != nks) { |
| 1859 | perror("inconsistency with get_kernel_syms -- is someone else " |
| 1860 | "playing with modules?"); |
| 1861 | free(ks); |
| 1862 | return 0; |
| 1863 | } |
| 1864 | |
| 1865 | /* Collect the module information. */ |
| 1866 | |
| 1867 | mod = NULL; |
| 1868 | nmod = -1; |
| 1869 | |
| 1870 | while (k->name[0] == '#' && k->name[1]) { |
| 1871 | struct old_kernel_sym *k2; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1872 | |
| 1873 | /* Find out how many symbols this module has. */ |
| 1874 | for (k2 = k + 1; k2->name[0] != '#'; ++k2) |
| 1875 | continue; |
| 1876 | nms = k2 - k - 1; |
| 1877 | |
| 1878 | mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod)); |
| 1879 | mod[nmod].name = k->name + 1; |
| 1880 | mod[nmod].addr = k->value; |
| 1881 | mod[nmod].used = 0; |
| 1882 | mod[nmod].nsyms = nms; |
| 1883 | mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL); |
| 1884 | |
| 1885 | for (i = 0, ++k; i < nms; ++i, ++s, ++k) { |
| 1886 | s->name = (unsigned long) k->name; |
| 1887 | s->value = k->value; |
| 1888 | } |
| 1889 | |
| 1890 | k = k2; |
| 1891 | } |
| 1892 | |
| 1893 | ext_modules = mod; |
| 1894 | n_ext_modules = nmod + 1; |
| 1895 | |
| 1896 | /* Now collect the symbols for the kernel proper. */ |
| 1897 | |
| 1898 | if (k->name[0] == '#') |
| 1899 | ++k; |
| 1900 | |
| 1901 | nksyms = nms = nks - (k - ks); |
| 1902 | ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL); |
| 1903 | |
| 1904 | for (i = 0; i < nms; ++i, ++s, ++k) { |
| 1905 | s->name = (unsigned long) k->name; |
| 1906 | s->value = k->value; |
| 1907 | } |
| 1908 | |
| 1909 | return 1; |
| 1910 | } |
| 1911 | |
| 1912 | /* Return the kernel symbol checksum version, or zero if not used. */ |
| 1913 | |
| 1914 | static int old_is_kernel_checksummed(void) |
| 1915 | { |
| 1916 | /* Using_Versions is the first symbol. */ |
| 1917 | if (nksyms > 0 |
| 1918 | && strcmp((char *) ksyms[0].name, |
| 1919 | "Using_Versions") == 0) return ksyms[0].value; |
| 1920 | else |
| 1921 | return 0; |
| 1922 | } |
| 1923 | |
| 1924 | |
| 1925 | static int old_create_mod_use_count(struct obj_file *f) |
| 1926 | { |
| 1927 | struct obj_section *sec; |
| 1928 | |
| 1929 | sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long), |
| 1930 | sizeof(long)); |
| 1931 | |
| 1932 | obj_add_symbol(f, "mod_use_count_", -1, |
| 1933 | ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0, |
| 1934 | sizeof(long)); |
| 1935 | |
| 1936 | return 1; |
| 1937 | } |
| 1938 | |
| 1939 | static int |
| 1940 | old_init_module(const char *m_name, struct obj_file *f, |
| 1941 | unsigned long m_size) |
| 1942 | { |
| 1943 | char *image; |
| 1944 | struct old_mod_routines routines; |
| 1945 | struct old_symbol_table *symtab; |
| 1946 | int ret; |
| 1947 | |
| 1948 | /* Create the symbol table */ |
| 1949 | { |
| 1950 | int nsyms = 0, strsize = 0, total; |
| 1951 | |
| 1952 | /* Size things first... */ |
| 1953 | if (flag_export) { |
| 1954 | int i; |
| 1955 | for (i = 0; i < HASH_BUCKETS; ++i) { |
| 1956 | struct obj_symbol *sym; |
| 1957 | for (sym = f->symtab[i]; sym; sym = sym->next) |
| 1958 | if (ELFW(ST_BIND) (sym->info) != STB_LOCAL |
| 1959 | && sym->secidx <= SHN_HIRESERVE) |
| 1960 | { |
| 1961 | sym->ksymidx = nsyms++; |
| 1962 | strsize += strlen(sym->name) + 1; |
| 1963 | } |
| 1964 | } |
| 1965 | } |
| 1966 | |
| 1967 | total = (sizeof(struct old_symbol_table) |
| 1968 | + nsyms * sizeof(struct old_module_symbol) |
| 1969 | + n_ext_modules_used * sizeof(struct old_module_ref) |
| 1970 | + strsize); |
| 1971 | symtab = xmalloc(total); |
| 1972 | symtab->size = total; |
| 1973 | symtab->n_symbols = nsyms; |
| 1974 | symtab->n_refs = n_ext_modules_used; |
| 1975 | |
| 1976 | if (flag_export && nsyms) { |
| 1977 | struct old_module_symbol *ksym; |
| 1978 | char *str; |
| 1979 | int i; |
| 1980 | |
| 1981 | ksym = symtab->symbol; |
| 1982 | str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol) |
| 1983 | + n_ext_modules_used * sizeof(struct old_module_ref)); |
| 1984 | |
| 1985 | for (i = 0; i < HASH_BUCKETS; ++i) { |
| 1986 | struct obj_symbol *sym; |
| 1987 | for (sym = f->symtab[i]; sym; sym = sym->next) |
| 1988 | if (sym->ksymidx >= 0) { |
| 1989 | ksym->addr = obj_symbol_final_value(f, sym); |
| 1990 | ksym->name = |
| 1991 | (unsigned long) str - (unsigned long) symtab; |
| 1992 | |
Matt Kraai | 70a7855 | 2001-01-04 02:00:17 +0000 | [diff] [blame] | 1993 | strcpy(str, sym->name); |
| 1994 | str += strlen(sym->name) + 1; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1995 | ksym++; |
| 1996 | } |
| 1997 | } |
| 1998 | } |
| 1999 | |
| 2000 | if (n_ext_modules_used) { |
| 2001 | struct old_module_ref *ref; |
| 2002 | int i; |
| 2003 | |
| 2004 | ref = (struct old_module_ref *) |
| 2005 | ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol)); |
| 2006 | |
| 2007 | for (i = 0; i < n_ext_modules; ++i) |
| 2008 | if (ext_modules[i].used) |
| 2009 | ref++->module = ext_modules[i].addr; |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | /* Fill in routines. */ |
| 2014 | |
| 2015 | routines.init = |
| 2016 | obj_symbol_final_value(f, obj_find_symbol(f, "init_module")); |
| 2017 | routines.cleanup = |
| 2018 | obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module")); |
| 2019 | |
| 2020 | /* Whew! All of the initialization is complete. Collect the final |
| 2021 | module image and give it to the kernel. */ |
| 2022 | |
| 2023 | image = xmalloc(m_size); |
| 2024 | obj_create_image(f, image); |
| 2025 | |
| 2026 | /* image holds the complete relocated module, accounting correctly for |
| 2027 | mod_use_count. However the old module kernel support assume that |
| 2028 | it is receiving something which does not contain mod_use_count. */ |
| 2029 | ret = old_sys_init_module(m_name, image + sizeof(long), |
| 2030 | m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN |
| 2031 | : 0), &routines, symtab); |
| 2032 | if (ret) |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 2033 | perror_msg("init_module: %s", m_name); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2034 | |
| 2035 | free(image); |
| 2036 | free(symtab); |
| 2037 | |
| 2038 | return ret == 0; |
| 2039 | } |
| 2040 | |
| 2041 | #else |
| 2042 | |
| 2043 | #define old_create_mod_use_count(x) TRUE |
| 2044 | #define old_init_module(x, y, z) TRUE |
| 2045 | |
Eric Andersen | f5d5e77 | 2001-01-24 23:34:48 +0000 | [diff] [blame] | 2046 | #endif /* BB_FEATURE_OLD_MODULE_INTERFACE */ |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2047 | |
| 2048 | |
| 2049 | |
| 2050 | /*======================================================================*/ |
| 2051 | /* Functions relating to module loading after 2.1.18. */ |
| 2052 | |
| 2053 | static int |
| 2054 | new_process_module_arguments(struct obj_file *f, int argc, char **argv) |
| 2055 | { |
| 2056 | while (argc > 0) { |
| 2057 | char *p, *q, *key; |
| 2058 | struct obj_symbol *sym; |
| 2059 | char *contents, *loc; |
| 2060 | int min, max, n; |
| 2061 | |
| 2062 | p = *argv; |
Eric Andersen | ef40aa8 | 2000-06-26 11:16:22 +0000 | [diff] [blame] | 2063 | if ((q = strchr(p, '=')) == NULL) { |
| 2064 | argc--; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2065 | continue; |
Eric Andersen | ef40aa8 | 2000-06-26 11:16:22 +0000 | [diff] [blame] | 2066 | } |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2067 | |
| 2068 | key = alloca(q - p + 6); |
| 2069 | memcpy(key, "parm_", 5); |
| 2070 | memcpy(key + 5, p, q - p); |
| 2071 | key[q - p + 5] = 0; |
| 2072 | |
| 2073 | p = get_modinfo_value(f, key); |
| 2074 | key += 5; |
| 2075 | if (p == NULL) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 2076 | error_msg("invalid parameter %s", key); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2077 | return 0; |
| 2078 | } |
| 2079 | |
| 2080 | sym = obj_find_symbol(f, key); |
| 2081 | |
| 2082 | /* Also check that the parameter was not resolved from the kernel. */ |
| 2083 | if (sym == NULL || sym->secidx > SHN_HIRESERVE) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 2084 | error_msg("symbol for parameter %s not found", key); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2085 | return 0; |
| 2086 | } |
| 2087 | |
| 2088 | if (isdigit(*p)) { |
| 2089 | min = strtoul(p, &p, 10); |
| 2090 | if (*p == '-') |
| 2091 | max = strtoul(p + 1, &p, 10); |
| 2092 | else |
| 2093 | max = min; |
| 2094 | } else |
| 2095 | min = max = 1; |
| 2096 | |
| 2097 | contents = f->sections[sym->secidx]->contents; |
| 2098 | loc = contents + sym->value; |
| 2099 | n = (*++q != '\0'); |
| 2100 | |
| 2101 | while (1) { |
| 2102 | if ((*p == 's') || (*p == 'c')) { |
| 2103 | char *str; |
| 2104 | |
| 2105 | /* Do C quoting if we begin with a ", else slurp the lot. */ |
| 2106 | if (*q == '"') { |
| 2107 | char *r; |
| 2108 | |
| 2109 | str = alloca(strlen(q)); |
| 2110 | for (r = str, q++; *q != '"'; ++q, ++r) { |
| 2111 | if (*q == '\0') { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 2112 | error_msg("improperly terminated string argument for %s", |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2113 | key); |
| 2114 | return 0; |
| 2115 | } else if (*q == '\\') |
| 2116 | switch (*++q) { |
| 2117 | case 'a': |
| 2118 | *r = '\a'; |
| 2119 | break; |
| 2120 | case 'b': |
| 2121 | *r = '\b'; |
| 2122 | break; |
| 2123 | case 'e': |
| 2124 | *r = '\033'; |
| 2125 | break; |
| 2126 | case 'f': |
| 2127 | *r = '\f'; |
| 2128 | break; |
| 2129 | case 'n': |
| 2130 | *r = '\n'; |
| 2131 | break; |
| 2132 | case 'r': |
| 2133 | *r = '\r'; |
| 2134 | break; |
| 2135 | case 't': |
| 2136 | *r = '\t'; |
| 2137 | break; |
| 2138 | |
| 2139 | case '0': |
| 2140 | case '1': |
| 2141 | case '2': |
| 2142 | case '3': |
| 2143 | case '4': |
| 2144 | case '5': |
| 2145 | case '6': |
| 2146 | case '7': |
| 2147 | { |
| 2148 | int c = *q - '0'; |
| 2149 | if (q[1] >= '0' && q[1] <= '7') { |
| 2150 | c = (c * 8) + *++q - '0'; |
| 2151 | if (q[1] >= '0' && q[1] <= '7') |
| 2152 | c = (c * 8) + *++q - '0'; |
| 2153 | } |
| 2154 | *r = c; |
| 2155 | } |
| 2156 | break; |
| 2157 | |
| 2158 | default: |
| 2159 | *r = *q; |
| 2160 | break; |
| 2161 | } else |
| 2162 | *r = *q; |
| 2163 | } |
| 2164 | *r = '\0'; |
| 2165 | ++q; |
| 2166 | } else { |
| 2167 | char *r; |
| 2168 | |
| 2169 | /* In this case, the string is not quoted. We will break |
| 2170 | it using the coma (like for ints). If the user wants to |
| 2171 | include comas in a string, he just has to quote it */ |
| 2172 | |
| 2173 | /* Search the next coma */ |
| 2174 | r = strchr(q, ','); |
| 2175 | |
| 2176 | /* Found ? */ |
| 2177 | if (r != (char *) NULL) { |
| 2178 | /* Recopy the current field */ |
| 2179 | str = alloca(r - q + 1); |
| 2180 | memcpy(str, q, r - q); |
| 2181 | |
| 2182 | /* I don't know if it is usefull, as the previous case |
| 2183 | doesn't null terminate the string ??? */ |
| 2184 | str[r - q] = '\0'; |
| 2185 | |
| 2186 | /* Keep next fields */ |
| 2187 | q = r; |
| 2188 | } else { |
| 2189 | /* last string */ |
| 2190 | str = q; |
| 2191 | q = ""; |
| 2192 | } |
| 2193 | } |
| 2194 | |
| 2195 | if (*p == 's') { |
| 2196 | /* Normal string */ |
| 2197 | obj_string_patch(f, sym->secidx, loc - contents, str); |
| 2198 | loc += tgt_sizeof_char_p; |
| 2199 | } else { |
| 2200 | /* Array of chars (in fact, matrix !) */ |
Pavel Roskin | ff5a903 | 2000-07-14 16:23:32 +0000 | [diff] [blame] | 2201 | unsigned long charssize; /* size of each member */ |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2202 | |
| 2203 | /* Get the size of each member */ |
| 2204 | /* Probably we should do that outside the loop ? */ |
| 2205 | if (!isdigit(*(p + 1))) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 2206 | error_msg("parameter type 'c' for %s must be followed by" |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 2207 | " the maximum size", key); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2208 | return 0; |
| 2209 | } |
| 2210 | charssize = strtoul(p + 1, (char **) NULL, 10); |
| 2211 | |
| 2212 | /* Check length */ |
| 2213 | if (strlen(str) >= charssize) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 2214 | error_msg("string too long for %s (max %ld)", key, |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2215 | charssize - 1); |
| 2216 | return 0; |
| 2217 | } |
| 2218 | |
| 2219 | /* Copy to location */ |
| 2220 | strcpy((char *) loc, str); |
| 2221 | loc += charssize; |
| 2222 | } |
| 2223 | } else { |
| 2224 | long v = strtoul(q, &q, 0); |
| 2225 | switch (*p) { |
| 2226 | case 'b': |
| 2227 | *loc++ = v; |
| 2228 | break; |
| 2229 | case 'h': |
| 2230 | *(short *) loc = v; |
| 2231 | loc += tgt_sizeof_short; |
| 2232 | break; |
| 2233 | case 'i': |
| 2234 | *(int *) loc = v; |
| 2235 | loc += tgt_sizeof_int; |
| 2236 | break; |
| 2237 | case 'l': |
| 2238 | *(long *) loc = v; |
| 2239 | loc += tgt_sizeof_long; |
| 2240 | break; |
| 2241 | |
| 2242 | default: |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 2243 | error_msg("unknown parameter type '%c' for %s", *p, key); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2244 | return 0; |
| 2245 | } |
| 2246 | } |
| 2247 | |
| 2248 | retry_end_of_value: |
| 2249 | switch (*q) { |
| 2250 | case '\0': |
| 2251 | goto end_of_arg; |
| 2252 | |
| 2253 | case ' ': |
| 2254 | case '\t': |
| 2255 | case '\n': |
| 2256 | case '\r': |
| 2257 | ++q; |
| 2258 | goto retry_end_of_value; |
| 2259 | |
| 2260 | case ',': |
| 2261 | if (++n > max) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 2262 | error_msg("too many values for %s (max %d)", key, max); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2263 | return 0; |
| 2264 | } |
| 2265 | ++q; |
| 2266 | break; |
| 2267 | |
| 2268 | default: |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 2269 | error_msg("invalid argument syntax for %s", key); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2270 | return 0; |
| 2271 | } |
| 2272 | } |
| 2273 | |
| 2274 | end_of_arg: |
| 2275 | if (n < min) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 2276 | error_msg("too few values for %s (min %d)", key, min); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2277 | return 0; |
| 2278 | } |
| 2279 | |
| 2280 | argc--, argv++; |
| 2281 | } |
| 2282 | |
| 2283 | return 1; |
| 2284 | } |
| 2285 | |
| 2286 | #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING |
| 2287 | static int new_is_module_checksummed(struct obj_file *f) |
| 2288 | { |
| 2289 | const char *p = get_modinfo_value(f, "using_checksums"); |
| 2290 | if (p) |
| 2291 | return atoi(p); |
| 2292 | else |
| 2293 | return 0; |
| 2294 | } |
| 2295 | |
| 2296 | /* Get the module's kernel version in the canonical integer form. */ |
| 2297 | |
| 2298 | static int |
| 2299 | new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN]) |
| 2300 | { |
| 2301 | char *p, *q; |
| 2302 | int a, b, c; |
| 2303 | |
| 2304 | p = get_modinfo_value(f, "kernel_version"); |
| 2305 | if (p == NULL) |
| 2306 | return -1; |
| 2307 | strncpy(str, p, STRVERSIONLEN); |
| 2308 | |
| 2309 | a = strtoul(p, &p, 10); |
| 2310 | if (*p != '.') |
| 2311 | return -1; |
| 2312 | b = strtoul(p + 1, &p, 10); |
| 2313 | if (*p != '.') |
| 2314 | return -1; |
| 2315 | c = strtoul(p + 1, &q, 10); |
| 2316 | if (p + 1 == q) |
| 2317 | return -1; |
| 2318 | |
| 2319 | return a << 16 | b << 8 | c; |
| 2320 | } |
| 2321 | |
| 2322 | #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */ |
| 2323 | |
| 2324 | |
Eric Andersen | f5d5e77 | 2001-01-24 23:34:48 +0000 | [diff] [blame] | 2325 | #ifdef BB_FEATURE_NEW_MODULE_INTERFACE |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2326 | |
| 2327 | /* Fetch the loaded modules, and all currently exported symbols. */ |
| 2328 | |
| 2329 | static int new_get_kernel_symbols(void) |
| 2330 | { |
| 2331 | char *module_names, *mn; |
| 2332 | struct external_module *modules, *m; |
| 2333 | struct new_module_symbol *syms, *s; |
| 2334 | size_t ret, bufsize, nmod, nsyms, i, j; |
| 2335 | |
| 2336 | /* Collect the loaded modules. */ |
| 2337 | |
| 2338 | module_names = xmalloc(bufsize = 256); |
| 2339 | retry_modules_load: |
| 2340 | if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) { |
Eric Andersen | 4f4bcfc | 2001-04-25 17:22:32 +0000 | [diff] [blame] | 2341 | if (errno == ENOSPC && bufsize < ret) { |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2342 | module_names = xrealloc(module_names, bufsize = ret); |
| 2343 | goto retry_modules_load; |
| 2344 | } |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 2345 | perror_msg("QM_MODULES"); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2346 | return 0; |
| 2347 | } |
| 2348 | |
| 2349 | n_ext_modules = nmod = ret; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2350 | |
| 2351 | /* Collect the modules' symbols. */ |
| 2352 | |
Mark Whitley | 94fd480 | 2001-03-12 23:08:34 +0000 | [diff] [blame] | 2353 | if (nmod){ |
| 2354 | ext_modules = modules = xmalloc(nmod * sizeof(*modules)); |
| 2355 | memset(modules, 0, nmod * sizeof(*modules)); |
| 2356 | for (i = 0, mn = module_names, m = modules; |
| 2357 | i < nmod; ++i, ++m, mn += strlen(mn) + 1) { |
| 2358 | struct new_module_info info; |
| 2359 | |
| 2360 | if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) { |
| 2361 | if (errno == ENOENT) { |
| 2362 | /* The module was removed out from underneath us. */ |
| 2363 | continue; |
| 2364 | } |
| 2365 | perror_msg("query_module: QM_INFO: %s", mn); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2366 | return 0; |
| 2367 | } |
Mark Whitley | 94fd480 | 2001-03-12 23:08:34 +0000 | [diff] [blame] | 2368 | |
| 2369 | syms = xmalloc(bufsize = 1024); |
| 2370 | retry_mod_sym_load: |
| 2371 | if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) { |
| 2372 | switch (errno) { |
| 2373 | case ENOSPC: |
| 2374 | syms = xrealloc(syms, bufsize = ret); |
| 2375 | goto retry_mod_sym_load; |
| 2376 | case ENOENT: |
| 2377 | /* The module was removed out from underneath us. */ |
| 2378 | continue; |
| 2379 | default: |
| 2380 | perror_msg("query_module: QM_SYMBOLS: %s", mn); |
| 2381 | return 0; |
| 2382 | } |
| 2383 | } |
| 2384 | nsyms = ret; |
| 2385 | |
| 2386 | m->name = mn; |
| 2387 | m->addr = info.addr; |
| 2388 | m->nsyms = nsyms; |
| 2389 | m->syms = syms; |
| 2390 | |
| 2391 | for (j = 0, s = syms; j < nsyms; ++j, ++s) { |
| 2392 | s->name += (unsigned long) syms; |
| 2393 | } |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2394 | } |
| 2395 | } |
| 2396 | |
| 2397 | /* Collect the kernel's symbols. */ |
| 2398 | |
| 2399 | syms = xmalloc(bufsize = 16 * 1024); |
| 2400 | retry_kern_sym_load: |
| 2401 | if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) { |
Eric Andersen | 4f4bcfc | 2001-04-25 17:22:32 +0000 | [diff] [blame] | 2402 | if (errno == ENOSPC && bufsize < ret) { |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2403 | syms = xrealloc(syms, bufsize = ret); |
| 2404 | goto retry_kern_sym_load; |
| 2405 | } |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 2406 | perror_msg("kernel: QM_SYMBOLS"); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2407 | return 0; |
| 2408 | } |
| 2409 | nksyms = nsyms = ret; |
| 2410 | ksyms = syms; |
| 2411 | |
| 2412 | for (j = 0, s = syms; j < nsyms; ++j, ++s) { |
| 2413 | s->name += (unsigned long) syms; |
| 2414 | } |
| 2415 | return 1; |
| 2416 | } |
| 2417 | |
| 2418 | |
| 2419 | /* Return the kernel symbol checksum version, or zero if not used. */ |
| 2420 | |
| 2421 | static int new_is_kernel_checksummed(void) |
| 2422 | { |
| 2423 | struct new_module_symbol *s; |
| 2424 | size_t i; |
| 2425 | |
| 2426 | /* Using_Versions is not the first symbol, but it should be in there. */ |
| 2427 | |
| 2428 | for (i = 0, s = ksyms; i < nksyms; ++i, ++s) |
| 2429 | if (strcmp((char *) s->name, "Using_Versions") == 0) |
| 2430 | return s->value; |
| 2431 | |
| 2432 | return 0; |
| 2433 | } |
| 2434 | |
| 2435 | |
| 2436 | static int new_create_this_module(struct obj_file *f, const char *m_name) |
| 2437 | { |
| 2438 | struct obj_section *sec; |
| 2439 | |
| 2440 | sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long, |
| 2441 | sizeof(struct new_module)); |
| 2442 | memset(sec->contents, 0, sizeof(struct new_module)); |
| 2443 | |
| 2444 | obj_add_symbol(f, "__this_module", -1, |
| 2445 | ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0, |
| 2446 | sizeof(struct new_module)); |
| 2447 | |
| 2448 | obj_string_patch(f, sec->idx, offsetof(struct new_module, name), |
| 2449 | m_name); |
| 2450 | |
| 2451 | return 1; |
| 2452 | } |
| 2453 | |
| 2454 | |
| 2455 | static int new_create_module_ksymtab(struct obj_file *f) |
| 2456 | { |
| 2457 | struct obj_section *sec; |
| 2458 | int i; |
| 2459 | |
| 2460 | /* We must always add the module references. */ |
| 2461 | |
| 2462 | if (n_ext_modules_used) { |
| 2463 | struct new_module_ref *dep; |
| 2464 | struct obj_symbol *tm; |
| 2465 | |
| 2466 | sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p, |
| 2467 | (sizeof(struct new_module_ref) |
| 2468 | * n_ext_modules_used)); |
| 2469 | if (!sec) |
| 2470 | return 0; |
| 2471 | |
| 2472 | tm = obj_find_symbol(f, "__this_module"); |
| 2473 | dep = (struct new_module_ref *) sec->contents; |
| 2474 | for (i = 0; i < n_ext_modules; ++i) |
| 2475 | if (ext_modules[i].used) { |
| 2476 | dep->dep = ext_modules[i].addr; |
| 2477 | obj_symbol_patch(f, sec->idx, |
| 2478 | (char *) &dep->ref - sec->contents, tm); |
| 2479 | dep->next_ref = 0; |
| 2480 | ++dep; |
| 2481 | } |
| 2482 | } |
| 2483 | |
| 2484 | if (flag_export && !obj_find_section(f, "__ksymtab")) { |
| 2485 | size_t nsyms; |
| 2486 | int *loaded; |
| 2487 | |
| 2488 | sec = |
| 2489 | obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p, |
| 2490 | 0); |
| 2491 | |
| 2492 | /* We don't want to export symbols residing in sections that |
| 2493 | aren't loaded. There are a number of these created so that |
| 2494 | we make sure certain module options don't appear twice. */ |
| 2495 | |
| 2496 | loaded = alloca(sizeof(int) * (i = f->header.e_shnum)); |
| 2497 | while (--i >= 0) |
| 2498 | loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0; |
| 2499 | |
| 2500 | for (nsyms = i = 0; i < HASH_BUCKETS; ++i) { |
| 2501 | struct obj_symbol *sym; |
| 2502 | for (sym = f->symtab[i]; sym; sym = sym->next) |
| 2503 | if (ELFW(ST_BIND) (sym->info) != STB_LOCAL |
| 2504 | && sym->secidx <= SHN_HIRESERVE |
| 2505 | && (sym->secidx >= SHN_LORESERVE |
| 2506 | || loaded[sym->secidx])) { |
| 2507 | ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p; |
| 2508 | |
| 2509 | obj_symbol_patch(f, sec->idx, ofs, sym); |
| 2510 | obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, |
| 2511 | sym->name); |
| 2512 | |
| 2513 | nsyms++; |
| 2514 | } |
| 2515 | } |
| 2516 | |
| 2517 | obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p); |
| 2518 | } |
| 2519 | |
| 2520 | return 1; |
| 2521 | } |
| 2522 | |
| 2523 | |
| 2524 | static int |
| 2525 | new_init_module(const char *m_name, struct obj_file *f, |
| 2526 | unsigned long m_size) |
| 2527 | { |
| 2528 | struct new_module *module; |
| 2529 | struct obj_section *sec; |
| 2530 | void *image; |
| 2531 | int ret; |
| 2532 | tgt_long m_addr; |
| 2533 | |
| 2534 | sec = obj_find_section(f, ".this"); |
Eric Andersen | 7f3b86e | 2001-04-26 19:29:58 +0000 | [diff] [blame] | 2535 | if (!sec || !sec->contents) { |
| 2536 | perror_msg_and_die("corrupt module %s?",m_name); |
| 2537 | } |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2538 | module = (struct new_module *) sec->contents; |
| 2539 | m_addr = sec->header.sh_addr; |
| 2540 | |
| 2541 | module->size_of_struct = sizeof(*module); |
| 2542 | module->size = m_size; |
| 2543 | module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0; |
| 2544 | |
| 2545 | sec = obj_find_section(f, "__ksymtab"); |
| 2546 | if (sec && sec->header.sh_size) { |
| 2547 | module->syms = sec->header.sh_addr; |
| 2548 | module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p); |
| 2549 | } |
| 2550 | |
| 2551 | if (n_ext_modules_used) { |
| 2552 | sec = obj_find_section(f, ".kmodtab"); |
| 2553 | module->deps = sec->header.sh_addr; |
| 2554 | module->ndeps = n_ext_modules_used; |
| 2555 | } |
| 2556 | |
| 2557 | module->init = |
| 2558 | obj_symbol_final_value(f, obj_find_symbol(f, "init_module")); |
| 2559 | module->cleanup = |
| 2560 | obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module")); |
| 2561 | |
| 2562 | sec = obj_find_section(f, "__ex_table"); |
| 2563 | if (sec) { |
| 2564 | module->ex_table_start = sec->header.sh_addr; |
| 2565 | module->ex_table_end = sec->header.sh_addr + sec->header.sh_size; |
| 2566 | } |
| 2567 | |
| 2568 | sec = obj_find_section(f, ".text.init"); |
| 2569 | if (sec) { |
| 2570 | module->runsize = sec->header.sh_addr - m_addr; |
| 2571 | } |
| 2572 | sec = obj_find_section(f, ".data.init"); |
| 2573 | if (sec) { |
| 2574 | if (!module->runsize || |
| 2575 | module->runsize > sec->header.sh_addr - m_addr) |
| 2576 | module->runsize = sec->header.sh_addr - m_addr; |
| 2577 | } |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 2578 | sec = obj_find_section(f, ARCHDATA_SEC_NAME); |
| 2579 | if (sec && sec->header.sh_size) { |
| 2580 | module->archdata_start = (void*)sec->header.sh_addr; |
| 2581 | module->archdata_end = module->archdata_start + sec->header.sh_size; |
| 2582 | } |
| 2583 | sec = obj_find_section(f, KALLSYMS_SEC_NAME); |
| 2584 | if (sec && sec->header.sh_size) { |
| 2585 | module->kallsyms_start = (void*)sec->header.sh_addr; |
| 2586 | module->kallsyms_end = module->kallsyms_start + sec->header.sh_size; |
| 2587 | } |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2588 | |
| 2589 | if (!arch_init_module(f, module)) |
| 2590 | return 0; |
| 2591 | |
| 2592 | /* Whew! All of the initialization is complete. Collect the final |
| 2593 | module image and give it to the kernel. */ |
| 2594 | |
| 2595 | image = xmalloc(m_size); |
| 2596 | obj_create_image(f, image); |
| 2597 | |
Eric Andersen | 64c8b17 | 2001-04-05 07:33:10 +0000 | [diff] [blame] | 2598 | ret = new_sys_init_module(m_name, (struct new_module *) image); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2599 | if (ret) |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 2600 | perror_msg("init_module: %s", m_name); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2601 | |
| 2602 | free(image); |
| 2603 | |
| 2604 | return ret == 0; |
| 2605 | } |
| 2606 | |
| 2607 | #else |
| 2608 | |
| 2609 | #define new_init_module(x, y, z) TRUE |
| 2610 | #define new_create_this_module(x, y) 0 |
| 2611 | #define new_create_module_ksymtab(x) |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 2612 | #define query_module(v, w, x, y, z) -1 |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2613 | |
Eric Andersen | f5d5e77 | 2001-01-24 23:34:48 +0000 | [diff] [blame] | 2614 | #endif /* BB_FEATURE_NEW_MODULE_INTERFACE */ |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2615 | |
| 2616 | |
| 2617 | /*======================================================================*/ |
| 2618 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 2619 | static int |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2620 | obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset, |
| 2621 | const char *string) |
| 2622 | { |
| 2623 | struct obj_string_patch *p; |
| 2624 | struct obj_section *strsec; |
| 2625 | size_t len = strlen(string) + 1; |
| 2626 | char *loc; |
| 2627 | |
| 2628 | p = xmalloc(sizeof(*p)); |
| 2629 | p->next = f->string_patches; |
| 2630 | p->reloc_secidx = secidx; |
| 2631 | p->reloc_offset = offset; |
| 2632 | f->string_patches = p; |
| 2633 | |
| 2634 | strsec = obj_find_section(f, ".kstrtab"); |
| 2635 | if (strsec == NULL) { |
| 2636 | strsec = obj_create_alloced_section(f, ".kstrtab", 1, len); |
| 2637 | p->string_offset = 0; |
| 2638 | loc = strsec->contents; |
| 2639 | } else { |
| 2640 | p->string_offset = strsec->header.sh_size; |
| 2641 | loc = obj_extend_section(strsec, len); |
| 2642 | } |
| 2643 | memcpy(loc, string, len); |
| 2644 | |
| 2645 | return 1; |
| 2646 | } |
| 2647 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 2648 | static int |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2649 | obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset, |
| 2650 | struct obj_symbol *sym) |
| 2651 | { |
| 2652 | struct obj_symbol_patch *p; |
| 2653 | |
| 2654 | p = xmalloc(sizeof(*p)); |
| 2655 | p->next = f->symbol_patches; |
| 2656 | p->reloc_secidx = secidx; |
| 2657 | p->reloc_offset = offset; |
| 2658 | p->sym = sym; |
| 2659 | f->symbol_patches = p; |
| 2660 | |
| 2661 | return 1; |
| 2662 | } |
| 2663 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 2664 | static int obj_check_undefineds(struct obj_file *f) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2665 | { |
| 2666 | unsigned long i; |
| 2667 | int ret = 1; |
| 2668 | |
| 2669 | for (i = 0; i < HASH_BUCKETS; ++i) { |
| 2670 | struct obj_symbol *sym; |
| 2671 | for (sym = f->symtab[i]; sym; sym = sym->next) |
| 2672 | if (sym->secidx == SHN_UNDEF) { |
| 2673 | if (ELFW(ST_BIND) (sym->info) == STB_WEAK) { |
| 2674 | sym->secidx = SHN_ABS; |
| 2675 | sym->value = 0; |
| 2676 | } else { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 2677 | error_msg("unresolved symbol %s", sym->name); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2678 | ret = 0; |
| 2679 | } |
| 2680 | } |
| 2681 | } |
| 2682 | |
| 2683 | return ret; |
| 2684 | } |
| 2685 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 2686 | static void obj_allocate_commons(struct obj_file *f) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2687 | { |
| 2688 | struct common_entry { |
| 2689 | struct common_entry *next; |
| 2690 | struct obj_symbol *sym; |
| 2691 | } *common_head = NULL; |
| 2692 | |
| 2693 | unsigned long i; |
| 2694 | |
| 2695 | for (i = 0; i < HASH_BUCKETS; ++i) { |
| 2696 | struct obj_symbol *sym; |
| 2697 | for (sym = f->symtab[i]; sym; sym = sym->next) |
| 2698 | if (sym->secidx == SHN_COMMON) { |
| 2699 | /* Collect all COMMON symbols and sort them by size so as to |
| 2700 | minimize space wasted by alignment requirements. */ |
| 2701 | { |
| 2702 | struct common_entry **p, *n; |
| 2703 | for (p = &common_head; *p; p = &(*p)->next) |
| 2704 | if (sym->size <= (*p)->sym->size) |
| 2705 | break; |
| 2706 | |
| 2707 | n = alloca(sizeof(*n)); |
| 2708 | n->next = *p; |
| 2709 | n->sym = sym; |
| 2710 | *p = n; |
| 2711 | } |
| 2712 | } |
| 2713 | } |
| 2714 | |
| 2715 | for (i = 1; i < f->local_symtab_size; ++i) { |
| 2716 | struct obj_symbol *sym = f->local_symtab[i]; |
| 2717 | if (sym && sym->secidx == SHN_COMMON) { |
| 2718 | struct common_entry **p, *n; |
| 2719 | for (p = &common_head; *p; p = &(*p)->next) |
| 2720 | if (sym == (*p)->sym) |
| 2721 | break; |
| 2722 | else if (sym->size < (*p)->sym->size) { |
| 2723 | n = alloca(sizeof(*n)); |
| 2724 | n->next = *p; |
| 2725 | n->sym = sym; |
| 2726 | *p = n; |
| 2727 | break; |
| 2728 | } |
| 2729 | } |
| 2730 | } |
| 2731 | |
| 2732 | if (common_head) { |
| 2733 | /* Find the bss section. */ |
| 2734 | for (i = 0; i < f->header.e_shnum; ++i) |
| 2735 | if (f->sections[i]->header.sh_type == SHT_NOBITS) |
| 2736 | break; |
| 2737 | |
| 2738 | /* If for some reason there hadn't been one, create one. */ |
| 2739 | if (i == f->header.e_shnum) { |
| 2740 | struct obj_section *sec; |
| 2741 | |
| 2742 | f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec)); |
| 2743 | f->sections[i] = sec = arch_new_section(); |
| 2744 | f->header.e_shnum = i + 1; |
| 2745 | |
| 2746 | memset(sec, 0, sizeof(*sec)); |
| 2747 | sec->header.sh_type = SHT_PROGBITS; |
| 2748 | sec->header.sh_flags = SHF_WRITE | SHF_ALLOC; |
| 2749 | sec->name = ".bss"; |
| 2750 | sec->idx = i; |
| 2751 | } |
| 2752 | |
| 2753 | /* Allocate the COMMONS. */ |
| 2754 | { |
| 2755 | ElfW(Addr) bss_size = f->sections[i]->header.sh_size; |
| 2756 | ElfW(Addr) max_align = f->sections[i]->header.sh_addralign; |
| 2757 | struct common_entry *c; |
| 2758 | |
| 2759 | for (c = common_head; c; c = c->next) { |
| 2760 | ElfW(Addr) align = c->sym->value; |
| 2761 | |
| 2762 | if (align > max_align) |
| 2763 | max_align = align; |
| 2764 | if (bss_size & (align - 1)) |
| 2765 | bss_size = (bss_size | (align - 1)) + 1; |
| 2766 | |
| 2767 | c->sym->secidx = i; |
| 2768 | c->sym->value = bss_size; |
| 2769 | |
| 2770 | bss_size += c->sym->size; |
| 2771 | } |
| 2772 | |
| 2773 | f->sections[i]->header.sh_size = bss_size; |
| 2774 | f->sections[i]->header.sh_addralign = max_align; |
| 2775 | } |
| 2776 | } |
| 2777 | |
| 2778 | /* For the sake of patch relocation and parameter initialization, |
| 2779 | allocate zeroed data for NOBITS sections now. Note that after |
| 2780 | this we cannot assume NOBITS are really empty. */ |
| 2781 | for (i = 0; i < f->header.e_shnum; ++i) { |
| 2782 | struct obj_section *s = f->sections[i]; |
| 2783 | if (s->header.sh_type == SHT_NOBITS) { |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 2784 | if (s->header.sh_size != 0) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2785 | s->contents = memset(xmalloc(s->header.sh_size), |
| 2786 | 0, s->header.sh_size); |
Eric Andersen | 21adca7 | 2000-12-06 18:18:26 +0000 | [diff] [blame] | 2787 | else |
| 2788 | s->contents = NULL; |
| 2789 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2790 | s->header.sh_type = SHT_PROGBITS; |
| 2791 | } |
| 2792 | } |
| 2793 | } |
| 2794 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 2795 | static unsigned long obj_load_size(struct obj_file *f) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2796 | { |
| 2797 | unsigned long dot = 0; |
| 2798 | struct obj_section *sec; |
| 2799 | |
| 2800 | /* Finalize the positions of the sections relative to one another. */ |
| 2801 | |
| 2802 | for (sec = f->load_order; sec; sec = sec->load_next) { |
| 2803 | ElfW(Addr) align; |
| 2804 | |
| 2805 | align = sec->header.sh_addralign; |
| 2806 | if (align && (dot & (align - 1))) |
| 2807 | dot = (dot | (align - 1)) + 1; |
| 2808 | |
| 2809 | sec->header.sh_addr = dot; |
| 2810 | dot += sec->header.sh_size; |
| 2811 | } |
| 2812 | |
| 2813 | return dot; |
| 2814 | } |
| 2815 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 2816 | static int obj_relocate(struct obj_file *f, ElfW(Addr) base) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2817 | { |
| 2818 | int i, n = f->header.e_shnum; |
| 2819 | int ret = 1; |
| 2820 | |
| 2821 | /* Finalize the addresses of the sections. */ |
| 2822 | |
| 2823 | f->baseaddr = base; |
| 2824 | for (i = 0; i < n; ++i) |
| 2825 | f->sections[i]->header.sh_addr += base; |
| 2826 | |
| 2827 | /* And iterate over all of the relocations. */ |
| 2828 | |
| 2829 | for (i = 0; i < n; ++i) { |
| 2830 | struct obj_section *relsec, *symsec, *targsec, *strsec; |
| 2831 | ElfW(RelM) * rel, *relend; |
| 2832 | ElfW(Sym) * symtab; |
| 2833 | const char *strtab; |
| 2834 | |
| 2835 | relsec = f->sections[i]; |
| 2836 | if (relsec->header.sh_type != SHT_RELM) |
| 2837 | continue; |
| 2838 | |
| 2839 | symsec = f->sections[relsec->header.sh_link]; |
| 2840 | targsec = f->sections[relsec->header.sh_info]; |
| 2841 | strsec = f->sections[symsec->header.sh_link]; |
| 2842 | |
| 2843 | rel = (ElfW(RelM) *) relsec->contents; |
| 2844 | relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM))); |
| 2845 | symtab = (ElfW(Sym) *) symsec->contents; |
| 2846 | strtab = (const char *) strsec->contents; |
| 2847 | |
| 2848 | for (; rel < relend; ++rel) { |
| 2849 | ElfW(Addr) value = 0; |
| 2850 | struct obj_symbol *intsym = NULL; |
| 2851 | unsigned long symndx; |
| 2852 | ElfW(Sym) * extsym = 0; |
| 2853 | const char *errmsg; |
| 2854 | |
| 2855 | /* Attempt to find a value to use for this relocation. */ |
| 2856 | |
| 2857 | symndx = ELFW(R_SYM) (rel->r_info); |
| 2858 | if (symndx) { |
| 2859 | /* Note we've already checked for undefined symbols. */ |
| 2860 | |
| 2861 | extsym = &symtab[symndx]; |
| 2862 | if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) { |
| 2863 | /* Local symbols we look up in the local table to be sure |
| 2864 | we get the one that is really intended. */ |
| 2865 | intsym = f->local_symtab[symndx]; |
| 2866 | } else { |
| 2867 | /* Others we look up in the hash table. */ |
| 2868 | const char *name; |
| 2869 | if (extsym->st_name) |
| 2870 | name = strtab + extsym->st_name; |
| 2871 | else |
| 2872 | name = f->sections[extsym->st_shndx]->name; |
| 2873 | intsym = obj_find_symbol(f, name); |
| 2874 | } |
| 2875 | |
| 2876 | value = obj_symbol_final_value(f, intsym); |
| 2877 | intsym->referenced = 1; |
| 2878 | } |
| 2879 | #if SHT_RELM == SHT_RELA |
| 2880 | #if defined(__alpha__) && defined(AXP_BROKEN_GAS) |
| 2881 | /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */ |
| 2882 | if (!extsym || !extsym->st_name || |
| 2883 | ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL) |
| 2884 | #endif |
| 2885 | value += rel->r_addend; |
| 2886 | #endif |
| 2887 | |
| 2888 | /* Do it! */ |
| 2889 | switch (arch_apply_relocation |
| 2890 | (f, targsec, symsec, intsym, rel, value)) { |
| 2891 | case obj_reloc_ok: |
| 2892 | break; |
| 2893 | |
| 2894 | case obj_reloc_overflow: |
| 2895 | errmsg = "Relocation overflow"; |
| 2896 | goto bad_reloc; |
| 2897 | case obj_reloc_dangerous: |
| 2898 | errmsg = "Dangerous relocation"; |
| 2899 | goto bad_reloc; |
| 2900 | case obj_reloc_unhandled: |
| 2901 | errmsg = "Unhandled relocation"; |
| 2902 | bad_reloc: |
| 2903 | if (extsym) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 2904 | error_msg("%s of type %ld for %s", errmsg, |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2905 | (long) ELFW(R_TYPE) (rel->r_info), |
| 2906 | strtab + extsym->st_name); |
| 2907 | } else { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 2908 | error_msg("%s of type %ld", errmsg, |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2909 | (long) ELFW(R_TYPE) (rel->r_info)); |
| 2910 | } |
| 2911 | ret = 0; |
| 2912 | break; |
| 2913 | } |
| 2914 | } |
| 2915 | } |
| 2916 | |
| 2917 | /* Finally, take care of the patches. */ |
| 2918 | |
| 2919 | if (f->string_patches) { |
| 2920 | struct obj_string_patch *p; |
| 2921 | struct obj_section *strsec; |
| 2922 | ElfW(Addr) strsec_base; |
| 2923 | strsec = obj_find_section(f, ".kstrtab"); |
| 2924 | strsec_base = strsec->header.sh_addr; |
| 2925 | |
| 2926 | for (p = f->string_patches; p; p = p->next) { |
| 2927 | struct obj_section *targsec = f->sections[p->reloc_secidx]; |
| 2928 | *(ElfW(Addr) *) (targsec->contents + p->reloc_offset) |
| 2929 | = strsec_base + p->string_offset; |
| 2930 | } |
| 2931 | } |
| 2932 | |
| 2933 | if (f->symbol_patches) { |
| 2934 | struct obj_symbol_patch *p; |
| 2935 | |
| 2936 | for (p = f->symbol_patches; p; p = p->next) { |
| 2937 | struct obj_section *targsec = f->sections[p->reloc_secidx]; |
| 2938 | *(ElfW(Addr) *) (targsec->contents + p->reloc_offset) |
| 2939 | = obj_symbol_final_value(f, p->sym); |
| 2940 | } |
| 2941 | } |
| 2942 | |
| 2943 | return ret; |
| 2944 | } |
| 2945 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 2946 | static int obj_create_image(struct obj_file *f, char *image) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2947 | { |
| 2948 | struct obj_section *sec; |
| 2949 | ElfW(Addr) base = f->baseaddr; |
| 2950 | |
| 2951 | for (sec = f->load_order; sec; sec = sec->load_next) { |
| 2952 | char *secimg; |
| 2953 | |
Eric Andersen | 2bf658d | 2001-02-24 20:01:53 +0000 | [diff] [blame] | 2954 | if (sec->contents == 0 || sec->header.sh_size == 0) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2955 | continue; |
| 2956 | |
| 2957 | secimg = image + (sec->header.sh_addr - base); |
| 2958 | |
| 2959 | /* Note that we allocated data for NOBITS sections earlier. */ |
| 2960 | memcpy(secimg, sec->contents, sec->header.sh_size); |
| 2961 | } |
| 2962 | |
| 2963 | return 1; |
| 2964 | } |
| 2965 | |
| 2966 | /*======================================================================*/ |
| 2967 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 2968 | static struct obj_file *obj_load(FILE * fp, int loadprogbits) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2969 | { |
| 2970 | struct obj_file *f; |
| 2971 | ElfW(Shdr) * section_headers; |
| 2972 | int shnum, i; |
| 2973 | char *shstrtab; |
| 2974 | |
| 2975 | /* Read the file header. */ |
| 2976 | |
| 2977 | f = arch_new_file(); |
| 2978 | memset(f, 0, sizeof(*f)); |
| 2979 | f->symbol_cmp = strcmp; |
| 2980 | f->symbol_hash = obj_elf_hash; |
| 2981 | f->load_order_search_start = &f->load_order; |
| 2982 | |
| 2983 | fseek(fp, 0, SEEK_SET); |
| 2984 | if (fread(&f->header, sizeof(f->header), 1, fp) != 1) { |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 2985 | perror_msg("error reading ELF header"); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2986 | return NULL; |
| 2987 | } |
| 2988 | |
| 2989 | if (f->header.e_ident[EI_MAG0] != ELFMAG0 |
| 2990 | || f->header.e_ident[EI_MAG1] != ELFMAG1 |
| 2991 | || f->header.e_ident[EI_MAG2] != ELFMAG2 |
| 2992 | || f->header.e_ident[EI_MAG3] != ELFMAG3) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 2993 | error_msg("not an ELF file"); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 2994 | return NULL; |
| 2995 | } |
| 2996 | if (f->header.e_ident[EI_CLASS] != ELFCLASSM |
| 2997 | || f->header.e_ident[EI_DATA] != ELFDATAM |
| 2998 | || f->header.e_ident[EI_VERSION] != EV_CURRENT |
| 2999 | || !MATCH_MACHINE(f->header.e_machine)) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 3000 | error_msg("ELF file not for this architecture"); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3001 | return NULL; |
| 3002 | } |
| 3003 | if (f->header.e_type != ET_REL) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 3004 | error_msg("ELF file not a relocatable object"); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3005 | return NULL; |
| 3006 | } |
| 3007 | |
| 3008 | /* Read the section headers. */ |
| 3009 | |
| 3010 | if (f->header.e_shentsize != sizeof(ElfW(Shdr))) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 3011 | error_msg("section header size mismatch: %lu != %lu", |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3012 | (unsigned long) f->header.e_shentsize, |
| 3013 | (unsigned long) sizeof(ElfW(Shdr))); |
| 3014 | return NULL; |
| 3015 | } |
| 3016 | |
| 3017 | shnum = f->header.e_shnum; |
| 3018 | f->sections = xmalloc(sizeof(struct obj_section *) * shnum); |
| 3019 | memset(f->sections, 0, sizeof(struct obj_section *) * shnum); |
| 3020 | |
| 3021 | section_headers = alloca(sizeof(ElfW(Shdr)) * shnum); |
| 3022 | fseek(fp, f->header.e_shoff, SEEK_SET); |
| 3023 | if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) { |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 3024 | perror_msg("error reading ELF section headers"); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3025 | return NULL; |
| 3026 | } |
| 3027 | |
| 3028 | /* Read the section data. */ |
| 3029 | |
| 3030 | for (i = 0; i < shnum; ++i) { |
| 3031 | struct obj_section *sec; |
| 3032 | |
| 3033 | f->sections[i] = sec = arch_new_section(); |
| 3034 | memset(sec, 0, sizeof(*sec)); |
| 3035 | |
| 3036 | sec->header = section_headers[i]; |
| 3037 | sec->idx = i; |
| 3038 | |
Eric Andersen | 2bf658d | 2001-02-24 20:01:53 +0000 | [diff] [blame] | 3039 | if(sec->header.sh_size) switch (sec->header.sh_type) { |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3040 | case SHT_NULL: |
| 3041 | case SHT_NOTE: |
| 3042 | case SHT_NOBITS: |
| 3043 | /* ignore */ |
| 3044 | break; |
| 3045 | |
| 3046 | case SHT_PROGBITS: |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 3047 | #if LOADBITS |
| 3048 | if (!loadprogbits) { |
| 3049 | sec->contents = NULL; |
| 3050 | break; |
| 3051 | } |
| 3052 | #endif |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3053 | case SHT_SYMTAB: |
| 3054 | case SHT_STRTAB: |
| 3055 | case SHT_RELM: |
| 3056 | if (sec->header.sh_size > 0) { |
| 3057 | sec->contents = xmalloc(sec->header.sh_size); |
| 3058 | fseek(fp, sec->header.sh_offset, SEEK_SET); |
| 3059 | if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) { |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 3060 | perror_msg("error reading ELF section data"); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3061 | return NULL; |
| 3062 | } |
| 3063 | } else { |
| 3064 | sec->contents = NULL; |
| 3065 | } |
| 3066 | break; |
| 3067 | |
| 3068 | #if SHT_RELM == SHT_REL |
| 3069 | case SHT_RELA: |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 3070 | error_msg("RELA relocations not supported on this architecture"); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3071 | return NULL; |
| 3072 | #else |
| 3073 | case SHT_REL: |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 3074 | error_msg("REL relocations not supported on this architecture"); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3075 | return NULL; |
| 3076 | #endif |
| 3077 | |
| 3078 | default: |
| 3079 | if (sec->header.sh_type >= SHT_LOPROC) { |
| 3080 | /* Assume processor specific section types are debug |
| 3081 | info and can safely be ignored. If this is ever not |
| 3082 | the case (Hello MIPS?), don't put ifdefs here but |
| 3083 | create an arch_load_proc_section(). */ |
| 3084 | break; |
| 3085 | } |
| 3086 | |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 3087 | error_msg("can't handle sections of type %ld", |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3088 | (long) sec->header.sh_type); |
| 3089 | return NULL; |
| 3090 | } |
| 3091 | } |
| 3092 | |
| 3093 | /* Do what sort of interpretation as needed by each section. */ |
| 3094 | |
| 3095 | shstrtab = f->sections[f->header.e_shstrndx]->contents; |
| 3096 | |
| 3097 | for (i = 0; i < shnum; ++i) { |
| 3098 | struct obj_section *sec = f->sections[i]; |
| 3099 | sec->name = shstrtab + sec->header.sh_name; |
| 3100 | } |
| 3101 | |
| 3102 | for (i = 0; i < shnum; ++i) { |
| 3103 | struct obj_section *sec = f->sections[i]; |
| 3104 | |
Eric Andersen | 4f4bcfc | 2001-04-25 17:22:32 +0000 | [diff] [blame] | 3105 | /* .modinfo should be contents only but gcc has no attribute for that. |
| 3106 | * The kernel may have marked .modinfo as ALLOC, ignore this bit. |
| 3107 | */ |
| 3108 | if (strcmp(sec->name, ".modinfo") == 0) |
| 3109 | sec->header.sh_flags &= ~SHF_ALLOC; |
| 3110 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3111 | if (sec->header.sh_flags & SHF_ALLOC) |
| 3112 | obj_insert_section_load_order(f, sec); |
| 3113 | |
| 3114 | switch (sec->header.sh_type) { |
| 3115 | case SHT_SYMTAB: |
| 3116 | { |
| 3117 | unsigned long nsym, j; |
| 3118 | char *strtab; |
| 3119 | ElfW(Sym) * sym; |
| 3120 | |
| 3121 | if (sec->header.sh_entsize != sizeof(ElfW(Sym))) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 3122 | error_msg("symbol size mismatch: %lu != %lu", |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3123 | (unsigned long) sec->header.sh_entsize, |
| 3124 | (unsigned long) sizeof(ElfW(Sym))); |
| 3125 | return NULL; |
| 3126 | } |
| 3127 | |
| 3128 | nsym = sec->header.sh_size / sizeof(ElfW(Sym)); |
| 3129 | strtab = f->sections[sec->header.sh_link]->contents; |
| 3130 | sym = (ElfW(Sym) *) sec->contents; |
| 3131 | |
| 3132 | /* Allocate space for a table of local symbols. */ |
| 3133 | j = f->local_symtab_size = sec->header.sh_info; |
Eric Andersen | 4f4bcfc | 2001-04-25 17:22:32 +0000 | [diff] [blame] | 3134 | f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *)); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3135 | |
| 3136 | /* Insert all symbols into the hash table. */ |
| 3137 | for (j = 1, ++sym; j < nsym; ++j, ++sym) { |
| 3138 | const char *name; |
| 3139 | if (sym->st_name) |
| 3140 | name = strtab + sym->st_name; |
Eric Andersen | 4f4bcfc | 2001-04-25 17:22:32 +0000 | [diff] [blame] | 3141 | else |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3142 | name = f->sections[sym->st_shndx]->name; |
| 3143 | |
| 3144 | obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx, |
| 3145 | sym->st_value, sym->st_size); |
Eric Andersen | 4f4bcfc | 2001-04-25 17:22:32 +0000 | [diff] [blame] | 3146 | } |
| 3147 | } |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3148 | break; |
| 3149 | |
| 3150 | case SHT_RELM: |
| 3151 | if (sec->header.sh_entsize != sizeof(ElfW(RelM))) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 3152 | error_msg("relocation entry size mismatch: %lu != %lu", |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3153 | (unsigned long) sec->header.sh_entsize, |
| 3154 | (unsigned long) sizeof(ElfW(RelM))); |
| 3155 | return NULL; |
| 3156 | } |
| 3157 | break; |
Eric Andersen | 4f4bcfc | 2001-04-25 17:22:32 +0000 | [diff] [blame] | 3158 | /* XXX Relocation code from modutils-2.3.19 is not here. |
| 3159 | * Why? That's about 20 lines of code from obj/obj_load.c, |
| 3160 | * which gets done in a second pass through the sections. |
| 3161 | * This BusyBox insmod does similar work in obj_relocate(). */ |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3162 | } |
| 3163 | } |
| 3164 | |
| 3165 | return f; |
| 3166 | } |
| 3167 | |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 3168 | #ifdef BB_FEATURE_INSMOD_LOADINKMEM |
| 3169 | /* |
| 3170 | * load the unloaded sections directly into the memory allocated by |
| 3171 | * kernel for the module |
| 3172 | */ |
| 3173 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame^] | 3174 | static int obj_load_progbits(FILE * fp, struct obj_file* f) |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 3175 | { |
| 3176 | char* imagebase = (char*) f->imagebase; |
| 3177 | ElfW(Addr) base = f->baseaddr; |
| 3178 | struct obj_section* sec; |
| 3179 | |
| 3180 | for (sec = f->load_order; sec; sec = sec->load_next) { |
| 3181 | |
| 3182 | /* section already loaded? */ |
| 3183 | if (sec->contents != NULL) |
| 3184 | continue; |
| 3185 | |
| 3186 | if (sec->header.sh_size == 0) |
| 3187 | continue; |
| 3188 | |
| 3189 | sec->contents = imagebase + (sec->header.sh_addr - base); |
| 3190 | fseek(fp, sec->header.sh_offset, SEEK_SET); |
| 3191 | if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) { |
| 3192 | errorMsg("error reading ELF section data: %s\n", strerror(errno)); |
| 3193 | return 0; |
| 3194 | } |
| 3195 | |
| 3196 | } |
| 3197 | return 1; |
| 3198 | } |
| 3199 | #endif |
| 3200 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3201 | static void hide_special_symbols(struct obj_file *f) |
| 3202 | { |
| 3203 | static const char *const specials[] = { |
| 3204 | "cleanup_module", |
| 3205 | "init_module", |
| 3206 | "kernel_version", |
| 3207 | NULL |
| 3208 | }; |
| 3209 | |
| 3210 | struct obj_symbol *sym; |
| 3211 | const char *const *p; |
| 3212 | |
| 3213 | for (p = specials; *p; ++p) |
| 3214 | if ((sym = obj_find_symbol(f, *p)) != NULL) |
| 3215 | sym->info = |
| 3216 | ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info)); |
| 3217 | } |
| 3218 | |
| 3219 | |
| 3220 | |
| 3221 | extern int insmod_main( int argc, char **argv) |
| 3222 | { |
Eric Andersen | a18aaf1 | 2001-01-24 19:07:09 +0000 | [diff] [blame] | 3223 | int opt; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3224 | int k_crcs; |
| 3225 | int k_new_syscalls; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 3226 | int len; |
| 3227 | char *tmp; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3228 | unsigned long m_size; |
| 3229 | ElfW(Addr) m_addr; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 3230 | FILE *fp; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3231 | struct obj_file *f; |
Eric Andersen | 02b9f42 | 2001-02-15 19:07:43 +0000 | [diff] [blame] | 3232 | struct stat st; |
Eric Andersen | 14d3543 | 2001-05-14 17:07:32 +0000 | [diff] [blame] | 3233 | char m_name[FILENAME_MAX + 1] = "\0"; |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 3234 | int exit_status = EXIT_FAILURE; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3235 | int m_has_modinfo; |
| 3236 | #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING |
| 3237 | int k_version; |
| 3238 | char k_strversion[STRVERSIONLEN]; |
| 3239 | char m_strversion[STRVERSIONLEN]; |
| 3240 | int m_version; |
| 3241 | int m_crcs; |
| 3242 | #endif |
| 3243 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 3244 | /* Parse any options */ |
Eric Andersen | 155c89b | 2001-01-25 04:11:06 +0000 | [diff] [blame] | 3245 | while ((opt = getopt(argc, argv, "fkvxLo:")) > 0) { |
Eric Andersen | a18aaf1 | 2001-01-24 19:07:09 +0000 | [diff] [blame] | 3246 | switch (opt) { |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3247 | case 'f': /* force loading */ |
| 3248 | flag_force_load = 1; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 3249 | break; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3250 | case 'k': /* module loaded by kerneld, auto-cleanable */ |
| 3251 | flag_autoclean = 1; |
| 3252 | break; |
| 3253 | case 'v': /* verbose output */ |
| 3254 | flag_verbose = 1; |
| 3255 | break; |
| 3256 | case 'x': /* do not export externs */ |
| 3257 | flag_export = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 3258 | break; |
Eric Andersen | 155c89b | 2001-01-25 04:11:06 +0000 | [diff] [blame] | 3259 | case 'o': /* name the output module */ |
Eric Andersen | 14d3543 | 2001-05-14 17:07:32 +0000 | [diff] [blame] | 3260 | strncpy(m_name, optarg, FILENAME_MAX); |
Eric Andersen | 155c89b | 2001-01-25 04:11:06 +0000 | [diff] [blame] | 3261 | break; |
Eric Andersen | a18aaf1 | 2001-01-24 19:07:09 +0000 | [diff] [blame] | 3262 | case 'L': /* Stub warning */ |
| 3263 | /* This is needed for compatibility with modprobe. |
| 3264 | * In theory, this does locking, but we don't do |
| 3265 | * that. So be careful and plan your life around not |
| 3266 | * loading the same module 50 times concurrently. */ |
| 3267 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 3268 | default: |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 3269 | show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 3270 | } |
Erik Andersen | d387d01 | 1999-12-21 02:55:11 +0000 | [diff] [blame] | 3271 | } |
Eric Andersen | a18aaf1 | 2001-01-24 19:07:09 +0000 | [diff] [blame] | 3272 | |
| 3273 | if (argv[optind] == NULL) { |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 3274 | show_usage(); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3275 | } |
Eric Andersen | a18aaf1 | 2001-01-24 19:07:09 +0000 | [diff] [blame] | 3276 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 3277 | /* Grab the module name */ |
Eric Andersen | a18aaf1 | 2001-01-24 19:07:09 +0000 | [diff] [blame] | 3278 | if ((tmp = strrchr(argv[optind], '/')) != NULL) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 3279 | tmp++; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3280 | } else { |
Eric Andersen | a18aaf1 | 2001-01-24 19:07:09 +0000 | [diff] [blame] | 3281 | tmp = argv[optind]; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3282 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 3283 | len = strlen(tmp); |
| 3284 | |
| 3285 | if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') |
| 3286 | len -= 2; |
Eric Andersen | f227815 | 2001-04-24 21:41:41 +0000 | [diff] [blame] | 3287 | memcpy(m_fullName, tmp, len); |
| 3288 | m_fullName[len]='\0'; |
Eric Andersen | 114ad9c | 2001-01-26 01:52:14 +0000 | [diff] [blame] | 3289 | if (*m_name == '\0') { |
| 3290 | strcpy(m_name, m_fullName); |
Eric Andersen | 155c89b | 2001-01-25 04:11:06 +0000 | [diff] [blame] | 3291 | } |
Eric Andersen | 114ad9c | 2001-01-26 01:52:14 +0000 | [diff] [blame] | 3292 | strcat(m_fullName, ".o"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 3293 | |
Eric Andersen | 14d3543 | 2001-05-14 17:07:32 +0000 | [diff] [blame] | 3294 | /* 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] | 3295 | if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) || |
| 3296 | (fp = fopen(argv[optind], "r")) == NULL) { |
Eric Andersen | 14d3543 | 2001-05-14 17:07:32 +0000 | [diff] [blame] | 3297 | struct utsname myuname; |
| 3298 | |
| 3299 | /* Hmm. Could not open it. First search under /lib/modules/`uname -r`, |
| 3300 | * but do not error out yet if we fail to find it... */ |
| 3301 | if (uname(&myuname) == 0) { |
| 3302 | char module_dir[FILENAME_MAX]; |
Eric Andersen | 2416dfc | 2001-05-14 20:03:04 +0000 | [diff] [blame] | 3303 | char real_module_dir[FILENAME_MAX]; |
Eric Andersen | 14d3543 | 2001-05-14 17:07:32 +0000 | [diff] [blame] | 3304 | snprintf (module_dir, sizeof(module_dir), "%s/%s", |
| 3305 | _PATH_MODULES, myuname.release); |
Eric Andersen | 2416dfc | 2001-05-14 20:03:04 +0000 | [diff] [blame] | 3306 | /* Jump through hoops in case /lib/modules/`uname -r` |
| 3307 | * is a symlink. We do not want recursive_action to |
| 3308 | * follow symlinks, but we do want to follow the |
| 3309 | * /lib/modules/`uname -r` dir, So resolve it ourselves |
| 3310 | * if it is a link... */ |
| 3311 | if (realpath (module_dir, real_module_dir) == NULL) |
| 3312 | strcpy(real_module_dir, module_dir); |
| 3313 | recursive_action(real_module_dir, TRUE, FALSE, FALSE, |
Eric Andersen | 14d3543 | 2001-05-14 17:07:32 +0000 | [diff] [blame] | 3314 | check_module_name_match, 0, m_fullName); |
| 3315 | } |
| 3316 | |
| 3317 | /* Check if we have found anything yet */ |
| 3318 | if (m_filename[0] == '\0' || ((fp = fopen(m_filename, "r")) == NULL)) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3319 | { |
Eric Andersen | 2416dfc | 2001-05-14 20:03:04 +0000 | [diff] [blame] | 3320 | char module_dir[FILENAME_MAX]; |
| 3321 | if (realpath (_PATH_MODULES, module_dir) == NULL) |
| 3322 | strcpy(module_dir, _PATH_MODULES); |
Eric Andersen | 14d3543 | 2001-05-14 17:07:32 +0000 | [diff] [blame] | 3323 | /* No module found under /lib/modules/`uname -r`, this |
| 3324 | * time cast the net a bit wider. Search /lib/modules/ */ |
Eric Andersen | 2416dfc | 2001-05-14 20:03:04 +0000 | [diff] [blame] | 3325 | if (recursive_action(module_dir, TRUE, FALSE, FALSE, |
Eric Andersen | 14d3543 | 2001-05-14 17:07:32 +0000 | [diff] [blame] | 3326 | check_module_name_match, 0, m_fullName) == FALSE) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3327 | { |
Eric Andersen | 14d3543 | 2001-05-14 17:07:32 +0000 | [diff] [blame] | 3328 | if (m_filename[0] == '\0' |
| 3329 | || ((fp = fopen(m_filename, "r")) == NULL)) |
| 3330 | { |
| 3331 | error_msg("%s: no module by that name found", m_fullName); |
| 3332 | return EXIT_FAILURE; |
| 3333 | } |
| 3334 | } else |
| 3335 | error_msg_and_die("%s: no module by that name found", m_fullName); |
| 3336 | } |
| 3337 | } else |
Eric Andersen | f227815 | 2001-04-24 21:41:41 +0000 | [diff] [blame] | 3338 | safe_strncpy(m_filename, argv[optind], sizeof(m_filename)); |
Erik Andersen | d387d01 | 1999-12-21 02:55:11 +0000 | [diff] [blame] | 3339 | |
Eric Andersen | 14d3543 | 2001-05-14 17:07:32 +0000 | [diff] [blame] | 3340 | printf("Using %s\n", m_filename); |
Erik Andersen | d387d01 | 1999-12-21 02:55:11 +0000 | [diff] [blame] | 3341 | |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 3342 | if ((f = obj_load(fp, LOADBITS)) == NULL) |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 3343 | perror_msg_and_die("Could not load the module"); |
Erik Andersen | d387d01 | 1999-12-21 02:55:11 +0000 | [diff] [blame] | 3344 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3345 | if (get_modinfo_value(f, "kernel_version") == NULL) |
| 3346 | m_has_modinfo = 0; |
| 3347 | else |
| 3348 | m_has_modinfo = 1; |
| 3349 | |
| 3350 | #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING |
| 3351 | /* Version correspondence? */ |
| 3352 | |
| 3353 | k_version = get_kernel_version(k_strversion); |
| 3354 | if (m_has_modinfo) { |
| 3355 | m_version = new_get_module_version(f, m_strversion); |
| 3356 | } else { |
| 3357 | m_version = old_get_module_version(f, m_strversion); |
| 3358 | if (m_version == -1) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 3359 | error_msg("couldn't find the kernel version the module was " |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 3360 | "compiled for"); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3361 | goto out; |
| 3362 | } |
| 3363 | } |
| 3364 | |
| 3365 | if (strncmp(k_strversion, m_strversion, STRVERSIONLEN) != 0) { |
| 3366 | if (flag_force_load) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 3367 | error_msg("Warning: kernel-module version mismatch\n" |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3368 | "\t%s was compiled for kernel version %s\n" |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 3369 | "\twhile this kernel is version %s", |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3370 | m_filename, m_strversion, k_strversion); |
| 3371 | } else { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 3372 | error_msg("kernel-module version mismatch\n" |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3373 | "\t%s was compiled for kernel version %s\n" |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 3374 | "\twhile this kernel is version %s.", |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3375 | m_filename, m_strversion, k_strversion); |
| 3376 | goto out; |
| 3377 | } |
| 3378 | } |
| 3379 | k_crcs = 0; |
| 3380 | #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */ |
| 3381 | |
| 3382 | k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL); |
| 3383 | |
| 3384 | if (k_new_syscalls) { |
Eric Andersen | f5d5e77 | 2001-01-24 23:34:48 +0000 | [diff] [blame] | 3385 | #ifdef BB_FEATURE_NEW_MODULE_INTERFACE |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3386 | if (!new_get_kernel_symbols()) |
| 3387 | goto out; |
| 3388 | k_crcs = new_is_kernel_checksummed(); |
| 3389 | #else |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 3390 | error_msg("Not configured to support new kernels"); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3391 | goto out; |
| 3392 | #endif |
| 3393 | } else { |
Eric Andersen | f5d5e77 | 2001-01-24 23:34:48 +0000 | [diff] [blame] | 3394 | #ifdef BB_FEATURE_OLD_MODULE_INTERFACE |
Eric Andersen | 8c185f9 | 2000-09-22 00:38:07 +0000 | [diff] [blame] | 3395 | if (!old_get_kernel_symbols(m_name)) |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3396 | goto out; |
| 3397 | k_crcs = old_is_kernel_checksummed(); |
| 3398 | #else |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 3399 | error_msg("Not configured to support old kernels"); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3400 | goto out; |
| 3401 | #endif |
| 3402 | } |
| 3403 | |
| 3404 | #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING |
| 3405 | if (m_has_modinfo) |
| 3406 | m_crcs = new_is_module_checksummed(f); |
| 3407 | else |
| 3408 | m_crcs = old_is_module_checksummed(f); |
| 3409 | |
| 3410 | if (m_crcs != k_crcs) |
| 3411 | obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash); |
| 3412 | #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */ |
| 3413 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 3414 | /* Let the module know about the kernel symbols. */ |
| 3415 | add_kernel_symbols(f); |
| 3416 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3417 | /* Allocate common symbols, symbol tables, and string tables. */ |
| 3418 | |
| 3419 | if (k_new_syscalls |
| 3420 | ? !new_create_this_module(f, m_name) |
| 3421 | : !old_create_mod_use_count(f)) |
| 3422 | { |
| 3423 | goto out; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 3424 | } |
| 3425 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3426 | if (!obj_check_undefineds(f)) { |
| 3427 | goto out; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 3428 | } |
| 3429 | obj_allocate_commons(f); |
| 3430 | |
Eric Andersen | 86f3d5b | 2001-01-24 23:59:50 +0000 | [diff] [blame] | 3431 | /* done with the module name, on to the optional var=value arguments */ |
| 3432 | ++optind; |
| 3433 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3434 | if (optind < argc) { |
| 3435 | if (m_has_modinfo |
| 3436 | ? !new_process_module_arguments(f, argc - optind, argv + optind) |
| 3437 | : !old_process_module_arguments(f, argc - optind, argv + optind)) |
| 3438 | { |
| 3439 | goto out; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 3440 | } |
| 3441 | } |
| 3442 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3443 | arch_create_got(f); |
| 3444 | hide_special_symbols(f); |
| 3445 | |
| 3446 | if (k_new_syscalls) |
| 3447 | new_create_module_ksymtab(f); |
| 3448 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 3449 | /* Find current size of the module */ |
| 3450 | m_size = obj_load_size(f); |
Erik Andersen | d387d01 | 1999-12-21 02:55:11 +0000 | [diff] [blame] | 3451 | |
| 3452 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 3453 | m_addr = create_module(m_name, m_size); |
Eric Andersen | 86f3d5b | 2001-01-24 23:59:50 +0000 | [diff] [blame] | 3454 | if (m_addr==-1) switch (errno) { |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3455 | case EEXIST: |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 3456 | error_msg("A module named %s already exists", m_name); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3457 | goto out; |
| 3458 | case ENOMEM: |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 3459 | error_msg("Can't allocate kernel memory for module; needed %lu bytes", |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3460 | m_size); |
| 3461 | goto out; |
Erik Andersen | d387d01 | 1999-12-21 02:55:11 +0000 | [diff] [blame] | 3462 | default: |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 3463 | perror_msg("create_module: %s", m_name); |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3464 | goto out; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 3465 | } |
Erik Andersen | d387d01 | 1999-12-21 02:55:11 +0000 | [diff] [blame] | 3466 | |
Eric Andersen | 8ae319a | 2001-05-21 16:09:18 +0000 | [diff] [blame] | 3467 | #if !LOADBITS |
| 3468 | /* |
| 3469 | * the PROGBITS section was not loaded by the obj_load |
| 3470 | * now we can load them directly into the kernel memory |
| 3471 | */ |
| 3472 | // f->imagebase = (char*) m_addr; |
| 3473 | f->imagebase = (ElfW(Addr)) m_addr; |
| 3474 | if (!obj_load_progbits(fp, f)) { |
| 3475 | delete_module(m_name); |
| 3476 | goto out; |
| 3477 | } |
| 3478 | #endif |
| 3479 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3480 | if (!obj_relocate(f, m_addr)) { |
| 3481 | delete_module(m_name); |
| 3482 | goto out; |
| 3483 | } |
Erik Andersen | d387d01 | 1999-12-21 02:55:11 +0000 | [diff] [blame] | 3484 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3485 | if (k_new_syscalls |
| 3486 | ? !new_init_module(m_name, f, m_size) |
| 3487 | : !old_init_module(m_name, f, m_size)) |
| 3488 | { |
| 3489 | delete_module(m_name); |
| 3490 | goto out; |
| 3491 | } |
| 3492 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 3493 | exit_status = EXIT_SUCCESS; |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 3494 | |
| 3495 | out: |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 3496 | fclose(fp); |
Eric Andersen | bb245ba | 2000-06-19 19:53:30 +0000 | [diff] [blame] | 3497 | return(exit_status); |
Erik Andersen | 0210432 | 1999-12-17 18:57:34 +0000 | [diff] [blame] | 3498 | } |