xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Machine-dependent ELF dynamic relocation inline functions. i386 version. |
| 2 | Copyright (C) 1995-2016 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #ifndef dl_machine_h |
| 20 | #define dl_machine_h |
| 21 | |
| 22 | #define ELF_MACHINE_NAME "i386" |
| 23 | |
| 24 | #include <sys/param.h> |
| 25 | #include <sysdep.h> |
| 26 | #include <tls.h> |
| 27 | #include <dl-tlsdesc.h> |
| 28 | #include <cpu-features.c> |
| 29 | |
| 30 | /* Return nonzero iff ELF header is compatible with the running host. */ |
| 31 | static inline int __attribute__ ((unused)) |
| 32 | elf_machine_matches_host (const Elf32_Ehdr *ehdr) |
| 33 | { |
| 34 | return ehdr->e_machine == EM_386; |
| 35 | } |
| 36 | |
| 37 | |
| 38 | /* Return the link-time address of _DYNAMIC. Conveniently, this is the |
| 39 | first element of the GOT, a special entry that is never relocated. */ |
| 40 | static inline Elf32_Addr __attribute__ ((unused, const)) |
| 41 | elf_machine_dynamic (void) |
| 42 | { |
| 43 | /* This produces a GOTOFF reloc that resolves to zero at link time, so in |
| 44 | fact just loads from the GOT register directly. By doing it without |
| 45 | an asm we can let the compiler choose any register. */ |
| 46 | extern const Elf32_Addr _GLOBAL_OFFSET_TABLE_[] attribute_hidden; |
| 47 | return _GLOBAL_OFFSET_TABLE_[0]; |
| 48 | } |
| 49 | |
| 50 | /* Return the run-time load address of the shared object. */ |
| 51 | static inline Elf32_Addr __attribute__ ((unused)) |
| 52 | elf_machine_load_address (void) |
| 53 | { |
| 54 | /* Compute the difference between the runtime address of _DYNAMIC as seen |
| 55 | by a GOTOFF reference, and the link-time address found in the special |
| 56 | unrelocated first GOT entry. */ |
| 57 | extern Elf32_Dyn bygotoff[] asm ("_DYNAMIC") attribute_hidden; |
| 58 | return (Elf32_Addr) &bygotoff - elf_machine_dynamic (); |
| 59 | } |
| 60 | |
| 61 | /* Set up the loaded object described by L so its unrelocated PLT |
| 62 | entries will jump to the on-demand fixup code in dl-runtime.c. */ |
| 63 | |
| 64 | static inline int __attribute__ ((unused, always_inline)) |
| 65 | elf_machine_runtime_setup (struct link_map *l, int lazy, int profile) |
| 66 | { |
| 67 | Elf32_Addr *got; |
| 68 | extern void _dl_runtime_resolve (Elf32_Word) attribute_hidden; |
| 69 | extern void _dl_runtime_profile (Elf32_Word) attribute_hidden; |
| 70 | |
| 71 | if (l->l_info[DT_JMPREL] && lazy) |
| 72 | { |
| 73 | /* The GOT entries for functions in the PLT have not yet been filled |
| 74 | in. Their initial contents will arrange when called to push an |
| 75 | offset into the .rel.plt section, push _GLOBAL_OFFSET_TABLE_[1], |
| 76 | and then jump to _GLOBAL_OFFSET_TABLE[2]. */ |
| 77 | got = (Elf32_Addr *) D_PTR (l, l_info[DT_PLTGOT]); |
| 78 | /* If a library is prelinked but we have to relocate anyway, |
| 79 | we have to be able to undo the prelinking of .got.plt. |
| 80 | The prelinker saved us here address of .plt + 0x16. */ |
| 81 | if (got[1]) |
| 82 | { |
| 83 | l->l_mach.plt = got[1] + l->l_addr; |
| 84 | l->l_mach.gotplt = (Elf32_Addr) &got[3]; |
| 85 | } |
| 86 | got[1] = (Elf32_Addr) l; /* Identify this shared object. */ |
| 87 | |
| 88 | /* The got[2] entry contains the address of a function which gets |
| 89 | called to get the address of a so far unresolved function and |
| 90 | jump to it. The profiling extension of the dynamic linker allows |
| 91 | to intercept the calls to collect information. In this case we |
| 92 | don't store the address in the GOT so that all future calls also |
| 93 | end in this function. */ |
| 94 | if (__glibc_unlikely (profile)) |
| 95 | { |
| 96 | got[2] = (Elf32_Addr) &_dl_runtime_profile; |
| 97 | |
| 98 | if (GLRO(dl_profile) != NULL |
| 99 | && _dl_name_match_p (GLRO(dl_profile), l)) |
| 100 | /* This is the object we are looking for. Say that we really |
| 101 | want profiling and the timers are started. */ |
| 102 | GL(dl_profile_map) = l; |
| 103 | } |
| 104 | else |
| 105 | /* This function will get called to fix up the GOT entry indicated by |
| 106 | the offset on the stack, and then jump to the resolved address. */ |
| 107 | got[2] = (Elf32_Addr) &_dl_runtime_resolve; |
| 108 | } |
| 109 | |
| 110 | return lazy; |
| 111 | } |
| 112 | |
| 113 | #ifdef IN_DL_RUNTIME |
| 114 | |
| 115 | # ifndef PROF |
| 116 | /* We add a declaration of this function here so that in dl-runtime.c |
| 117 | the ELF_MACHINE_RUNTIME_TRAMPOLINE macro really can pass the parameters |
| 118 | in registers. |
| 119 | |
| 120 | We cannot use this scheme for profiling because the _mcount call |
| 121 | destroys the passed register information. */ |
| 122 | #define ARCH_FIXUP_ATTRIBUTE __attribute__ ((regparm (3), stdcall, unused)) |
| 123 | |
| 124 | extern ElfW(Addr) _dl_fixup (struct link_map *l, |
| 125 | ElfW(Word) reloc_offset) |
| 126 | ARCH_FIXUP_ATTRIBUTE; |
| 127 | extern ElfW(Addr) _dl_profile_fixup (struct link_map *l, |
| 128 | ElfW(Word) reloc_offset, |
| 129 | ElfW(Addr) retaddr, void *regs, |
| 130 | long int *framesizep) |
| 131 | ARCH_FIXUP_ATTRIBUTE; |
| 132 | # endif |
| 133 | |
| 134 | #endif |
| 135 | |
| 136 | /* Mask identifying addresses reserved for the user program, |
| 137 | where the dynamic linker should not map anything. */ |
| 138 | #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL |
| 139 | |
| 140 | /* Initial entry point code for the dynamic linker. |
| 141 | The C function `_dl_start' is the real entry point; |
| 142 | its return value is the user program's entry point. */ |
| 143 | |
| 144 | #define RTLD_START asm ("\n\ |
| 145 | .text\n\ |
| 146 | .align 16\n\ |
| 147 | 0: movl (%esp), %ebx\n\ |
| 148 | ret\n\ |
| 149 | .align 16\n\ |
| 150 | .globl _start\n\ |
| 151 | .globl _dl_start_user\n\ |
| 152 | _start:\n\ |
| 153 | # Note that _dl_start gets the parameter in %eax.\n\ |
| 154 | movl %esp, %eax\n\ |
| 155 | call _dl_start\n\ |
| 156 | _dl_start_user:\n\ |
| 157 | # Save the user entry point address in %edi.\n\ |
| 158 | movl %eax, %edi\n\ |
| 159 | # Point %ebx at the GOT.\n\ |
| 160 | call 0b\n\ |
| 161 | addl $_GLOBAL_OFFSET_TABLE_, %ebx\n\ |
| 162 | # See if we were run as a command with the executable file\n\ |
| 163 | # name as an extra leading argument.\n\ |
| 164 | movl _dl_skip_args@GOTOFF(%ebx), %eax\n\ |
| 165 | # Pop the original argument count.\n\ |
| 166 | popl %edx\n\ |
| 167 | # Adjust the stack pointer to skip _dl_skip_args words.\n\ |
| 168 | leal (%esp,%eax,4), %esp\n\ |
| 169 | # Subtract _dl_skip_args from argc.\n\ |
| 170 | subl %eax, %edx\n\ |
| 171 | # Push argc back on the stack.\n\ |
| 172 | push %edx\n\ |
| 173 | # The special initializer gets called with the stack just\n\ |
| 174 | # as the application's entry point will see it; it can\n\ |
| 175 | # switch stacks if it moves these contents over.\n\ |
| 176 | " RTLD_START_SPECIAL_INIT "\n\ |
| 177 | # Load the parameters again.\n\ |
| 178 | # (eax, edx, ecx, *--esp) = (_dl_loaded, argc, argv, envp)\n\ |
| 179 | movl _rtld_local@GOTOFF(%ebx), %eax\n\ |
| 180 | leal 8(%esp,%edx,4), %esi\n\ |
| 181 | leal 4(%esp), %ecx\n\ |
| 182 | movl %esp, %ebp\n\ |
| 183 | # Make sure _dl_init is run with 16 byte aligned stack.\n\ |
| 184 | andl $-16, %esp\n\ |
| 185 | pushl %eax\n\ |
| 186 | pushl %eax\n\ |
| 187 | pushl %ebp\n\ |
| 188 | pushl %esi\n\ |
| 189 | # Clear %ebp, so that even constructors have terminated backchain.\n\ |
| 190 | xorl %ebp, %ebp\n\ |
| 191 | # Call the function to run the initializers.\n\ |
| 192 | call _dl_init\n\ |
| 193 | # Pass our finalizer function to the user in %edx, as per ELF ABI.\n\ |
| 194 | leal _dl_fini@GOTOFF(%ebx), %edx\n\ |
| 195 | # Restore %esp _start expects.\n\ |
| 196 | movl (%esp), %esp\n\ |
| 197 | # Jump to the user's entry point.\n\ |
| 198 | jmp *%edi\n\ |
| 199 | .previous\n\ |
| 200 | "); |
| 201 | |
| 202 | #ifndef RTLD_START_SPECIAL_INIT |
| 203 | # define RTLD_START_SPECIAL_INIT /* nothing */ |
| 204 | #endif |
| 205 | |
| 206 | /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or |
| 207 | TLS variable, so undefined references should not be allowed to |
| 208 | define the value. |
| 209 | ELF_RTYPE_CLASS_COPY iff TYPE should not be allowed to resolve to one |
| 210 | of the main executable's symbols, as for a COPY reloc. |
| 211 | ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA iff TYPE describes relocation may |
| 212 | against protected data whose address be external due to copy relocation. |
| 213 | */ |
| 214 | # define elf_machine_type_class(type) \ |
| 215 | ((((type) == R_386_JMP_SLOT || (type) == R_386_TLS_DTPMOD32 \ |
| 216 | || (type) == R_386_TLS_DTPOFF32 || (type) == R_386_TLS_TPOFF32 \ |
| 217 | || (type) == R_386_TLS_TPOFF || (type) == R_386_TLS_DESC) \ |
| 218 | * ELF_RTYPE_CLASS_PLT) \ |
| 219 | | (((type) == R_386_COPY) * ELF_RTYPE_CLASS_COPY) \ |
| 220 | | (((type) == R_386_GLOB_DAT) * ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA)) |
| 221 | |
| 222 | /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */ |
| 223 | #define ELF_MACHINE_JMP_SLOT R_386_JMP_SLOT |
| 224 | |
| 225 | /* The i386 never uses Elf32_Rela relocations for the dynamic linker. |
| 226 | Prelinked libraries may use Elf32_Rela though. */ |
| 227 | #define ELF_MACHINE_PLT_REL 1 |
| 228 | |
| 229 | /* We define an initialization functions. This is called very early in |
| 230 | _dl_sysdep_start. */ |
| 231 | #define DL_PLATFORM_INIT dl_platform_init () |
| 232 | |
| 233 | static inline void __attribute__ ((unused)) |
| 234 | dl_platform_init (void) |
| 235 | { |
| 236 | if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0') |
| 237 | /* Avoid an empty string which would disturb us. */ |
| 238 | GLRO(dl_platform) = NULL; |
| 239 | |
| 240 | init_cpu_features (&GLRO(dl_x86_cpu_features)); |
| 241 | } |
| 242 | |
| 243 | static inline Elf32_Addr |
| 244 | elf_machine_fixup_plt (struct link_map *map, lookup_t t, |
| 245 | const Elf32_Rel *reloc, |
| 246 | Elf32_Addr *reloc_addr, Elf32_Addr value) |
| 247 | { |
| 248 | return *reloc_addr = value; |
| 249 | } |
| 250 | |
| 251 | /* Return the final value of a plt relocation. */ |
| 252 | static inline Elf32_Addr |
| 253 | elf_machine_plt_value (struct link_map *map, const Elf32_Rel *reloc, |
| 254 | Elf32_Addr value) |
| 255 | { |
| 256 | return value; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | /* Names of the architecture-specific auditing callback functions. */ |
| 261 | #define ARCH_LA_PLTENTER i86_gnu_pltenter |
| 262 | #define ARCH_LA_PLTEXIT i86_gnu_pltexit |
| 263 | |
| 264 | #endif /* !dl_machine_h */ |
| 265 | |
| 266 | /* The i386 never uses Elf32_Rela relocations for the dynamic linker. |
| 267 | Prelinked libraries may use Elf32_Rela though. */ |
| 268 | #define ELF_MACHINE_NO_RELA defined RTLD_BOOTSTRAP |
| 269 | #define ELF_MACHINE_NO_REL 0 |
| 270 | |
| 271 | #ifdef RESOLVE_MAP |
| 272 | |
| 273 | /* Perform the relocation specified by RELOC and SYM (which is fully resolved). |
| 274 | MAP is the object containing the reloc. */ |
| 275 | |
| 276 | auto inline void |
| 277 | __attribute ((always_inline)) |
| 278 | elf_machine_rel (struct link_map *map, const Elf32_Rel *reloc, |
| 279 | const Elf32_Sym *sym, const struct r_found_version *version, |
| 280 | void *const reloc_addr_arg, int skip_ifunc) |
| 281 | { |
| 282 | Elf32_Addr *const reloc_addr = reloc_addr_arg; |
| 283 | const unsigned int r_type = ELF32_R_TYPE (reloc->r_info); |
| 284 | |
| 285 | # if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC |
| 286 | if (__glibc_unlikely (r_type == R_386_RELATIVE)) |
| 287 | { |
| 288 | # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC |
| 289 | /* This is defined in rtld.c, but nowhere in the static libc.a; |
| 290 | make the reference weak so static programs can still link. |
| 291 | This declaration cannot be done when compiling rtld.c |
| 292 | (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the |
| 293 | common defn for _dl_rtld_map, which is incompatible with a |
| 294 | weak decl in the same file. */ |
| 295 | # ifndef SHARED |
| 296 | weak_extern (_dl_rtld_map); |
| 297 | # endif |
| 298 | if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */ |
| 299 | # endif |
| 300 | *reloc_addr += map->l_addr; |
| 301 | } |
| 302 | # ifndef RTLD_BOOTSTRAP |
| 303 | else if (__glibc_unlikely (r_type == R_386_NONE)) |
| 304 | return; |
| 305 | # endif |
| 306 | else |
| 307 | # endif /* !RTLD_BOOTSTRAP and have no -z combreloc */ |
| 308 | { |
| 309 | # ifndef RTLD_BOOTSTRAP |
| 310 | const Elf32_Sym *const refsym = sym; |
| 311 | # endif |
| 312 | struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type); |
| 313 | Elf32_Addr value = sym_map == NULL ? 0 : sym_map->l_addr + sym->st_value; |
| 314 | |
| 315 | if (sym != NULL |
| 316 | && __builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC, |
| 317 | 0) |
| 318 | && __builtin_expect (sym->st_shndx != SHN_UNDEF, 1) |
| 319 | && __builtin_expect (!skip_ifunc, 1)) |
| 320 | value = ((Elf32_Addr (*) (void)) value) (); |
| 321 | |
| 322 | switch (r_type) |
| 323 | { |
| 324 | # ifndef RTLD_BOOTSTRAP |
| 325 | case R_386_SIZE32: |
| 326 | /* Set to symbol size plus addend. */ |
| 327 | *reloc_addr += sym->st_size; |
| 328 | break; |
| 329 | # endif |
| 330 | case R_386_GLOB_DAT: |
| 331 | case R_386_JMP_SLOT: |
| 332 | *reloc_addr = value; |
| 333 | break; |
| 334 | |
| 335 | case R_386_TLS_DTPMOD32: |
| 336 | # ifdef RTLD_BOOTSTRAP |
| 337 | /* During startup the dynamic linker is always the module |
| 338 | with index 1. |
| 339 | XXX If this relocation is necessary move before RESOLVE |
| 340 | call. */ |
| 341 | *reloc_addr = 1; |
| 342 | # else |
| 343 | /* Get the information from the link map returned by the |
| 344 | resolv function. */ |
| 345 | if (sym_map != NULL) |
| 346 | *reloc_addr = sym_map->l_tls_modid; |
| 347 | # endif |
| 348 | break; |
| 349 | case R_386_TLS_DTPOFF32: |
| 350 | # ifndef RTLD_BOOTSTRAP |
| 351 | /* During relocation all TLS symbols are defined and used. |
| 352 | Therefore the offset is already correct. */ |
| 353 | if (sym != NULL) |
| 354 | *reloc_addr = sym->st_value; |
| 355 | # endif |
| 356 | break; |
| 357 | case R_386_TLS_DESC: |
| 358 | { |
| 359 | struct tlsdesc volatile *td = |
| 360 | (struct tlsdesc volatile *)reloc_addr; |
| 361 | |
| 362 | # ifndef RTLD_BOOTSTRAP |
| 363 | if (! sym) |
| 364 | td->entry = _dl_tlsdesc_undefweak; |
| 365 | else |
| 366 | # endif |
| 367 | { |
| 368 | # ifndef RTLD_BOOTSTRAP |
| 369 | # ifndef SHARED |
| 370 | CHECK_STATIC_TLS (map, sym_map); |
| 371 | # else |
| 372 | if (!TRY_STATIC_TLS (map, sym_map)) |
| 373 | { |
| 374 | td->arg = _dl_make_tlsdesc_dynamic |
| 375 | (sym_map, sym->st_value + (ElfW(Word))td->arg); |
| 376 | td->entry = _dl_tlsdesc_dynamic; |
| 377 | } |
| 378 | else |
| 379 | # endif |
| 380 | # endif |
| 381 | { |
| 382 | td->arg = (void*)(sym->st_value - sym_map->l_tls_offset |
| 383 | + (ElfW(Word))td->arg); |
| 384 | td->entry = _dl_tlsdesc_return; |
| 385 | } |
| 386 | } |
| 387 | break; |
| 388 | } |
| 389 | case R_386_TLS_TPOFF32: |
| 390 | /* The offset is positive, backward from the thread pointer. */ |
| 391 | # ifdef RTLD_BOOTSTRAP |
| 392 | *reloc_addr += map->l_tls_offset - sym->st_value; |
| 393 | # else |
| 394 | /* We know the offset of object the symbol is contained in. |
| 395 | It is a positive value which will be subtracted from the |
| 396 | thread pointer. To get the variable position in the TLS |
| 397 | block we subtract the offset from that of the TLS block. */ |
| 398 | if (sym != NULL) |
| 399 | { |
| 400 | CHECK_STATIC_TLS (map, sym_map); |
| 401 | *reloc_addr += sym_map->l_tls_offset - sym->st_value; |
| 402 | } |
| 403 | # endif |
| 404 | break; |
| 405 | case R_386_TLS_TPOFF: |
| 406 | /* The offset is negative, forward from the thread pointer. */ |
| 407 | # ifdef RTLD_BOOTSTRAP |
| 408 | *reloc_addr += sym->st_value - map->l_tls_offset; |
| 409 | # else |
| 410 | /* We know the offset of object the symbol is contained in. |
| 411 | It is a negative value which will be added to the |
| 412 | thread pointer. */ |
| 413 | if (sym != NULL) |
| 414 | { |
| 415 | CHECK_STATIC_TLS (map, sym_map); |
| 416 | *reloc_addr += sym->st_value - sym_map->l_tls_offset; |
| 417 | } |
| 418 | # endif |
| 419 | break; |
| 420 | |
| 421 | # ifndef RTLD_BOOTSTRAP |
| 422 | case R_386_32: |
| 423 | *reloc_addr += value; |
| 424 | break; |
| 425 | case R_386_PC32: |
| 426 | *reloc_addr += (value - (Elf32_Addr) reloc_addr); |
| 427 | break; |
| 428 | case R_386_COPY: |
| 429 | if (sym == NULL) |
| 430 | /* This can happen in trace mode if an object could not be |
| 431 | found. */ |
| 432 | break; |
| 433 | if (__builtin_expect (sym->st_size > refsym->st_size, 0) |
| 434 | || (__builtin_expect (sym->st_size < refsym->st_size, 0) |
| 435 | && GLRO(dl_verbose))) |
| 436 | { |
| 437 | const char *strtab; |
| 438 | |
| 439 | strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]); |
| 440 | _dl_error_printf ("\ |
| 441 | %s: Symbol `%s' has different size in shared object, consider re-linking\n", |
| 442 | RTLD_PROGNAME, strtab + refsym->st_name); |
| 443 | } |
| 444 | memcpy (reloc_addr_arg, (void *) value, |
| 445 | MIN (sym->st_size, refsym->st_size)); |
| 446 | break; |
| 447 | case R_386_IRELATIVE: |
| 448 | value = map->l_addr + *reloc_addr; |
| 449 | value = ((Elf32_Addr (*) (void)) value) (); |
| 450 | *reloc_addr = value; |
| 451 | break; |
| 452 | default: |
| 453 | _dl_reloc_bad_type (map, r_type, 0); |
| 454 | break; |
| 455 | # endif /* !RTLD_BOOTSTRAP */ |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | # ifndef RTLD_BOOTSTRAP |
| 461 | auto inline void |
| 462 | __attribute__ ((always_inline)) |
| 463 | elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc, |
| 464 | const Elf32_Sym *sym, const struct r_found_version *version, |
| 465 | void *const reloc_addr_arg, int skip_ifunc) |
| 466 | { |
| 467 | Elf32_Addr *const reloc_addr = reloc_addr_arg; |
| 468 | const unsigned int r_type = ELF32_R_TYPE (reloc->r_info); |
| 469 | |
| 470 | if (ELF32_R_TYPE (reloc->r_info) == R_386_RELATIVE) |
| 471 | *reloc_addr = map->l_addr + reloc->r_addend; |
| 472 | else if (r_type != R_386_NONE) |
| 473 | { |
| 474 | # ifndef RESOLVE_CONFLICT_FIND_MAP |
| 475 | const Elf32_Sym *const refsym = sym; |
| 476 | # endif |
| 477 | struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type); |
| 478 | Elf32_Addr value = sym == NULL ? 0 : sym_map->l_addr + sym->st_value; |
| 479 | |
| 480 | if (sym != NULL |
| 481 | && __builtin_expect (sym->st_shndx != SHN_UNDEF, 1) |
| 482 | && __builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC, 0) |
| 483 | && __builtin_expect (!skip_ifunc, 1)) |
| 484 | value = ((Elf32_Addr (*) (void)) value) (); |
| 485 | |
| 486 | switch (ELF32_R_TYPE (reloc->r_info)) |
| 487 | { |
| 488 | case R_386_SIZE32: |
| 489 | /* Set to symbol size plus addend. */ |
| 490 | value = sym->st_size; |
| 491 | case R_386_GLOB_DAT: |
| 492 | case R_386_JMP_SLOT: |
| 493 | case R_386_32: |
| 494 | *reloc_addr = value + reloc->r_addend; |
| 495 | break; |
| 496 | # ifndef RESOLVE_CONFLICT_FIND_MAP |
| 497 | /* Not needed for dl-conflict.c. */ |
| 498 | case R_386_PC32: |
| 499 | *reloc_addr = (value + reloc->r_addend - (Elf32_Addr) reloc_addr); |
| 500 | break; |
| 501 | |
| 502 | case R_386_TLS_DTPMOD32: |
| 503 | /* Get the information from the link map returned by the |
| 504 | resolv function. */ |
| 505 | if (sym_map != NULL) |
| 506 | *reloc_addr = sym_map->l_tls_modid; |
| 507 | break; |
| 508 | case R_386_TLS_DTPOFF32: |
| 509 | /* During relocation all TLS symbols are defined and used. |
| 510 | Therefore the offset is already correct. */ |
| 511 | *reloc_addr = (sym == NULL ? 0 : sym->st_value) + reloc->r_addend; |
| 512 | break; |
| 513 | case R_386_TLS_DESC: |
| 514 | { |
| 515 | struct tlsdesc volatile *td = |
| 516 | (struct tlsdesc volatile *)reloc_addr; |
| 517 | |
| 518 | # ifndef RTLD_BOOTSTRAP |
| 519 | if (!sym) |
| 520 | { |
| 521 | td->arg = (void*)reloc->r_addend; |
| 522 | td->entry = _dl_tlsdesc_undefweak; |
| 523 | } |
| 524 | else |
| 525 | # endif |
| 526 | { |
| 527 | # ifndef RTLD_BOOTSTRAP |
| 528 | # ifndef SHARED |
| 529 | CHECK_STATIC_TLS (map, sym_map); |
| 530 | # else |
| 531 | if (!TRY_STATIC_TLS (map, sym_map)) |
| 532 | { |
| 533 | td->arg = _dl_make_tlsdesc_dynamic |
| 534 | (sym_map, sym->st_value + reloc->r_addend); |
| 535 | td->entry = _dl_tlsdesc_dynamic; |
| 536 | } |
| 537 | else |
| 538 | # endif |
| 539 | # endif |
| 540 | { |
| 541 | td->arg = (void*)(sym->st_value - sym_map->l_tls_offset |
| 542 | + reloc->r_addend); |
| 543 | td->entry = _dl_tlsdesc_return; |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | break; |
| 548 | case R_386_TLS_TPOFF32: |
| 549 | /* The offset is positive, backward from the thread pointer. */ |
| 550 | /* We know the offset of object the symbol is contained in. |
| 551 | It is a positive value which will be subtracted from the |
| 552 | thread pointer. To get the variable position in the TLS |
| 553 | block we subtract the offset from that of the TLS block. */ |
| 554 | if (sym != NULL) |
| 555 | { |
| 556 | CHECK_STATIC_TLS (map, sym_map); |
| 557 | *reloc_addr = sym_map->l_tls_offset - sym->st_value |
| 558 | + reloc->r_addend; |
| 559 | } |
| 560 | break; |
| 561 | case R_386_TLS_TPOFF: |
| 562 | /* The offset is negative, forward from the thread pointer. */ |
| 563 | /* We know the offset of object the symbol is contained in. |
| 564 | It is a negative value which will be added to the |
| 565 | thread pointer. */ |
| 566 | if (sym != NULL) |
| 567 | { |
| 568 | CHECK_STATIC_TLS (map, sym_map); |
| 569 | *reloc_addr = sym->st_value - sym_map->l_tls_offset |
| 570 | + reloc->r_addend; |
| 571 | } |
| 572 | break; |
| 573 | case R_386_COPY: |
| 574 | if (sym == NULL) |
| 575 | /* This can happen in trace mode if an object could not be |
| 576 | found. */ |
| 577 | break; |
| 578 | if (__builtin_expect (sym->st_size > refsym->st_size, 0) |
| 579 | || (__builtin_expect (sym->st_size < refsym->st_size, 0) |
| 580 | && GLRO(dl_verbose))) |
| 581 | { |
| 582 | const char *strtab; |
| 583 | |
| 584 | strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]); |
| 585 | _dl_error_printf ("\ |
| 586 | %s: Symbol `%s' has different size in shared object, consider re-linking\n", |
| 587 | RTLD_PROGNAME, strtab + refsym->st_name); |
| 588 | } |
| 589 | memcpy (reloc_addr_arg, (void *) value, |
| 590 | MIN (sym->st_size, refsym->st_size)); |
| 591 | break; |
| 592 | # endif /* !RESOLVE_CONFLICT_FIND_MAP */ |
| 593 | case R_386_IRELATIVE: |
| 594 | value = map->l_addr + reloc->r_addend; |
| 595 | value = ((Elf32_Addr (*) (void)) value) (); |
| 596 | *reloc_addr = value; |
| 597 | break; |
| 598 | default: |
| 599 | /* We add these checks in the version to relocate ld.so only |
| 600 | if we are still debugging. */ |
| 601 | _dl_reloc_bad_type (map, r_type, 0); |
| 602 | break; |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | # endif /* !RTLD_BOOTSTRAP */ |
| 607 | |
| 608 | auto inline void |
| 609 | __attribute ((always_inline)) |
| 610 | elf_machine_rel_relative (Elf32_Addr l_addr, const Elf32_Rel *reloc, |
| 611 | void *const reloc_addr_arg) |
| 612 | { |
| 613 | Elf32_Addr *const reloc_addr = reloc_addr_arg; |
| 614 | assert (ELF32_R_TYPE (reloc->r_info) == R_386_RELATIVE); |
| 615 | *reloc_addr += l_addr; |
| 616 | } |
| 617 | |
| 618 | # ifndef RTLD_BOOTSTRAP |
| 619 | auto inline void |
| 620 | __attribute__ ((always_inline)) |
| 621 | elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc, |
| 622 | void *const reloc_addr_arg) |
| 623 | { |
| 624 | Elf32_Addr *const reloc_addr = reloc_addr_arg; |
| 625 | *reloc_addr = l_addr + reloc->r_addend; |
| 626 | } |
| 627 | # endif /* !RTLD_BOOTSTRAP */ |
| 628 | |
| 629 | auto inline void |
| 630 | __attribute__ ((always_inline)) |
| 631 | elf_machine_lazy_rel (struct link_map *map, |
| 632 | Elf32_Addr l_addr, const Elf32_Rel *reloc, |
| 633 | int skip_ifunc) |
| 634 | { |
| 635 | Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset); |
| 636 | const unsigned int r_type = ELF32_R_TYPE (reloc->r_info); |
| 637 | /* Check for unexpected PLT reloc type. */ |
| 638 | if (__glibc_likely (r_type == R_386_JMP_SLOT)) |
| 639 | { |
| 640 | if (__builtin_expect (map->l_mach.plt, 0) == 0) |
| 641 | *reloc_addr += l_addr; |
| 642 | else |
| 643 | *reloc_addr = (map->l_mach.plt |
| 644 | + (((Elf32_Addr) reloc_addr) - map->l_mach.gotplt) * 4); |
| 645 | } |
| 646 | else if (__glibc_likely (r_type == R_386_TLS_DESC)) |
| 647 | { |
| 648 | struct tlsdesc volatile * __attribute__((__unused__)) td = |
| 649 | (struct tlsdesc volatile *)reloc_addr; |
| 650 | |
| 651 | /* Handle relocations that reference the local *ABS* in a simple |
| 652 | way, so as to preserve a potential addend. */ |
| 653 | if (ELF32_R_SYM (reloc->r_info) == 0) |
| 654 | td->entry = _dl_tlsdesc_resolve_abs_plus_addend; |
| 655 | /* Given a known-zero addend, we can store a pointer to the |
| 656 | reloc in the arg position. */ |
| 657 | else if (td->arg == 0) |
| 658 | { |
| 659 | td->arg = (void*)reloc; |
| 660 | td->entry = _dl_tlsdesc_resolve_rel; |
| 661 | } |
| 662 | else |
| 663 | { |
| 664 | /* We could handle non-*ABS* relocations with non-zero addends |
| 665 | by allocating dynamically an arg to hold a pointer to the |
| 666 | reloc, but that sounds pointless. */ |
| 667 | const Elf32_Rel *const r = reloc; |
| 668 | /* The code below was borrowed from elf_dynamic_do_rel(). */ |
| 669 | const ElfW(Sym) *const symtab = |
| 670 | (const void *) D_PTR (map, l_info[DT_SYMTAB]); |
| 671 | |
| 672 | # ifdef RTLD_BOOTSTRAP |
| 673 | /* The dynamic linker always uses versioning. */ |
| 674 | assert (map->l_info[VERSYMIDX (DT_VERSYM)] != NULL); |
| 675 | # else |
| 676 | if (map->l_info[VERSYMIDX (DT_VERSYM)]) |
| 677 | # endif |
| 678 | { |
| 679 | const ElfW(Half) *const version = |
| 680 | (const void *) D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]); |
| 681 | ElfW(Half) ndx = version[ELFW(R_SYM) (r->r_info)] & 0x7fff; |
| 682 | elf_machine_rel (map, r, &symtab[ELFW(R_SYM) (r->r_info)], |
| 683 | &map->l_versions[ndx], |
| 684 | (void *) (l_addr + r->r_offset), skip_ifunc); |
| 685 | } |
| 686 | # ifndef RTLD_BOOTSTRAP |
| 687 | else |
| 688 | elf_machine_rel (map, r, &symtab[ELFW(R_SYM) (r->r_info)], NULL, |
| 689 | (void *) (l_addr + r->r_offset), skip_ifunc); |
| 690 | # endif |
| 691 | } |
| 692 | } |
| 693 | else if (__glibc_unlikely (r_type == R_386_IRELATIVE)) |
| 694 | { |
| 695 | Elf32_Addr value = map->l_addr + *reloc_addr; |
| 696 | if (__glibc_likely (!skip_ifunc)) |
| 697 | value = ((Elf32_Addr (*) (void)) value) (); |
| 698 | *reloc_addr = value; |
| 699 | } |
| 700 | else |
| 701 | _dl_reloc_bad_type (map, r_type, 1); |
| 702 | } |
| 703 | |
| 704 | # ifndef RTLD_BOOTSTRAP |
| 705 | |
| 706 | auto inline void |
| 707 | __attribute__ ((always_inline)) |
| 708 | elf_machine_lazy_rela (struct link_map *map, |
| 709 | Elf32_Addr l_addr, const Elf32_Rela *reloc, |
| 710 | int skip_ifunc) |
| 711 | { |
| 712 | Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset); |
| 713 | const unsigned int r_type = ELF32_R_TYPE (reloc->r_info); |
| 714 | if (__glibc_likely (r_type == R_386_JMP_SLOT)) |
| 715 | ; |
| 716 | else if (__glibc_likely (r_type == R_386_TLS_DESC)) |
| 717 | { |
| 718 | struct tlsdesc volatile * __attribute__((__unused__)) td = |
| 719 | (struct tlsdesc volatile *)reloc_addr; |
| 720 | |
| 721 | td->arg = (void*)reloc; |
| 722 | td->entry = _dl_tlsdesc_resolve_rela; |
| 723 | } |
| 724 | else if (__glibc_unlikely (r_type == R_386_IRELATIVE)) |
| 725 | { |
| 726 | Elf32_Addr value = map->l_addr + reloc->r_addend; |
| 727 | if (__glibc_likely (!skip_ifunc)) |
| 728 | value = ((Elf32_Addr (*) (void)) value) (); |
| 729 | *reloc_addr = value; |
| 730 | } |
| 731 | else |
| 732 | _dl_reloc_bad_type (map, r_type, 1); |
| 733 | } |
| 734 | |
| 735 | # endif /* !RTLD_BOOTSTRAP */ |
| 736 | |
| 737 | #endif /* RESOLVE_MAP */ |