Print this page
11057 hidden undefined weak symbols should not leave relocations
11058 libld entrance descriptor assertions get NDEBUG check backwards
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/sgs/libld/common/machrel.sparc.c
+++ new/usr/src/cmd/sgs/libld/common/machrel.sparc.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 1988 AT&T
24 24 * All Rights Reserved
25 25 *
26 26 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
27 27 */
28 28
29 29 /* Get the sparc version of the relocation engine */
30 30 #define DO_RELOC_LIBLD_SPARC
31 31
32 32 #include <string.h>
33 33 #include <stdio.h>
34 34 #include <sys/elf_SPARC.h>
35 35 #include <debug.h>
36 36 #include <reloc.h>
37 37 #include <sparc/machdep_sparc.h>
38 38 #include "msg.h"
39 39 #include "_libld.h"
40 40 #include "machsym.sparc.h"
41 41
42 42 /*
43 43 * Local Variable Definitions
44 44 */
45 45 static Sword neggotoffset = 0; /* off. of GOT table from GOT symbol */
46 46 static Sword smlgotcnt = M_GOT_XNumber; /* no. of small GOT symbols */
47 47 static Sword mixgotcnt = 0; /* # syms with both large/small GOT */
48 48
49 49 /*
50 50 * Search the GOT index list for a GOT entry with a matching reference and the
51 51 * proper addend.
52 52 */
53 53 static Gotndx *
54 54 ld_find_got_ndx(Alist *alp, Gotref gref, Ofl_desc *ofl, Rel_desc *rdesc)
55 55 {
56 56 Aliste idx;
57 57 Gotndx *gnp;
58 58
59 59 assert(rdesc != 0);
60 60
61 61 if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
62 62 return (ofl->ofl_tlsldgotndx);
63 63
64 64 for (ALIST_TRAVERSE(alp, idx, gnp)) {
65 65 if ((rdesc->rel_raddend == gnp->gn_addend) &&
66 66 (gref == gnp->gn_gotref))
67 67 return (gnp);
68 68 }
69 69 return (NULL);
70 70 }
71 71
72 72 static Xword
73 73 ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl)
74 74 {
75 75 Os_desc *osp = ofl->ofl_osgot;
76 76 Sym_desc *sdp = rdesc->rel_sym;
77 77 Xword gotndx;
78 78 Gotref gref;
79 79 Gotndx *gnp;
80 80
81 81 if (rdesc->rel_flags & FLG_REL_DTLS)
82 82 gref = GOT_REF_TLSGD;
83 83 else if (rdesc->rel_flags & FLG_REL_MTLS)
84 84 gref = GOT_REF_TLSLD;
85 85 else if (rdesc->rel_flags & FLG_REL_STLS)
86 86 gref = GOT_REF_TLSIE;
87 87 else
88 88 gref = GOT_REF_GENERIC;
89 89
90 90 gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, rdesc);
91 91 assert(gnp);
92 92
93 93 gotndx = (Xword)gnp->gn_gotndx;
94 94
95 95 if ((rdesc->rel_flags & FLG_REL_DTLS) &&
96 96 (rdesc->rel_rtype == M_R_DTPOFF))
97 97 gotndx++;
98 98
99 99 return ((Xword)((osp->os_shdr->sh_addr) + (gotndx * M_GOT_ENTSIZE) +
100 100 (-neggotoffset * M_GOT_ENTSIZE)));
101 101 }
102 102
103 103 static Word
104 104 ld_init_rel(Rel_desc *reld, Word *typedata, void *reloc)
105 105 {
106 106 Rela *rela = (Rela *)reloc;
107 107
108 108 /* LINTED */
109 109 reld->rel_rtype = (Word)ELF_R_TYPE(rela->r_info, M_MACH);
110 110 reld->rel_roffset = rela->r_offset;
111 111 reld->rel_raddend = rela->r_addend;
112 112 *typedata = (Word)ELF_R_TYPE_DATA(rela->r_info);
113 113
114 114 reld->rel_flags |= FLG_REL_RELA;
115 115
116 116 return ((Word)ELF_R_SYM(rela->r_info));
117 117 }
118 118
119 119 static void
120 120 ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
121 121 {
122 122 Word eflags = ofl->ofl_dehdr->e_flags;
123 123 Word memopt1, memopt2;
124 124 static int firstpass;
125 125
126 126 /*
127 127 * If a *PLUS relocatable is included, the output object is type *PLUS.
128 128 */
129 129 if ((ehdr->e_machine == EM_SPARC32PLUS) &&
130 130 (ehdr->e_flags & EF_SPARC_32PLUS))
131 131 ofl->ofl_dehdr->e_machine = EM_SPARC32PLUS;
132 132
133 133 /*
134 134 * On the first pass, we don't yet have a memory model to compare
135 135 * against, therefore the initial file becomes our baseline. Subsequent
136 136 * passes will do the comparison described below.
137 137 */
↓ open down ↓ |
137 lines elided |
↑ open up ↑ |
138 138 if (firstpass == 0) {
139 139 ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
140 140 firstpass++;
141 141 return;
142 142 }
143 143
144 144 /*
145 145 * Determine which memory model to mark the binary with. The options
146 146 * are (most restrictive to least):
147 147 *
148 - * EF_SPARCV9_TSO 0x0 Total Store Order
148 + * EF_SPARCV9_TSO 0x0 Total Store Order
149 149 * EF_SPARCV9_PSO 0x1 Partial Store Order
150 150 * EF_SPARCV9_RMO 0x2 Relaxed Memory Order
151 151 *
152 152 * Mark the binary with the most restrictive option encountered from a
153 153 * relocatable object included in the link.
154 154 */
155 155 eflags |= (ehdr->e_flags & ~EF_SPARCV9_MM);
156 156 memopt1 = eflags & EF_SPARCV9_MM;
157 157 memopt2 = ehdr->e_flags & EF_SPARCV9_MM;
158 158 eflags &= ~EF_SPARCV9_MM;
159 159
160 160 if ((memopt1 == EF_SPARCV9_TSO) || (memopt2 == EF_SPARCV9_TSO))
161 161 /* EMPTY */
162 162 ;
163 163 else if ((memopt1 == EF_SPARCV9_PSO) || (memopt2 == EF_SPARCV9_PSO))
164 164 eflags |= EF_SPARCV9_PSO;
165 165 else
166 166 eflags |= EF_SPARCV9_RMO;
167 167
168 168 ofl->ofl_dehdr->e_flags = eflags;
169 169 }
170 170
171 171 static void
172 172 ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
173 173 {
174 174 if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
175 175 /*
176 176 * Create this entry if we are going to create a PLT table.
177 177 */
178 178 if (ofl->ofl_pltcnt)
179 179 (*cnt)++; /* DT_PLTGOT */
180 180 }
181 181 }
182 182
183 183 static void
184 184 ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
185 185 {
186 186 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) {
187 187 (*dyn)->d_tag = DT_PLTGOT;
188 188 if (ofl->ofl_osplt)
189 189 (*dyn)->d_un.d_ptr = ofl->ofl_osplt->os_shdr->sh_addr;
190 190 else
191 191 (*dyn)->d_un.d_ptr = 0;
192 192 (*dyn)++;
193 193 }
194 194 }
195 195
196 196 #if defined(_ELF64)
197 197
198 198 static Xword
199 199 ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
200 200 {
201 201 Xword value, pltndx, farpltndx;
202 202
203 203 pltndx = sdp->sd_aux->sa_PLTndx + M_PLT_XNumber - 1;
204 204
205 205 if ((pltndx) < M64_PLT_NEARPLTS) {
206 206 value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
207 207 (pltndx * M_PLT_ENTSIZE);
208 208 return (value);
209 209 }
210 210
211 211 farpltndx = pltndx - M64_PLT_NEARPLTS;
212 212
213 213 /*
214 214 * pltoffset of a far plt is calculated by:
215 215 *
216 216 * <size of near plt table> +
217 217 * <size of preceding far plt blocks> +
218 218 * <blockndx * sizeof (far plt entsize)>
219 219 */
220 220 value =
221 221 /* size of near plt table */
222 222 (M64_PLT_NEARPLTS * M_PLT_ENTSIZE) +
223 223 /* size of preceding far plt blocks */
224 224 ((farpltndx / M64_PLT_FBLKCNTS) *
225 225 ((M64_PLT_FENTSIZE + sizeof (Addr)) *
226 226 M64_PLT_FBLKCNTS)) +
227 227 /* pltblockendx * fentsize */
228 228 ((farpltndx % M64_PLT_FBLKCNTS) * M64_PLT_FENTSIZE);
229 229
230 230 value += (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
231 231 return (value);
232 232 }
233 233
234 234 /*
235 235 * Instructions required for Far PLT's
236 236 */
237 237 static uchar_t farplt_instrs[24] = {
238 238 0x8a, 0x10, 0x00, 0x0f, /* mov %o7, %g5 */
239 239 0x40, 0x00, 0x00, 0x02, /* call . + 0x8 */
240 240 0x01, 0x00, 0x00, 0x00, /* nop */
241 241 0xc2, 0x5b, 0xe0, 0x00, /* ldx [%o7 + 0], %g1 */
242 242 0x83, 0xc3, 0xc0, 0x01, /* jmpl %o7 + %g1, %g1 */
243 243 0x9e, 0x10, 0x00, 0x05 /* mov %g5, %o7 */
244 244 };
245 245
246 246 /*
247 247 * Far PLT'S:
248 248 *
249 249 * Far PLT's are established in blocks of '160' at a time. These
250 250 * PLT's consist of 6 instructions (24 bytes) and 1 pointer (8 bytes).
251 251 * The instructions are collected together in blocks of 160 entries
252 252 * followed by 160 pointers. The last group of entries and pointers
253 253 * may contain less then 160 items. No padding is required.
254 254 *
255 255 * .PLT32768:
256 256 * mov %o7, %g5
257 257 * call . + 8
258 258 * nop
259 259 * ldx [%o7 + .PLTP32768 - (.PLT32768 + 4)], %g1
260 260 * jmpl %o7 + %g1, %g1
261 261 * mov %g5, %o7
262 262 * ................................
263 263 * .PLT32927:
264 264 * mov %o7, %g5
265 265 * call . + 8
266 266 * nop
267 267 * ldx [%o7 + .PLTP32927 - (.PLT32927 + 4)], %g1
268 268 * jmpl %o7 + %g1, %g1
269 269 * mov %g5, %o7
270 270 * .PLTP32768:
271 271 * .xword .PLT0-(.PLT32768+4)
272 272 * ................................
273 273 * .PLTP32927:
274 274 * .xword .PLT0-(.PLT32927+4)
275 275 *
276 276 */
277 277 static void
278 278 plt_far_entry(Ofl_desc *ofl, Xword pltndx, Xword *roffset, Sxword *raddend)
279 279 {
280 280 uint_t blockndx; /* # of far PLT blocks */
281 281 uint_t farblkcnt; /* Index to far PLT block */
282 282 Xword farpltndx; /* index of Far Plt */
283 283 Xword farpltblkndx; /* index of PLT in BLOCK */
284 284 uint32_t *pltent; /* ptr to plt instr. sequence */
285 285 uint64_t *pltentptr; /* ptr to plt addr ptr */
286 286 Sxword pltblockoff; /* offset to Far plt block */
287 287 Sxword pltoff; /* offset to PLT instr. sequence */
288 288 Sxword pltptroff; /* offset to PLT addr ptr */
289 289 uchar_t *pltbuf; /* ptr to PLT's in file */
290 290
291 291
292 292 farblkcnt = ((ofl->ofl_pltcnt - 1 +
293 293 M_PLT_XNumber - M64_PLT_NEARPLTS) / M64_PLT_FBLKCNTS);
294 294
295 295 /*
296 296 * Determine the 'Far' PLT index.
297 297 */
298 298 farpltndx = pltndx - 1 + M_PLT_XNumber - M64_PLT_NEARPLTS;
299 299 farpltblkndx = farpltndx % M64_PLT_FBLKCNTS;
300 300
301 301 /*
302 302 * Determine what FPLT block this plt falls into.
303 303 */
304 304 blockndx = (uint_t)(farpltndx / M64_PLT_FBLKCNTS);
305 305
306 306 /*
307 307 * Calculate the starting offset of the Far PLT block
308 308 * that this PLT is a member of.
309 309 */
310 310 pltblockoff = (M64_PLT_NEARPLTS * M_PLT_ENTSIZE) +
311 311 (blockndx * M64_PLT_FBLOCKSZ);
312 312
313 313 pltoff = pltblockoff +
314 314 (farpltblkndx * M64_PLT_FENTSIZE);
315 315
316 316 pltptroff = pltblockoff;
317 317
318 318
319 319 if (farblkcnt > blockndx) {
320 320 /*
321 321 * If this is a full block - the 'pltptroffs' start
322 322 * after 160 fplts.
323 323 */
324 324 pltptroff += (M64_PLT_FBLKCNTS * M64_PLT_FENTSIZE) +
325 325 (farpltblkndx * M64_PLT_PSIZE);
326 326 } else {
327 327 Xword lastblkpltndx;
328 328 /*
329 329 * If this is the last block - the the pltptr's start
330 330 * after the last FPLT instruction sequence.
331 331 */
332 332 lastblkpltndx = (ofl->ofl_pltcnt - 1 + M_PLT_XNumber -
333 333 M64_PLT_NEARPLTS) % M64_PLT_FBLKCNTS;
334 334 pltptroff += ((lastblkpltndx + 1) * M64_PLT_FENTSIZE) +
335 335 (farpltblkndx * M64_PLT_PSIZE);
336 336 }
337 337 pltbuf = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
338 338
339 339 /*
340 340 * For far-plts, the Raddend and Roffset fields are defined
341 341 * to be:
342 342 *
343 343 * roffset: address of .PLTP#
344 344 * raddend: -(.PLT#+4)
345 345 */
346 346 *roffset = pltptroff + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
347 347 *raddend = -(pltoff + 4 + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr));
348 348
349 349 /* LINTED */
350 350 pltent = (uint32_t *)(pltbuf + pltoff);
351 351 /* LINTED */
352 352 pltentptr = (uint64_t *)(pltbuf + pltptroff);
353 353 (void) memcpy(pltent, farplt_instrs, sizeof (farplt_instrs));
354 354
355 355 /*
356 356 * update
357 357 * ldx [%o7 + 0], %g1
358 358 * to
359 359 * ldx [%o7 + .PLTP# - (.PLT# + 4)], %g1
360 360 */
361 361 /* LINTED */
362 362 pltent[3] |= (uint32_t)(pltptroff - (pltoff + 4));
363 363
364 364 /*
365 365 * Store:
366 366 * .PLTP#
367 367 * .xword .PLT0 - .PLT# + 4
368 368 */
369 369 *pltentptr = -(pltoff + 4);
370 370 }
371 371
372 372 /*
373 373 * Build a single V9 P.L.T. entry - code is:
374 374 *
375 375 * For Target Addresses +/- 4GB of the entry
376 376 * -----------------------------------------
377 377 * sethi (. - .PLT0), %g1
378 378 * ba,a %xcc, .PLT1
379 379 * nop
380 380 * nop
381 381 * nop
382 382 * nop
383 383 * nop
384 384 * nop
385 385 *
386 386 * For Target Addresses +/- 2GB of the entry
387 387 * -----------------------------------------
388 388 *
389 389 * .PLT0 is the address of the first entry in the P.L.T.
390 390 * This one is filled in by the run-time link editor. We just
391 391 * have to leave space for it.
392 392 */
393 393 static void
394 394 plt_entry(Ofl_desc *ofl, Xword pltndx, Xword *roffset, Sxword *raddend)
395 395 {
396 396 uchar_t *pltent; /* PLT entry being created. */
397 397 Sxword pltoff; /* Offset of this entry from PLT top */
398 398 int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
399 399
400 400 /*
401 401 * The second part of the V9 ABI (sec. 5.2.4)
402 402 * applies to plt entries greater than 0x8000 (32,768).
403 403 * This is handled in 'plt_far_entry()'
404 404 */
405 405 if ((pltndx - 1 + M_PLT_XNumber) >= M64_PLT_NEARPLTS) {
406 406 plt_far_entry(ofl, pltndx, roffset, raddend);
407 407 return;
408 408 }
409 409
410 410 pltoff = M_PLT_RESERVSZ + (pltndx - 1) * M_PLT_ENTSIZE;
411 411 pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf + pltoff;
412 412
413 413 *roffset = pltoff + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
414 414 *raddend = 0;
415 415
416 416 /*
417 417 * PLT[0]: sethi %hi(. - .L0), %g1
418 418 */
419 419 /* LINTED */
420 420 *(Word *)pltent = M_SETHIG1 | pltoff;
421 421 if (bswap)
422 422 /* LINTED */
423 423 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
424 424
425 425 /*
426 426 * PLT[1]: ba,a %xcc, .PLT1 (.PLT1 accessed as a
427 427 * PC-relative index of longwords).
428 428 */
429 429 pltent += M_PLT_INSSIZE;
430 430 pltoff += M_PLT_INSSIZE;
431 431 pltoff = -pltoff;
432 432 /* LINTED */
433 433 *(Word *)pltent = M_BA_A_XCC |
434 434 (((pltoff + M_PLT_ENTSIZE) >> 2) & S_MASK(19));
435 435 if (bswap)
436 436 /* LINTED */
437 437 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
438 438
439 439 /*
440 440 * PLT[2]: sethi 0, %g0 (NOP for delay slot of eventual CTI).
441 441 */
442 442 pltent += M_PLT_INSSIZE;
443 443 /* LINTED */
444 444 *(Word *)pltent = M_NOP;
445 445 if (bswap)
446 446 /* LINTED */
447 447 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
448 448
449 449 /*
450 450 * PLT[3]: sethi 0, %g0 (NOP for PLT padding).
451 451 */
452 452 pltent += M_PLT_INSSIZE;
453 453 /* LINTED */
454 454 *(Word *)pltent = M_NOP;
455 455 if (bswap)
456 456 /* LINTED */
457 457 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
458 458
459 459 /*
460 460 * PLT[4]: sethi 0, %g0 (NOP for PLT padding).
461 461 */
462 462 pltent += M_PLT_INSSIZE;
463 463 /* LINTED */
464 464 *(Word *)pltent = M_NOP;
465 465 if (bswap)
466 466 /* LINTED */
467 467 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
468 468
469 469 /*
470 470 * PLT[5]: sethi 0, %g0 (NOP for PLT padding).
471 471 */
472 472 pltent += M_PLT_INSSIZE;
473 473 /* LINTED */
474 474 *(Word *)pltent = M_NOP;
475 475 if (bswap)
476 476 /* LINTED */
477 477 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
478 478
479 479 /*
480 480 * PLT[6]: sethi 0, %g0 (NOP for PLT padding).
481 481 */
482 482 pltent += M_PLT_INSSIZE;
483 483 /* LINTED */
484 484 *(Word *)pltent = M_NOP;
485 485 if (bswap)
486 486 /* LINTED */
487 487 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
488 488
489 489 /*
490 490 * PLT[7]: sethi 0, %g0 (NOP for PLT padding).
491 491 */
492 492 pltent += M_PLT_INSSIZE;
493 493 /* LINTED */
494 494 *(Word *)pltent = M_NOP;
495 495 if (bswap)
496 496 /* LINTED */
497 497 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
498 498 }
499 499
500 500
501 501 #else /* Elf 32 */
502 502
503 503 static Xword
504 504 ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
505 505 {
506 506 Xword value, pltndx;
507 507
508 508 pltndx = sdp->sd_aux->sa_PLTndx + M_PLT_XNumber - 1;
509 509 value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
510 510 (pltndx * M_PLT_ENTSIZE);
511 511 return (value);
512 512 }
513 513
514 514
515 515 /*
516 516 * Build a single P.L.T. entry - code is:
517 517 *
518 518 * sethi (. - .L0), %g1
519 519 * ba,a .L0
520 520 * sethi 0, %g0 (nop)
521 521 *
522 522 * .L0 is the address of the first entry in the P.L.T.
523 523 * This one is filled in by the run-time link editor. We just
524 524 * have to leave space for it.
525 525 */
526 526 static void
527 527 plt_entry(Ofl_desc * ofl, Xword pltndx, Xword *roffset, Sxword *raddend)
528 528 {
529 529 Byte *pltent; /* PLT entry being created. */
530 530 Sxword pltoff; /* Offset of this entry from PLT top */
531 531 int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
532 532
533 533 pltoff = M_PLT_RESERVSZ + (pltndx - 1) * M_PLT_ENTSIZE;
534 534 pltent = (Byte *)ofl->ofl_osplt->os_outdata->d_buf + pltoff;
535 535
536 536 *roffset = pltoff + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
537 537 *raddend = 0;
538 538
539 539 /*
540 540 * PLT[0]: sethi %hi(. - .L0), %g1
541 541 */
542 542 /* LINTED */
543 543 *(Word *)pltent = M_SETHIG1 | pltoff;
544 544 if (bswap)
545 545 /* LINTED */
546 546 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
547 547
548 548 /*
549 549 * PLT[1]: ba,a .L0 (.L0 accessed as a PC-relative index of longwords)
550 550 */
551 551 pltent += M_PLT_INSSIZE;
552 552 pltoff += M_PLT_INSSIZE;
553 553 pltoff = -pltoff;
554 554 /* LINTED */
555 555 *(Word *)pltent = M_BA_A | ((pltoff >> 2) & S_MASK(22));
556 556 if (bswap)
557 557 /* LINTED */
558 558 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
559 559
560 560 /*
561 561 * PLT[2]: sethi 0, %g0 (NOP for delay slot of eventual CTI).
562 562 */
563 563 pltent += M_PLT_INSSIZE;
564 564 /* LINTED */
565 565 *(Word *)pltent = M_SETHIG0;
566 566 if (bswap)
567 567 /* LINTED */
568 568 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
569 569
570 570 /*
571 571 * PLT[3]: sethi 0, %g0 (NOP for PLT padding).
572 572 */
573 573 pltent += M_PLT_INSSIZE;
574 574 /* LINTED */
575 575 *(Word *)pltent = M_SETHIG0;
576 576 if (bswap)
577 577 /* LINTED */
578 578 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
579 579 }
580 580
581 581 #endif /* _ELF64 */
582 582
583 583 static uintptr_t
584 584 ld_perform_outreloc(Rel_desc *orsp, Ofl_desc *ofl, Boolean *remain_seen)
585 585 {
586 586 Os_desc *relosp, *osp = NULL;
587 587 Xword ndx, roffset, value;
588 588 Sxword raddend;
589 589 const Rel_entry *rep;
590 590 Rela rea;
591 591 char *relbits;
592 592 Sym_desc *sdp, *psym = NULL;
593 593 int sectmoved = 0;
594 594 Word dtflags1 = ofl->ofl_dtflags_1;
595 595 ofl_flag_t flags = ofl->ofl_flags;
596 596
597 597 raddend = orsp->rel_raddend;
598 598 sdp = orsp->rel_sym;
599 599
600 600 /*
601 601 * Special case, a regsiter symbol associated with symbol
602 602 * index 0 is initialized (i.e. relocated) to a constant
603 603 * in the r_addend field rather than to a symbol value.
604 604 */
605 605 if ((orsp->rel_rtype == M_R_REGISTER) && !sdp) {
606 606 relosp = ofl->ofl_osrel;
607 607 relbits = (char *)relosp->os_outdata->d_buf;
608 608
609 609 rea.r_info = ELF_R_INFO(0,
610 610 ELF_R_TYPE_INFO(RELAUX_GET_TYPEDATA(orsp),
611 611 orsp->rel_rtype));
612 612 rea.r_offset = orsp->rel_roffset;
613 613 rea.r_addend = raddend;
614 614 DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea,
615 615 relosp->os_name, ld_reloc_sym_name(orsp)));
616 616
617 617 assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
618 618 (void) memcpy((relbits + relosp->os_szoutrels),
619 619 (char *)&rea, sizeof (Rela));
620 620 relosp->os_szoutrels += (Xword)sizeof (Rela);
621 621
622 622 return (1);
623 623 }
624 624
625 625 /*
626 626 * If the section this relocation is against has been discarded
627 627 * (-zignore), then also discard (skip) the relocation itself.
628 628 */
629 629 if (orsp->rel_isdesc && ((orsp->rel_flags &
630 630 (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
631 631 (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
632 632 DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
633 633 return (1);
634 634 }
635 635
636 636 /*
637 637 * If this is a relocation against a move table, or expanded move
638 638 * table, adjust the relocation entries.
639 639 */
640 640 if (RELAUX_GET_MOVE(orsp))
641 641 ld_adj_movereloc(ofl, orsp);
642 642
643 643 /*
644 644 * If this is a relocation against a section then we need to adjust the
645 645 * raddend field to compensate for the new position of the input section
646 646 * within the new output section.
647 647 */
648 648 if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
649 649 if (ofl->ofl_parsyms &&
650 650 (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
651 651 (psym = ld_am_I_partial(orsp, orsp->rel_raddend))) {
652 652 /*
653 653 * If the symbol is moved, adjust the value
654 654 */
655 655 DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
656 656 sectmoved = 1;
657 657 if (ofl->ofl_flags & FLG_OF_RELOBJ)
658 658 raddend = psym->sd_sym->st_value;
659 659 else
660 660 raddend = psym->sd_sym->st_value -
661 661 psym->sd_isc->is_osdesc->os_shdr->sh_addr;
662 662 /* LINTED */
663 663 raddend += (Off)_elf_getxoff(psym->sd_isc->is_indata);
664 664 if (psym->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
665 665 raddend +=
666 666 psym->sd_isc->is_osdesc->os_shdr->sh_addr;
667 667 } else {
668 668 /* LINTED */
669 669 raddend += (Off)_elf_getxoff(sdp->sd_isc->is_indata);
670 670 if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
671 671 raddend +=
672 672 sdp->sd_isc->is_osdesc->os_shdr->sh_addr;
673 673 }
674 674 }
675 675
676 676 value = sdp->sd_sym->st_value;
677 677
678 678 if (orsp->rel_flags & FLG_REL_GOT) {
679 679 osp = ofl->ofl_osgot;
680 680 roffset = ld_calc_got_offset(orsp, ofl);
681 681
682 682 } else if (orsp->rel_flags & FLG_REL_PLT) {
683 683 osp = ofl->ofl_osplt;
684 684 plt_entry(ofl, sdp->sd_aux->sa_PLTndx, &roffset, &raddend);
685 685 } else if (orsp->rel_flags & FLG_REL_BSS) {
686 686 /*
687 687 * This must be a R_SPARC_COPY. For these set the roffset to
688 688 * point to the new symbols location.
689 689 */
690 690 osp = ofl->ofl_isbss->is_osdesc;
691 691 roffset = (Xword)value;
692 692
693 693 /*
694 694 * The raddend doesn't mean anything in an R_SPARC_COPY
695 695 * relocation. Null it out because it can confuse people.
696 696 */
697 697 raddend = 0;
698 698 } else if (orsp->rel_flags & FLG_REL_REG) {
699 699 /*
700 700 * The offsets of relocations against register symbols
701 701 * identifiy the register directly - so the offset
702 702 * does not need to be adjusted.
703 703 */
704 704 roffset = orsp->rel_roffset;
705 705 } else {
706 706 osp = RELAUX_GET_OSDESC(orsp);
707 707
708 708 /*
709 709 * Calculate virtual offset of reference point; equals offset
710 710 * into section + vaddr of section for loadable sections, or
711 711 * offset plus section displacement for nonloadable sections.
712 712 */
713 713 roffset = orsp->rel_roffset +
714 714 (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
715 715 if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
716 716 roffset += orsp->rel_isdesc->is_osdesc->
717 717 os_shdr->sh_addr;
718 718 }
719 719
720 720 if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
721 721 relosp = ofl->ofl_osrel;
722 722
723 723 /*
724 724 * Verify that the output relocations offset meets the
725 725 * alignment requirements of the relocation being processed.
726 726 */
727 727 rep = &reloc_table[orsp->rel_rtype];
728 728 if (((flags & FLG_OF_RELOBJ) || !(dtflags1 & DF_1_NORELOC)) &&
729 729 !(rep->re_flags & FLG_RE_UNALIGN)) {
730 730 if (((rep->re_fsize == 2) && (roffset & 0x1)) ||
731 731 ((rep->re_fsize == 4) && (roffset & 0x3)) ||
732 732 ((rep->re_fsize == 8) && (roffset & 0x7))) {
733 733 Conv_inv_buf_t inv_buf;
734 734
735 735 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_NONALIGN),
736 736 conv_reloc_SPARC_type(orsp->rel_rtype, 0, &inv_buf),
737 737 orsp->rel_isdesc->is_file->ifl_name,
738 738 ld_reloc_sym_name(orsp), EC_XWORD(roffset));
739 739 return (S_ERROR);
740 740 }
741 741 }
742 742
743 743 /*
744 744 * Assign the symbols index for the output relocation. If the
745 745 * relocation refers to a SECTION symbol then it's index is based upon
746 746 * the output sections symbols index. Otherwise the index can be
747 747 * derived from the symbols index itself.
748 748 */
749 749 if (orsp->rel_rtype == R_SPARC_RELATIVE)
750 750 ndx = STN_UNDEF;
751 751 else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
752 752 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
753 753 if (sectmoved == 0) {
754 754 /*
755 755 * Check for a null input section. This can
756 756 * occur if this relocation references a symbol
757 757 * generated by sym_add_sym().
758 758 */
759 759 if (sdp->sd_isc && sdp->sd_isc->is_osdesc)
760 760 ndx = sdp->sd_isc->is_osdesc->os_identndx;
761 761 else
762 762 ndx = sdp->sd_shndx;
763 763 } else
764 764 ndx = ofl->ofl_parexpnndx;
765 765 } else
766 766 ndx = sdp->sd_symndx;
767 767
768 768 /*
769 769 * Add the symbols 'value' to the addend field.
770 770 */
771 771 if (orsp->rel_flags & FLG_REL_ADVAL)
↓ open down ↓ |
613 lines elided |
↑ open up ↑ |
772 772 raddend += value;
773 773
774 774 /*
775 775 * The addend field for R_SPARC_TLS_DTPMOD32 and R_SPARC_TLS_DTPMOD64
776 776 * mean nothing. The addend is propagated in the corresponding
777 777 * R_SPARC_TLS_DTPOFF* relocations.
778 778 */
779 779 if (orsp->rel_rtype == M_R_DTPMOD)
780 780 raddend = 0;
781 781
782 - relbits = (char *)relosp->os_outdata->d_buf;
782 + /*
783 + * Note that the other case which writes out the relocation, above, is
784 + * M_R_REGISTER specific and so does not need this check.
785 + */
786 + if ((orsp->rel_rtype != M_R_NONE) &&
787 + (orsp->rel_rtype != M_R_REGISTER) &&
788 + (orsp->rel_rtype != M_R_RELATIVE)) {
789 + if (ndx == 0) {
790 + Conv_inv_buf_t inv_buf;
791 + Is_desc *isp = orsp->rel_isdesc;
792 +
793 + ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_NOSYMBOL),
794 + conv_reloc_type(ofl->ofl_nehdr->e_machine,
795 + orsp->rel_rtype, 0, &inv_buf),
796 + isp->is_file->ifl_name, EC_WORD(isp->is_scnndx),
797 + isp->is_name, EC_XWORD(roffset));
798 + return (S_ERROR);
799 + }
800 + }
783 801
784 802 rea.r_info = ELF_R_INFO(ndx,
785 803 ELF_R_TYPE_INFO(RELAUX_GET_TYPEDATA(orsp), orsp->rel_rtype));
786 804 rea.r_offset = roffset;
787 805 rea.r_addend = raddend;
788 806 DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, relosp->os_name,
789 807 ld_reloc_sym_name(orsp)));
790 808
791 809 /*
792 810 * Assert we haven't walked off the end of our relocation table.
793 811 */
794 812 assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
795 813
814 + relbits = (char *)relosp->os_outdata->d_buf;
815 +
796 816 (void) memcpy((relbits + relosp->os_szoutrels),
797 817 (char *)&rea, sizeof (Rela));
798 818 relosp->os_szoutrels += (Xword)sizeof (Rela);
799 819
800 820 /*
801 821 * Determine if this relocation is against a non-writable, allocatable
802 822 * section. If so we may need to provide a text relocation diagnostic.
803 823 */
804 824 ld_reloc_remain_entry(orsp, osp, ofl, remain_seen);
805 825 return (1);
806 826 }
807 827
808 828
809 829 /*
810 830 * Sparc Instructions for TLS processing
811 831 */
812 832 #if defined(_ELF64)
813 833 #define TLS_GD_IE_LD 0xd0580000 /* ldx [%g0 + %g0], %o0 */
814 834 #else
815 835 #define TLS_GD_IE_LD 0xd0000000 /* ld [%g0 + %g0], %o0 */
816 836 #endif
817 837 #define TLS_GD_IE_ADD 0x9001c008 /* add %g7, %o0, %o0 */
818 838
819 839 #define TLS_GD_LE_XOR 0x80182000 /* xor %g0, 0, %g0 */
820 840 #define TLS_IE_LE_OR 0x80100000 /* or %g0, %o0, %o1 */
821 841 /* synthetic: mov %g0, %g0 */
822 842
823 843 #define TLS_LD_LE_CLRO0 0x90100000 /* clr %o0 */
824 844
825 845 #define FM3_REG_MSK_RD (0x1f << 25) /* Formate (3) rd register mask */
826 846 /* bits 25->29 */
827 847 #define FM3_REG_MSK_RS1 (0x1f << 14) /* Formate (3) rs1 register mask */
828 848 /* bits 14->18 */
829 849 #define FM3_REG_MSK_RS2 0x1f /* Formate (3) rs2 register mask */
830 850 /* bits 0->4 */
831 851
832 852 #define REG_G7 7 /* %g7 register */
833 853
834 854 static Fixupret
835 855 tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
836 856 {
837 857 Sym_desc *sdp = arsp->rel_sym;
838 858 Word rtype = arsp->rel_rtype;
839 859 Word *offset, w;
840 860 int bswap = OFL_SWAP_RELOC_DATA(ofl, arsp);
841 861
842 862
843 863 offset = (Word *)((uintptr_t)arsp->rel_roffset +
844 864 (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
845 865 (uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf);
846 866
847 867 if (sdp->sd_ref == REF_DYN_NEED) {
848 868 /*
849 869 * IE reference model
850 870 */
851 871 switch (rtype) {
852 872 case R_SPARC_TLS_GD_HI22:
853 873 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
854 874 R_SPARC_TLS_IE_HI22, arsp,
855 875 ld_reloc_sym_name));
856 876 arsp->rel_rtype = R_SPARC_TLS_IE_HI22;
857 877 return (FIX_RELOC);
858 878
859 879 case R_SPARC_TLS_GD_LO10:
860 880 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
861 881 R_SPARC_TLS_IE_LO10, arsp,
862 882 ld_reloc_sym_name));
863 883 arsp->rel_rtype = R_SPARC_TLS_IE_LO10;
864 884 return (FIX_RELOC);
865 885
866 886 case R_SPARC_TLS_GD_ADD:
867 887 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
868 888 R_SPARC_NONE, arsp, ld_reloc_sym_name));
869 889 w = bswap ? ld_bswap_Word(*offset) : *offset;
870 890 w = (TLS_GD_IE_LD |
871 891 (w & (FM3_REG_MSK_RS1 | FM3_REG_MSK_RS2)));
872 892 *offset = bswap ? ld_bswap_Word(w) : w;
873 893 return (FIX_DONE);
874 894
875 895 case R_SPARC_TLS_GD_CALL:
876 896 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
877 897 R_SPARC_NONE, arsp, ld_reloc_sym_name));
878 898 *offset = TLS_GD_IE_ADD;
879 899 if (bswap)
880 900 *offset = ld_bswap_Word(*offset);
881 901 return (FIX_DONE);
882 902 }
883 903 return (FIX_RELOC);
884 904 }
885 905
886 906 /*
887 907 * LE reference model
888 908 */
889 909 switch (rtype) {
890 910 case R_SPARC_TLS_IE_HI22:
891 911 case R_SPARC_TLS_GD_HI22:
892 912 case R_SPARC_TLS_LDO_HIX22:
893 913 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
894 914 R_SPARC_TLS_LE_HIX22, arsp, ld_reloc_sym_name));
895 915 arsp->rel_rtype = R_SPARC_TLS_LE_HIX22;
896 916 return (FIX_RELOC);
897 917
898 918 case R_SPARC_TLS_LDO_LOX10:
899 919 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
900 920 R_SPARC_TLS_LE_LOX10, arsp, ld_reloc_sym_name));
901 921 arsp->rel_rtype = R_SPARC_TLS_LE_LOX10;
902 922 return (FIX_RELOC);
903 923
904 924 case R_SPARC_TLS_IE_LO10:
905 925 case R_SPARC_TLS_GD_LO10:
906 926 /*
907 927 * Current instruction is:
908 928 *
909 929 * or r1, %lo(x), r2
910 930 * or
911 931 * add r1, %lo(x), r2
912 932 *
913 933 * Need to udpate this to:
914 934 *
915 935 * xor r1, %lox(x), r2
916 936 */
917 937 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
918 938 R_SPARC_TLS_LE_LOX10, arsp, ld_reloc_sym_name));
919 939 w = bswap ? ld_bswap_Word(*offset) : *offset;
↓ open down ↓ |
114 lines elided |
↑ open up ↑ |
920 940 w = TLS_GD_LE_XOR |
921 941 (w & (FM3_REG_MSK_RS1 | FM3_REG_MSK_RD));
922 942 *offset = bswap ? ld_bswap_Word(w) : w;
923 943 arsp->rel_rtype = R_SPARC_TLS_LE_LOX10;
924 944 return (FIX_RELOC);
925 945
926 946 case R_SPARC_TLS_IE_LD:
927 947 case R_SPARC_TLS_IE_LDX:
928 948 /*
929 949 * Current instruction:
930 - * ld{x} [r1 + r2], r3
950 + * ld{x} [r1 + r2], r3
931 951 *
932 952 * Need to update this to:
933 953 *
934 954 * mov r2, r3 (or %g0, r2, r3)
935 955 */
936 956 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
937 957 R_SPARC_NONE, arsp, ld_reloc_sym_name));
938 958 w = bswap ? ld_bswap_Word(*offset) : *offset;
939 959 w = (w & (FM3_REG_MSK_RS2 | FM3_REG_MSK_RD)) | TLS_IE_LE_OR;
940 960 *offset = bswap ? ld_bswap_Word(w) : w;
941 961 return (FIX_DONE);
942 962
943 963 case R_SPARC_TLS_LDO_ADD:
944 964 case R_SPARC_TLS_GD_ADD:
945 965 /*
946 966 * Current instruction is:
947 967 *
948 968 * add gptr_reg, r2, r3
949 969 *
950 970 * Need to updated this to:
951 971 *
952 972 * add %g7, r2, r3
953 973 */
954 974 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
955 975 R_SPARC_NONE, arsp, ld_reloc_sym_name));
956 976 w = bswap ? ld_bswap_Word(*offset) : *offset;
957 977 w = w & (~FM3_REG_MSK_RS1);
958 978 w = w | (REG_G7 << 14);
959 979 *offset = bswap ? ld_bswap_Word(w) : w;
960 980 return (FIX_DONE);
961 981
962 982 case R_SPARC_TLS_LDM_CALL:
963 983 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
964 984 R_SPARC_NONE, arsp, ld_reloc_sym_name));
965 985 *offset = TLS_LD_LE_CLRO0;
966 986 if (bswap)
967 987 *offset = ld_bswap_Word(*offset);
968 988 return (FIX_DONE);
969 989
970 990 case R_SPARC_TLS_LDM_HI22:
971 991 case R_SPARC_TLS_LDM_LO10:
972 992 case R_SPARC_TLS_LDM_ADD:
973 993 case R_SPARC_TLS_IE_ADD:
974 994 case R_SPARC_TLS_GD_CALL:
975 995 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
976 996 R_SPARC_NONE, arsp, ld_reloc_sym_name));
977 997 *offset = M_NOP;
978 998 if (bswap)
979 999 *offset = ld_bswap_Word(*offset);
980 1000 return (FIX_DONE);
981 1001 }
982 1002 return (FIX_RELOC);
983 1003 }
984 1004
985 1005 #define GOTOP_ADDINST 0x80000000 /* add %g0, %g0, %g0 */
986 1006
987 1007 static Fixupret
988 1008 gotop_fixups(Ofl_desc *ofl, Rel_desc *arsp)
989 1009 {
990 1010 Word rtype = arsp->rel_rtype;
991 1011 Word *offset, w;
992 1012 const char *ifl_name;
993 1013 Conv_inv_buf_t inv_buf;
994 1014 int bswap;
995 1015
996 1016 switch (rtype) {
997 1017 case R_SPARC_GOTDATA_OP_HIX22:
998 1018 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
999 1019 R_SPARC_GOTDATA_HIX22, arsp, ld_reloc_sym_name));
1000 1020 arsp->rel_rtype = R_SPARC_GOTDATA_HIX22;
1001 1021 return (FIX_RELOC);
↓ open down ↓ |
61 lines elided |
↑ open up ↑ |
1002 1022
1003 1023 case R_SPARC_GOTDATA_OP_LOX10:
1004 1024 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
1005 1025 R_SPARC_GOTDATA_LOX10, arsp, ld_reloc_sym_name));
1006 1026 arsp->rel_rtype = R_SPARC_GOTDATA_LOX10;
1007 1027 return (FIX_RELOC);
1008 1028
1009 1029 case R_SPARC_GOTDATA_OP:
1010 1030 /*
1011 1031 * Current instruction:
1012 - * ld{x} [r1 + r2], r3
1032 + * ld{x} [r1 + r2], r3
1013 1033 *
1014 1034 * Need to update this to:
1015 1035 *
1016 1036 * add r1, r2, r3
1017 1037 */
1018 1038 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
1019 1039 R_SPARC_NONE, arsp, ld_reloc_sym_name));
1020 1040 offset = (Word *)(uintptr_t)(arsp->rel_roffset +
1021 1041 _elf_getxoff(arsp->rel_isdesc->is_indata) +
1022 1042 (uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf);
1023 1043 bswap = OFL_SWAP_RELOC_DATA(ofl, arsp);
1024 1044 w = bswap ? ld_bswap_Word(*offset) : *offset;
1025 1045 w = (w & (FM3_REG_MSK_RS1 |
1026 1046 FM3_REG_MSK_RS2 | FM3_REG_MSK_RD)) | GOTOP_ADDINST;
1027 1047 *offset = bswap ? ld_bswap_Word(w) : w;
1028 1048 return (FIX_DONE);
1029 1049 }
1030 1050 /*
1031 1051 * We should not get here
1032 1052 */
1033 1053 if (arsp->rel_isdesc->is_file)
1034 1054 ifl_name = arsp->rel_isdesc->is_file->ifl_name;
1035 1055 else
1036 1056 ifl_name = MSG_INTL(MSG_STR_NULL);
1037 1057
1038 1058 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_BADGOTFIX),
1039 1059 conv_reloc_SPARC_type(arsp->rel_rtype, 0, &inv_buf),
1040 1060 ifl_name, ld_reloc_sym_name(arsp));
1041 1061
1042 1062 assert(0);
1043 1063 return (FIX_ERROR);
1044 1064 }
1045 1065
1046 1066 static uintptr_t
1047 1067 ld_do_activerelocs(Ofl_desc *ofl)
1048 1068 {
1049 1069 Rel_desc *arsp;
1050 1070 Rel_cachebuf *rcbp;
1051 1071 Aliste idx;
1052 1072 uintptr_t return_code = 1;
1053 1073 ofl_flag_t flags = ofl->ofl_flags;
1054 1074
1055 1075 if (aplist_nitems(ofl->ofl_actrels.rc_list) != 0)
1056 1076 DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
1057 1077
1058 1078 /*
1059 1079 * Process active relocations.
1060 1080 */
1061 1081 REL_CACHE_TRAVERSE(&ofl->ofl_actrels, idx, rcbp, arsp) {
1062 1082 uchar_t *addr;
1063 1083 Xword value;
1064 1084 Sym_desc *sdp;
1065 1085 const char *ifl_name;
1066 1086 Xword refaddr;
1067 1087 Os_desc *osp;
1068 1088
1069 1089 /*
1070 1090 * If the section this relocation is against has been discarded
1071 1091 * (-zignore), then discard (skip) the relocation itself.
1072 1092 */
1073 1093 if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
1074 1094 ((arsp->rel_flags & (FLG_REL_GOT | FLG_REL_BSS |
1075 1095 FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
1076 1096 DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, arsp));
1077 1097 continue;
1078 1098 }
1079 1099
1080 1100 /*
1081 1101 * Perform any required TLS fixups.
1082 1102 */
1083 1103 if (arsp->rel_flags & FLG_REL_TLSFIX) {
1084 1104 Fixupret ret;
1085 1105
1086 1106 if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
1087 1107 return (S_ERROR);
1088 1108 if (ret == FIX_DONE)
1089 1109 continue;
1090 1110 }
1091 1111
1092 1112 /*
1093 1113 * Perform any required GOTOP fixups.
1094 1114 */
1095 1115 if (arsp->rel_flags & FLG_REL_GOTFIX) {
1096 1116 Fixupret ret;
1097 1117
1098 1118 if ((ret = gotop_fixups(ofl, arsp)) == FIX_ERROR)
1099 1119 return (S_ERROR);
1100 1120 if (ret == FIX_DONE)
1101 1121 continue;
1102 1122 }
1103 1123
1104 1124 /*
1105 1125 * If this is a relocation against the move table, or
1106 1126 * expanded move table, adjust the relocation entries.
1107 1127 */
1108 1128 if (RELAUX_GET_MOVE(arsp))
1109 1129 ld_adj_movereloc(ofl, arsp);
1110 1130
1111 1131 sdp = arsp->rel_sym;
1112 1132 refaddr = arsp->rel_roffset +
1113 1133 (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
1114 1134
1115 1135 if ((arsp->rel_flags & FLG_REL_CLVAL) ||
1116 1136 (arsp->rel_flags & FLG_REL_GOTCL))
1117 1137 value = 0;
1118 1138 else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
1119 1139 Sym_desc *sym;
1120 1140
1121 1141 /*
1122 1142 * The value for a symbol pointing to a SECTION
1123 1143 * is based off of that sections position.
1124 1144 */
1125 1145 if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
1126 1146 (sym = ld_am_I_partial(arsp, arsp->rel_raddend))) {
1127 1147 /*
1128 1148 * The symbol was moved, so adjust the value
1129 1149 * relative to the new section.
1130 1150 */
1131 1151 value = _elf_getxoff(sym->sd_isc->is_indata);
1132 1152 if (sym->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
1133 1153 value += sym->sd_isc->
1134 1154 is_osdesc->os_shdr->sh_addr;
1135 1155
1136 1156 /*
1137 1157 * The original raddend covers the displacement
1138 1158 * from the section start to the desired
1139 1159 * address. The value computed above gets us
1140 1160 * from the section start to the start of the
1141 1161 * symbol range. Adjust the old raddend to
1142 1162 * remove the offset from section start to
1143 1163 * symbol start, leaving the displacement
1144 1164 * within the range of the symbol.
1145 1165 */
1146 1166 arsp->rel_raddend -= sym->sd_osym->st_value;
1147 1167 } else {
1148 1168 value = _elf_getxoff(sdp->sd_isc->is_indata);
1149 1169 if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
1150 1170 value += sdp->sd_isc->
1151 1171 is_osdesc->os_shdr->sh_addr;
1152 1172 }
1153 1173
1154 1174 if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
1155 1175 value -= ofl->ofl_tlsphdr->p_vaddr;
1156 1176
1157 1177 } else if (IS_SIZE(arsp->rel_rtype)) {
1158 1178 /*
1159 1179 * Size relocations require the symbols size.
1160 1180 */
1161 1181 value = sdp->sd_sym->st_size;
1162 1182
1163 1183 } else if ((sdp->sd_flags & FLG_SY_CAP) &&
1164 1184 sdp->sd_aux && sdp->sd_aux->sa_PLTndx) {
1165 1185 /*
1166 1186 * If relocation is against a capabilities symbol, we
1167 1187 * need to jump to an associated PLT, so that at runtime
1168 1188 * ld.so.1 is involved to determine the best binding
1169 1189 * choice. Otherwise, the value is the symbols value.
1170 1190 */
1171 1191 value = ld_calc_plt_addr(sdp, ofl);
1172 1192
1173 1193 } else
1174 1194 value = sdp->sd_sym->st_value;
1175 1195
1176 1196 /*
1177 1197 * Relocation against the GLOBAL_OFFSET_TABLE.
1178 1198 */
1179 1199 if ((arsp->rel_flags & FLG_REL_GOT) &&
1180 1200 !ld_reloc_set_aux_osdesc(ofl, arsp, ofl->ofl_osgot))
1181 1201 return (S_ERROR);
1182 1202 osp = RELAUX_GET_OSDESC(arsp);
1183 1203
1184 1204 /*
1185 1205 * If loadable and not producing a relocatable object add the
1186 1206 * sections virtual address to the reference address.
1187 1207 */
1188 1208 if ((arsp->rel_flags & FLG_REL_LOAD) &&
1189 1209 ((flags & FLG_OF_RELOBJ) == 0))
1190 1210 refaddr +=
1191 1211 arsp->rel_isdesc->is_osdesc->os_shdr->sh_addr;
1192 1212
1193 1213 /*
1194 1214 * If this entry has a PLT assigned to it, its value is actually
1195 1215 * the address of the PLT (and not the address of the function).
1196 1216 */
1197 1217 if (IS_PLT(arsp->rel_rtype)) {
1198 1218 if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
1199 1219 value = ld_calc_plt_addr(sdp, ofl);
1200 1220 }
1201 1221
1202 1222 /*
1203 1223 * Add relocations addend to value. Add extra
1204 1224 * relocation addend if needed.
1205 1225 */
1206 1226 value += arsp->rel_raddend;
1207 1227 if (IS_EXTOFFSET(arsp->rel_rtype))
1208 1228 value += RELAUX_GET_TYPEDATA(arsp);
1209 1229
1210 1230 /*
1211 1231 * Determine whether the value needs further adjustment. Filter
1212 1232 * through the attributes of the relocation to determine what
1213 1233 * adjustment is required. Note, many of the following cases
1214 1234 * are only applicable when a .got is present. As a .got is
1215 1235 * not generated when a relocatable object is being built,
1216 1236 * any adjustments that require a .got need to be skipped.
1217 1237 */
1218 1238 if ((arsp->rel_flags & FLG_REL_GOT) &&
1219 1239 ((flags & FLG_OF_RELOBJ) == 0)) {
1220 1240 Xword R1addr;
1221 1241 uintptr_t R2addr;
1222 1242 Sword gotndx;
1223 1243 Gotndx *gnp;
1224 1244 Gotref gref;
1225 1245
1226 1246 /*
1227 1247 * Clear the GOT table entry, on SPARC we clear
1228 1248 * the entry and the 'value' if needed is stored
1229 1249 * in an output relocations addend.
1230 1250 *
1231 1251 * Calculate offset into GOT at which to apply
1232 1252 * the relocation.
1233 1253 */
1234 1254 if (arsp->rel_flags & FLG_REL_DTLS)
1235 1255 gref = GOT_REF_TLSGD;
1236 1256 else if (arsp->rel_flags & FLG_REL_MTLS)
1237 1257 gref = GOT_REF_TLSLD;
1238 1258 else if (arsp->rel_flags & FLG_REL_STLS)
1239 1259 gref = GOT_REF_TLSIE;
1240 1260 else
1241 1261 gref = GOT_REF_GENERIC;
1242 1262
1243 1263 gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
1244 1264 assert(gnp);
1245 1265
1246 1266 if (arsp->rel_rtype == M_R_DTPOFF)
1247 1267 gotndx = gnp->gn_gotndx + 1;
1248 1268 else
1249 1269 gotndx = gnp->gn_gotndx;
1250 1270
1251 1271 /* LINTED */
1252 1272 R1addr = (Xword)((-neggotoffset * M_GOT_ENTSIZE) +
1253 1273 (gotndx * M_GOT_ENTSIZE));
1254 1274
1255 1275 /*
1256 1276 * Add the GOTs data's offset.
1257 1277 */
1258 1278 R2addr = R1addr + (uintptr_t)osp->os_outdata->d_buf;
1259 1279
1260 1280 DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml,
1261 1281 ELF_DBG_LD_ACT, M_MACH, SHT_RELA,
1262 1282 arsp, R1addr, value, ld_reloc_sym_name));
1263 1283
1264 1284 /*
1265 1285 * And do it.
1266 1286 */
1267 1287 if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
1268 1288 *(Xword *)R2addr = ld_bswap_Xword(value);
1269 1289 else
1270 1290 *(Xword *)R2addr = value;
1271 1291 continue;
1272 1292
1273 1293 } else if (IS_GOT_BASED(arsp->rel_rtype) &&
1274 1294 ((flags & FLG_OF_RELOBJ) == 0)) {
1275 1295 value -= (ofl->ofl_osgot->os_shdr->sh_addr +
1276 1296 (-neggotoffset * M_GOT_ENTSIZE));
1277 1297
1278 1298 } else if (IS_PC_RELATIVE(arsp->rel_rtype)) {
1279 1299 value -= refaddr;
1280 1300
1281 1301 } else if (IS_TLS_INS(arsp->rel_rtype) &&
1282 1302 IS_GOT_RELATIVE(arsp->rel_rtype) &&
1283 1303 ((flags & FLG_OF_RELOBJ) == 0)) {
1284 1304 Gotndx *gnp;
1285 1305 Gotref gref;
1286 1306
1287 1307 if (arsp->rel_flags & FLG_REL_STLS)
1288 1308 gref = GOT_REF_TLSIE;
1289 1309 else if (arsp->rel_flags & FLG_REL_DTLS)
1290 1310 gref = GOT_REF_TLSGD;
1291 1311 else if (arsp->rel_flags & FLG_REL_MTLS)
1292 1312 gref = GOT_REF_TLSLD;
1293 1313
1294 1314 gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
1295 1315 assert(gnp);
1296 1316
1297 1317 value = gnp->gn_gotndx * M_GOT_ENTSIZE;
1298 1318
1299 1319 } else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
1300 1320 ((flags & FLG_OF_RELOBJ) == 0)) {
1301 1321 Gotndx *gnp;
1302 1322
1303 1323 gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
1304 1324 GOT_REF_GENERIC, ofl, arsp);
1305 1325 assert(gnp);
1306 1326
1307 1327 value = gnp->gn_gotndx * M_GOT_ENTSIZE;
1308 1328
1309 1329 } else if ((arsp->rel_flags & FLG_REL_STLS) &&
1310 1330 ((flags & FLG_OF_RELOBJ) == 0)) {
1311 1331 Xword tlsstatsize;
1312 1332
1313 1333 /*
1314 1334 * This is the LE TLS reference model. Static offset is
1315 1335 * hard-coded, and negated so that it can be added to
1316 1336 * the thread pointer (%g7)
1317 1337 */
1318 1338 tlsstatsize =
1319 1339 S_ROUND(ofl->ofl_tlsphdr->p_memsz, M_TLSSTATALIGN);
1320 1340 value = -(tlsstatsize - value);
1321 1341 }
1322 1342
1323 1343 if (arsp->rel_isdesc->is_file)
1324 1344 ifl_name = arsp->rel_isdesc->is_file->ifl_name;
1325 1345 else
1326 1346 ifl_name = MSG_INTL(MSG_STR_NULL);
1327 1347
1328 1348 /*
1329 1349 * Make sure we have data to relocate. Compiler and assembler
1330 1350 * developers have been known to generate relocations against
1331 1351 * invalid sections (normally .bss), so for their benefit give
1332 1352 * them sufficient information to help analyze the problem.
1333 1353 * End users should never see this.
1334 1354 */
1335 1355 if (arsp->rel_isdesc->is_indata->d_buf == 0) {
1336 1356 Conv_inv_buf_t inv_buf;
1337 1357
1338 1358 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_EMPTYSEC),
1339 1359 conv_reloc_SPARC_type(arsp->rel_rtype, 0, &inv_buf),
1340 1360 ifl_name, ld_reloc_sym_name(arsp),
1341 1361 EC_WORD(arsp->rel_isdesc->is_scnndx),
1342 1362 arsp->rel_isdesc->is_name);
1343 1363 return (S_ERROR);
1344 1364 }
1345 1365
1346 1366 /*
1347 1367 * Get the address of the data item we need to modify.
1348 1368 */
1349 1369 addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
1350 1370 (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata));
1351 1371
1352 1372 DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
1353 1373 M_MACH, SHT_RELA, arsp, EC_NATPTR(addr), value,
1354 1374 ld_reloc_sym_name));
1355 1375 addr += (uintptr_t)osp->os_outdata->d_buf;
1356 1376
1357 1377 if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1358 1378 ofl->ofl_size) || (arsp->rel_roffset >
1359 1379 osp->os_shdr->sh_size)) {
1360 1380 Conv_inv_buf_t inv_buf;
1361 1381 int class;
1362 1382
1363 1383 if (((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1364 1384 ofl->ofl_size)
1365 1385 class = ERR_FATAL;
1366 1386 else
1367 1387 class = ERR_WARNING;
1368 1388
1369 1389 ld_eprintf(ofl, class, MSG_INTL(MSG_REL_INVALOFFSET),
1370 1390 conv_reloc_SPARC_type(arsp->rel_rtype, 0, &inv_buf),
1371 1391 ifl_name, EC_WORD(arsp->rel_isdesc->is_scnndx),
1372 1392 arsp->rel_isdesc->is_name, ld_reloc_sym_name(arsp),
1373 1393 EC_ADDR((uintptr_t)addr -
1374 1394 (uintptr_t)ofl->ofl_nehdr));
1375 1395
1376 1396 if (class == ERR_FATAL) {
1377 1397 return_code = S_ERROR;
1378 1398 continue;
1379 1399 }
1380 1400 }
1381 1401
1382 1402 /*
1383 1403 * If '-z noreloc' is specified - skip the do_reloc stage.
1384 1404 */
1385 1405 if (OFL_DO_RELOC(ofl)) {
1386 1406 if (do_reloc_ld(arsp, addr, &value, ld_reloc_sym_name,
1387 1407 ifl_name, OFL_SWAP_RELOC_DATA(ofl, arsp),
1388 1408 ofl->ofl_lml) == 0) {
1389 1409 ofl->ofl_flags |= FLG_OF_FATAL;
1390 1410 return_code = S_ERROR;
1391 1411 }
1392 1412 }
1393 1413 }
1394 1414 return (return_code);
1395 1415 }
1396 1416
1397 1417 static uintptr_t
1398 1418 ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
1399 1419 {
1400 1420 Rel_desc *orsp;
1401 1421 Sym_desc *sdp = rsp->rel_sym;
↓ open down ↓ |
379 lines elided |
↑ open up ↑ |
1402 1422 Conv_inv_buf_t inv_buf;
1403 1423
1404 1424 /*
1405 1425 * Static executables *do not* want any relocations against them.
1406 1426 * Since our engine still creates relocations against a WEAK UNDEFINED
1407 1427 * symbol in a static executable, it's best to disable them here
1408 1428 * instead of through out the relocation code.
1409 1429 */
1410 1430 if (OFL_IS_STATIC_EXEC(ofl))
1411 1431 return (1);
1432 +
1433 + /*
1434 + * If the symbol will be reduced, we can't leave outstanding
1435 + * relocations against it, as nothing will ever be able to satisfy them
1436 + * (and the symbol won't be in .dynsym
1437 + */
1438 + if ((sdp != NULL) &&
1439 + (sdp->sd_sym->st_shndx == SHN_UNDEF) &&
1440 + (rsp->rel_rtype != M_R_NONE) &&
1441 + (rsp->rel_rtype != M_R_REGISTER) &&
1442 + (rsp->rel_rtype != M_R_RELATIVE)) {
1443 + if (ld_sym_reducable(ofl, sdp))
1444 + return (1);
1445 + }
1412 1446
1413 1447 /*
1414 1448 * Certain relocations do not make sense in a 64bit shared object,
1415 1449 * if building a shared object do a sanity check on the output
1416 1450 * relocations being created.
1417 1451 */
1418 1452 if (ofl->ofl_flags & FLG_OF_SHAROBJ) {
1419 1453 Word rtype = rsp->rel_rtype;
1420 1454 /*
1421 1455 * Because the R_SPARC_HIPLT22 & R_SPARC_LOPLT10 relocations
1422 1456 * are not relative they make no sense to create in a shared
1423 1457 * object - so emit the proper error message if that occurs.
1424 1458 */
1425 1459 if ((rtype == R_SPARC_HIPLT22) || (rtype == R_SPARC_LOPLT10)) {
1426 1460 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_UNRELREL),
1427 1461 conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
1428 1462 rsp->rel_isdesc->is_file->ifl_name,
1429 1463 ld_reloc_sym_name(rsp));
1430 1464 return (S_ERROR);
1431 1465 }
1432 1466 #if defined(_ELF64)
1433 1467 /*
1434 1468 * Each of the following relocations requires that the
1435 1469 * object being built be loaded in either the upper 32 or
1436 1470 * 44 bit range of memory. Since shared libraries traditionally
1437 1471 * are loaded in the lower range of memory - this isn't going
1438 1472 * to work.
1439 1473 */
1440 1474 if ((rtype == R_SPARC_H44) || (rtype == R_SPARC_M44) ||
1441 1475 (rtype == R_SPARC_L44)) {
1442 1476 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_SHOBJABS44),
1443 1477 conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
1444 1478 rsp->rel_isdesc->is_file->ifl_name,
1445 1479 ld_reloc_sym_name(rsp));
1446 1480 return (S_ERROR);
1447 1481 }
1448 1482 #endif
1449 1483 }
1450 1484
1451 1485 /*
1452 1486 * If we are adding a output relocation against a section
1453 1487 * symbol (non-RELATIVE) then mark that section. These sections
1454 1488 * will be added to the .dynsym symbol table.
1455 1489 */
1456 1490 if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
1457 1491 ((flags & FLG_REL_SCNNDX) ||
1458 1492 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
1459 1493
1460 1494 /*
1461 1495 * If this is a COMMON symbol - no output section
1462 1496 * exists yet - (it's created as part of sym_validate()).
1463 1497 * So - we mark here that when it's created it should
1464 1498 * be tagged with the FLG_OS_OUTREL flag.
1465 1499 */
1466 1500 if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
1467 1501 (sdp->sd_sym->st_shndx == SHN_COMMON)) {
1468 1502 if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
1469 1503 ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
1470 1504 else
1471 1505 ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
1472 1506 } else {
1473 1507 Os_desc *osp;
1474 1508 Is_desc *isp = sdp->sd_isc;
1475 1509
1476 1510 if (isp && ((osp = isp->is_osdesc) != NULL) &&
1477 1511 ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
1478 1512 ofl->ofl_dynshdrcnt++;
1479 1513 osp->os_flags |= FLG_OS_OUTREL;
1480 1514 }
1481 1515 }
1482 1516 }
1483 1517
1484 1518 /* Enter it into the output relocation cache */
1485 1519 if ((orsp = ld_reloc_enter(ofl, &ofl->ofl_outrels, rsp, flags)) == NULL)
1486 1520 return (S_ERROR);
1487 1521
1488 1522 if (flags & FLG_REL_GOT)
1489 1523 ofl->ofl_relocgotsz += (Xword)sizeof (Rela);
1490 1524 else if (flags & FLG_REL_PLT)
1491 1525 ofl->ofl_relocpltsz += (Xword)sizeof (Rela);
1492 1526 else if (flags & FLG_REL_BSS)
1493 1527 ofl->ofl_relocbsssz += (Xword)sizeof (Rela);
1494 1528 else if (flags & FLG_REL_NOINFO)
1495 1529 ofl->ofl_relocrelsz += (Xword)sizeof (Rela);
1496 1530 else
1497 1531 RELAUX_GET_OSDESC(orsp)->os_szoutrels += (Xword)sizeof (Rela);
1498 1532
1499 1533 if (orsp->rel_rtype == M_R_RELATIVE)
1500 1534 ofl->ofl_relocrelcnt++;
1501 1535
1502 1536 #if defined(_ELF64)
1503 1537 /*
1504 1538 * When building a 64-bit object any R_SPARC_WDISP30 relocation is given
1505 1539 * a plt padding entry, unless we're building a relocatable object
1506 1540 * (ld -r) or -b is in effect.
1507 1541 */
1508 1542 if ((orsp->rel_rtype == R_SPARC_WDISP30) &&
1509 1543 ((ofl->ofl_flags & (FLG_OF_BFLAG | FLG_OF_RELOBJ)) == 0) &&
1510 1544 ((orsp->rel_sym->sd_flags & FLG_SY_PLTPAD) == 0)) {
1511 1545 ofl->ofl_pltpad++;
1512 1546 orsp->rel_sym->sd_flags |= FLG_SY_PLTPAD;
1513 1547 }
1514 1548 #endif
1515 1549 /*
1516 1550 * We don't perform sorting on PLT relocations because
1517 1551 * they have already been assigned a PLT index and if we
1518 1552 * were to sort them we would have to re-assign the plt indexes.
1519 1553 */
1520 1554 if (!(flags & FLG_REL_PLT))
1521 1555 ofl->ofl_reloccnt++;
1522 1556
1523 1557 /*
1524 1558 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
1525 1559 */
1526 1560 if (IS_GOT_REQUIRED(orsp->rel_rtype))
1527 1561 ofl->ofl_flags |= FLG_OF_BLDGOT;
1528 1562
1529 1563 /*
1530 1564 * Identify and possibly warn of a displacement relocation.
1531 1565 */
1532 1566 if (orsp->rel_flags & FLG_REL_DISP) {
1533 1567 ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
1534 1568
1535 1569 if (ofl->ofl_flags & FLG_OF_VERBOSE)
1536 1570 ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
1537 1571 }
1538 1572 DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA,
1539 1573 M_MACH, orsp));
1540 1574 return (1);
1541 1575 }
1542 1576
1543 1577 /*
1544 1578 * Process relocation against a register symbol. Note, of -z muldefs is in
1545 1579 * effect there may have been multiple register definitions, which would have
1546 1580 * been processed as non-fatal, with the first definition winning. But, we
1547 1581 * will also process multiple relocations for these multiple definitions. In
1548 1582 * this case we must only preserve the relocation for the definition that was
1549 1583 * kept. The sad part is that register relocations don't typically specify
1550 1584 * the register symbol with which they are associated, so we might have to
1551 1585 * search the input files global symbols to determine if this relocation is
1552 1586 * appropriate.
1553 1587 */
1554 1588 static uintptr_t
1555 1589 ld_reloc_register(Rel_desc *rsp, Is_desc *isp, Ofl_desc *ofl)
1556 1590 {
1557 1591 if (ofl->ofl_flags & FLG_OF_MULDEFS) {
1558 1592 Ifl_desc *ifl = isp->is_file;
1559 1593 Sym_desc *sdp = rsp->rel_sym;
1560 1594
1561 1595 if (sdp == 0) {
1562 1596 Xword offset = rsp->rel_roffset;
1563 1597 Word ndx;
1564 1598
1565 1599 for (ndx = ifl->ifl_locscnt;
1566 1600 ndx < ifl->ifl_symscnt; ndx++) {
1567 1601 if (((sdp = ifl->ifl_oldndx[ndx]) != 0) &&
1568 1602 (sdp->sd_flags & FLG_SY_REGSYM) &&
1569 1603 (sdp->sd_sym->st_value == offset))
1570 1604 break;
1571 1605 }
1572 1606 }
1573 1607 if (sdp && (sdp->sd_file != ifl))
1574 1608 return (1);
1575 1609 }
1576 1610 return (ld_add_outrel((rsp->rel_flags | FLG_REL_REG), rsp, ofl));
1577 1611 }
1578 1612
1579 1613 /*
1580 1614 * process relocation for a LOCAL symbol
1581 1615 */
1582 1616 static uintptr_t
1583 1617 ld_reloc_local(Rel_desc *rsp, Ofl_desc *ofl)
1584 1618 {
1585 1619 ofl_flag_t flags = ofl->ofl_flags;
1586 1620 Sym_desc *sdp = rsp->rel_sym;
1587 1621 Word shndx = sdp->sd_sym->st_shndx;
1588 1622
1589 1623 /*
1590 1624 * if ((shared object) and (not pc relative relocation) and
1591 1625 * (not against ABS symbol))
1592 1626 * then
1593 1627 * if (rtype != R_SPARC_32)
1594 1628 * then
1595 1629 * build relocation against section
1596 1630 * else
1597 1631 * build R_SPARC_RELATIVE
1598 1632 * fi
1599 1633 * fi
1600 1634 */
1601 1635 if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
1602 1636 !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
1603 1637 !(IS_GOT_BASED(rsp->rel_rtype)) &&
1604 1638 !(rsp->rel_isdesc != NULL &&
1605 1639 (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
1606 1640 (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
1607 1641 (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
1608 1642 Word ortype = rsp->rel_rtype;
1609 1643
1610 1644 if ((rsp->rel_rtype != R_SPARC_32) &&
1611 1645 (rsp->rel_rtype != R_SPARC_PLT32) &&
1612 1646 (rsp->rel_rtype != R_SPARC_64))
1613 1647 return (ld_add_outrel((FLG_REL_SCNNDX | FLG_REL_ADVAL),
1614 1648 rsp, ofl));
1615 1649
1616 1650 rsp->rel_rtype = R_SPARC_RELATIVE;
1617 1651 if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR)
1618 1652 return (S_ERROR);
1619 1653 rsp->rel_rtype = ortype;
1620 1654 return (1);
1621 1655 }
1622 1656
1623 1657 /*
1624 1658 * If the relocation is against a 'non-allocatable' section
1625 1659 * and we can not resolve it now - then give a warning
1626 1660 * message.
1627 1661 *
1628 1662 * We can not resolve the symbol if either:
1629 1663 * a) it's undefined
1630 1664 * b) it's defined in a shared library and a
1631 1665 * COPY relocation hasn't moved it to the executable
1632 1666 *
1633 1667 * Note: because we process all of the relocations against the
1634 1668 * text segment before any others - we know whether
1635 1669 * or not a copy relocation will be generated before
1636 1670 * we get here (see reloc_init()->reloc_segments()).
1637 1671 */
1638 1672 if (!(rsp->rel_flags & FLG_REL_LOAD) &&
1639 1673 ((shndx == SHN_UNDEF) ||
1640 1674 ((sdp->sd_ref == REF_DYN_NEED) &&
1641 1675 ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1642 1676 Conv_inv_buf_t inv_buf;
1643 1677 Os_desc *osp = RELAUX_GET_OSDESC(rsp);
1644 1678
1645 1679 /*
1646 1680 * If the relocation is against a SHT_SUNW_ANNOTATE
1647 1681 * section - then silently ignore that the relocation
1648 1682 * can not be resolved.
1649 1683 */
1650 1684 if (osp && (osp->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
1651 1685 return (0);
1652 1686 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
1653 1687 conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
1654 1688 rsp->rel_isdesc->is_file->ifl_name,
1655 1689 ld_reloc_sym_name(rsp), osp->os_name);
1656 1690 return (1);
1657 1691 }
1658 1692
1659 1693 /*
1660 1694 * Perform relocation.
1661 1695 */
1662 1696 return (ld_add_actrel(NULL, rsp, ofl));
1663 1697 }
1664 1698
1665 1699 /*
1666 1700 * Establish a relocation transition. Note, at this point of input relocation
1667 1701 * processing, we have no idea of the relocation value that will be used in
1668 1702 * the eventual relocation calculation. This value is only known after the
1669 1703 * initial image has been constructed. Therefore, there is a small chance
1670 1704 * that a value can exceed the capabilities of the transitioned relocation.
1671 1705 * One example might be the offset from the GOT to a symbol.
1672 1706 *
1673 1707 * The only instance of this failure discovered so far has been via the use of
1674 1708 * ABS symbols to represent an external memory location. This situation is
1675 1709 * rare, since ABS symbols aren't typically generated by the compilers.
1676 1710 * Therefore, our solution is to excluded ABS symbols from the transition
1677 1711 * relocation possibilities. As an additional safeguard, if an inappropriate
1678 1712 * value is passed to the final relocation engine, a verification ("V")
1679 1713 * relocation should trigger a fatal error condition.
1680 1714 */
1681 1715 static uintptr_t
1682 1716 ld_reloc_GOTOP(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
1683 1717 {
1684 1718 Word rtype = rsp->rel_rtype;
1685 1719
1686 1720 if (!local || (rsp->rel_sym->sd_sym->st_shndx == SHN_ABS)) {
1687 1721 /*
1688 1722 * When binding to a external symbol, no fixups are required
1689 1723 * and the GOTDATA_OP relocation can be ignored.
1690 1724 */
1691 1725 if (rtype == R_SPARC_GOTDATA_OP)
1692 1726 return (1);
1693 1727 return (ld_reloc_GOT_relative(local, rsp, ofl));
1694 1728 }
1695 1729
1696 1730 /*
1697 1731 * When binding to a local symbol the relocations can be transitioned:
1698 1732 *
1699 1733 * R_*_GOTDATA_OP_HIX22 -> R_*_GOTDATA_HIX22
1700 1734 * R_*_GOTDATA_OP_LOX10 -> R_*_GOTDATA_LOX10
1701 1735 * R_*_GOTDATA_OP -> instruction fixup
1702 1736 */
1703 1737 return (ld_add_actrel(FLG_REL_GOTFIX, rsp, ofl));
1704 1738 }
1705 1739
1706 1740 static uintptr_t
1707 1741 ld_reloc_TLS(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
1708 1742 {
1709 1743 Word rtype = rsp->rel_rtype;
1710 1744 Sym_desc *sdp = rsp->rel_sym;
1711 1745 ofl_flag_t flags = ofl->ofl_flags;
1712 1746 Gotndx *gnp;
1713 1747
1714 1748 /*
1715 1749 * If we're building an executable - use either the IE or LE access
1716 1750 * model. If we're building a shared object process any IE model.
1717 1751 */
1718 1752 if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
1719 1753 /*
1720 1754 * Set the DF_STATIC_TLS flag.
1721 1755 */
1722 1756 ofl->ofl_dtflags |= DF_STATIC_TLS;
1723 1757
1724 1758 if (!local || ((flags & FLG_OF_EXEC) == 0)) {
1725 1759 /*
1726 1760 * When processing static TLS - these relocations
1727 1761 * can be ignored.
1728 1762 */
1729 1763 if ((rtype == R_SPARC_TLS_IE_LD) ||
1730 1764 (rtype == R_SPARC_TLS_IE_LDX) ||
1731 1765 (rtype == R_SPARC_TLS_IE_ADD))
1732 1766 return (1);
1733 1767
1734 1768 /*
1735 1769 * Assign a GOT entry for IE static TLS references.
1736 1770 */
1737 1771 if (((rtype == R_SPARC_TLS_GD_HI22) ||
1738 1772 (rtype == R_SPARC_TLS_GD_LO10) ||
1739 1773 (rtype == R_SPARC_TLS_IE_HI22) ||
1740 1774 (rtype == R_SPARC_TLS_IE_LO10)) &&
1741 1775 ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
1742 1776 GOT_REF_TLSIE, ofl, rsp)) == NULL)) {
1743 1777
1744 1778 if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1745 1779 gnp, GOT_REF_TLSIE, FLG_REL_STLS,
1746 1780 rtype, M_R_TPOFF, NULL) == S_ERROR)
1747 1781 return (S_ERROR);
1748 1782 }
1749 1783
1750 1784 /*
1751 1785 * IE access model.
1752 1786 */
1753 1787 if (IS_TLS_IE(rtype))
1754 1788 return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1755 1789
1756 1790 /*
1757 1791 * Fixups are required for other executable models.
1758 1792 */
1759 1793 return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
1760 1794 rsp, ofl));
1761 1795 }
1762 1796
1763 1797 /*
1764 1798 * LE access model.
1765 1799 */
1766 1800 if (IS_TLS_LE(rtype))
1767 1801 return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1768 1802
1769 1803 /*
1770 1804 * When processing static TLS - these relocations can be
1771 1805 * ignored.
1772 1806 */
1773 1807 if (rtype == R_SPARC_TLS_IE_ADD)
1774 1808 return (1);
1775 1809
1776 1810 return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
1777 1811 rsp, ofl));
1778 1812 }
1779 1813
1780 1814 /*
1781 1815 * Building a shared object.
1782 1816 *
1783 1817 * For dynamic TLS references, ADD relocations are ignored.
1784 1818 */
1785 1819 if ((rtype == R_SPARC_TLS_GD_ADD) || (rtype == R_SPARC_TLS_LDM_ADD) ||
1786 1820 (rtype == R_SPARC_TLS_LDO_ADD))
1787 1821 return (1);
1788 1822
1789 1823 /*
1790 1824 * Assign a GOT entry for a dynamic TLS reference.
1791 1825 */
1792 1826 if (((rtype == R_SPARC_TLS_LDM_HI22) ||
1793 1827 (rtype == R_SPARC_TLS_LDM_LO10)) &&
1794 1828 ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSLD,
1795 1829 ofl, rsp)) == NULL)) {
1796 1830
1797 1831 if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
1798 1832 FLG_REL_MTLS, rtype, M_R_DTPMOD, 0) == S_ERROR)
1799 1833 return (S_ERROR);
1800 1834
1801 1835 } else if (((rtype == R_SPARC_TLS_GD_HI22) ||
1802 1836 (rtype == R_SPARC_TLS_GD_LO10)) &&
1803 1837 ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSGD,
1804 1838 ofl, rsp)) == NULL)) {
1805 1839
1806 1840 if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1807 1841 FLG_REL_DTLS, rtype, M_R_DTPMOD, M_R_DTPOFF) == S_ERROR)
1808 1842 return (S_ERROR);
1809 1843 }
1810 1844
1811 1845 /*
1812 1846 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually
1813 1847 * cause a call to __tls_get_addr(). Convert this relocation to that
1814 1848 * symbol now, and prepare for the PLT magic.
1815 1849 */
1816 1850 if ((rtype == R_SPARC_TLS_GD_CALL) || (rtype == R_SPARC_TLS_LDM_CALL)) {
1817 1851 Sym_desc *tlsgetsym;
1818 1852
1819 1853 if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_U),
1820 1854 ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR)
1821 1855 return (S_ERROR);
1822 1856
1823 1857 rsp->rel_sym = tlsgetsym;
1824 1858 rsp->rel_rtype = R_SPARC_WPLT30;
1825 1859
1826 1860 if (ld_reloc_plt(rsp, ofl) == S_ERROR)
1827 1861 return (S_ERROR);
1828 1862
1829 1863 rsp->rel_sym = sdp;
1830 1864 rsp->rel_rtype = rtype;
1831 1865 return (1);
1832 1866 }
1833 1867
1834 1868 if (IS_TLS_LD(rtype))
1835 1869 return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
1836 1870
1837 1871 return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
1838 1872 }
1839 1873
1840 1874 /*
1841 1875 * ld_allocate_got: if a GOT is to be made, after the section is built this
1842 1876 * function is called to allocate all the GOT slots. The allocation is
1843 1877 * deferred until after all GOTs have been counted and sorted according
1844 1878 * to their size, for only then will we know how to allocate them on
1845 1879 * a processor like SPARC which has different models for addressing the
1846 1880 * GOT. SPARC has two: small and large, small uses a signed 13-bit offset
1847 1881 * into the GOT, whereas large uses an unsigned 32-bit offset.
1848 1882 */
1849 1883 static Sword small_index; /* starting index for small GOT entries */
1850 1884 static Sword mixed_index; /* starting index for mixed GOT entries */
1851 1885 static Sword large_index; /* starting index for large GOT entries */
1852 1886
1853 1887 static uintptr_t
1854 1888 ld_assign_got(Ofl_desc *ofl, Sym_desc *sdp)
1855 1889 {
1856 1890 Aliste idx;
1857 1891 Gotndx *gnp;
1858 1892
1859 1893 for (ALIST_TRAVERSE(sdp->sd_GOTndxs, idx, gnp)) {
1860 1894 uint_t gotents;
1861 1895 Gotref gref = gnp->gn_gotref;
1862 1896
1863 1897 if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
1864 1898 gotents = 2;
1865 1899 else
1866 1900 gotents = 1;
1867 1901
1868 1902 switch (gnp->gn_gotndx) {
1869 1903 case M_GOT_SMALL:
1870 1904 gnp->gn_gotndx = small_index;
1871 1905 small_index += gotents;
1872 1906 if (small_index == 0)
1873 1907 small_index = M_GOT_XNumber;
1874 1908 break;
1875 1909 case M_GOT_MIXED:
1876 1910 gnp->gn_gotndx = mixed_index;
1877 1911 mixed_index += gotents;
1878 1912 break;
1879 1913 case M_GOT_LARGE:
1880 1914 gnp->gn_gotndx = large_index;
1881 1915 large_index += gotents;
1882 1916 break;
1883 1917 default:
1884 1918 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_ASSIGNGOT),
1885 1919 EC_XWORD(gnp->gn_gotndx), demangle(sdp->sd_name));
1886 1920 return (S_ERROR);
1887 1921 }
1888 1922 }
1889 1923 return (1);
1890 1924 }
1891 1925
1892 1926 static uintptr_t
1893 1927 ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl,
1894 1928 Rel_desc *rsp, Sym_desc *sdp)
1895 1929 {
1896 1930 Xword raddend;
1897 1931 Gotndx gn, *gnp;
1898 1932 Aliste idx;
1899 1933 uint_t gotents;
1900 1934
1901 1935 /* Some TLS requires two relocations with two GOT entries */
1902 1936 if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
1903 1937 gotents = 2;
1904 1938 else
1905 1939 gotents = 1;
1906 1940
1907 1941 raddend = rsp->rel_raddend;
1908 1942 if (pgnp && (pgnp->gn_addend == raddend) && (pgnp->gn_gotref == gref)) {
1909 1943
1910 1944 /*
1911 1945 * If an entry for this addend already exists, determine if it
1912 1946 * has mixed mode GOT access (both PIC and pic).
1913 1947 *
1914 1948 * In order to be accessible by both large and small pic,
1915 1949 * a mixed mode GOT must be located in the positive index
1916 1950 * range above _GLOBAL_OFFSET_TABLE_, and in the range
1917 1951 * reachable small pic. This is necessary because the large
1918 1952 * PIC mode cannot use a negative offset. This implies that
1919 1953 * there can be no more than (M_GOT_MAXSMALL/2 - M_GOT_XNumber)
1920 1954 * such entries.
1921 1955 */
1922 1956 switch (pgnp->gn_gotndx) {
1923 1957 case M_GOT_SMALL:
1924 1958 /*
1925 1959 * This one was previously identified as a small
1926 1960 * GOT. If this access is large, then convert
1927 1961 * it to mixed.
1928 1962 */
1929 1963 if (rsp->rel_rtype != R_SPARC_GOT13) {
1930 1964 pgnp->gn_gotndx = M_GOT_MIXED;
1931 1965 mixgotcnt += gotents;
1932 1966 }
1933 1967 break;
1934 1968
1935 1969 case M_GOT_LARGE:
1936 1970 /*
1937 1971 * This one was previously identified as a large
1938 1972 * GOT. If this access is small, convert it to mixed.
1939 1973 */
1940 1974 if (rsp->rel_rtype == R_SPARC_GOT13) {
1941 1975 smlgotcnt += gotents;
1942 1976 mixgotcnt += gotents;
1943 1977 pgnp->gn_gotndx = M_GOT_MIXED;
1944 1978 sdp->sd_flags |= FLG_SY_SMGOT;
1945 1979 }
1946 1980 break;
1947 1981 }
1948 1982 return (1);
1949 1983 }
1950 1984
1951 1985 gn.gn_addend = raddend;
1952 1986 gn.gn_gotref = gref;
1953 1987
1954 1988 if (rsp->rel_rtype == R_SPARC_GOT13) {
1955 1989 gn.gn_gotndx = M_GOT_SMALL;
1956 1990 smlgotcnt += gotents;
1957 1991 sdp->sd_flags |= FLG_SY_SMGOT;
1958 1992 } else
1959 1993 gn.gn_gotndx = M_GOT_LARGE;
1960 1994
1961 1995 ofl->ofl_gotcnt += gotents;
1962 1996
1963 1997 if (gref == GOT_REF_TLSLD) {
1964 1998 if (ofl->ofl_tlsldgotndx == NULL) {
1965 1999 if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL)
1966 2000 return (S_ERROR);
1967 2001 (void) memcpy(gnp, &gn, sizeof (Gotndx));
1968 2002 ofl->ofl_tlsldgotndx = gnp;
1969 2003 }
1970 2004 return (1);
1971 2005 }
1972 2006
1973 2007 idx = 0;
1974 2008 for (ALIST_TRAVERSE(*alpp, idx, gnp)) {
1975 2009 if (gnp->gn_addend > raddend)
1976 2010 break;
1977 2011 }
1978 2012
1979 2013 /*
1980 2014 * GOT indexes are maintained on an Alist, where there is typically
1981 2015 * only one index. The usage of this list is to scan the list to find
1982 2016 * an index, and then apply that index immediately to a relocation.
1983 2017 * Thus there are no external references to these GOT index structures
1984 2018 * that can be compromised by the Alist being reallocated.
1985 2019 */
1986 2020 if (alist_insert(alpp, &gn, sizeof (Gotndx),
1987 2021 AL_CNT_SDP_GOT, idx) == NULL)
1988 2022 return (S_ERROR);
1989 2023
1990 2024 return (1);
1991 2025 }
1992 2026
1993 2027 static void
1994 2028 ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
1995 2029 {
1996 2030 sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
1997 2031 }
1998 2032
1999 2033
2000 2034 static uintptr_t
2001 2035 ld_allocate_got(Ofl_desc * ofl)
2002 2036 {
2003 2037 const Sword first_large_ndx = M_GOT_MAXSMALL / 2;
2004 2038 Sym_desc *sdp;
2005 2039 Addr addr;
2006 2040
2007 2041 /*
2008 2042 * Sanity check -- is this going to fit at all? There are two
2009 2043 * limits to be concerned about:
2010 2044 * 1) There is a limit on the number of small pic GOT indices,
2011 2045 * given by M_GOT_MAXSMALL.
2012 2046 * 2) If there are more than (M_GOT_MAXSMALL/2 - M_GOT_XNumber)
2013 2047 * small GOT indices, there will be items at negative
2014 2048 * offsets from _GLOBAL_OFFSET_TABLE_. Items that are
2015 2049 * accessed via large (PIC) code cannot reach these
2016 2050 * negative slots, so mixed mode items must be in the
2017 2051 * non-negative range. This implies a limit of
2018 2052 * (M_GOT_MAXSMALL/2 - M_GOT_XNumber) mixed mode indices.
2019 2053 */
2020 2054 if (smlgotcnt > M_GOT_MAXSMALL) {
2021 2055 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_SMALLGOT),
2022 2056 EC_WORD(smlgotcnt), M_GOT_MAXSMALL);
2023 2057 return (S_ERROR);
2024 2058 }
2025 2059 if (mixgotcnt > (first_large_ndx - M_GOT_XNumber)) {
2026 2060 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_MIXEDGOT),
2027 2061 EC_WORD(mixgotcnt), first_large_ndx - M_GOT_XNumber);
2028 2062 return (S_ERROR);
2029 2063 }
2030 2064
2031 2065 /*
2032 2066 * Set starting offset to be either 0, or a negative index into
2033 2067 * the GOT based on the number of small symbols we've got.
2034 2068 */
2035 2069 neggotoffset = ((smlgotcnt >= first_large_ndx) ?
2036 2070 (first_large_ndx - smlgotcnt) : 0);
2037 2071
2038 2072 /*
2039 2073 * Initialize the got offsets used by assign_got() to
2040 2074 * locate GOT items:
2041 2075 * small - Starting index of items referenced only
2042 2076 * by small offsets (-Kpic).
2043 2077 * mixed - Starting index of items referenced
2044 2078 * by both large (-KPIC) and small (-Kpic).
2045 2079 * large - Indexes referenced only by large (-KPIC)
2046 2080 *
2047 2081 * Small items can have negative indexes (i.e. lie below
2048 2082 * _GLOBAL_OFFSET_TABLE_). Mixed and large items must have
2049 2083 * non-negative offsets.
2050 2084 */
2051 2085 small_index = (neggotoffset == 0) ? M_GOT_XNumber : neggotoffset;
2052 2086 large_index = neggotoffset + smlgotcnt;
2053 2087 mixed_index = large_index - mixgotcnt;
2054 2088
2055 2089 /*
2056 2090 * Assign bias to GOT symbols.
2057 2091 */
2058 2092 addr = -neggotoffset * M_GOT_ENTSIZE;
2059 2093 if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL), SYM_NOHASH,
2060 2094 NULL, ofl)) != NULL)
2061 2095 sdp->sd_sym->st_value = addr;
2062 2096 if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U), SYM_NOHASH,
2063 2097 NULL, ofl)) != NULL)
2064 2098 sdp->sd_sym->st_value = addr;
2065 2099
2066 2100 if (ofl->ofl_tlsldgotndx) {
2067 2101 ofl->ofl_tlsldgotndx->gn_gotndx = large_index;
2068 2102 large_index += 2;
2069 2103 }
2070 2104 return (1);
2071 2105 }
2072 2106
2073 2107 /*
2074 2108 * Initializes .got[0] with the _DYNAMIC symbol value.
2075 2109 */
2076 2110 static uintptr_t
2077 2111 ld_fillin_gotplt(Ofl_desc *ofl)
2078 2112 {
2079 2113 if (ofl->ofl_osgot) {
2080 2114 Sym_desc *sdp;
2081 2115
2082 2116 if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
2083 2117 SYM_NOHASH, NULL, ofl)) != NULL) {
2084 2118 uchar_t *genptr;
2085 2119
2086 2120 genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
2087 2121 (-neggotoffset * M_GOT_ENTSIZE) +
2088 2122 (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
2089 2123 /* LINTED */
2090 2124 *((Xword *)genptr) = sdp->sd_sym->st_value;
2091 2125 if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
2092 2126 /* LINTED */
2093 2127 *((Xword *)genptr) =
2094 2128 /* LINTED */
2095 2129 ld_bswap_Xword(*((Xword *)genptr));
2096 2130 }
2097 2131 }
2098 2132 return (1);
2099 2133 }
2100 2134
2101 2135
2102 2136
2103 2137 /*
2104 2138 * Template for generating "void (*)(void)" function
2105 2139 */
2106 2140 static const uchar_t nullfunc_tmpl[] = {
2107 2141 /* 0x00 */ 0x81, 0xc3, 0xe0, 0x08, /* retl */
2108 2142 /* 0x04 */ 0x01, 0x00, 0x00, 0x00 /* nop */
2109 2143 };
2110 2144
2111 2145
2112 2146
2113 2147 /*
2114 2148 * Return the ld_targ definition for this target.
2115 2149 */
2116 2150 const Target *
2117 2151 ld_targ_init_sparc(void)
2118 2152 {
2119 2153 static const Target _ld_targ = {
2120 2154 { /* Target_mach */
2121 2155 M_MACH, /* m_mach */
2122 2156 M_MACHPLUS, /* m_machplus */
2123 2157 M_FLAGSPLUS, /* m_flagsplus */
2124 2158 M_CLASS, /* m_class */
2125 2159 M_DATA, /* m_data */
2126 2160
2127 2161 M_SEGM_ALIGN, /* m_segm_align */
2128 2162 M_SEGM_ORIGIN, /* m_segm_origin */
2129 2163 M_SEGM_AORIGIN, /* m_segm_aorigin */
2130 2164 M_DATASEG_PERM, /* m_dataseg_perm */
2131 2165 M_STACK_PERM, /* m_stack_perm */
2132 2166 M_WORD_ALIGN, /* m_word_align */
2133 2167 /* m_def_interp */
2134 2168 #if defined(_ELF64)
2135 2169 MSG_ORIG(MSG_PTH_RTLD_SPARCV9),
2136 2170 #else
2137 2171 MSG_ORIG(MSG_PTH_RTLD),
2138 2172 #endif
2139 2173
2140 2174 /* Relocation type codes */
2141 2175 M_R_ARRAYADDR, /* m_r_arrayaddr */
2142 2176 M_R_COPY, /* m_r_copy */
2143 2177 M_R_GLOB_DAT, /* m_r_glob_dat */
2144 2178 M_R_JMP_SLOT, /* m_r_jmp_slot */
2145 2179 M_R_NUM, /* m_r_num */
2146 2180 M_R_NONE, /* m_r_none */
2147 2181 M_R_RELATIVE, /* m_r_relative */
2148 2182 M_R_REGISTER, /* m_r_register */
2149 2183
2150 2184 /* Relocation related constants */
2151 2185 M_REL_DT_COUNT, /* m_rel_dt_count */
2152 2186 M_REL_DT_ENT, /* m_rel_dt_ent */
2153 2187 M_REL_DT_SIZE, /* m_rel_dt_size */
2154 2188 M_REL_DT_TYPE, /* m_rel_dt_type */
2155 2189 M_REL_SHT_TYPE, /* m_rel_sht_type */
2156 2190
2157 2191 /* GOT related constants */
2158 2192 M_GOT_ENTSIZE, /* m_got_entsize */
2159 2193 M_GOT_XNumber, /* m_got_xnumber */
2160 2194
2161 2195 /* PLT related constants */
2162 2196 M_PLT_ALIGN, /* m_plt_align */
2163 2197 M_PLT_ENTSIZE, /* m_plt_entsize */
2164 2198 M_PLT_RESERVSZ, /* m_plt_reservsz */
2165 2199 M_PLT_SHF_FLAGS, /* m_plt_shf_flags */
2166 2200
2167 2201 /* Section type of .eh_frame/.eh_frame_hdr sections */
2168 2202 SHT_PROGBITS, /* m_sht_unwind */
2169 2203
2170 2204 M_DT_REGISTER, /* m_dt_register */
2171 2205 },
2172 2206 { /* Target_machid */
2173 2207 M_ID_ARRAY, /* id_array */
2174 2208 M_ID_BSS, /* id_bss */
2175 2209 M_ID_CAP, /* id_cap */
2176 2210 M_ID_CAPINFO, /* id_capinfo */
2177 2211 M_ID_CAPCHAIN, /* id_capchain */
2178 2212 M_ID_DATA, /* id_data */
2179 2213 M_ID_DYNAMIC, /* id_dynamic */
2180 2214 M_ID_DYNSORT, /* id_dynsort */
2181 2215 M_ID_DYNSTR, /* id_dynstr */
2182 2216 M_ID_DYNSYM, /* id_dynsym */
2183 2217 M_ID_DYNSYM_NDX, /* id_dynsym_ndx */
2184 2218 M_ID_GOT, /* id_got */
2185 2219 M_ID_GOTDATA, /* id_gotdata */
2186 2220 M_ID_HASH, /* id_hash */
2187 2221 M_ID_INTERP, /* id_interp */
2188 2222 M_ID_UNKNOWN, /* id_lbss (unused) */
2189 2223 M_ID_LDYNSYM, /* id_ldynsym */
2190 2224 M_ID_NOTE, /* id_note */
2191 2225 M_ID_NULL, /* id_null */
2192 2226 M_ID_PLT, /* id_plt */
2193 2227 M_ID_REL, /* id_rel */
2194 2228 M_ID_STRTAB, /* id_strtab */
2195 2229 M_ID_SYMINFO, /* id_syminfo */
2196 2230 M_ID_SYMTAB, /* id_symtab */
2197 2231 M_ID_SYMTAB_NDX, /* id_symtab_ndx */
2198 2232 M_ID_TEXT, /* id_text */
2199 2233 M_ID_TLS, /* id_tls */
2200 2234 M_ID_TLSBSS, /* id_tlsbss */
2201 2235 M_ID_UNKNOWN, /* id_unknown */
2202 2236 M_ID_UNWIND, /* id_unwind */
2203 2237 M_ID_UNWINDHDR, /* id_unwindhdr */
2204 2238 M_ID_USER, /* id_user */
2205 2239 M_ID_VERSION, /* id_version */
2206 2240 },
2207 2241 { /* Target_nullfunc */
2208 2242 nullfunc_tmpl, /* nf_template */
2209 2243 sizeof (nullfunc_tmpl), /* nf_size */
2210 2244 },
2211 2245 { /* Target_fillfunc */
2212 2246 /*
2213 2247 * On sparc, special filling of executable sections
2214 2248 * is undesirable, and the default 0 fill supplied
2215 2249 * by libelf is preferred:
2216 2250 *
2217 2251 * - 0 fill is interpreted as UNIMP instructions,
2218 2252 * which cause an illegal_instruction_trap. These
2219 2253 * serve as a sentinel against poorly written
2220 2254 * code. The sparc architecture manual discusses
2221 2255 * this as providing a measure of runtime safety.
2222 2256 *
2223 2257 * - The one place where a hole should conceivably
2224 2258 * be filled with NOP instructions is in the
2225 2259 * .init/.fini sections. However, the sparc
2226 2260 * assembler sizes the sections it generates
2227 2261 * to a multiple of the section alignment, and as
2228 2262 * such, takes the filling task out of our hands.
2229 2263 * Furthermore, the sparc assembler uses 0-fill
2230 2264 * for this, forcing the authors of sparc
2231 2265 * assembler for .init/.fini sections to be aware
2232 2266 * of this case and explicitly supply NOP fill.
2233 2267 * Hence, there is no role for the link-editor.
2234 2268 */
2235 2269 NULL /* ff_execfill */
2236 2270 },
2237 2271 { /* Target_machrel */
2238 2272 reloc_table,
2239 2273
2240 2274 ld_init_rel, /* mr_init_rel */
2241 2275 ld_mach_eflags, /* mr_mach_eflags */
2242 2276 ld_mach_make_dynamic, /* mr_mach_make_dynamic */
2243 2277 ld_mach_update_odynamic, /* mr_mach_update_odynamic */
2244 2278 ld_calc_plt_addr, /* mr_calc_plt_addr */
2245 2279 ld_perform_outreloc, /* mr_perform_outreloc */
2246 2280 ld_do_activerelocs, /* mr_do_activerelocs */
2247 2281 ld_add_outrel, /* mr_add_outrel */
2248 2282 ld_reloc_register, /* mr_reloc_register */
2249 2283 ld_reloc_local, /* mr_reloc_local */
2250 2284 ld_reloc_GOTOP, /* mr_reloc_GOTOP */
2251 2285 ld_reloc_TLS, /* mr_reloc_TLS */
2252 2286 ld_assign_got, /* mr_assign_got */
2253 2287 ld_find_got_ndx, /* mr_find_got_ndx */
2254 2288 ld_calc_got_offset, /* mr_calc_got_offset */
2255 2289 ld_assign_got_ndx, /* mr_assign_got_ndx */
2256 2290 ld_assign_plt_ndx, /* mr_assign_plt_ndx */
2257 2291 ld_allocate_got, /* mr_allocate_got */
2258 2292 ld_fillin_gotplt, /* mr_fillin_gotplt */
2259 2293 },
2260 2294 { /* Target_machsym */
2261 2295 ld_reg_check_sparc, /* ms_reg_check */
2262 2296 ld_mach_sym_typecheck_sparc, /* ms_mach_sym_typecheck */
2263 2297 ld_is_regsym_sparc, /* ms_is_regsym */
2264 2298 ld_reg_find_sparc, /* ms_reg_find */
2265 2299 ld_reg_enter_sparc /* ms_reg_enter */
2266 2300 }
2267 2301 };
2268 2302
2269 2303 return (&_ld_targ);
2270 2304 }
↓ open down ↓ |
849 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX