1 /*-
2  * Copyright (c) 2012,2013 Kai Wang
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include "ld.h"
28 #include "ld_arch.h"
29 #include "ld_dynamic.h"
30 #include "ld_input.h"
31 #include "ld_layout.h"
32 #include "ld_output.h"
33 #include "ld_reloc.h"
34 #include "ld_symbols.h"
35 #include "ld_utils.h"
36 #include "amd64.h"
37 
38 ELFTC_VCSID("$Id: amd64.c 3402 2016-02-13 15:35:06Z jkoshy $");
39 
40 static void _create_plt_reloc(struct ld *ld, struct ld_symbol *lsb,
41     uint64_t offset);
42 static void _create_got_reloc(struct ld *ld, struct ld_symbol *lsb,
43     uint64_t type, uint64_t offset);
44 static void _create_copy_reloc(struct ld *ld, struct ld_symbol *lsb);
45 static void _create_dynamic_reloc(struct ld *ld, struct ld_input_section *is,
46     struct ld_symbol *lsb, uint64_t type, uint64_t offset, int64_t addend);
47 static void _scan_reloc(struct ld *ld, struct ld_input_section *is,
48     struct ld_reloc_entry *lre);
49 static struct ld_input_section *_find_and_create_got_section(struct ld *ld,
50     int create);
51 static struct ld_input_section *_find_and_create_gotplt_section(struct ld *ld,
52     int create);
53 static struct ld_input_section *_find_and_create_plt_section(struct ld *ld,
54     int create);
55 static void _finalize_got_and_plt(struct ld *ld);
56 static uint64_t _get_max_page_size(struct ld *ld);
57 static uint64_t _get_common_page_size(struct ld *ld);
58 static void _adjust_reloc(struct ld *ld, struct ld_input_section *is,
59     struct ld_reloc_entry *lre, struct ld_symbol *lsb, uint8_t *buf);
60 static void _process_reloc(struct ld *ld, struct ld_input_section *is,
61     struct ld_reloc_entry *lre, struct ld_symbol *lsb, uint8_t *buf);
62 static const char *_reloc2str(uint64_t r);
63 static void _reserve_got_entry(struct ld *ld, struct ld_symbol *lsb, int num);
64 static void _reserve_gotplt_entry(struct ld *ld, struct ld_symbol *lsb);
65 static void _reserve_plt_entry(struct ld *ld, struct ld_symbol *lsb);
66 static int _is_absolute_reloc(uint64_t r);
67 static void _warn_pic(struct ld *ld, struct ld_reloc_entry *lre);
68 static void _create_tls_gd_reloc(struct ld *ld, struct ld_symbol *lsb);
69 static void _create_tls_ld_reloc(struct ld *ld, struct ld_symbol *lsb);
70 static void _create_tls_ie_reloc(struct ld *ld, struct ld_symbol *lsb);
71 static enum ld_tls_relax _tls_check_relax(struct ld *ld,
72     struct ld_reloc_entry *lre);
73 static uint64_t _got_offset(struct ld *ld, struct ld_symbol *lsb);
74 static int _tls_verify_gd(uint8_t *buf, uint64_t off);
75 static int _tls_verify_ld(uint8_t *buf, uint64_t off);
76 static void _tls_relax_gd_to_ie(struct ld *ld, struct ld_state *ls,
77     struct ld_output *lo,struct ld_reloc_entry *lre, uint64_t p, uint64_t g,
78     uint8_t *buf);
79 static void _tls_relax_gd_to_le(struct ld *ld, struct ld_state *ls,
80     struct ld_output *lo, struct ld_reloc_entry *lre, struct ld_symbol *lsb,
81     uint8_t *buf);
82 static void _tls_relax_ld_to_le(struct ld *ld, struct ld_state *ls,
83     struct ld_reloc_entry *lre, uint8_t *buf);
84 static void _tls_relax_ie_to_le(struct ld *ld, struct ld_output *lo,
85     struct ld_reloc_entry *lre, struct ld_symbol *lsb, uint8_t *buf);
86 static int32_t _tls_dtpoff(struct ld_output *lo, struct ld_symbol *lsb);
87 static int32_t _tls_tpoff(struct ld_output *lo, struct ld_symbol *lsb);
88 
89 static uint64_t
_get_max_page_size(struct ld * ld)90 _get_max_page_size(struct ld *ld)
91 {
92 
93 	(void) ld;
94 	return (0x200000);
95 }
96 
97 static uint64_t
_get_common_page_size(struct ld * ld)98 _get_common_page_size(struct ld *ld)
99 {
100 
101 	(void) ld;
102 	return (0x1000);
103 }
104 
105 static const char *
_reloc2str(uint64_t r)106 _reloc2str(uint64_t r)
107 {
108 	static char s[32];
109 
110 	switch (r) {
111 		case 0: return "R_X86_64_NONE";
112 		case 1: return "R_X86_64_64";
113 		case 2: return "R_X86_64_PC32";
114 		case 3: return "R_X86_64_GOT32";
115 		case 4: return "R_X86_64_PLT32";
116 		case 5: return "R_X86_64_COPY";
117 		case 6: return "R_X86_64_GLOB_DAT";
118 		case 7: return "R_X86_64_JUMP_SLOT";
119 		case 8: return "R_X86_64_RELATIVE";
120 		case 9: return "R_X86_64_GOTPCREL";
121 		case 10: return "R_X86_64_32";
122 		case 11: return "R_X86_64_32S";
123 		case 12: return "R_X86_64_16";
124 		case 13: return "R_X86_64_PC16";
125 		case 14: return "R_X86_64_8";
126 		case 15: return "R_X86_64_PC8";
127 		case 16: return "R_X86_64_DTPMOD64";
128 		case 17: return "R_X86_64_DTPOFF64";
129 		case 18: return "R_X86_64_TPOFF64";
130 		case 19: return "R_X86_64_TLSGD";
131 		case 20: return "R_X86_64_TLSLD";
132 		case 21: return "R_X86_64_DTPOFF32";
133 		case 22: return "R_X86_64_GOTTPOFF";
134 		case 23: return "R_X86_64_TPOFF32";
135 	default:
136 		snprintf(s, sizeof(s), "<unkown: %ju>", (uintmax_t) r);
137 		return (s);
138 	}
139 }
140 
141 static int
_is_absolute_reloc(uint64_t r)142 _is_absolute_reloc(uint64_t r)
143 {
144 
145 	if (r == R_X86_64_64 || r == R_X86_64_32 || r == R_X86_64_32S ||
146 	    r == R_X86_64_16 || r == R_X86_64_8)
147 		return (1);
148 
149 	return (0);
150 }
151 
152 static int
_is_relative_reloc(uint64_t r)153 _is_relative_reloc(uint64_t r)
154 {
155 
156 	if (r == R_X86_64_RELATIVE)
157 		return (1);
158 
159 	return (0);
160 }
161 
162 static void
_warn_pic(struct ld * ld,struct ld_reloc_entry * lre)163 _warn_pic(struct ld *ld, struct ld_reloc_entry *lre)
164 {
165 	struct ld_symbol *lsb;
166 
167 	lsb = lre->lre_sym;
168 
169 	if (lsb->lsb_bind != STB_LOCAL)
170 		ld_warn(ld, "relocation %s against `%s' can not be used"
171 		    " by runtime linker; recompile with -fPIC",
172 		    _reloc2str(lre->lre_type), lsb->lsb_name);
173 	else
174 		ld_warn(ld, "relocation %s can not be used by runtime linker;"
175 		    " recompile with -fPIC", _reloc2str(lre->lre_type));
176 }
177 
178 static struct ld_input_section *
_find_and_create_got_section(struct ld * ld,int create)179 _find_and_create_got_section(struct ld *ld, int create)
180 {
181 	struct ld_input_section *is;
182 
183 	/* Check if the GOT section is already created. */
184 	is = ld_input_find_internal_section(ld, ".got");
185 	if (is != NULL)
186 		return (is);
187 
188 	if (create) {
189 		is = ld_input_add_internal_section(ld, ".got");
190 		is->is_entsize = 8;
191 		is->is_align = 8;
192 		is->is_type = SHT_PROGBITS;
193 		is->is_flags = SHF_ALLOC | SHF_WRITE;
194 	}
195 
196 	return (is);
197 }
198 
199 static struct ld_input_section *
_find_and_create_gotplt_section(struct ld * ld,int create)200 _find_and_create_gotplt_section(struct ld *ld, int create)
201 {
202 	struct ld_input_section *is;
203 
204 	/* Check if the GOT (for PLT) section is already created. */
205 	is = ld_input_find_internal_section(ld, ".got.plt");
206 	if (is != NULL)
207 		return (is);
208 
209 	if (create) {
210 		is = ld_input_add_internal_section(ld, ".got.plt");
211 		is->is_entsize = 8;
212 		is->is_align = 8;
213 		is->is_type = SHT_PROGBITS;
214 		is->is_flags = SHF_ALLOC | SHF_WRITE;
215 
216 		/* Reserve space for the initial entries. */
217 		(void) ld_input_reserve_ibuf(is, 3);
218 
219 		/* Create _GLOBAL_OFFSET_TABLE_ symbol. */
220 		ld_symbols_add_internal(ld, "_GLOBAL_OFFSET_TABLE_", 0, 0,
221 		    is->is_index, STB_LOCAL, STT_OBJECT, STV_HIDDEN, is, NULL);
222 	}
223 
224 	return (is);
225 }
226 
227 static struct ld_input_section *
_find_and_create_plt_section(struct ld * ld,int create)228 _find_and_create_plt_section(struct ld *ld, int create)
229 {
230 	struct ld_input_section *is;
231 
232 	/* Check if the PLT section is already created. */
233 	is = ld_input_find_internal_section(ld, ".plt");
234 	if (is != NULL)
235 		return (is);
236 
237 	if (create) {
238 		is = ld_input_add_internal_section(ld, ".plt");
239 		is->is_entsize = 16;
240 		is->is_align = 4;
241 		is->is_type = SHT_PROGBITS;
242 		is->is_flags = SHF_ALLOC | SHF_EXECINSTR;
243 
244 		/* Reserve space for the initial entry. */
245 		(void) ld_input_reserve_ibuf(is, 1);
246 	}
247 
248 	return (is);
249 }
250 
251 static void
_reserve_got_entry(struct ld * ld,struct ld_symbol * lsb,int num)252 _reserve_got_entry(struct ld *ld, struct ld_symbol *lsb, int num)
253 {
254 	struct ld_input_section *is;
255 
256 	is = _find_and_create_got_section(ld, 1);
257 
258 	/* Check if the entry already has a GOT entry. */
259 	if (lsb->lsb_got)
260 		return;
261 
262 	/* Reserve GOT entries. */
263 	lsb->lsb_got_off = ld_input_reserve_ibuf(is, num);
264 	lsb->lsb_got = 1;
265 }
266 
267 static void
_reserve_gotplt_entry(struct ld * ld,struct ld_symbol * lsb)268 _reserve_gotplt_entry(struct ld *ld, struct ld_symbol *lsb)
269 {
270 	struct ld_input_section *is;
271 
272 	is = _find_and_create_gotplt_section(ld, 1);
273 
274 	/* Reserve a GOT entry for PLT. */
275 	(void) ld_input_reserve_ibuf(is, 1);
276 
277 	/*
278 	 * Record a R_X86_64_JUMP_SLOT entry for this symbol. Note that
279 	 * we don't need to record the offset (relative to the GOT section)
280 	 * here, since the PLT relocations will be sorted later and we
281 	 * will generate GOT section according to the new order.
282 	 */
283 	_create_plt_reloc(ld, lsb, 0);
284 }
285 
286 static void
_reserve_plt_entry(struct ld * ld,struct ld_symbol * lsb)287 _reserve_plt_entry(struct ld *ld, struct ld_symbol *lsb)
288 {
289 	struct ld_input_section *is;
290 
291 	is = _find_and_create_plt_section(ld, 1);
292 
293 	(void) ld_input_reserve_ibuf(is, 1);
294 	lsb->lsb_plt = 1;
295 }
296 
297 static void
_create_plt_reloc(struct ld * ld,struct ld_symbol * lsb,uint64_t offset)298 _create_plt_reloc(struct ld *ld, struct ld_symbol *lsb, uint64_t offset)
299 {
300 
301 	ld_reloc_create_entry(ld, ".rela.plt", NULL, R_X86_64_JUMP_SLOT,
302 	    lsb, offset, 0);
303 
304 	lsb->lsb_dynrel = 1;
305 }
306 
307 static void
_create_got_reloc(struct ld * ld,struct ld_symbol * lsb,uint64_t type,uint64_t offset)308 _create_got_reloc(struct ld *ld, struct ld_symbol *lsb, uint64_t type,
309     uint64_t offset)
310 {
311 	struct ld_input_section *tis;
312 
313 	tis = _find_and_create_got_section(ld, 0);
314 	assert(tis != NULL);
315 
316 	ld_reloc_create_entry(ld, ".rela.got", tis, type, lsb, offset, 0);
317 
318 	if (type != R_X86_64_RELATIVE)
319 		lsb->lsb_dynrel = 1;
320 }
321 
322 static void
_create_copy_reloc(struct ld * ld,struct ld_symbol * lsb)323 _create_copy_reloc(struct ld *ld, struct ld_symbol *lsb)
324 {
325 	struct ld_input_section *tis;
326 
327 	ld_dynamic_reserve_dynbss_entry(ld, lsb);
328 
329 	tis = ld_input_find_internal_section(ld, ".dynbss");
330 	assert(tis != NULL);
331 
332 	ld_reloc_create_entry(ld, ".rela.bss", tis, R_X86_64_COPY, lsb,
333 	    lsb->lsb_value, 0);
334 
335 	lsb->lsb_dynrel = 1;
336 }
337 
338 static void
_create_dynamic_reloc(struct ld * ld,struct ld_input_section * is,struct ld_symbol * lsb,uint64_t type,uint64_t offset,int64_t addend)339 _create_dynamic_reloc(struct ld *ld, struct ld_input_section *is,
340     struct ld_symbol *lsb, uint64_t type, uint64_t offset, int64_t addend)
341 {
342 
343 	if (lsb->lsb_bind == STB_LOCAL) {
344 		if (is->is_flags & SHF_WRITE)
345 			ld_reloc_create_entry(ld, ".rela.data.rel.local",
346 			    is, type, lsb, offset, addend);
347 		else
348 			ld_reloc_create_entry(ld, ".rela.data.rel.ro.local",
349 			    is, type, lsb, offset, addend);
350 	} else {
351 		if (is->is_flags & SHF_WRITE)
352 			ld_reloc_create_entry(ld, ".rela.data.rel",
353 			    is, type, lsb, offset, addend);
354 		else
355 			ld_reloc_create_entry(ld, ".rela.data.rel.ro",
356 			    is, type, lsb, offset, addend);
357 	}
358 
359 	if (type != R_X86_64_RELATIVE)
360 		lsb->lsb_dynrel = 1;
361 }
362 
363 static void
_finalize_reloc(struct ld * ld,struct ld_input_section * tis,struct ld_reloc_entry * lre)364 _finalize_reloc(struct ld *ld, struct ld_input_section *tis,
365     struct ld_reloc_entry *lre)
366 {
367 	struct ld_symbol *lsb;
368 
369 	(void) ld;
370 	(void) tis;
371 
372 	lsb = ld_symbols_ref(lre->lre_sym);
373 
374 	switch (lre->lre_type) {
375 	case R_X86_64_RELATIVE:
376 		/*
377 		 * Update the addend stored in the original relocation to
378 		 * point to the new location, by adding the updated symbol
379 		 * value.
380 		 */
381 		lre->lre_addend += lsb->lsb_value;
382 
383 		/* R_X86_64_RELATIVE should not associate with a symbol. */
384 		lre->lre_sym = NULL;
385 		break;
386 
387 	case R_X86_64_DTPMOD64:
388 		/*
389 		 * Relocation R_X86_64_DTPMOD64 generated for local dynamic
390 		 * TLS model should not assoicate with a symbol.
391 		 */
392 		if (lre->lre_type == R_X86_64_DTPMOD64 &&
393 		    lsb->lsb_tls_ld)
394 			lre->lre_sym = NULL;
395 		break;
396 
397 	default:
398 		break;
399 	}
400 }
401 
402 static void
_finalize_got_and_plt(struct ld * ld)403 _finalize_got_and_plt(struct ld *ld)
404 {
405 	struct ld_output *lo;
406 	struct ld_input_section *got_is, *rela_got_is, *plt_is, *rela_plt_is;
407 	struct ld_output_section *got_os, *plt_os, *rela_plt_os;
408 	struct ld_reloc_entry *lre;
409 	struct ld_symbol *lsb;
410 	char dynamic_symbol[] = "_DYNAMIC";
411 	uint8_t *got, *plt;
412 	uint64_t u64;
413 	int32_t s32, pltgot, gotpcrel;
414 	int i, j;
415 
416 	lo = ld->ld_output;
417 	assert(lo != NULL);
418 
419 	/*
420 	 * Intiailze all .got section entries to zero.
421 	 */
422 	got_is = _find_and_create_got_section(ld, 0);
423 	if (got_is != NULL)
424 		memset(got_is->is_ibuf, 0, got_is->is_size);
425 
426 	/*
427 	 * Search for GOT relocations that requires filling in symbol
428 	 * value.
429 	 */
430 	rela_got_is = ld_input_find_internal_section(ld, ".rela.got");
431 	if (rela_got_is != NULL && rela_got_is->is_reloc != NULL) {
432 		STAILQ_FOREACH(lre, rela_got_is->is_reloc, lre_next) {
433 			if (lre->lre_type == R_X86_64_RELATIVE) {
434 				lsb = lre->lre_sym;
435 				got = (uint8_t *) got_is->is_ibuf +
436 				    lsb->lsb_got_off;
437 				WRITE_64(got, lsb->lsb_value);
438 			}
439 		}
440 	}
441 
442 	/*
443 	 * Find the .plt section. The buffers should have been allocated
444 	 * at this point.
445 	 */
446 	plt_is = _find_and_create_plt_section(ld, 0);
447 	if (plt_is == NULL)
448 		return;
449 	plt_os = plt_is->is_output;
450 	plt = plt_is->is_ibuf;
451 	assert(plt != NULL);
452 
453 	/*
454 	 * Find the .got.plt and .rela.plt section. If the .plt section
455 	 * exists, the .got.plt and .rela.plt section should exist too.
456 	 */
457 	got_is = _find_and_create_gotplt_section(ld, 0);
458 	assert(got_is != NULL);
459 	got_os = got_is->is_output;
460 	lo->lo_gotplt = got_os;
461 	got = got_is->is_ibuf;
462 	assert(got != NULL);
463 	rela_plt_is = ld_input_find_internal_section(ld, ".rela.plt");
464 	assert(rela_plt_is != NULL);
465 	rela_plt_os = rela_plt_is->is_output;
466 	lo->lo_rel_plt = rela_plt_os;
467 
468 	/* Point sh_info field of the .rela.plt to .plt section. */
469 	rela_plt_os->os_info = plt_os;
470 
471 	/* Fill in the value of symbol _DYNAMIC in the first GOT entry. */
472 	ld_symbols_get_value(ld, dynamic_symbol, &u64);
473 	WRITE_64(got, u64);
474 	got += 8;
475 
476 	/* Reserve the second and the third entry for the dynamic linker. */
477 	memset(got, 0, 16);
478 	got += 16;
479 
480 	/*
481 	 * Write the initial PLT entry.
482 	 */
483 
484 	/* Calculate the relative offset from PLT to GOT. */
485 	pltgot = got_os->os_addr - plt_os->os_addr;
486 
487 	/*
488 	 * Push the second GOT entry to the stack for the dynamic
489 	 * linker. (PUSH reg/memXX [RIP+disp32]) (6 bytes for push)
490 	 */
491 	WRITE_8(plt, 0xff);
492 	WRITE_8(plt + 1, 0x35);
493 	s32 = pltgot - 6 + 8;
494 	WRITE_32(plt + 2, s32);
495 	plt += 6;
496 
497 	/*
498 	 * Jump to the address in the third GOT entry (call into
499 	 * the dynamic linker). (JMP reg/memXX [RIP+disp32])
500 	 * (6 bytes for jmp)
501 	 */
502 	WRITE_8(plt, 0xff);
503 	WRITE_8(plt + 1, 0x25);
504 	s32 = pltgot - 12 + 16;
505 	WRITE_32(plt + 2, s32);
506 	plt += 6;
507 
508 	/* Padding: 4-byte nop. (NOP [rAx+disp8]) */
509 	WRITE_8(plt, 0x0f);
510 	WRITE_8(plt + 1, 0x1f);
511 	WRITE_8(plt + 2, 0x40);
512 	WRITE_8(plt + 3, 0x0);
513 	plt += 4;
514 
515 	/*
516 	 * Walk through the sorted PLT relocations in the output section
517 	 * and fill in each GOT and PLT entries.
518 	 */
519 	i = 3;
520 	j = 0;
521 	STAILQ_FOREACH(lre, rela_plt_is->is_reloc, lre_next) {
522 		lsb = ld_symbols_ref(lre->lre_sym);
523 
524 		/*
525 		 * Set symbol's PLT offset to the address of this PLT entry.
526 		 * The PLT offset is used in relocation processing later.
527 		 */
528 		lsb->lsb_plt_off = plt_os->os_addr + (i - 2) * 16;
529 
530 		/*
531 		 * Update the offset for the R_X86_64_JUMP_SLOT relocation
532 		 * entry, pointing to the corresponding GOT entry.
533 		 */
534 		lre->lre_offset = got_os->os_addr + i * 8;
535 
536 		/*
537 		 * Calculate the IP-relative offset to the GOT entry for
538 		 * this function. (6 bytes for jmp)
539 		 */
540 		gotpcrel = pltgot + i * 8 - (i - 2) * 16 - 6;
541 
542 		/*
543 		 * PLT: Jump to the address in the GOT entry for this
544 		 * function. (JMP reg/memXX [RIP+disp32])
545 		 */
546 		WRITE_8(plt, 0xff);
547 		WRITE_8(plt + 1, 0x25);
548 		WRITE_32(plt + 2, gotpcrel);
549 		plt += 6;
550 
551 		/*
552 		 * PLT: Symbol is not resolved, push the relocation index to
553 		 * the stack. (PUSH imm32)
554 		 */
555 		WRITE_8(plt, 0x68);
556 		WRITE_32(plt + 1, j);
557 		plt += 5;
558 
559 		/*
560 		 * PLT: Jump to the first PLT entry, eventually call the
561 		 * dynamic linker. (JMP rel32off)
562 		 */
563 		WRITE_8(plt, 0xe9);
564 		s32 = - (i - 1) * 16;
565 		WRITE_32(plt + 1, s32);
566 		plt += 5;
567 
568 		/*
569 		 * GOT: Write the GOT entry for this function, pointing to
570 		 * the push op.
571 		 */
572 		u64 = plt_os->os_addr + (i - 2) * 16 + 6;
573 		WRITE_64(got, u64);
574 
575 		/* Increase relocation entry index. */
576 		j++;
577 
578 		/* Move to next GOT entry. */
579 		got += 8;
580 		i++;
581 	}
582 
583 	assert(got == (uint8_t *) got_is->is_ibuf + got_is->is_size);
584 	assert(plt == (uint8_t *) plt_is->is_ibuf + plt_is->is_size);
585 }
586 
587 static void
_scan_reloc(struct ld * ld,struct ld_input_section * is,struct ld_reloc_entry * lre)588 _scan_reloc(struct ld *ld, struct ld_input_section *is,
589     struct ld_reloc_entry *lre)
590 {
591 	struct ld_symbol *lsb;
592 	enum ld_tls_relax tr;
593 
594 	lsb = ld_symbols_ref(lre->lre_sym);
595 
596 	/*
597 	 * TODO: We do not yet support "Large Models" and relevant
598 	 * relocation types R_X86_64_GOT64, R_X86_64_GOTPCREL64,
599 	 * R_X86_64_GOTPC64, R_X86_64_GOTPLT64 and R_X86_64_PLTOFF64.
600 	 * Refer to AMD64 ELF ABI for details.
601 	 */
602 
603 	switch (lre->lre_type) {
604 	case R_X86_64_NONE:
605 		break;
606 
607 	case R_X86_64_64:
608 	case R_X86_64_32:
609 	case R_X86_64_32S:
610 	case R_X86_64_16:
611 	case R_X86_64_8:
612 
613 		/*
614 		 * For a local symbol, if the linker output a PIE or DSO,
615 		 * we should generate a R_X86_64_RELATIVE reloc for
616 		 * R_X86_64_64. We don't know how to generate dynamic reloc
617 		 * for other reloc types since R_X86_64_RELATIVE is 64 bits.
618 		 * We can not use them directly either because FreeBSD rtld(1)
619 		 * (and probably glibc) doesn't accept absolute address
620 		 * reloction other than R_X86_64_64.
621 		 */
622 		if (lsb->lsb_bind == STB_LOCAL) {
623 			if (ld->ld_pie || ld->ld_dso) {
624 				if (lre->lre_type == R_X86_64_64)
625 					_create_dynamic_reloc(ld, is, lsb,
626 					    R_X86_64_RELATIVE, lre->lre_offset,
627 					    lre->lre_addend);
628 				else
629 					_warn_pic(ld, lre);
630 			}
631 			break;
632 		}
633 
634 		/*
635 		 * For a global symbol, we probably need to generate PLT entry
636 		 * and/or a dynamic relocation.
637 		 *
638 		 * Note here, normally the compiler will generate a PC-relative
639 		 * relocation for function calls. However, if the code retrieve
640 		 * the address of a function and call it indirectly, assembler
641 		 * will generate absolute relocation instead. That's why we
642 		 * should check if we need to create a PLT entry here. Also, if
643 		 * we're going to create the PLT entry, we should also set the
644 		 * symbol value to the address of PLT entry just in case the
645 		 * function address is used to compare with other function
646 		 * addresses. (If PLT address is used, function will have
647 		 * unified address in the main executable and DSOs)
648 		 */
649 		if (ld_reloc_require_plt(ld, lre)) {
650 			if (!lsb->lsb_plt) {
651 				_reserve_gotplt_entry(ld, lsb);
652 				_reserve_plt_entry(ld, lsb);
653 			}
654 			/*
655 			 * Note here even if we have generated PLT for this
656 			 * function before, we still need to set this flag.
657 			 * It's possible that we first see the relative
658 			 * relocation then this absolute relocation, in
659 			 * other words, the same function can be called in
660 			 * different ways.
661 			 */
662 			lsb->lsb_func_addr = 1;
663 		}
664 
665 		if (ld_reloc_require_copy_reloc(ld, lre) &&
666 		    !lsb->lsb_copy_reloc)
667 			_create_copy_reloc(ld, lsb);
668 		else if (ld_reloc_require_dynamic_reloc(ld, lre)) {
669 			/* We only support R_X86_64_64. (See above) */
670 			if (lre->lre_type != R_X86_64_64) {
671 				_warn_pic(ld, lre);
672 				break;
673 			}
674 			/*
675 			 * Check if we can relax R_X86_64_64 to
676 			 * R_X86_64_RELATIVE instead.
677 			 */
678 			if (ld_reloc_relative_relax(ld, lre))
679 				_create_dynamic_reloc(ld, is, lsb,
680 				    R_X86_64_RELATIVE, lre->lre_offset,
681 				    lre->lre_addend);
682 			else
683 				_create_dynamic_reloc(ld, is, lsb,
684 				    R_X86_64_64, lre->lre_offset,
685 				    lre->lre_addend);
686 		}
687 
688 		break;
689 
690 	case R_X86_64_PLT32:
691 		/*
692 		 * In some cases we don't really need to generate a PLT
693 		 * entry, then a R_X86_64_PLT32 relocation can be relaxed
694 		 * to a R_X86_64_PC32 relocation.
695 		 */
696 
697 		if (lsb->lsb_bind == STB_LOCAL ||
698 		    !ld_reloc_require_plt(ld, lre)) {
699 			lre->lre_type = R_X86_64_PC32;
700 			break;
701 		}
702 
703 		/*
704 		 * If linker outputs an normal executable and the symbol is
705 		 * defined but is not defined inside a DSO, we can generate
706 		 * a R_X86_64_PC32 relocation instead.
707 		 */
708 		if (ld->ld_exec && lsb->lsb_shndx != SHN_UNDEF &&
709 		    (lsb->lsb_input == NULL ||
710 		    lsb->lsb_input->li_type != LIT_DSO)) {
711 			lre->lre_type = R_X86_64_PC32;
712 			break;
713 		}
714 
715 		/* Create an PLT entry otherwise. */
716 		if (!lsb->lsb_plt) {
717 			_reserve_gotplt_entry(ld, lsb);
718 			_reserve_plt_entry(ld, lsb);
719 		}
720 		break;
721 
722 	case R_X86_64_PC64:
723 	case R_X86_64_PC32:
724 	case R_X86_64_PC16:
725 	case R_X86_64_PC8:
726 
727 		/*
728 		 * When these relocations apply to a global symbol, we should
729 		 * check if we need to generate PLT entry and/or a dynamic
730 		 * relocation.
731 		 */
732 		if (lsb->lsb_bind != STB_LOCAL) {
733 			if (ld_reloc_require_plt(ld, lre) && !lsb->lsb_plt) {
734 				_reserve_gotplt_entry(ld, lsb);
735 				_reserve_plt_entry(ld, lsb);
736 			}
737 
738 			if (ld_reloc_require_copy_reloc(ld, lre) &&
739 			    !lsb->lsb_copy_reloc)
740 				_create_copy_reloc(ld, lsb);
741 			else if (ld_reloc_require_dynamic_reloc(ld, lre)) {
742 				/*
743 				 * We can not generate dynamic relocation for
744 				 * these PC-relative relocation since they
745 				 * are probably not supported by the runtime
746 				 * linkers.
747 				 *
748 				 * Note: FreeBSD rtld(1) does support
749 				 * R_X86_64_PC32.
750 				 */
751 				_warn_pic(ld, lre);
752 			}
753 		}
754 		break;
755 
756 	case R_X86_64_GOTOFF64:
757 	case R_X86_64_GOTPC32:
758 		/*
759 		 * These relocation types use GOT address as a base address
760 		 * and instruct the linker to build a GOT.
761 		 */
762 		(void) _find_and_create_got_section(ld, 1);
763 		break;
764 
765 	case R_X86_64_GOT32:
766 	case R_X86_64_GOTPCREL:
767 		/*
768 		 * These relocation types instruct the linker to build a
769 		 * GOT and generate a GOT entry.
770 		 */
771 		if (!lsb->lsb_got) {
772 			_reserve_got_entry(ld, lsb, 1);
773 			/*
774 			 * TODO: For now we always create a R_X86_64_GLOB_DAT
775 			 * relocation for a GOT entry. There are cases that
776 			 * the symbol's address is known at link time and
777 			 * the GOT entry value can be filled in by the program
778 			 * linker instead.
779 			 */
780 			if (ld_reloc_require_glob_dat(ld, lre))
781 				_create_got_reloc(ld, lsb, R_X86_64_GLOB_DAT,
782 				    lsb->lsb_got_off);
783 			else
784 				_create_got_reloc(ld, lsb, R_X86_64_RELATIVE,
785 				    lsb->lsb_got_off);
786 		}
787 		break;
788 
789 	case R_X86_64_TLSGD:	/* Global Dynamic */
790 		tr = _tls_check_relax(ld, lre);
791 		switch (tr) {
792 		case TLS_RELAX_NONE:
793 			_create_tls_gd_reloc(ld, lsb);
794 			break;
795 		case TLS_RELAX_INIT_EXEC:
796 			_create_tls_ie_reloc(ld, lsb);
797 			break;
798 		case TLS_RELAX_LOCAL_EXEC:
799 			break;
800 		default:
801 			ld_fatal(ld, "Internal: invalid TLS relaxation %d",
802 			    tr);
803 			break;
804 		}
805 		break;
806 
807 	case R_X86_64_TLSLD:	/* Local Dynamic */
808 		tr = _tls_check_relax(ld, lre);
809 		if (tr == TLS_RELAX_NONE)
810 			_create_tls_ld_reloc(ld, lsb);
811 		else if (tr != TLS_RELAX_LOCAL_EXEC)
812 			ld_fatal(ld, "Internal: invalid TLS relaxation %d",
813 			    tr);
814 		break;
815 
816 	case R_X86_64_DTPOFF32:
817 		/* Handled by R_X86_64_TLSLD case. */
818 		break;
819 
820 	case R_X86_64_GOTTPOFF:	/* Initial Exec */
821 		tr = _tls_check_relax(ld, lre);
822 		if (tr == TLS_RELAX_NONE)
823 			_create_tls_ie_reloc(ld, lsb);
824 		else if (tr != TLS_RELAX_LOCAL_EXEC)
825 			ld_fatal(ld, "Internal: invalid TLS relaxation %d",
826 			    tr);
827 		break;
828 
829 	case R_X86_64_TPOFF32:	/* Local Exec */
830 		/* No further relaxation possible. */
831 		break;
832 
833 	case R_X86_64_GOTPC32_TLSDESC:
834 	case R_X86_64_TLSDESC_CALL:
835 		/* TODO. */
836 		break;
837 
838 	default:
839 		ld_warn(ld, "can not handle relocation %ju",
840 		    lre->lre_type);
841 		break;
842 	}
843 }
844 
845 static uint64_t
_got_offset(struct ld * ld,struct ld_symbol * lsb)846 _got_offset(struct ld *ld, struct ld_symbol *lsb)
847 {
848 	struct ld_output_section *os;
849 
850 	assert(lsb->lsb_got);
851 
852 	if (ld->ld_got == NULL) {
853 		ld->ld_got = _find_and_create_got_section(ld, 0);
854 		assert(ld->ld_got != NULL);
855 	}
856 
857 	os = ld->ld_got->is_output;
858 
859 	return (os->os_addr + ld->ld_got->is_reloff + lsb->lsb_got_off);
860 }
861 
862 static void
_process_reloc(struct ld * ld,struct ld_input_section * is,struct ld_reloc_entry * lre,struct ld_symbol * lsb,uint8_t * buf)863 _process_reloc(struct ld *ld, struct ld_input_section *is,
864     struct ld_reloc_entry *lre, struct ld_symbol *lsb, uint8_t *buf)
865 {
866 	struct ld_state *ls;
867 	struct ld_output *lo;
868 	uint64_t u64, s, l, p, g;
869 	int64_t s64;
870 	uint32_t u32;
871 	int32_t s32;
872 	enum ld_tls_relax tr;
873 
874 	ls = &ld->ld_state;
875 
876 	lo = ld->ld_output;
877 	assert(lo != NULL);
878 
879 	l = lsb->lsb_plt_off;
880 	p = lre->lre_offset + is->is_output->os_addr + is->is_reloff;
881 	s = lsb->lsb_value;
882 
883 	switch (lre->lre_type) {
884 	case R_X86_64_NONE:
885 		break;
886 
887 	case R_X86_64_64:
888 		WRITE_64(buf + lre->lre_offset, s + lre->lre_addend);
889 		break;
890 
891 	case R_X86_64_PC32:
892 		if (lsb->lsb_plt)
893 			s32 = l + lre->lre_addend - p;
894 		else
895 			s32 = s + lre->lre_addend - p;
896 		WRITE_32(buf + lre->lre_offset, s32);
897 		break;
898 
899 	case R_X86_64_PLT32:
900 		if (!ls->ls_ignore_next_plt) {
901 			s32 = l + lre->lre_addend - p;
902 			WRITE_32(buf + lre->lre_offset, s32);
903 		} else
904 			ls->ls_ignore_next_plt = 0;
905 		break;
906 
907 	case R_X86_64_GOTPCREL:
908 		g = _got_offset(ld, lsb);
909 		s32 = g + lre->lre_addend - p;
910 		WRITE_32(buf + lre->lre_offset, s32);
911 		break;
912 
913 	case R_X86_64_32:
914 		u64 = s + lre->lre_addend;
915 		u32 = u64 & 0xffffffff;
916 		if (u64 != u32)
917 			ld_fatal(ld, "R_X86_64_32 relocation failed");
918 		WRITE_32(buf + lre->lre_offset, u32);
919 		break;
920 
921 	case R_X86_64_32S:
922 		s64 = s + lre->lre_addend;
923 		s32 = s64 & 0xffffffff;
924 		if (s64 != s32)
925 			ld_fatal(ld, "R_X86_64_32S relocation failed");
926 		WRITE_32(buf + lre->lre_offset, s32);
927 		break;
928 
929 	case R_X86_64_TLSGD:	/* Global Dynamic */
930 		tr = _tls_check_relax(ld, lre);
931 		switch (tr) {
932 		case TLS_RELAX_NONE:
933 			g = _got_offset(ld, lsb);
934 			s32 = g + lre->lre_addend - p;
935 			WRITE_32(buf + lre->lre_offset, s32);
936 			break;
937 		case TLS_RELAX_INIT_EXEC:
938 			g = _got_offset(ld, lsb);
939 			_tls_relax_gd_to_ie(ld, ls, lo, lre, p, g, buf);
940 			break;
941 		case TLS_RELAX_LOCAL_EXEC:
942 			_tls_relax_gd_to_le(ld, ls, lo, lre, lsb, buf);
943 			break;
944 		default:
945 			ld_fatal(ld, "Internal: invalid TLS relaxation %d",
946 			    tr);
947 			break;
948 		}
949 		break;
950 
951 	case R_X86_64_TLSLD:	/* Local Dynamic */
952 		tr = _tls_check_relax(ld, lre);
953 		switch (tr) {
954 		case TLS_RELAX_NONE:
955 			g = _got_offset(ld, lsb);
956 			s32 = g + lre->lre_addend - p;
957 			WRITE_32(buf + lre->lre_offset, s32);
958 			break;
959 		case TLS_RELAX_LOCAL_EXEC:
960 			_tls_relax_ld_to_le(ld, ls, lre, buf);
961 			break;
962 		default:
963 			ld_fatal(ld, "Internal: invalid TLS relaxation %d",
964 			    tr);
965 			break;
966 		}
967 		break;
968 
969 	case R_X86_64_DTPOFF32:	/* Local Dynamic (offset) */
970 		tr = _tls_check_relax(ld, lre);
971 		switch (tr) {
972 		case TLS_RELAX_NONE:
973 			s32 = _tls_dtpoff(lo, lsb);
974 			WRITE_32(buf + lre->lre_offset, s32);
975 			break;
976 		case TLS_RELAX_LOCAL_EXEC:
977 			s32 = _tls_tpoff(lo, lsb);
978 			WRITE_32(buf + lre->lre_offset, s32);
979 			break;
980 		default:
981 			ld_fatal(ld, "Internal: invalid TLS relaxation %d",
982 			    tr);
983 			break;
984 		}
985 		break;
986 
987 	case R_X86_64_GOTTPOFF:	/* Initial Exec */
988 		tr = _tls_check_relax(ld, lre);
989 		switch (tr) {
990 		case TLS_RELAX_NONE:
991 			g = _got_offset(ld, lsb);
992 			s32 = g + lre->lre_addend - p;
993 			WRITE_32(buf + lre->lre_offset, s32);
994 			break;
995 		case TLS_RELAX_LOCAL_EXEC:
996 			_tls_relax_ie_to_le(ld, lo, lre, lsb, buf);
997 			break;
998 		default:
999 			ld_fatal(ld, "Internal: invalid TLS relaxation %d",
1000 			    tr);
1001 			break;
1002 		}
1003 		break;
1004 
1005 	case R_X86_64_TPOFF32:	/* Local Exec */
1006 		s32 = _tls_tpoff(lo, lsb);
1007 		WRITE_32(buf + lre->lre_offset, s32);
1008 		break;
1009 
1010 	default:
1011 		ld_warn(ld, "Relocation %s not supported",
1012 		    _reloc2str(lre->lre_type));
1013 		break;
1014 	}
1015 }
1016 
1017 static void
_adjust_reloc(struct ld * ld,struct ld_input_section * is,struct ld_reloc_entry * lre,struct ld_symbol * lsb,uint8_t * buf)1018 _adjust_reloc(struct ld *ld, struct ld_input_section *is,
1019     struct ld_reloc_entry *lre, struct ld_symbol *lsb, uint8_t *buf)
1020 {
1021 	struct ld_input_section *_is;
1022 
1023 	(void) ld;
1024 	(void) is;
1025 	(void) buf;
1026 
1027 	/* Only need to adjust relocation against section symbols. */
1028 	if (lsb->lsb_type != STT_SECTION)
1029 		return;
1030 
1031 	if ((_is = lsb->lsb_is) == NULL || _is->is_output == NULL)
1032 		return;
1033 
1034 	/*
1035 	 * Update the relocation addend to point to the new location
1036 	 * in the output object.
1037 	 */
1038 	lre->lre_addend += _is->is_reloff;
1039 }
1040 
1041 static enum ld_tls_relax
_tls_check_relax(struct ld * ld,struct ld_reloc_entry * lre)1042 _tls_check_relax(struct ld *ld, struct ld_reloc_entry *lre)
1043 {
1044 	struct ld_symbol *lsb;
1045 
1046 	lsb = ld_symbols_ref(lre->lre_sym);
1047 
1048 	/*
1049 	 * If the linker is performing -static linking, we should always
1050 	 * use the Local Exec model.
1051 	 */
1052 	if (!ld->ld_dynamic_link)
1053 		return (TLS_RELAX_LOCAL_EXEC);
1054 
1055 	/*
1056 	 * If the linker is creating a DSO, we can not perform any TLS
1057 	 * relaxation.
1058 	 */
1059 	if (ld->ld_dso)
1060 		return (TLS_RELAX_NONE);
1061 
1062 	/*
1063 	 * The linker is creating an executable, if the symbol is
1064 	 * defined in a regular object, we can use the Local Exec model.
1065 	 */
1066 	if (lsb->lsb_shndx != SHN_UNDEF && ld_symbols_in_regular(lsb))
1067 		return (TLS_RELAX_LOCAL_EXEC);
1068 
1069 	/*
1070 	 * If the TLS model is Global Dynamic, we can relax it to Initial
1071 	 * Exec model since the linker is creating an executable.
1072 	 */
1073 	if (lre->lre_type == R_X86_64_TLSGD)
1074 		return (TLS_RELAX_INIT_EXEC);
1075 
1076 	/* For all the other cases, no relaxation can be done. */
1077 	return (TLS_RELAX_NONE);
1078 }
1079 
1080 static int32_t
_tls_tpoff(struct ld_output * lo,struct ld_symbol * lsb)1081 _tls_tpoff(struct ld_output *lo, struct ld_symbol *lsb)
1082 {
1083 	int32_t tls_off;
1084 
1085 	tls_off = -roundup(lo->lo_tls_size, lo->lo_tls_align);
1086 
1087 	return (tls_off + (lsb->lsb_value - lo->lo_tls_addr));
1088 }
1089 
1090 static int32_t
_tls_dtpoff(struct ld_output * lo,struct ld_symbol * lsb)1091 _tls_dtpoff(struct ld_output *lo, struct ld_symbol *lsb)
1092 {
1093 
1094 	return (lsb->lsb_value - lo->lo_tls_addr);
1095 }
1096 
1097 static int
_tls_verify_gd(uint8_t * buf,uint64_t off)1098 _tls_verify_gd(uint8_t *buf, uint64_t off)
1099 {
1100 	/*
1101 	 * Global Dynamic model:
1102 	 *
1103 	 * 0x00 .byte 0x66
1104 	 * 0x01 leaq x@tlsgd(%rip), %rdi
1105 	 * 0x08 .word 0x6666
1106 	 * 0x0a rex64
1107 	 * 0x0b call _tls_get_addr@plt
1108 	 */
1109 	uint8_t gd[] = "\x66\x48\x8d\x3d\x00\x00\x00\x00"
1110 	    "\x66\x66\x48\xe8\x00\x00\x00\x00";
1111 
1112 	if (memcmp(buf + off, gd, sizeof(gd) - 1) == 0)
1113 		return (1);
1114 
1115 	return (0);
1116 }
1117 
1118 static int
_tls_verify_ld(uint8_t * buf,uint64_t off)1119 _tls_verify_ld(uint8_t *buf, uint64_t off)
1120 {
1121 	/*
1122 	 * Local Dynamic model:
1123 	 *
1124 	 * 0x00 leaq x@tlsld(%rip), %rdi
1125 	 * 0x07 call _tls_get_addr@plt
1126 	 */
1127 	uint8_t ld[] = "\x48\x8d\x3d\x00\x00\x00\x00"
1128 	    "\xe8\x00\x00\x00\x00";
1129 
1130 	if (memcmp(buf + off, ld, sizeof(ld) - 1) == 0)
1131 		return (1);
1132 
1133 	return (0);
1134 }
1135 
1136 static void
_tls_relax_gd_to_ie(struct ld * ld,struct ld_state * ls,struct ld_output * lo,struct ld_reloc_entry * lre,uint64_t p,uint64_t g,uint8_t * buf)1137 _tls_relax_gd_to_ie(struct ld *ld, struct ld_state *ls, struct ld_output *lo,
1138     struct ld_reloc_entry *lre, uint64_t p, uint64_t g, uint8_t *buf)
1139 {
1140 	/*
1141 	 * Initial Exec model:
1142 	 *
1143 	 * 0x00 movq %fs:0, %rax
1144 	 * 0x09 addq x@gottpoff(%rip), %rax
1145 	 */
1146 	uint8_t ie[] = "\x64\x48\x8b\x04\x25\x00\x00\x00\x00"
1147 	    "\x48\x03\x05\x00\x00\x00\x00";
1148 	int32_t s32;
1149 
1150 	assert(lre->lre_type == R_X86_64_TLSGD);
1151 
1152 	if (!_tls_verify_gd(buf, lre->lre_offset - 4))
1153 		ld_warn(ld, "unrecognized TLS global dynamic model code");
1154 
1155 	/* Rewrite Global Dynamic to Initial Exec model. */
1156 	memcpy((uint8_t *) buf + lre->lre_offset - 4, ie, sizeof(ie) - 1);
1157 
1158 	/*
1159 	 * R_X86_64_TLSGD relocation is applied at gd[4]. After it's relaxed
1160 	 * to Initial Exec model, the resulting R_X86_64_GOTTPOFF relocation
1161 	 * should be applied at ie[12]. The addend should remain the same
1162 	 * since instruction "leaq x@tlsgd(%rip), %rdi" and
1163 	 * "addq x@gottpoff(%rip), %rax" has the same length. `p' is moved
1164 	 * 8 bytes forward.
1165 	 */
1166 	s32 = g + lre->lre_addend - (p + 8);
1167 	WRITE_32(buf + lre->lre_offset + 8, s32);
1168 
1169 	/* Ignore the next R_X86_64_PLT32 relocation for _tls_get_addr. */
1170 	ls->ls_ignore_next_plt = 1;
1171 }
1172 
1173 static void
_tls_relax_gd_to_le(struct ld * ld,struct ld_state * ls,struct ld_output * lo,struct ld_reloc_entry * lre,struct ld_symbol * lsb,uint8_t * buf)1174 _tls_relax_gd_to_le(struct ld *ld, struct ld_state *ls, struct ld_output *lo,
1175     struct ld_reloc_entry *lre, struct ld_symbol *lsb, uint8_t *buf)
1176 {
1177 	/*
1178 	 * Local Exec model:
1179 	 *
1180 	 * 0x00 movq %fs:0, %rax
1181 	 * 0x09 leaq x@tpoff(%rax), %rax
1182 	 */
1183 	uint8_t le[] = "\x64\x48\x8b\x04\x25\x00\x00\x00\x00"
1184 	    "\x48\x8d\x80\x00\x00\x00\x00";
1185 	int32_t s32;
1186 
1187 	if (!_tls_verify_gd(buf, lre->lre_offset - 4))
1188 		ld_warn(ld, "unrecognized TLS global dynamic model code");
1189 
1190 	/* Rewrite Global Dynamic to Local Exec model. */
1191 	memcpy((uint8_t *) buf + lre->lre_offset - 4, le, sizeof(le) - 1);
1192 
1193 	/*
1194 	 * R_X86_64_TLSGD relocation is applied at gd[4]. After it's relaxed
1195 	 * to Local Exec model, the resulting R_X86_64_TPOFF32 should be
1196 	 * applied at le[12].
1197 	 */
1198 	s32 = _tls_tpoff(lo, lsb);
1199 	WRITE_32(buf + lre->lre_offset + 8, s32);
1200 
1201 	/* Ignore the next R_X86_64_PLT32 relocation for _tls_get_addr. */
1202 	ls->ls_ignore_next_plt = 1;
1203 }
1204 
1205 static void
_tls_relax_ld_to_le(struct ld * ld,struct ld_state * ls,struct ld_reloc_entry * lre,uint8_t * buf)1206 _tls_relax_ld_to_le(struct ld *ld, struct ld_state *ls,
1207     struct ld_reloc_entry *lre, uint8_t *buf)
1208 {
1209 	/*
1210 	 * Local Exec model: (with padding)
1211 	 *
1212 	 * 0x00 .word 0x6666
1213 	 * 0x02 .byte 0x66
1214 	 * 0x03 movq %fs:0, %rax
1215 	 */
1216 	uint8_t le_p[] = "\x66\x66\x66\x64\x48\x8b\x04\x25\x00\x00\x00\x00";
1217 
1218 	assert(lre->lre_type == R_X86_64_TLSLD);
1219 
1220 	if (!_tls_verify_ld(buf, lre->lre_offset - 3))
1221 		ld_warn(ld, "unrecognized TLS local dynamic model code");
1222 
1223 	/* Rewrite Local Dynamic to Local Exec model. */
1224 	memcpy(buf + lre->lre_offset - 3, le_p, sizeof(le_p) - 1);
1225 
1226 	/* Ignore the next R_X86_64_PLT32 relocation for _tls_get_addr. */
1227 	ls->ls_ignore_next_plt = 1;
1228 }
1229 
1230 static void
_tls_relax_ie_to_le(struct ld * ld,struct ld_output * lo,struct ld_reloc_entry * lre,struct ld_symbol * lsb,uint8_t * buf)1231 _tls_relax_ie_to_le(struct ld *ld, struct ld_output *lo,
1232     struct ld_reloc_entry *lre, struct ld_symbol *lsb, uint8_t *buf)
1233 {
1234 	int32_t s32;
1235 	uint8_t reg;
1236 
1237 	(void) ld;
1238 
1239 	assert(lre->lre_type == R_X86_64_GOTTPOFF);
1240 
1241 	/*
1242 	 * Rewrite Initial Exec to Local Exec model: rewrite
1243 	 * "movq 0x0(%rip),%reg" to "movq 0x0,%reg". or,
1244 	 * "addq 0x0(%rip),%rsp" to "addq 0x0,%rsp". or,
1245 	 * "addq 0x0(%rip),%reg" to "leaq 0x0(%reg),%reg"
1246 	 */
1247 	reg = buf[lre->lre_offset - 1] >> 3;
1248 	if (buf[lre->lre_offset - 2] == 0x8b) {
1249 		/* movq 0x0(%rip),%reg -> movq 0x0,%reg. */
1250 		buf[lre->lre_offset - 2] = 0xc7;
1251 		buf[lre->lre_offset - 1] = 0xc0 | reg; /* Set r/m to `reg' */
1252 		/*
1253 		 * Set REX.B (high bit for r/m) if REX.R (high bit for reg)
1254 		 * is set.
1255 		 */
1256 		if (buf[lre->lre_offset - 3] == 0x4c)
1257 			buf[lre->lre_offset - 3] = 0x49;
1258 	} else if (reg == 4) {
1259 		/* addq 0x0(%rip),%rsp -> addq 0x0,%rsp */
1260 		buf[lre->lre_offset - 2] = 0x81;
1261 		buf[lre->lre_offset - 1] = 0xc0 | reg; /* Set r/m to `reg' */
1262 		/*
1263 		 * Set REX.B (high bit for r/m) if REX.R (high bit for reg)
1264 		 * is set.
1265 		 */
1266 		if (buf[lre->lre_offset - 3] == 0x4c)
1267 			buf[lre->lre_offset - 3] = 0x49;
1268 	} else {
1269 		/* addq 0x0(%rip),%reg -> leaq 0x0(%reg),%reg */
1270 		buf[lre->lre_offset - 2] = 0x8d;
1271 		/* Both reg and r/m in ModRM should be set to `reg' */
1272 		buf[lre->lre_offset - 1] = 0x80 | reg | (reg << 3);
1273 		/* Set both REX.B and REX.R if REX.R is set */
1274 		if (buf[lre->lre_offset - 3] == 0x4c)
1275 			buf[lre->lre_offset - 3] = 0x4d;
1276 	}
1277 	/*
1278 	 * R_X86_64_GOTTPOFF relocation is applied at ie[12]. After it's
1279 	 * relaxed to Local Exec model, the resulting R_X86_64_TPOFF32
1280 	 * should be applied at le[12]. Thus the offset remains the same.
1281 	 */
1282 	s32 = _tls_tpoff(lo, lsb);
1283 	WRITE_32(buf + lre->lre_offset, s32);
1284 }
1285 
1286 static void
_create_tls_gd_reloc(struct ld * ld,struct ld_symbol * lsb)1287 _create_tls_gd_reloc(struct ld *ld, struct ld_symbol *lsb)
1288 {
1289 
1290 	/*
1291 	 * Reserve 2 GOT entries and generate R_X86_64_DTPMOD64 and
1292 	 * R_X86_64_DTPOFF64 relocations.
1293 	 */
1294 	if (!lsb->lsb_got) {
1295 		_reserve_got_entry(ld, lsb, 2);
1296 		_create_got_reloc(ld, lsb, R_X86_64_DTPMOD64,
1297 		    lsb->lsb_got_off);
1298 		_create_got_reloc(ld, lsb, R_X86_64_DTPOFF64,
1299 		    lsb->lsb_got_off + 8);
1300 	}
1301 }
1302 
1303 static void
_create_tls_ld_reloc(struct ld * ld,struct ld_symbol * lsb)1304 _create_tls_ld_reloc(struct ld *ld, struct ld_symbol *lsb)
1305 {
1306 
1307 	/* Reserve 2 GOT entries and generate R_X86_64_DTPMOD64 reloation. */
1308 	if (!lsb->lsb_got) {
1309 		_reserve_got_entry(ld, lsb, 2);
1310 		_create_got_reloc(ld, lsb, R_X86_64_DTPMOD64,
1311 		    lsb->lsb_got_off);
1312 		lsb->lsb_tls_ld = 1;
1313 	}
1314 }
1315 
1316 static void
_create_tls_ie_reloc(struct ld * ld,struct ld_symbol * lsb)1317 _create_tls_ie_reloc(struct ld *ld, struct ld_symbol *lsb)
1318 {
1319 
1320 	/* Reserve 1 GOT entry and generate R_X86_64_TPOFF64 relocation. */
1321 	if (!lsb->lsb_got) {
1322 		_reserve_got_entry(ld, lsb, 1);
1323 		_create_got_reloc(ld, lsb, R_X86_64_TPOFF64,
1324 		    lsb->lsb_got_off);
1325 	}
1326 }
1327 
1328 void
amd64_register(struct ld * ld)1329 amd64_register(struct ld *ld)
1330 {
1331 	struct ld_arch *amd64, *amd64_alt;
1332 
1333 	if ((amd64 = calloc(1, sizeof(*amd64))) == NULL)
1334 		ld_fatal_std(ld, "calloc");
1335 
1336 	snprintf(amd64->name, sizeof(amd64->name), "%s", "amd64");
1337 
1338 	amd64->script = amd64_script;
1339 	amd64->interp = "/libexec/ld-elf.so.1";
1340 	amd64->get_max_page_size = _get_max_page_size;
1341 	amd64->get_common_page_size = _get_common_page_size;
1342 	amd64->scan_reloc = _scan_reloc;
1343 	amd64->process_reloc = _process_reloc;
1344 	amd64->adjust_reloc = _adjust_reloc;
1345 	amd64->is_absolute_reloc = _is_absolute_reloc;
1346 	amd64->is_relative_reloc = _is_relative_reloc;
1347 	amd64->finalize_reloc = _finalize_reloc;
1348 	amd64->finalize_got_and_plt = _finalize_got_and_plt;
1349 	amd64->reloc_is_64bit = 1;
1350 	amd64->reloc_is_rela = 1;
1351 	amd64->reloc_entsize = sizeof(Elf64_Rela);
1352 
1353 	HASH_ADD_STR(ld->ld_arch_list, name, amd64);
1354 
1355 	if ((amd64_alt = calloc(1, sizeof(*amd64_alt))) == NULL)
1356 		ld_fatal_std(ld, "calloc");
1357 	memcpy(amd64_alt, amd64, sizeof(struct ld_arch));
1358 	amd64_alt->alias = amd64;
1359 	snprintf(amd64_alt->name, sizeof(amd64_alt->name), "%s", "x86-64");
1360 
1361 	HASH_ADD_STR(ld->ld_arch_list, name, amd64_alt);
1362 }
1363