1 /* Machine-dependent ELF dynamic relocation inline functions. C-SKY version.
2 Copyright (C) 2018-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
22 #define ELF_MACHINE_NAME "csky"
24 #include <sys/param.h>
28 /* Return nonzero if ELF header is compatible with the running host. */
30 elf_machine_matches_host (const Elf32_Ehdr
*ehdr
)
32 return ehdr
->e_machine
== EM_CSKY
;
35 /* Return the link-time address of _DYNAMIC.
36 This must be inlined in a function which uses global data. */
37 static inline Elf32_Addr
38 elf_machine_dynamic (void)
40 register Elf32_Addr
*got
__asm__ ("gb");
44 /* Return the run-time load address ,of the shared object. */
45 static inline Elf32_Addr
46 elf_machine_load_address (void)
48 extern Elf32_Addr
__dl_start (void *) asm ("_dl_start");
49 Elf32_Addr got_addr
= (Elf32_Addr
) &__dl_start
;
50 Elf32_Addr pcrel_addr
;
51 asm ("grs %0,_dl_start\n" : "=r" (pcrel_addr
));
53 return pcrel_addr
- got_addr
;
57 /* Set up the loaded object described by L so its unrelocated PLT
58 entries will jump to the on-demand fixup code in dl-runtime.c. */
60 static inline int __attribute__ ((always_inline
))
61 elf_machine_runtime_setup (struct link_map
*l
, struct r_scope_elem
*scope
[],
62 int lazy
, int profile
)
65 extern void _dl_runtime_resolve (Elf32_Word
);
67 if (l
->l_info
[DT_JMPREL
] && lazy
)
69 /* The GOT entries for functions in the PLT have not yet been
70 filled in. Their initial contents will arrange when called
71 to push an offset into the .rela.plt section, push
72 _GLOBAL_OFFSET_TABLE_[1], and then jump to
73 _GLOBAL_OFFSET_TABLE_[2]. */
74 got
= (Elf32_Addr
*) D_PTR (l
, l_info
[DT_PLTGOT
]);
77 l
->l_mach
.plt
= got
[1] + l
->l_addr
;
78 got
[1] = (Elf32_Addr
) l
; /* Identify this shared object. */
80 /* The got[2] entry contains the address of a function which gets
81 called to get the address of a so far unresolved function and
82 jump to it. The profiling extension of the dynamic linker allows
83 to intercept the calls to collect information. In this case we
84 don't store the address in the GOT so that all future calls also
85 end in this function. */
86 got
[2] = (Elf32_Addr
) &_dl_runtime_resolve
;
91 /* Mask identifying addresses reserved for the user program,
92 where the dynamic linker should not map anything. */
93 #define ELF_MACHINE_USER_ADDRESS_MASK 0x80000000UL
95 /* Initial entry point code for the dynamic linker.
96 The C function `_dl_start' is the real entry point;
97 its return value is the user program's entry point. */
98 #define RTLD_START asm ("\
101 .type _start, @function\n\
102 .globl _dl_start_user\n\
103 .type _dl_start_user, @function\n\
107 lrw t0, .Lgetpc1@GOTPC\n\
110 lrw t1, _dl_start@GOTOFF\n\
114 /* get _dl_skip_args */ \n\
115 lrw r11, _dl_skip_args@GOTOFF\n\
118 /* store program entry address in r11 */ \n\
132 lrw a0, _rtld_local@GOTOFF\n\
135 lrw t1, _dl_init@PLT\n\
139 lrw a0, _dl_fini@GOTOFF\n\
149 lrw a3, _dl_argv@GOTOFF\n\
155 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
156 TLS variable, so undefined references should not be allowed to
158 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
159 of the main executable's symbols, as for a COPY reloc. */
160 #ifndef RTLD_BOOTSTRAP
161 # define elf_machine_type_class(type) \
162 ((((type) == R_CKCORE_JUMP_SLOT || (type) == R_CKCORE_TLS_DTPMOD32 \
163 || (type) == R_CKCORE_TLS_DTPOFF32 || (type) == R_CKCORE_TLS_TPOFF32) \
164 * ELF_RTYPE_CLASS_PLT) \
165 | (((type) == R_CKCORE_COPY) * ELF_RTYPE_CLASS_COPY))
167 # define elf_machine_type_class(type) \
168 ((((type) == R_CKCORE_JUMP_SLOT \
169 | (((type) == R_CKCORE_COPY) * ELF_RTYPE_CLASS_COPY))
172 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
173 #define ELF_MACHINE_JMP_SLOT R_CKCORE_JUMP_SLOT
175 /* C-SKY never uses Elf32_Rel relocations. */
176 #define ELF_MACHINE_NO_REL 1
177 #define ELF_MACHINE_NO_RELA 0
179 /* We define an initialization functions. This is called very early in
181 #define DL_PLATFORM_INIT dl_platform_init ()
183 static inline void __attribute__ ((unused
))
184 dl_platform_init (void)
186 if (GLRO(dl_platform
) != NULL
&& *GLRO(dl_platform
) == '\0')
187 /* Avoid an empty string which would disturb us. */
188 GLRO(dl_platform
) = NULL
;
191 static inline Elf32_Addr
192 elf_machine_fixup_plt (struct link_map
*map
, lookup_t t
,
193 const ElfW(Sym
) *refsym
, const ElfW(Sym
) *sym
,
194 const Elf32_Rela
*reloc
,
195 Elf32_Addr
*reloc_addr
, Elf32_Addr value
)
197 return *reloc_addr
= value
;
200 /* Return the final value of a plt relocation. On the csky the JMP_SLOT
201 relocation ignores the addend. */
202 static inline Elf32_Addr
203 elf_machine_plt_value (struct link_map
*map
, const Elf32_Rela
*reloc
,
209 /* Names of the architecture-specific auditing callback functions. */
210 #define ARCH_LA_PLTENTER csky_gnu_pltenter
211 #define ARCH_LA_PLTEXIT csky_gnu_pltexit
213 #endif /* !dl_machine_h */
216 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
217 MAP is the object containing the reloc. */
219 static inline void __attribute__ ((unused
, always_inline
))
220 elf_machine_rela (struct link_map
*map
, struct r_scope_elem
*scope
[],
221 const Elf32_Rela
*reloc
, const Elf32_Sym
*sym
,
222 const struct r_found_version
*version
,
223 void *const reloc_addr_arg
, int skip_ifunc
)
225 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
226 const unsigned int r_type
= ELF32_R_TYPE (reloc
->r_info
);
227 unsigned short __attribute__ ((unused
)) *opcode16_addr
;
228 Elf32_Addr
__attribute__ ((unused
)) insn_opcode
= 0x0;
230 if (__builtin_expect (r_type
== R_CKCORE_RELATIVE
, 0))
231 *reloc_addr
= map
->l_addr
+ reloc
->r_addend
;
234 const Elf32_Sym
*const refsym
= sym
;
235 struct link_map
*sym_map
= RESOLVE_MAP (map
, scope
, &sym
, version
,
237 ElfW(Addr
) value
= SYMBOL_ADDRESS (sym_map
, sym
, true);
238 opcode16_addr
= (unsigned short *)reloc_addr
;
244 /* This can happen in trace mode if an object could not be
247 if (sym
->st_size
> refsym
->st_size
248 || (sym
->st_size
< refsym
->st_size
&& GLRO(dl_verbose
)))
252 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
254 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
255 rtld_progname
?: "<program name unknown>",
256 strtab
+ refsym
->st_name
);
258 memcpy (reloc_addr_arg
, (void *) value
,
259 MIN (sym
->st_size
, refsym
->st_size
));
261 case R_CKCORE_GLOB_DAT
:
262 case R_CKCORE_JUMP_SLOT
:
265 case R_CKCORE_ADDR32
:
266 *reloc_addr
= value
+ reloc
->r_addend
;
268 case R_CKCORE_PCREL32
:
269 *reloc_addr
= value
+ reloc
->r_addend
- (Elf32_Addr
) reloc_addr
;
271 #if defined(__CK810__) || defined(__CK807__)
272 case R_CKCORE_ADDR_HI16
:
273 insn_opcode
= (*opcode16_addr
<< 16) | (*(opcode16_addr
+ 1));
274 insn_opcode
= (insn_opcode
& 0xffff0000)
275 | (((value
+ reloc
->r_addend
) >> 16) & 0xffff);
276 *(opcode16_addr
++) = (unsigned short)(insn_opcode
>> 16);
277 *opcode16_addr
= (unsigned short)(insn_opcode
& 0xffff);
279 case R_CKCORE_ADDR_LO16
:
280 insn_opcode
= (*opcode16_addr
<< 16) | (*(opcode16_addr
+ 1));
281 insn_opcode
= (insn_opcode
& 0xffff0000)
282 | ((value
+ reloc
->r_addend
) & 0xffff);
283 *(opcode16_addr
++) = (unsigned short)(insn_opcode
>> 16);
284 *opcode16_addr
= (unsigned short)(insn_opcode
& 0xffff);
286 case R_CKCORE_PCREL_IMM26BY2
:
288 unsigned int offset
= ((value
+ reloc
->r_addend
289 - (unsigned int)reloc_addr
) >> 1);
290 insn_opcode
= (*opcode16_addr
<< 16) | (*(opcode16_addr
+ 1));
291 if (offset
> 0x3ffffff){
293 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
296 %s:The reloc R_CKCORE_PCREL_IMM26BY2 cannot reach the symbol '%s'.\n",
297 rtld_progname
?: "<program name unknown>",
298 strtab
+ refsym
->st_name
);
301 insn_opcode
= (insn_opcode
& ~0x3ffffff) | offset
;
302 *(opcode16_addr
++) = (unsigned short)(insn_opcode
>> 16);
303 *opcode16_addr
= (unsigned short)(insn_opcode
& 0xffff);
306 case R_CKCORE_PCREL_JSR_IMM26BY2
:
309 #ifndef RTLD_BOOTSTRAP
310 case R_CKCORE_TLS_DTPMOD32
:
311 /* Get the information from the link map returned by the
314 *reloc_addr
= sym_map
->l_tls_modid
;
316 case R_CKCORE_TLS_DTPOFF32
:
318 *reloc_addr
=(sym
== NULL
? 0 : sym
->st_value
) + reloc
->r_addend
;
320 case R_CKCORE_TLS_TPOFF32
:
323 CHECK_STATIC_TLS (map
, sym_map
);
324 *reloc_addr
= (sym
->st_value
+ sym_map
->l_tls_offset
328 #endif /* !RTLD_BOOTSTRAP */
337 static inline void __attribute__ ((unused
, always_inline
))
338 elf_machine_rela_relative (Elf32_Addr l_addr
, const Elf32_Rela
*reloc
,
339 void *const reloc_addr_arg
)
341 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
342 *reloc_addr
= l_addr
+ reloc
->r_addend
;
345 static inline void __attribute__ ((unused
, always_inline
))
346 elf_machine_lazy_rel (struct link_map
*map
, struct r_scope_elem
*scope
[],
347 Elf32_Addr l_addr
, const Elf32_Rela
*reloc
,
350 Elf32_Addr
*const reloc_addr
= (void *) (l_addr
+ reloc
->r_offset
);
351 const unsigned int r_type
= ELF32_R_TYPE (reloc
->r_info
);
352 if (ELF32_R_TYPE (reloc
->r_info
) == R_CKCORE_JUMP_SLOT
)
354 /* Check for unexpected PLT reloc type. */
355 if (__builtin_expect (r_type
== R_CKCORE_JUMP_SLOT
, 1))
357 if (__builtin_expect (map
->l_mach
.plt
, 0) == 0)
358 *reloc_addr
= l_addr
+ reloc
->r_addend
;
360 *reloc_addr
= map
->l_mach
.plt
;
365 #endif /* RESOLVE_MAP */