1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 1988 AT&T 24 * All Rights Reserved 25 * 26 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 27 * Use is subject to license terms. 28 */ 29 30 #pragma ident "%Z%%M% %I% %E% SMI" 31 32 /* 33 * SPARC machine dependent and a.out format file class dependent functions. 34 * Contains routines for performing function binding and symbol relocations. 35 */ 36 #include "_synonyms.h" 37 38 #include <stdio.h> 39 #include <sys/types.h> 40 #include <sys/mman.h> 41 #include <synch.h> 42 #include <dlfcn.h> 43 #include <debug.h> 44 #include "_a.out.h" 45 #include "_rtld.h" 46 #include "_audit.h" 47 #include "msg.h" 48 49 extern void iflush_range(caddr_t, size_t); 50 51 /* 52 * Function binding routine - invoked on the first call to a function through 53 * the procedure linkage table; 54 * passes first through an assembly language interface. 55 * 56 * Takes the address of the PLT entry where the call originated, 57 * the offset into the relocation table of the associated 58 * relocation entry and the address of the link map (rt_private_map struct) 59 * for the entry. 60 * 61 * Returns the address of the function referenced after re-writing the PLT 62 * entry to invoke the function directly. 63 * 64 * On error, causes process to terminate with a signal. 65 */ 66 ulong_t 67 aout_bndr(caddr_t pc) 68 { 69 Rt_map *lmp, *nlmp, *llmp; 70 struct relocation_info *rp; 71 struct nlist *sp; 72 Sym *sym; 73 char *name; 74 int rndx, entry; 75 ulong_t symval; 76 Slookup sl; 77 uint_t binfo; 78 Lm_list *lml; 79 80 /* 81 * For compatibility with libthread (TI_VERSION 1) we track the entry 82 * value. A zero value indicates we have recursed into ld.so.1 to 83 * further process a locking request (see comments in completion()). 84 * Under this recursion we disable tsort and cleanup activities. 85 */ 86 entry = enter(0); 87 88 for (lmp = lml_main.lm_head; lmp; lmp = (Rt_map *)NEXT(lmp)) { 89 if (FCT(lmp) == &aout_fct) { 90 if (pc > (caddr_t)(LM2LP(lmp)->lp_plt) && 91 pc < (caddr_t)((int)LM2LP(lmp)->lp_plt + 92 AOUTDYN(lmp)->v2->ld_plt_sz)) { 93 break; 94 } 95 } 96 } 97 98 #define LAST22BITS 0x3fffff 99 100 /* LINTED */ 101 rndx = *(int *)(pc + (sizeof (ulong_t *) * 2)) & LAST22BITS; 102 rp = &LM2LP(lmp)->lp_rp[rndx]; 103 sp = &LM2LP(lmp)->lp_symtab[rp->r_symbolnum]; 104 name = &LM2LP(lmp)->lp_symstr[sp->n_un.n_strx]; 105 106 /* 107 * Determine the last link-map of this list, this'll be the starting 108 * point for any tsort() processing. 109 */ 110 lml = LIST(lmp); 111 llmp = lml->lm_tail; 112 113 /* 114 * Find definition for symbol. Initialize the symbol lookup data 115 * structure. 116 */ 117 SLOOKUP_INIT(sl, name, lmp, lml->lm_head, ld_entry_cnt, 0, 0, 0, 0, 118 LKUP_DEFT); 119 120 if ((sym = aout_lookup_sym(&sl, &nlmp, &binfo, NULL)) == 0) { 121 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_NOSYM), NAME(lmp), 122 demangle(name)); 123 rtldexit(lml, 1); 124 } 125 126 symval = sym->st_value; 127 if (!(FLAGS(nlmp) & FLG_RT_FIXED) && 128 (sym->st_shndx != SHN_ABS)) 129 symval += (int)(ADDR(nlmp)); 130 if ((lmp != nlmp) && ((FLAGS1(nlmp) & FL1_RT_NOINIFIN) == 0)) { 131 /* 132 * Record that this new link map is now bound to the caller. 133 */ 134 if (bind_one(lmp, nlmp, BND_REFER) == 0) 135 rtldexit(lml, 1); 136 } 137 138 /* 139 * Print binding information and rebuild PLT entry. 140 */ 141 DBG_CALL(Dbg_bind_global(lmp, (Addr)(ADDR(lmp) + rp->r_address), 142 (Off)rp->r_address, (Xword)(-1), PLT_T_NONE, nlmp, 143 (Addr)symval, sym->st_value, name, binfo)); 144 145 if (!(rtld_flags & RT_FL_NOBIND)) 146 aout_plt_write((caddr_t)(ADDR(lmp) + rp->r_address), symval); 147 148 /* 149 * Complete any processing for newly loaded objects. Note we don't 150 * know exactly where any new objects are loaded (we know the object 151 * that supplied the symbol, but others may have been loaded lazily as 152 * we searched for the symbol), so sorting starts from the last 153 * link-map know on entry to this routine. 154 */ 155 if (entry) 156 load_completion(llmp); 157 158 /* 159 * If the object we've bound to is in the process of being initialized 160 * by another thread, determine whether we should block. 161 */ 162 is_dep_ready(nlmp, lmp, DBG_WAIT_SYMBOL); 163 164 /* 165 * Make sure the object to which we've bound has had it's .init fired. 166 * Cleanup before return to user code. 167 */ 168 if (entry) { 169 is_dep_init(nlmp, lmp); 170 leave(lml, 0); 171 } 172 173 return (symval); 174 } 175 176 177 #define IS_PC_RELATIVE(X) (pc_rel_type[(X)] == 1) 178 179 static const uchar_t pc_rel_type[] = { 180 0, /* RELOC_8 */ 181 0, /* RELOC_16 */ 182 0, /* RELOC_32 */ 183 1, /* RELOC_DISP8 */ 184 1, /* RELOC_DISP16 */ 185 1, /* RELOC_DISP32 */ 186 1, /* RELOC_WDISP30 */ 187 1, /* RELOC_WDISP22 */ 188 0, /* RELOC_HI22 */ 189 0, /* RELOC_22 */ 190 0, /* RELOC_13 */ 191 0, /* RELOC_LO10 */ 192 0, /* RELOC_SFA_BASE */ 193 0, /* RELOC_SFA_OFF13 */ 194 0, /* RELOC_BASE10 */ 195 0, /* RELOC_BASE13 */ 196 0, /* RELOC_BASE22 */ 197 0, /* RELOC_PC10 */ 198 0, /* RELOC_PC22 */ 199 0, /* RELOC_JMP_TBL */ 200 0, /* RELOC_SEGOFF16 */ 201 0, /* RELOC_GLOB_DAT */ 202 0, /* RELOC_JMP_SLOT */ 203 0 /* RELOC_RELATIVE */ 204 }; 205 206 int 207 aout_reloc(Rt_map * lmp, uint_t plt, int *in_nfavl) 208 { 209 int k; /* loop temporary */ 210 int nr; /* number of relocations */ 211 char *name; /* symbol being searched for */ 212 long *et; /* cached _etext of object */ 213 long value; /* relocation temporary */ 214 long *ra; /* cached relocation address */ 215 struct relocation_info *rp; /* current relocation */ 216 struct nlist *sp; /* symbol table of "symbol" */ 217 Rt_map * _lmp; /* lm which holds symbol definition */ 218 Sym * sym; /* symbol definition */ 219 int textrel = 0, ret = 1; 220 APlist *bound = NULL; 221 Lm_list *lml = LIST(lmp); 222 223 DBG_CALL(Dbg_reloc_run(lmp, SHT_RELA, plt, DBG_REL_START)); 224 225 /* 226 * If we've been called upon to promote an RTLD_LAZY object to an 227 * RTLD_NOW don't bother to do anything - a.out's are bound as if 228 * RTLD_NOW regardless. 229 */ 230 if (plt) 231 return (1); 232 233 rp = LM2LP(lmp)->lp_rp; 234 et = (long *)ETEXT(lmp); 235 nr = GETRELSZ(AOUTDYN(lmp)) / sizeof (struct relocation_info); 236 237 /* 238 * Initialize _PLT_, if any. 239 */ 240 if (AOUTDYN(lmp)->v2->ld_plt_sz) 241 aout_plt_write((caddr_t)LM2LP(lmp)->lp_plt->jb_inst, 242 (ulong_t)aout_rtbndr); 243 244 /* 245 * Loop through relocations. 246 */ 247 for (k = 0; k < nr; k++, rp++) { 248 /* LINTED */ 249 ra = (long *)&((char *)ADDR(lmp))[rp->r_address]; 250 251 /* 252 * Check to see if we're relocating in the text segment 253 * and turn off the write protect if necessary. 254 */ 255 if ((ra < et) && (textrel == 0)) { 256 if (aout_set_prot(lmp, PROT_WRITE) == 0) { 257 ret = 0; 258 break; 259 } 260 textrel = 1; 261 } 262 263 /* 264 * Perform the relocation. 265 */ 266 if (rp->r_extern == 0) { 267 name = (char *)0; 268 value = ADDR(lmp); 269 } else { 270 Slookup sl; 271 uint_t binfo; 272 273 if (rp->r_type == RELOC_JMP_SLOT) 274 continue; 275 sp = &LM2LP(lmp)->lp_symtab[rp->r_symbolnum]; 276 name = &LM2LP(lmp)->lp_symstr[sp->n_un.n_strx]; 277 278 /* 279 * Locate symbol. Initialize the symbol lookup data 280 * structure. 281 */ 282 SLOOKUP_INIT(sl, name, lmp, 0, ld_entry_cnt, 0, 0, 0, 0, 283 LKUP_STDRELOC); 284 285 if ((sym = aout_lookup_sym(&sl, &_lmp, 286 &binfo, in_nfavl)) == 0) { 287 if (lml->lm_flags & LML_FLG_TRC_WARN) { 288 (void) 289 printf(MSG_INTL(MSG_LDD_SYM_NFOUND), 290 demangle(name), NAME(lmp)); 291 continue; 292 } else { 293 eprintf(lml, ERR_FATAL, 294 MSG_INTL(MSG_REL_NOSYM), NAME(lmp), 295 demangle(name)); 296 ret = 0; 297 break; 298 } 299 } 300 301 /* 302 * If symbol was found in an object other than the 303 * referencing object then record the binding. 304 */ 305 if ((lmp != _lmp) && 306 ((FLAGS1(_lmp) & FL1_RT_NOINIFIN) == 0)) { 307 if (aplist_test(&bound, _lmp, 308 AL_CNT_RELBIND) == 0) { 309 ret = 0; 310 break; 311 } 312 } 313 314 value = sym->st_value + rp->r_addend; 315 if (!(FLAGS(_lmp) & FLG_RT_FIXED) && 316 (sym->st_shndx != SHN_COMMON) && 317 (sym->st_shndx != SHN_ABS)) 318 value += ADDR(_lmp); 319 320 if (IS_PC_RELATIVE(rp->r_type)) 321 value -= (long)ADDR(lmp); 322 323 DBG_CALL(Dbg_bind_global(lmp, (Addr)ra, 324 (Off)(ra - ADDR(lmp)), (Xword)(-1), PLT_T_NONE, 325 _lmp, (Addr)value, sym->st_value, name, binfo)); 326 } 327 328 /* 329 * Perform a specific relocation operation. 330 */ 331 switch (rp->r_type) { 332 case RELOC_RELATIVE: 333 value += *ra << (32-22); 334 *(long *)ra = (*(long *)ra & ~S_MASK(22)) | 335 ((value >> (32 - 22)) & S_MASK(22)); 336 ra++; 337 value += (*ra & S_MASK(10)); 338 *(long *)ra = (*(long *)ra & ~S_MASK(10)) | 339 (value & S_MASK(10)); 340 break; 341 case RELOC_8: 342 case RELOC_DISP8: 343 value += *ra & S_MASK(8); 344 if (!S_INRANGE(value, 8)) { 345 eprintf(lml, ERR_FATAL, 346 MSG_INTL(MSG_REL_OVERFLOW), NAME(lmp), 347 (name ? demangle(name) : 348 MSG_INTL(MSG_STR_UNKNOWN)), (int)value, 8, 349 (uint_t)ra); 350 } 351 *ra = value; 352 break; 353 case RELOC_LO10: 354 case RELOC_BASE10: 355 value += *ra & S_MASK(10); 356 *(long *)ra = (*(long *)ra & ~S_MASK(10)) | 357 (value & S_MASK(10)); 358 break; 359 case RELOC_BASE13: 360 case RELOC_13: 361 value += *ra & S_MASK(13); 362 *(long *)ra = (*(long *)ra & ~S_MASK(13)) | 363 (value & S_MASK(13)); 364 break; 365 case RELOC_16: 366 case RELOC_DISP16: 367 value += *ra & S_MASK(16); 368 if (!S_INRANGE(value, 16)) { 369 eprintf(lml, ERR_FATAL, 370 MSG_INTL(MSG_REL_OVERFLOW), NAME(lmp), 371 (name ? demangle(name) : 372 MSG_INTL(MSG_STR_UNKNOWN)), (int)value, 16, 373 (uint_t)ra); 374 } 375 *(short *)ra = value; 376 break; 377 case RELOC_22: 378 case RELOC_BASE22: 379 value += *ra & S_MASK(22); 380 if (!S_INRANGE(value, 22)) { 381 eprintf(lml, ERR_FATAL, 382 MSG_INTL(MSG_REL_OVERFLOW), NAME(lmp), 383 (name ? demangle(name) : 384 MSG_INTL(MSG_STR_UNKNOWN)), (int)value, 22, 385 (uint_t)ra); 386 } 387 *(long *)ra = (*(long *)ra & ~S_MASK(22)) | 388 (value & S_MASK(22)); 389 break; 390 case RELOC_HI22: 391 value += (*ra & S_MASK(22)) << (32 - 22); 392 *(long *)ra = (*(long *)ra & ~S_MASK(22)) | 393 ((value >> (32 - 22)) & S_MASK(22)); 394 break; 395 case RELOC_WDISP22: 396 value += *ra & S_MASK(22); 397 value >>= 2; 398 if (!S_INRANGE(value, 22)) { 399 eprintf(lml, ERR_FATAL, 400 MSG_INTL(MSG_REL_OVERFLOW), NAME(lmp), 401 (name ? demangle(name) : 402 MSG_INTL(MSG_STR_UNKNOWN)), (int)value, 22, 403 (uint_t)ra); 404 } 405 *(long *)ra = (*(long *)ra & ~S_MASK(22)) | 406 (value & S_MASK(22)); 407 break; 408 case RELOC_WDISP30: 409 value += *ra & S_MASK(30); 410 value >>= 2; 411 *(long *)ra = (*(long *)ra & ~S_MASK(30)) | 412 (value & S_MASK(30)); 413 break; 414 case RELOC_32: 415 case RELOC_GLOB_DAT: 416 case RELOC_DISP32: 417 value += *ra; 418 *(long *)ra = value; 419 break; 420 default: 421 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_UNIMPL), 422 NAME(lmp), (name ? demangle(name) : 423 MSG_INTL(MSG_STR_UNKNOWN)), rp->r_type); 424 ret = 0; 425 break; 426 } 427 428 /* 429 * If this relocation is against a text segment we must make 430 * sure that the instruction cache is flushed. 431 */ 432 if (textrel) { 433 if (rp->r_type == RELOC_RELATIVE) 434 iflush_range((caddr_t)(ra - 1), 0x8); 435 else 436 iflush_range((caddr_t)ra, 0x4); 437 } 438 } 439 440 return (relocate_finish(lmp, bound, textrel, ret)); 441 } 442