xref: /titanic_50/usr/src/cmd/sgs/libld/common/unwind.c (revision b56bf881a9655cb27b53cba1468312f7c6dfb0a2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include	<string.h>
28 #include	<stdio.h>
29 #include	<sys/types.h>
30 #include	<sgs.h>
31 #include	<debug.h>
32 #include	<_libld.h>
33 #include	<dwarf.h>
34 #include	<stdlib.h>
35 
36 /*
37  * A EH_FRAME_HDR consists of the following:
38  *
39  *	Encoding	Field
40  *	--------------------------------
41  *	unsigned byte	version
42  *	unsigned byte	eh_frame_ptr_enc
43  *	unsigned byte	fde_count_enc
44  *	unsigned byte	table_enc
45  *	encoded		eh_frame_ptr
46  *	encoded		fde_count
47  *	[ binary search table ]
48  *
49  * The binary search table entries each consists of:
50  *
51  *	encoded		initial_func_loc
52  *	encoded		FDE_address
53  *
54  * The entries in the binary search table are sorted
55  * in a increasing order by the initial location.
56  *
57  *
58  * version
59  *
60  *   Version of the .eh_frame_hdr format. This value shall be 1.
61  *
62  * eh_frame_ptr_enc
63  *
64  *    The encoding format of the eh_frame_ptr field.  For shared
65  *    libraries the encoding must be
66  *    DW_EH_PE_sdata4|DW_EH_PE_pcrel or
67  *    DW_EH_PE_sdata4|DW_EH_PE_datarel.
68  *
69  *
70  * fde_count_enc
71  *
72  *    The encoding format of the fde_count field. A value of
73  *    DW_EH_PE_omit indicates the binary search table is not
74  *    present.
75  *
76  * table_enc
77  *
78  *    The encoding format of the entries in the binary search
79  *    table. A value of DW_EH_PE_omit indicates the binary search
80  *    table is not present. For shared libraries the encoding
81  *    must be DW_EH_PE_sdata4|DW_EH_PE_pcrel or
82  *    DW_EH_PE_sdata4|DW_EH_PE_datarel.
83  *
84  *
85  * eh_frame_ptr
86  *
87  *    The encoded value of the pointer to the start of the
88  *    .eh_frame section.
89  *
90  * fde_count
91  *
92  *    The encoded value of the count of entries in the binary
93  *    search table.
94  *
95  * binary search table
96  *
97  *    A binary search table containing fde_count entries. Each
98  *    entry of the table consist of two encoded values, the
99  *    initial location of the function to which an FDE applies,
100  *    and the address of the FDE. The entries are sorted in an
101  *    increasing order by the initial location value.
102  *
103  */
104 
105 
106 /*
107  * EH_FRAME sections
108  * =================
109  *
110  * The call frame information needed for unwinding the stack is output in
111  * an ELF section(s) of type SHT_AMD64_UNWIND (amd64) or SHT_PROGBITS (other).
112  * In the simplest case there will be one such section per object file and it
113  * will be named ".eh_frame".  An .eh_frame section consists of one or more
114  * subsections. Each subsection contains a CIE (Common Information Entry)
115  * followed by varying number of FDEs (Frame Descriptor Entry). A FDE
116  * corresponds to an explicit or compiler generated function in a
117  * compilation unit, all FDEs can access the CIE that begins their
118  * subsection for data.
119  *
120  * If an object file contains C++ template instantiations, there shall be
121  * a separate CIE immediately preceding each FDE corresponding to an
122  * instantiation.
123  *
124  * Using the preferred encoding specified below, the .eh_frame section can
125  * be entirely resolved at link time and thus can become part of the
126  * text segment.
127  *
128  * .eh_frame Section Layout
129  * ------------------------
130  *
131  * EH_PE encoding below refers to the pointer encoding as specified in the
132  * enhanced LSB Chapter 7 for Eh_Frame_Hdr.
133  *
134  * Common Information Entry (CIE)
135  * ------------------------------
136  * CIE has the following format:
137  *
138  *                           Length
139  *                              in
140  *     Field                   Byte      Description
141  *     -----                  ------     -----------
142  *  1. Length                   4        Length of CIE (not including
143  *					 this 4-byte field).
144  *
145  *  2. CIE id                   4        Value Zero (0) for .eh_frame
146  *					 (used to distinguish CIEs and
147  *					 FDEs when scanning the section)
148  *
149  *  3. Version                  1        Value One (1)
150  *
151  *  4. CIE Augmentation       string     Null-terminated string with legal
152  *					 values being "" or 'z' optionally
153  *					 followed by single occurrences of
154  *					 'P', 'L', or 'R' in any order.
155  *     String                            The presence of character(s) in the
156  *                                       string dictates the content of
157  *                                       field 8, the Augmentation Section.
158  *					 Each character has one or two
159  *					 associated operands in the AS.
160  *					 Operand order depends on
161  *					 position in the string ('z' must
162  *					 be first).
163  *
164  *  5. Code Align Factor      uleb128    To be multiplied with the
165  *					 "Advance Location" instructions in
166  *                                       the Call Frame Instructions
167  *
168  *  6. Data Align Factor      sleb128    To be multiplied with all offset
169  *                                       in the Call Frame Instructions
170  *
171  *  7. Ret Address Reg          1        A "virtual" register representation
172  *                                       of the return address. In Dwarf V2,
173  *                                       this is a byte, otherwise it is
174  *                                       uleb128. It is a byte in gcc 3.3.x
175  *
176  *  8. Optional CIE           varying    Present if Augmentation String in
177  *     Augmentation Section              field 4 is not 0.
178  *
179  *     z:
180  * 	size		   uleb128       Length of the remainder of the
181  *				         Augmentation Section
182  *
183  *     P:
184  * 	personality_enc    1	         Encoding specifier - preferred
185  *					 value is a pc-relative, signed
186  *				         4-byte
187  *
188  *
189  *        personality routine (encoded)  Encoded pointer to personality
190  *					 routine (actually to the PLT
191  *				         entry for the personality
192  *				         routine)
193  *     R:
194  * 	code_enc           1	      Non-default encoding for the
195  *				      code-pointers (FDE members
196  *				      "initial_location" and "address_range"
197  *				      and the operand for DW_CFA_set_loc)
198  *				      - preferred value is pc-relative,
199  *				      signed 4-byte.
200  *     L:
201  * 	lsda_enc	   1	      FDE augmentation bodies may contain
202  *				      LSDA pointers. If so they are
203  *				      encoded as specified here -
204  *				      preferred value is pc-relative,
205  *				      signed 4-byte possibly indirect
206  *				      thru a GOT entry.
207  *
208  *
209  *  9. Optional Call Frame varying
210  *     Instructions
211  *
212  * The size of the optional call frame instruction area must be computed
213  * based on the overall size and the offset reached while scanning the
214  * preceding fields of the CIE.
215  *
216  *
217  * Frame Descriptor Entry (FDE)
218  * ----------------------------
219  * FDE has the following format:
220  *
221  *                            Length
222  *                              in
223  *     Field                   Byte      Description
224  *     -----                  ------     -----------
225  *  1. Length                   4        Length of remainder of this FDE
226  *
227  *  2. CIE Pointer              4        Distance from this field to the
228  *				         nearest preceding CIE
229  *				         (uthe value is subtracted from the
230  *					 current address). This value
231  *				         can never be zero and thus can
232  *				         be used to distinguish CIE's and
233  *				         FDE's when scanning the
234  *				         .eh_frame section
235  *
236  *  3. Initial Location       varying    Reference to the function code
237  *                                       corresponding to this FDE.
238  *                                       If 'R' is missing from the CIE
239  *                                       Augmentation String, the field is an
240  *                                       8-byte absolute pointer. Otherwise,
241  *                                       the corresponding EH_PE encoding in the
242  *                                       CIE Augmentation Section is used to
243  *                                       interpret the reference.
244  *
245  *  4. Address Range          varying    Size of the function code corresponding
246  *                                       to this FDE.
247  *                                       If 'R' is missing from the CIE
248  *                                       Augmentation String, the field is an
249  *                                       8-byte unsigned number. Otherwise,
250  *                                       the size is determined by the
251  *				         corresponding EH_PE encoding in the
252  *                                       CIE Augmentation Section (the
253  *				         value is always absolute).
254  *
255  *  5. Optional FDE           varying    present if CIE augmentation
256  *     Augmentation Section	         string is non-empty.
257  *
258  *
259  *     'z':
260  * 	length		   uleb128       length of the remainder of the
261  *				         FDE augmentation section
262  *
263  *
264  *     'L' (and length > 0):
265  *         LSDA               varying    LSDA pointer, encoded in the
266  *				         format specified by the
267  *				         corresponding operand in the CIE's
268  *				         augmentation body.
269  *
270  *  6. Optional Call          varying
271  *     Frame Instructions
272  *
273  * The size of the optional call frame instruction area must be computed
274  * based on the overall size and the offset reached while scanning the
275  * preceding fields of the FDE.
276  *
277  * The overall size of a .eh_frame section is given in the ELF section
278  * header.  The only way to determine the number of entries is to scan
279  * the section till the end and count.
280  *
281  */
282 
283 
284 
285 
286 static uint_t
287 extract_uint(const uchar_t *data, uint64_t *ndx, int do_swap)
288 {
289 	uint_t	r;
290 	uchar_t *p = (uchar_t *)&r;
291 
292 	data += *ndx;
293 	if (do_swap)
294 		UL_ASSIGN_BSWAP_WORD(p, data);
295 	else
296 		UL_ASSIGN_WORD(p, data);
297 
298 	(*ndx) += 4;
299 	return (r);
300 }
301 
302 /*
303  * Create an unwind header (.eh_frame_hdr) output section.
304  * The section is created and space reserved, but the data
305  * is not copied into place. That is done by a later call
306  * to ld_unwind_populate(), after active relocations have been
307  * processed.
308  *
309  * When GNU linkonce processing is in effect, we can end up in a situation
310  * where the FDEs related to discarded sections remain in the eh_frame
311  * section. Ideally, we would remove these dead entries from eh_frame.
312  * However, that optimization has not yet been implemented. In the current
313  * implementation, the number of dead FDEs cannot be determined until
314  * active relocations are processed, and that processing follows the
315  * call to this function. This means that we are unable to detect dead FDEs
316  * here, and the section created by this routine is sized for maximum case
317  * where all FDEs are valid.
318  */
319 uintptr_t
320 ld_unwind_make_hdr(Ofl_desc *ofl)
321 {
322 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
323 	Shdr		*shdr;
324 	Elf_Data	*elfdata;
325 	Is_desc		*isp;
326 	size_t		size;
327 	Xword		fde_cnt;
328 	Aliste		idx1;
329 	Os_desc		*osp;
330 
331 	/*
332 	 * we only build a unwind header if we have
333 	 * some unwind information in the file.
334 	 */
335 	if (ofl->ofl_unwind == NULL)
336 		return (1);
337 
338 	/*
339 	 * Allocate and initialize the Elf_Data structure.
340 	 */
341 	if ((elfdata = libld_calloc(sizeof (Elf_Data), 1)) == NULL)
342 		return (S_ERROR);
343 	elfdata->d_type = ELF_T_BYTE;
344 	elfdata->d_align = ld_targ.t_m.m_word_align;
345 	elfdata->d_version = ofl->ofl_dehdr->e_version;
346 
347 	/*
348 	 * Allocate and initialize the Shdr structure.
349 	 */
350 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == NULL)
351 		return (S_ERROR);
352 	shdr->sh_type = ld_targ.t_m.m_sht_unwind;
353 	shdr->sh_flags = SHF_ALLOC;
354 	shdr->sh_addralign = ld_targ.t_m.m_word_align;
355 	shdr->sh_entsize = 0;
356 
357 	/*
358 	 * Allocate and initialize the Is_desc structure.
359 	 */
360 	if ((isp = libld_calloc(1, sizeof (Is_desc))) == NULL)
361 		return (S_ERROR);
362 	isp->is_name = MSG_ORIG(MSG_SCN_UNWINDHDR);
363 	isp->is_shdr = shdr;
364 	isp->is_indata = elfdata;
365 
366 	if ((ofl->ofl_unwindhdr = ld_place_section(ofl, isp, NULL,
367 	    ld_targ.t_id.id_unwindhdr, NULL)) == (Os_desc *)S_ERROR)
368 		return (S_ERROR);
369 
370 	/*
371 	 * Scan through all of the input Frame information, counting each FDE
372 	 * that requires an index.  Each fde_entry gets a corresponding entry
373 	 * in the binary search table.
374 	 */
375 	fde_cnt = 0;
376 	for (APLIST_TRAVERSE(ofl->ofl_unwind, idx1, osp)) {
377 		Aliste	idx2;
378 		int	os_isdescs_idx;
379 
380 		OS_ISDESCS_TRAVERSE(os_isdescs_idx, osp, idx2, isp) {
381 			uchar_t		*data;
382 			uint64_t	off = 0;
383 
384 			data = isp->is_indata->d_buf;
385 			size = isp->is_indata->d_size;
386 
387 			while (off < size) {
388 				uint_t		length, id;
389 				uint64_t	ndx = 0;
390 
391 				/*
392 				 * Extract length in lsb format.  A zero length
393 				 * indicates that this CIE is a terminator and
394 				 * that processing for unwind information is
395 				 * complete.
396 				 */
397 				length = extract_uint(data + off, &ndx, bswap);
398 				if (length == 0)
399 					break;
400 
401 				/*
402 				 * Extract CIE id in lsb format.
403 				 */
404 				id = extract_uint(data + off, &ndx, bswap);
405 
406 				/*
407 				 * A CIE record has a id of '0', otherwise
408 				 * this is a FDE entry and the 'id' is the
409 				 * CIE pointer.
410 				 */
411 				if (id == 0) {
412 					uint_t	cieversion;
413 					/*
414 					 * The only CIE version supported
415 					 * is '1' - quick sanity check
416 					 * here.
417 					 */
418 					cieversion = data[off + ndx];
419 					ndx += 1;
420 					/* BEGIN CSTYLED */
421 					if (cieversion != 1) {
422 					    eprintf(ofl->ofl_lml, ERR_FATAL,
423 						MSG_INTL(MSG_UNW_BADCIEVERS),
424 						isp->is_file->ifl_name,
425 						isp->is_name, off);
426 					    return (S_ERROR);
427 					}
428 					/* END CSTYLED */
429 				} else {
430 					fde_cnt++;
431 				}
432 				off += length + 4;
433 			}
434 		}
435 	}
436 
437 	/*
438 	 * section size:
439 	 *	byte	    version		+1
440 	 *	byte	    eh_frame_ptr_enc	+1
441 	 *	byte	    fde_count_enc	+1
442 	 *	byte	    table_enc		+1
443 	 *	4 bytes	    eh_frame_ptr	+4
444 	 *	4 bytes	    fde_count		+4
445 	 *	[4 bytes] [4bytes] * fde_count	...
446 	 */
447 	size = 12 + (8 * fde_cnt);
448 
449 	if ((elfdata->d_buf = libld_calloc(size, 1)) == NULL)
450 		return (S_ERROR);
451 	elfdata->d_size = size;
452 	shdr->sh_size = (Xword)size;
453 
454 	return (1);
455 }
456 
457 /*
458  * the comparator function needs to calculate
459  * the actual 'initloc' of a bintab entry - to
460  * do this we initialize the following global to point
461  * to it.
462  */
463 static Addr framehdr_addr;
464 
465 static int
466 bintabcompare(const void *p1, const void *p2)
467 {
468 	uint_t	    *bintab1, *bintab2;
469 	uint_t	    ent1, ent2;
470 
471 	bintab1 = (uint_t *)p1;
472 	bintab2 = (uint_t *)p2;
473 
474 	assert(bintab1 != 0);
475 	assert(bintab2 != 0);
476 
477 	ent1 = *bintab1 + framehdr_addr;
478 	ent2 = *bintab2 + framehdr_addr;
479 
480 	if (ent1 > ent2)
481 		return (1);
482 	if (ent1 < ent2)
483 		return (-1);
484 	return (0);
485 }
486 
487 uintptr_t
488 ld_unwind_populate_hdr(Ofl_desc *ofl)
489 {
490 	uchar_t		*hdrdata;
491 	uint_t		*binarytable;
492 	uint_t		hdroff;
493 	Aliste		idx;
494 	Addr		hdraddr;
495 	Os_desc		*hdrosp;
496 	Os_desc		*osp;
497 	Os_desc		*first_unwind;
498 	uint_t		fde_count;
499 	uint_t		*uint_ptr;
500 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
501 
502 	/*
503 	 * Are we building the unwind hdr?
504 	 */
505 	if ((hdrosp = ofl->ofl_unwindhdr) == 0)
506 		return (1);
507 
508 	hdrdata = hdrosp->os_outdata->d_buf;
509 	hdraddr = hdrosp->os_shdr->sh_addr;
510 	hdroff = 0;
511 
512 	/*
513 	 * version == 1
514 	 */
515 	hdrdata[hdroff++] = 1;
516 	/*
517 	 * The encodings are:
518 	 *
519 	 *  eh_frameptr_enc	sdata4 | pcrel
520 	 *  fde_count_enc	udata4
521 	 *  table_enc		sdata4 | datarel
522 	 */
523 	hdrdata[hdroff++] = DW_EH_PE_sdata4 | DW_EH_PE_pcrel;
524 	hdrdata[hdroff++] = DW_EH_PE_udata4;
525 	hdrdata[hdroff++] = DW_EH_PE_sdata4 | DW_EH_PE_datarel;
526 
527 	/*
528 	 *	Header Offsets
529 	 *	-----------------------------------
530 	 *	byte	    version		+1
531 	 *	byte	    eh_frame_ptr_enc	+1
532 	 *	byte	    fde_count_enc	+1
533 	 *	byte	    table_enc		+1
534 	 *	4 bytes	    eh_frame_ptr	+4
535 	 *	4 bytes	    fde_count		+4
536 	 */
537 	/* LINTED */
538 	binarytable =  (uint_t *)(hdrdata + 12);
539 	first_unwind = 0;
540 	fde_count = 0;
541 
542 	for (APLIST_TRAVERSE(ofl->ofl_unwind, idx, osp)) {
543 		uchar_t		*data;
544 		size_t		size;
545 		uint64_t	off = 0;
546 		uint_t		cieRflag = 0, ciePflag = 0;
547 		Shdr		*shdr;
548 
549 		/*
550 		 * remember first UNWIND section to
551 		 * point to in the frame_ptr entry.
552 		 */
553 		if (first_unwind == 0)
554 			first_unwind = osp;
555 
556 		data = osp->os_outdata->d_buf;
557 		shdr = osp->os_shdr;
558 		size = shdr->sh_size;
559 
560 		while (off < size) {
561 			uint_t	    length, id;
562 			uint64_t    ndx = 0;
563 
564 			/*
565 			 * Extract length in lsb format.  A zero length
566 			 * indicates that this CIE is a terminator and that
567 			 * processing of unwind information is complete.
568 			 */
569 			length = extract_uint(data + off, &ndx, bswap);
570 			if (length == 0)
571 				goto done;
572 
573 			/*
574 			 * Extract CIE id in lsb format.
575 			 */
576 			id = extract_uint(data + off, &ndx, bswap);
577 
578 			/*
579 			 * A CIE record has a id of '0'; otherwise
580 			 * this is a FDE entry and the 'id' is the
581 			 * CIE pointer.
582 			 */
583 			if (id == 0) {
584 				char	*cieaugstr;
585 				uint_t	cieaugndx;
586 
587 				ciePflag = 0;
588 				cieRflag = 0;
589 				/*
590 				 * We need to drill through the CIE
591 				 * to find the Rflag.  It's the Rflag
592 				 * which describes how the FDE code-pointers
593 				 * are encoded.
594 				 */
595 
596 				/*
597 				 * burn through version
598 				 */
599 				ndx++;
600 
601 				/*
602 				 * augstr
603 				 */
604 				cieaugstr = (char *)(&data[off + ndx]);
605 				ndx += strlen(cieaugstr) + 1;
606 
607 				/*
608 				 * calign & dalign
609 				 */
610 				(void) uleb_extract(&data[off], &ndx);
611 				(void) sleb_extract(&data[off], &ndx);
612 
613 				/*
614 				 * retreg
615 				 */
616 				ndx++;
617 
618 				/*
619 				 * we walk through the augmentation
620 				 * section now looking for the Rflag
621 				 */
622 				for (cieaugndx = 0; cieaugstr[cieaugndx];
623 				    cieaugndx++) {
624 					/* BEGIN CSTYLED */
625 					switch (cieaugstr[cieaugndx]) {
626 					case 'z':
627 					    /* size */
628 					    (void) uleb_extract(&data[off],
629 						&ndx);
630 					    break;
631 					case 'P':
632 					    /* personality */
633 					    ciePflag = data[off + ndx];
634 					    ndx++;
635 						/*
636 						 * Just need to extract the
637 						 * value to move on to the next
638 						 * field.
639 						 */
640 					    (void) dwarf_ehe_extract(
641 						&data[off + ndx],
642 						&ndx, ciePflag,
643 						ofl->ofl_dehdr->e_ident,
644 						shdr->sh_addr, off + ndx);
645 					    break;
646 					case 'R':
647 					    /* code encoding */
648 					    cieRflag = data[off + ndx];
649 					    ndx++;
650 					    break;
651 					case 'L':
652 					    /* lsda encoding */
653 					    ndx++;
654 					    break;
655 					}
656 					/* END CSTYLED */
657 				}
658 			} else {
659 				uint_t	    bintabndx;
660 				uint64_t    initloc;
661 				uint64_t    fdeaddr;
662 
663 				initloc = dwarf_ehe_extract(&data[off],
664 				    &ndx, cieRflag, ofl->ofl_dehdr->e_ident,
665 				    shdr->sh_addr, off + ndx);
666 
667 				/*
668 				 * Ignore FDEs with initloc set to 0.
669 				 * initloc will not be 0 unless this FDE was
670 				 * abandoned due to GNU linkonce processing.
671 				 * The 0 value occurs because we don't resolve
672 				 * sloppy relocations for unwind header target
673 				 * sections.
674 				 */
675 				if (initloc != 0) {
676 					bintabndx = fde_count * 2;
677 					fde_count++;
678 
679 					/*
680 					 * FDEaddr is adjusted
681 					 * to account for the length & id which
682 					 * have already been consumed.
683 					 */
684 					fdeaddr = shdr->sh_addr + off;
685 
686 					binarytable[bintabndx] =
687 					    (uint_t)(initloc - hdraddr);
688 					binarytable[bintabndx + 1] =
689 					    (uint_t)(fdeaddr - hdraddr);
690 				}
691 			}
692 
693 			/*
694 			 * the length does not include the length
695 			 * itself - so account for that too.
696 			 */
697 			off += length + 4;
698 		}
699 	}
700 
701 done:
702 	/*
703 	 * Do a quicksort on the binary table. If this is a cross
704 	 * link from a system with the opposite byte order, xlate
705 	 * the resulting values into LSB order.
706 	 */
707 	framehdr_addr = hdraddr;
708 	qsort((void *)binarytable, (size_t)fde_count,
709 	    (size_t)(sizeof (uint_t) * 2), bintabcompare);
710 	if (bswap) {
711 		uint_t	*btable = binarytable;
712 		uint_t	cnt;
713 
714 		for (cnt = fde_count * 2; cnt-- > 0; btable++)
715 			*btable = ld_bswap_Word(*btable);
716 	}
717 
718 	/*
719 	 * Fill in:
720 	 *	first_frame_ptr
721 	 *	fde_count
722 	 */
723 	hdroff = 4;
724 	/* LINTED */
725 	uint_ptr = (uint_t *)(&hdrdata[hdroff]);
726 	*uint_ptr = first_unwind->os_shdr->sh_addr -
727 	    (hdrosp->os_shdr->sh_addr + hdroff);
728 	if (bswap)
729 		*uint_ptr = ld_bswap_Word(*uint_ptr);
730 
731 	hdroff += 4;
732 	/* LINTED */
733 	uint_ptr = (uint_t *)&hdrdata[hdroff];
734 	*uint_ptr = fde_count;
735 	if (bswap)
736 		*uint_ptr = ld_bswap_Word(*uint_ptr);
737 
738 	/*
739 	 * If relaxed relocations are active, then there is a chance
740 	 * that we didn't use all the space reserved for this section.
741 	 * For details, see the note at head of ld_unwind_make_hdr() above.
742 	 *
743 	 * Find the PT_SUNW_UNWIND program header, and change the size values
744 	 * to the size of the subset of the section that was actually used.
745 	 */
746 	if (ofl->ofl_flags1 & FLG_OF1_RLXREL) {
747 		Word	phnum = ofl->ofl_nehdr->e_phnum;
748 		Phdr	*phdr = ofl->ofl_phdr;
749 
750 		for (; phnum-- > 0; phdr++) {
751 			if (phdr->p_type == PT_SUNW_UNWIND) {
752 				phdr->p_memsz = 12 + (8 * fde_count);
753 				phdr->p_filesz = phdr->p_memsz;
754 				break;
755 			}
756 		}
757 	}
758 
759 	return (1);
760 }
761 
762 /*
763  * Append an .eh_frame section to our output list if not already present.
764  *
765  * Usually, there is a single .eh_frame output section. However, there can
766  * be more if there are incompatible section flags on incoming sections.
767  * If this does happen, the frame_ptr field of the eh_frame_hdr section
768  * will point at the base of the first output section, and the other
769  * sections will not be accessible via frame_ptr. However, the .eh_frame_hdr
770  * will be able to access all the data in the different .eh_frame sections,
771  * because the entries in sorted table are all encoded as DW_EH_PE_datarel.
772  */
773 uintptr_t
774 ld_unwind_register(Os_desc *osp, Ofl_desc * ofl)
775 {
776 	Aliste	idx;
777 	Os_desc	*_osp;
778 	/*
779 	 * Check to see if this output section is already
780 	 * on the list.
781 	 */
782 	for (APLIST_TRAVERSE(ofl->ofl_unwind, idx, _osp))
783 		if (osp == _osp)
784 			return (1);
785 
786 	/*
787 	 * Append output section to unwind list
788 	 */
789 	if (aplist_append(&ofl->ofl_unwind, osp, AL_CNT_OFL_UNWIND) == NULL)
790 		return (S_ERROR);
791 
792 	return (1);
793 }
794