Warning: This file is not a C or C++ file. It does not have highlighting.
1 | /* Machine-dependent ELF dynamic relocation inline functions. i386 version. |
---|---|
2 | Copyright (C) 1995-2024 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 | <https://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 <assert.h> |
25 | #include <sys/param.h> |
26 | #include <sysdep.h> |
27 | #include <tls.h> |
28 | #include <dl-tlsdesc.h> |
29 | #include <dl-static-tls.h> |
30 | #include <dl-machine-rel.h> |
31 | |
32 | /* Return nonzero iff ELF header is compatible with the running host. */ |
33 | static inline int __attribute__ ((unused)) |
34 | elf_machine_matches_host (const Elf32_Ehdr *ehdr) |
35 | { |
36 | return ehdr->e_machine == EM_386; |
37 | } |
38 | |
39 | |
40 | /* Return the run-time load address of the shared object. */ |
41 | static inline Elf32_Addr __attribute__ ((unused)) |
42 | elf_machine_load_address (void) |
43 | { |
44 | extern const Elf32_Ehdr __ehdr_start attribute_hidden; |
45 | return (Elf32_Addr) &__ehdr_start; |
46 | } |
47 | |
48 | /* Return the link-time address of _DYNAMIC. */ |
49 | static inline Elf32_Addr __attribute__ ((unused)) |
50 | elf_machine_dynamic (void) |
51 | { |
52 | extern Elf32_Dyn _DYNAMIC[] attribute_hidden; |
53 | return (Elf32_Addr) _DYNAMIC - elf_machine_load_address (); |
54 | } |
55 | |
56 | /* Set up the loaded object described by L so its unrelocated PLT |
57 | entries will jump to the on-demand fixup code in dl-runtime.c. */ |
58 | |
59 | static inline int __attribute__ ((unused, always_inline)) |
60 | elf_machine_runtime_setup (struct link_map *l, struct r_scope_elem *scope[], |
61 | int lazy, int profile) |
62 | { |
63 | Elf32_Addr *got; |
64 | extern void _dl_runtime_resolve (Elf32_Word) attribute_hidden; |
65 | extern void _dl_runtime_profile (Elf32_Word) attribute_hidden; |
66 | extern void _dl_runtime_resolve_shstk (Elf32_Word) attribute_hidden; |
67 | extern void _dl_runtime_profile_shstk (Elf32_Word) attribute_hidden; |
68 | |
69 | if (l->l_info[DT_JMPREL] && lazy) |
70 | { |
71 | /* The GOT entries for functions in the PLT have not yet been filled |
72 | in. Their initial contents will arrange when called to push an |
73 | offset into the .rel.plt section, push _GLOBAL_OFFSET_TABLE_[1], |
74 | and then jump to _GLOBAL_OFFSET_TABLE[2]. */ |
75 | got = (Elf32_Addr *) D_PTR (l, l_info[DT_PLTGOT]); |
76 | /* If a library is prelinked but we have to relocate anyway, |
77 | we have to be able to undo the prelinking of .got.plt. |
78 | The prelinker saved us here address of .plt + 0x16. */ |
79 | if (got[1]) |
80 | { |
81 | l->l_mach.plt = got[1] + l->l_addr; |
82 | l->l_mach.gotplt = (Elf32_Addr) &got[3]; |
83 | } |
84 | got[1] = (Elf32_Addr) l; /* Identify this shared object. */ |
85 | |
86 | /* The got[2] entry contains the address of a function which gets |
87 | called to get the address of a so far unresolved function and |
88 | jump to it. The profiling extension of the dynamic linker allows |
89 | to intercept the calls to collect information. In this case we |
90 | don't store the address in the GOT so that all future calls also |
91 | end in this function. */ |
92 | #ifdef SHARED |
93 | if (__glibc_unlikely (profile)) |
94 | { |
95 | got[2] = (Elf32_Addr) &_dl_runtime_profile; |
96 | |
97 | if (GLRO(dl_profile) != NULL |
98 | && _dl_name_match_p (GLRO(dl_profile), l)) |
99 | /* This is the object we are looking for. Say that we really |
100 | want profiling and the timers are started. */ |
101 | GL(dl_profile_map) = l; |
102 | } |
103 | else |
104 | #endif |
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 | /* Mask identifying addresses reserved for the user program, |
114 | where the dynamic linker should not map anything. */ |
115 | #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL |
116 | |
117 | /* Initial entry point code for the dynamic linker. |
118 | The C function `_dl_start' is the real entry point; |
119 | its return value is the user program's entry point. */ |
120 | |
121 | #define RTLD_START asm ("\n\ |
122 | .text\n\ |
123 | .align 16\n\ |
124 | 0: movl (%esp), %ebx\n\ |
125 | ret\n\ |
126 | .align 16\n\ |
127 | .globl _start\n\ |
128 | .globl _dl_start_user\n\ |
129 | _start:\n\ |
130 | movl %esp, %eax\n\ |
131 | subl $12, %esp\n\ |
132 | pushl %eax\n\ |
133 | call _dl_start\n\ |
134 | addl $16, %esp\n\ |
135 | _dl_start_user:\n\ |
136 | # Save the user entry point address in %edi.\n\ |
137 | movl %eax, %edi\n\ |
138 | # Point %ebx at the GOT.\n\ |
139 | call 0b\n\ |
140 | addl $_GLOBAL_OFFSET_TABLE_, %ebx\n\ |
141 | # Read the original argument count.\n\ |
142 | movl (%esp), %edx\n\ |
143 | # The special initializer gets called with the stack just\n\ |
144 | # as the application's entry point will see it; it can\n\ |
145 | # switch stacks if it moves these contents over.\n\ |
146 | " RTLD_START_SPECIAL_INIT "\n\ |
147 | # Load the parameters again.\n\ |
148 | # (eax, edx, ecx, esi) = (_dl_loaded, argc, argv, envp)\n\ |
149 | movl _rtld_local@GOTOFF(%ebx), %eax\n\ |
150 | leal 8(%esp,%edx,4), %esi\n\ |
151 | leal 4(%esp), %ecx\n\ |
152 | movl %esp, %ebp\n\ |
153 | # Make sure _dl_init is run with 16 byte aligned stack.\n\ |
154 | andl $-16, %esp\n\ |
155 | subl $12, %esp\n\ |
156 | pushl %ebp\n\ |
157 | # Arguments for _dl_init.\n\ |
158 | pushl %esi\n\ |
159 | pushl %ecx\n\ |
160 | pushl %edx\n\ |
161 | pushl %eax\n\ |
162 | # Clear %ebp, so that even constructors have terminated backchain.\n\ |
163 | xorl %ebp, %ebp\n\ |
164 | # Call the function to run the initializers.\n\ |
165 | call _dl_init\n\ |
166 | # Pass our finalizer function to the user in %edx, as per ELF ABI.\n\ |
167 | leal _dl_fini@GOTOFF(%ebx), %edx\n\ |
168 | # Restore %esp _start expects.\n\ |
169 | movl 16(%esp), %esp\n\ |
170 | # Jump to the user's entry point.\n\ |
171 | jmp *%edi\n\ |
172 | .previous\n\ |
173 | "); |
174 | |
175 | #ifndef RTLD_START_SPECIAL_INIT |
176 | # define RTLD_START_SPECIAL_INIT /* nothing */ |
177 | #endif |
178 | |
179 | /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or |
180 | TLS variable, so undefined references should not be allowed to |
181 | define the value. |
182 | ELF_RTYPE_CLASS_COPY iff TYPE should not be allowed to resolve to one |
183 | of the main executable's symbols, as for a COPY reloc. */ |
184 | # define elf_machine_type_class(type) \ |
185 | ((((type) == R_386_JMP_SLOT || (type) == R_386_TLS_DTPMOD32 \ |
186 | || (type) == R_386_TLS_DTPOFF32 || (type) == R_386_TLS_TPOFF32 \ |
187 | || (type) == R_386_TLS_TPOFF || (type) == R_386_TLS_DESC) \ |
188 | * ELF_RTYPE_CLASS_PLT) \ |
189 | | (((type) == R_386_COPY) * ELF_RTYPE_CLASS_COPY)) |
190 | |
191 | /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */ |
192 | #define ELF_MACHINE_JMP_SLOT R_386_JMP_SLOT |
193 | |
194 | /* We define an initialization functions. This is called very early in |
195 | _dl_sysdep_start. */ |
196 | #define DL_PLATFORM_INIT dl_platform_init () |
197 | |
198 | static inline void __attribute__ ((unused)) |
199 | dl_platform_init (void) |
200 | { |
201 | #if IS_IN (rtld) |
202 | /* _dl_x86_init_cpu_features is a wrapper for init_cpu_features which |
203 | has been called early from __libc_start_main in static executable. */ |
204 | _dl_x86_init_cpu_features (); |
205 | #else |
206 | if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0') |
207 | /* Avoid an empty string which would disturb us. */ |
208 | GLRO(dl_platform) = NULL; |
209 | #endif |
210 | } |
211 | |
212 | static inline Elf32_Addr |
213 | elf_machine_fixup_plt (struct link_map *map, lookup_t t, |
214 | const ElfW(Sym) *refsym, const ElfW(Sym) *sym, |
215 | const Elf32_Rel *reloc, |
216 | Elf32_Addr *reloc_addr, Elf32_Addr value) |
217 | { |
218 | return *reloc_addr = value; |
219 | } |
220 | |
221 | /* Return the final value of a plt relocation. */ |
222 | static inline Elf32_Addr |
223 | elf_machine_plt_value (struct link_map *map, const Elf32_Rel *reloc, |
224 | Elf32_Addr value) |
225 | { |
226 | return value; |
227 | } |
228 | |
229 | |
230 | /* Names of the architecture-specific auditing callback functions. */ |
231 | #define ARCH_LA_PLTENTER i86_gnu_pltenter |
232 | #define ARCH_LA_PLTEXIT i86_gnu_pltexit |
233 | |
234 | #endif /* !dl_machine_h */ |
235 | |
236 | #ifdef RESOLVE_MAP |
237 | |
238 | /* Perform the relocation specified by RELOC and SYM (which is fully resolved). |
239 | MAP is the object containing the reloc. */ |
240 | |
241 | static inline void |
242 | __attribute ((always_inline)) |
243 | elf_machine_rel (struct link_map *map, struct r_scope_elem *scope[], |
244 | const Elf32_Rel *reloc, |
245 | const Elf32_Sym *sym, const struct r_found_version *version, |
246 | void *const reloc_addr_arg, int skip_ifunc) |
247 | { |
248 | Elf32_Addr *const reloc_addr = reloc_addr_arg; |
249 | const unsigned int r_type = ELF32_R_TYPE (reloc->r_info); |
250 | |
251 | # if !defined RTLD_BOOTSTRAP |
252 | if (__glibc_unlikely (r_type == R_386_RELATIVE)) |
253 | *reloc_addr += map->l_addr; |
254 | # ifndef RTLD_BOOTSTRAP |
255 | else if (__glibc_unlikely (r_type == R_386_NONE)) |
256 | return; |
257 | # endif |
258 | else |
259 | # endif /* !RTLD_BOOTSTRAP */ |
260 | { |
261 | # ifndef RTLD_BOOTSTRAP |
262 | const Elf32_Sym *const refsym = sym; |
263 | # endif |
264 | struct link_map *sym_map = RESOLVE_MAP (map, scope, &sym, version, |
265 | r_type); |
266 | Elf32_Addr value = SYMBOL_ADDRESS (sym_map, sym, true); |
267 | |
268 | if (sym != NULL |
269 | && __glibc_unlikely (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC) |
270 | && __glibc_likely (sym->st_shndx != SHN_UNDEF) |
271 | && __glibc_likely (!skip_ifunc)) |
272 | { |
273 | # ifndef RTLD_BOOTSTRAP |
274 | if (sym_map != map |
275 | && !sym_map->l_relocated) |
276 | { |
277 | const char *strtab |
278 | = (const char *) D_PTR (map, l_info[DT_STRTAB]); |
279 | if (sym_map->l_type == lt_executable) |
280 | _dl_fatal_printf ("\ |
281 | %s: IFUNC symbol '%s' referenced in '%s' is defined in the executable \ |
282 | and creates an unsatisfiable circular dependency.\n", |
283 | RTLD_PROGNAME, strtab + refsym->st_name, |
284 | map->l_name); |
285 | else |
286 | _dl_error_printf ("\ |
287 | %s: Relink `%s' with `%s' for IFUNC symbol `%s'\n", |
288 | RTLD_PROGNAME, map->l_name, |
289 | sym_map->l_name, |
290 | strtab + refsym->st_name); |
291 | } |
292 | # endif |
293 | value = ((Elf32_Addr (*) (void)) value) (); |
294 | } |
295 | |
296 | switch (r_type) |
297 | { |
298 | # ifndef RTLD_BOOTSTRAP |
299 | case R_386_SIZE32: |
300 | /* Set to symbol size plus addend. */ |
301 | *reloc_addr += sym->st_size; |
302 | break; |
303 | # endif |
304 | case R_386_GLOB_DAT: |
305 | case R_386_JMP_SLOT: |
306 | *reloc_addr = value; |
307 | break; |
308 | |
309 | case R_386_TLS_DTPMOD32: |
310 | # ifdef RTLD_BOOTSTRAP |
311 | /* During startup the dynamic linker is always the module |
312 | with index 1. |
313 | XXX If this relocation is necessary move before RESOLVE |
314 | call. */ |
315 | *reloc_addr = 1; |
316 | # else |
317 | /* Get the information from the link map returned by the |
318 | resolv function. */ |
319 | if (sym_map != NULL) |
320 | *reloc_addr = sym_map->l_tls_modid; |
321 | # endif |
322 | break; |
323 | case R_386_TLS_DTPOFF32: |
324 | # ifndef RTLD_BOOTSTRAP |
325 | /* During relocation all TLS symbols are defined and used. |
326 | Therefore the offset is already correct. */ |
327 | if (sym != NULL) |
328 | *reloc_addr = sym->st_value; |
329 | # endif |
330 | break; |
331 | case R_386_TLS_DESC: |
332 | { |
333 | struct tlsdesc volatile *td = |
334 | (struct tlsdesc volatile *)reloc_addr; |
335 | |
336 | # ifndef RTLD_BOOTSTRAP |
337 | if (! sym) |
338 | td->entry = _dl_tlsdesc_undefweak; |
339 | else |
340 | # endif |
341 | { |
342 | # ifndef RTLD_BOOTSTRAP |
343 | # ifndef SHARED |
344 | CHECK_STATIC_TLS (map, sym_map); |
345 | # else |
346 | if (!TRY_STATIC_TLS (map, sym_map)) |
347 | { |
348 | td->arg = _dl_make_tlsdesc_dynamic |
349 | (sym_map, sym->st_value + (ElfW(Word))td->arg); |
350 | td->entry = GLRO(dl_x86_tlsdesc_dynamic); |
351 | } |
352 | else |
353 | # endif |
354 | # endif |
355 | { |
356 | td->arg = (void*)(sym->st_value - sym_map->l_tls_offset |
357 | + (ElfW(Word))td->arg); |
358 | td->entry = _dl_tlsdesc_return; |
359 | } |
360 | } |
361 | break; |
362 | } |
363 | case R_386_TLS_TPOFF32: |
364 | /* The offset is positive, backward from the thread pointer. */ |
365 | # ifdef RTLD_BOOTSTRAP |
366 | *reloc_addr += map->l_tls_offset - sym->st_value; |
367 | # else |
368 | /* We know the offset of object the symbol is contained in. |
369 | It is a positive value which will be subtracted from the |
370 | thread pointer. To get the variable position in the TLS |
371 | block we subtract the offset from that of the TLS block. */ |
372 | if (sym != NULL) |
373 | { |
374 | CHECK_STATIC_TLS (map, sym_map); |
375 | *reloc_addr += sym_map->l_tls_offset - sym->st_value; |
376 | } |
377 | # endif |
378 | break; |
379 | case R_386_TLS_TPOFF: |
380 | /* The offset is negative, forward from the thread pointer. */ |
381 | # ifdef RTLD_BOOTSTRAP |
382 | *reloc_addr += sym->st_value - map->l_tls_offset; |
383 | # else |
384 | /* We know the offset of object the symbol is contained in. |
385 | It is a negative value which will be added to the |
386 | thread pointer. */ |
387 | if (sym != NULL) |
388 | { |
389 | CHECK_STATIC_TLS (map, sym_map); |
390 | *reloc_addr += sym->st_value - sym_map->l_tls_offset; |
391 | } |
392 | # endif |
393 | break; |
394 | |
395 | # ifndef RTLD_BOOTSTRAP |
396 | case R_386_32: |
397 | *reloc_addr += value; |
398 | break; |
399 | case R_386_PC32: |
400 | *reloc_addr += (value - (Elf32_Addr) reloc_addr); |
401 | break; |
402 | case R_386_COPY: |
403 | if (sym == NULL) |
404 | /* This can happen in trace mode if an object could not be |
405 | found. */ |
406 | break; |
407 | if (__glibc_unlikely (sym->st_size > refsym->st_size) |
408 | || (__glibc_unlikely(sym->st_size < refsym->st_size) |
409 | && GLRO(dl_verbose))) |
410 | { |
411 | const char *strtab; |
412 | |
413 | strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]); |
414 | _dl_error_printf ("\ |
415 | %s: Symbol `%s' has different size in shared object, consider re-linking\n", |
416 | RTLD_PROGNAME, strtab + refsym->st_name); |
417 | } |
418 | memcpy (reloc_addr_arg, (void *) value, |
419 | MIN (sym->st_size, refsym->st_size)); |
420 | break; |
421 | case R_386_IRELATIVE: |
422 | value = map->l_addr + *reloc_addr; |
423 | if (__glibc_likely (!skip_ifunc)) |
424 | value = ((Elf32_Addr (*) (void)) value) (); |
425 | *reloc_addr = value; |
426 | break; |
427 | default: |
428 | _dl_reloc_bad_type (map, r_type, 0); |
429 | break; |
430 | # endif /* !RTLD_BOOTSTRAP */ |
431 | } |
432 | } |
433 | } |
434 | |
435 | |
436 | static inline void |
437 | __attribute ((always_inline)) |
438 | elf_machine_rel_relative (Elf32_Addr l_addr, const Elf32_Rel *reloc, |
439 | void *const reloc_addr_arg) |
440 | { |
441 | Elf32_Addr *const reloc_addr = reloc_addr_arg; |
442 | assert (ELF32_R_TYPE (reloc->r_info) == R_386_RELATIVE); |
443 | *reloc_addr += l_addr; |
444 | } |
445 | |
446 | # ifndef RTLD_BOOTSTRAP |
447 | static inline void |
448 | __attribute__ ((always_inline)) |
449 | elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc, |
450 | void *const reloc_addr_arg) |
451 | { |
452 | Elf32_Addr *const reloc_addr = reloc_addr_arg; |
453 | *reloc_addr = l_addr + reloc->r_addend; |
454 | } |
455 | # endif /* !RTLD_BOOTSTRAP */ |
456 | |
457 | static inline void |
458 | __attribute__ ((always_inline)) |
459 | elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[], |
460 | Elf32_Addr l_addr, const Elf32_Rel *reloc, |
461 | int skip_ifunc) |
462 | { |
463 | Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset); |
464 | const unsigned int r_type = ELF32_R_TYPE (reloc->r_info); |
465 | /* Check for unexpected PLT reloc type. */ |
466 | if (__glibc_likely (r_type == R_386_JMP_SLOT)) |
467 | { |
468 | /* Prelink has been deprecated. */ |
469 | if (__glibc_likely (map->l_mach.plt == 0)) |
470 | *reloc_addr += l_addr; |
471 | else |
472 | *reloc_addr = (map->l_mach.plt |
473 | + (((Elf32_Addr) reloc_addr) - map->l_mach.gotplt) * 4); |
474 | } |
475 | else if (__glibc_likely (r_type == R_386_TLS_DESC)) |
476 | { |
477 | const Elf32_Rel *const r = reloc; |
478 | /* The code below was borrowed from elf_dynamic_do_rel(). */ |
479 | const ElfW(Sym) *const symtab = |
480 | (const void *) D_PTR (map, l_info[DT_SYMTAB]); |
481 | |
482 | /* Always initialize TLS descriptors completely at load time, in |
483 | case static TLS is allocated for it that requires locking. */ |
484 | # ifdef RTLD_BOOTSTRAP |
485 | /* The dynamic linker always uses versioning. */ |
486 | assert (map->l_info[VERSYMIDX (DT_VERSYM)] != NULL); |
487 | # else |
488 | if (map->l_info[VERSYMIDX (DT_VERSYM)]) |
489 | # endif |
490 | { |
491 | const ElfW(Half) *const version = |
492 | (const void *) D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]); |
493 | ElfW(Half) ndx = version[ELFW(R_SYM) (r->r_info)] & 0x7fff; |
494 | elf_machine_rel (map, scope, r, &symtab[ELFW(R_SYM) (r->r_info)], |
495 | &map->l_versions[ndx], |
496 | (void *) (l_addr + r->r_offset), skip_ifunc); |
497 | } |
498 | # ifndef RTLD_BOOTSTRAP |
499 | else |
500 | elf_machine_rel (map, scope, r, &symtab[ELFW(R_SYM) (r->r_info)], NULL, |
501 | (void *) (l_addr + r->r_offset), skip_ifunc); |
502 | # endif |
503 | } |
504 | else if (__glibc_unlikely (r_type == R_386_IRELATIVE)) |
505 | { |
506 | Elf32_Addr value = map->l_addr + *reloc_addr; |
507 | if (__glibc_likely (!skip_ifunc)) |
508 | value = ((Elf32_Addr (*) (void)) value) (); |
509 | *reloc_addr = value; |
510 | } |
511 | else |
512 | _dl_reloc_bad_type (map, r_type, 1); |
513 | } |
514 | |
515 | #endif /* RESOLVE_MAP */ |
516 |
Warning: This file is not a C or C++ file. It does not have highlighting.