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.amd.c
+++ new/usr/src/cmd/sgs/libld/common/machrel.amd.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) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 */
25 25
26 26 /* Get the x86 version of the relocation engine */
27 27 #define DO_RELOC_LIBLD_X86
28 28
29 29 #include <string.h>
30 30 #include <stdio.h>
31 31 #include <strings.h>
32 32 #include <sys/elf_amd64.h>
33 33 #include <debug.h>
34 34 #include <reloc.h>
35 35 #include <i386/machdep_x86.h>
36 36 #include "msg.h"
37 37 #include "_libld.h"
38 38
39 39 /*
40 40 * This module uses do_reloc_ld() to execute several synthesized relocations.
41 41 * That function expects to be passed two things that we need to construct
42 42 * here:
43 43 *
44 44 * 1) A Rel_desc descriptor for each relocation type, from which the
45 45 * rel_rtype field, and nothing else, is obtained. This is easily
46 46 * handled by constructing the necessary descriptors.
47 47 *
48 48 * 2) A function, which called with the Rel_desc descriptor, returns
49 49 * a string representing the name of the symbol associated with
50 50 * the descriptor. The usual function for this is ld_reloc_sym_name().
51 51 * However, that function will not work in this case, as these synthetic
52 52 * relocations do not have an associated symbol. We supply the
53 53 * syn_rdesc_sym_name() function to simply return the fixed name.
54 54 */
55 55 static Rel_desc rdesc_r_amd64_gotpcrel = {
56 56 NULL, NULL, NULL, 0, 0, 0, R_AMD64_GOTPCREL };
57 57 static Rel_desc rdesc_r_amd64_32 = {
58 58 NULL, NULL, NULL, 0, 0, 0, R_AMD64_32 };
59 59 static Rel_desc rdesc_r_amd64_pc32 = {
60 60 NULL, NULL, NULL, 0, 0, 0, R_AMD64_PC32 };
61 61
62 62 /*ARGSUSED*/
63 63 static const char *
64 64 syn_rdesc_sym_name(Rel_desc *rdesc)
65 65 {
66 66 return (MSG_ORIG(MSG_SYM_PLTENT));
67 67 }
68 68
69 69 /*
70 70 * Search the GOT index list for a GOT entry with a matching reference and the
71 71 * proper addend.
72 72 */
73 73 static Gotndx *
74 74 ld_find_got_ndx(Alist *alp, Gotref gref, Ofl_desc *ofl, Rel_desc *rdesc)
75 75 {
76 76 Aliste idx;
77 77 Gotndx *gnp;
78 78
79 79 assert(rdesc != 0);
80 80
81 81 if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
82 82 return (ofl->ofl_tlsldgotndx);
83 83
84 84 for (ALIST_TRAVERSE(alp, idx, gnp)) {
85 85 if ((rdesc->rel_raddend == gnp->gn_addend) &&
86 86 (gnp->gn_gotref == gref)) {
87 87 return (gnp);
88 88 }
89 89 }
90 90 return (NULL);
91 91 }
92 92
93 93 static Xword
94 94 ld_calc_got_offset(Rel_desc *rdesc, Ofl_desc *ofl)
95 95 {
96 96 Os_desc *osp = ofl->ofl_osgot;
97 97 Sym_desc *sdp = rdesc->rel_sym;
98 98 Xword gotndx;
99 99 Gotref gref;
100 100 Gotndx *gnp;
101 101
102 102 if (rdesc->rel_flags & FLG_REL_DTLS)
103 103 gref = GOT_REF_TLSGD;
104 104 else if (rdesc->rel_flags & FLG_REL_MTLS)
105 105 gref = GOT_REF_TLSLD;
106 106 else if (rdesc->rel_flags & FLG_REL_STLS)
107 107 gref = GOT_REF_TLSIE;
108 108 else
109 109 gref = GOT_REF_GENERIC;
110 110
111 111 gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, rdesc);
112 112 assert(gnp);
113 113
114 114 gotndx = (Xword)gnp->gn_gotndx;
115 115
116 116 if ((rdesc->rel_flags & FLG_REL_DTLS) &&
117 117 (rdesc->rel_rtype == R_AMD64_DTPOFF64))
118 118 gotndx++;
119 119
120 120 return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE)));
121 121 }
122 122
123 123 static Word
124 124 ld_init_rel(Rel_desc *reld, Word *typedata, void *reloc)
125 125 {
126 126 Rela *rel = (Rela *)reloc;
127 127
128 128 /* LINTED */
129 129 reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info, M_MACH);
130 130 reld->rel_roffset = rel->r_offset;
131 131 reld->rel_raddend = rel->r_addend;
132 132 *typedata = 0;
133 133
134 134 reld->rel_flags |= FLG_REL_RELA;
135 135
136 136 return ((Word)ELF_R_SYM(rel->r_info));
137 137 }
138 138
139 139 static void
140 140 ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
141 141 {
142 142 ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
143 143 }
144 144
145 145 static void
146 146 ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
147 147 {
148 148 if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
149 149 /*
150 150 * Create this entry if we are going to create a PLT table.
151 151 */
152 152 if (ofl->ofl_pltcnt)
153 153 (*cnt)++; /* DT_PLTGOT */
154 154 }
155 155 }
156 156
157 157 static void
158 158 ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
159 159 {
160 160 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) {
161 161 (*dyn)->d_tag = DT_PLTGOT;
162 162 if (ofl->ofl_osgot)
163 163 (*dyn)->d_un.d_ptr = ofl->ofl_osgot->os_shdr->sh_addr;
164 164 else
165 165 (*dyn)->d_un.d_ptr = 0;
166 166 (*dyn)++;
167 167 }
168 168 }
169 169
170 170 static Xword
171 171 ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
172 172 {
173 173 Xword value;
174 174
175 175 value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
176 176 M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE);
177 177 return (value);
178 178 }
179 179
180 180 /*
181 181 * Build a single plt entry - code is:
182 182 * JMP *name1@GOTPCREL(%rip)
183 183 * PUSHL $index
184 184 * JMP .PLT0
185 185 */
186 186 static uchar_t pltn_entry[M_PLT_ENTSIZE] = {
187 187 /* 0x00 jmpq *name1@GOTPCREL(%rip) */ 0xff, 0x25, 0x00, 0x00, 0x00, 0x00,
188 188 /* 0x06 pushq $index */ 0x68, 0x00, 0x00, 0x00, 0x00,
189 189 /* 0x0b jmpq .plt0(%rip) */ 0xe9, 0x00, 0x00, 0x00, 0x00
190 190 /* 0x10 */
191 191 };
192 192
193 193 static uintptr_t
194 194 plt_entry(Ofl_desc * ofl, Sym_desc * sdp)
195 195 {
196 196 uchar_t *plt0, *pltent, *gotent;
197 197 Sword plt_off;
198 198 Word got_off;
199 199 Xword val1;
200 200 int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
201 201
202 202 got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
203 203 plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) *
204 204 M_PLT_ENTSIZE);
205 205 plt0 = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf);
206 206 pltent = plt0 + plt_off;
207 207 gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off;
208 208
209 209 bcopy(pltn_entry, pltent, sizeof (pltn_entry));
210 210 /*
211 211 * Fill in the got entry with the address of the next instruction.
212 212 */
213 213 /* LINTED */
214 214 *(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off +
215 215 M_PLT_INSSIZE;
216 216 if (bswap)
217 217 /* LINTED */
218 218 *(Word *)gotent = ld_bswap_Word(*(Word *)gotent);
219 219
220 220 /*
221 221 * If '-z noreloc' is specified - skip the do_reloc_ld
222 222 * stage.
223 223 */
224 224 if (!OFL_DO_RELOC(ofl))
225 225 return (1);
226 226
227 227 /*
228 228 * patchup:
229 229 * jmpq *name1@gotpcrel(%rip)
230 230 *
231 231 * NOTE: 0x06 represents next instruction.
232 232 */
233 233 val1 = (ofl->ofl_osgot->os_shdr->sh_addr + got_off) -
234 234 (ofl->ofl_osplt->os_shdr->sh_addr + plt_off) - 0x06;
235 235
236 236 if (do_reloc_ld(&rdesc_r_amd64_gotpcrel, &pltent[0x02], &val1,
237 237 syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT), bswap,
238 238 ofl->ofl_lml) == 0) {
239 239 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL),
240 240 sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
241 241 return (S_ERROR);
242 242 }
243 243
244 244 /*
245 245 * patchup:
246 246 * pushq $pltndx
247 247 */
248 248 val1 = (Xword)(sdp->sd_aux->sa_PLTndx - 1);
249 249
250 250 if (do_reloc_ld(&rdesc_r_amd64_32, &pltent[0x07], &val1,
251 251 syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT), bswap,
252 252 ofl->ofl_lml) == 0) {
253 253 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL),
254 254 sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
255 255 return (S_ERROR);
256 256 }
257 257
258 258 /*
259 259 * patchup:
260 260 * jmpq .plt0(%rip)
261 261 * NOTE: 0x10 represents next instruction. The rather complex
262 262 * series of casts is necessary to sign extend an offset into
263 263 * a 64-bit value while satisfying various compiler error
264 264 * checks. Handle with care.
265 265 */
266 266 val1 = (Xword)((intptr_t)((uintptr_t)plt0 -
267 267 (uintptr_t)(&pltent[0x10])));
268 268
269 269 if (do_reloc_ld(&rdesc_r_amd64_pc32, &pltent[0x0c], &val1,
270 270 syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT), bswap,
271 271 ofl->ofl_lml) == 0) {
272 272 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL),
273 273 sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
274 274 return (S_ERROR);
275 275 }
276 276
277 277 return (1);
278 278 }
279 279
280 280 static uintptr_t
281 281 ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl, Boolean *remain_seen)
282 282 {
283 283 Os_desc * relosp, * osp = 0;
284 284 Word ndx;
285 285 Xword roffset, value;
286 286 Sxword raddend;
287 287 Rela rea;
288 288 char *relbits;
289 289 Sym_desc * sdp, * psym = (Sym_desc *)0;
290 290 int sectmoved = 0;
291 291
292 292 raddend = orsp->rel_raddend;
293 293 sdp = orsp->rel_sym;
294 294
295 295 /*
296 296 * If the section this relocation is against has been discarded
297 297 * (-zignore), then also discard (skip) the relocation itself.
298 298 */
299 299 if (orsp->rel_isdesc && ((orsp->rel_flags &
300 300 (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
301 301 (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
302 302 DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
303 303 return (1);
304 304 }
305 305
306 306 /*
307 307 * If this is a relocation against a move table, or expanded move
308 308 * table, adjust the relocation entries.
309 309 */
310 310 if (RELAUX_GET_MOVE(orsp))
311 311 ld_adj_movereloc(ofl, orsp);
312 312
313 313 /*
314 314 * If this is a relocation against a section then we need to adjust the
315 315 * raddend field to compensate for the new position of the input section
316 316 * within the new output section.
317 317 */
318 318 if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
319 319 if (ofl->ofl_parsyms &&
320 320 (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
321 321 /* LINTED */
322 322 (psym = ld_am_I_partial(orsp, orsp->rel_raddend))) {
323 323 DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
324 324 sectmoved = 1;
325 325 if (ofl->ofl_flags & FLG_OF_RELOBJ)
326 326 raddend = psym->sd_sym->st_value;
327 327 else
328 328 raddend = psym->sd_sym->st_value -
329 329 psym->sd_isc->is_osdesc->os_shdr->sh_addr;
330 330 /* LINTED */
331 331 raddend += (Off)_elf_getxoff(psym->sd_isc->is_indata);
332 332 if (psym->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
333 333 raddend +=
334 334 psym->sd_isc->is_osdesc->os_shdr->sh_addr;
335 335 } else {
336 336 /* LINTED */
337 337 raddend += (Off)_elf_getxoff(sdp->sd_isc->is_indata);
338 338 if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
339 339 raddend +=
340 340 sdp->sd_isc->is_osdesc->os_shdr->sh_addr;
341 341 }
342 342 }
343 343
344 344 value = sdp->sd_sym->st_value;
345 345
346 346 if (orsp->rel_flags & FLG_REL_GOT) {
347 347 /*
348 348 * Note: for GOT relative relocations on amd64
349 349 * we discard the addend. It was relevant
350 350 * to the reference - not to the data item
351 351 * being referenced (ie: that -4 thing).
352 352 */
353 353 raddend = 0;
354 354 osp = ofl->ofl_osgot;
355 355 roffset = ld_calc_got_offset(orsp, ofl);
356 356
357 357 } else if (orsp->rel_flags & FLG_REL_PLT) {
358 358 /*
359 359 * Note that relocations for PLT's actually
360 360 * cause a relocation againt the GOT.
361 361 */
362 362 osp = ofl->ofl_osplt;
363 363 roffset = (ofl->ofl_osgot->os_shdr->sh_addr) +
364 364 sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
365 365 raddend = 0;
366 366 if (plt_entry(ofl, sdp) == S_ERROR)
367 367 return (S_ERROR);
368 368
369 369 } else if (orsp->rel_flags & FLG_REL_BSS) {
370 370 /*
371 371 * This must be a R_AMD64_COPY. For these set the roffset to
372 372 * point to the new symbols location.
373 373 */
374 374 osp = ofl->ofl_isbss->is_osdesc;
375 375 roffset = value;
376 376
377 377 /*
378 378 * The raddend doesn't mean anything in a R_SPARC_COPY
379 379 * relocation. Null it out because it can confuse people.
380 380 */
381 381 raddend = 0;
382 382 } else {
383 383 osp = RELAUX_GET_OSDESC(orsp);
384 384
385 385 /*
386 386 * Calculate virtual offset of reference point; equals offset
387 387 * into section + vaddr of section for loadable sections, or
388 388 * offset plus section displacement for nonloadable sections.
389 389 */
390 390 roffset = orsp->rel_roffset +
391 391 (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
392 392 if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
393 393 roffset += orsp->rel_isdesc->is_osdesc->
394 394 os_shdr->sh_addr;
395 395 }
396 396
397 397 if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
398 398 relosp = ofl->ofl_osrel;
399 399
400 400 /*
401 401 * Assign the symbols index for the output relocation. If the
402 402 * relocation refers to a SECTION symbol then it's index is based upon
403 403 * the output sections symbols index. Otherwise the index can be
404 404 * derived from the symbols index itself.
405 405 */
406 406 if (orsp->rel_rtype == R_AMD64_RELATIVE)
407 407 ndx = STN_UNDEF;
408 408 else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
409 409 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
410 410 if (sectmoved == 0) {
411 411 /*
412 412 * Check for a null input section. This can
413 413 * occur if this relocation references a symbol
414 414 * generated by sym_add_sym().
415 415 */
416 416 if (sdp->sd_isc && sdp->sd_isc->is_osdesc)
417 417 ndx = sdp->sd_isc->is_osdesc->os_identndx;
418 418 else
419 419 ndx = sdp->sd_shndx;
420 420 } else
421 421 ndx = ofl->ofl_parexpnndx;
422 422 } else
423 423 ndx = sdp->sd_symndx;
424 424
425 425 /*
426 426 * Add the symbols 'value' to the addend field.
427 427 */
↓ open down ↓ |
427 lines elided |
↑ open up ↑ |
428 428 if (orsp->rel_flags & FLG_REL_ADVAL)
429 429 raddend += value;
430 430
431 431 /*
432 432 * The addend field for R_AMD64_DTPMOD64 means nothing. The addend
433 433 * is propagated in the corresponding R_AMD64_DTPOFF64 relocation.
434 434 */
435 435 if (orsp->rel_rtype == R_AMD64_DTPMOD64)
436 436 raddend = 0;
437 437
438 - relbits = (char *)relosp->os_outdata->d_buf;
438 + if ((orsp->rel_rtype != M_R_NONE) &&
439 + (orsp->rel_rtype != M_R_RELATIVE)) {
440 + if (ndx == 0) {
441 + Conv_inv_buf_t inv_buf;
442 + Is_desc *isp = orsp->rel_isdesc;
443 +
444 + ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_NOSYMBOL),
445 + conv_reloc_type(ofl->ofl_nehdr->e_machine,
446 + orsp->rel_rtype, 0, &inv_buf),
447 + isp->is_file->ifl_name, EC_WORD(isp->is_scnndx),
448 + isp->is_name, EC_XWORD(roffset));
449 + return (S_ERROR);
450 + }
451 + }
439 452
440 453 rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype);
441 454 rea.r_offset = roffset;
442 455 rea.r_addend = raddend;
443 456 DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, relosp->os_name,
444 457 ld_reloc_sym_name(orsp)));
445 458
446 459 /*
447 460 * Assert we haven't walked off the end of our relocation table.
448 461 */
449 462 assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
450 463
464 + relbits = (char *)relosp->os_outdata->d_buf;
465 +
451 466 (void) memcpy((relbits + relosp->os_szoutrels),
452 467 (char *)&rea, sizeof (Rela));
453 468 relosp->os_szoutrels += (Xword)sizeof (Rela);
454 469
455 470 /*
456 471 * Determine if this relocation is against a non-writable, allocatable
457 472 * section. If so we may need to provide a text relocation diagnostic.
458 473 * Note that relocations against the .plt (R_AMD64_JUMP_SLOT) actually
459 474 * result in modifications to the .got.
460 475 */
461 476 if (orsp->rel_rtype == R_AMD64_JUMP_SLOT)
462 477 osp = ofl->ofl_osgot;
463 478
464 479 ld_reloc_remain_entry(orsp, osp, ofl, remain_seen);
465 480 return (1);
466 481 }
467 482
468 483 /*
469 484 * amd64 Instructions for TLS processing
470 485 */
471 486 static uchar_t tlsinstr_gd_ie[] = {
472 487 /*
473 488 * 0x00 movq %fs:0, %rax
474 489 */
475 490 0x64, 0x48, 0x8b, 0x04, 0x25,
476 491 0x00, 0x00, 0x00, 0x00,
477 492 /*
478 493 * 0x09 addq x@gottpoff(%rip), %rax
479 494 */
480 495 0x48, 0x03, 0x05, 0x00, 0x00,
481 496 0x00, 0x00
482 497 };
483 498
484 499 static uchar_t tlsinstr_gd_le[] = {
485 500 /*
486 501 * 0x00 movq %fs:0, %rax
487 502 */
488 503 0x64, 0x48, 0x8b, 0x04, 0x25,
489 504 0x00, 0x00, 0x00, 0x00,
490 505 /*
491 506 * 0x09 leaq x@gottpoff(%rip), %rax
492 507 */
493 508 0x48, 0x8d, 0x80, 0x00, 0x00,
494 509 0x00, 0x00
495 510 };
496 511
497 512 static uchar_t tlsinstr_ld_le[] = {
498 513 /*
499 514 * .byte 0x66
500 515 */
501 516 0x66,
502 517 /*
503 518 * .byte 0x66
504 519 */
505 520 0x66,
506 521 /*
507 522 * .byte 0x66
508 523 */
509 524 0x66,
510 525 /*
511 526 * movq %fs:0, %rax
512 527 */
513 528 0x64, 0x48, 0x8b, 0x04, 0x25,
514 529 0x00, 0x00, 0x00, 0x00
515 530 };
516 531
517 532 #define REX_B 0x1
518 533 #define REX_X 0x2
519 534 #define REX_R 0x4
520 535 #define REX_W 0x8
521 536 #define REX_PREFIX 0x40
522 537
523 538 #define REX_RW (REX_PREFIX | REX_R | REX_W)
524 539 #define REX_BW (REX_PREFIX | REX_B | REX_W)
525 540 #define REX_BRW (REX_PREFIX | REX_B | REX_R | REX_W)
526 541
527 542 #define REG_ESP 0x4
528 543
529 544 #define INSN_ADDMR 0x03 /* addq mem,reg */
530 545 #define INSN_ADDIR 0x81 /* addq imm,reg */
531 546 #define INSN_MOVMR 0x8b /* movq mem,reg */
532 547 #define INSN_MOVIR 0xc7 /* movq imm,reg */
533 548 #define INSN_LEA 0x8d /* leaq mem,reg */
534 549
535 550 static Fixupret
536 551 tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
537 552 {
538 553 Sym_desc *sdp = arsp->rel_sym;
539 554 Word rtype = arsp->rel_rtype;
540 555 uchar_t *offset;
541 556
542 557 offset = (uchar_t *)((uintptr_t)arsp->rel_roffset +
543 558 (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
544 559 (uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf);
545 560
546 561 /*
547 562 * Note that in certain of the original insn sequences below, the
548 563 * instructions are not necessarily adjacent
549 564 */
550 565 if (sdp->sd_ref == REF_DYN_NEED) {
551 566 /*
552 567 * IE reference model
553 568 */
554 569 switch (rtype) {
555 570 case R_AMD64_TLSGD:
556 571 /*
557 572 * GD -> IE
558 573 *
559 574 * Transition:
560 575 * 0x00 .byte 0x66
561 576 * 0x01 leaq x@tlsgd(%rip), %rdi
562 577 * 0x08 .word 0x6666
563 578 * 0x0a rex64
564 579 * 0x0b call __tls_get_addr@plt
565 580 * 0x10
566 581 * To:
567 582 * 0x00 movq %fs:0, %rax
568 583 * 0x09 addq x@gottpoff(%rip), %rax
569 584 * 0x10
570 585 */
571 586 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
572 587 R_AMD64_GOTTPOFF, arsp, ld_reloc_sym_name));
573 588 arsp->rel_rtype = R_AMD64_GOTTPOFF;
574 589 arsp->rel_roffset += 8;
575 590 arsp->rel_raddend = (Sxword)-4;
576 591
577 592 /*
578 593 * Adjust 'offset' to beginning of instruction
579 594 * sequence.
580 595 */
581 596 offset -= 4;
582 597 (void) memcpy(offset, tlsinstr_gd_ie,
583 598 sizeof (tlsinstr_gd_ie));
584 599 return (FIX_RELOC);
585 600
586 601 case R_AMD64_PLT32:
587 602 /*
588 603 * Fixup done via the TLS_GD relocation.
589 604 */
590 605 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
591 606 R_AMD64_NONE, arsp, ld_reloc_sym_name));
592 607 return (FIX_DONE);
593 608 }
594 609 }
595 610
596 611 /*
597 612 * LE reference model
598 613 */
599 614 switch (rtype) {
600 615 case R_AMD64_TLSGD:
601 616 /*
602 617 * GD -> LE
603 618 *
604 619 * Transition:
605 620 * 0x00 .byte 0x66
606 621 * 0x01 leaq x@tlsgd(%rip), %rdi
607 622 * 0x08 .word 0x6666
608 623 * 0x0a rex64
609 624 * 0x0b call __tls_get_addr@plt
610 625 * 0x10
611 626 * To:
612 627 * 0x00 movq %fs:0, %rax
613 628 * 0x09 leaq x@tpoff(%rax), %rax
614 629 * 0x10
615 630 */
616 631 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
617 632 R_AMD64_TPOFF32, arsp, ld_reloc_sym_name));
618 633 arsp->rel_rtype = R_AMD64_TPOFF32;
619 634 arsp->rel_roffset += 8;
620 635 arsp->rel_raddend = 0;
621 636
622 637 /*
623 638 * Adjust 'offset' to beginning of instruction sequence.
624 639 */
625 640 offset -= 4;
626 641 (void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le));
627 642 return (FIX_RELOC);
628 643
629 644 case R_AMD64_GOTTPOFF: {
630 645 /*
631 646 * IE -> LE
632 647 *
633 648 * Transition 1:
634 649 * movq %fs:0, %reg
635 650 * addq x@gottpoff(%rip), %reg
636 651 * To:
637 652 * movq %fs:0, %reg
638 653 * leaq x@tpoff(%reg), %reg
639 654 *
640 655 * Transition (as a special case):
641 656 * movq %fs:0, %r12/%rsp
642 657 * addq x@gottpoff(%rip), %r12/%rsp
643 658 * To:
644 659 * movq %fs:0, %r12/%rsp
645 660 * addq x@tpoff(%rax), %r12/%rsp
646 661 *
647 662 * Transition 2:
648 663 * movq x@gottpoff(%rip), %reg
649 664 * movq %fs:(%reg), %reg
650 665 * To:
651 666 * movq x@tpoff(%reg), %reg
652 667 * movq %fs:(%reg), %reg
653 668 */
654 669 Conv_inv_buf_t inv_buf;
655 670 uint8_t reg; /* Register */
656 671
657 672 offset -= 3;
658 673
659 674 reg = offset[2] >> 3; /* Encoded dest. reg. operand */
660 675
661 676 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
662 677 R_AMD64_TPOFF32, arsp, ld_reloc_sym_name));
663 678 arsp->rel_rtype = R_AMD64_TPOFF32;
664 679 arsp->rel_raddend = 0;
665 680
666 681 /*
667 682 * This is transition 2, and the special case of form 1 where
668 683 * a normal transition would index %rsp or %r12 and need a SIB
669 684 * byte in the leaq for which we lack space
670 685 */
671 686 if ((offset[1] == INSN_MOVMR) ||
672 687 ((offset[1] == INSN_ADDMR) && (reg == REG_ESP))) {
673 688 /*
674 689 * If we needed an extra bit of MOD.reg to refer to
675 690 * this register as the dest of the original movq we
676 691 * need an extra bit of MOD.rm to refer to it in the
677 692 * dest of the replacement movq or addq.
678 693 */
679 694 if (offset[0] == REX_RW)
680 695 offset[0] = REX_BW;
681 696
682 697 offset[1] = (offset[1] == INSN_MOVMR) ?
683 698 INSN_MOVIR : INSN_ADDIR;
684 699 offset[2] = 0xc0 | reg;
685 700
686 701 return (FIX_RELOC);
687 702 } else if (offset[1] == INSN_ADDMR) {
688 703 /*
689 704 * If we needed an extra bit of MOD.reg to refer to
690 705 * this register in the dest of the addq we need an
691 706 * extra bit of both MOD.reg and MOD.rm to refer to it
692 707 * in the source and dest of the leaq
693 708 */
694 709 if (offset[0] == REX_RW)
695 710 offset[0] = REX_BRW;
696 711
697 712 offset[1] = INSN_LEA;
698 713 offset[2] = 0x80 | (reg << 3) | reg;
699 714
700 715 return (FIX_RELOC);
701 716 }
702 717
703 718 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_BADTLSINS),
704 719 conv_reloc_amd64_type(arsp->rel_rtype, 0, &inv_buf),
705 720 arsp->rel_isdesc->is_file->ifl_name,
706 721 ld_reloc_sym_name(arsp),
707 722 arsp->rel_isdesc->is_name,
708 723 EC_OFF(arsp->rel_roffset));
709 724 return (FIX_ERROR);
710 725 }
711 726 case R_AMD64_TLSLD:
712 727 /*
713 728 * LD -> LE
714 729 *
715 730 * Transition
716 731 * 0x00 leaq x1@tlsgd(%rip), %rdi
717 732 * 0x07 call __tls_get_addr@plt
718 733 * 0x0c
719 734 * To:
720 735 * 0x00 .byte 0x66
721 736 * 0x01 .byte 0x66
722 737 * 0x02 .byte 0x66
723 738 * 0x03 movq %fs:0, %rax
724 739 */
725 740 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
726 741 R_AMD64_NONE, arsp, ld_reloc_sym_name));
727 742 offset -= 3;
728 743 (void) memcpy(offset, tlsinstr_ld_le, sizeof (tlsinstr_ld_le));
729 744 return (FIX_DONE);
730 745
731 746 case R_AMD64_DTPOFF32:
732 747 /*
733 748 * LD->LE
734 749 *
735 750 * Transition:
736 751 * 0x00 leaq x1@dtpoff(%rax), %rcx
737 752 * To:
738 753 * 0x00 leaq x1@tpoff(%rax), %rcx
739 754 */
740 755 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
741 756 R_AMD64_TPOFF32, arsp, ld_reloc_sym_name));
742 757 arsp->rel_rtype = R_AMD64_TPOFF32;
743 758 return (FIX_RELOC);
744 759 }
745 760
746 761 return (FIX_RELOC);
747 762 }
748 763
749 764 static uintptr_t
750 765 ld_do_activerelocs(Ofl_desc *ofl)
751 766 {
752 767 Rel_desc *arsp;
753 768 Rel_cachebuf *rcbp;
754 769 Aliste idx;
755 770 uintptr_t return_code = 1;
756 771 ofl_flag_t flags = ofl->ofl_flags;
757 772
758 773 if (aplist_nitems(ofl->ofl_actrels.rc_list) != 0)
759 774 DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
760 775
761 776 /*
762 777 * Process active relocations.
763 778 */
764 779 REL_CACHE_TRAVERSE(&ofl->ofl_actrels, idx, rcbp, arsp) {
765 780 uchar_t *addr;
766 781 Xword value;
767 782 Sym_desc *sdp;
768 783 const char *ifl_name;
769 784 Xword refaddr;
770 785 int moved = 0;
771 786 Gotref gref;
772 787 Os_desc *osp;
773 788
774 789 /*
775 790 * If the section this relocation is against has been discarded
776 791 * (-zignore), then discard (skip) the relocation itself.
777 792 */
778 793 if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
779 794 ((arsp->rel_flags & (FLG_REL_GOT | FLG_REL_BSS |
780 795 FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
781 796 DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, arsp));
782 797 continue;
783 798 }
784 799
785 800 /*
786 801 * We determine what the 'got reference' model (if required)
787 802 * is at this point. This needs to be done before tls_fixup()
788 803 * since it may 'transition' our instructions.
789 804 *
790 805 * The got table entries have already been assigned,
791 806 * and we bind to those initial entries.
792 807 */
793 808 if (arsp->rel_flags & FLG_REL_DTLS)
794 809 gref = GOT_REF_TLSGD;
795 810 else if (arsp->rel_flags & FLG_REL_MTLS)
796 811 gref = GOT_REF_TLSLD;
797 812 else if (arsp->rel_flags & FLG_REL_STLS)
798 813 gref = GOT_REF_TLSIE;
799 814 else
800 815 gref = GOT_REF_GENERIC;
801 816
802 817 /*
803 818 * Perform any required TLS fixups.
804 819 */
805 820 if (arsp->rel_flags & FLG_REL_TLSFIX) {
806 821 Fixupret ret;
807 822
808 823 if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
809 824 return (S_ERROR);
810 825 if (ret == FIX_DONE)
811 826 continue;
812 827 }
813 828
814 829 /*
815 830 * If this is a relocation against a move table, or
816 831 * expanded move table, adjust the relocation entries.
817 832 */
818 833 if (RELAUX_GET_MOVE(arsp))
819 834 ld_adj_movereloc(ofl, arsp);
820 835
821 836 sdp = arsp->rel_sym;
822 837 refaddr = arsp->rel_roffset +
823 838 (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
824 839
825 840 if ((arsp->rel_flags & FLG_REL_CLVAL) ||
826 841 (arsp->rel_flags & FLG_REL_GOTCL))
827 842 value = 0;
828 843 else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
829 844 Sym_desc *sym;
830 845
831 846 /*
832 847 * The value for a symbol pointing to a SECTION
833 848 * is based off of that sections position.
834 849 */
835 850 if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
836 851 /* LINTED */
837 852 (sym = ld_am_I_partial(arsp, arsp->rel_raddend))) {
838 853 /*
839 854 * The symbol was moved, so adjust the value
840 855 * relative to the new section.
841 856 */
842 857 value = sym->sd_sym->st_value;
843 858 moved = 1;
844 859
845 860 /*
846 861 * The original raddend covers the displacement
847 862 * from the section start to the desired
848 863 * address. The value computed above gets us
849 864 * from the section start to the start of the
850 865 * symbol range. Adjust the old raddend to
851 866 * remove the offset from section start to
852 867 * symbol start, leaving the displacement
853 868 * within the range of the symbol.
854 869 */
855 870 arsp->rel_raddend -= sym->sd_osym->st_value;
856 871 } else {
857 872 value = _elf_getxoff(sdp->sd_isc->is_indata);
858 873 if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
859 874 value += sdp->sd_isc->is_osdesc->
860 875 os_shdr->sh_addr;
861 876 }
862 877 if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
863 878 value -= ofl->ofl_tlsphdr->p_vaddr;
864 879
865 880 } else if (IS_SIZE(arsp->rel_rtype)) {
866 881 /*
867 882 * Size relocations require the symbols size.
868 883 */
869 884 value = sdp->sd_sym->st_size;
870 885
871 886 } else if ((sdp->sd_flags & FLG_SY_CAP) &&
872 887 sdp->sd_aux && sdp->sd_aux->sa_PLTndx) {
873 888 /*
874 889 * If relocation is against a capabilities symbol, we
875 890 * need to jump to an associated PLT, so that at runtime
876 891 * ld.so.1 is involved to determine the best binding
877 892 * choice. Otherwise, the value is the symbols value.
878 893 */
879 894 value = ld_calc_plt_addr(sdp, ofl);
880 895 } else
881 896 value = sdp->sd_sym->st_value;
882 897
883 898 /*
884 899 * Relocation against the GLOBAL_OFFSET_TABLE.
885 900 */
886 901 if ((arsp->rel_flags & FLG_REL_GOT) &&
887 902 !ld_reloc_set_aux_osdesc(ofl, arsp, ofl->ofl_osgot))
888 903 return (S_ERROR);
889 904 osp = RELAUX_GET_OSDESC(arsp);
890 905
891 906 /*
892 907 * If loadable and not producing a relocatable object add the
893 908 * sections virtual address to the reference address.
894 909 */
895 910 if ((arsp->rel_flags & FLG_REL_LOAD) &&
896 911 ((flags & FLG_OF_RELOBJ) == 0))
897 912 refaddr += arsp->rel_isdesc->is_osdesc->
898 913 os_shdr->sh_addr;
899 914
900 915 /*
901 916 * If this entry has a PLT assigned to it, its value is actually
902 917 * the address of the PLT (and not the address of the function).
903 918 */
904 919 if (IS_PLT(arsp->rel_rtype)) {
905 920 if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
906 921 value = ld_calc_plt_addr(sdp, ofl);
907 922 }
908 923
909 924 /*
910 925 * Add relocations addend to value. Add extra
911 926 * relocation addend if needed.
912 927 *
913 928 * Note: For GOT relative relocations on amd64 we discard the
914 929 * addend. It was relevant to the reference - not to the
915 930 * data item being referenced (ie: that -4 thing).
916 931 */
917 932 if ((arsp->rel_flags & FLG_REL_GOT) == 0)
918 933 value += arsp->rel_raddend;
919 934
920 935 /*
921 936 * Determine whether the value needs further adjustment. Filter
922 937 * through the attributes of the relocation to determine what
923 938 * adjustment is required. Note, many of the following cases
924 939 * are only applicable when a .got is present. As a .got is
925 940 * not generated when a relocatable object is being built,
926 941 * any adjustments that require a .got need to be skipped.
927 942 */
928 943 if ((arsp->rel_flags & FLG_REL_GOT) &&
929 944 ((flags & FLG_OF_RELOBJ) == 0)) {
930 945 Xword R1addr;
931 946 uintptr_t R2addr;
932 947 Word gotndx;
933 948 Gotndx *gnp;
934 949
935 950 /*
936 951 * Perform relocation against GOT table. Since this
937 952 * doesn't fit exactly into a relocation we place the
938 953 * appropriate byte in the GOT directly
939 954 *
940 955 * Calculate offset into GOT at which to apply
941 956 * the relocation.
942 957 */
943 958 gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
944 959 assert(gnp);
945 960
946 961 if (arsp->rel_rtype == R_AMD64_DTPOFF64)
947 962 gotndx = gnp->gn_gotndx + 1;
948 963 else
949 964 gotndx = gnp->gn_gotndx;
950 965
951 966 R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
952 967
953 968 /*
954 969 * Add the GOTs data's offset.
955 970 */
956 971 R2addr = R1addr + (uintptr_t)osp->os_outdata->d_buf;
957 972
958 973 DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
959 974 M_MACH, SHT_RELA, arsp, R1addr, value,
960 975 ld_reloc_sym_name));
961 976
962 977 /*
963 978 * And do it.
964 979 */
965 980 if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
966 981 *(Xword *)R2addr = ld_bswap_Xword(value);
967 982 else
968 983 *(Xword *)R2addr = value;
969 984 continue;
970 985
971 986 } else if (IS_GOT_BASED(arsp->rel_rtype) &&
972 987 ((flags & FLG_OF_RELOBJ) == 0)) {
973 988 value -= ofl->ofl_osgot->os_shdr->sh_addr;
974 989
975 990 } else if (IS_GOTPCREL(arsp->rel_rtype) &&
976 991 ((flags & FLG_OF_RELOBJ) == 0)) {
977 992 Gotndx *gnp;
978 993
979 994 /*
980 995 * Calculation:
981 996 * G + GOT + A - P
982 997 */
983 998 gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
984 999 assert(gnp);
985 1000 value = (Xword)(ofl->ofl_osgot->os_shdr-> sh_addr) +
986 1001 ((Xword)gnp->gn_gotndx * M_GOT_ENTSIZE) +
987 1002 arsp->rel_raddend - refaddr;
988 1003
989 1004 } else if (IS_GOT_PC(arsp->rel_rtype) &&
990 1005 ((flags & FLG_OF_RELOBJ) == 0)) {
991 1006 value = (Xword)(ofl->ofl_osgot->os_shdr->
992 1007 sh_addr) - refaddr + arsp->rel_raddend;
993 1008
994 1009 } else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
995 1010 (((flags & FLG_OF_RELOBJ) == 0) ||
996 1011 (osp == sdp->sd_isc->is_osdesc))) {
997 1012 value -= refaddr;
998 1013
999 1014 } else if (IS_TLS_INS(arsp->rel_rtype) &&
1000 1015 IS_GOT_RELATIVE(arsp->rel_rtype) &&
1001 1016 ((flags & FLG_OF_RELOBJ) == 0)) {
1002 1017 Gotndx *gnp;
1003 1018
1004 1019 gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
1005 1020 assert(gnp);
1006 1021 value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
1007 1022
1008 1023 } else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
1009 1024 ((flags & FLG_OF_RELOBJ) == 0)) {
1010 1025 Gotndx *gnp;
1011 1026
1012 1027 gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
1013 1028 assert(gnp);
1014 1029 value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
1015 1030
1016 1031 } else if ((arsp->rel_flags & FLG_REL_STLS) &&
1017 1032 ((flags & FLG_OF_RELOBJ) == 0)) {
1018 1033 Xword tlsstatsize;
1019 1034
1020 1035 /*
1021 1036 * This is the LE TLS reference model. Static
1022 1037 * offset is hard-coded.
1023 1038 */
1024 1039 tlsstatsize = S_ROUND(ofl->ofl_tlsphdr->p_memsz,
1025 1040 M_TLSSTATALIGN);
1026 1041 value = tlsstatsize - value;
1027 1042
1028 1043 /*
1029 1044 * Since this code is fixed up, it assumes a negative
1030 1045 * offset that can be added to the thread pointer.
1031 1046 */
1032 1047 if (arsp->rel_rtype == R_AMD64_TPOFF32)
1033 1048 value = -value;
1034 1049 }
1035 1050
1036 1051 if (arsp->rel_isdesc->is_file)
1037 1052 ifl_name = arsp->rel_isdesc->is_file->ifl_name;
1038 1053 else
1039 1054 ifl_name = MSG_INTL(MSG_STR_NULL);
1040 1055
1041 1056 /*
1042 1057 * Make sure we have data to relocate. Compiler and assembler
1043 1058 * developers have been known to generate relocations against
1044 1059 * invalid sections (normally .bss), so for their benefit give
1045 1060 * them sufficient information to help analyze the problem.
1046 1061 * End users should never see this.
1047 1062 */
1048 1063 if (arsp->rel_isdesc->is_indata->d_buf == 0) {
1049 1064 Conv_inv_buf_t inv_buf;
1050 1065
1051 1066 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_EMPTYSEC),
1052 1067 conv_reloc_amd64_type(arsp->rel_rtype, 0, &inv_buf),
1053 1068 ifl_name, ld_reloc_sym_name(arsp),
1054 1069 EC_WORD(arsp->rel_isdesc->is_scnndx),
1055 1070 arsp->rel_isdesc->is_name);
1056 1071 return (S_ERROR);
1057 1072 }
1058 1073
1059 1074 /*
1060 1075 * Get the address of the data item we need to modify.
1061 1076 */
1062 1077 addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
1063 1078 (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata));
1064 1079
1065 1080 DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
1066 1081 M_MACH, SHT_RELA, arsp, EC_NATPTR(addr), value,
1067 1082 ld_reloc_sym_name));
1068 1083 addr += (uintptr_t)osp->os_outdata->d_buf;
1069 1084
1070 1085 if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1071 1086 ofl->ofl_size) || (arsp->rel_roffset >
1072 1087 osp->os_shdr->sh_size)) {
1073 1088 int class;
1074 1089 Conv_inv_buf_t inv_buf;
1075 1090
1076 1091 if (((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1077 1092 ofl->ofl_size)
1078 1093 class = ERR_FATAL;
1079 1094 else
1080 1095 class = ERR_WARNING;
1081 1096
1082 1097 ld_eprintf(ofl, class, MSG_INTL(MSG_REL_INVALOFFSET),
1083 1098 conv_reloc_amd64_type(arsp->rel_rtype, 0, &inv_buf),
1084 1099 ifl_name, EC_WORD(arsp->rel_isdesc->is_scnndx),
1085 1100 arsp->rel_isdesc->is_name, ld_reloc_sym_name(arsp),
1086 1101 EC_ADDR((uintptr_t)addr -
1087 1102 (uintptr_t)ofl->ofl_nehdr));
1088 1103
1089 1104 if (class == ERR_FATAL) {
1090 1105 return_code = S_ERROR;
1091 1106 continue;
1092 1107 }
1093 1108 }
1094 1109
1095 1110 /*
1096 1111 * The relocation is additive. Ignore the previous symbol
1097 1112 * value if this local partial symbol is expanded.
1098 1113 */
1099 1114 if (moved)
1100 1115 value -= *addr;
1101 1116
1102 1117 /*
1103 1118 * If '-z noreloc' is specified - skip the do_reloc_ld stage.
1104 1119 */
1105 1120 if (OFL_DO_RELOC(ofl)) {
1106 1121 /*
1107 1122 * If this is a PROGBITS section and the running linker
1108 1123 * has a different byte order than the target host,
1109 1124 * tell do_reloc_ld() to swap bytes.
1110 1125 */
1111 1126 if (do_reloc_ld(arsp, addr, &value, ld_reloc_sym_name,
1112 1127 ifl_name, OFL_SWAP_RELOC_DATA(ofl, arsp),
1113 1128 ofl->ofl_lml) == 0) {
1114 1129 ofl->ofl_flags |= FLG_OF_FATAL;
1115 1130 return_code = S_ERROR;
1116 1131 }
1117 1132 }
1118 1133 }
1119 1134 return (return_code);
1120 1135 }
1121 1136
1122 1137 static uintptr_t
1123 1138 ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
1124 1139 {
1125 1140 Rel_desc *orsp;
↓ open down ↓ |
665 lines elided |
↑ open up ↑ |
1126 1141 Sym_desc *sdp = rsp->rel_sym;
1127 1142
1128 1143 /*
1129 1144 * Static executables *do not* want any relocations against them.
1130 1145 * Since our engine still creates relocations against a WEAK UNDEFINED
1131 1146 * symbol in a static executable, it's best to disable them here
1132 1147 * instead of through out the relocation code.
1133 1148 */
1134 1149 if (OFL_IS_STATIC_EXEC(ofl))
1135 1150 return (1);
1151 +
1152 + /*
1153 + * If the symbol will be reduced, we can't leave outstanding
1154 + * relocations against it, as nothing will ever be able to satisfy them
1155 + * (and the symbol won't be in .dynsym
1156 + */
1157 + if ((sdp != NULL) &&
1158 + (sdp->sd_sym->st_shndx == SHN_UNDEF) &&
1159 + (rsp->rel_rtype != M_R_NONE) &&
1160 + (rsp->rel_rtype != M_R_RELATIVE)) {
1161 + if (ld_sym_reducable(ofl, sdp))
1162 + return (1);
1163 + }
1136 1164
1137 1165 /*
1138 1166 * If we are adding a output relocation against a section
1139 1167 * symbol (non-RELATIVE) then mark that section. These sections
1140 1168 * will be added to the .dynsym symbol table.
1141 1169 */
1142 1170 if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
1143 1171 ((flags & FLG_REL_SCNNDX) ||
1144 1172 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
1145 1173
1146 1174 /*
1147 1175 * If this is a COMMON symbol - no output section
1148 1176 * exists yet - (it's created as part of sym_validate()).
1149 1177 * So - we mark here that when it's created it should
1150 1178 * be tagged with the FLG_OS_OUTREL flag.
1151 1179 */
1152 1180 if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
1153 1181 (sdp->sd_sym->st_shndx == SHN_COMMON)) {
1154 1182 if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
1155 1183 ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
1156 1184 else
1157 1185 ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
1158 1186 } else {
1159 1187 Os_desc *osp;
1160 1188 Is_desc *isp = sdp->sd_isc;
1161 1189
1162 1190 if (isp && ((osp = isp->is_osdesc) != NULL) &&
1163 1191 ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
1164 1192 ofl->ofl_dynshdrcnt++;
1165 1193 osp->os_flags |= FLG_OS_OUTREL;
1166 1194 }
1167 1195 }
1168 1196 }
1169 1197
1170 1198 /* Enter it into the output relocation cache */
1171 1199 if ((orsp = ld_reloc_enter(ofl, &ofl->ofl_outrels, rsp, flags)) == NULL)
1172 1200 return (S_ERROR);
1173 1201
1174 1202 if (flags & FLG_REL_GOT)
1175 1203 ofl->ofl_relocgotsz += (Xword)sizeof (Rela);
1176 1204 else if (flags & FLG_REL_PLT)
1177 1205 ofl->ofl_relocpltsz += (Xword)sizeof (Rela);
1178 1206 else if (flags & FLG_REL_BSS)
1179 1207 ofl->ofl_relocbsssz += (Xword)sizeof (Rela);
1180 1208 else if (flags & FLG_REL_NOINFO)
1181 1209 ofl->ofl_relocrelsz += (Xword)sizeof (Rela);
1182 1210 else
1183 1211 RELAUX_GET_OSDESC(orsp)->os_szoutrels += (Xword)sizeof (Rela);
1184 1212
1185 1213 if (orsp->rel_rtype == M_R_RELATIVE)
1186 1214 ofl->ofl_relocrelcnt++;
1187 1215
1188 1216 /*
1189 1217 * We don't perform sorting on PLT relocations because
1190 1218 * they have already been assigned a PLT index and if we
1191 1219 * were to sort them we would have to re-assign the plt indexes.
1192 1220 */
1193 1221 if (!(flags & FLG_REL_PLT))
1194 1222 ofl->ofl_reloccnt++;
1195 1223
1196 1224 /*
1197 1225 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
1198 1226 */
1199 1227 if (IS_GOT_REQUIRED(orsp->rel_rtype))
1200 1228 ofl->ofl_flags |= FLG_OF_BLDGOT;
1201 1229
1202 1230 /*
1203 1231 * Identify and possibly warn of a displacement relocation.
1204 1232 */
1205 1233 if (orsp->rel_flags & FLG_REL_DISP) {
1206 1234 ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
1207 1235
1208 1236 if (ofl->ofl_flags & FLG_OF_VERBOSE)
1209 1237 ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
1210 1238 }
1211 1239 DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA,
1212 1240 M_MACH, orsp));
1213 1241 return (1);
1214 1242 }
1215 1243
1216 1244 /*
1217 1245 * process relocation for a LOCAL symbol
1218 1246 */
1219 1247 static uintptr_t
1220 1248 ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
1221 1249 {
1222 1250 ofl_flag_t flags = ofl->ofl_flags;
1223 1251 Sym_desc *sdp = rsp->rel_sym;
1224 1252 Word shndx = sdp->sd_sym->st_shndx;
1225 1253 Word ortype = rsp->rel_rtype;
1226 1254
1227 1255 /*
1228 1256 * if ((shared object) and (not pc relative relocation) and
1229 1257 * (not against ABS symbol))
1230 1258 * then
1231 1259 * build R_AMD64_RELATIVE
1232 1260 * fi
1233 1261 */
1234 1262 if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
1235 1263 !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
1236 1264 !(IS_GOT_BASED(rsp->rel_rtype)) &&
1237 1265 !(rsp->rel_isdesc != NULL &&
1238 1266 (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
1239 1267 (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
1240 1268 (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
1241 1269
1242 1270 /*
1243 1271 * R_AMD64_RELATIVE updates a 64bit address, if this
1244 1272 * relocation isn't a 64bit binding then we can not
1245 1273 * simplify it to a RELATIVE relocation.
1246 1274 */
1247 1275 if (reloc_table[ortype].re_fsize != sizeof (Addr)) {
1248 1276 return (ld_add_outrel(0, rsp, ofl));
1249 1277 }
1250 1278
1251 1279 rsp->rel_rtype = R_AMD64_RELATIVE;
1252 1280 if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR)
1253 1281 return (S_ERROR);
1254 1282 rsp->rel_rtype = ortype;
1255 1283 return (1);
1256 1284 }
1257 1285
1258 1286 /*
1259 1287 * If the relocation is against a 'non-allocatable' section
1260 1288 * and we can not resolve it now - then give a warning
1261 1289 * message.
1262 1290 *
1263 1291 * We can not resolve the symbol if either:
1264 1292 * a) it's undefined
1265 1293 * b) it's defined in a shared library and a
1266 1294 * COPY relocation hasn't moved it to the executable
1267 1295 *
1268 1296 * Note: because we process all of the relocations against the
1269 1297 * text segment before any others - we know whether
1270 1298 * or not a copy relocation will be generated before
1271 1299 * we get here (see reloc_init()->reloc_segments()).
1272 1300 */
1273 1301 if (!(rsp->rel_flags & FLG_REL_LOAD) &&
1274 1302 ((shndx == SHN_UNDEF) ||
1275 1303 ((sdp->sd_ref == REF_DYN_NEED) &&
1276 1304 ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1277 1305 Conv_inv_buf_t inv_buf;
1278 1306 Os_desc *osp = RELAUX_GET_OSDESC(rsp);
1279 1307
1280 1308 /*
1281 1309 * If the relocation is against a SHT_SUNW_ANNOTATE
1282 1310 * section - then silently ignore that the relocation
1283 1311 * can not be resolved.
1284 1312 */
1285 1313 if (osp && (osp->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
1286 1314 return (0);
1287 1315 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
1288 1316 conv_reloc_amd64_type(rsp->rel_rtype, 0, &inv_buf),
1289 1317 rsp->rel_isdesc->is_file->ifl_name,
1290 1318 ld_reloc_sym_name(rsp), osp->os_name);
1291 1319 return (1);
1292 1320 }
1293 1321
1294 1322 /*
1295 1323 * Perform relocation.
1296 1324 */
1297 1325 return (ld_add_actrel(NULL, rsp, ofl));
1298 1326 }
1299 1327
1300 1328
1301 1329 static uintptr_t
1302 1330 ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
1303 1331 {
1304 1332 Word rtype = rsp->rel_rtype;
1305 1333 Sym_desc *sdp = rsp->rel_sym;
1306 1334 ofl_flag_t flags = ofl->ofl_flags;
1307 1335 Gotndx *gnp;
1308 1336
1309 1337 /*
1310 1338 * If we're building an executable - use either the IE or LE access
1311 1339 * model. If we're building a shared object process any IE model.
1312 1340 */
1313 1341 if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
1314 1342 /*
1315 1343 * Set the DF_STATIC_TLS flag.
1316 1344 */
1317 1345 ofl->ofl_dtflags |= DF_STATIC_TLS;
1318 1346
1319 1347 if (!local || ((flags & FLG_OF_EXEC) == 0)) {
1320 1348 /*
1321 1349 * Assign a GOT entry for static TLS references.
1322 1350 */
1323 1351 if ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
1324 1352 GOT_REF_TLSIE, ofl, rsp)) == NULL) {
1325 1353
1326 1354 if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1327 1355 gnp, GOT_REF_TLSIE, FLG_REL_STLS,
1328 1356 rtype, R_AMD64_TPOFF64, 0) == S_ERROR)
1329 1357 return (S_ERROR);
1330 1358 }
1331 1359
1332 1360 /*
1333 1361 * IE access model.
1334 1362 */
1335 1363 if (IS_TLS_IE(rtype))
1336 1364 return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1337 1365
1338 1366 /*
1339 1367 * Fixups are required for other executable models.
1340 1368 */
1341 1369 return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
1342 1370 rsp, ofl));
1343 1371 }
1344 1372
1345 1373 /*
1346 1374 * LE access model.
1347 1375 */
1348 1376 if (IS_TLS_LE(rtype))
1349 1377 return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1350 1378
1351 1379 return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
1352 1380 rsp, ofl));
1353 1381 }
1354 1382
1355 1383 /*
1356 1384 * Building a shared object.
1357 1385 *
1358 1386 * Assign a GOT entry for a dynamic TLS reference.
1359 1387 */
1360 1388 if (IS_TLS_LD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
1361 1389 GOT_REF_TLSLD, ofl, rsp)) == NULL)) {
1362 1390
1363 1391 if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
1364 1392 FLG_REL_MTLS, rtype, R_AMD64_DTPMOD64, NULL) == S_ERROR)
1365 1393 return (S_ERROR);
1366 1394
1367 1395 } else if (IS_TLS_GD(rtype) &&
1368 1396 ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSGD,
1369 1397 ofl, rsp)) == NULL)) {
1370 1398
1371 1399 if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1372 1400 FLG_REL_DTLS, rtype, R_AMD64_DTPMOD64,
1373 1401 R_AMD64_DTPOFF64) == S_ERROR)
1374 1402 return (S_ERROR);
1375 1403 }
1376 1404
1377 1405 if (IS_TLS_LD(rtype))
1378 1406 return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
1379 1407
1380 1408 return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
1381 1409 }
1382 1410
1383 1411 /* ARGSUSED5 */
1384 1412 static uintptr_t
1385 1413 ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl,
1386 1414 Rel_desc *rsp, Sym_desc *sdp)
1387 1415 {
1388 1416 Xword raddend;
1389 1417 Gotndx gn, *gnp;
1390 1418 Aliste idx;
1391 1419 uint_t gotents;
1392 1420
1393 1421 raddend = rsp->rel_raddend;
1394 1422 if (pgnp && (pgnp->gn_addend == raddend) && (pgnp->gn_gotref == gref))
1395 1423 return (1);
1396 1424
1397 1425 if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
1398 1426 gotents = 2;
1399 1427 else
1400 1428 gotents = 1;
1401 1429
1402 1430 gn.gn_addend = raddend;
1403 1431 gn.gn_gotndx = ofl->ofl_gotcnt;
1404 1432 gn.gn_gotref = gref;
1405 1433
1406 1434 ofl->ofl_gotcnt += gotents;
1407 1435
1408 1436 if (gref == GOT_REF_TLSLD) {
1409 1437 if (ofl->ofl_tlsldgotndx == NULL) {
1410 1438 if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL)
1411 1439 return (S_ERROR);
1412 1440 (void) memcpy(gnp, &gn, sizeof (Gotndx));
1413 1441 ofl->ofl_tlsldgotndx = gnp;
1414 1442 }
1415 1443 return (1);
1416 1444 }
1417 1445
1418 1446 idx = 0;
1419 1447 for (ALIST_TRAVERSE(*alpp, idx, gnp)) {
1420 1448 if (gnp->gn_addend > raddend)
1421 1449 break;
1422 1450 }
1423 1451
1424 1452 /*
1425 1453 * GOT indexes are maintained on an Alist, where there is typically
1426 1454 * only one index. The usage of this list is to scan the list to find
1427 1455 * an index, and then apply that index immediately to a relocation.
1428 1456 * Thus there are no external references to these GOT index structures
1429 1457 * that can be compromised by the Alist being reallocated.
1430 1458 */
1431 1459 if (alist_insert(alpp, &gn, sizeof (Gotndx),
1432 1460 AL_CNT_SDP_GOT, idx) == NULL)
1433 1461 return (S_ERROR);
1434 1462
1435 1463 return (1);
1436 1464 }
1437 1465
1438 1466 static void
1439 1467 ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
1440 1468 {
1441 1469 sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
1442 1470 sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
1443 1471 ofl->ofl_flags |= FLG_OF_BLDGOT;
1444 1472 }
1445 1473
1446 1474 static uchar_t plt0_template[M_PLT_ENTSIZE] = {
1447 1475 /* 0x00 PUSHQ GOT+8(%rip) */ 0xff, 0x35, 0x00, 0x00, 0x00, 0x00,
1448 1476 /* 0x06 JMP *GOT+16(%rip) */ 0xff, 0x25, 0x00, 0x00, 0x00, 0x00,
1449 1477 /* 0x0c NOP */ 0x90,
1450 1478 /* 0x0d NOP */ 0x90,
1451 1479 /* 0x0e NOP */ 0x90,
1452 1480 /* 0x0f NOP */ 0x90
1453 1481 };
1454 1482
1455 1483 /*
1456 1484 * Initializes .got[0] with the _DYNAMIC symbol value.
1457 1485 */
1458 1486 static uintptr_t
1459 1487 ld_fillin_gotplt(Ofl_desc *ofl)
1460 1488 {
1461 1489 int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
1462 1490
1463 1491 if (ofl->ofl_osgot) {
1464 1492 Sym_desc *sdp;
1465 1493
1466 1494 if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
1467 1495 SYM_NOHASH, NULL, ofl)) != NULL) {
1468 1496 uchar_t *genptr;
1469 1497
1470 1498 genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
1471 1499 (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
1472 1500 /* LINTED */
1473 1501 *(Xword *)genptr = sdp->sd_sym->st_value;
1474 1502 if (bswap)
1475 1503 /* LINTED */
1476 1504 *(Xword *)genptr =
1477 1505 /* LINTED */
1478 1506 ld_bswap_Xword(*(Xword *)genptr);
1479 1507 }
1480 1508 }
1481 1509
1482 1510 /*
1483 1511 * Fill in the reserved slot in the procedure linkage table the first
1484 1512 * entry is:
1485 1513 * 0x00 PUSHQ GOT+8(%rip) # GOT[1]
1486 1514 * 0x06 JMP *GOT+16(%rip) # GOT[2]
1487 1515 * 0x0c NOP
1488 1516 * 0x0d NOP
1489 1517 * 0x0e NOP
1490 1518 * 0x0f NOP
1491 1519 */
1492 1520 if ((ofl->ofl_flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
1493 1521 uchar_t *pltent;
1494 1522 Xword val1;
1495 1523
1496 1524 pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
1497 1525 bcopy(plt0_template, pltent, sizeof (plt0_template));
1498 1526
1499 1527 /*
1500 1528 * If '-z noreloc' is specified - skip the do_reloc_ld
1501 1529 * stage.
1502 1530 */
1503 1531 if (!OFL_DO_RELOC(ofl))
1504 1532 return (1);
1505 1533
1506 1534 /*
1507 1535 * filin:
1508 1536 * PUSHQ GOT + 8(%rip)
1509 1537 *
1510 1538 * Note: 0x06 below represents the offset to the
1511 1539 * next instruction - which is what %rip will
1512 1540 * be pointing at.
1513 1541 */
1514 1542 val1 = (ofl->ofl_osgot->os_shdr->sh_addr) +
1515 1543 (M_GOT_XLINKMAP * M_GOT_ENTSIZE) -
1516 1544 ofl->ofl_osplt->os_shdr->sh_addr - 0x06;
1517 1545
1518 1546 if (do_reloc_ld(&rdesc_r_amd64_gotpcrel, &pltent[0x02],
1519 1547 &val1, syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT),
1520 1548 bswap, ofl->ofl_lml) == 0) {
1521 1549 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_PLT_PLT0FAIL));
1522 1550 return (S_ERROR);
1523 1551 }
1524 1552
1525 1553 /*
1526 1554 * filin:
1527 1555 * JMP *GOT+16(%rip)
1528 1556 */
1529 1557 val1 = (ofl->ofl_osgot->os_shdr->sh_addr) +
1530 1558 (M_GOT_XRTLD * M_GOT_ENTSIZE) -
1531 1559 ofl->ofl_osplt->os_shdr->sh_addr - 0x0c;
1532 1560
1533 1561 if (do_reloc_ld(&rdesc_r_amd64_gotpcrel, &pltent[0x08],
1534 1562 &val1, syn_rdesc_sym_name, MSG_ORIG(MSG_SPECFIL_PLTENT),
1535 1563 bswap, ofl->ofl_lml) == 0) {
1536 1564 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_PLT_PLT0FAIL));
1537 1565 return (S_ERROR);
1538 1566 }
1539 1567 }
1540 1568
1541 1569 return (1);
1542 1570 }
1543 1571
1544 1572
1545 1573
1546 1574 /*
1547 1575 * Template for generating "void (*)(void)" function
1548 1576 */
1549 1577 static const uchar_t nullfunc_tmpl[] = { /* amd64 */
1550 1578 /* 0x00 */ 0x55, /* pushq %rbp */
1551 1579 /* 0x01 */ 0x48, 0x8b, 0xec, /* movq %rsp,%rbp */
1552 1580 /* 0x04 */ 0x48, 0x8b, 0xe5, /* movq %rbp,%rsp */
1553 1581 /* 0x07 */ 0x5d, /* popq %rbp */
1554 1582 /* 0x08 */ 0xc3 /* ret */
1555 1583 };
1556 1584
1557 1585
1558 1586 /*
1559 1587 * Function used to provide fill padding in SHF_EXECINSTR sections
1560 1588 *
1561 1589 * entry:
1562 1590 *
1563 1591 * base - base address of section being filled
1564 1592 * offset - starting offset for fill within memory referenced by base
1565 1593 * cnt - # bytes to be filled
1566 1594 *
1567 1595 * exit:
1568 1596 * The fill has been completed.
1569 1597 */
1570 1598 static void
1571 1599 execfill(void *base, off_t off, size_t cnt)
1572 1600 {
1573 1601 /*
1574 1602 * 0x90 is an X86 NOP instruction in both 32 and 64-bit worlds.
1575 1603 * There are no alignment constraints.
1576 1604 */
1577 1605 (void) memset(off + (char *)base, 0x90, cnt);
1578 1606 }
1579 1607
1580 1608
1581 1609 /*
1582 1610 * Return the ld_targ definition for this target.
1583 1611 */
1584 1612 const Target *
1585 1613 ld_targ_init_x86(void)
1586 1614 {
1587 1615 static const Target _ld_targ = {
1588 1616 { /* Target_mach */
1589 1617 M_MACH, /* m_mach */
1590 1618 M_MACHPLUS, /* m_machplus */
1591 1619 M_FLAGSPLUS, /* m_flagsplus */
1592 1620 M_CLASS, /* m_class */
1593 1621 M_DATA, /* m_data */
1594 1622
1595 1623 M_SEGM_ALIGN, /* m_segm_align */
1596 1624 M_SEGM_ORIGIN, /* m_segm_origin */
1597 1625 M_SEGM_AORIGIN, /* m_segm_aorigin */
1598 1626 M_DATASEG_PERM, /* m_dataseg_perm */
1599 1627 M_STACK_PERM, /* m_stack_perm */
1600 1628 M_WORD_ALIGN, /* m_word_align */
1601 1629 MSG_ORIG(MSG_PTH_RTLD_AMD64), /* m_def_interp */
1602 1630
1603 1631 /* Relocation type codes */
1604 1632 M_R_ARRAYADDR, /* m_r_arrayaddr */
1605 1633 M_R_COPY, /* m_r_copy */
1606 1634 M_R_GLOB_DAT, /* m_r_glob_dat */
1607 1635 M_R_JMP_SLOT, /* m_r_jmp_slot */
1608 1636 M_R_NUM, /* m_r_num */
1609 1637 M_R_NONE, /* m_r_none */
1610 1638 M_R_RELATIVE, /* m_r_relative */
1611 1639 M_R_REGISTER, /* m_r_register */
1612 1640
1613 1641 /* Relocation related constants */
1614 1642 M_REL_DT_COUNT, /* m_rel_dt_count */
1615 1643 M_REL_DT_ENT, /* m_rel_dt_ent */
1616 1644 M_REL_DT_SIZE, /* m_rel_dt_size */
1617 1645 M_REL_DT_TYPE, /* m_rel_dt_type */
1618 1646 M_REL_SHT_TYPE, /* m_rel_sht_type */
1619 1647
1620 1648 /* GOT related constants */
1621 1649 M_GOT_ENTSIZE, /* m_got_entsize */
1622 1650 M_GOT_XNumber, /* m_got_xnumber */
1623 1651
1624 1652 /* PLT related constants */
1625 1653 M_PLT_ALIGN, /* m_plt_align */
1626 1654 M_PLT_ENTSIZE, /* m_plt_entsize */
1627 1655 M_PLT_RESERVSZ, /* m_plt_reservsz */
1628 1656 M_PLT_SHF_FLAGS, /* m_plt_shf_flags */
1629 1657
1630 1658 /* Section type of .eh_frame/.eh_frame_hdr sections */
1631 1659 SHT_AMD64_UNWIND, /* m_sht_unwind */
1632 1660
1633 1661 M_DT_REGISTER, /* m_dt_register */
1634 1662 },
1635 1663 { /* Target_machid */
1636 1664 M_ID_ARRAY, /* id_array */
1637 1665 M_ID_BSS, /* id_bss */
1638 1666 M_ID_CAP, /* id_cap */
1639 1667 M_ID_CAPINFO, /* id_capinfo */
1640 1668 M_ID_CAPCHAIN, /* id_capchain */
1641 1669 M_ID_DATA, /* id_data */
1642 1670 M_ID_DYNAMIC, /* id_dynamic */
1643 1671 M_ID_DYNSORT, /* id_dynsort */
1644 1672 M_ID_DYNSTR, /* id_dynstr */
1645 1673 M_ID_DYNSYM, /* id_dynsym */
1646 1674 M_ID_DYNSYM_NDX, /* id_dynsym_ndx */
1647 1675 M_ID_GOT, /* id_got */
1648 1676 M_ID_UNKNOWN, /* id_gotdata (unused) */
1649 1677 M_ID_HASH, /* id_hash */
1650 1678 M_ID_INTERP, /* id_interp */
1651 1679 M_ID_LBSS, /* id_lbss */
1652 1680 M_ID_LDYNSYM, /* id_ldynsym */
1653 1681 M_ID_NOTE, /* id_note */
1654 1682 M_ID_NULL, /* id_null */
1655 1683 M_ID_PLT, /* id_plt */
1656 1684 M_ID_REL, /* id_rel */
1657 1685 M_ID_STRTAB, /* id_strtab */
1658 1686 M_ID_SYMINFO, /* id_syminfo */
1659 1687 M_ID_SYMTAB, /* id_symtab */
1660 1688 M_ID_SYMTAB_NDX, /* id_symtab_ndx */
1661 1689 M_ID_TEXT, /* id_text */
1662 1690 M_ID_TLS, /* id_tls */
1663 1691 M_ID_TLSBSS, /* id_tlsbss */
1664 1692 M_ID_UNKNOWN, /* id_unknown */
1665 1693 M_ID_UNWIND, /* id_unwind */
1666 1694 M_ID_UNWINDHDR, /* id_unwindhdr */
1667 1695 M_ID_USER, /* id_user */
1668 1696 M_ID_VERSION, /* id_version */
1669 1697 },
1670 1698 { /* Target_nullfunc */
1671 1699 nullfunc_tmpl, /* nf_template */
1672 1700 sizeof (nullfunc_tmpl), /* nf_size */
1673 1701 },
1674 1702 { /* Target_fillfunc */
1675 1703 execfill /* ff_execfill */
1676 1704 },
1677 1705 { /* Target_machrel */
1678 1706 reloc_table,
1679 1707
1680 1708 ld_init_rel, /* mr_init_rel */
1681 1709 ld_mach_eflags, /* mr_mach_eflags */
1682 1710 ld_mach_make_dynamic, /* mr_mach_make_dynamic */
1683 1711 ld_mach_update_odynamic, /* mr_mach_update_odynamic */
1684 1712 ld_calc_plt_addr, /* mr_calc_plt_addr */
1685 1713 ld_perform_outreloc, /* mr_perform_outreloc */
1686 1714 ld_do_activerelocs, /* mr_do_activerelocs */
1687 1715 ld_add_outrel, /* mr_add_outrel */
1688 1716 NULL, /* mr_reloc_register */
1689 1717 ld_reloc_local, /* mr_reloc_local */
1690 1718 NULL, /* mr_reloc_GOTOP */
1691 1719 ld_reloc_TLS, /* mr_reloc_TLS */
1692 1720 NULL, /* mr_assign_got */
1693 1721 ld_find_got_ndx, /* mr_find_got_ndx */
1694 1722 ld_calc_got_offset, /* mr_calc_got_offset */
1695 1723 ld_assign_got_ndx, /* mr_assign_got_ndx */
1696 1724 ld_assign_plt_ndx, /* mr_assign_plt_ndx */
1697 1725 NULL, /* mr_allocate_got */
1698 1726 ld_fillin_gotplt, /* mr_fillin_gotplt */
1699 1727 },
1700 1728 { /* Target_machsym */
1701 1729 NULL, /* ms_reg_check */
1702 1730 NULL, /* ms_mach_sym_typecheck */
1703 1731 NULL, /* ms_is_regsym */
1704 1732 NULL, /* ms_reg_find */
1705 1733 NULL /* ms_reg_enter */
1706 1734 }
1707 1735 };
1708 1736
1709 1737 return (&_ld_targ);
1710 1738 }
↓ open down ↓ |
565 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX